package servers;
/**
* (./) FaceDetection.java, 03/05/08
* (by) cousot stephane @ http://www.ubaa.net/
* (cc) some right reserved
*
* Sample program for "OpenCV" project.
* Use ESC key to close the program properly.
*
* This sample is released under a Creative Commons Attribution 3.0 License
* ‹ http://creativecommons.org/licenses/by/3.0/ ›
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.image.MemoryImageSource;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import processing.core.PImage;
import utils.ConsoleIO;
import hypermedia.video.OpenCV;
public class FaceDetection extends Frame implements Runnable {
// program execution frame rate (millisecond)
final int FRAME_RATE = 1000/30;
boolean isWorking = false;
Frame frameCanvas;
OpenCV cv = null; // OpenCV Object
Thread t = null; // the sample thread
boolean showProcess = true;
// the input video stream image
Image frame = null;
PImage image = null;
// list of all face detected areax
Rectangle[] faces;// = new Rectangle[0];
Rectangle[] eyes = new Rectangle[0];
public FaceDetection(){
cv = new OpenCV();
}
public Rectangle[] analyze(PImage image){
isWorking = true;
faces = new Rectangle[0];
this.image = image;
// start running program
t = new Thread( this );
// poner un flag
t.start();
while(isWorking);
return faces;
}
public void openFrame(){
// frame setup
this.setBounds( 0, -cv.height, cv.width, cv.height );
this.setBackground( Color.BLACK );
this.setVisible( true );
this.addKeyListener(
new KeyAdapter() {
public void keyReleased( KeyEvent e ) {
if ( e.getKeyCode()==KeyEvent.VK_ESCAPE ) { // ESC : release OpenCV resources
cv.dispose();
System.exit(0);
}
}
}
);
}
/**
* Draw video frame and each detected faces area.
*/
public void paint( Graphics g ) {
// draw image
g.drawImage( frame, 0, 0, null );
// draw squares
g.setColor( Color.RED );
for( Rectangle f : faces ){
g.drawRect( f.x, f.y, f.width, f.height );
}
}
/**
* Execute this sample.
*/
public void run() {
//cv.dispose();
cv.allocate(image.width, image.height);
cv.copy(image); // esto es una cagada porque tengo que pasar por PIMage...
cv.read();
cv.cascade( OpenCV.CASCADE_FRONTALFACE_ALT );
faces = cv.detect( 1.1f, 3, 0, 50, 50 );
isWorking = false;
if(showProcess){
// create a new image from cv pixels data
MemoryImageSource mis = new MemoryImageSource( cv.width, cv.height, cv.pixels(), 0, cv.width );
frame = createImage( mis );
repaint();
}
}
/**
* Main method.
* @param String[] a list of user's arguments passed from the console to this program
*/
public static void main( String[] args ) {
System.out.println( "\nOpenCV face detection sample\n" );
new FaceDetection();
}
public Rectangle[] getFaces() {
return faces;
}
}