PyImageSearch / imutils

A series of convenience functions to make basic image processing operations such as translation, rotation, resizing, skeletonization, and displaying Matplotlib images easier with OpenCV and Python.
MIT License
4.54k stars 1.03k forks source link

Is there a way of reading previous frame in VideoCapture ? #160

Closed sbmalik closed 5 years ago

sbmalik commented 5 years ago

I am trying to grab previous frame of video stream because i am using the hogdetector and want to only update the result when len(rects) > 0 or len(rects) == 0, if length of rects remain same then there is no need to update result(display) or print. I want to grab the previous frame and want to compare it with current frame so i can check either length of rects changed or not.

KD1994 commented 5 years ago

I am trying to grab previous frame of video stream because i am using the hogdetector and want to only update the result when len(rects) > 0 or len(rects) == 0, if length of rects remain same then there is no need to update result(display) or print. I want to grab the previous frame and want to compare it with current frame so i can check either length of rects changed or not.

I don't think so there is a single function/method available from OpenCV which can give you previous frame., but what you can do is store the frame while reading it so that you can use it later. But if you come across something else kindly share that as well.

e.g.

cap = cv2.VideoCapture(add your input source here)
ret, frame = cap.read()

while(cap.isOpened():
    prev_frame = frame
    ret, frame = cap.read()
    if not ret:
        break
    whatever you want to do with it...
jrosebr1 commented 5 years ago

This sounds like the perfect use case for the deque data structure. Example can be found here:

https://www.pyimagesearch.com/2015/09/21/opencv-track-object-movement/