Options to read webcam using Python/Linux
Solution 1
You could use gstreamer-0.10.
Get it to work on the command line. e.g.: gst-launch -v v4l2src ! decodebin ! ffmpegcolorspace ! pngenc ! filesink location=out.png
Use the parse_launch function to get a shortcut to a working pipeline in you python code.
import gst pipeline = gst.parse_launch(""" v4l2src ! decodebin ! ffmpegcolorspace ! pngenc ! filesink location="%s" """ % sys.argv[-1]) pipeline.set_state(gst.STATE_PLAYING)
Solution 2
I have tried several methods to capture single frames from a webcam:
uvccapture is one option and here's a command:
uvccapture -d /dev/video0 -o outfile.jpg
streamer is another and the command looks about like this:
streamer -c /dev/video0 -o outfile.jpeg
Yes I realize that this isn't the most high performance since you have to use Python's "command" module to execute this command and get the results, and then open up the resulting file in OpenCV to do processing.
BUT it does work. I've been using it in production on several automation projects quite successfully. The lag I experience is all based on my image processing software, raw images can be displayed VERY quickly.
Michael Fryer
Updated on June 18, 2022Comments
-
Michael Fryer almost 2 years
I am looking to find a way to integrate a webcam into my python program.
I am running on a Raspberry Pi Model A OC'd to 900mHz, so the solution will need to be ARM compatible and (hopefully) lightweight.
Most posts I have seen recommend using the OpenCV module to read the webcam, but I am unable to get anything but a black frame to appear from my webcam. I assume that OpenCV is not compatible with my webcam. However, every other webcam application available for linux can detect and display the feed from my webcam.
I am wondering if there are any other lightweight or simple methods for capturing from my webcam using python. Perhaps a way that I could directly interface with the video0 device that comes up under /dev/ for my webcam? I am open to any suggestions; because what I am doing now, is not working.
Thanks
(as requested):
Output of
v4l2-ctl --all
:Driver Info (not using libv4l2): Driver name : uvcvideo Card type : UVC Camera (046d:081b) Bus info : usb-bcm2708_usb-1.2 Driver version: 3.2.27 Capabilities : 0x04000001 Video Capture Streaming Format Video Capture: Width/Height : 640/480 Pixel Format : 'YUYV' Field : None Bytes per Line: 1280 Size Image : 614400 Colorspace : SRGB Crop Capability Video Capture: Bounds : Left 0, Top 0, Width 640, Height 480 Default : Left 0, Top 0, Width 640, Height 480 Pixel Aspect: 1/1 Video input : 0 (Camera 1: ok) Streaming Parameters Video Capture: Capabilities : timeperframe Frames per second: 30.000 (30/1) Read buffers : 0
And this is the code snippet I'm using:
import cv cv.NamedWindow("camera", 1) capture = cv.CaptureFromCAM(0) while True: img = cv.QueryFrame(capture) cv.ShowImage("camera", img) if cv.WaitKey(10) == 27: break cv.DestroyWindow("camera")
Thanks for your help!