Need help randomizing face overlays while tracking in OpenCV!
in
Contributed Library Questions
•
2 years ago
Hi Everyone, I'm still not well versed in processing, I'm a really good copy pasta and get the notion of what I'm doing most of the time.
I'm tinkering with OpenCv and have built an array into the Face Tracking demo. I've been successful in randomizing the image that load but I'd like to get each face to take on a unique image vs all in frame at once taking on the same... if you have any thoughts you would make me a much more sane person.
I began to implement Daniel Shiffman's timer class to break out the time between faces but I'm not really sure if that's the right solve. Any thoughts and/or additional solves would be extremely appreciated because I'm really not sure how to do this and I've been been staring at my monitor with red eyes for WAY too long.
Here is a link to the sketch with the /data folder [images] and the timer class.
here is the sketch code:
import processing.xml.*;
import hypermedia.video.*;
import java.awt.Rectangle;
PImage[] images = new PImage[13];
OpenCV opencv;
// contrast/brightness values
int contrast_value = 0;
int brightness_value = 0;
int index = 0;
//TIMER INITIALIZE
Timer timer;
void setup() {
size( 640,480, P2D );
//size(854,486);
smooth();
//TIMER SETUP
timer = new Timer(5000);
timer.start();
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" );
//chargement des images
images[0] = loadImage("face1.png");
images[1] = loadImage("face2.png");
images[2] = loadImage("face3.png");
images[3] = loadImage("face4.png");
images[4] = loadImage("face5.png");
images[5] = loadImage("face6.png");
images[6] = loadImage("face7.png");
images[7] = loadImage("face8.png");
images[8] = loadImage("face9.png");
images[9] = loadImage("face10.png");
images[10] = loadImage("face11.png");
images[11] = loadImage("face12.png");
images[12] = loadImage("face13.png");
index = int(random(images.length));
}
void draw() {
background(0);
// grab a new frame
opencv.read();
// and convert to gray
opencv.convert( RGB );
// flip horizontally
opencv.flip( OpenCV.FLIP_HORIZONTAL );
opencv.contrast( contrast_value );
opencv.brightness( brightness_value );
// proceed detection
Rectangle[] faces = opencv.detect( 1.2, 2, OpenCV.HAAR_DO_CANNY_PRUNING, 60, 60 );
// display the image
image( opencv.image(), 0, 0 );
// draw face area(s)
noFill();
stroke(255,0,0);
if( faces.length == 0 ) { // Number of people's faces.
println("faces 0");
index = int(random(images.length));
//TIMER CODE DRAW
if (timer.isFinished()) {
background(random(255));
timer.start();
println("I'm working on it fool, timer is hitting naow");
}
}
for( int i=0; i<faces.length; i++ ) {
rectMode(CENTER);
//rect( faces[i].x, faces[i].y, faces[i].width, faces[i].height );
image (images[index],faces[i].x, faces[i].y, faces[i].width, faces[i].height );
}
println(frameRate);
}
void stop(){
super.stop();
}
/**
* Changes contrast/brightness values
*/
void mouseDragged() {
contrast_value = (int) map( mouseX, 0, width, -128, 128 );
brightness_value = (int) map( mouseY, 0, width, -128, 128 );
}
________________________________________________________________________________________
//Timer class from daniel shiffman
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com
// Example 10-5: Object-oriented timer
class Timer {
int savedTime; // When Timer started
int totalTime; // How long Timer should last
Timer(int tempTotalTime) {
totalTime = tempTotalTime;
}
// Starting the timer
void start() {
// When the timer starts it stores the current time in milliseconds.
savedTime = millis();
}
// The function isFinished() returns true if 5,000 ms have passed.
// The work of the timer is farmed out to this method.
boolean isFinished() {
// Check how much time has passed
int passedTime = millis()- savedTime;
if (passedTime > totalTime) {
return true;
} else {
return false;
}
}
}
I'm tinkering with OpenCv and have built an array into the Face Tracking demo. I've been successful in randomizing the image that load but I'd like to get each face to take on a unique image vs all in frame at once taking on the same... if you have any thoughts you would make me a much more sane person.
I began to implement Daniel Shiffman's timer class to break out the time between faces but I'm not really sure if that's the right solve. Any thoughts and/or additional solves would be extremely appreciated because I'm really not sure how to do this and I've been been staring at my monitor with red eyes for WAY too long.
Here is a link to the sketch with the /data folder [images] and the timer class.
here is the sketch code:
import processing.xml.*;
import hypermedia.video.*;
import java.awt.Rectangle;
PImage[] images = new PImage[13];
OpenCV opencv;
// contrast/brightness values
int contrast_value = 0;
int brightness_value = 0;
int index = 0;
//TIMER INITIALIZE
Timer timer;
void setup() {
size( 640,480, P2D );
//size(854,486);
smooth();
//TIMER SETUP
timer = new Timer(5000);
timer.start();
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" );
//chargement des images
images[0] = loadImage("face1.png");
images[1] = loadImage("face2.png");
images[2] = loadImage("face3.png");
images[3] = loadImage("face4.png");
images[4] = loadImage("face5.png");
images[5] = loadImage("face6.png");
images[6] = loadImage("face7.png");
images[7] = loadImage("face8.png");
images[8] = loadImage("face9.png");
images[9] = loadImage("face10.png");
images[10] = loadImage("face11.png");
images[11] = loadImage("face12.png");
images[12] = loadImage("face13.png");
index = int(random(images.length));
}
void draw() {
background(0);
// grab a new frame
opencv.read();
// and convert to gray
opencv.convert( RGB );
// flip horizontally
opencv.flip( OpenCV.FLIP_HORIZONTAL );
opencv.contrast( contrast_value );
opencv.brightness( brightness_value );
// proceed detection
Rectangle[] faces = opencv.detect( 1.2, 2, OpenCV.HAAR_DO_CANNY_PRUNING, 60, 60 );
// display the image
image( opencv.image(), 0, 0 );
// draw face area(s)
noFill();
stroke(255,0,0);
if( faces.length == 0 ) { // Number of people's faces.
println("faces 0");
index = int(random(images.length));
//TIMER CODE DRAW
if (timer.isFinished()) {
background(random(255));
timer.start();
println("I'm working on it fool, timer is hitting naow");
}
}
for( int i=0; i<faces.length; i++ ) {
rectMode(CENTER);
//rect( faces[i].x, faces[i].y, faces[i].width, faces[i].height );
image (images[index],faces[i].x, faces[i].y, faces[i].width, faces[i].height );
}
println(frameRate);
}
void stop(){
super.stop();
}
/**
* Changes contrast/brightness values
*/
void mouseDragged() {
contrast_value = (int) map( mouseX, 0, width, -128, 128 );
brightness_value = (int) map( mouseY, 0, width, -128, 128 );
}
________________________________________________________________________________________
//Timer class from daniel shiffman
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com
// Example 10-5: Object-oriented timer
class Timer {
int savedTime; // When Timer started
int totalTime; // How long Timer should last
Timer(int tempTotalTime) {
totalTime = tempTotalTime;
}
// Starting the timer
void start() {
// When the timer starts it stores the current time in milliseconds.
savedTime = millis();
}
// The function isFinished() returns true if 5,000 ms have passed.
// The work of the timer is farmed out to this method.
boolean isFinished() {
// Check how much time has passed
int passedTime = millis()- savedTime;
if (passedTime > totalTime) {
return true;
} else {
return false;
}
}
}
1