We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Using two web cams with Open CV (Read 12920 times)
Using two web cams with Open CV
Mar 17th, 2009, 2:36am
 
I am trying to use two web cams.  In theory, OpenCV allows us to address different cameras.  I have two cameras in two USB ports, and Amcap can show either one.  The OpenCV capture command can take three args, the last one being the camera index.  However, the code below does not seem to work.  I'm not sure what indexs to put in the third arg, and chose -1 only because I found this on a java thread about OpenCV.

import hypermedia.video.*;

OpenCV opencv;
OpenCV opencv2;

void setup() {

   size(640, 240 );

   opencv = new OpenCV(this);
   opencv.capture( 320, 240,-1 );
   opencv2 = new OpenCV(this);
   opencv2.capture( 320, 240, 0 );
}

void draw() {

   opencv.read();
   image( opencv.image(), 0, 0 );
  opencv2.read();
   image( opencv2.image(), 320, 0 );
}
Re: Using two web cams with Open CV
Reply #1 - Mar 19th, 2009, 10:06am
 
Hi

Im not sure of this one, but try doing this to see the index for the cameras:

String[] devices = Capture.list();
println(devices);

Just put it in the last part of the setup() but before you do any openCV stuff.
Re: Using two web cams with Open CV
Reply #2 - Mar 19th, 2009, 1:50pm
 
Hi,

Thanks for your note.  Using the standard video library this does work with two USB cams on a PC, and I have to use a vdig and Quicktime for Java.  But I was hoping to use two cams with JMyron or OpenCV, because they give me access to algorithms for face detection, blob detection, etc.  The OpenCV for PRocessing people wrote that they might add this feature to their library at some point.
Re: Using two web cams with Open CV
Reply #3 - Mar 20th, 2009, 10:40am
 
hi stephen

i can help with this one - was trying to do exactly the same thing the other day.

Theoretically you should be able to get more than one camera working with openCV, but i think part of the problem with the indexing is that the openCV library only recognises a few types of camera. this is where i got stuck too...

what you have to do is load up the different cameras using the processing video library and then kinda graft those feeds into the openCV buffers.

check this code out and let me know how you get on.

hitting the space bar should record the openCV buffer. it isn't perfect yet; i need to figure out how to record two images to the buffer. ideally i'd like to stitch the two video feeds together at the start...

S

Quote:
import processing.video.*;
import hypermedia.video.*;

OpenCV ocv1;
OpenCV ocv2;
Capture myCapture; 
Capture myCapture2; 
String[] captureDevices;
int threshold = 80;

void setup()

  size(640, 780);
  println (Capture.list());
  captureDevices = Capture.list();
  
  myCapture = new Capture(this, width/2, height/3, 30);
  myCapture.settings();  
  
  myCapture2 = new Capture(this, width/2, height/3, 30);
  myCapture2.settings(); 
  
  ocv1 = new OpenCV( this );  
  ocv1.allocate(width/2,height/3);
  
  ocv2 = new OpenCV( this );        
  ocv2.allocate(width/2,height/3);
 

 
void draw() {
  if (myCapture.available()) {
    myCapture.read(); 
    ocv1.copy(myCapture);
    ocv1.blur(OpenCV.MEDIAN, 13); 
    ocv1.convert( GRAY );
    image(ocv1.image(),0,0);  
    image( ocv1.image(OpenCV.MEMORY), 0, 1*height/3 ); // image in memory
    ocv1.absDiff();
    ocv1.threshold(threshold);
    image( ocv1.image(OpenCV.GRAY), 0, 2*height/3 ); // absolute difference image
  
    ocv1.copy(myCapture);  
    ocv1.blur(OpenCV.MEDIAN, 13); 
  } 
  if (myCapture2.available()) {
    myCapture2.read(); 
    ocv2.copy(myCapture2);
    ocv2.blur(OpenCV.MEDIAN, 13); 
    ocv2.convert( GRAY );
   image(ocv2.image(),width/2,0);  
   image( ocv2.image(OpenCV.MEMORY), width/2, 1*height/3 ); // image in memory
   ocv2.absDiff();
   ocv2.threshold(threshold);
   image( ocv2.image(OpenCV.GRAY), width/2, 2*height/3 ); // absolute difference image
  
   ocv2.copy(myCapture2);  
   ocv2.blur(OpenCV.MEDIAN, 13); 
  } 
}

void keyPressed() {
    if ( key==' ' ){
      if (myCapture.available()) {
        ocv1.remember(1,2); //SOURCE,FLIP_HORIZONTAL
      }
      if (myCapture2.available()) {
      ocv2.remember(1,2); //SOURCE,FLIP_HORIZONTAL
    }}}
    
public void stop() {
    ocv1.stop();
    ocv2.stop();
    super.stop();
}
 


Re: Using two web cams with Open CV
Reply #4 - Mar 20th, 2009, 5:04pm
 
