1

我正在尝试编写一个代码来使用 opencv 2.2 和 python 2.7 从网络摄像头录制视频。我得到了一个似乎给我错误的代码。

代码:

import cv, sys

cap = cv.CaptureFromCAM(0)   # 0 is for /dev/video0
if not cap:
    sys.stdout.write("!!! Failed CaptureFromCAM")
    sys.exit(1)

frame = cv.RetrieveFrame(cap)
if not frame: 
    sys.stdout.write("!!! Failed to retrieve first frame")
    sys.exit(1)


fps = 25.0      # so we need to hardcode the FPS
print "Recording at: ", fps, " fps"  

frame_size = cv.GetSize(frame)
print "Video size: ", frame_size  

writer = cv.CreateVideoWriter("out.mp4", cv.CV_FOURCC('F', 'M', 'P', '4'), 
                              fps, frame_size, True)
if not writer:
    sys.stdout.write("!!! Error in creating video writer")
    sys.exit(1)


while True :
    if not cv.GrabFrame(cap) : 
        break
    frame = cv.RetrieveFrame(cap)
    cv.WriteFrame(writer, frame)

cv.ReleaseVideoWriter(writer)
cv.ReleaseCapture(cap)

错误是:

!!! Failed to retrieve first frame Traceback (most recent call last): 
File "C:\Users\Acer\Desktop\WORKING PROGRAMS\mp4.py", line 11, in
<module>
    sys.exit(1) SystemExit: 1

当我尝试在没有任何错误的情况下运行 python 程序时,我经常在我的 Windows 7 机器上遇到这样的错误(图像) 。

4

0 回答 0