Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import processing.xml.*;
- import hypermedia.video.*;
- /*
- REQUIRES:
- OpenCV library for Processing
- KNOWN BUGS:
- 1) Text doesn't rotate. :(
- 2) Fast movement head movement will lose the head-tracker for a second.
- 3) Still some flicker issues, but better than it was...
- No express purpose is intended or implied, use at own risk.
- Ben Kurtz
- awgh@awgh.org
- 12/15/08
- */
- OpenCV opencv;
- // contrast/brightness values
- int contrast_value = 0;
- int brightness_value = 0;
- int scp = 0;
- float stability = .1; //Controls how often it scales - 0 to 1
- Rectangle[] lastfaces;
- PFont font;
- String s;
- PImage img;
- void setup() {
- size(640,480);
- frameRate( 30 );
- //I thought what I'd do was, I'd pretend I was one of those deaf-mutes
- img = loadImage("laugh.png");
- opencv = new OpenCV( this );
- opencv.capture( width, height ); // open video stream
- opencv.cascade( OpenCV.CASCADE_FRONTALFACE_ALT ); // load detection description, here-> front face detection : "haarcascade_frontalface_alt.xml"
- // print usage
- println( "Drag mouse on X-axis inside this sketch window to change contrast" );
- println( "Drag mouse on Y-axis inside this sketch window to change brightness" );
- }
- public void stop() {
- opencv.stop();
- super.stop();
- }
- void draw() {
- try{
- // grab a new frame
- // and convert to gray
- opencv.read();
- opencv.convert( GRAY );
- opencv.contrast( contrast_value );
- opencv.brightness( brightness_value );
- // proceed detection
- Rectangle[] faces = opencv.detect( 1.2, 2, OpenCV.HAAR_DO_CANNY_PRUNING, 40, 40 );
- // display the image
- image( opencv.image(), 0, 0 );
- // to smooth it out
- if( faces.length > 0 ) {
- lastfaces = faces;
- }
- if( lastfaces != null ) {
- //Use commented line to get more than one
- for( int i=0; i<1; i++ ) {
- //for( int i=0; i<lastfaces.length; i++ ) {
- pushMatrix();
- translate(lastfaces[i].x * .75, lastfaces[i].y * .75);
- if( img != null ) {
- //the sc stuff is the scale - the width and height of the drawn image
- int sc = lastfaces[i].width-lastfaces[i].height;
- if(sc>0) {
- sc = lastfaces[i].width;
- }
- else {
- sc = lastfaces[i].height;
- }
- //the if-else here is necessary so it doesn�t rescale every frame, which looks bad
- if((sc>scp && sc<scp*(1+stability)) || (sc<scp && sc>scp*(1-stability))) {
- sc=scp;
- }
- else {
- scp=sc;
- }
- // Tweak this line for x and y offset
- image(img, -sc/8, -sc/4, sc+150, sc+150);
- }
- popMatrix();
- }
- }
- } catch(Exception e) { e.printStackTrace(); }
- }
- /**
- * Changes contrast/brigthness values
- */
- void mouseDragged() {
- contrast_value = (int) map( mouseX, 0, width, -128, 128 );
- brightness_value = (int) map( mouseY, 0, width, -128, 128 );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement