// Runs a single detector across a single image and returns results. func detectImage(tmpl *detect.FeatTmpl, im image.Image, margin int, step float64, sbin int, localmax bool, maxiou float64) []detect.Det { // Construct pyramid. // Get range of scales. scales := imgpyr.Scales(im.Bounds().Size(), tmpl.Size, step) // Define feature transform. phi := hog.Transform{hog.FGMRConfig(sbin)} // Define amount and type of padding. pad := feat.Pad{feat.Margin{margin, margin, margin, margin}, imsamp.Continue} pyr := featpyr.NewPad(imgpyr.New(im, scales), phi, pad) // Search feature pyramid. // Options for running detector on each level. detopts := detect.DetFilter{LocalMax: localmax, MinScore: math.Inf(-1)} // Use intersection-over-union criteria for non-max suppression. overlap := func(a, b image.Rectangle) bool { return detect.IOU(a, b) > maxiou } // Options for non-max suppression. suppropts := detect.SupprFilter{MaxNum: 0, Overlap: overlap} dets := detect.Pyramid(pyr, tmpl, detopts, suppropts) return dets }
func main() { var ( sbin = flag.Int("sbin", 4, "Spatial binning parameter to HOG") margin = flag.Int("margin", 0, "Margin to add around images before computing features") step = flag.Float64("pyr-step", 1.1, "Geometric step to use in image pyramid") maxinter = flag.Float64("max-intersect", 0.5, "Maximum overlap of detections. Zero means detections can't overlap at all, one means they can overlap entirely.") localmax = flag.Bool("local-max", true, "Detections cannot score less than a neighbor") ) flag.Usage = usage flag.Parse() if flag.NArg() != 3 { flag.Usage() os.Exit(1) } var ( tmplFile = flag.Arg(0) imFile = flag.Arg(1) detsFile = flag.Arg(2) ) // Load image. im, err := loadImage(imFile) if err != nil { log.Fatal(err) } // Construct pyramid. scales := imgpyr.Scales(im.Bounds().Size(), image.Pt(24, 24), *step) phi := hog.Transform{hog.FGMRConfig(*sbin)} pad := feat.Pad{feat.Margin{*margin, *margin, *margin, *margin}, imsamp.Continue} pyr := featpyr.NewPad(imgpyr.New(im, scales), phi, pad) // Load template. var tmpl *detect.FeatTmpl if err := loadGob(tmplFile, &tmpl); err != nil { log.Fatal(err) } detopts := detect.DetFilter{ LocalMax: *localmax, MinScore: math.Inf(-1), } // Use intersection-over-union criteria for non-max suppression. overlap := func(a, b image.Rectangle) bool { return detect.IOU(a, b) > *maxinter } suppropts := detect.SupprFilter{ MaxNum: 0, Overlap: overlap, } dets := detect.Pyramid(pyr, tmpl, detopts, suppropts) if err := saveJSON(detsFile, dets); err != nil { log.Fatal(err) } for i, det := range dets { r := det.Rect cmd := fmt.Sprintf("rectangle %d,%d %d,%d", r.Min.X, r.Min.Y, r.Max.X, r.Max.Y) fmt.Printf("convert %s -fill none -stroke white -draw '%s' det_%06d.jpg\n", imFile, cmd, i) } }