DYF
我思故我在!

摄像头脸部识别 (1)opencv 抓取视频数据并保存

基于python 和 opencv 3.4.0 (兼容 opencv 2.X 参考注释),详细如代码


import numpy as np
import cv2

# 从文件打开视频
#videoFile = "test.mp4"
#capture = cv2.VideoCapture(videoFile)
#从摄像头获取视频
capture = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
#opcv3.x
#从视频文件打开可以获取fps
#fps = capture.get(cv2.CAP_PROP_FPS)
fps =  30
size = (int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)),
        int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
#opencv2.x
#fps = capture.get(cv2.cv.CV_CAP_PROP_FPS)
#size = (int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)),
#        int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)))


out = cv2.VideoWriter('output.avi',fourcc, fps, size)

print("Video size:",size)
imageindex = 0;
while(capture.isOpened()):
    ret, frame = capture.read()
    if ret==True:
        out.write(frame)
        cv2.imshow('frame',frame)
        cv2.imwrite('image%s.jpg'%(str(imageindex)),frame)
        imageindex +=1
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        print("Read video frame failed!")
        break

# Release everything if job is finished
capture.release()
out.release()
cv2.destroyAllWindows()

posted on 2018-01-04 18:10  o(∩_∩)o...  阅读(1059)  评论(0编辑  收藏  举报