feat(objectDetector): add cli option to remove big object

This commit is contained in:
Cyrille Nofficial 2024-01-15 19:14:24 +01:00
parent f97c9cfc61
commit 67dc92e476
3 changed files with 28 additions and 1 deletions

View File

@ -86,6 +86,7 @@ func main() {
steering.WithGridMap(gridMapConfig), steering.WithGridMap(gridMapConfig),
steering.WithObjectMoveFactors(objectsMoveFactorsConfig), steering.WithObjectMoveFactors(objectsMoveFactorsConfig),
steering.WithImageSize(imgWidth, imgHeight), steering.WithImageSize(imgWidth, imgHeight),
steering.WithSizeThreshold(0.75),
), ),
), ),
steering.WithObjectsCorrectionEnabled(enableObjectsCorrection, enableObjectsCorrectionOnUserMode), steering.WithObjectsCorrectionEnabled(enableObjectsCorrection, enableObjectsCorrectionOnUserMode),

View File

@ -45,6 +45,11 @@ func objectToRect(object *events.Object, imgWidth, imgHeight int) *image.Rectang
return &r 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 { func rectToObject(r *image.Rectangle, imgWidth, imgHeight int) *events.Object {
return &events.Object{ return &events.Object{
Type: events.TypeObject_ANY, Type: events.TypeObject_ANY,

View File

@ -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 { func WidthDeltaMiddle(d float64) OptionCorrector {
return func(c *GridCorrector) { return func(c *GridCorrector) {
c.deltaMiddle = d c.deltaMiddle = d
@ -80,6 +86,7 @@ func NewGridCorrector(options ...OptionCorrector) *GridCorrector {
deltaMiddle: 0.1, deltaMiddle: 0.1,
imgWidth: 160, imgWidth: 160,
imgHeight: 120, imgHeight: 120,
sizeThreshold: 0.75,
} }
for _, o := range options { for _, o := range options {
o(c) o(c)
@ -92,6 +99,7 @@ type GridCorrector struct {
objectMoveFactors *GridMap objectMoveFactors *GridMap
deltaMiddle float64 deltaMiddle float64
imgWidth, imgHeight int imgWidth, imgHeight int
sizeThreshold float64
} }
/* /*
@ -130,7 +138,9 @@ AdjustFromObjectPosition modify steering value according object positions
40% |-----|-----|-----|-----|-----|-----| 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)) zap.S().Debugf("%v objects to avoid", len(objects))
if len(objects) == 0 { if len(objects) == 0 {
return currentSteering return currentSteering
@ -213,6 +223,17 @@ func (c *GridCorrector) nearObject(objects []*events.Object) (*events.Object, er
return result, nil 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) { func NewGridMapFromJson(fileName string) (*GridMap, error) {
content, err := os.ReadFile(fileName) content, err := os.ReadFile(fileName)
if err != nil { if err != nil {