Test GrouBBoxes OK

This commit is contained in:
Cyrille Nofficial 2022-08-22 12:06:41 +02:00
parent dffa7b4898
commit 2b31a3b7eb
2 changed files with 59 additions and 38 deletions

View File

@ -1,11 +1,16 @@
package steering package steering
import ( import (
"gocv.io/x/gocv"
"image" "image"
) )
func GroupBBoxes(bboxes []image.Rectangle) []image.Rectangle { func GroupBBoxes(bboxes []image.Rectangle) []image.Rectangle {
resp := make([]image.Rectangle, 0, len(bboxes)) if len(bboxes) == 0 {
copy(bboxes, resp) return []image.Rectangle{}
return resp }
if len(bboxes) == 1 {
return []image.Rectangle{bboxes[0]}
}
return gocv.GroupRectangles(bboxes, 1, 0.2)
} }

View File

@ -24,40 +24,24 @@ type BBox struct {
} }
var ( var (
bboxes1 []image.Rectangle dataBBoxes map[string][]image.Rectangle
bboxes2 []image.Rectangle dataImages map[string]*gocv.Mat
bboxes3 []image.Rectangle
bboxes4 []image.Rectangle
) )
func init() { func init() {
img1, bb, err := load_data("01") // TODO: empty img without bbox
if err != nil { dataNames := []string{"01", "02", "03", "04"}
zap.S().Panicf("unable to load data test: %w", err) dataBBoxes = make(map[string][]image.Rectangle, len(dataNames))
} dataImages = make(map[string]*gocv.Mat, len(dataNames))
defer img1.Close()
bboxes1 = bboxesToRectangles(bb, img1.Cols(), img1.Rows())
img2, bb, err := load_data("02") for _, dataName := range dataNames {
img, bb, err := load_data(dataName)
if err != nil { if err != nil {
zap.S().Panicf("unable to load data test: %w", err) zap.S().Panicf("unable to load data test: %v", err)
} }
defer img2.Close() dataBBoxes[dataName] = bboxesToRectangles(bb, img.Cols(), img.Rows())
bboxes2 = bboxesToRectangles(bb, img2.Cols(), img2.Rows()) dataImages[dataName] = img
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 { func bboxesToRectangles(bboxes []BBox, imgWidth, imgHeiht int) []image.Rectangle {
@ -117,6 +101,12 @@ func drawImage(img *gocv.Mat, bboxes []BBox) {
} }
} }
func drawRectangles(img *gocv.Mat, rects []image.Rectangle, c color.RGBA) {
for _, r := range rects {
gocv.Rectangle(img, r, c, 2)
}
}
func saveImage(name string, img *gocv.Mat) error { func saveImage(name string, img *gocv.Mat) error {
err := os.MkdirAll("test_result", os.ModePerm) err := os.MkdirAll("test_result", os.ModePerm)
if err != nil { if err != nil {
@ -156,7 +146,6 @@ func TestDisplayBBox(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
args args args args
//want []*image.Rectangle
}{ }{
{ {
name: "default", name: "default",
@ -189,7 +178,7 @@ func TestDisplayBBox(t *testing.T) {
func TestGroupBBoxes(t *testing.T) { func TestGroupBBoxes(t *testing.T) {
type args struct { type args struct {
bboxes []image.Rectangle dataName string
} }
tests := []struct { tests := []struct {
name string name string
@ -197,16 +186,43 @@ func TestGroupBBoxes(t *testing.T) {
want []image.Rectangle want []image.Rectangle
}{ }{
{ {
name: "data-01", name: "groupbbox-01",
args: args{ args: args{
bboxes: bboxes1, dataName: "01",
}, },
want: []image.Rectangle{image.Rectangle{Min: image.Point{X: 1, Y: 2}, Max: image.Point{X: 3, Y: 4}}}, want: []image.Rectangle{{Min: image.Point{X: 42, Y: 20}, Max: image.Point{X: 84, Y: 57}}},
},
{
name: "groupbbox-02",
args: args{
dataName: "02",
},
want: []image.Rectangle{{Min: image.Point{X: 25, Y: 13}, Max: image.Point{X: 110, Y: 80}}},
},
{
name: "groupbbox-03",
args: args{
dataName: "03",
},
want: []image.Rectangle{{Min: image.Point{X: 0, Y: 17}, Max: image.Point{X: 35, Y: 77}}},
},
{
name: "groupbbox-04",
args: args{
dataName: "04",
},
want: []image.Rectangle{{Min: image.Point{X: 129, Y: 10}, Max: image.Point{X: 159, Y: 64}}},
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if got := GroupBBoxes(tt.args.bboxes); !reflect.DeepEqual(got, tt.want) { got := GroupBBoxes(dataBBoxes[tt.args.dataName])
img := gocv.NewMat()
defer img.Close()
dataImages[tt.args.dataName].CopyTo(&img)
drawRectangles(&img, got, color.RGBA{R: 0, G: 0, B: 255, A: 0})
saveImage(tt.name, &img)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GroupBBoxes() = %v, want %v", got, tt.want) t.Errorf("GroupBBoxes() = %v, want %v", got, tt.want)
} }
}) })