Real-time detection using YOlOv5

I want to perform real-time detection using Parrot Anafi and YOLO v5. I’m using rtsp, but I get the error “Failed to open rtsp: // …”. The log is shown below. Please let me know if anyone has been able to resolve this error. Thank you.

and after whit threating

from time import sleep
from matplotlib.image import imread
import torch
import cv2
import threading
import os

RTSP_URL =‘rtsp://192.168.42.1/live’
os.environ[‘OPENCV_FFMPEG_CAPTURE_OPTIONS’]=“rtsp_transport;udp”

Model

model = torch.hub.load(‘ultralytics/yolov5’, ‘yolov5s’, pretrained=True)

THICKNESS = 2
COLOR = (255, 0, 0)

make a thread for finding human

class MyThread (threading.Thread):
def init(self):
threading.Thread.init(self)
self.result = None
self.frame = None
self.stop = False

def run(self):
model = torch.hub.load(r’/home/fabien/.cache/torch/hub/ultralytics_yolov5_master’,‘custom’,path=r’/home/fabien/projetS8/drone/yolov5s.pt’,source=‘local’)
while not self.stop:
# wait if no frame
if self.frame is None:
sleep(0.01)
continue

        # Inference
        results = model([self.frame]).xyxy[0]

        # take one people
        for row in results:
            if not row[-1]:
                self.result=tuple(row.int().numpy()[:-2])
                self.frame = None

open camera and check if the webcam is opened correctly

cap = cv2.VideoCapture(RTSP_URL)
if not cap.isOpened():
raise IOError(“Cannot open webcam”)

create thread and start

myThread = MyThread()
myThread.start()

while True:
# get next frame
ret, frame = cap.read()

# send new frame 
if myThread.frame is None:
    myThread.frame = frame

# draw rectangle
if myThread.result is not None:
    frame = cv2.rectangle(frame, myThread.result[:2], myThread.result[2:], COLOR, THICKNESS)

# show the frame
cv2.imshow('Input', frame)

# read escape key 
c = cv2.waitKey(1)
if c == 27:
    myThread.stop=True
    break

close webcam

cap.release()
cv2.destroyAllWindows()

1 Like

after that I take an image out of 4 and I can integrate my image processing with a counter and I use pcmd movements. I would like to use import Queue but it’s still complicated for me

Thank you for your reply.
The code you gave me worked fine.
However, there is a phenomenon that the image is delayed and roughened.
Therefore, the detection accuracy is low.
Is such a phenomenon occurring in your environment?

This topic was automatically closed after 30 days. New replies are no longer allowed.