我去年使用OpenCV和Python工作。今天我想使用Golang和GOCV包尝试OpenCV。我只是想要一个简单的Python example()来评估但是在Golang中。我甚至使用了相同的参数(除了hiThresh和finalThreshold,我使用了默认值)。不知怎的,我无法让它与GOCV合作,他只找到一个集中的结果。
这是我的代码:
package main
import (
"encoding/json"
"fmt"
"image"
"image/color"
"gocv.io/x/gocv"
)
func main() {
// define default hog descriptor
hog := gocv.NewHOGDescriptor()
defer hog.Close()
hog.SetSVMDetector(gocv.HOGDefaultPeopleDetector())
// color for the rect when faces detected
blue := color.RGBA{0, 0, 255, 0}
// read image
img := gocv.IMRead("images/person_010.bmp", 0)
//resize image
fact := float64(400) / float64(img.Cols())
newY := float64(img.Rows()) * fact
gocv.Resize(img, img, image.Point{X: 400, Y: int(newY)}, 0, 0, 1)
// detect people in image
rects := hog.DetectMultiScaleWithParams(img, 0, image.Point{X: 8, Y: 8}, image.Point{X: 16, Y: 16}, 1.05, 2, false)
// print found points
printStruct(rects)
// draw a rectangle around each face on the original image,
// along with text identifing as "Human"
for _, r := range rects {
gocv.Rectangle(img, r, blue, 3)
size := gocv.GetTextSize("Human", gocv.FontHersheyPlain, 1.2, 2)
pt := image.Pt(r.Min.X+(r.Min.X/2)-(size.X/2), r.Min.Y-2)
gocv.PutText(img, "Human", pt, gocv.FontHersheyPlain, 1.2, blue, 2)
}
if ok := gocv.IMWrite("loool.jpg", img); !ok {
fmt.Println("Error")
}
}
func printStruct(i interface{}) {
b, err := json.Marshal(i)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(b))
}