Getting Webcam Images with Python and OpenCV 2

UPDATE: This is wrong! The example in this post still uses the older 1.x OpenCV classes, but a little differently than Joseph Perla’s example. I will update soon with a real example of OpenCV 2.x camera usage in the next day or two.

I have a need to capture images programmatically for a personal project I’m working on. I’m using Python, and the best way to grab frames from a webcam with Python seems to be OpenCV. It looks pretty amazing, way more than what I would need to just capture images, but it definitely seems like a great library to get to know, so why not start here?

I first began working with this excellent guide by Joseph Perla. I managed to grab some frames and thought it was going great. Then, after a reboot, the opencv library vanished from my system. No matter what I did, I was unable to “import opencv.” Installing the binaries from the package manager didn’t help, compiling from source didn’t provide that interface, and several hours of Googling didn’t help either. Still have no idea how that happened!

Anyways,  Mr. Perla’s example uses a now deprecated Python interface, so I ended up just rewriting my little bit of code using the newer 2.1+ documentation and the current, unified interface.

To run this you just need Python (I’m using 2.7), OpenCV (python-opencv using apt-get on Ubuntu), and some sort of webcam. Hope this helps someone else who wants to get started taking images with their camera using Python! A big thanks goes to Joseph Perla for getting me started. As with everything on this site, feel free to use it however you would like.

import sys
import cv
import time

# Camera 0 is the integrated web cam on my netbook
camera_port = 0
# FPS to use when ramping the camera
fps = 30
# Number of frames to take during camera ramp
ramp_frames = 30

# Now we can set up the camera with the CaptureFromCAM() function. All it needs is
# the index to a camera port. The 'camera' variable will be a cv.capture object
camera = cv.CaptureFromCAM(camera_port)

# Captures a single image from the camera and returns it in IplImage format
def get_image():
    # QueryFrame is the easiest way to get a full image out of a capture object
    im = cv.QueryFrame(camera)
    return im

# I have found that my camera needs to capture a few frames to adjust to the
# ambient light levels. Just call this once at the beginning of each capture
# session. Probably could use tweaking or even complete omission depending on
# your camera
def ramp_camera(fps, num_frames):
    print("Ramping camera at " + str(fps) + " FPS for "
             + str(num_frames) + " frames...\n")
    while num_frames > 0:
    # Don't need to actually save these images
    im = get_image()
    num_frames = num_frames - 1

# Ramp the camera, capture an image, and save it as a PNG file
ramp_camera(30, 30)
print("Taking image...")
camera_capture = get_image()
file = "/home/codeplasma/test_image.png"
# A nice feature of the SaveImage method is that it will automatically choose the
# correct format based on the file extension you provide. Convenient!
cv.SaveImage(file, camera_capture)

# You'll want to release the camera, otherwise you won't be able to create a new
# capture object until your script exits

Of course, as always, feedback and questions are welcome!

One thought on “Getting Webcam Images with Python and OpenCV 2

  1. Pingback: Getting Webcam Images with Python and OpenCV 2 (For Real This Time) | codeplasma

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s