0

I try to use Yolo to detect car like this code. It have no error when I use VideoCapture with mp4. when I change to RTSP it can run about 5 Second then program auto stop running . It's not show error.

cap = cv2.VideoCapture("rtsp:// url ")
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))

model = models.get('yolo_nas_s', pretrained_weights="coco").to(device)

count = 0

classNames = ["car"]

limitup = [103, 161, 296, 161]

skip_frames = 3

while True:

    ret, frame = cap.read()
    count += 1
    if ret:
        detections = np.empty((0, 5))

        result = list(model.predict(frame, conf=0.3))[0]

        bbox_xyxys = result.prediction.bboxes_xyxy.tolist()
        confidences = result.prediction.confidence
        labels = result.prediction.labels.tolist()
        
        resize_frame = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)

        cv2.imshow("Frame", frame)
        if cv2.waitKey(1) & 0xFF == ord('1'):
            break
        
    else:
        break

cap.release()
cv2.destroyAllWindows()

If I comment at line result = list(model.predict(frame, conf=0.3))[0] it can run normal. How to fix this code to run by not auto stop running.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
user572575
  • 1,009
  • 3
  • 25
  • 45
  • your code throttles the stream because the AI processing slows it down below real-time. this will eventually fail. you must handle all the frames as quickly as they are produced: apply AI or drop the frame. this is a common problem. – Christoph Rackwitz Jul 03 '23 at 19:50

0 Answers0