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)
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.
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.
Working and very well explained =)
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”
Oh good to know, thank you for posting!
How to take picture from webcam in C++? Plz suggest. I am a beginner
Hi Huma, I’m sorry that I don’t think I’ll be able to offer much help, I haven’t used c++ in many years. I did find this though: https://gist.github.com/pklaus/3773505#file-video-cpp. Sorry I can’t offer more help!
thanks for that,but the only thing i get is a black picture,whats the reason for that?
Pingback: Getting Webcam Images with Python and OpenCV 2 (For Real This Time) | Yoann's Blog
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?
Hi!
How do I save multiple frames?
This code runs perfectly but saves only one image. I want multiple frames.
Thanks.
Pingback: Python:Capture image for processing – IT Sprite
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.
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.
how do we display one single frame
if i want to use two usb_cameras, how to edit ‘camera_port’?
This program works very well for cameras under bright light also can you please elaborate me how this works. Thanks In Advance
how to find the camera port used by an external webcam?
Pingback: Plant Project – Stats Monitor – Michael Reyes
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!
Works!!! THANKS😀