Sunday, September 30, 2012

Ultra-short Webcam Capture Script

I think that the below is the shortest possible Python script to capture and then save an image from a webcam:

    import cv
    capture = cv.CaptureFromCAM(-1)
    frame = cv.QueryFrame(capture)
    cv.SaveImage( "Test.png", frame)


The -1 on the second line works if you only have one webcam otherwise you will have to specify the one you want to capture from...and since they move around...you have to add logic for that as well.

This is the basics of what I need but I have been chasing a problem with the webcam on the RPi for a while.   Namely that the first image is exposed correctly and all subsequent images are extremely under exposed.   This persists until the RPi is rebooted or the webcam unplugged and plugged back in.

I have tried unbinding/rebinding the device drivers thinking this would accomplish the same thing as the unplug/replug but to no avail.  The webcam that I am using works on Ubuntu and another webcam that I borrowed from my wife works through multiple exposures on the RPi. 

Obviously I can get another webcam but I do have a problem that you can see to the right.   I seriously doubt that my wife will happily watch me disassemble her webcam for this project!

No comments:

Post a Comment