From a998b0b0727ac7bcabeb16359a62ca0c2051e806 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Thu, 5 Oct 2017 20:05:12 +0200 Subject: [PATCH] opencv: OpenCV face detector that is much more concurrent Signed-off-by: deadprogram --- examples/opencv_face_detect.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/examples/opencv_face_detect.go b/examples/opencv_face_detect.go index c4fa6612..997108ac 100644 --- a/examples/opencv_face_detect.go +++ b/examples/opencv_face_detect.go @@ -7,12 +7,16 @@ package main import ( "path" "runtime" + "sync/atomic" + "time" "github.com/hybridgroup/gocv" "gobot.io/x/gobot" "gobot.io/x/gobot/platforms/opencv" ) +var img atomic.Value + func main() { _, currentfile, _, _ := runtime.Caller(0) cascade := path.Join(path.Dir(currentfile), "haarcascade_frontalface_alt.xml") @@ -21,8 +25,19 @@ func main() { 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.DetectFaces(cascade, i) opencv.DrawRectangles(i, faces, 0, 255, 0, 5) window.ShowImage(i)