Advertisement
Guest User

Pan Tilt Processing Sketch

a guest
Jul 31st, 2017
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.81 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 ServoFirmata Sketch.
  19. * Two servos mounted on a pan/tilt backet must be connected to the Arduino pins 4 and 7.
  20. * The Arduino must be powered by a 9V external power supply.
  21. *
  22. * Read this tutorial for more information:
  23. **********************************************************************************************/
  24. import gab.opencv.*;
  25. import processing.video.*;
  26. import java.awt.*;
  27. import processing.serial.*;
  28.  
  29. Capture video;
  30. OpenCV opencv;  //Create an instance of the OpenCV library.
  31.  
  32. Serial serial; // The serial port
  33.  
  34. //Variables for keeping track of the current servo positions.
  35. char servoTiltPosition;
  36. char servoPanPosition;
  37. //The pan/tilt servo ids for the Arduino serial command interface.
  38. char tiltChannel = 0;
  39. char panChannel = 1;
  40.  
  41. //These variables hold the x and y location for the middle of the detected face.
  42. int midFaceY=0;
  43. int midFaceX=0;
  44.  
  45. //The variables correspond to the middle of the screen, and will be compared to the midFace values
  46. int midScreenY = (height/2);
  47. int midScreenX = (width/2);
  48. int midScreenWindow = 10;  //This is the acceptable 'error' for the center of the screen.
  49.  
  50. //The degree of change that will be applied to the servo each time we update the position.
  51. int stepSize=1;
  52.  
  53. void setup() {
  54.   size(640, 480);
  55.   video = new Capture(this, 640/2, 480/2);
  56.   opencv = new OpenCV(this, 640/2, 480/2);
  57.   opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);  
  58.  
  59.   video.start();
  60.  
  61.   printArray(Serial.list());
  62.  
  63.  
  64.   serial = new Serial(this, "COM3", 57600);
  65.  
  66. }
  67.  
  68.  
  69.  
  70. void draw() {
  71.   scale(2);
  72.   opencv.loadImage(video);
  73.  
  74.   image(video, 0, 0 );
  75.  
  76.   noFill();
  77.   stroke(53, 204, 255);
  78.   strokeWeight(3);
  79.   Rectangle[] faces = opencv.detect();
  80.   println(faces.length);
  81.  
  82.   for (int i = 0; i < faces.length; i++) {
  83.     println(faces[i].x + "," + faces[i].y);
  84.     rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
  85.   }
  86.  
  87.  
  88.   if(faces.length > 0){
  89.     //If a face was found, find the midpoint of the first face in the frame.
  90.     //NOTE: The .x and .y of the face rectangle corresponds to the upper left corner of the rectangle,
  91.     //      so we manipulate these values to find the midpoint of the rectangle.
  92.     midFaceY = faces[0].y + (faces[0].height/2);
  93.     midFaceX = faces[0].x + (faces[0].width/2);
  94.    
  95.     //Find out if the Y component of the face is below the middle of the screen.
  96.     if(midFaceY < (midScreenY - midScreenWindow)){
  97.       if(servoTiltPosition >= 5)servoTiltPosition -= stepSize; //If it is below the middle of the screen, update the tilt position variable to lower the tilt servo.
  98.     }
  99.     //Find out if the Y component of the face is above the middle of the screen.
  100.     else if(midFaceY > (midScreenY + midScreenWindow)){
  101.       if(servoTiltPosition <= 175)servoTiltPosition +=stepSize; //Update the tilt position variable to raise the tilt servo.
  102.     }
  103.     //Find out if the X component of the face is to the left of the middle of the screen.
  104.     if(midFaceX < (midScreenX - midScreenWindow)){
  105.       if(servoPanPosition >= 5)servoPanPosition -= stepSize; //Update the pan position variable to move the servo to the left.
  106.     }
  107.     //Find out if the X component of the face is to the right of the middle of the screen.
  108.     else if(midFaceX > (midScreenX + midScreenWindow)){
  109.       if(servoPanPosition <= 175)servoPanPosition +=stepSize; //Update the pan position variable to move the servo to the right.
  110.     }
  111.    
  112.   }
  113.   //Update the servo positions by sending the serial command to the Arduino.
  114.   serial.write(tiltChannel);      //Send the tilt servo ID
  115.   serial.write(servoTiltPosition); //Send the updated tilt position.
  116.   serial.write(panChannel);        //Send the Pan servo ID
  117.   serial.write(servoPanPosition);  //Send the updated pan position.
  118.   delay(1);
  119. }
  120.  
  121.  
  122.  
  123. void captureEvent(Capture c) {
  124.  
  125.   c.read();
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement