3

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

Community
  • 1
  • 1
Alex S
  • 4,726
  • 7
  • 39
  • 67

1 Answers1

4

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.

Abid Rahman K
  • 51,886
  • 31
  • 146
  • 157
  • One other question, would it be possible for me to crop that same image while saving? Ultimately I'm trying to crop faces that are detected, so I'd just have individual files of the faces. – Alex S Jul 07 '12 at 15:07
  • I have enough 'thanks' in my pocket. May be you can give your upvote. – Abid Rahman K Jul 07 '12 at 15:08
  • What you mean by cropping? cropping during video play or after saving the image? – Abid Rahman K Jul 07 '12 at 15:11
  • I want the output pictures to be cropped, so I'm not sure if the best way would be to save the file like is being done now, then open it again and crop it, or if I could just save a cropped version to start with. – Alex S Jul 07 '12 at 15:15
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13554/discussion-between-abid-rahman-k-and-alex-s) – Abid Rahman K Jul 07 '12 at 15:16
  • 3
    In some conditions(cropping etc.) cv.SaveImage("test.jpg",frame) gives error in cv2. Please use cv2.imwrite("test.jpg",frame) with cv2. – obayhan Feb 09 '16 at 09:42