We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
opencv face-tracking (Read 1402 times)
opencv face-tracking
Nov 30th, 2009, 11:56am
 
hello,

i've got a problem that seems to be simple but its gonna drive me crazy. I want to use face-tracking to control the camera in a 3d-scene. that works quite fine except one thing. the face-tracking example simply draws a rectangle around my head. if i come closer to the monitor the rectangangle becomes bigger thats clear. to control the z-position of the camera i use the size of this rectangle. that works but is there a way to tell him, that the z-position decreases while the height of the rectangle increases? if i set the var negative, it just flips to the negative z-area. do you know what i mean. maybe you could try it yourself. the lib is installed in a minute  Wink . this is my code, i know theres a lot to optimize and to clean up but i'm new with processing and still learning...

Code:

import hypermedia.video.*;
import processing.opengl.*;
import codeanticode.glgraphics.*;

OpenCV opencv;

PFont futura;
float z = 0;
float camX = 0;
float camY = 0;
float camZ = 1000.0;
float camCenterY = 0;
float camCenterX = 0;
float camCenterZ = 0;
float a = -500;
boolean changedir = false;
float linerotone = 0;
float linerottwo = 0;
int side = 0;
float tr;

void setup() {
size(1024 , 768, OPENGL);
opencv = new OpenCV( this );
opencv.capture( width/4, height/4 ); // open video stream
opencv.cascade( OpenCV.CASCADE_FRONTALFACE_ALT_TREE ); // load detection description, here-> front face detection : "haarcascade_frontalface_alt.xml"
futura = createFont("DIN-Bold", 48);
fill(204);
frameRate(30);
smooth();
}

void draw() {
background(0);
opencv.read();
opencv.convert( GRAY );

Rectangle[] faces = opencv.detect( 1.2, 2, OpenCV.HAAR_DO_CANNY_PRUNING, 40, 40 );
image( opencv.image(), 0, 0 );

lights();
noFill();
stroke(0,0,255);
strokeWeight(1);
for( int i=0; i<faces.length; i++ ) {
rect( faces[i].x, faces[i].y, faces[i].width, faces[i].height );
camX = round(-(-width/2+faces[i].x*4)*HALF_PI);
camCenterX = (faces[i].x);
camY = round(((-height/2+faces[i].y*4)*HALF_PI)+200);
camCenterY = round(faces[i].y);
//z = faces[i].height*10;
camCenterZ = -z;
//camCenterZ = 0.0;
//z = dist(camX,camY,faces[i].height*10,camCenterX,camCenterY,camCenterZ);
z = mag(0,faces[i].height*10);
float distance = z;
println(z);
camZ = distance;
}

perspective();

// Change height of the camera with facedetect
camera(camX , camY, camZ, // eyeX, eyeY, eyeZ
camCenterX, camCenterY, camCenterZ, // centerX, centerY, centerZ
0.0 , 1.0, 0.0); // upX, upY, upZ
//ambientLight(100, 50, 50);
directionalLight(255,255,255,-1,0.5,0);

if(keyPressed){
if(key == 's'){
camZ+=10;
}
else if(key == 'w'){
camZ-=10.0;
}
}
noFill();
pushMatrix();
fill(255,50);
pushMatrix();
translate(2000,300,2000);
rotateX(HALF_PI);
rect(-4000,-4000,4000,4000);
popMatrix();
for(int side = 0; side<2000; side+=200){
stroke(125);
strokeWeight(2);
line(-(side),100,-(side),side,100,-(side));
line(side,100,-(side),side,100,side);
line(side,100,side,-(side),100,side);
line(-(side),100,side,-(side),100,-side);
}
popMatrix();
pushMatrix();
line(-2000,100,-2000,2000,100,2000);
line(2000,100,-2000,-2000,100,2000);
popMatrix();


strokeWeight(6);
linerotone+=0.01;
pushMatrix();
rotateZ(linerotone);
stroke(200,0,0);
line(10,10,0,0,180,0);
pushMatrix();
translate(0,180,0);
noStroke();
fill(255);
sphere(20);
popMatrix();
stroke(200,0,0);
line(-10,-10,0,0,-180,0);
pushMatrix();
translate(0,-180,0);
noStroke();
fill(255);
sphere(20);
popMatrix();
popMatrix();
pushMatrix();
rotateZ(linerotone*(-PI/2));
stroke(200,0,0);
line(10,10,0,0,200,-200);
pushMatrix();
translate(0,200,-200);
noStroke();
fill(255);
sphere(20);
popMatrix();
stroke(200,0,0);
line(-10,-10,0,0,-200,200);
pushMatrix();
translate(0,-200,200);
noStroke();
fill(255);
sphere(20);
popMatrix();
popMatrix();
linerottwo-=0.01;
pushMatrix();
rotateZ(linerotone*(PI/8));
stroke(200,0,0);
line(10,10,0,0,200,-400);
pushMatrix();
translate(0,200,-400);
noStroke();
fill(255);
sphere(20);
popMatrix();
stroke(200,0,0);
line(-10,-10,0,0,-200,400);
pushMatrix();
translate(0,-200,400);
noStroke();
fill(255);
sphere(20);
popMatrix();
popMatrix();
pushMatrix();
rotateZ(linerotone*(-PI/16));
stroke(200,0,0);
line(10,10,0,0,200,-600);
pushMatrix();
translate(0,200,-600);
noStroke();
fill(255);
sphere(20);
popMatrix();
stroke(200,0,0);
line(-10,-10,0,0,-200,600);
pushMatrix();
translate(0,-200,600);
noStroke();
fill(255);
sphere(20);
popMatrix();
popMatrix();

noStroke();
fill(0,255,0);
if(changedir == false){
a+=5;
}
if(a>=500){
changedir = true;
}
if(changedir == true){
a-=5;
}
if(a<=-500){
changedir = false;
}
tr+=0.005;
rotateY(tr);
textFont(futura);
text("...wir sind im Besitz der Mittel, aber ohne Idee!",-400,0,a);
//println(frameRate);

}
Page Index Toggle Pages: 1