Packages

A generic Beam Bots object-detection estimator for camera frames, running any model declared with BB.Inference.

Current section

Files

Jump to
Raw

usage-rules.md

<!--
SPDX-FileCopyrightText: 2026 James Harton

SPDX-License-Identifier: Apache-2.0
-->

# BB.Estimator.ObjectDetection Usage Rules

`bb_estimator_object_detection` runs any `bb_inference` object-detection
model over camera frames and publishes `BB.Message.Vision.Detections`. See
`bb_inference`'s rules for models and `bb_camera`'s for cameras; this file
covers only what is specific here.

## Core principles

1. **Nest it inside a camera sensor.** Frames reach it through the camera's
   estimator source, never PubSub.
2. **Name a model, and nothing about it.** Colour order, normalisation and
   letterboxing go in the model's `codec` options.
3. **It publishes once per frame examined,** empty or not, with the frame's
   `frame_id` and capture time. It skips frames when it can't keep up.
4. **Boxes are `{x1, y1, x2, y2}` in frame pixels,** clipped to the frame.
5. **A codec must use `BB.Estimator.ObjectDetection.Image`** to prepare
   frames and to map boxes back.

## Wiring it in

```elixir
sensor :camera, {BB.Camera.V4L, device: "/dev/video0"} do
  estimator :objects, {BB.Estimator.ObjectDetection, model: :detector, score_threshold: 0.5}
end
```

```elixir
BB.subscribe(MyRobot, [:sensor, :head, :camera, :objects])

receive do
  {:bb, _path, %BB.Message{payload: %BB.Message.Vision.Detections{detections: detections}}} ->
    for d <- detections, do: {d.label, d.score, d.box}
end
```

## Options

| Option | Default | |
|---|---|---|
| `model` | required | Must name a model whose codec's task is `:object_detection` - checked at compile time |
| `score_threshold` | `0.3` | |
| `nms_threshold` | `0.45` | Only used when the codec's `nms?/1` is true |

## Writing a detection codec

Implement `BB.Inference.ModelCodec` and `BB.Estimator.ObjectDetection.Codec`.
Merge `Image.options_schema/1` into your options with your artefact's
defaults; build an `Image.Spec` in `fit/2`; call `Image.prepare/2` in
`encode/2` and keep the transform and threshold as context; return
`BB.Message.Vision.Detections.Detection`s in frame pixels from `decode/3`,
reading quantised outputs with `BB.Inference.Quantisation.value/5`.

## Anti-patterns

- **Declaring it under a link.** It receives no frames.
- **Converting images or whole tensors in Nx.** Use `Image` and lazy reads.
- **Expecting it to run in simulation** without `simulation :start` on the
  model: the estimator can't start, and the robot latches in `:error`.

## Further reading

- [Hexdocs](https://hexdocs.pm/bb_estimator_object_detection)
- `BB.Estimator.ObjectDetection`, `BB.Estimator.ObjectDetection.Image`,
  `BB.Estimator.ObjectDetection.Codec`