Using USB camera instead of iSight

Hi,

I'm using this code for a facial recognition sketch, but it uses my inbuilt iSight camera instead of the USB camera I want to use. I tried looking for some workarounds, but no succes. How can I use my USB camera instead of the iSight?

import gab.opencv.*;
import processing.video.*;
import java.awt.*;

import ddf.minim.*;

Minim minim;
AudioPlayer soundtrack;

Capture video;
OpenCV opencv;

void setup() {

  size(640, 480);
  video = new Capture(this, 640/2, 480/2);

  opencv = new OpenCV(this, 640/2, 480/2);
  opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);  

  video.start();


   // always start Minim before you do anything with it
  minim = new Minim(this);
  //load a file, give the AudioPlayer buffers that are 2048 samples long
  soundtrack = minim.loadFile("laser.mp3");
  video.settings();
}

void draw() {
  scale(2);
  opencv.loadImage(video);

  image(video, 0, 0 );

  noFill();
  stroke(255, 255, 0);
  strokeWeight(3);
  Rectangle[] faces = opencv.detect();
  println(faces.length);

  for (int i = 0; i < faces.length; i++) {
    println(faces[i].x + ", FACE" + faces[i].y);
    rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
    soundtrack.play();    
      delay(3000);
    soundtrack.rewind();



  }

}

void captureEvent(Capture c) {
  c.read();
}

void stop()
{
  // always close Minim audio classes when you are done with them

  soundtrack.close();
  minim.stop(); 
  super.stop();
}

Answers

  • you can't sorry

  • edited October 2014 Answer ✓

    Thanks for the really helpful comment. I fixed it by finding out what the name of the usb is by using this code:

    Capture video;
    
    
    
    import processing.video.*;
    
    void setup() {
      size(640, 480);
    
      String[] cameras = Capture.list();
    
    
    
      if (cameras.length == 0) {
        println("There are no cameras available for capture.");
        exit();
      } else {
        println("Available cameras:");
        for (int i = 0; i < cameras.length; i++) {
          println(cameras[i]);
        }
    
        // The camera can be initialized directly using an 
        // element from the array returned by list():
     video = new Capture(this, width, height, Vid, 24);
        video.start();  
    
      }      
    }
    
    void draw() {
      if (video.available() == true) {
        video.read();
      }
      image(video, 0, 0);
      // The following does the same, and is faster when just drawing the image
      // without any additional resizing, transformations, or tint.
      //set(0, 0, cam);
    }
    

    The name was "USB Webcam"

    so then I added this to my original code:

      String Vid = "USB Camera";
      video = new Capture(this, 640/2, 480/2, Vid);
    

    And it works :)

    Full code here:

    import gab.opencv.*;
    import processing.video.*;
    import java.awt.*;
    
    import ddf.minim.*;
    
    Minim minim;
    AudioPlayer soundtrack;
    
    Capture video;
    OpenCV opencv;
    
    void setup() {
    
      size(640, 480);
            String Vid = "USB Camera";
      video = new Capture(this, 640/2, 480/2, Vid);
    
      opencv = new OpenCV(this, 640/2, 480/2);
      opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);  
    
      video.start();
    
    
       // always start Minim before you do anything with it
      minim = new Minim(this);
      //load a file, give the AudioPlayer buffers that are 2048 samples long
      soundtrack = minim.loadFile("laser.mp3");
    
    }
    
    void draw() {
      scale(2);
      opencv.loadImage(video);
    
      image(video, 0, 0 );
    
      noFill();
      stroke(255, 255, 0);
      strokeWeight(3);
      Rectangle[] faces = opencv.detect();
      println(faces.length);
    
      for (int i = 0; i < faces.length; i++) {
        println(faces[i].x + ", FACE" + faces[i].y);
        rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
        soundtrack.play();    
          delay(3000);
        soundtrack.rewind();
    
    
    
      }
    
    }
    
    void captureEvent(Capture c) {
      c.read();
    }
    
    void stop()
    {
      // always close Minim audio classes when you are done with them
    
      soundtrack.close();
      minim.stop(); 
      super.stop();
    }
    

    Hope this is helpful for people who have the same problem.

Sign In or Register to comment.