简体   繁体   中英

Video Capture with Python

I'm trying to write some code for automatically capturing video from a webcam when activated through a batch script on Windows. I've managed to piece together a working script, but it does not appear to be saving the file. I know the code is (at least on a basic level) working because there are no errors and the activation light on the webcam lights up when the code is run. I've reproduced the code below if anyone has any suggestions on how to get it writing to a file, that would be great!

import numpy as np
import cv2
import msvcrt

cap = cv2.VideoCapture(0)
w=int(cap.get(cv2.CAP_PROP_FRAME_WIDTH ))
h=int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT ))
sub=raw_input("Subject#: ")

#Define the codec and create VideoWriter object
#fourcc = cv2.VideoWriter_fourcc(*'DIVX')
fourcc = cv2.VideoWriter_fourcc(*'FMP4')
out = cv2.VideoWriter('C:\path\to\output_' + sub + '.mp4', fourcc, 30, (w,h))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:

        out.write(frame)
        if msvcrt.kbhit():
            if ord(msvcrt.getch()) != None:
                break

    else:
        break

#Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()   

The codec FMP4 is just not being supported by your camera I think. Neither in mine. If I change in your code the encoding to 'MJPG' and write to an '.avi' extension, it works very well.

PS Wanted to make a comment but I'm still underage (< 50).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM