Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV-Python: How to get latest frame from the live video stream or skip old ones

I've integrated an IP camera with OpenCV in Python to get the video processing done frame by frame from the live stream. I've configured camera FPS as 1 second so that I can get 1 frame per second in the buffer to process, but my algorithm takes 4 seconds to process each frame, causing stagnation of unprocessed frame in the buffer, that keeps growing by time & causing exponentially delay. To sort this out, I have created one more Thread where I'm calling cv2.grab() API to clean the buffer, it moves pointer towards latest frame in each call. In main Thread, I'm calling retrieve() method which gives me the last frame grabbed by the first Thread. By this design, frame stagnation problem is fixed and exponential delay is removed, but still constant delay of 12-13 seconds could not be removed. I suspect when cv2.retrieve() is called its not getting the latest frame, but the 4th or 5th frame from the latest frame. Is there any API in OpenCV or any other design pattern to get this problem fixed so that I can get the latest frame to process.

like image 613
Hemant Mahsky Avatar asked Jul 25 '17 18:07

Hemant Mahsky


1 Answers

If you don't mind compromising on speed. you can create a python generator which opens camera and returns frame.

def ReadCamera(Camera):
    while True:
        cap = cv2.VideoCapture(Camera)
        (grabbed, frame) = cap.read()
        if grabbed == True:
            yield frame

Now when you want to process the frame.

for frame in ReadCamera(Camera):
      .....

This works perfectly fine. except opening and closing camera will add up to time.

like image 142
amitnair92 Avatar answered Nov 17 '22 04:11

amitnair92