Ask Your Question
0

Lot of Delay with my RTSP cam with OpenCV on Python

asked 2020-12-10 12:23:38 -0600

Pinigse gravatar image

updated 2020-12-10 12:26:32 -0600

Good evening everyone.

I have some concerns regarding a project that I am setting up.

Indeed, when I display a simple Rtsp video stream via OpenCv, I have no problems. Everything is fluid. However I am using an haarcascaded face detection code and I have a lot of latencies and frames loss when i use it in my code. I am looking for some avenues to explore because I can't find solutions despite my research on the Net. I tried to change my camera but the problem is the same.

Below is the code:

import numpy as np
import cv2
from threading import Thread

class Algo(Thread):
    def __init__(self, frame):
        Thread.__init__(self)
        self.frame = frame

    def run(self):
        faces = face_cascade.detectMultiScale(gray, 1.3,5)

        for (x,y,w,h) in faces:
            cv2.rectangle(frame, (x,y), (x+y, y+h), (255,0,0), 2)
            roi_gray = gray[y:y+h, x:x+w]
            roi_color = frame[y:y+h, x:x+w] 
cap = cv2.VideoCapture('rtsp://[username]:[password]@[IP]:554/Streaming/Channels/1/')

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')

while(True):
# Capture frame-by-frame
cap.grab()
ret, frame = cap.retrieve()

# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

thread_1 = Algo(frame)
thread_1.start()
thread_1.join()

# Display the resulting frame
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
    break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

Regards, Pinigseu

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-12-10 13:14:55 -0600

crackwitz gravatar image

updated 2020-12-10 13:16:05 -0600

the camera will produce frames at its own constant rate. if you don't consume them promptly, they queue up. that is the delay you see.

use this. it will always give you the latest frame (but never twice unless you ask for that), and it will drop frames when you aren't consuming quickly enough.

https://gist.github.com/crackwitz/15c...

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-12-10 12:23:38 -0600

Seen: 6,940 times

Last updated: Dec 10 '20