2014-04-28 07:58:34 +08:00
|
|
|
package opencv
|
2014-04-26 18:11:51 +08:00
|
|
|
|
|
|
|
import (
|
2017-10-05 23:05:10 +08:00
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
|
2017-10-19 00:13:40 +08:00
|
|
|
"gocv.io/x/gocv"
|
2014-04-26 18:11:51 +08:00
|
|
|
)
|
|
|
|
|
2017-10-05 23:05:10 +08:00
|
|
|
var classifier *gocv.CascadeClassifier
|
|
|
|
|
2014-10-21 00:23:33 +08:00
|
|
|
// loadHaarClassifierCascade returns open cv HaarCascade loaded
|
2017-10-05 23:05:10 +08:00
|
|
|
func loadCascadeClassifier(haar string) *gocv.CascadeClassifier {
|
|
|
|
if classifier != nil {
|
|
|
|
return classifier
|
|
|
|
}
|
|
|
|
|
|
|
|
c := gocv.NewCascadeClassifier()
|
|
|
|
c.Load(haar)
|
|
|
|
classifier = &c
|
|
|
|
return classifier
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2017-10-23 17:45:36 +08:00
|
|
|
// DetectObjects loads Haar cascade to detect face objects in image
|
|
|
|
func DetectObjects(haar string, img gocv.Mat) []image.Rectangle {
|
2017-10-05 23:05:10 +08:00
|
|
|
return loadCascadeClassifier(haar).DetectMultiScale(img)
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2014-10-21 00:23:33 +08:00
|
|
|
// DrawRectangles uses Rect array values to return image with rectangles drawn.
|
2017-10-05 23:05:10 +08:00
|
|
|
func DrawRectangles(img gocv.Mat, rects []image.Rectangle, r int, g int, b int, thickness int) {
|
|
|
|
for _, rect := range rects {
|
|
|
|
gocv.Rectangle(img, rect, color.RGBA{uint8(r), uint8(g), uint8(b), 0}, thickness)
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
2017-10-05 23:05:10 +08:00
|
|
|
return
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|