攝像頭臉部識別 (1)opencv 抓取視頻數據並保存

攝像頭臉部識別 (1)opencv 抓取視頻數據並保存

基於python 和 opencv 3.4.0 (兼容 opencv 2.X 參考註釋),詳細如代碼python

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()
相關文章
相關標籤/搜索