mikoff
Posts: 1
Joined: Thu Jul 19, 2012 11:24 am

OpenCV + webcam

Thu Jul 19, 2012 11:35 am

Hello everyone.
Now i am doing some tests with RaspberryPi and OpenCV. I succesfully compiled and installed OpenCV library, testes some examples without webcam - all of them work with no issues.
But now i want to run a simple program, which gets image from webcam and shows it to the user, the main problem is that i get "select timeout" message and stream from webcam freezes.
If i change image size to 160x120 it works fine, but not in 320x240 or higher. Command cvSetCaptureProperty(capture, CV_CAP_PROP_FPS, 5) doesn`t work.
I tested webcam using luvcview:
luvcview -d /dev/video0 -f yuv -s 320x240 -i 5
It gives me stable 2-3 FPS and doesn`t hang at all!
Could you give me some recipes or advices how to resolve this problem in OpenCV. Thanks a lot!

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include "cv.h"
#include "highgui.h"

int main( int argc, char **argv){
    CvCapture *capture = 0;
    IplImage  *frame = 0;
    int       key = 0;

    /* initialize camera */
    capture = cvCaptureFromCAM(0);
    cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 320); 
    cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 240);
    cvSetCaptureProperty(capture, CV_CAP_PROP_FPS, 5);
    /* always check */
    if ( !capture ) {
        fprintf( stderr, "Cannot open initialize webcam!\n" );
        return 1;
    }

    /* create a window for the video */
    cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );

    while( key != 'q' ) {
        /* get a frame */
        frame = cvQueryFrame( capture );
        /* always check */
        if( !frame ) 
		{
			break;
			fprintf( stdout, "ERROR: frame is null...\n" );
		}

        /* display current frame */
        cvShowImage( "result", frame );

        /* exit if user press 'q' */
        key = cvWaitKey( 1 );
    }

    /* free memory */
    cvDestroyWindow( "result" );
    cvReleaseCapture( &capture );

    return 0;
}

hoehermann
Posts: 24
Joined: Thu Jul 26, 2012 10:23 am

Re: OpenCV + webcam

Thu Jul 26, 2012 10:33 am

I am stuck with the "select timeout" error, too. It occurs while fetching data from my Logitech c270. Both mplayer and OpenCV fail. A resolution of 160x120 or 320x240 works fine, but is insufficient for my needs. With uvccapture I can get 640x480 single shot stills from the camera. I can not set the capturing framerate as it is not supported by the camera. In any case the colours are garbled. Default format for capturing is MJPEG. Higher resolutions demand YUYV capture. Uncompressed YUYV always fails, independet from the resolution. Whenever I try to actually stream from the webcam with mplayer, it grabs some frames and then hickups while the underlying v4l2 libraries print "select timeout" to the console. This also happens if video data is not processed, but simply discarded (null video output). I suspect the data can not be transferred from the webcam to the RAM fast enough. The select function seems to be a getter for a fifo somewhere deep in the guts of v4l2. I hope this is a driver issue with my camera only, that will be resolved sometime in the future.

ceros
Posts: 9
Joined: Wed Jul 25, 2012 11:10 pm

Re: OpenCV + webcam

Thu Jul 26, 2012 2:27 pm

Have you seen this post

http://eduardofv.com/read_post/185-Inst ... spberry-Pi

Read his "Update:..." at the top of the page

hoehermann
Posts: 24
Joined: Thu Jul 26, 2012 10:23 am

Re: OpenCV + webcam

Thu Jul 26, 2012 3:41 pm

mikoff and I already installed OpenCV successfully. The core problem is that pictures with high data rates can not be read from the camera at all. For me the very same camera works fine on an Intel Atom board running a Debian kernel of equal version. So it's probably an ARM binary related or hardware related issue (slow DMA). Or do you suggest switching to Arch Linux just to give it a try?

hoehermann
Posts: 24
Joined: Thu Jul 26, 2012 10:23 am

Re: OpenCV + webcam

Sat Jul 28, 2012 9:50 am

I managed to solve the "select timeout" for my application. Two measures were neccessary. First of all with my specific webcam, the USB-Audio driver built-in Microphone does not work properly. I can disable it by adding

Code: Select all

blacklist snd_usb_audio
to one of the files in /etc/modprobe.d/.
Second I simply increase the timeout by passing the parameters

Code: Select all

nodrop=1 timeout=5000
to the uvcvideo module. You can increase the verbosity by passing something like trace=15 to have more information about the module in dmesg. Maybe the quality can be increased further by adding some manual quirks.

ceros
Posts: 9
Joined: Wed Jul 25, 2012 11:10 pm

Re: OpenCV + webcam

Wed Aug 01, 2012 4:22 pm

