Opencv+Python 捕获摄像头的帧

捕获摄像头10秒钟的视频:

import cv2

cameraCapture = cv2.VideoCapture(0)  # 获取视频,未开始读取
fps = 30  # 此处VideoCapture.get(cv2.CAP_PROP_FPS)不能获取摄像头的帧速率,只返回0
size = (int(cameraCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),
        int(cameraCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))

videoWrite = cv2.VideoWriter(r"D:\OpencvTest\camera.avi",
                             cv2.VideoWriter_fourcc("I","4","2","0"), fps, size)

success, frame = cameraCapture.read()
numFramesRemaining = 10 * fps - 1  # 10秒 * fps - 上行代码已读一帧

while success and numFramesRemaining > 0:
    videoWrite.write(frame)
    success, frame = cameraCapture.read()
    numFramesRemaining -= 1  # 每读一帧减一

cameraCapture.release()


你可能感兴趣的:(Opencv+Python)