wip
This commit is contained in:
		
							
								
								
									
										11
									
								
								pkg/steering/bbox.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								pkg/steering/bbox.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
			
		||||
package steering
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"image"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func GroupBBoxes(bboxes []*image.Rectangle) []*image.Rectangle {
 | 
			
		||||
	resp := make([]*image.Rectangle, 0, len(bboxes))
 | 
			
		||||
	copy(bboxes, resp)
 | 
			
		||||
	return resp
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										163
									
								
								pkg/steering/bbox_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										163
									
								
								pkg/steering/bbox_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,163 @@
 | 
			
		||||
package steering
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"gocv.io/x/gocv"
 | 
			
		||||
	"image"
 | 
			
		||||
	"image/color"
 | 
			
		||||
	_ "image/jpeg"
 | 
			
		||||
	"os"
 | 
			
		||||
	"reflect"
 | 
			
		||||
	"testing"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type ObjectsList struct {
 | 
			
		||||
	BBoxes []BBox `json:"bboxes"`
 | 
			
		||||
}
 | 
			
		||||
type BBox struct {
 | 
			
		||||
	Left       float32 `json:"left"`
 | 
			
		||||
	Top        float32 `json:"top"`
 | 
			
		||||
	Bottom     float32 `json:"bottom"`
 | 
			
		||||
	Right      float32 `json:"right"`
 | 
			
		||||
	Confidence float32 `json:"confidence"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (bb *BBox) toRect(imgWidth, imgHeight int) image.Rectangle {
 | 
			
		||||
	return image.Rect(
 | 
			
		||||
		int(bb.Left*float32(imgWidth)),
 | 
			
		||||
		int(bb.Top*float32(imgHeight)),
 | 
			
		||||
		int(bb.Right*float32(imgWidth)),
 | 
			
		||||
		int(bb.Bottom*float32(imgHeight)),
 | 
			
		||||
	)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func load_data(dataName string) (*gocv.Mat, []BBox, error) {
 | 
			
		||||
	contentBBoxes, err := os.ReadFile(fmt.Sprintf("test_data/bboxes-%s.json", dataName))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, []BBox{}, fmt.Errorf("unable to load json file for bbox of '%v': %w", dataName, err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var obj ObjectsList
 | 
			
		||||
	err = json.Unmarshal(contentBBoxes, &obj)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, []BBox{}, fmt.Errorf("unable to unmarsh json file for bbox of '%v': %w", dataName, err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	imgContent, err := os.ReadFile(fmt.Sprintf("test_data/img-%s.jpg", dataName))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, []BBox{}, fmt.Errorf("unable to load jpg file of '%v': %w", dataName, err)
 | 
			
		||||
	}
 | 
			
		||||
	img, err := gocv.IMDecode(imgContent, gocv.IMReadUnchanged)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, []BBox{}, fmt.Errorf("unable to load jpg of '%v': %w", dataName, err)
 | 
			
		||||
	}
 | 
			
		||||
	return &img, obj.BBoxes, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func drawImage(img *gocv.Mat, bboxes []BBox) {
 | 
			
		||||
	for _, bb := range bboxes {
 | 
			
		||||
		gocv.Rectangle(img, bb.toRect(img.Cols(), img.Rows()), color.RGBA{R: 0, G: 255, B: 0, A: 0}, 2)
 | 
			
		||||
		gocv.PutText(
 | 
			
		||||
			img,
 | 
			
		||||
			fmt.Sprintf("%.2f", bb.Confidence),
 | 
			
		||||
			image.Point{
 | 
			
		||||
				X: int(bb.Left*float32(img.Cols()) + 10.),
 | 
			
		||||
				Y: int(bb.Top*float32(img.Rows()) + 10.),
 | 
			
		||||
			},
 | 
			
		||||
			gocv.FontHersheyTriplex,
 | 
			
		||||
			0.4,
 | 
			
		||||
			color.RGBA{R: 0, G: 0, B: 0, A: 0},
 | 
			
		||||
			1)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func saveImage(name string, img *gocv.Mat) error {
 | 
			
		||||
	err := os.MkdirAll("test_result", os.ModePerm)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("unable to create directory for test result: %w", err)
 | 
			
		||||
	}
 | 
			
		||||
	jpg, err := gocv.IMEncode(gocv.JPEGFileExt, *img)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("unable to encode jpg image: %w", err)
 | 
			
		||||
	}
 | 
			
		||||
	defer jpg.Close()
 | 
			
		||||
 | 
			
		||||
	err = os.WriteFile(fmt.Sprintf("test_result/%s.jpg", name), jpg.GetBytes(), os.ModePerm)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("unable to write jpeg file: %w", err)
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func DisplayImageAndBBoxes(dataName string) error {
 | 
			
		||||
	img, bboxes, err := load_data(dataName)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("unable to load image and bboxes: %w", err)
 | 
			
		||||
	}
 | 
			
		||||
	drawImage(img, bboxes)
 | 
			
		||||
	err = saveImage(dataName, img)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("unable to save image: %w", err)
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestDisplayBBox(t *testing.T) {
 | 
			
		||||
 | 
			
		||||
	type args struct {
 | 
			
		||||
		dataName string
 | 
			
		||||
	}
 | 
			
		||||
	tests := []struct {
 | 
			
		||||
		name string
 | 
			
		||||
		args args
 | 
			
		||||
		//want []*image.Rectangle
 | 
			
		||||
	}{
 | 
			
		||||
		{
 | 
			
		||||
			name: "default",
 | 
			
		||||
			args: args{dataName: "01"},
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			name: "02",
 | 
			
		||||
			args: args{dataName: "02"},
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			name: "03",
 | 
			
		||||
			args: args{dataName: "03"},
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			name: "04",
 | 
			
		||||
			args: args{dataName: "04"},
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, tt := range tests {
 | 
			
		||||
		t.Run(tt.name, func(t *testing.T) {
 | 
			
		||||
			err := DisplayImageAndBBoxes(tt.args.dataName)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				t.Errorf("unable to draw image: %v", err)
 | 
			
		||||
			}
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestGroupBBoxes(t *testing.T) {
 | 
			
		||||
	type args struct {
 | 
			
		||||
		bboxes []*image.Rectangle
 | 
			
		||||
	}
 | 
			
		||||
	tests := []struct {
 | 
			
		||||
		name string
 | 
			
		||||
		args args
 | 
			
		||||
		want []*image.Rectangle
 | 
			
		||||
	}{
 | 
			
		||||
		// TODO: Add test cases.
 | 
			
		||||
	}
 | 
			
		||||
	for _, tt := range tests {
 | 
			
		||||
		t.Run(tt.name, func(t *testing.T) {
 | 
			
		||||
			if got := GroupBBoxes(tt.args.bboxes); !reflect.DeepEqual(got, tt.want) {
 | 
			
		||||
				t.Errorf("GroupBBoxes() = %v, want %v", got, tt.want)
 | 
			
		||||
			}
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										12
									
								
								pkg/steering/test_data/bboxes-01.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								pkg/steering/test_data/bboxes-01.json
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
			
		||||
{
 | 
			
		||||
  "bboxes": [
 | 
			
		||||
    {
 | 
			
		||||
      "right": 0.5258789,
 | 
			
		||||
      "top": 0.1706543,
 | 
			
		||||
      "left": 0.26660156,
 | 
			
		||||
      "bottom": 0.47583008,
 | 
			
		||||
      "confidence": 0.4482422
 | 
			
		||||
    }
 | 
			
		||||
  ]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										25
									
								
								pkg/steering/test_data/bboxes-02.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								pkg/steering/test_data/bboxes-02.json
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,25 @@
 | 
			
		||||
{
 | 
			
		||||
  "bboxes": [
 | 
			
		||||
    {
 | 
			
		||||
      "right": 0.6879883,
 | 
			
		||||
      "top": 0.115234375,
 | 
			
		||||
      "left": 0.1586914,
 | 
			
		||||
      "bottom": 0.66796875,
 | 
			
		||||
      "confidence": 0.82714844
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "right": 0.6894531,
 | 
			
		||||
      "top": 0.111816406,
 | 
			
		||||
      "left": 0.15698242,
 | 
			
		||||
      "bottom": 0.66748047,
 | 
			
		||||
      "confidence": 0.83447266
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "right": 0.6875,
 | 
			
		||||
      "top": 0.11328125,
 | 
			
		||||
      "left": 0.15673828,
 | 
			
		||||
      "bottom": 0.66748047,
 | 
			
		||||
      "confidence": 0.85253906
 | 
			
		||||
    }
 | 
			
		||||
  ]
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										27
									
								
								pkg/steering/test_data/bboxes-03.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								pkg/steering/test_data/bboxes-03.json
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,27 @@
 | 
			
		||||
{
 | 
			
		||||
  "bboxes": [
 | 
			
		||||
    {
 | 
			
		||||
      "right": 0.2211914,
 | 
			
		||||
      "top": 0.14953613,
 | 
			
		||||
      "left": 0.0015258789,
 | 
			
		||||
      "bottom": 0.64941406,
 | 
			
		||||
      "confidence": 0.5595703
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "right": 0.22192383,
 | 
			
		||||
      "top": 0.14819336,
 | 
			
		||||
      "left": 0.0014038086,
 | 
			
		||||
      "bottom": 0.64941406,
 | 
			
		||||
      "confidence": 0.5493164
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "right": 0.21948242,
 | 
			
		||||
      "top": 0.1459961,
 | 
			
		||||
      "left": 0.0015258789,
 | 
			
		||||
      "bottom": 0.65185547,
 | 
			
		||||
      "confidence": 0.5595703
 | 
			
		||||
    }
 | 
			
		||||
  ]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										25
									
								
								pkg/steering/test_data/bboxes-04.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								pkg/steering/test_data/bboxes-04.json
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,25 @@
 | 
			
		||||
{
 | 
			
		||||
  "bboxes": [
 | 
			
		||||
    {
 | 
			
		||||
      "right": 0.99902344,
 | 
			
		||||
      "top": 0.08947754,
 | 
			
		||||
      "left": 0.8095703,
 | 
			
		||||
      "bottom": 0.54296875,
 | 
			
		||||
      "confidence": 0.4741211
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "right": 0.99902344,
 | 
			
		||||
      "top": 0.08666992,
 | 
			
		||||
      "left": 0.80859375,
 | 
			
		||||
      "bottom": 0.54003906,
 | 
			
		||||
      "confidence": 0.453125
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "right": 0.99902344,
 | 
			
		||||
      "top": 0.09423828,
 | 
			
		||||
      "left": 0.8095703,
 | 
			
		||||
      "bottom": 0.54345703,
 | 
			
		||||
      "confidence": 0.44995117
 | 
			
		||||
    }
 | 
			
		||||
  ]
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								pkg/steering/test_data/img-01.jpg
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								pkg/steering/test_data/img-01.jpg
									
									
									
									
									
										Executable file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 5.6 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								pkg/steering/test_data/img-02.jpg
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								pkg/steering/test_data/img-02.jpg
									
									
									
									
									
										Executable file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 6.4 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								pkg/steering/test_data/img-03.jpg
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								pkg/steering/test_data/img-03.jpg
									
									
									
									
									
										Executable file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 6.1 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								pkg/steering/test_data/img-04.jpg
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								pkg/steering/test_data/img-04.jpg
									
									
									
									
									
										Executable file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 5.0 KiB  | 
		Reference in New Issue
	
	Block a user