webcam (usb) access under Ubuntu

Berco Beute cyberco at gmail.com
Thu Apr 17 16:11:17 CEST 2008


On Apr 16, 2:26 pm, yoz <y... at home.havin.us> wrote:
> 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

Thank you! That seems to work under both Linux and windows. The opencv
library is the clue here. I've used the ctypes wrapper for opencv
provided by the first link below, but there are more options.

For future reference here are some relevant links:

http://wwwx.cs.unc.edu/~gb/wp/blog/2007/02/04/python-opencv-wrapper-using-ctypes/
http://opencvlibrary.sourceforge.net/
http://opencvlibrary.sourceforge.net/NoamLewis
http://opencvlibrary.sourceforge.net/PythonInterface

Windows specific:
http://www.instructables.com/id/Using-openCV-1.0-with-python-2.5-in-Windows-XP/
http://dip.sun.ac.za/3Dvision/talks/OpenCV_in_Python_on_Windows.pps
http://opencvlibrary.sourceforge.net/NoamLewis

Thanks for all the help.

2B



More information about the Python-list mailing list