This commit is contained in:
Cyrille Nofficial 2022-08-21 22:46:51 +02:00
parent 3ae2986580
commit dffa7b4898
3 changed files with 60 additions and 9 deletions

View File

@ -4,8 +4,8 @@ import (
"image" "image"
) )
func GroupBBoxes(bboxes []*image.Rectangle) []*image.Rectangle { func GroupBBoxes(bboxes []image.Rectangle) []image.Rectangle {
resp := make([]*image.Rectangle, 0, len(bboxes)) resp := make([]image.Rectangle, 0, len(bboxes))
copy(bboxes, resp) copy(bboxes, resp)
return resp return resp
} }

View File

@ -3,10 +3,10 @@ package steering
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"go.uber.org/zap"
"gocv.io/x/gocv" "gocv.io/x/gocv"
"image" "image"
"image/color" "image/color"
_ "image/jpeg"
"os" "os"
"reflect" "reflect"
"testing" "testing"
@ -23,6 +23,51 @@ type BBox struct {
Confidence float32 `json:"confidence"` Confidence float32 `json:"confidence"`
} }
var (
bboxes1 []image.Rectangle
bboxes2 []image.Rectangle
bboxes3 []image.Rectangle
bboxes4 []image.Rectangle
)
func init() {
img1, bb, err := load_data("01")
if err != nil {
zap.S().Panicf("unable to load data test: %w", err)
}
defer img1.Close()
bboxes1 = bboxesToRectangles(bb, img1.Cols(), img1.Rows())
img2, bb, err := load_data("02")
if err != nil {
zap.S().Panicf("unable to load data test: %w", err)
}
defer img2.Close()
bboxes2 = bboxesToRectangles(bb, img2.Cols(), img2.Rows())
img3, bb, err := load_data("03")
if err != nil {
zap.S().Panicf("unable to load data test: %w", err)
}
defer img3.Close()
bboxes3 = bboxesToRectangles(bb, img3.Cols(), img3.Rows())
img4, bb, err := load_data("04")
if err != nil {
zap.S().Panicf("unable to load data test: %w", err)
}
defer img4.Close()
bboxes4 = bboxesToRectangles(bb, img4.Cols(), img4.Rows())
}
func bboxesToRectangles(bboxes []BBox, imgWidth, imgHeiht int) []image.Rectangle {
rects := make([]image.Rectangle, 0, len(bboxes))
for _, bb := range bboxes {
rects = append(rects, bb.toRect(imgWidth, imgHeiht))
}
return rects
}
func (bb *BBox) toRect(imgWidth, imgHeight int) image.Rectangle { func (bb *BBox) toRect(imgWidth, imgHeight int) image.Rectangle {
return image.Rect( return image.Rect(
int(bb.Left*float32(imgWidth)), int(bb.Left*float32(imgWidth)),
@ -144,14 +189,20 @@ func TestDisplayBBox(t *testing.T) {
func TestGroupBBoxes(t *testing.T) { func TestGroupBBoxes(t *testing.T) {
type args struct { type args struct {
bboxes []*image.Rectangle bboxes []image.Rectangle
} }
tests := []struct { tests := []struct {
name string name string
args args args args
want []*image.Rectangle want []image.Rectangle
}{ }{
// TODO: Add test cases. {
name: "data-01",
args: args{
bboxes: bboxes1,
},
want: []image.Rectangle{image.Rectangle{Min: image.Point{X: 1, Y: 2}, Max: image.Point{X: 3, Y: 4}}},
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {

View File

@ -8,10 +8,10 @@
"confidence": 0.82714844 "confidence": 0.82714844
}, },
{ {
"right": 0.6894531, "right": 0.2794531,
"top": 0.111816406, "top": 0.301816406,
"left": 0.15698242, "left": 0.15698242,
"bottom": 0.66748047, "bottom": 0.65748047,
"confidence": 0.83447266 "confidence": 0.83447266
}, },
{ {