import traer.physics.*; import hypermedia.video.*; OpenCV opencv; // contrast/brightness values int contrast_value = 0; int brightness_value = 0; Particle mouse, b, c; ParticleSystem physics; void setup() { size( 400, 400 ); frameRate( 24 ); smooth(); ellipseMode( CENTER ); noStroke(); noCursor(); 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" physics = new ParticleSystem( 0, 0.1 ); mouse = physics.makeParticle(); mouse.makeFixed(); b = physics.makeParticle( 1.0, random( 0, width ), random( 0, height ), 0 ); c = physics.makeParticle( 1.0, random( 0, width ), random( 0, height ), 0 ); physics.makeAttraction( mouse, b, 10000, 10 ); physics.makeAttraction( mouse, c, 10000, 10 ); physics.makeAttraction( b, c, -10000, 5 ); } void draw() { background( 255 ); opencv.read(); opencv.flip( OpenCV.FLIP_HORIZONTAL); 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 ); // draw face area(s) noFill(); stroke(255,0,0); for(int i = 0; i < faces.length; i++) { rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height); } if (faces.length > 0) { mouse.moveTo( faces[0].x, faces[0].y, 0 ); } else { mouse.moveTo( 200, 200, 0 ); } handleBoundaryCollisions( b ); handleBoundaryCollisions( c ); physics.tick(); fill( 255, 0, 0 ); ellipse( mouse.position().x(), mouse.position().y(), 35, 35 ); fill( 0, 255, 0 ); ellipse( b.position().x(), b.position().y(), 35, 35 ); fill( 0, 0, 255 ); ellipse( c.position().x(), c.position().y(), 35, 35 ); } // really basic collision strategy: // sides of the window are walls // if it hits a wall pull it outside the wall and flip the direction of the velocity // the collisions aren't perfect so we take them down a notch too void handleBoundaryCollisions( Particle p ) { if ( p.position().x() < 0 || p.position().x() > width ) p.setVelocity( -0.9*p.velocity().x(), p.velocity().y(), 0 ); if ( p.position().y() < 0 || p.position().y() > height ) p.setVelocity( p.velocity().x(), -0.9*p.velocity().y(), 0 ); p.moveTo( constrain( p.position().x(), 0, width ), constrain( p.position().y(), 0, height ), 0 ); } public void stop() { opencv.stop(); super.stop(); }