// port forwarding if you need it: // ssh -L 3306:localhost:3306 rtwomey@192.168.0.183 import hypermedia.video.*; import java.awt.Rectangle; import de.bezier.data.sql.*; import java.util.Collections; import java.util.Random; OpenCV opencv; // contrast/brightness values int contrast_value = 0; int brightness_value = 0; // ---- face tracking stuff ---- // last recognized data int x_pos = 0; int y_pos = 0; int w_face = 0; int h_face = 0; // to store face recognition data int MAX = 10; int[] x = new int[MAX]; int[] y = new int[MAX]; int[] w = new int[MAX]; int[] h = new int[MAX]; // for data smoothing int SAMPLES = 10; int PERSISTENCE=5; int curr=0; int lastface=0; int[] sx = new int[SAMPLES]; int[] sy = new int[SAMPLES]; int[] sw = new int[SAMPLES]; int[] sh = new int[SAMPLES]; PImage img, blackimg; // SQL stuff MySQL msql; PFont font; // what table and what column to query in db // this determines what data is played back String table = "media"; String ftable = "videofaces"; String column = "name"; PImage input; // vars holding response most recent query String data= ""; String path=""; String fname=""; String filetype=""; // list of table IDs matching query ArrayList imageIDs; Random repeatableRandom; //int i=1900; //int i=2163; //int i=487; //int i=9; int filenum=257; int facenum=0; int frame=0; int id=0; // timing int last=0; boolean paused=false; void setup() { // general graphics setup //size( 1280, 720 ); size(1920, 1200);//screen.height, screen.height); background(0); smooth(); blackimg = createImage(width, height, RGB); blackimg.loadPixels(); for (int i = 0; i < blackimg.pixels.length; i++) { blackimg.pixels[i] = color(0, 0, 0); } strokeWeight(2); stroke(255,200,0); noFill(); // Font and text drawing setup font = loadFont("HelveticaNeue-48.vlw"); textAlign(CENTER); textAlign(CENTER,CENTER); rectMode(CORNER); fill(255); textFont(font, 64); input = createImage(width-50, height-50, 50); // this example assumes that you are running the // mysql server locally (on "localhost"). // // replace --username--, --password-- with your mysql-account. // String user = "bob"; String pass = "password"; // name of the database to use String database = "naming"; // connect to database of server "localhost" msql = new MySQL( this, "localhost", database, user, pass ); if (msql.connect()) { msql.execute( "CREATE TABLE IF NOT EXISTS " +ftable + " ("+"id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, image_id INT (11), filename VARCHAR( 100 ), x_coord VARCHAR(10), y_coord VARCHAR(10), width VARCHAR(10), height VARCHAR(10))"); } else { println("connection failed!"); } msql.query( "SELECT COUNT(*) FROM " + table+" WHERE filetype=\"video\""); msql.next(); println( "number of rows: " + msql.getInt(1) ); imageIDs = new ArrayList(); // build list of image files and shuffle msql.query("SELECT * FROM "+table+" WHERE filetype=\"video\""); while(msql.next()) { data=msql.getString("name"); id=msql.getInt("id"); println(data+" "+id); imageIDs.add(id); } repeatableRandom = new Random(5); Collections.shuffle(imageIDs, repeatableRandom); last=-1000; openNextVideoFile(); initializeAvg(); } void openVideoFile(int i) { opencv = new OpenCV( this ); opencv.cascade( OpenCV.CASCADE_FRONTALFACE_ALT ); // load detection description, here-> front face detection : "haarcascade_frontalface_alt.xml" // opencv.cascade(OpenCV.CASCADE_PROFILEFACE); // select image file data from random row msql.query("SELECT * FROM "+table+" WHERE id= "+imageIDs.get(i)); if(msql.next() == true) { data = msql.getString("name"); id = msql.getInt("id"); path = msql.getString("path"); fname = msql.getString("fname"); opencv.movie( path+"/"+fname);//, width, height ); // load movie file frame=0; } } void openNextVideoFile() { filenum++; if(filenum>=imageIDs.size()) { exit(); } openVideoFile(filenum); } void openPreviousVideoFile() { filenum--; if(filenum<0)filenum=imageIDs.size()-1; openVideoFile(filenum); } void fillAvg(int x, int y, int w, int h) { for(int i=0;i= input.height) { // sf=float(width)/float(input.width); // } // else { // sf=float(height)/float(input.height); // } // scaledW=round(input.width*sf); // scaledH=round(input.height*sf); // xoff=(width-scaledW)/2; // yoff=(height-scaledH)/2; // println(" input image: "+input.width+" "+input.height+" "+sf+" "+sw+" "+sh+" "+xoff+" "+yoff); // opencv.copy(input);//, 0, 0, input.width, input.height, xoff, yoff, sw, sh); // proceed detection Rectangle[] faces = opencv.detect( 1.2, 2, OpenCV.HAAR_DO_CANNY_PRUNING, 40, 40 ); // display the image image( opencv.image(), 0, 0); // draw face area(s) noFill(); stroke(255,0,0); int count = faces.length; if (count>0) { // data smoothing curr++; if(curr>=SAMPLES)curr=0; if(lastface==0) fillAvg(faces[0].x, faces[0].y, faces[0].width, faces[0].height); sx[curr]=faces[0].x; sy[curr]=faces[0].y; sw[curr]=faces[0].width; sh[curr]=faces[0].height; //stroke(0, 0, 255); //drawAvg(); //stroke(255,0,0); // detected faces for( int i=0; i0) { stroke(0, 0, 255); storeFaceInDB(filenum);//drawAvg(); stroke(255,0,0); lastface--; //println(" "+lastface); }; } last=millis(); } fill(255, 255, 255, map(millis()-last, 0, 1000, 0, 255)); int fheight = 36; textFont(font, fheight); text(id+" "+data, width/2, height-fheight-20);//width/2, 200); // fill(192, 192, 192, map(millis()-last, 0, 1000, 0, 255)); // textFont(font, 16); // text(id, width/2, height*.6666); } } void storeFaceInDB(int k) { String vidName = id+"_"+frame+"_"+k+".jpg"; //saveFrame("images/"+vidName); // PImage face = input.get(x_pos, y_pos, w_face, h_face); float ax, ay, aw, ah; ax=0; ay=0; aw=0; ah=0; for(int i=0;i