2017-03-13 23:01:39 +08:00
|
|
|
// +build example
|
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
2014-04-26 18:11:51 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"path"
|
|
|
|
"runtime"
|
2014-05-21 14:36:44 +08:00
|
|
|
"time"
|
2014-07-11 08:21:21 +08:00
|
|
|
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/platforms/opencv"
|
|
|
|
"gobot.io/x/gobot/platforms/parrot/ardrone"
|
2017-10-19 00:13:40 +08:00
|
|
|
"gocv.io/x/gocv"
|
2014-04-26 18:11:51 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2014-06-09 10:50:15 +08:00
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
|
|
|
2014-04-26 18:11:51 +08:00
|
|
|
_, currentfile, _, _ := runtime.Caller(0)
|
|
|
|
cascade := path.Join(path.Dir(currentfile), "haarcascade_frontalface_alt.xml")
|
2016-09-25 17:46:55 +08:00
|
|
|
window := opencv.NewWindowDriver()
|
|
|
|
camera := opencv.NewCameraDriver("tcp://192.168.1.1:5555")
|
|
|
|
ardroneAdaptor := ardrone.NewAdaptor()
|
|
|
|
drone := ardrone.NewDriver(ardroneAdaptor)
|
2014-04-26 18:11:51 +08:00
|
|
|
|
|
|
|
work := func() {
|
|
|
|
detect := false
|
|
|
|
drone.TakeOff()
|
2017-10-06 00:00:22 +08:00
|
|
|
var img gocv.Mat
|
2016-09-01 18:17:43 +08:00
|
|
|
camera.On(opencv.Frame, func(data interface{}) {
|
2017-10-06 00:00:22 +08:00
|
|
|
img = data.(gocv.Mat)
|
2016-07-13 23:32:22 +08:00
|
|
|
if !detect {
|
2017-10-06 00:00:22 +08:00
|
|
|
window.IMShow(img)
|
|
|
|
window.WaitKey(1)
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
})
|
2016-09-01 18:17:43 +08:00
|
|
|
drone.On(ardrone.Flying, func(data interface{}) {
|
2014-05-21 14:36:44 +08:00
|
|
|
gobot.After(1*time.Second, func() { drone.Up(0.2) })
|
|
|
|
gobot.After(2*time.Second, func() { drone.Hover() })
|
|
|
|
gobot.After(5*time.Second, func() {
|
2014-04-26 18:11:51 +08:00
|
|
|
detect = true
|
2014-06-09 10:50:15 +08:00
|
|
|
gobot.Every(300*time.Millisecond, func() {
|
2014-04-26 18:11:51 +08:00
|
|
|
drone.Hover()
|
2017-10-06 00:00:22 +08:00
|
|
|
i := img
|
2017-10-23 17:45:36 +08:00
|
|
|
faces := opencv.DetectObjects(cascade, i)
|
2014-04-26 18:11:51 +08:00
|
|
|
biggest := 0
|
2017-10-06 00:00:22 +08:00
|
|
|
var face image.Rectangle
|
2014-04-26 18:11:51 +08:00
|
|
|
for _, f := range faces {
|
|
|
|
if f.Width() > biggest {
|
|
|
|
biggest = f.Width()
|
|
|
|
face = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if face != nil {
|
2017-10-06 00:00:22 +08:00
|
|
|
opencv.DrawRectangles(i, []img.Rectangle{face}, 0, 255, 0, 5)
|
|
|
|
centerX := float64(img.Size()).X * 0.5
|
2017-10-06 00:01:34 +08:00
|
|
|
turn := -(float64(face.Min.X - centerX)) / centerX
|
2014-04-26 18:11:51 +08:00
|
|
|
fmt.Println("turning:", turn)
|
|
|
|
if turn < 0 {
|
|
|
|
drone.Clockwise(math.Abs(turn * 0.4))
|
|
|
|
} else {
|
|
|
|
drone.CounterClockwise(math.Abs(turn * 0.4))
|
|
|
|
}
|
|
|
|
}
|
2017-10-06 00:00:22 +08:00
|
|
|
window.IMShow(i)
|
|
|
|
window.WaitKey(1)
|
2014-04-26 18:11:51 +08:00
|
|
|
})
|
2014-05-21 14:36:44 +08:00
|
|
|
gobot.After(20*time.Second, func() { drone.Land() })
|
2014-04-26 18:11:51 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-09 09:36:14 +08:00
|
|
|
robot := gobot.NewRobot("face",
|
|
|
|
[]gobot.Connection{ardroneAdaptor},
|
|
|
|
[]gobot.Device{window, camera, drone},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
2016-10-19 03:08:25 +08:00
|
|
|
robot.Start()
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|