# `BB.Estimator.ObjectDetection`
[🔗](https://github.com/beam-bots/bb_estimator_object_detection/blob/main/lib/bb/estimator/object_detection.ex#L5)

Finds objects in camera frames with any object-detection model declared in
the robot's `inference` section.

    inference do
      model :detector do
        runtime BB.Inference.OpenCV
        source "https://github.com/Megvii-BaseDetection/YOLOX/releases/download/0.1.1rc0/yolox_nano.onnx"
        sha256 "c789161ed43c8269fcd4e67c67eeeb4e80c622da2eb296a20bc6007bd18a0b7d"
        codec BB.Estimator.Yolo.YoloX
        labels MyRobot.Coco.labels()
      end
    end

    topology do
      link :head do
        sensor :camera, {BB.Camera.V4L, device: "/dev/video0"} do
          estimator :objects, {BB.Estimator.ObjectDetection, model: :detector}
        end
      end
    end

It names only the model. Everything about the artefact - how to prepare a
frame for it, how to read what it gives back - belongs to the model's
codec, so swapping one artefact for another changes the `model` and
nothing here. Compiling the robot fails if `:model` names no model, or one
whose codec isn't an object detector.

## What it does with each frame

1. The codec prepares the frame and decodes the model's output into
   candidates at or above `:score_threshold`
   (`BB.Inference.infer/4`).
2. If the codec says its candidates still overlap, it suppresses those
   overlapping by more than `:nms_threshold` intersection-over-union,
   keeping the highest scoring, whatever their class (`suppress/3`).
3. It labels each detection from the model's `labels`, sorts them highest
   score first, and publishes a `BB.Message.Vision.Detections` at its own
   path, with the frame's `frame_id` and capture time.

It publishes for every frame it examines, including frames with nothing in
them. Only `{:raw, :I420}` frames are read; anything else is ignored. Like
every camera estimator, it is handed the newest frame each time it finishes
the last, so a model slower than the camera skips frames rather than falling
behind.

## Start-up

It fetches the loaded model in `handle_continue/2`, waiting for the model to
finish loading. If the model isn't running - in simulation, models are
omitted unless declared with `simulation :start` - it logs why and stops,
and its supervisor restarts it like any component that can't start.

## Telemetry

Each frame is a `[:bb, :estimator, :object_detection]` span, with
`%{robot: robot, path: path, model: model}` metadata and the number of
detections in the stop metadata. Inside it, `BB.Inference.infer/4` emits
its own spans for encoding, running and decoding.

# `suppress`

```elixir
@spec suppress([BB.Message.Vision.Detections.Detection.t()], float(), float()) :: [
  BB.Message.Vision.Detections.Detection.t()
]
```

Non-maximum suppression: of any two detections whose boxes overlap by more
than `nms_threshold` intersection-over-union, drop the lower scoring,
whatever their classes. Detections below `score_threshold` are dropped too.

Suppression ignores class, as YOLOX's own post-processing does: a detector
often scores one object as two similar classes - a dog and a cat - from
neighbouring cells, and only the more confident should survive.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
