Multiple rtsp ip camera stream on raspberry using python opencv lagging and increasing delay

Question:

I want to analyse image from IP cameras (from 2 to 6 cameras) with a Raspberry 3b+.

I’m using python opencv but there is a increasing delay (as the video was in x0.5 so the delay accumulates)

from threading import Thread
import cv2, time

class ThreadedCamera(object):
    def __init__(self, src=0):
        self.capture = cv2.VideoCapture(src)
        self.capture.set(cv2.CAP_PROP_BUFFERSIZE, 2)
       
        # FPS = 1/X
        # X = desired FPS
        self.FPS = 1/30
        self.FPS_MS = int(self.FPS * 1000)
        
        # Start frame retrieval thread
        self.thread = Thread(target=self.update, args=())
        self.thread.daemon = True
        self.thread.start()
        
    def update(self):
        while True:
            if self.capture.isOpened():
                (self.status, self.frame) = self.capture.read()
            time.sleep(self.FPS)
            
    def show_frame(self):
        cv2.imshow('frame', self.frame)
        cv2.waitKey(self.FPS_MS)

if __name__ == '__main__':
    src = 'rtsp://user:[email protected]:554/Streaming/Channels/1401'
    threaded_camera = ThreadedCamera(src)
    while True:
        try:
            threaded_camera.show_frame()
        except AttributeError:
            pass

I try without the FPS logic and the result is the same (I try to reduce FPS and it doesnt work). I don’t need 30 FPS but I want minimum 3 FPS.

What can I do ? Is there an good alternative to opencv ? Do I have to use another language

Asked By: Luc Lagarde

||

Answers:

You have to remove the time.sleep after cv2.capture.

I don’t know why but it works for me.

Answered By: Dorian