3
votes

I'm trying to use opencv to save frames from a webcam as jpgs or pngs or whatever. It's proving more difficult than I'd thought, despite having examples like the one here: Capturing a single image from my webcam in Java or Python

I am trying to do this:

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) 

As you can see I've tried to make it capture an image when I press the letter c, using a modification of the code suggested by Froyo in that other question. It doesn't work and I can't find documentation to make it work.

Please help! Thanks a lot, Alex

1

1 Answers

4
votes

Change your saving section as follows :

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

It works good for me. Since you have already captured the frame for detection, you need to capture it again to save this.

Also cv.CaptureFromCam() and cv.VideoCapture() are different. They are not to be mixed up.