photobooth using opencv face detection
in
Contributed Library Questions
•
1 year ago
Hi all,
I'm trying to program an interaction using opencv, in which
1) The script looks for faces in a video feed
2) When a face is found, it calculates the area of the face
3) If the area is larger than a set number, it starts a photobooth. Otherwise, start from (1)
4) When in photobooth mode, the screen flashes the "3" for one second, "2" for one second, "1" for one second, and then it captures the frame
5) After the frame is captured, the image is displayed on screen for as long as the person stands in front of the camera (as long as a face is still detected)
6) When a face is no longer detected, start over.
I'm having problems with (4) and (5)
This is the script I have so far. In place of 4, I have it display a word "hello" for 3 seconds, but what I'm getting is a blank screen for three seconds, then a captured image, then a blank screen for three seconds, etc.
import hypermedia.video.*;
import java.awt.Rectangle;
boolean doCapture = false; // determines whether to enter sequence
int prevMillis;
int count;
OpenCV opencv;
OpenCV photob;
PFont font;
PImage photo;
void setup() {
size(640,480);
// Font
font = loadFont("skyhook.vlw");
textFont(font, 200);
// OpenCV
opencv=new OpenCV(this); // when doCapture=false
opencv.capture(width,height,1);
opencv.cascade(OpenCV.CASCADE_FRONTALFACE_ALT); // load the FRONTALFACE description file
photob=new OpenCV(this); // when doCapture=true
photob.capture(width,height,24);
}
void draw() {
if (!doCapture) {
opencv.read();
image( opencv.image(), 0, 0 );
// detect anything resembling a FRONTALFACE
Rectangle[] faces = opencv.detect(1.2, 2, 0, 1, 1);
// check if found face meets the conditions for triggering a capture.
for( int i=0; i<faces.length; i++ ) {
// Is the face at least 7.5% the total area of the screen?
if( ((faces[i].width * faces[i].height) > (.075 * width * height)) ){
doCapture = true;
}
else {
doCapture = false;
}
}
}
// If the face is at least 7.5% the total area of the screen
if(doCapture) {
background(0); // clear background
prevMillis = millis(); // start time counter
photobooth(); // start photobooth sequence
}
}
// photobooth sequence
void photobooth() {
photob.read();
image( photob.image(), 0, 0);
// insert text in photobooth mode
fill(255,0,55,100);
textAlign(CENTER,BOTTOM);
text("hello", width/2, height/1);
println (millis() - prevMillis); // troubleshooting time count
// if the time elapsed is less than 3 seconds
if( millis() - prevMillis < 3000) {
photobooth();
}
// if the time elapsed is more than 3 seconds
else {
image( photob.image(), 0, 0); // load video feed without text
saveFrame("image" + count + ".jpg"); // save image
photo = loadImage("image" + count + ".jpg"); // display image on screen
count++; // add 1 to the file number
}
}
Any help would be greatly appreciated, thanks!
1