webcam (usb) access under Ubuntu

yoz yoz at home.havin.us
Wed Apr 16 08:26:04 EDT 2008


Berco Beute wrote:
> I've been trying to access my webcam using Python, but I failed
> miserably. The camera works fine under Ubuntu (using camora and
> skype), but I am unable to get WebCamSpy or libfg to access my webcam.
> 
> First I tried webcamspy (http://webcamspy.sourceforge.net/). That
> requires pySerial and pyParallel, and optionally pyI2C. Runing
> WebCamSpy results in:
> 
> Exception exceptions.AttributeError: "Parallel instance has no
> attribute '_fd'" in <bound method Parallel.__del__ of
> <parallel.parallelppdev.Parallel instance at 0x83326ac>> ignored
> 
> This seems to come from importing I2C. The application window opens,
> but there's an error message:
> 
> NO VIDEO SOURCE FOUND
> 
> Next I tried libfg (http://antonym.org/libfg). I built it, made the
> Python bindings and installed it. Unfortunately the following:
> 
>>>> import fg
>>>> grabber = fg.Grabber()
> 
> results in:
> 
> fg_open(): open video device failed: No such file or directory
> 
> Since the camera works fine in Ubuntu itself my guess is that the
> problem is with the python libraries (or even likelier, my usage of
> them). Is there anybody here that was successful in accessing their
> webcam on linux using Python? Else I have to reside to Windows and
> VideoCapture (which relies on the win32 api and thus is Windows-only),
> something I'd rather not do.
> 
> Thanks for any help,
> 2B
> 
> ===============
> I am uUsing:
> WebCam: Logitech QuickCam Pro 400
> Ubuntu
> Python 2.5


Some time ago I was playing with writing a webcam server under Linux 
using V4L - I found this bit of code which may help (it works for me). 
Obviously it needs X running to work and the associated libs installed.
Note this is not my code but it was a good starting point for me to work 
from. I can't find a link to the original article but credit to the author.

import pygame
import Image
from pygame.locals import *
import sys

import opencv
#this is important for capturing/displaying images
from opencv import highgui

camera = highgui.cvCreateCameraCapture(0)
def get_image():
     im = highgui.cvQueryFrame(camera)
     #convert Ipl image to PIL image
     return opencv.adaptors.Ipl2PIL(im)

fps = 30.0
pygame.init()
window = pygame.display.set_mode((320,240))
pygame.display.set_caption("WebCam Demo")
screen = pygame.display.get_surface()

while True:
     events = pygame.event.get()
     for event in events:
         if event.type == QUIT or event.type == KEYDOWN:
             sys.exit(0)
     im = get_image()
     pg_img = pygame.image.frombuffer(im.tostring(), im.size, im.mode)
     screen.blit(pg_img, (0,0))
     pygame.display.flip()
     pygame.time.delay(int(1000 * 1.0/fps))


Best of Luck
Bgeddy



More information about the Python-list mailing list