So everything is working? Did you try ArchLinux?

I just got my PI yesterday but I'm too busy at the University right now to try OpenCV on ArchLinux.

hoehermann
Posts: 24
Joined: Thu Jul 26, 2012 10:23 am

Re: OpenCV + webcam

Thu Aug 02, 2012 8:34 am

No, I did not try ArchLinux. I didn't find an image right away and was reluctant to run the setup myself.
Regarding my application, everyhing is fine. However, I still observe some problems. Reading a frame from the webcam takes pretty long. It looks like I can only read from the camera in MJPEG mode, reading uncompressed YVYU frames fails. I am not sure whether this is a problem with the uvcvideo module and this very camera or a general problem. For the very same reason I can not select the maximum resolution of 1280x720. Luckily 960x720 is what I chose anyways. A greater resolution demands YVVU.
The pi's overall performance regarding OpenCV is acceptable, I have no memory problems. See it in action: http://youtu.be/Urm2kdGX8WQ

ceros
Posts: 9
Joined: Wed Jul 25, 2012 11:10 pm

Re: OpenCV + webcam

Thu Aug 02, 2012 2:08 pm

Nice but it's pretty slow. It took like 2-3sec before you see the first dice on your screen.

hoehermann
Posts: 24
Joined: Thu Jul 26, 2012 10:23 am

Re: OpenCV + webcam

Thu Aug 02, 2012 2:25 pm

Indeed, the lag loogs quite long. But it's not always that bad. Sometimes replugging decreases the lag. I did not investigate this any further as it's probably related to my cheap webcam. Additionally, there is a 1 second sleep in that demo for debugging purposes. The image processing itself can run at about 3, maybe 5 fps. In my opinion, this is fine for a 25$ system.

ceros
Posts: 9
Joined: Wed Jul 25, 2012 11:10 pm

Re: OpenCV + webcam

Thu Aug 02, 2012 2:40 pm

Do you think that with the upcomming RPi cam it will be better?

ceros
Posts: 9
Joined: Wed Jul 25, 2012 11:10 pm

Re: OpenCV + webcam

Thu Aug 02, 2012 2:46 pm


hoehermann
Posts: 24
Joined: Thu Jul 26, 2012 10:23 am

Re: OpenCV + webcam

Thu Aug 02, 2012 7:17 pm

I guess the upcoming RPi cam will be better. My problems seem to originate from crappy drivers and/or general problems with high load on the USB bus.
I intentionally bought one of the webcams listed as working. It's a Logitech c270. I don't know who put it on there, but he or she probably didn't notice any problems. They only occur if you use resolutions higher than the default of 320x240.

HVA
Posts: 10
Joined: Fri Aug 03, 2012 2:28 pm

Re: OpenCV + webcam

Fri Aug 03, 2012 2:37 pm

mikoff wrote: ...........

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include "cv.h"
#include "highgui.h"

int main( int argc, char **argv){
    CvCapture *capture = 0;
    IplImage  *frame = 0;
    int       key = 0;

    /* initialize camera */
    capture = cvCaptureFromCAM(0);
    cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 320); 
    cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 240);
    cvSetCaptureProperty(capture, CV_CAP_PROP_FPS, 5);
    /* always check */
    if ( !capture ) {
        fprintf( stderr, "Cannot open initialize webcam!\n" );
        return 1;
    }

    /* create a window for the video */
    cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );

    while( key != 'q' ) {
        /* get a frame */
        frame = cvQueryFrame( capture );
        /* always check */
        if( !frame ) 
		{
			break;
			fprintf( stdout, "ERROR: frame is null...\n" );
		}

        /* display current frame */
        cvShowImage( "result", frame );

        /* exit if user press 'q' */
        key = cvWaitKey( 1 );
    }

    /* free memory */
    cvDestroyWindow( "result" );
    cvReleaseCapture( &capture );

    return 0;
}
Hi Mikoff,

I have installed the OpenCV 2.3.1 and I have tried to compile your code (one single c file).
but gcc doesn't find the "cv.h", which I did expect.
I just don't know to tell to gcc from where to get "cv.h" and all the other OpenCv things.

As a fact I can't find the opencv2 folder on my raspberry... But the example do work (at least the ones that do not require an web cam)

As you can realize I am very new to Linux, but I did used OpenCv in win using Visual Studio.

Thank you

PS please excuse my spelling mistakes...

hoehermann
Posts: 24
Joined: Thu Jul 26, 2012 10:23 am

Re: OpenCV + webcam

Fri Aug 03, 2012 4:01 pm

