OpenCV从摄像头获取视频流并保存到本地

用OpenCV调取摄像头获取视频流,图片经过处理后保存到本地。

#coding:utf-8
import cv2 
    
# 获取视频流
cap = cv2.VideoCapture(0)
# 指定编码格式
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
# cv2.VideoWriter的参数分别为:保存路径,编码格式,帧率,帧大小
out = cv2.VideoWriter("rest.avi", fourcc, 20.0, (640,480))

while(cap.isOpened()):
    ret, frame = cap.read()
    
    if ret==True:
        # 如果帧的大小与上述的(640, 480)不一致,需要resize
        out.write(frame)

        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

cap.release()
out.release()
cv2.destroyAllWindows()

猜你喜欢

转载自blog.csdn.net/Guo_Python/article/details/105821786