Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

if you really meant "few seconds", you can use a ringbuffer, or python's builtin Queue for this. (else it will need far too much memory, and then you should write short videos to disk instead):

import cv2
import numpy as np

import queue # Queue in 2.7 !


def main():
    fps = 25
    nseconds = 5 # delay
    ring = queue.Queue(fps * nseconds)

    cap = cv2.VideoCapture(0)
    if not cap.isOpened():
        print("capture did not open!")
        return

    while(True):
        ret,frame = cap.read()
        if not ret: ## someone stepped on the cable ?
            print("no frame")
            break

        ring.put(frame)
        if ring.full():
            src = ring.get()
            cv2.imshow('src', src)

        k = cv2.waitKey(int(1000.0/fps))
        if k==27: break ## esc. pressed

if __name__ == "__main__":
    main()

(try it anyway, it's a fun prog !)