forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathardrone_face_tracking.go
83 lines (74 loc) · 1.95 KB
/
ardrone_face_tracking.go
1
2
3
4
5
6
7
8
9
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"fmt"
cv "github.com/hybridgroup/go-opencv/opencv"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/ardrone"
"github.com/hybridgroup/gobot/opencv"
"math"
"path"
"runtime"
)
func main() {
_, currentfile, _, _ := runtime.Caller(0)
cascade := path.Join(path.Dir(currentfile), "haarcascade_frontalface_alt.xml")
window := opencv.NewWindowDriver()
window.Name = "window"
camera := opencv.NewCamera()
camera.Name = "camera"
camera.Source = "tcp://192.168.1.1:5555"
ardroneAdaptor := ardrone.NewArdroneAdaptor()
ardroneAdaptor.Name = "Drone"
drone := ardrone.NewArdroneDriver(ardroneAdaptor)
drone.Name = "Drone"
work := func() {
detect := false
drone.TakeOff()
var image *cv.IplImage
gobot.On(camera.Events["Frame"], func(data interface{}) {
image = data.(*cv.IplImage)
if detect == false {
window.ShowImage(image)
}
})
gobot.On(drone.Events["Flying"], func(data interface{}) {
gobot.After("1s", func() { drone.Up(0.2) })
gobot.After("2s", func() { drone.Hover() })
gobot.After("5s", func() {
detect = true
gobot.Every("0.3s", func() {
drone.Hover()
i := image
faces := opencv.DetectFaces(cascade, i)
biggest := 0
var face *cv.Rect
for _, f := range faces {
if f.Width() > biggest {
biggest = f.Width()
face = f
}
}
if face != nil {
opencv.DrawRectangles(i, []*cv.Rect{face})
center_x := float64(image.Width()) * 0.5
turn := -(float64(face.X()) - center_x) / center_x
fmt.Println("turning:", turn)
if turn < 0 {
drone.Clockwise(math.Abs(turn * 0.4))
} else {
drone.CounterClockwise(math.Abs(turn * 0.4))
}
}
window.ShowImage(i)
})
gobot.After("20s", func() { drone.Land() })
})
})
}
robot := gobot.Robot{
Connections: []gobot.Connection{ardroneAdaptor},
Devices: []gobot.Device{window, camera, drone},
Work: work,
}
robot.Start()
}