Skip to content

label a calibrated image in 3D

Keywords: camera · pinhole · calibration · labelme · image labeling · CalibratedPlaneMapper · frustum · split view · InteractionSurface · checkbox · ImageData.update · background image

Loads one bundled BOP T-LESS training image plus its pinhole calibration and a small labelme annotation file, then shows the same world scene in two panes:

  • left — the calibrated camera view (the photo as a 2d-navigation background), where the labelme shapes — mapped from pixel space to a fixed depth plane in front of the camera by a ~pytanga.viz.CalibratedPlaneMapper — are drawn as interactive ~pytanga.viz.ActSceneObject composites (drag their handles to edit). A toolbar drives drag-to-draw on the pane's ~pytanga.viz.InteractionSurface (the ⟂-optical-axis plane at _DEPTH);
  • right — the same scene from an overview camera (read-only), showing the camera ~pytanga.geometry.Frustum and the same shapes in 3D.
  • toolbar — a "Noise image" checkbox swaps the background between the photo and generated noise (exercising ImageData.update()), and a "Swap views" button swaps the two panes (exercising the background image surviving a layout re-push).

Pixel↔world mapping is a ~pytanga.viz.CalibratedPlaneMapper handed to ~pytanga.viz.LabelMeStore, so the labelme JSON round-trips through 3D. Attribution: T-LESS, Hodan et al., WACV 2017, CC BY 4.0.

Run

uv run python py/examples/apps/calibrated_labeling_app.py

Source

apps/calibrated_labeling_app.py

Code

# SPDX-License-Identifier: Apache-2.0
# Copyright 2021 Christian Perwass

"""calibrated_labeling_app.py — label a calibrated image in 3D.

Loads one bundled BOP T-LESS training image plus its pinhole calibration and a
small labelme annotation file, then shows the **same** ``world`` scene in two
panes:

- **left** — the calibrated camera view (the photo as a ``2d``-navigation
  background), where the labelme shapes — mapped from pixel space to a fixed
  depth plane in front of the camera by a
  :class:`~pytanga.viz.CalibratedPlaneMapper` — are drawn as interactive
  :class:`~pytanga.viz.ActSceneObject` composites (drag their handles to edit).
  A toolbar drives drag-to-draw on the pane's
  :class:`~pytanga.viz.InteractionSurface` (the ⟂-optical-axis plane at
  ``_DEPTH``);
- **right** — the same scene from an overview camera (read-only), showing the
  camera :class:`~pytanga.geometry.Frustum` and the same shapes in 3D.
- **toolbar** — a "Noise image" checkbox swaps the background between the photo
  and generated noise (exercising ``ImageData.update()``), and a "Swap views"
  button swaps the two panes (exercising the background image surviving a layout
  re-push).

Pixel↔world mapping is a :class:`~pytanga.viz.CalibratedPlaneMapper` handed to
:class:`~pytanga.viz.LabelMeStore`, so the labelme JSON round-trips through 3D.
Attribution: T-LESS, Hodan et al., WACV 2017, CC BY 4.0.

Run with:  uv run python py/examples/apps/calibrated_labeling_app.py

Keywords: camera, pinhole, calibration, labelme, image labeling, CalibratedPlaneMapper, frustum, split view, InteractionSurface, checkbox, ImageData.update, background image
"""

from __future__ import annotations

import json
import math
from pathlib import Path
from typing import Any

import numpy as np
from PIL import Image

from pytanga.geometry import (
    Circle,
    Ellipse,
    Frustum,
    Line,
    Matrix,
    OpenCVFrame,
    Point,
    Rectangle2D,
)
from pytanga.viz import (
    ActiveObject,
    ActCircle,
    ActEllipse,
    ActLine,
    ActPoint,
    ActPolygon,
    ActRectangle2D,
    ButtonView,
    CalibratedPlaneMapper,
    CalibratedSurface,
    CameraCalibration,
    CameraConfig3d,
    CameraView,
    CheckboxView,
    CirclePointStyle,
    CircleStyle,
    Color,
    ControlEvent,
    DragBinding,
    DragMode,
    DragPreview,
    EllipseStyle,
    GroupView,
    ImageData,
    LabelMeStore,
    LabelView,
    LineStyle,
    ModifierKey,
    MouseButton,
    PointPath,
    PointPathStyle,
    Rectangle2DStyle,
    SceneView,
    Size,
    SplitView,
    SquarePointStyle,
    StackView,
    ToolbarView,
    ViewportConfig,
    Visualizer,
)
from pytanga.viz.image import pil_to_numpy

_DATA_DIR = Path(__file__).resolve().parent.parent / "viz" / "camera" / "data" / "tless"
_LABELS_PATH = _DATA_DIR / "labels.json"

#: Annotation plane distance (metres) in front of the camera.
_DEPTH = 0.6

_ATTRIBUTION = (
    "Image & data: T-LESS (Hodan et al., WACV 2017), BOP benchmark — License: CC BY 4.0"
)


def _load_calibration() -> tuple[dict[str, Any], ImageData]:
    data = json.loads((_DATA_DIR / "calibration.json").read_text(encoding="utf-8"))
    img = Image.open(_DATA_DIR / data["image"])
    img.load()
    return data, ImageData("camera", data=pil_to_numpy(img))


def _frustum_corners(frustum: Frustum) -> np.ndarray:
    """Return the 8 corner points of a ``Frustum`` (N×3, metres)."""
    o = np.array([frustum.origin.x, frustum.origin.y, frustum.origin.z], dtype=float)
    axis = np.array([frustum.axis.x, frustum.axis.y, frustum.axis.z], dtype=float)
    horiz = np.array(
        [frustum.horizontal.x, frustum.horizontal.y, frustum.horizontal.z],
        dtype=float,
    )
    vert = np.cross(horiz, axis)

    def plane(d: float, hw: float, hh: float) -> list[np.ndarray]:
        center = o + axis * d
        return [
            center + horiz * (sx * hw) + vert * (sy * hh)
            for sx in (-1.0, 1.0)
            for sy in (-1.0, 1.0)
        ]

    if frustum.near <= 0.0:
        near_pts = [o, o, o, o]
    else:
        near_pts = plane(
            frustum.near,
            frustum.far_half_width * frustum.near / frustum.far,
            frustum.far_half_height * frustum.near / frustum.far,
        )
    return np.asarray(
        near_pts + plane(frustum.far, frustum.far_half_width, frustum.far_half_height),
        dtype=float,
    )


def _overview_camera(points: np.ndarray, *, fov: float = 50.0) -> CameraConfig3d:
    """Return a 3D camera framing *points* (N×3) from a three-quarter view."""
    lo = points.min(axis=0)
    hi = points.max(axis=0)
    center = (lo + hi) / 2.0
    radius = 0.5 * float(np.linalg.norm(hi - lo))
    distance = (radius / math.sin(math.radians(fov) / 2.0)) * 1.1
    direction = np.array([0.6, 0.5, 0.7])
    direction /= np.linalg.norm(direction)
    position = center + direction * distance
    return CameraConfig3d(
        fov=fov,
        position=(float(position[0]), float(position[1]), float(position[2])),
        target=(float(center[0]), float(center[1]), float(center[2])),
        up=(0.0, 1.0, 0.0),
        near=max(0.01, distance * 0.001),
        far=distance * 10.0,
    )


def _style_for(mode: str, fill: str) -> Any:
    if mode == "rect":
        return Rectangle2DStyle(color=fill, fill=True, fill_opacity=0.15)
    if mode == "ellipse":
        return EllipseStyle(color=fill, fill=True, fill_opacity=0.15)
    if mode == "circle":
        return CircleStyle(color=fill, thickness=2, fill=True, fill_opacity=0.15)
    if mode == "polygon":
        return PointPathStyle(color=fill, line_thickness=2)
    if mode == "point":
        return SquarePointStyle(color=fill, size=6.0, thickness=2, screen_space=True)
    return LineStyle(color=fill, thickness=2)


def _loaded_style(obj: Any) -> Any:
    """The style for a loaded labelme entity (points are screen-space markers)."""
    if isinstance(obj, Point):
        return SquarePointStyle(size=6.0, thickness=2, screen_space=True)
    if isinstance(obj, Rectangle2D):
        return Rectangle2DStyle(fill=True, fill_opacity=0.15)
    if isinstance(obj, Ellipse):
        return EllipseStyle(fill=True, fill_opacity=0.15)
    if isinstance(obj, Circle):
        return CircleStyle(thickness=2, fill=True, fill_opacity=0.15)
    if isinstance(obj, PointPath):
        return PointPathStyle(line_thickness=2)
    return LineStyle(thickness=2)


class _CalibratedLabeler:
    """Draw/select shapes on the calibrated plane, adding them to the world scene."""

    _FACTORIES = {
        "rect": ActRectangle2D,
        "ellipse": ActEllipse,
        "circle": ActCircle,
        "line": ActLine,
        "polygon": ActPolygon,
    }

    def __init__(
        self,
        world: Any,
        calib: CameraCalibration,
        depth: float,
        *,
        fill: str = "#ff4444",
    ) -> None:
        self._world = world
        self._bg: Any = None
        self._mode: str | None = None
        self.selected: Any = None
        self.shapes: list[tuple[Any, Any]] = []  # (act, body_style)
        self.selected_color = "#ffff44"
        self._styles: dict[str, Any] = {}
        self._previews: dict[str, DragPreview] = {}

        # World units per image pixel, from the mapper (single source).
        self._pixel_scale = CalibratedPlaneMapper(calib, depth).world_units_per_pixel()
        # 0.5 image px minimum half-extent, in world units.
        self._min_half = 0.5 * self._pixel_scale
        self._max_half: float | None = None

        select = self._make_select_handler()
        vertex_style = CirclePointStyle(
            color=Color.RED, size=6.0, thickness=2.0, screen_space=True
        )
        end_style = CirclePointStyle(
            color=Color.GREEN, size=6.0, thickness=2.0, screen_space=True
        )
        for mode, factory in self._FACTORIES.items():
            style = _style_for(mode, fill)
            self._styles[mode] = style
            kwargs = {"on_click": select}
            if mode == "polygon":
                kwargs["handle_style"] = vertex_style
                kwargs["end_handle_style"] = end_style
            self._previews[mode] = DragPreview(
                world,
                factory=factory,
                style=style,
                factory_kwargs=kwargs,
            )
        # "point" is click-to-place (no DragPreview), but still needs a style.
        self._styles["point"] = _style_for("point", fill)

        self._draw_binding = DragBinding(MouseButton.LEFT, self._on_drag, enabled=False)
        self._brightness_contrast_binding = DragBinding(
            MouseButton.LEFT, self._on_brightness_contrast_drag, ModifierKey.CTRL
        )
        self._tool_buttons: dict[str, ButtonView] = {}
        self.surface = CalibratedSurface(
            calib,
            depth,
            on_drag_start=self._on_drag_start,
            on_drag_end=self._on_drag_end,
            on_click=self._on_click,
            drag_bindings=[self._draw_binding, self._brightness_contrast_binding],
        )

        world.on_key("Delete", self._on_delete)
        world.on_key("Backspace", self._on_delete)
        world.on_key("Escape", self._on_escape)

    def set_mode(self, mode: str | None) -> None:
        self._mode = mode
        self._draw_binding.enabled = mode is not None and mode != "point"
        self.surface.set_enabled(mode is not None)
        self.surface.set_click_enabled(mode == "point")
        self._world.set_cursor("crosshair" if mode is not None else None)
        for cid, button in self._tool_buttons.items():
            button.set_selected(cid == mode)
        self._deselect()
        self._sync_handles()

    def set_background_view(self, viz: Any, view: Any) -> None:
        """Bind the ctrl+drag brightness/contrast handler to *view*'s background."""
        self._bg = view.camera_view.background_image

    async def _on_brightness_contrast_drag(self, event: Any, _surface: Any) -> bool:
        # Ctrl+left drag: horizontal movement → contrast, vertical → brightness.
        if self._bg is None:
            return False
        dx, dy = event.delta_pixels
        u = self._bg.uniforms
        self._bg.set_uniform("u_contrast", max(0.0, u["u_contrast"] + 0.005 * dx))
        self._bg.set_uniform("u_brightness", u["u_brightness"] + 0.005 * dy)
        return True
        return True

    def toolbar(self) -> ToolbarView:
        def _button(cid: str, icon: str, tip: str) -> ButtonView:
            async def on_click(_value: Any, _event: Any) -> None:
                self.set_mode(cid if self._mode != cid else None)

            return ButtonView(
                cid, icon=icon, icon_only=True, tooltip=tip, on_click=on_click
            )

        self._tool_buttons = {
            "rect": _button("rect", "material:crop_square", "Add rectangle"),
            "ellipse": _button("ellipse", "material:app_badging", "Add ellipse"),
            "circle": _button("circle", "material:circle", "Add circle"),
            "line": _button("line", "material:diagonal_line", "Add line"),
            "polygon": _button("polygon", "material:gesture", "Add polygon"),
            "point": _button("point", "material:place", "Add point (click)"),
        }
        return ToolbarView(list(self._tool_buttons.values()), border=False)

    async def _on_drag_start(self, event: Any, _surface: Any) -> None:
        if ModifierKey.CTRL in event.modifiers:
            # Ctrl+drag is the brightness/contrast gesture, not shape creation.
            return
        preview = self._previews.get(self._mode or "")
        if preview is not None:
            preview.begin(event.world_position)

    async def _on_drag(self, event: Any, _surface: Any) -> bool:
        preview = self._previews.get(self._mode or "")
        if preview is None:
            return False
        if preview.anchor is None:
            preview.begin(event.world_position)
        preview.update(event.world_position)
        return True

    async def _on_drag_end(self, event: Any, _surface: Any) -> None:
        if ModifierKey.CTRL in event.modifiers:
            return
        preview = self._previews.get(self._mode or "")
        if preview is None or preview.anchor is None:
            return
        if math.hypot(*event.delta_pixels) < 3.0:
            # A click (negligible movement) must not create a shape.
            preview.discard()
            return
        act = preview.finalize(event.world_position)
        self._add_shape(act, self._styles[self._mode or ""])
        self._select(act)

    async def _on_click(self, event: Any, _surface: Any) -> None:
        if self._mode != "point":
            return
        act = ActPoint(
            event.world_position,
            drag_mode=DragMode.VIEW_PLANE,
            on_click=self._make_select_handler(),
        )
        self._add_shape(act, self._styles["point"])
        self._select(act)

    # ── Shape management / selection ────────────────────────

    def _make_select_handler(self) -> Any:
        async def on_click(_event: Any, act: Any) -> None:
            self._select(act)

        return on_click

    def _select(self, act: Any) -> None:
        if self.selected is act:
            return
        self._deselect()
        self.selected = act
        self._set_selected_style(act, True)

    def _deselect(self) -> None:
        if self.selected is None:
            return
        self._set_selected_style(self.selected, False)
        self.selected = None

    def _set_selected_style(self, act: Any, selected: bool) -> None:
        """Recolor the body (yellow) on selection and restore it on deselect."""
        for shape_act, style in self.shapes:
            if shape_act is act:
                color = self.selected_color if selected else (style.color or "#ffffff")
                self._world.update(shape_act.entity_id, color=color)
                break
        self._world.flush()

    async def _on_delete(self, _event: Any) -> None:
        if self.selected is None:
            return
        act = self.selected
        self.selected = None
        self.shapes = [(a, s) for a, s in self.shapes if a is not act]
        act.remove()
        self._world.flush()

    async def _on_escape(self, _event: Any) -> None:
        self._deselect()
        for preview in self._previews.values():
            preview.discard()

    def _sync_handles(self) -> None:
        """Show control points for every entity of the active edit type."""
        active_type = self._FACTORIES.get(self._mode or "")
        for act, _style in self.shapes:
            if isinstance(act, ActPoint):
                # A bare point stays visible; it is only draggable in point mode.
                act.set_handles_enabled(self._mode == "point")
            elif isinstance(act, ActiveObject):
                act.set_handles_visible(
                    active_type is not None and isinstance(act, active_type)
                )

    def _apply_size_limits(self, act: Any) -> None:
        """Clamp resize to the app's pixel-derived limits (half-extent)."""
        act.set_pixel_scale(self._pixel_scale)
        if isinstance(act, ActRectangle2D):
            act.set_size_limits(
                None if self._min_half is None else 2.0 * self._min_half,
                None if self._max_half is None else 2.0 * self._max_half,
            )
        elif isinstance(act, (ActCircle, ActEllipse)):
            act.set_radius_limits(self._min_half, self._max_half)

    def _add_shape(self, act: Any, style: Any) -> None:
        self._apply_size_limits(act)
        self._world.add(act, style=style)
        self.shapes.append((act, style))
        self._sync_handles()
        self._world.flush()

    def _act_from_entity(self, entity: Any) -> Any:
        """Wrap a plain labelme entity in an interactive act (click-to-select)."""
        select = self._make_select_handler()
        if isinstance(entity, Point):
            return ActPoint(entity, drag_mode=DragMode.VIEW_PLANE, on_click=select)
        if isinstance(entity, Line):
            return ActLine(start=entity.start, end=entity.end, on_click=select)
        if isinstance(entity, Circle):
            return ActCircle.from_entity(entity, on_click=select)
        if isinstance(entity, Rectangle2D):
            return ActRectangle2D.from_entity(entity, on_click=select)
        if isinstance(entity, Ellipse):
            return ActEllipse.from_entity(entity, on_click=select)
        if isinstance(entity, PointPath):
            pts = [Point(x, y, z) for x, y, z in entity.points]
            closed = len(pts) > 1 and pts[0] == pts[-1]
            if closed:
                pts = pts[:-1]
            return ActPolygon(pts, closed=closed, on_click=select)
        raise TypeError(f"unsupported labelme entity: {type(entity).__name__}")

    def add_loaded_shape(self, entity: Any) -> None:
        """Add a loaded labelme entity: wrap it in an act, style it, wire select."""
        self._add_shape(self._act_from_entity(entity), _loaded_style(entity))


