1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-29 13:49:14 +08:00
hybridgroup.gobot/examples/opencv_face_detect.go
Thomas Kohler 865e724af0
Build(v2): revert move to v2 subfolder (#932)
* revert move to v2 subfolder
* fix CI and adjust CHANGELOG
2023-05-29 19:23:28 +02:00

58 lines
988 B
Go

//go:build example
// +build example
//
// Do not build by default.
package main
import (
"path"
"runtime"
"sync/atomic"
"time"
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/platforms/opencv"
"gocv.io/x/gocv"
)
var img atomic.Value
func main() {
_, currentfile, _, _ := runtime.Caller(0)
cascade := path.Join(path.Dir(currentfile), "haarcascade_frontalface_alt.xml")
window := opencv.NewWindowDriver()
camera := opencv.NewCameraDriver(1)
work := func() {
mat := gocv.NewMat()
img.Store(mat)
camera.On(opencv.Frame, func(data interface{}) {
i := data.(gocv.Mat)
img.Store(i)
})
gobot.Every(10*time.Millisecond, func() {
i := img.Load().(gocv.Mat)
if i.Empty() {
return
}
faces := opencv.DetectObjects(cascade, i)
opencv.DrawRectangles(i, faces, 0, 255, 0, 5)
window.ShowImage(i)
window.WaitKey(1)
})
}
robot := gobot.NewRobot("faceBot",
[]gobot.Connection{},
[]gobot.Device{window, camera},
work,
)
robot.Start()
}