LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Debian (https://www.linuxquestions.org/questions/debian-26/)
-   -   Fast webcam capture (https://www.linuxquestions.org/questions/debian-26/fast-webcam-capture-600232/)

ErrorBound 11-16-2007 05:04 PM

Fast webcam capture
 
I'm working on a project now that needs to do the following things, as fast as humanly (or not-humanly) possible:

1. Capture still image from webcam
2. Process image
3. Take some action, depending on what was found in #2.

At the moment I'm working only on #1. I've found a couple of programs (gqcam and vgrabbj) which will take and image from the webcam and write it to a file, exactly what I need them to do. But they are slow. Even when writing to tmpfs instead of a real disk, the total time for this operation is around 0.3s. But technically, since the webcam can stream video at ~15 frames per second, it should be possible to do this much faster, no?

Can you recommend a way to do this?

Brian1 11-17-2007 11:31 AM

never done this but my thought is use a video capture card and video camera. Then you can select certain frames to view for comparison. I doubt you would ever be able to comapre frame to frame in real time because of the amount of data from an image or video frame. Maybe if you have a fiber network of a cluster farm running the app.

Brian

tredegar 11-17-2007 11:47 AM

Maybe motion will have a tool you can use: http://www.lavrsen.dk/twiki/bin/view/Motion/WebHome

ponggr 11-20-2007 02:03 PM

I too was searching how to capture an image but fortunately for me 1 frame per second is enough as i am using matlab and based on my following theory i would have a bit of a trouble with matlab getting it faster than that.

I guess that you are getting such slow speeds because the capture application has to write the file on disk and then your other application needs to read from the disk. So depending on your system specs it might take a second or two to do both. I guess that if you need maximum speed then you need to read the image from RAM and not a file which is written on the hard drive...

I just found this: osdir.com/ml/lib.opencv/2006-02/msg00836.html which looks like c++ and dont know if it will help you at all. I am sure you can do something similar with Java although i dont know what you are using. So, bottom line the fastest would be to read a file from within your application without calling another app avoiding writing on disk.

tredegar 11-20-2007 02:21 PM

@ ponggr,

Thanks for your input, but the Original Poster seems to have disappeared. It happens ;)
Meanwhile, welcome to LQ :) In my experience, this is one of the better places to seek / offer help. Feel free to hang around, and contribute when you can.

Just didn't want your first post to go unnoticed :)

ponggr 11-21-2007 12:15 AM

Thanks tredegar!

That website is back online again. In case it goes down again here is the code although after checking it out again I cant see it working on its own:

/////////////////////

#include "inc/main.h"


using namespace std;

//void callback(IplImage* image);
void stereocallback(IplImage* image1, IplImage* image2);
int ct =0;

int main(int argc, char *argv[])
{
IplImage* image;
char file[255];

int ncams = cvcamGetCamerasCount();
cout <<ncams<<endl;
int cam_1 = 1;
int cam_2 = 3;


cvNamedWindow("Caméra 1", CV_WINDOW_AUTOSIZE);
HWND MyWin_1 = (HWND)cvGetWindowHandle("Caméra 1");
cvcamSetProperty(cam_1, CVCAM_PROP_ENABLE, CVCAMTRUE);
cvcamSetProperty(cam_1, CVCAM_PROP_RENDER, CVCAMTRUE);
cvcamSetProperty(cam_1, CVCAM_PROP_WINDOW, &MyWin_1);


cvNamedWindow("Caméra 2", CV_WINDOW_AUTOSIZE);
HWND MyWin_2 = (HWND)cvGetWindowHandle("Caméra 2");
cvcamSetProperty(cam_2, CVCAM_PROP_ENABLE, CVCAMTRUE);
cvcamSetProperty(cam_2, CVCAM_PROP_RENDER, CVCAMTRUE);
cvcamSetProperty(cam_2, CVCAM_PROP_WINDOW, &MyWin_2);//

cvcamSetProperty(cam_2, CVCAM_STEREO_CALLBACK, (void*)stereocallback);
// this line must
// be replaced if using
a single camera

// Replace the stereocallback by callback when using only 1 webcam
//cvcamSetProperty(0, CVCAM_PROP_CALLBACK, (void*)callback);
//this callback will treat each frame seperately


//cvWaitKey(0);

cvcamInit();
cvcamStart();
cvWaitKey(0);
cvcamStop();
cvcamExit();

return 0;
}




void stereocallback(IplImage* image1, IplImage* image2)
{
int i,j;
char file[255];
sprintf(file, "../images/Lefti%i.bmp",ct);
cvSaveImage(file,image1);
sprintf(file, "../images/righti%i.bmp",ct);
cvSaveImage(file,image2);
ct++;

}

void callback(IplImage* image)
{
int i,j;
char file[255];
sprintf(file, "../images/image%i.bmp",ct);
cvSaveImage(file,image);
ct++;

}


/*

This code allow to capture form 2 webcams. I used 2 Creative NX webcams
enjoy openCV ;-)
*/


/////////////////////////////////

I didnt have any luck getting an image from the webcam on Fedora. The problem is I cant install many things because it the university's machine and i dont have su access. The only things I can work with are MPlayer and xawtv.

With Mplayer I can get as many images I want but the program wont complete without pressing ctrl+c which means I cannot just call it from matlab get my image and continue. It the requires user to press ctrl-c.

xawtv with streamer I cant get it to work.

I tried to find how to write a Java app that would do that and that is even more trouble.

I ve spent so many hours with no result. Any ideas would be really appreciated. What I need is a command that I can run that will capture one frame from the webcam and will save it on the hard drive. Then i can easily make a script that will call the same command in regular intervals so that my captured image gets updated.

I managed to do this on my mac but on the linux machine which I will have to use for this I had no luck. Worst case scenario I will use the mac to capture the image which will also act as a local webserver so I can get the images in the Fedora 6 machine through http locally. :S

Thanks
ponggr

ErrorBound 11-21-2007 01:33 AM

Quote:

Originally Posted by ponggr (Post 2965643)
I guess that you are getting such slow speeds because the capture application has to write the file on disk and then your other application needs to read from the disk. So depending on your system specs it might take a second or two to do both. I guess that if you need maximum speed then you need to read the image from RAM and not a file which is written on the hard drive...

I thought of the same thing. What I've been doing is mounting a small tmpfs filesystem (essentially a filesystem existing only in memory) and reading/writing from that instead of to an actual disk. But the times don't seem to change much, so I think most of the time is spend transferring the image over USB, though I don't have a way to verify this. I'll check out the link you posted, it looks like it might be promising.

Quote:

Originally Posted by tredegar
the Original Poster seems to have disappeared.

I haven't disappeared! I just haven't had time to investigate the things which have been suggested, since I see them taking more than just a few minutes to look into. I'll have some time to do this soon, and I'll let you know how it goes.

infinition 11-23-2007 12:14 PM

The code you have pasted is an example of how to use the cvcam library. This library is part of the Intel Open source computer vision libary (opencv). I've worked a lot with cvcam under windows and I know it is using DirectShow to do the grab stuff. Under linux, I'm pretty sure it uses Video For Linux. If you cannot install Opencv for Linux, you should at least take a look into the cvcam library files to see how they use Video For Linux.

Eric

infinition 11-23-2007 12:18 PM

BTW The example will run only with the window version of cvcam as the linux version doesnt support stero callback. I think it is the only difference between the two API.

Eric


All times are GMT -5. The time now is 07:20 PM.