def main() -> None:
    data, background = _load_calibration()
    width, height = int(data["width"]), int(data["height"])

    calib = CameraCalibration(
        K=Matrix(data["K"]),
        R=Matrix(data["R_w2c"]),
        t=data["t_w2c"],
        image_size=(width, height),
        frame=OpenCVFrame(),
        units=0.001,  # T-LESS stores lengths in millimetres
    )
    cam = calib.to_pinhole_camera()

    store = LabelMeStore(mapper=CalibratedPlaneMapper(calib, _DEPTH))

    viz = Visualizer(
        title="Tanga — Calibrated Labeling (T-LESS)",
        add_default_axes=False,
        add_default_grid=False,
    )
    world = viz.scene("world")

    labeler = _CalibratedLabeler(world, calib, _DEPTH)

    result = store.load(_LABELS_PATH)
    for message in result.errors:
        print(f"labelme: skipped {message}")
    pairs, errors = store.iter_objects(result.document)
    for message in errors:
        print(f"labelme: skipped {message}")
    for entity, _label in pairs:
        labeler.add_loaded_shape(entity)

    frustum = Frustum.from_camera(cam, near=0.05, far=0.7)
    frustum_ref = world.new(frustum, color="#ffcc44")

    overview_cam = _overview_camera(_frustum_corners(frustum))

    left = SceneView(
        "world",
        camera_view=CameraView(
            cam,
            navigation="2d",
            viewport=ViewportConfig(zoom=1.0, pan=(0.0, 0.0)),
            background_image=background,
        ),
        surface=labeler.surface,
    )
    left.set_visible(frustum_ref.id, False)
    right = SceneView(
        "world",
        camera_view=CameraView(overview_cam),
        read_only=True,
        overlay=[
            GroupView(
                "Data",
                [LabelView("attribution", value=_ATTRIBUTION, font_size=12)],
                position="bottom-left",
            )
        ],
    )

    # Ctrl+left-drag on the calibrated pane adjusts the background's brightness
    # (vertical) and contrast (horizontal).
    labeler.set_background_view(viz, left)

    # Keep the original pixels so the "Noise image" checkbox can round-trip.
    default_data = background.data
    assert default_data is not None
    noise_rng = np.random.default_rng(12345)

    async def _on_noise(value: Any, _event: ControlEvent) -> None:
        if value:
            background.update(
                data=noise_rng.integers(
                    0, 256, size=default_data.shape, dtype=default_data.dtype
                )
            )
        else:
            background.update(data=default_data)
        camera_view = left.camera_view
        if camera_view is not None and camera_view.background_image is not None:
            camera_view.background_image.set_image(background)

    async def _on_swap(_value: Any, _event: ControlEvent) -> None:
        # Reordering the split re-pushes the layout; the reused `background`
        # instance keeps its `version`, so the image frame is not re-sent.
        split.children.reverse()
        viz.set_layout(split)

    app_toolbar = ToolbarView(
        [
            CheckboxView(
                "noise", label="Noise image", value=False, on_change=_on_noise
            ),
            ButtonView("swap", label="Swap views", on_click=_on_swap),
        ]
    )

    left.preferred_height = Size.fr(1)
    left_pane = StackView("vertical", [app_toolbar, labeler.toolbar(), left], fill=True)
    split = SplitView("horizontal", [left_pane, right])

    viz.show(layout=split)
    viz.wait()


if __name__ == "__main__":
    main()