使用opencv python绑定将帧从网络摄像头保存到磁盘

时间:2012-07-07 06:03:54

标签: python opencv webcam

我正在尝试使用opencv将网络摄像头中的帧保存为jpgs或pngs等等。事实证明这比我想象的要困难,尽管有像这样的例子:Capturing a single image from my webcam in Java or Python

我正在尝试这样做:

if __name__ == "__main__":
print "Press ESC to exit ..."

# create windows
cv.NamedWindow('Raw', cv.CV_WINDOW_AUTOSIZE)
cv.NamedWindow('Processed', cv.CV_WINDOW_AUTOSIZE)

# create capture device
device = 0 # assume we want first device
capture = cv.CaptureFromCAM(0)
cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, 640)
cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, 480)


# check if capture device is OK
if not capture:
    print "Error opening capture device"
    sys.exit(1)

while 1:
    # do forever

    # capture the current frame
    frame = cv.QueryFrame(capture)
    if frame is None:
        break

    # mirror
    cv.Flip(frame, None, 1)

    # face detection
    detect(frame)

    # display webcam image
    cv.ShowImage('Raw', frame)

    # handle events
    k = cv.WaitKey(10)

    if k == 0x1b: # ESC
        print 'ESC pressed. Exiting ...'
        break

    if k == 0x63 or k == 0x43:
        print 'capturing!'
        s, img = capture.read()
        if s:
            cv.SaveImage("r'C:\test.jpg", img) 

正如你所看到的那样,当我按下字母c时,我试图让它捕获图像,使用Froyo在其他问题中建议的代码修改。它不起作用,我找不到文档来使它工作。

请帮忙! 非常感谢, 亚历

1 个答案:

答案 0 :(得分:3)

按如下方式更改保存部分:

if k == 0x63 or k == 0x43:
    print 'capturing!'
    cv.SaveImage("test.jpg",frame) 

对我有用。由于您已经捕获了帧以进行检测,因此需要再次捕获它以保存它。

cv.CaptureFromCam()和cv.VideoCapture()也不同。他们不要混在一起。