Advertisement
Guest User

PanTiltFaceTracking

a guest
Jun 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.36 KB | None | 0 0
  1. /**********************************************************************************************
  2. * Pan/Tilt Face Tracking Sketch
  3. * Written by Ryan Owens for SparkFun Electronics
  4. * Uses the OpenCV real-time computer vision  framework from Intel
  5. * Based on the OpenCV Processing Examples from ubaa.net
  6. * This example is released under the Beerware License.
  7. * (Use the code however you'd like, but mention us and by me a beer if we ever meet!)
  8. *
  9. * The Pan/Tilt Face Tracking Sketch interfaces with an Arduino Main board to control
  10. * two servos, pan and tilt, which are connected to a webcam. The OpenCV library
  11. * looks for a face in the image from the webcam. If a face is detected the sketch
  12. * uses the coordinates of the face to manipulate the pan and tilt servos to move the webcam
  13. * in order to keep the face in the center of the frame.
  14. *
  15. * Setup-
  16. * A webcam must be connected to the computer.
  17. * An Arduino must be connected to the computer. Note the port which the Arduino is connected on.
  18. * The Arduino must be loaded with the SerialServoControl Sketch.
  19. * Two servos mounted on a pan/tilt backet must be connected to the Arduino pins 2 and 3.
  20. * The Arduino must be powered by a 9V external power supply.
  21. *
  22. * Read this tutorial for more information:
  23. **********************************************************************************************/
  24. import java.awt.Rectangle;  //A rectangle class which keeps track of the face coordinates.
  25. import processing.serial.*; //The serial library is needed to communicate with the Arduino.
  26.  
  27. import gab.opencv.*;
  28. import processing.video.*;
  29. import java.awt.*;
  30.  
  31. Capture video;
  32. OpenCV opencv;  //Create an instance of the OpenCV library.
  33.  
  34.  
  35.  
  36. // contrast/brightness values
  37. int contrast_value    = 0;
  38. int brightness_value  = 0;
  39.  
  40. Serial port; // The serial port
  41.  
  42. //Variables for keeping track of the current servo positions.
  43. char servoTiltPosition = 90;
  44. char servoPanPosition = 90;
  45. //The pan/tilt servo ids for the Arduino serial command interface.
  46. char tiltChannel = 0;
  47. char panChannel = 1;
  48.  
  49. //These variables hold the x and y location for the middle of the detected face.
  50. int midFaceY=0;
  51. int midFaceX=0;
  52.  
  53. //The variables correspond to the middle of the screen, and will be compared to the midFace values
  54. int midScreenY = (480/2);
  55. int midScreenX = (640/2);
  56. int midScreenWindow = 10;  //This is the acceptable 'error' for the center of the screen.
  57.  
  58. //The degree of change that will be applied to the servo each time we update the position.
  59. int stepSize=1;
  60.  
  61. void setup() {
  62.   //Create a window for the sketch.
  63.   size( 640, 480 );
  64.   video = new Capture(this, 640/2, 480/2);
  65.   opencv = new OpenCV(this, 640/2, 480/2);
  66.   opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);  
  67.  
  68.   println(Serial.list()); // List COM-ports (Use this to figure out which port the Arduino is connected to)
  69.  
  70.   //select first com-port from the list (change the number in the [] if your sketch fails to connect to the Arduino)
  71.   port = new Serial(this, Serial.list()[0], 57600);   //Baud rate is set to 57600 to match the Arduino baud rate.
  72.  
  73.   // print usage
  74.   println( "Drag mouse on X-axis inside this sketch window to change contrast" );
  75.   println( "Drag mouse on Y-axis inside this sketch window to change brightness" );
  76.  
  77.   //Send the initial pan/tilt angles to the Arduino to set the device up to look straight forward.
  78.   port.write(tiltChannel);    //Send the Tilt Servo ID
  79.   port.write(servoTiltPosition);  //Send the Tilt Position (currently 90 degrees)
  80.   port.write(panChannel);         //Send the Pan Servo ID
  81.   port.write(servoPanPosition);   //Send the Pan Position (currently 90 degrees)
  82.     video.start();
  83.  
  84. }
  85.  
  86.  
  87. public void stop() {
  88.   super.stop();
  89. }
  90.  
  91.  
  92.  
  93. void draw() {
  94.   // grab a new frame
  95.   // and convert to gray
  96.   scale(2);
  97.   opencv.loadImage(video);
  98.   //opencv.contrast( contrast_value );
  99.   //opencv.brightness( brightness_value );
  100.  
  101.   image(video, 0, 0 );
  102.   noFill();
  103.   stroke(0, 255, 0);
  104.   strokeWeight(3);
  105.   // proceed detection
  106.   Rectangle[] faces = opencv.detect();
  107.  
  108.   // display the image
  109.  
  110.   // draw face area(s)
  111.  
  112.  
  113.   for( int i=0; i<faces.length; i++ ) {
  114.         println(faces[i].x + "," + faces[i].y);
  115.     rect( faces[i].x, faces[i].y, faces[i].width, faces[i].height );
  116.   }
  117.  
  118.   //Find out if any faces were detected.
  119.   if(faces.length > 0){
  120.     //If a face was found, find the midpoint of the first face in the frame.
  121.     //NOTE: The .x and .y of the face rectangle corresponds to the upper left corner of the rectangle,
  122.     //      so we manipulate these values to find the midpoint of the rectangle.
  123.     midFaceY = faces[0].y + (faces[0].height/2);
  124.     midFaceX = faces[0].x + (faces[0].width/2);
  125.    
  126.     //Find out if the Y component of the face is below the middle of the screen.
  127.     if(midFaceY < (midScreenY - midScreenWindow)){
  128.       if(servoTiltPosition >= 5)servoTiltPosition -= stepSize; //If it is below the middle of the screen, update the tilt position variable to lower the tilt servo.
  129.     }
  130.     //Find out if the Y component of the face is above the middle of the screen.
  131.     else if(midFaceY > (midScreenY + midScreenWindow)){
  132.       if(servoTiltPosition <= 175)servoTiltPosition +=stepSize; //Update the tilt position variable to raise the tilt servo.
  133.     }
  134.     //Find out if the X component of the face is to the left of the middle of the screen.
  135.     if(midFaceX < (midScreenX - midScreenWindow)){
  136.       if(servoPanPosition >= 5)servoPanPosition -= stepSize; //Update the pan position variable to move the servo to the left.
  137.     }
  138.     //Find out if the X component of the face is to the right of the middle of the screen.
  139.     else if(midFaceX > (midScreenX + midScreenWindow)){
  140.       if(servoPanPosition <= 175)servoPanPosition +=stepSize; //Update the pan position variable to move the servo to the right.
  141.     }
  142.    
  143.   }
  144.   //Update the servo positions by sending the serial command to the Arduino.
  145.   port.write(tiltChannel);      //Send the tilt servo ID
  146.   port.write(servoTiltPosition); //Send the updated tilt position.
  147.   port.write(panChannel);        //Send the Pan servo ID
  148.   port.write(servoPanPosition);  //Send the updated pan position.
  149.   delay(1);
  150. }
  151.  
  152.  
  153.  
  154. /**
  155.  * Changes contrast/brigthness values
  156.  */
  157. void mouseDragged() {
  158.   contrast_value   = (int) map( mouseX, 0, width, -128, 128 );
  159.   brightness_value = (int) map( mouseY, 0, width, -128, 128 );
  160. }
  161.  
  162. void captureEvent(Capture c) {
  163.   c.read();
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement