Advertisement
Guest User

Deltadesu

a guest
Dec 31st, 2008
348
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. 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. 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.cascade( OpenCV.CASCADE_FRONTALFACE_ALT ); // load detection description, here-> front face detection : "haarcascade_frontalface_alt.xml"
  47.  
  48. // print usage
  49. println( "Drag mouse on X-axis inside this sketch window to change contrast" );
  50. println( "Drag mouse on Y-axis inside this sketch window to change brightness" );
  51.  
  52. }
  53.  
  54. public void stop() {
  55. opencv.stop();
  56. super.stop();
  57. }
  58.  
  59. void draw() {
  60.  
  61. try{
  62.  
  63. // grab a new frame
  64. // and convert to gray
  65. opencv.read();
  66. opencv.convert( GRAY );
  67. opencv.contrast( contrast_value );
  68. opencv.brightness( brightness_value );
  69.  
  70. // proceed detection
  71. Rectangle[] faces = opencv.detect( 1.2, 2, OpenCV.HAAR_DO_CANNY_PRUNING, 40, 40 );
  72.  
  73. // display the image
  74. image( opencv.image(), 0, 0 );
  75.  
  76. // to smooth it out
  77. if( faces.length > 0 ) {
  78. lastfaces = faces;
  79. }
  80.  
  81. if( lastfaces != null ) {
  82. //Use commented line to get more than one
  83. for( int i=0; i<1; i++ ) {
  84. //for( int i=0; i<lastfaces.length; i++ ) {
  85. pushMatrix();
  86. translate(lastfaces[i].x * .75, lastfaces[i].y * .75);
  87.  
  88. if( img != null ) {
  89. //the sc stuff is the scale - the width and height of the drawn image
  90. int sc = lastfaces[i].width-lastfaces[i].height;
  91. if(sc>0) {
  92. sc = lastfaces[i].width;
  93. }
  94. else {
  95. sc = lastfaces[i].height;
  96. }
  97. //the if-else here is necessary so it doesn�t rescale every frame, which looks bad
  98. if((sc>scp && sc<scp*(1+stability)) || (sc<scp && sc>scp*(1-stability))) {
  99. sc=scp;
  100. }
  101. else {
  102. scp=sc;
  103. }
  104. // Tweak this line for x and y offset
  105. image(img, -sc/8, -sc/4, sc+150, sc+150);
  106. }
  107. popMatrix();
  108. }
  109. }
  110.  
  111. } catch(Exception e) { e.printStackTrace(); }
  112. }
  113.  
  114. /**
  115. * Changes contrast/brigthness values
  116. */
  117. void mouseDragged() {
  118. contrast_value = (int) map( mouseX, 0, width, -128, 128 );
  119. brightness_value = (int) map( mouseY, 0, width, -128, 128 );
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement