如何设置实时视频捕捉的时间限制?

时间:2016-09-22 09:59:38

标签: python python-2.7 video image-processing

我有从摄像头捕获视频的代码。捕获的帧会附加到列表中。但是如何设置此捕获的时间限制?。我只想捕获录制必须停止后的前两分钟。我的代码是

import cv2
import numpy

#creating video capture object
capture=cv2.VideoCapture(0)

#Set the resolution of capturing to 640W*480H
capture.set(3,640)
capture.set(4,480)
frame_set=[]
while(True):
    # Capture frame-by-frame
    ret, frame = capture.read()

    # Converting to Gray Scale
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    frame_set.append(gray)
    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
capture.release()
cv2.destroyAllWindows()

1 个答案:

答案 0 :(得分:1)

使用时间套餐

import cv2
import numpy
import time
capture=cv2.VideoCapture(0)
capture.set(3,640)
capture.set(4,480)
frame_set=[]
start_time=time.time()
while(True):
    ret, frame = capture.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    frame_set.append(gray)
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    end_time=time.time()
    elapsed = end_time - start_time
    if elapsed > 120:
       break
 capture.release()
 cv2.destroyAllWindows()