mirror of
https://github.com/LdDl/go-darknet.git
synced 2025-09-26 19:51:27 +08:00
upd
This commit is contained in:
@@ -7,9 +7,11 @@ import (
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"log"
|
||||
"math"
|
||||
"os"
|
||||
|
||||
darknet "github.com/LdDl/go-darknet"
|
||||
"github.com/disintegration/imaging"
|
||||
)
|
||||
|
||||
var configFile = flag.String("configFile", "",
|
||||
@@ -78,6 +80,16 @@ func main() {
|
||||
bBox.StartPoint.X, bBox.StartPoint.Y,
|
||||
bBox.EndPoint.X, bBox.EndPoint.Y,
|
||||
)
|
||||
|
||||
// Uncomment code below if you want save cropped objects to files
|
||||
// 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))
|
||||
// err := saveToFile(src, rect, fmt.Sprintf("crop_%d.jpeg", i))
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// return
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -87,3 +99,24 @@ func imageToBytes(img image.Image) ([]byte, error) {
|
||||
err := jpeg.Encode(buf, img, nil)
|
||||
return buf.Bytes(), err
|
||||
}
|
||||
|
||||
func round(v float64) int {
|
||||
if v >= 0 {
|
||||
return int(math.Floor(v + 0.5))
|
||||
}
|
||||
return int(math.Ceil(v - 0.5))
|
||||
}
|
||||
|
||||
func saveToFile(imgSrc image.Image, bbox image.Rectangle, fname string) error {
|
||||
rectcropimg := imaging.Crop(imgSrc, bbox)
|
||||
f, err := os.Create(fname)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
err = jpeg.Encode(f, rectcropimg, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user