LdDl / go-darknet

Go bindings for Darknet (YOLO v4 / v7-tiny / v3)
Apache License 2.0
82 stars 19 forks source link

crop the bounding box content #2

Closed ghost closed 4 years ago

ghost commented 4 years ago

Hi,

Again ! :-)

I was wondering if there is a function to crop the content of the bounding box in separate pictures ?

Cheers, X.

LdDl commented 4 years ago

Hello there! @x0rzkov I've made PR in example main.go file: https://github.com/LdDl/go-darknet/blob/master/example/main.go#L84

It uses https://github.com/disintegration/imaging (MIT license)

Convert darknet's bbox to image.Rectangle of standart library

minX, minY := float64(bBox.StartPoint.X), float64(bBox.StartPoint.Y)
maxX, maxY := float64(bBox.EndPoint.X), float64(bBox.EndPoint.Y)
rect := image.Rect(round(minX), round(minY), round(maxX), round(maxY))

Crop *image.NRGBAfrom image via provided rect (disintegration's library must be used)

rectcropimg := imaging.Crop(imgSrc, bbox)

And then you are good to go with saving *image.NRGBA to file:

f, err := os.Create(fname)
if err != nil {
    return err
}
defer f.Close()
err = jpeg.Encode(f, rectcropimg, nil)
if err != nil {
    return err
}
ghost commented 4 years ago

awesome, will try just now

works like a charm :-) Spassiba

LdDl commented 4 years ago

glad to know it works :) closing