python - 如何设置实时视频捕获的时间限制?

标签 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()

最佳答案

使用时间包

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()

关于python - 如何设置实时视频捕获的时间限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39635970/

相关文章:

python - 无法在 mac 上的 python 中导入 mysqldb

windows - 在 python 上使用 ctypes 不显示多个消息框

matlab - MPEG4 质量保存在 MATLAB 中

python - 如何使用 ponyorm 中的实体进行 get() 查询

python - django 表单为关键字参数获取了多个值

python - Xor 加密/解密 Python 2.7.5

javascript - HTML 视频播放器时间隐藏控件

Firefox 说 ffmpeg 中从 mkv 转换为 mp4 的某些文件已损坏

python - flask 属性错误 : 'NoneType' object has no attribute 'request'

python - PyQt QThreads 通信