GCC should find the header files itself. When you install a package, the header files are automatically put in the appropriate folders. On Debian it is /usr/include/. Therefore you should include <cv.h> rather than "cv.h", but it should work anyway. Sometimes a packager thinks it is useful to have the headers in a subdirectory. You can examine the examples C source file or the Makefile (compiler option -I) to find out the directory. I give you the advice never to compile something from source and install it yourself unless you really really have to. OpenCV is in the repository and installable via apt-get. Keep in mind that there often are two packages, for example opencv and opencv-dev. For development, you need both, as the latter includes the header files.

kuroOluffy
Posts: 7
Joined: Mon Aug 13, 2012 7:10 pm

Re: OpenCV + webcam

Mon Aug 13, 2012 7:13 pm

hoehermann wrote:I managed to solve the "select timeout" for my application. Two measures were neccessary. First of all with my specific webcam, the USB-Audio driver built-in Microphone does not work properly. I can disable it by adding

Code: Select all

blacklist snd_usb_audio
to one of the files in /etc/modprobe.d/.
Second I simply increase the timeout by passing the parameters

Code: Select all

nodrop=1 timeout=5000
to the uvcvideo module. You can increase the verbosity by passing something like trace=15 to have more information about the module in dmesg. Maybe the quality can be increased further by adding some manual quirks.
Hi,

I am currently using Arch Linux and a microscope USB camera. With the resolution 320x240 I am still getting the select timeout error. Where can i edit the uvcvideo module to allow for nodrops and longer timeout times?

Thanks!!

kuroOluffy
Posts: 7
Joined: Mon Aug 13, 2012 7:10 pm

Re: OpenCV + webcam

Mon Aug 13, 2012 7:19 pm

Hi,

I am using Arch Linux and a USB Microscope Digital Camera. I am receiving the 'select timeout' error even on the 320x240 resolution.

I would like to edit the uvcvideo module to allow for no drops as proposed by hoehermann but I do not know where to find this module. I could not read the uvcvideo.ko properly in libs/modules/.../kernels/drivers/media/video/uvc

Thanks in advance! Your help is greatly appreciated!!

hoehermann
Posts: 24
Joined: Thu Jul 26, 2012 10:23 am

Re: OpenCV + webcam

Mon Aug 13, 2012 8:57 pm

You can not edit the binary uvcvideo.ko file directly. I advise not trying to compile it yourself. It's not worth the struggle. You can try various options temporarily on the fly. Attach your webcam, the driver module should be loaded and shown by

Code: Select all

lsmod
. Then you unload the module using

Code: Select all

rmmod uvcvideo
. Afterwards you can reload the module with something like

Code: Select all

modprobe uvcvideo nodrop=1 timeout=5000
. Fiddle around with the settings.
You can make a set of parameters permanent by editing the file /etc/modprobe.d/modprobe.conf . For the above settings, simply add a line saying

Code: Select all

[code]uvcvideo nodrop=1 timeout=5000
[/code].

ceros
Posts: 9
Joined: Wed Jul 25, 2012 11:10 pm

Re: OpenCV + webcam

Mon Aug 13, 2012 9:01 pm

The .ko is a file generated by the make command from a source file.

kuroOluffy
Posts: 7
Joined: Mon Aug 13, 2012 7:10 pm

Re: OpenCV + webcam

Wed Aug 15, 2012 3:46 pm

Okay!

Thanks for the prompt replies! I will try it out and let you know how it goes!!

Thanks again!!

hoehermann
Posts: 24
Joined: Thu Jul 26, 2012 10:23 am

Re: OpenCV + webcam

Wed Aug 15, 2012 4:40 pm

Good luck. I just noticed the last box in my post is wrong. The line should read

Code: Select all

options uvcvideo nodrop=1 timeout=5000
.

kuroOluffy
Posts: 7
Joined: Mon Aug 13, 2012 7:10 pm

Re: OpenCV + webcam

Wed Aug 15, 2012 7:53 pm

So after trying

Code: Select all

uvcvideo nodrop=1 timeout=5000
I do not have any more 'select timeout' errors. However I find that my program stalls at the first instance of cvQueryFrame. When I leave the program to run for a lengthy amount of time (1 hour) it still does not move past the cvQueryFrame.

Here is my current code:

Code: Select all

#include "cv.h"
#include "highgui.h"
#include "math.h"
#include "stdio.h"

using namespace std;