Thanks, Sam.  I couldn't get that code working with two cameras, but I think your idea is a good one--use standard video library to capture, then pass the data to OpenCV for its algorithms on face detection, etc.  I'll build up to it step by step and see if I can get it to work.  FWIW, I did get this response from the developers of OpenCV library when I asked about using dual cameras with their library:

"I can confirm that this will not work for the moment. This is because of the way JNI works and not because of OpenCV. We have just spent a day 's worth pf work trying to find a solution (creating separate instances of all our variables inside the C++ code) which looks like it will be a total pain on our side, but possible, yes. We do not know if we will fix this problem now, or later. I fear later, as we want to add OpticalFlow for next week. We'll see this morning how it goes."
Re: Using two web cams with Open CV
Reply #5 - Mar 20th, 2009, 7:12pm
 
So I did get the following code working with two copies of the video object and two copies of the OpenCV object.  Shows two camera images side by side and successfully does face tracking.  (haar files must be in the current dir).  Trouble is, it runs at about 1 fps, compared to about 8 or 10 fps for the pure OpenCV version tracking one camera.  Not sure if my coding is optimized or good.  Thanks for getting me started on this.  Thoughts?

import processing.video.*;  
import hypermedia.video.*;

OpenCV ocv1;
OpenCV ocv2;
Capture myCapture;  
Capture myCapture2;  
String[] captureDevices;  
int threshold = 80;
int midX;

void setup()  
{  
 size(640, 240);  
 midX = width/2;  // place to start drawing image from second cam
 println (Capture.list());  
 captureDevices = Capture.list();  
 
 myCapture = new Capture(this, 320, height, 30);  
 myCapture2 = new Capture(this, 320, height, 30);  
 ocv1 = new OpenCV(this);
 ocv1.allocate(320,height);
 ocv2 = new OpenCV(this);
 ocv2.allocate(320,height);
 ocv1.cascade( OpenCV.CASCADE_FRONTALFACE_ALT );  
 ocv2.cascade( OpenCV.CASCADE_FRONTALFACE_ALT );
 
}  


void draw() {  
 if (myCapture.available()) {  
   myCapture.read();  
   ocv1.copy(myCapture);  // copy to OpenCV buffer
   image(ocv1.image(),0,0); // display to screen
   Rectangle[] faces1 = ocv1.detect();// detect anything ressembling a FRONTALFACE
   noFill();
   stroke(255,0,0);
   for( int i=0; i<faces1.length; i++ ) {  // draw rect around detected face area(s)
       rect( faces1[i].x, faces1[i].y, faces1[i].width, faces1[i].height );
   }
 }  
  if (myCapture2.available()) {  
   myCapture2.read();  
   ocv2.copy(myCapture2);
   image(ocv2.image(),320,0);  // draw second image on the right half of the window  
   Rectangle[] faces2 = ocv2.detect();// detect anything ressembling a FRONTALFACE
   noFill();
   stroke(255,0,0);
   for( int i=0; i<faces2.length; i++ ) {
       rect( faces2[i].x + midX, faces2[i].y, faces2[i].width, faces2[i].height );
   }
 }  

}

 
public void stop() {
   ocv1.stop();
   ocv2.stop();
   super.stop();
}
 
Re: Using two web cams with Open CV
Reply #6 - Mar 20th, 2009, 10:37pm
 
hi stephen

there must be something in your code; but i've had too long a day to be able to read it - time for a beer now!

i've slightly changed my approach during the day because of what i want to do - on reflection doing the same might help you.

i think in my original example i grafted two video feeds into two openCV objects: this has now changed and i have in essence stitched the two feeds together into one openCV object. (this can potentially take any number of camera feeds). It leaves one openCV object to play with.

this will help you with your speed issues; and often cameras might be covering adjacent areas, so it can simplify the analysis stage too.

i then run through a colour filter, greying,threshold,blur systems,and finally a blob detection and tracking - speed is all fine here.

the code is a bit involved (and i haven't commented it) and too long to post here; but i could email it to you if you like. let me know: maybe the later areas are not necessary for you.

s


edit: i think my working framerate is about 30fps. s.
Re: Using two web cams with Open CV
Reply #7 - May 5th, 2009, 10:38am
 
Hello,

So — I'm also working through this problem of multiple connected web cams and Processing. If you wouldn't mind sharing your code, I'd be grateful!

You can PM me or email me julian at nearfuturelaboratory dot com

.julian.
Re: Using two web cams with Open CV
Reply #8 - May 30th, 2009, 7:35am
 
Hi sam_mcelhinney,
Can you put your code somewhere on the web so we can help?
Re: Using two web cams with Open CV
Reply #9 - Mar 30th, 2010, 10:17am
 
I know this is a really old thread but i am dealing with the same exact issues and would love to take a look at your code sam_mcelhinney if thats possible.
Page Index Toggle Pages: 1