So I’m doing a videogame where you use your face to move the gun and blink to shot.
Mi first attempt was doing it like this:
setup()
{
camara = new Capture( this, ancho, alto );
camara.start();
opencv = new OpenCV(this, ancho, alto );
opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);
}
draw()
{
camara.read();
opencv.loadImage( camara );
//detect face
rostros = opencv.detect();
for (int i = 0; i < rostros.length; i++)
{
PImage cara = camara.get(rostros[i].x, rostros[i].y, rostros[i].width, rostros[i].height);
//eyes opencv
opencv_ojos = new OpenCV(this, cara );
opencv_ojos.loadCascade(OpenCV.CASCADE_EYE);
Rectangle[] ojos = opencv_ojos.detect();
//draw eyes
stroke(0,0,255);
if(ojos.length!=0 && ojos.length!=1) //only show 2 eyes
{
for(int n=0; n<2;n++)
{
noFill();
rect(ojos[n].x + rostros[i].x, ojos[n].y + rostros[i].y,ojos[n].width,ojos[n].height);
}
}
}
And it worked, but the game renders slow because I’m creating the opencv_ojos object several times, so I tried another way:
setup(){
camara = new Capture( this, ancho, alto );
camara.start();
opencv = new OpenCV(this, ancho, alto );
opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);
opencv_ojos = new OpenCV(this, ancho,alto );
opencv_ojos.loadCascade(OpenCV.CASCADE_EYE);
}
draw()
{
camara.read();
opencv.loadImage( camara );
//detect face
rostros = opencv.detect();
for (int i = 0; i < rostros.length; i++)
{
PImage cara = camara.get(rostros[i].x, rostros[i].y, rostros[i].width, rostros[i].height);
// load pimage cara into opencv_ojos
opencv_ojos.loadImage(cara);
Rectangle[] ojos = opencv_ojos.detect();
//draw eyes
stroke(0,0,255);
if(ojos.length!=0 && ojos.length!=1)
{
for(int n=0; n<2;n++)
{
noFill();
rect(ojos[n].x + rostros[i].x, ojos[n].y + rostros[i].y,ojos[n].width,ojos[n].height);
}
}
}
But in this way, the program does not detect the eyes, so i don’t know what to do, any advice ?
Thanks!