Skip to content

Run Yolo8 in GPU #3084

@MahaKhh

Description

@MahaKhh

Search before asking

Question

Hi,

I tried to run yolo8 in GPU but it's not working.
I use torch to set the device to cuda but still not working on my GPU. The model I am using is PPE detection yolo8. I want to achieve fast reading and detection from the camera using rtsp and then sending the detection frame to rtmp server.

`
import cv2
from ultralytics import YOLO
import subprocess
import requests
import json
import random
import base64
from PIL import Image
import threading

import torch

torch.cuda.set_device(0)

Camera Stream

path = "rtsp://admin:deer2022@192.168.1.149:554/Streaming/Channels/101/"
cap = cv2.VideoCapture(path)

Load the YOLOv8 model

model = YOLO('best.pt')
classes= {0: 'Hardhat', 1: 'Mask', 2: 'NO-Hardhat', 3: 'NO-Mask', 4: 'NO-Safety Vest', 5: 'Person', 6: 'Safety Cone', 7: 'Safety Vest', 8: 'machinery', 9: 'vehicle'}

Loop through the video frames

while cap.isOpened():
# Read a frame from the video
success, frame = cap.read()

if success:
    # Run YOLOv8 inference on the frame
    results = model(frame)
    annotated_frame = results[0].plot()
    # Saving the image
    cv2.imwrite("test1.jpeg", annotated_frame)
    # Encode the resized annotated frame to base64


    # Display the annotated frame
    cv2.imshow("YOLOv8 Inference", annotated_frame)

    # Break the loop if 'q' is pressed
    if cv2.waitKey(1) & 0xFF == ord("q"):
        break
else:
    # Break the loop if the end of the video is reached
    break

def show_frame(frame):
cv2.imshow("YOLOv8 Inference", frame)

Release the video capture object and close the display window

cap.release()
cv2.destroyAllWindows()`

Additional

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    StaleStale and schedule for closing soonquestionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions