opencv-python读取webcam并保存为视频文件

这个其实有些项目可视化demo的时候都需要用到cv2(opencv-python),每次写这部分的时候又是一顿找博客,所以这里自己写一下,方便以后重复使用:

import cv2
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('C:\\Users\\26254\\Desktop\\save_webcam_video.avi', fourcc, 20.0, (640,  480))
while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
    # write frame
    out.write(frame)
    cv2.imshow('webcam', frame)
    # press key 'q' to quit
    if cv2.waitKey(1) == ord('q'):
        break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

opencv-python读取webcam并保存为视频文件_第1张图片

你可能感兴趣的:(opencv-python)