Advertisement
Guest User

Untitled

a guest
Dec 31st, 2008
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 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. int counter = 0;
  28. float stability = .1; //Controls how often it scales - 0 to 1
  29.  
  30. Rectangle[] lastfaces;
  31.  
  32. PFont font;
  33. String s;
  34. PImage img;
  35.  
  36. void setup() {
  37.  
  38. size(640,480);
  39. frameRate( 30 );
  40.  
  41. //I thought what I'd do was, I'd pretend I was one of those deaf-mutes
  42. img = loadImage("laugh.png");
  43.  
  44. opencv = new OpenCV( this );
  45. opencv.capture( width, height ); // open video stream
  46. //opencv.movie("m.mov", width, height);
  47. opencv.cascade( OpenCV.CASCADE_FRONTALFACE_ALT ); // load detection description, here-> front face detection : "haarcascade_frontalface_alt.xml"
  48.  
  49. // print usage
  50. println( "Drag mouse on X-axis inside this sketch window to change contrast" );
  51. println( "Drag mouse on Y-axis inside this sketch window to change brightness" );
  52.  
  53. }
  54.  
  55. public void stop() {
  56. opencv.stop();
  57. super.stop();
  58. }
  59.  
  60. void draw() {
  61. if(counter>=70) counter=0;
  62. img = loadImage("b/" + counter +".png");
  63. counter++;
  64. try{
  65.  
  66. // grab a new frame
  67. // and convert to gray
  68. opencv.read();
  69. opencv.flip(1);
  70. opencv.convert( GRAY );
  71. opencv.contrast( contrast_value );
  72. opencv.brightness( brightness_value );
  73.  
  74. // proceed detection
  75. Rectangle[] faces = opencv.detect( 1.2, 2, OpenCV.HAAR_DO_CANNY_PRUNING, 40, 40 );
  76.  
  77. // display the image
  78. image( opencv.image(), 0, 0 );
  79.  
  80. // to smooth it out
  81. if( faces.length > 0 ) {
  82. lastfaces = faces;
  83. }
  84.  
  85. if( lastfaces != null ) {
  86. //Use commented line to get more than one
  87. for( int i=0; i<1; i++ ) {
  88. //for( int i=0; i<lastfaces.length; i++ ) {
  89. pushMatrix();
  90. translate(lastfaces[i].x * .75, lastfaces[i].y * .75);
  91.  
  92. if( img != null ) {
  93. //the sc stuff is the scale - the width and height of the drawn image
  94. int sc = lastfaces[i].width-lastfaces[i].height;
  95. if(sc>0) {
  96. sc = lastfaces[i].width;
  97. }
  98. else {
  99. sc = lastfaces[i].height;
  100. }
  101. //the if-else here is necessary so it doesn�t rescale every frame, which looks bad
  102. if((sc>scp && sc<scp*(1+stability)) || (sc<scp && sc>scp*(1-stability))) {
  103. sc=scp;
  104. }
  105. else {
  106. scp=sc;
  107. }
  108. // Tweak this line for x and y offset
  109. image(img, -sc/8, -sc/4, (sc+150), (sc+150)*.9);
  110. }
  111. popMatrix();
  112. }
  113. }
  114.  
  115. } catch(Exception e) { e.printStackTrace(); }
  116. }
  117.  
  118. /**
  119. * Changes contrast/brigthness values
  120. */
  121. void mouseDragged() {
  122. contrast_value = (int) map( mouseX, 0, width, -128, 128 );
  123. brightness_value = (int) map( mouseY, 0, width, -128, 128 );
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement