Reputation: 13
i am trying build a face detection with the opencv and openkinect libraries. for the image input i want to use the xbox kinect v2. i am basing my code on the face detection example within the opencv library. i am working on a mac.
this is my code so far:
import gab.opencv.*;
import java.awt.Rectangle;
/* KINECT */
import org.openkinect.freenect.*;
import org.openkinect.freenect2.*;
import org.openkinect.processing.*;
OpenCV opencv;
Kinect2 kinect2;
Rectangle[] faces;
void setup() {
opencv = new OpenCV(this, 640/2, 480/2);
size(640, 480);
// Kinectv2
kinect2 = new Kinect2(this);
kinect2.initVideo();
kinect2.initDevice();
opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);
faces = opencv.detect();
}
void draw() {
opencv.loadImage(kinect2.getVideoImage());
image(kinect2.getVideoImage(), 0, 0, 640, 480);
noFill();
stroke(0, 255, 0);
strokeWeight(3);
for (int i = 0; i < faces.length; i++) {
rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
}
}
the problem seems to be in the line "opencv.loadImage(kinect2.getVideoImage());" since the detection does not work. when working with the isight camera (using the build-in function "capture" and "video"-add-on) instead of kinect everything works perfectly fine.
can anyone help?
Upvotes: -1
Views: 1662
Reputation: 42176
In the future, please try to provide an MCVE. That means starting over with a blank sketch and only adding enough code so we can see the problem. In your case, we don't need to see any kinect code. Just load a hardcoded image and pass that to OpenCV.
Here is an example sketch that does exactly that. I got the image url from the human Wikipedia page.
import gab.opencv.*;
import java.awt.Rectangle;
PImage image;
OpenCV opencv;
void setup() {
size(500, 500);
image = loadImage("https://upload.wikimedia.org/wikipedia/commons/thumb/2/27/A_young_Man_and_Lady.png/800px-A_young_Man_and_Lady.png");
image.resize(width, height);
opencv = new OpenCV(this, width, height);
opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);
}
void draw() {
image(image, 0, 0);
opencv.loadImage(image);
Rectangle[] faces = opencv.detect();
noFill();
stroke(255, 0, 0);
strokeWeight(3);
for (Rectangle face : faces) {
rect(face.x, face.y, face.width, face.height);
}
}
Notice that I'm calling the opencv.detect()
function every frame. You're only calling it from setup()
, which means you're only detecting faces in the very first frame.
If you still can't get it working, then you're going to have to do some debugging. Try to isolate your problem as much as possible. Get rid of the kinect code and just use a hardcoded image. Take a screen capture of the image being obtained through your kinect, and use that instead of the live stream. Work in smaller steps, that way you can post a more specific question when you get stuck (it's hard to help with general "this isn't working" questions- it's much easier to help with specific "I tried X, expected Y, but got Z instead" type questions). Good luck.
Upvotes: 1