Speeding up face detection
in
Contributed Library Questions
•
2 years ago
Hi
I'm new to processing but have made a couple of sketches. I still dont fully understand how to form the code so please excuse me.
I am trying to make a simple sketch that detects presence of a viewer by detecting their face using openCV lib. When a face is detected it changes the image displayed (from image_1 to image_2). I am trying to speed the whole thing up and was wondering if using threads to split the tasks would work?
1 thread for detecting the face and one for changing the image. I am having trouble getting the threads to work and i'm not sure this is the right way to go about speeding up the program. Here is the code for the simpler one that works that i havent tried to thread.
Thanks
import hypermedia.video.*;
import java.awt.Rectangle;
int facecount = 0;
OpenCV opencv;
PImage b;
PImage a;
final static float fps=24;
long t = 0L, drawt = 0L;
void setup() {
frameRate (fps);
b= loadImage("image_2.jpg");
a= loadImage("image_1.jpg");
size(640, 480);
opencv = new OpenCV(this);
opencv.capture( width, height );
opencv.cascade( OpenCV.CASCADE_FRONTALFACE_ALT ); // load the FRONTALFACE description file
t = System.currentTimeMillis();
}
void draw() {
timelog("draw() : fps = "+(((double)1000)/((System.currentTimeMillis()-drawt))),drawt);
// tint(255, 100);
opencv.read();
image( opencv.image(), 0, 0 );
timelog("read image");
// detect anything ressembling a FRONTALFACE
Rectangle[] faces = opencv.detect();
facecount = faces.length;
timelog("detect_faces");
// draw detected face area(s)
noFill();
stroke(201,101,171);
for( int i=0; i<faces.length; i++ ) {
rect( faces[i].x, faces[i].y, faces[i].width, faces[i].height );
}
timelog("draw face rects");
if(facecount>0) {
image(b, 0, 0);
println("I SEE YOU!");
}
else {
image(a, 0, 0);
println("Wheres Ee To?");
}
timelog("draw image");
}
void timelog(String s, long tmpt) {
println(s+" : "+(System.currentTimeMillis()-tmpt)+"ms");
tmpt = System.currentTimeMillis();
}
void timelog(String s) {
println(s+" : "+(System.currentTimeMillis()-t)+"ms");
t = System.currentTimeMillis();
}
I'm new to processing but have made a couple of sketches. I still dont fully understand how to form the code so please excuse me.
I am trying to make a simple sketch that detects presence of a viewer by detecting their face using openCV lib. When a face is detected it changes the image displayed (from image_1 to image_2). I am trying to speed the whole thing up and was wondering if using threads to split the tasks would work?
1 thread for detecting the face and one for changing the image. I am having trouble getting the threads to work and i'm not sure this is the right way to go about speeding up the program. Here is the code for the simpler one that works that i havent tried to thread.
Thanks
import hypermedia.video.*;
import java.awt.Rectangle;
int facecount = 0;
OpenCV opencv;
PImage b;
PImage a;
final static float fps=24;
long t = 0L, drawt = 0L;
void setup() {
frameRate (fps);
b= loadImage("image_2.jpg");
a= loadImage("image_1.jpg");
size(640, 480);
opencv = new OpenCV(this);
opencv.capture( width, height );
opencv.cascade( OpenCV.CASCADE_FRONTALFACE_ALT ); // load the FRONTALFACE description file
t = System.currentTimeMillis();
}
void draw() {
timelog("draw() : fps = "+(((double)1000)/((System.currentTimeMillis()-drawt))),drawt);
// tint(255, 100);
opencv.read();
image( opencv.image(), 0, 0 );
timelog("read image");
// detect anything ressembling a FRONTALFACE
Rectangle[] faces = opencv.detect();
facecount = faces.length;
timelog("detect_faces");
// draw detected face area(s)
noFill();
stroke(201,101,171);
for( int i=0; i<faces.length; i++ ) {
rect( faces[i].x, faces[i].y, faces[i].width, faces[i].height );
}
timelog("draw face rects");
if(facecount>0) {
image(b, 0, 0);
println("I SEE YOU!");
}
else {
image(a, 0, 0);
println("Wheres Ee To?");
}
timelog("draw image");
}
void timelog(String s, long tmpt) {
println(s+" : "+(System.currentTimeMillis()-tmpt)+"ms");
tmpt = System.currentTimeMillis();
}
void timelog(String s) {
println(s+" : "+(System.currentTimeMillis()-t)+"ms");
t = System.currentTimeMillis();
}
1