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 (
|
|
|
|
"path"
|
|
|
|
"runtime"
|
2017-10-06 02:05:12 +08:00
|
|
|
"sync/atomic"
|
|
|
|
"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"
|
2017-10-19 00:13:40 +08:00
|
|
|
"gocv.io/x/gocv"
|
2014-04-26 18:11:51 +08:00
|
|
|
)
|
|
|
|
|
2017-10-06 02:05:12 +08:00
|
|
|
var img atomic.Value
|
|
|
|
|
2014-04-26 18:11:51 +08:00
|
|
|
func main() {
|
|
|
|
_, currentfile, _, _ := runtime.Caller(0)
|
|
|
|
cascade := path.Join(path.Dir(currentfile), "haarcascade_frontalface_alt.xml")
|
|
|
|
|
2016-10-03 22:58:43 +08:00
|
|
|
window := opencv.NewWindowDriver()
|
2017-10-05 23:47:19 +08:00
|
|
|
camera := opencv.NewCameraDriver(1)
|
2014-04-26 18:11:51 +08:00
|
|
|
|
|
|
|
work := func() {
|
2017-10-06 02:05:12 +08:00
|
|
|
mat := gocv.NewMat()
|
|
|
|
img.Store(mat)
|
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
camera.On(opencv.Frame, func(data interface{}) {
|
2017-10-05 23:05:10 +08:00
|
|
|
i := data.(gocv.Mat)
|
2017-10-06 02:05:12 +08:00
|
|
|
img.Store(i)
|
|
|
|
})
|
|
|
|
|
|
|
|
gobot.Every(10*time.Millisecond, func() {
|
|
|
|
i := img.Load().(gocv.Mat)
|
|
|
|
if i.Empty() {
|
|
|
|
return
|
|
|
|
}
|
2017-10-23 17:45:36 +08:00
|
|
|
faces := opencv.DetectObjects(cascade, i)
|
2017-10-05 23:05:10 +08:00
|
|
|
opencv.DrawRectangles(i, faces, 0, 255, 0, 5)
|
|
|
|
window.ShowImage(i)
|
2017-10-05 23:47:19 +08:00
|
|
|
window.WaitKey(1)
|
2014-06-09 09:14:18 +08:00
|
|
|
})
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2014-07-09 09:36:14 +08:00
|
|
|
robot := gobot.NewRobot("faceBot",
|
|
|
|
[]gobot.Connection{},
|
|
|
|
[]gobot.Device{window, camera},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
2016-10-19 03:08:25 +08:00
|
|
|
robot.Start()
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|