Facial recognition and arrays
in
Contributed Library Questions
•
9 months ago
Hi ,
I looked at other reverse array topics but nothing seems to work with my sketch.
I'm working with code that draws a tree and I've used OpenCV to draw only when it sees your face. I would like to reverse the array when it stops seeing it, instead of just pausing the drawing. Would anyone know how to do this?
Thanks!
import hypermedia.video.*;
import java.awt.Rectangle;
import codeanticode.glgraphics.*;
OpenCV opencv;
boolean Rectangle[] ;
int numcolor= int (random (0,255));
float angle;
int col[];
void setup(){
opencv = new OpenCV(this);
opencv.capture(width, height);
opencv.cascade(OpenCV.CASCADE_FRONTALFACE_ALT);
col= new int [numcolor] ;
background (200);
size(800,800,OPENGL);
//stroke(255);
fill(255);
strokeWeight(100);
// stroke(0);
rotate(random(-PI/3,PI/4));
smooth();
frameRate(30);
//strokeCap(ROUND);
}
float[] calculateDirectionVector(float angle,float speed){
angle+=0.12;
angle*=6.28;//straight growth
float[] returnValue={(cos(angle)-sin(angle))*2,(sin(angle)+cos(angle))*2};
return returnValue;
}
float[] tempPosition;
int numberOfGenerations=100;
int initialThickness=110;
int wait=100;
int frameNumber=0;
float[][] sprouts={{300,700,300,700,0.5+random(-0.001,0.001),random(-0.03,0.03),0,200+random(10),0,1}}; //intial branch angles of growth and number of generations
//must be 0.5 to grow straight
void addSprout(float positionX,float positionY,float direction,float generation,float parentLength)
{
if (generation<numberOfGenerations){
float[] tempSprout={positionX,positionY,positionX,positionY,direction+random(-0.15,0.15),random(-0.03,0.03),0,parentLength/2.0+random(parentLength/2.0),0,generation+2};
sprouts=(float[][])expand(sprouts,sprouts.length+1);
sprouts[sprouts.length-1]=tempSprout;
}
if ( positionX>= 600) { stroke ( 29,100,20); }
else { stroke (29,20,20);
}}
void draw(){
angle+=0.12;
angle*=6.28;
opencv.read();
opencv.convert(GRAY);
Rectangle[] faces = opencv.detect( 1.2, 2, OpenCV.HAAR_DO_CANNY_PRUNING, 40, 40 );
for ( int f=0; f<faces.length; f++ ) {
for(int i=0;i<sprouts.length;i++){
if(sprouts[i][8]<sprouts[i][7]){
strokeWeight(initialThickness/sprouts[i][9]);
line(sprouts[i][2],sprouts[i][3],sprouts[i][0],sprouts[i][1]);// initial branch
//translate(100,-600);
sprouts[i][2]=sprouts[i][0];
sprouts[i][3]=sprouts[i][1];
tempPosition=calculateDirectionVector(sprouts[i][4]%1+0.02*sin(sprouts[i][8]/10.0),0.9);
sprouts[i][0]+=tempPosition[0];
sprouts[i][1]+=tempPosition[1];
sprouts[i][4]+=sprouts[i][5]*sprouts[i][6];
if(sprouts[i][8]>sprouts[i][7]*0.12){
sprouts[i][6]+=0.001/(sprouts[i][7]/100.0);
if(random(1)<0.05)addSprout(sprouts[i][0],sprouts[i][1],sprouts[i][4],sprouts[i][9],sprouts[i][7]);
} // change 0.1 to make it sprout more/less
sprouts[i][8]++;
}
else { // if it doesn't see a face then grow backwards
for ( int tempf=0; tempf>faces.length; tempf++ ) {
float[] temp =sprouts[i];
sprouts[i] = sprouts[sprouts.length - i - 1];
sprouts[sprouts.length - i - 1] = temp;
}
// if(sprouts[i][8]<sprouts[i][7]){
// strokeWeight(initialThickness/sprouts[i][9]);
// line(sprouts[i][2],sprouts[i][3],sprouts[i][0],sprouts[i][1]);// initial branch
//translate(100,-600);
// sprouts[i][2]*=-sprouts[i][0];
// sprouts[i][3]*=-sprouts[i][1];
// tempPosition=calculateDirectionVector(sprouts[i][4]%1+0.02*-sin(sprouts[i][8]/10.0),0.9);
// sprouts[i][0]*=-tempPosition[0];
// sprouts[i][1]*=-tempPosition[1];
// sprouts[i][4]*=-sprouts[i][5]*sprouts[i][6];
// if(sprouts[i][8]>sprouts[i][7]*-0.12){
// sprouts[i][6]*=-0.001/(sprouts[i][7]/100.0);
// if(random(1)<0.05)addSprout(sprouts[i][0],sprouts[i][1],sprouts[i][4],sprouts[i][9],sprouts[i][7]);
// } // change 0.1 to make it sprout more/less
// sprouts[i][8]--;
// }
}}
frameNumber++;
}}
1