http://bbs.makercollider.com/?/article/27
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
|
$ImagingDevice = $ImagingDevices[[2]];dev = DeviceOpen["Serial", "COM3"]Dynamic[ i = CurrentImage[]; boxes = FindFaces[i]; If[boxes =!= {}, {X, Y} = Round[Mean @@ boxes]; Column@ { HighlightImage[i, Circle[{X, Y}, 50], ImageSize -> {320, 240}], DeviceWrite[dev, ToString[X]] }, i] ] |
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
#include <Servo.h>#define servomaxx 180 // max degree servo horizontal (x) can turn#define screenmaxx 320 // max screen horizontal (x)resolution#define screenmaxy 240 // max screen vertical (y) resolution#define servocenterx 90 // center po#define of x servo#define servopinx 9 // digital pin for servo x#define baudrate 9600 // com port speed. Must match your setting#define distancex 2 // x servo rotation stepsint valx = 0; // store x data from serial portint posx = 0;int incx = 10; // significant increments of horizontal (x) camera movementServo servox;void setup() { Serial.begin(baudrate); // connect to the serial port Serial.setTimeout(20); Serial.println("Starting Cam-servo Face tracker"); pinMode(servopinx, OUTPUT); // declare the LED's pin as output servox.attach(servopinx); // center servos servox.write(servocenterx); delay(200);}void loop () { while (Serial.available() <= 0); // wait for incoming serial data if (Serial.available() >= 1) { // get X axis 2-byte integer from serial valx = Serial.parseInt(); // read last servos positions posx = servox.read(); //Find out if the X component of the face is to the left of the middle of the screen. if (valx < (screenmaxx / 2 - incx)) { if ( posx >= incx ) posx += distancex; //Update the pan position variable to move the servo to the left. } //Find out if the X component of the face is to the right of the middle of the screen. else if (valx > screenmaxx / 2 + incx) { if (posx <= servomaxx - incx) posx -= distancex; //Update the pan position variable to move the servo to the right. } // Servos will rotate accordingly servox.write(posx); }} |

这篇文章还没有评论