From 67dc92e476ab890c044ed0637f707e539d913d23 Mon Sep 17 00:00:00 2001 From: Cyrille Nofficial Date: Mon, 15 Jan 2024 19:14:24 +0100 Subject: [PATCH] feat(objectDetector): add cli option to remove big object --- cmd/rc-steering/rc-steering.go | 1 + pkg/steering/bbox.go | 5 +++++ pkg/steering/corrector.go | 23 ++++++++++++++++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/cmd/rc-steering/rc-steering.go b/cmd/rc-steering/rc-steering.go index c6b44a8..df17584 100644 --- a/cmd/rc-steering/rc-steering.go +++ b/cmd/rc-steering/rc-steering.go @@ -86,6 +86,7 @@ func main() { steering.WithGridMap(gridMapConfig), steering.WithObjectMoveFactors(objectsMoveFactorsConfig), steering.WithImageSize(imgWidth, imgHeight), + steering.WithSizeThreshold(0.75), ), ), steering.WithObjectsCorrectionEnabled(enableObjectsCorrection, enableObjectsCorrectionOnUserMode), diff --git a/pkg/steering/bbox.go b/pkg/steering/bbox.go index 05dbb34..ca8b679 100644 --- a/pkg/steering/bbox.go +++ b/pkg/steering/bbox.go @@ -45,6 +45,11 @@ func objectToRect(object *events.Object, imgWidth, imgHeight int) *image.Rectang return &r } +func sizeObject(object *events.Object, imgWidth, imgHeight int) float64 { + r := objectToRect(object, imgWidth, imgHeight) + return float64(r.Dx()) * float64(r.Dy()) +} + func rectToObject(r *image.Rectangle, imgWidth, imgHeight int) *events.Object { return &events.Object{ Type: events.TypeObject_ANY, diff --git a/pkg/steering/corrector.go b/pkg/steering/corrector.go index a3508c2..9f46fe8 100644 --- a/pkg/steering/corrector.go +++ b/pkg/steering/corrector.go @@ -67,6 +67,12 @@ func WithImageSize(width, height int) OptionCorrector { } } +func WithSizeThreshold(sizeThreshold float64) OptionCorrector { + return func(c *GridCorrector) { + c.sizeThreshold = sizeThreshold + } +} + func WidthDeltaMiddle(d float64) OptionCorrector { return func(c *GridCorrector) { c.deltaMiddle = d @@ -80,6 +86,7 @@ func NewGridCorrector(options ...OptionCorrector) *GridCorrector { deltaMiddle: 0.1, imgWidth: 160, imgHeight: 120, + sizeThreshold: 0.75, } for _, o := range options { o(c) @@ -92,6 +99,7 @@ type GridCorrector struct { objectMoveFactors *GridMap deltaMiddle float64 imgWidth, imgHeight int + sizeThreshold float64 } /* @@ -130,7 +138,9 @@ AdjustFromObjectPosition modify steering value according object positions 40% |-----|-----|-----|-----|-----|-----| : | ... | ... | ... | ... | ... | ... | */ -func (c *GridCorrector) AdjustFromObjectPosition(currentSteering float64, objects []*events.Object) float64 { +func (c *GridCorrector) AdjustFromObjectPosition(currentSteering float64, objs []*events.Object) float64 { + objects := c.filter_big_objects(objs, c.imgWidth, c.imgHeight, c.sizeThreshold) + zap.S().Debugf("%v objects to avoid", len(objects)) if len(objects) == 0 { return currentSteering @@ -213,6 +223,17 @@ func (c *GridCorrector) nearObject(objects []*events.Object) (*events.Object, er return result, nil } +func (c *GridCorrector) filter_big_objects(objts []*events.Object, imgWidth int, imgHeight int, sizeThreshold float64) []*events.Object { + objectFiltred := make([]*events.Object, 0, len(objts)) + sizeLimit := float64(imgWidth*imgHeight) * sizeThreshold + for _, o := range objts { + if sizeObject(o, imgWidth, imgHeight) < sizeLimit { + objectFiltred = append(objectFiltred, o) + } + } + return objectFiltred +} + func NewGridMapFromJson(fileName string) (*GridMap, error) { content, err := os.ReadFile(fileName) if err != nil {