# `BB.Camera.Annotation`
[🔗](https://github.com/beam-bots/bb_camera/blob/main/lib/bb/camera/annotation.ex#L5)

How a message payload is drawn over the frame it describes.

An estimator nested under a camera publishes what it found - markers,
detections, a horizon line - as an ordinary semantic payload, and the
package that owns that payload implements this protocol for it. A viewer
that shows the camera's video draws each such message as a
`BB.Camera.Annotation.Layer` over it, without knowing what the payload is.
Estimators never publish separate drawing messages: what is drawn is
derived from what was found, in one place.

## What a viewer does with a layer

Each publishing path is one layer, and each message on that path replaces
the whole layer. There are no per-item identifiers or actions, because one
estimator owns each layer and knows its full state every time it publishes.
A layer with no primitives clears it.

A layer also disappears once its `lifetime` has passed with nothing newer,
counted from the message's capture time (its envelope's `monotonic_time`)
rather than from when it arrived, so a result that was late getting
anywhere is shown for less time rather than for longer.

## Implementing it

The lifetime is chosen here, by the payload's owner, because a layer is all
the viewer sees. An estimator that publishes on every frame wants a few frame
intervals: long enough to ride out a dropped frame, short enough that a
stalled estimator's last result doesn't linger over live video.

    defimpl BB.Camera.Annotation, for: MyPackage.Detections do
      alias BB.Camera.Annotation.{Layer, Points}

      def layer(detections) do
        Layer.new!(
          width: detections.width,
          height: detections.height,
          lifetime: ~u(200 millisecond),
          primitives:
            Enum.map(detections.boxes, fn box ->
              Points.new!(type: :line_loop, points: box, outline_color: ...)
            end)
        )
      end
    end

## Consolidation

A consumer finds out whether a payload can be drawn by calling
`BB.Camera.Annotation.impl_for(payload)`. With protocols consolidated - Mix's
default in `:prod`, and every Beam Bots package's - that knows only the
implementations compiled into the application, which is every dependency's,
so it finds all of them. Unconsolidated, as in `:dev` and `:test`, it looks
each one up as it is asked, so an implementation defined in a test is found
too.

# `t`

```elixir
@type t() :: term()
```

All the types that implement this protocol.

# `layer`

```elixir
@spec layer(t()) :: BB.Camera.Annotation.Layer.t()
```

The layer to draw for `payload`.

---

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