调用摄像头
import cv2
import numpy as np
def video_demo():
print('开始')
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
i = 0
photoname = 1
while True:
i = i + 1
reg, frame = cap.read()
frame = cv2.flip(frame, 1)
cv2.imshow('window', frame)
if i == 1440:
filename = str(photoname) + '.jpg'
cv2.imwrite('D:\mywork\JPG' + '\\' + filename, frame)
print(filename + '保存成功')
i = 0
photoname = photoname + 1
if photoname >= 60:
break
if cv2.waitKey(1) & 0xff == ord('q'):
break
cap.release()
video_demo()
cv2.destroyAllWindows()
将保存的图片保存为MP4文件
import cv2
import os
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
out = cv2.VideoWriter('D:\mywork\IMG\output.mp4', fourcc, 20, (640,480))
for i in range(1,60):
im_name = os.path.join('D:\mywork\JPG', str(i)+'.jpg')
print(im_name)
frame = cv2.imread(im_name)
out.write(frame)
out.release()