Getting Webcam Images with Python and OpenCV 2 (For Real This Time)

Alright, so my post Getting Webcam Images with Python and OpenCV 2 was wrong! I did not fully understand how to read the OpenCV API documentation and instead of demonstrating how to capture an image with OpenCV 2, I just demonstrated a way to capture an image with OpenCV 1.x in a way different from the example I first followed. Here is how one would actually use the newer, OO, consolidated OpenCV 2 Python API.

import cv2

# Camera 0 is the integrated web cam on my netbook
camera_port = 0

#Number of frames to throw away while the camera adjusts to light levels
ramp_frames = 30

# Now we can initialize the camera capture object with the cv2.VideoCapture class.
# All it needs is the index to a camera port.
camera = cv2.VideoCapture(camera_port)

# Captures a single image from the camera and returns it in PIL format
def get_image():
 # read is the easiest way to get a full image out of a VideoCapture object.
 retval, im = camera.read()
 return im

# Ramp the camera - these frames will be discarded and are only used to allow v4l2
# to adjust light levels, if necessary
for i in xrange(ramp_frames):
 temp = get_image()
print("Taking image...")
# Take the actual image we want to keep
camera_capture = get_image()
file = "/home/codeplasma/test_image.png"
# A nice feature of the imwrite method is that it will automatically choose the
# correct format based on the file extension you provide. Convenient!
cv2.imwrite(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
del(camera)
Advertisements

21 thoughts on “Getting Webcam Images with Python and OpenCV 2 (For Real This Time)

  1. Tim

    This program works beautifully. I was wondering, however, how you would go about adding a wait before the picture is taken. I attempted to add a “time.sleep()” but am still having issues. What I would like to accomplish is have the webcam window show the live webcam feed for 5 or 10 seconds, and then take the picture. How the program is now, it takes the picture immediately. Thank you very much.

    Reply
    1. Julius Post author

      This got a little lost in my email, very sorry for not replying! I’m pretty busy for the next few days but I’ll try to get back to you shortly on this.

      Reply
  2. Wilson

    Some comments:

    In line 15, 16, 17, and 22 I replaced the initial space for a tab

    In Windows, I had to escape the backslash character:
    file = r”C:\Users\Me\test_image.png”

    Reply
      1. daniel

        thanks for that,but the only thing i get is a black picture,whats the reason for that?

  3. Pingback: Getting Webcam Images with Python and OpenCV 2 (For Real This Time) | Yoann's Blog

  4. Sounak Ranjan Das

    Hi, I am getting an error like
    HIGHGUI ERROR: V4L/V4L2: VIDIOC_S_CROP
    select timeout
    select timeout
    Segmentation fault

    Can you help me on this matter?

    Reply
  5. Shreya

    Hi!

    How do I save multiple frames?
    This code runs perfectly but saves only one image. I want multiple frames.

    Thanks.

    Reply
  6. Pingback: Python:Capture image for processing – IT Sprite

  7. SRINIDHI

    Hello Sir,
    I am using windows 8, python version 2.7 when i try the following program i get an output screen which is gray in colour, it gives no image as an output.
    I even tried to add other functions like cv2.startWindowThread() but it seems to be of no use
    Please help me.

    Reply
  8. Swathi

    Hi Julius!
    I am new to openCV.I tried this example and it is working perfectly.I do not understand this part of code “Ramp the camera.”
    # Ramp the camera – these frames will be discarded and are only used to allow v4l2
    # to adjust light levels, if necessary
    for i in xrange(ramp_frames):
    temp = get_image()

    Can you please brief on it. Also if I remove the above for-loop , the captured image is just a black window. Appreciate your help.

    Reply
  9. Pingback: Plant Project – Stats Monitor – Michael Reyes

  10. Julius Post author

    Hi all – I just logged on for the first time in several years and saw all of these comments. I really appreciate all of the input and questions, however I have not maintained this website or programmed regularly in several years and will not be responding to any of the questions. I encourage you to keep on working on any problems you are encountering and to modify and reuse any of my code that you find useful. Thank you so much for visiting my site!

    Reply

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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