Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Options to read webcam using Python/Linux

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!

like image 668
Michael Fryer Avatar asked Dec 18 '12 15:12

Michael Fryer


1 Answers

You could use gstreamer-0.10.

  1. Get it to work on the command line. e.g.: gst-launch -v v4l2src ! decodebin ! ffmpegcolorspace ! pngenc ! filesink location=out.png

  2. 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)
    
like image 66
kallaballa Avatar answered Nov 03 '22 01:11

kallaballa