Tuesday, June 8, 2010

Capturing Frames from webcams using OpenCV


After many unsuccessful try, today I finally captured some frames from my usb webcam. The webcam I am using is Logitech Webcam Pro 9000. New linux kernel, like 2.6.32 supports UVC compliant webcams. The webcam is detected as soon as you plug in the usb to the self powered USB hub. I could also see that my webcam is detected by typing

root@beagleboard:~# lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 002: ID 0409:005a NEC Corp. HighSpeed Hub
Bus 002 Device 003: ID 413c:1002 Dell Computer Corp. Keyboard Hub
Bus 002 Device 004: ID 0461:4d15 Primax Electronics, Ltd Dell Optical Mouse
Bus 002 Device 005: ID 046d:0809 Logitech, Inc.
Bus 002 Device 006: ID 0b95:772a ASIX Electronics Corp.
Bus 002 Device 007: ID 413c:2002 Dell Computer Corp. SK-8125 Keyboard

and for further confirmation that the UVC module is loaded I typed the following

root@beagleboard:/media/work/opencv# lsmod
Module Size Used by
g_ether 24916 0
nfsd 241018 8
nfs_acl 2173 1 nfsd
exportfs 3092 1 nfsd
ipv6 249183 10
rfcomm 33488 0
hidp 11193 0
l2cap 30104 4 rfcomm,hidp
bluetooth 49221 3 rfcomm,hidp,l2cap
rfkill 15030 1 bluetooth
rtc_twl 4451 0
rtc_core 12599 1 rtc_twl
uvcvideo 55469 0
mailbox_mach 4183 0
mailbox 3609 1 mailbox_mach

I had to struggle a little to successfully capture frames from my webcam. I searched for many
resources and finally came across http://groups.google.com/group/beagleboard/browse_thread/thread/2b2d4a50c5c77f8a/931f1e5160f9bbee?lnk=gst&q=opencv+on+bb#931f1e5160f9bbee . In this thread, a group member has pointed to setup procedure given by Don Lewis. Many thanks to the group member and Don Lewis. It was a relief after so many unsuccessful
try. I tried to follow exactly what it said. While compiling the program I got following warnings
but there were no errors.

root@beagleboard:/media/work/opencv# gcc -I/usr/include/opencv -g Capture.c -o
test -lml -lcvaux -lhighgui -lcv -lcxcore
In file included from /usr/include/opencv/cxcore.h:70,
from /usr/include/opencv/cv.h:58,
from Capture.c:5:
/usr/include/opencv/cxtypes.h: In function 'cvRound':
/usr/include/opencv/cxtypes.h:228: warning: incompatible implicit declaration of
built-in function 'lrint' root@beagleboard:/media/work/opencv#

However, execution failed giving following errors and messages on the console.

root@beagleboard:/media/work/opencv# ./test
libv4lconvert: warning more framesizes then I can handle!
libv4lconvert: warning more framesizes then I can handle!
[27996.920166] keyboard.c: can't emulate rawmode for keycode 212
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
libv4l2: error allocating conversion buffer
mmap: Cannot allocate memory
[27997.052886] uvcvideo: Failed to query (1) UVC control 4 (unit 1) : -32 (exp. 4).
HIGHGUI ERROR: V4L2: Failed to set control "10094850": Input/output error (value 305)
HIGHGUI WARNING: Setting property 10094850 through v4l2 failed. Trying with v4l1 .
HIGHGUI ERROR: V4L: property #10094850 is not supported
munmap: Invalid argument munmap: Invalid argument
munmap: Invalid argument munmap: Invalid argument
Unable to stop the stream.: Bad file descriptor
munmap: Invalid argument
munmap: Invalid argument
munmap: Invalid argument
munmap: Invalid argument
libv4lconvert: warning more framesizes then I can handle!
libv4lconvert: warning more framesizes then I can handle!
[27997.192169] keyboard.c: can't emulate rawmode for keycode 212
libv4l1: error allocating v4l1 buffer: Cannot allocate memory
HIGHGUI ERROR: V4L: Mapping Memmory from video source error: Invalid argument
HIGHGUI ERROR: V4L: Initial Capture Error: Unable to load initial memory buffers
. failed to get a video frame
OpenCV Error: Null pointer (NULL array pointer is passed) in cvGetMat, file /OE/
angstrom-dev/work/armv7a-angstrom-linux-gnueabi/opencv-2.1.0+svnr3058-r0/opencv/
src/cxcore/cxarray.cpp, line 2376 terminate called after throwing an instance of 'cv::Exception'
what(): /OE/angstrom-dev/work/armv7a-angstrom-linux-gnueabi/opencv-2.1.0+svnr
3058-r0/opencv/src/cxcore/cxarray.cpp:2376: error: (-27) NULL array pointer is p
assed in function cvGetMat
Aborted
root@beagleboard:/media/work/opencv#

Initially I could not figure out what caused to abort. I gave a try to some other example mentioned at http://blog.damiles.com/?p=61. Still the problem was same.
I then the changed the "mem" argument for uboot. Initially I had set "mem=80M". Then changed it for "mem=128M" in my uboot. Booted my Beagleboard and then run the test. This time, I could successfully capture frames although I could still see some messages on my console,

root@beagleboard:/media/work/opencv# ./test
libv4lconvert: warning more framesizes then I can handle!
libv4lconvert: warning more framesizes then I can handle!
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
[ 138.964233] uvcvideo: Failed to query (1) UVC control 4 (unit 1) : -32 (exp.
4).
HIGHGUI ERROR: V4L2: Failed to set control "10094850": Input/output error (value
305)
HIGHGUI WARNING: Setting property 10094850 through v4l2 failed. Trying with v4l1
.
HIGHGUI ERROR: V4L: property #10094850 is not supported
root@beagleboard:/media/work/opencv#

I am still not sure how to fix these warnings. I searched for this issue and find out many topics regarding it but still has not come across the solution. Ffor now I am able to capture some frames using my webcam and further need to work on adding this to my application. One of the images I captured using my webcam is the one at the top.

No comments:

Post a Comment