int main()
{
	IplImage *captureImage;
	CvCapture *captureCamera;

	printf ("Connecting to Camera...");

	captureCamera = cvCreateCameraCapture(-1);

	printf ("Connected!!\n");

	printf ("Setting Resolution of Camera...");

	//Set Resolution of Camera
	cvSetCaptureProperty( captureCamera, CV_CAP_PROP_FRAME_WIDTH, 320 );
	cvSetCaptureProperty( captureCamera, CV_CAP_PROP_FRAME_HEIGHT, 240 );

	printf ("Complete!!\n");

	if(!captureCamera)
	{
		printf("Unable to capture camera.");
		exit(1);
	}

	printf ("Querying Frames...\n");
	
	while(1)
	{
		printf ("Enter while loop!\n");

		captureImage = cvQueryFrame(captureCamera);

		if( (cvWaitKey(10) & 255) == 27 )
		{
			printf ("Escape Key Pressed!\n");
			break;
		}
	}

	printf ("Exited While Loop\n");
	printf ("Rotating Image...");	

	//Rotate Image (Horizontal to Vertical)
	IplImage *transposeImage = cvCreateImage( cvSize(captureImage->height, captureImage->width), 8, 3 );

	cvTranspose (captureImage, transposeImage);

	printf ("Complete!!\n");

	printf ("Saving Image...");

	cvSaveImage ("CameraImage.jpg", transposeImage);

	printf ("Complete!!\n");

	printf ("Enter Key to Exit\n");

	cvWaitKey();

	cvDestroyWindow("Camera Feed");

	return 0;
}
I receive the output:
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
Connecting to Camera...Connected!!
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
Setting Resolution of Camera...Complete!!
Querying Frames...
Enter while loop!
And the program stalls at this point.

Currently my setup on the raspberry pi is Archlinux and the camera I am using is a USB Digital Microscope which is written for Windows OS. For my project however we require this camera to be used in conjunction with linux and the raspberry pi.

I think these errors may be related to driver issues but I am not sure and am not quite sure how to check. I have tried

Code: Select all

lsusb
and the camera is listed as
Bus 001 Device 004: ID 05e3:0511 Genesys Logic, Inc.
But I am still not sure if the correct drivers are installed, I have read that for USB Digital Microscopes like mine, the uvcvideo should be sufficient.

Thank you for your attention!!

hoehermann
Posts: 24
Joined: Thu Jul 26, 2012 10:23 am

Re: OpenCV + webcam

Thu Aug 16, 2012 7:18 am

kuroOluffy wrote:I have read that for USB Digital Microscopes like mine, the uvcvideo should be sufficient.
And you are right. In theory, it should now work. I have the strong suspicion, the Raspberry Pi has troubles with high data load on the USB.
Other things you can try: There is the command line program uvccapture. It works on a very low level. It allows you to set resolution, frames to capture (for tests only one frame is quite handy) and - this was very important to me - the format to transmit the image data. In my case the better raw YUV transmission always fails. :( If you want to have some more stream like behaviour, you can use mplayer opening tv:// . mplayer allows you to set many parameters via the -tv option. Fiddling around with this programs you can make sure it's the camera or the driver that is not working and not your program or the OpenCV library.
And now the bad news: There are some more settings on the uvcvideo kernel module. They are called "quirks". Unfortunately, I do not know a good documentation for them.
All I can say is that I am glad to have been lucky with my camera.

kuroOluffy
Posts: 7
Joined: Mon Aug 13, 2012 7:10 pm

Re: OpenCV + webcam

Thu Aug 16, 2012 7:18 pm

For my project, I only need to capture one frame from the camera and process the image. I do not require any streaming features.

I am not too familiar with uvccapture, but if I am able to just capture and save one frame with it, that would be amazing!

I will look into it right now!

Thanks again!

kuroOluffy
Posts: 7
Joined: Mon Aug 13, 2012 7:10 pm

Re: OpenCV + webcam

Fri Aug 17, 2012 2:26 pm

I could not find 'uvccapture' on Arch Linux. So right now I am trying out the wheezy-raspbian OS instead.

Which platforms are you using?

kuroOluffy
Posts: 7
Joined: Mon Aug 13, 2012 7:10 pm

Re: OpenCV + webcam

Fri Aug 17, 2012 4:04 pm

Okay! I have installed Wheezy on the Raspberry Pi and have started to use 'uvccapture'.

The good news is that I am able to receive images from my camera now. However when I use

Code: Select all

./uvccapture -v -m
I receive an image that is tricoloured (blue, red and green) and is split into different rectangles that do not align perfectly. I am not quite sure if this is characteristic of a YUYV image or not but it mostly likely is not correct.

When I use the default settings

Code: Select all

./uvccapture -v
I receive the following output:
Using videodevice: /dev/video0
Saving images to: snap.jpg
Image size: 320x240
Taking snapshot every 0 seconds
Taking images using mmap
Unable to set format: 22.
Init v4L2 failed !! exit fatal
Is there any way to get around this error?

Thank you!!

Return to “Automation, sensing and robotics”

Who is online

Users browsing this forum: No registered users and 17 guests