Face tracking with OpenCV using threads
in
Contributed Library Questions
•
1 years ago
Hi,
Sorry for my english. I'm trying to code for a final project a Processing game with a moving 3D scenario from player's perspective using a webcam and face detection/tracking. I tested the example code at http://ubaa.net/shared/processing/opencv/opencv_detect.html with success but when I included the face tracking in the sketch where the OpenGL environment is, the FPS falls down from 40 to 7-8.
I read about using threads to solved this kind of issues, but I'm a newbie programmer and after hours of "test & error" I couldn't hit the nail on the head.
Has anyone done something similar? I paste below what is my incomplete attempt of the "opencv_detect" example implemented with threads. I have no idea how to display the camera image and draw the face rectangles on screen calling the detectFace class.
Thanks in advance!
Exequiel
Sorry for my english. I'm trying to code for a final project a Processing game with a moving 3D scenario from player's perspective using a webcam and face detection/tracking. I tested the example code at http://ubaa.net/shared/processing/opencv/opencv_detect.html with success but when I included the face tracking in the sketch where the OpenGL environment is, the FPS falls down from 40 to 7-8.
I read about using threads to solved this kind of issues, but I'm a newbie programmer and after hours of "test & error" I couldn't hit the nail on the head.
Has anyone done something similar? I paste below what is my incomplete attempt of the "opencv_detect" example implemented with threads. I have no idea how to display the camera image and draw the face rectangles on screen calling the detectFace class.
Thanks in advance!
Exequiel
- import hypermedia.video.*;
import java.awt.Rectangle;
OpenCV opencv;
detectFace detectthread;
void setup() {
size( 320, 240 );
detectthread= new detectFace(this);
detectthread.start();
}
void draw() {
Rectangle[] faces = detectthread.getRectangles();
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 );
}
}
- import java.awt.Rectangle;
import processing.core.PApplet;
import hypermedia.video.*;
public class detectFace extends Thread {
private boolean running;
private OpenCV opencv;
private PApplet parent;
private Rectangle[] faces= new Rectangle[0];
public detectFace (PApplet pa) {
parent=pa;
running = false;
}
public void start ()
{
running = true;
opencv = new OpenCV(parent);
opencv.capture(320, 240);
opencv.cascade( OpenCV.CASCADE_FRONTALFACE_ALT );
super.start();
}
public Rectangle[] getRectangles() {
return faces;
}
public void run ()
{
while (running) {
opencv.read();
Rectangle[] faces = opencv.detect( (float)1.2, 2, OpenCV.HAAR_DO_CANNY_PRUNING, 20, 20 );
}
}
}
1