这是初步获取摄像头的帧流。
#初步知识:
1、 VideoCapture类和VideoWriter类的格式
cv2.VideoWriter_fourcc('I', '4', '2', '0')
cv2.VideoWriter_fourcc('P', 'I', 'M', '1')
cv2.VideoWriter_fourcc('X', 'V', 'I', 'D') 三个的拓展名都是:.avi
cv2.VideoWriter_fourcc('T', 'H', 'E', 'O') 拓展名:.ogv
cv2.VideoWriter_fourcc('F', 'L', 'V', '1') 拓展名:.fly
2、
执行VideoCapture.isOpened 函数可以返回一个Boolean值,判断read()函数是否获取到数据。
代码:
“”“本次测试,将用摄像头捕获10秒的视频信息,并写入一个AVI文件中”""
import cv2
cameraCapture = cv2.VideoCapture(0)
fps = 30 # assumption
size = (int(cameraCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(cameraCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
videoWriter = cv2.VideoWriter(
‘Output.avi’,cv2.VideoWriter_fourcc(‘I’, ‘4’, ‘2’, ‘0’),
fps, size)
success, framem = cameraCapture.read()
numFramesReamining = 10 * fps - 1
while success and numFramesReamining > 0:
videoWriter.write(framem)
success, framem = cameraCapture.read()
numFramesReamining -= 1
cameraCapture.release() # 记得每次使用摄像头后进行关闭动作。