使用OpenCV和Python拍摄并保存多个图像和视频

时间:2019-03-07 06:32:20

标签: python image opencv video

我正在使用OpenCV和Python拍摄图像和视频。单击键盘上的某些特定字母时,我希望OpenCV可以拍摄多张图片和视频。但是目前,我只能拍摄多张图像,不能同时拍摄视频。这是我当前的代码:

import numpy as np
import cv2, time
video=cv2.VideoCapture(0)
a=0
i=0

while True:
    a = a + 1
    check, frame = video.read()
    cv2.imshow("Capturing", frame)
    key=cv2.waitKey(1) 

    if key == ord ('r'):
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
        out.write(frame)
    if key == ord ('x'):
        i+=1
        cv2.imwrite('image'+str(i)+'.jpg', frame)
        cv2.imshow("Hasil Capture", frame)
        print('taking pictures')
    if key == ord ('q'):
        break


print(a)
video.release()
cv2.destroyAllWindows

0 个答案:

没有答案