Advertisement
Guest User

Untitled

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