Cant get image from webcam

edited March 14 in Kinect

Hi,

I have my first processing sketch running with no errors but I cannot get an image from the webcam.

import hypermedia.video.*;
import java.awt.Rectangle;

OpenCV opencv;

// contrast/brightness values
int contrast_value    = 0;
int brightness_value  = 0;



void setup() {

    size( 320, 240 );

    opencv = new OpenCV( this );
    opencv.capture( width, height );                   // open video stream
    opencv.cascade( OpenCV.CASCADE_FRONTALFACE_ALT );  // load detection description, here-> front face detection : "haarcascade_frontalface_alt.xml"


    // print usage
    println( "Drag mouse on X-axis inside this sketch window to change contrast" );
    println( "Drag mouse on Y-axis inside this sketch window to change brightness" );

}


public void stop() {
    opencv.stop();
    super.stop();
}



void draw() {

    // grab a new frame
    // and convert to gray
    opencv.read();
    opencv.convert( GRAY );
    opencv.contrast( contrast_value );
    opencv.brightness( brightness_value );

    // proceed detection
    Rectangle[] faces = opencv.detect( 1.2, 2, OpenCV.HAAR_DO_CANNY_PRUNING, 40, 40 );

    // display the image
    image( opencv.image(), 0, 0 );

    // draw face area(s)
    noFill();
    stroke(255,0,0);
    for( int i=0; i<faces.length; i++ ) {
        rect( faces[i].x, faces[i].y, faces[i].width, faces[i].height ); 
    }
}



/**
 * Changes contrast/brigthness values
 */
void mouseDragged() {
    contrast_value   = (int) map( mouseX, 0, width, -128, 128 );
    brightness_value = (int) map( mouseY, 0, width, -128, 128 );
}

The programme is supposed to display an image, detect a face and draw a rectangle around the face. I am using windows 7 with a USB webcam. I have tried a couple of versions of processing and both are the same.

Any ideas of where to look first?

Thanks Paul

Answers

  • not sure what happened with my code paste. Hope it still makes sense.

  • Please format your code. Edit your post (gear on top right side of any of your posts), select your code and hit ctrl+o. Leave an empty line above and below your block of code. Details here: https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text

    Related to your post, what Processing version did you try? Did you try running the examples under the Capture folder to test the camera by itself? Did you try the examples that comes with the OpenCV library? Do they work? Notice that it is better to test the camera by itself before testing your opencv code.

    Kf

  • edited March 14

    Thanks. I've formatted the code in the above post.

    I have tried it on processing v3.3.6 and v2.2.1. I found reference online to people using 1.5.1 but my PC doesn't like this version because of the JAVA version, even though I tried downloading the JAVA that it suggested.

    I'm using openCV v1.0 and the code is the face_tracking example sketch (with just the import rectangle line added). I have not tried the camera using capture but I will do tonight.

    Worth saying as well that the PC is 64 bit windows 7, however after struggling with 32/64 bit compatibility errors I'm now running 32 bit openCV and processing.

  • edited March 16

    Hi all,

    So I finally got round to trying the capture code;

    // Step 1. Import the video library.
    import processing.video.*;
    
    //Step 2. Declare a capture object.
    Capture video;
    
    // Step 5. Read from the camera when a new image is available!
    void captureEvent(Capture video) {
      video.read();
    }
    
    void setup() {  
      size(320, 240);
    
      // Step 3. Initialize Capture object.
      video = new Capture(this, 320, 240);
    
      // Step 4. Start the capturing process.
      video.start();
    }
    
    // Step 6. Display the image.
    void draw() {  
    image(video, 0, 0);
    }
    

    However.... I'm using a laptop with a built in webcam but I want to use the USB camera. The sketch defaults to the built-in laptop. Is there an easy way of telling processing to read from the USB camera instead?

    I guess this means there’s an error in the open cv code?

    Thanks Paul

  • edited March 17

    Doesn't the Capture reference tell you how to do that? Iirc there's code to list all the cameras and then you supply an argument to the constructor to choose one.

    https://processing.org/reference/libraries/video/Capture.html

  • Thanks. Maybe there are different capture sketches. The one i posted here was from this forum too. The main thing for now is getting the original OpenCV face tracking sketch working.

    Does anybody have any ideas as to what the issue may be or how best to trouble shoot? Its all new to me and I'm pretty stuck.

    Thanks Paul

  • Does anybody have any ideas as to what the issue may be or how best to trouble shoot? Its all new to me and I'm pretty stuck.

    You have two issues: Getting the video stream via your usb cam and getting OpenCV working. Those are two different things that can be tested by themselves.

    For the USB camera: You need to run the capture example that comes with the library. In the PDE, go to files>>Examples>>Video>Capture and run the Getting Started example. Or check this link (one of the many in the forum): https://forum.processing.org/two/discussion/26640/processing-video-library-on-windows-10-tablet-with-z8350-processor-and-intel-avstream-cam-problem/p1 were it shows the way to list the available cameras in your system. If your USB is detected, it will be displayed there when you print the list. That is half of the battle won. Then you need to assign of of those detected camera to your capture object and test it to see if it works.

    For the OpenCV, you can test it with your laptop camera. If it doesn't work, then describe the details or any errors that you are getting. Describe what you did and even provide links to your reference sources, if any. Keep everything related under the same post as it will keep all the information to a minimum and your problem better documented, not only for you but also for other future forum goers.

    Kf

  • Thanks. Sorry if I confused it with another question. I’ll start another topic and me more specific. The OpenCV code is the main issue I’d like to fix.

  • I’ll start another topic and me more specific

    Please don't. Keep it here.

Sign In or Register to comment.