Advertisement
Guest User

Deltadesu

a guest
Dec 31st, 2008
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. import processing.xml.*;
  2. import hypermedia.video.*;
  3.  
  4. /*
  5.  
  6. REQUIRES:
  7. OpenCV library for Processing
  8.  
  9. KNOWN BUGS:
  10. 1) Text doesn't rotate. :(
  11. 2) Fast movement head movement will lose the head-tracker for a second.
  12. 3) Still some flicker issues, but better than it was...
  13.  
  14. No express purpose is intended or implied, use at own risk.
  15.  
  16. Ben Kurtz
  17. 12/15/08
  18.  
  19. */
  20.  
  21. OpenCV opencv;
  22.  
  23. // contrast/brightness values
  24. int contrast_value = 0;
  25. int brightness_value = 0;
  26. int scp = 0;
  27. float stability = .1; //Controls how often it scales - 0 to 1
  28.  
  29. Rectangle[] lastfaces;
  30.  
  31. PFont font;
  32. String s;
  33. PImage img;
  34.  
  35. void setup() {
  36.  
  37. size(640,480);
  38. frameRate( 30 );
  39.  
  40. //I thought what I'd do was, I'd pretend I was one of those deaf-mutes
  41. img = loadImage("laugh.png");
  42.  
  43. opencv = new OpenCV( this );
  44. opencv.capture( width, height ); // open video stream
  45. opencv.cascade( OpenCV.CASCADE_FRONTALFACE_ALT ); // load detection description, here-> front face detection : "haarcascade_frontalface_alt.xml"
  46.  
  47. // print usage
  48. println( "Drag mouse on X-axis inside this sketch window to change contrast" );
  49. println( "Drag mouse on Y-axis inside this sketch window to change brightness" );
  50.  
  51. }
  52.  
  53. public void stop() {
  54. opencv.stop();
  55. super.stop();
  56. }
  57.  
  58. void draw() {
  59.  
  60. try{
  61.  
  62. // grab a new frame
  63. // and convert to gray
  64. opencv.read();
  65. opencv.convert( GRAY );
  66. opencv.contrast( contrast_value );
  67. opencv.brightness( brightness_value );
  68.  
  69. // proceed detection
  70. Rectangle[] faces = opencv.detect( 1.2, 2, OpenCV.HAAR_DO_CANNY_PRUNING, 40, 40 );
  71.  
  72. // display the image
  73. image( opencv.image(), 0, 0 );
  74.  
  75. // to smooth it out
  76. if( faces.length > 0 ) {
  77. lastfaces = faces;
  78. }
  79.  
  80. if( lastfaces != null ) {
  81. //Use commented line to get more than one
  82. for( int i=0; i<1; i++ ) {
  83. //for( int i=0; i<lastfaces.length; i++ ) {
  84. pushMatrix();
  85. translate(lastfaces[i].x * .75, lastfaces[i].y * .75);
  86.  
  87. if( img != null ) {
  88. //the sc stuff is the scale - the width and height of the drawn image
  89. int sc = lastfaces[i].width-lastfaces[i].height;
  90. if(sc>0) {
  91. sc = lastfaces[i].width;
  92. }
  93. else {
  94. sc = lastfaces[i].height;
  95. }
  96. //the if-else here is necessary so it doesn�t rescale every frame, which looks bad
  97. if((sc>scp && sc<scp*(1+stability)) || (sc<scp && sc>scp*(1-stability))) {
  98. sc=scp;
  99. }
  100. else {
  101. scp=sc;
  102. }
  103. // Tweak this line for x and y offset
  104. image(img, -sc/8, -sc/4, sc+150, sc+150);
  105. }
  106. popMatrix();
  107. }
  108. }
  109.  
  110. } catch(Exception e) { e.printStackTrace(); }
  111. }
  112.  
  113. /**
  114. * Changes contrast/brigthness values
  115. */
  116. void mouseDragged() {
  117. contrast_value = (int) map( mouseX, 0, width, -128, 128 );
  118. brightness_value = (int) map( mouseY, 0, width, -128, 128 );
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement