Past ten frame retrieval from a webcam

asked 2015-02-09 07:55:16 -0500

reggie gravatar image

I have a Python/Opencv2 security camera application that detects movement from a live feed. When a detection is triggered I want to be able to save the last ten frames pria to detection, the detected frames and ten frames following detection as jpgs to a specified folder.

I'm not too sure on how to do this, but would a constantly updating "one in" and "one out" Python frame array be the way to go?

The user could specify to save 10 frames pria, and say 10 frames post detection. Then the frame array would have to constantly store the last 10 frames (frames as a numpy array). Then following detection expand to the detected frames and the user specified post detection number of frames.

Once detection is finished, and the code has added the user specified number of post frames into the array, the array could be emptied, converted to jpgs, and saved to a specified folder.

The array would not be totally emptied, it would always need to contain the pre specified number of frames, just in case of a detection quickly after the first detection.

1) Does this make sense and sound like a valid approach? 2) How can I save a numpy array into a Python array, in effect an array of arrays? 3) Could someone point me towards some opencv2 and python code that does something similar, so I can have a look?

edit retag flag offensive close merge delete


I have made some progress using the class deque (

from collections import deque
max_qlength = 10
q = deque ([0,max_qlength])

def save_frame_deque(picture, max_qlen):

    max_qlength = max_qlen


    #print(len(q))# actual length of q  
    #print (max_qlength)# max length of q

    if len(q) >  (max_qlength+1): # if actual length of q is bigger than5

        cv2.imshow("frame", q.popleft())
        #cv2.imwrite('C:/Users/reg/Desktop/frame_1.png', q.popleft())
        #cv2.imwrite('C:/Users/reg/Desktop/frame_2.png', q.popleft())

call using save_frame_deque(frame,5)

reggie gravatar imagereggie ( 2015-02-11 08:39:53 -0500 )edit

frame is a numpy array that is pushed into the q. see the below code on how i get the frame.

got_frame, frame =
reggie gravatar imagereggie ( 2015-02-11 08:47:16 -0500 )edit