toolbox: projection rendering + world-frame check; gel centre fix
Browse files- toolbox/calib_epoch.py +58 -10
- toolbox/calibration.py +5 -1
- toolbox/frames.py +61 -9
- toolbox/viz.py +71 -0
- toolbox/world_frame.py +13 -3
toolbox/calib_epoch.py
CHANGED
|
@@ -44,6 +44,8 @@ import json
|
|
| 44 |
from functools import lru_cache
|
| 45 |
from pathlib import Path
|
| 46 |
|
|
|
|
|
|
|
| 47 |
REPO = Path(__file__).resolve().parent
|
| 48 |
RELEASE = Path("/media/yxma/Disk1/twm/release")
|
| 49 |
|
|
@@ -57,7 +59,7 @@ CALIB_DIRS = {
|
|
| 57 |
EXPECTED_EPOCH = {"motherboard": "2026-05-12", "pushT": "2026-06-26"}
|
| 58 |
|
| 59 |
|
| 60 |
-
def calib_dir(task: str) -> Path:
|
| 61 |
"""Directory of camera extrinsics valid for `task`.
|
| 62 |
|
| 63 |
Looked up in this order, so the module works outside the repository it
|
|
@@ -79,14 +81,40 @@ def calib_dir(task: str) -> Path:
|
|
| 79 |
slightly miscalibrated rig, which is how it shipped unnoticed once.
|
| 80 |
"""
|
| 81 |
import os as _os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
env = _os.environ.get("REACT_CALIB")
|
| 83 |
if env and Path(env).is_dir():
|
| 84 |
-
return Path(env)
|
| 85 |
rel = _os.environ.get("REACT_RELEASE")
|
| 86 |
if rel:
|
| 87 |
cand = Path(rel) / task / "calibration"
|
| 88 |
if cand.is_dir():
|
| 89 |
-
return cand
|
| 90 |
try:
|
| 91 |
d = CALIB_DIRS[task]
|
| 92 |
except KeyError:
|
|
@@ -99,12 +127,17 @@ def calib_dir(task: str) -> Path:
|
|
| 99 |
f"calibration dir for {task!r} missing: {d}. Set REACT_CALIB, or "
|
| 100 |
f"REACT_RELEASE so that $REACT_RELEASE/{task}/calibration exists "
|
| 101 |
f"(the dataset publishes it there).")
|
| 102 |
-
return d
|
| 103 |
|
| 104 |
|
| 105 |
-
def calib_dir_for_path(p: str | Path) -> Path:
|
| 106 |
"""Epoch dir inferred from any path containing a task-name component.
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
For the interactive viewers, whose old default was `calibration/result`
|
| 109 |
for every input — June-26 extrinsics under May recordings. No guess on
|
| 110 |
failure: raises, listing the known tasks, so the caller passes explicit
|
|
@@ -113,7 +146,7 @@ def calib_dir_for_path(p: str | Path) -> Path:
|
|
| 113 |
parts = set(Path(p).parts) | set(Path(p).resolve().parts)
|
| 114 |
hits = [t for t in CALIB_DIRS if t in parts]
|
| 115 |
if len(hits) == 1:
|
| 116 |
-
return calib_dir(hits[0])
|
| 117 |
raise KeyError(
|
| 118 |
f"cannot infer task from {str(p)!r} (matches: {hits or 'none'}); pass "
|
| 119 |
f"explicit --cam_calib/--gel_* paths. Known tasks: {sorted(CALIB_DIRS)}")
|
|
@@ -158,8 +191,15 @@ def release_episodes(task: str) -> set[str]:
|
|
| 158 |
return set(_episodes(task))
|
| 159 |
|
| 160 |
|
| 161 |
-
def world_offset_m(task: str, date: str, episode: str
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
Read from the release's own `episodes.jsonl` (`world_frame_offset`), never
|
| 165 |
restated. `episode` may be `episode_002` or `2026-05-19/episode_002`.
|
|
@@ -178,14 +218,22 @@ def world_offset_m(task: str, date: str, episode: str) -> tuple[float, float, fl
|
|
| 178 |
raise KeyError(
|
| 179 |
f"{task}: {key!r} is not in {RELEASE / task / 'episodes.jsonl'}, so "
|
| 180 |
f"its world-frame offset is unknown. Refusing to assume zero — "
|
| 181 |
-
f"2026-05-19 is offset (0.23, 0, 0
|
|
|
|
| 182 |
off = eps[key].get("world_frame_offset") or (0.0, 0.0, 0.0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
return (float(off[0]), float(off[1]), float(off[2]))
|
| 184 |
|
| 185 |
|
| 186 |
def describe(task: str, date: str, episode: str) -> str:
|
| 187 |
"""One line for a status bar, so the applied correction is visible."""
|
| 188 |
-
|
|
|
|
| 189 |
s = f"calib {epoch_of(task)}"
|
| 190 |
if any((dx, dy, dz)):
|
| 191 |
s += f" world+({dx:g},{dy:g},{dz:g})m"
|
|
|
|
| 44 |
from functools import lru_cache
|
| 45 |
from pathlib import Path
|
| 46 |
|
| 47 |
+
import numpy as np
|
| 48 |
+
|
| 49 |
REPO = Path(__file__).resolve().parent
|
| 50 |
RELEASE = Path("/media/yxma/Disk1/twm/release")
|
| 51 |
|
|
|
|
| 59 |
EXPECTED_EPOCH = {"motherboard": "2026-05-12", "pushT": "2026-06-26"}
|
| 60 |
|
| 61 |
|
| 62 |
+
def calib_dir(task: str, *, up_axis: str | None = None) -> Path:
|
| 63 |
"""Directory of camera extrinsics valid for `task`.
|
| 64 |
|
| 65 |
Looked up in this order, so the module works outside the repository it
|
|
|
|
| 81 |
slightly miscalibrated rig, which is how it shipped unnoticed once.
|
| 82 |
"""
|
| 83 |
import os as _os
|
| 84 |
+
|
| 85 |
+
def _ok(d: Path) -> Path:
|
| 86 |
+
"""Refuse a tree in the wrong convention instead of returning it.
|
| 87 |
+
|
| 88 |
+
This resolves $REACT_RELEASE before the repo's own tree, and the
|
| 89 |
+
release is now Z-up while every raw-HDF5 reader is Y-up. Returning a
|
| 90 |
+
path cannot convert anything, so the only honest options are the right
|
| 91 |
+
tree or an error -- not a 200 px picture that looks plausible.
|
| 92 |
+
"""
|
| 93 |
+
if up_axis is None:
|
| 94 |
+
return d
|
| 95 |
+
if up_axis not in ("y", "z"):
|
| 96 |
+
raise ValueError(f"up axis must be 'y' or 'z', got {up_axis!r}")
|
| 97 |
+
f = d / "T_mocap_to_cam_middle.json"
|
| 98 |
+
got = "y"
|
| 99 |
+
if f.exists():
|
| 100 |
+
got = json.loads(f.read_text()).get("up_axis") or "y"
|
| 101 |
+
if got != up_axis:
|
| 102 |
+
raise ValueError(
|
| 103 |
+
f"{d} is a {got}-up calibration but the caller needs "
|
| 104 |
+
f"{up_axis}-up. Raw HDF5 poses are Y-up as recorded; the "
|
| 105 |
+
f"published release is Z-up. Point REACT_CALIB at a {up_axis}"
|
| 106 |
+
f"-up tree, or convert with react_toolbox.frames.as_up_axis "
|
| 107 |
+
f"after loading.")
|
| 108 |
+
return d
|
| 109 |
+
|
| 110 |
env = _os.environ.get("REACT_CALIB")
|
| 111 |
if env and Path(env).is_dir():
|
| 112 |
+
return _ok(Path(env))
|
| 113 |
rel = _os.environ.get("REACT_RELEASE")
|
| 114 |
if rel:
|
| 115 |
cand = Path(rel) / task / "calibration"
|
| 116 |
if cand.is_dir():
|
| 117 |
+
return _ok(cand)
|
| 118 |
try:
|
| 119 |
d = CALIB_DIRS[task]
|
| 120 |
except KeyError:
|
|
|
|
| 127 |
f"calibration dir for {task!r} missing: {d}. Set REACT_CALIB, or "
|
| 128 |
f"REACT_RELEASE so that $REACT_RELEASE/{task}/calibration exists "
|
| 129 |
f"(the dataset publishes it there).")
|
| 130 |
+
return _ok(d)
|
| 131 |
|
| 132 |
|
| 133 |
+
def calib_dir_for_path(p: str | Path, *, up_axis: str = "y") -> Path:
|
| 134 |
"""Epoch dir inferred from any path containing a task-name component.
|
| 135 |
|
| 136 |
+
Defaults to `up_axis="y"` because every caller is an interactive viewer
|
| 137 |
+
reading poses straight out of the source HDF5, which is Y-up as recorded.
|
| 138 |
+
If $REACT_RELEASE points calib_dir at the Z-up release, this raises rather
|
| 139 |
+
than viewing through a 200 px error.
|
| 140 |
+
|
| 141 |
For the interactive viewers, whose old default was `calibration/result`
|
| 142 |
for every input — June-26 extrinsics under May recordings. No guess on
|
| 143 |
failure: raises, listing the known tasks, so the caller passes explicit
|
|
|
|
| 146 |
parts = set(Path(p).parts) | set(Path(p).resolve().parts)
|
| 147 |
hits = [t for t in CALIB_DIRS if t in parts]
|
| 148 |
if len(hits) == 1:
|
| 149 |
+
return calib_dir(hits[0], up_axis=up_axis)
|
| 150 |
raise KeyError(
|
| 151 |
f"cannot infer task from {str(p)!r} (matches: {hits or 'none'}); pass "
|
| 152 |
f"explicit --cam_calib/--gel_* paths. Known tasks: {sorted(CALIB_DIRS)}")
|
|
|
|
| 191 |
return set(_episodes(task))
|
| 192 |
|
| 193 |
|
| 194 |
+
def world_offset_m(task: str, date: str, episode: str, *,
|
| 195 |
+
up_axis: str) -> tuple[float, float, float]:
|
| 196 |
+
"""Offset to ADD to poses to reach the release frame, in `up_axis`.
|
| 197 |
+
|
| 198 |
+
`up_axis` is REQUIRED and has no default on purpose. The value is stored
|
| 199 |
+
Z-up because the release is, but the documented use -- adding it to a pose
|
| 200 |
+
read straight out of the source H5 -- is Y-up. A default would have been
|
| 201 |
+
right for one caller and silently 175 mm wrong on the wrong axis for the
|
| 202 |
+
other, which is how this class of bug got in.
|
| 203 |
|
| 204 |
Read from the release's own `episodes.jsonl` (`world_frame_offset`), never
|
| 205 |
restated. `episode` may be `episode_002` or `2026-05-19/episode_002`.
|
|
|
|
| 218 |
raise KeyError(
|
| 219 |
f"{task}: {key!r} is not in {RELEASE / task / 'episodes.jsonl'}, so "
|
| 220 |
f"its world-frame offset is unknown. Refusing to assume zero — "
|
| 221 |
+
f"2026-05-19 is offset (0.23, -0.175, 0) m Z-up and would "
|
| 222 |
+
f"render wrong.")
|
| 223 |
off = eps[key].get("world_frame_offset") or (0.0, 0.0, 0.0)
|
| 224 |
+
stored = eps[key].get("up_axis") or "y"
|
| 225 |
+
off = np.asarray([float(off[0]), float(off[1]), float(off[2])], float)
|
| 226 |
+
if stored != up_axis:
|
| 227 |
+
from react_toolbox.frames import YUP_TO_ZUP
|
| 228 |
+
M = np.asarray(YUP_TO_ZUP, float)
|
| 229 |
+
off = (M if up_axis == "z" else M.T) @ off
|
| 230 |
return (float(off[0]), float(off[1]), float(off[2]))
|
| 231 |
|
| 232 |
|
| 233 |
def describe(task: str, date: str, episode: str) -> str:
|
| 234 |
"""One line for a status bar, so the applied correction is visible."""
|
| 235 |
+
# a status line for the raw-H5 render paths, so: the Y-up convention
|
| 236 |
+
dx, dy, dz = world_offset_m(task, date, episode, up_axis="y")
|
| 237 |
s = f"calib {epoch_of(task)}"
|
| 238 |
if any((dx, dy, dz)):
|
| 239 |
s += f" world+({dx:g},{dy:g},{dz:g})m"
|
toolbox/calibration.py
CHANGED
|
@@ -23,7 +23,10 @@ def load_calibration(task_root):
|
|
| 23 |
plus "gel_left"/"gel_right" center (3,) in rigid-body mm.
|
| 24 |
"""
|
| 25 |
cdir = Path(task_root) / "calibration"
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
for cam in ("left", "middle", "right"):
|
| 28 |
p = cdir / f"T_mocap_to_cam_{cam}.json"
|
| 29 |
if not p.exists():
|
|
@@ -35,6 +38,7 @@ def load_calibration(task_root):
|
|
| 35 |
"serial": d.get("camera_serial"),
|
| 36 |
"rmse": d.get("rmse_mm", d.get("rmse_px")),
|
| 37 |
}
|
|
|
|
| 38 |
for side in ("left", "right"):
|
| 39 |
p = cdir / f"T_gel_to_rigid_{side}.json"
|
| 40 |
if p.exists():
|
|
|
|
| 23 |
plus "gel_left"/"gel_right" center (3,) in rigid-body mm.
|
| 24 |
"""
|
| 25 |
cdir = Path(task_root) / "calibration"
|
| 26 |
+
# A calibration written before the Z-up conversion carries no declaration.
|
| 27 |
+
# Absent means Y-up: that is what every pre-conversion file was, and
|
| 28 |
+
# defaulting the other way would let a stale tree pass a Z-up check.
|
| 29 |
+
out = {"cams": {}, "up_axis": None}
|
| 30 |
for cam in ("left", "middle", "right"):
|
| 31 |
p = cdir / f"T_mocap_to_cam_{cam}.json"
|
| 32 |
if not p.exists():
|
|
|
|
| 38 |
"serial": d.get("camera_serial"),
|
| 39 |
"rmse": d.get("rmse_mm", d.get("rmse_px")),
|
| 40 |
}
|
| 41 |
+
out["up_axis"] = d.get("up_axis", "y")
|
| 42 |
for side in ("left", "right"):
|
| 43 |
p = cdir / f"T_gel_to_rigid_{side}.json"
|
| 44 |
if p.exists():
|
toolbox/frames.py
CHANGED
|
@@ -1,15 +1,22 @@
|
|
| 1 |
"""Which way is up — declared, and converted as one piece or not at all.
|
| 2 |
|
| 3 |
-
WHAT THE DATA
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
THE CONVERSION, AND THE TRAP IN IT
|
| 15 |
|
|
@@ -34,7 +41,7 @@ from __future__ import annotations
|
|
| 34 |
|
| 35 |
import numpy as np
|
| 36 |
|
| 37 |
-
UP_AXIS_RECORDED = "
|
| 38 |
UP_AXIS_ROBOTICS = "z"
|
| 39 |
|
| 40 |
# (x, y, z)_yup -> (x, -z, y)_zup. det = +1, and it sends +y to +z.
|
|
@@ -74,6 +81,7 @@ def convert_calibration(cal: dict, to_zup: bool = True) -> dict:
|
|
| 74 |
"""
|
| 75 |
M = _R(to_zup)
|
| 76 |
out = {k: v for k, v in cal.items()}
|
|
|
|
| 77 |
out["cams"] = {}
|
| 78 |
for name, c in cal["cams"].items():
|
| 79 |
T = np.asarray(c["T_mocap_to_cam"], float).copy()
|
|
@@ -89,3 +97,47 @@ def to_zup(poses7, cal: dict):
|
|
| 89 |
one piece.
|
| 90 |
"""
|
| 91 |
return convert_poses(poses7, True), convert_calibration(cal, True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""Which way is up — declared, and converted as one piece or not at all.
|
| 2 |
|
| 3 |
+
WHAT THE DATA IS, AND WHAT IT WAS
|
| 4 |
|
| 5 |
+
The release is now **Z-up, right-handed**: the table normal in world
|
| 6 |
+
coordinates is (0.053, -0.056, 0.997), 4.4 degrees off +z, and both the pose
|
| 7 |
+
rotations and `T_mocap_to_cam` have determinant +1.
|
| 8 |
|
| 9 |
+
OptiTrack RECORDS Y-up. The release used to ship that unchanged and said so
|
| 10 |
+
nowhere, which is the worse half of the problem: robotics code overwhelmingly
|
| 11 |
+
assumes Z-up, and a reader taking `pose[2]` as height got a horizontal
|
| 12 |
+
coordinate with nothing to complain — plausible numbers, fine-looking plots,
|
| 13 |
+
and an error that surfaces only as a model that never learns which way gravity
|
| 14 |
+
points.
|
| 15 |
+
|
| 16 |
+
Poses and extrinsics were converted together, so every projection is
|
| 17 |
+
unchanged and every rendered preview, overlay and clip stayed valid. Only the
|
| 18 |
+
numbers moved. `ZUP_TO_YUP` converts back for anything that still wants the
|
| 19 |
+
raw OptiTrack convention.
|
| 20 |
|
| 21 |
THE CONVERSION, AND THE TRAP IN IT
|
| 22 |
|
|
|
|
| 41 |
|
| 42 |
import numpy as np
|
| 43 |
|
| 44 |
+
UP_AXIS_RECORDED = "z"
|
| 45 |
UP_AXIS_ROBOTICS = "z"
|
| 46 |
|
| 47 |
# (x, y, z)_yup -> (x, -z, y)_zup. det = +1, and it sends +y to +z.
|
|
|
|
| 81 |
"""
|
| 82 |
M = _R(to_zup)
|
| 83 |
out = {k: v for k, v in cal.items()}
|
| 84 |
+
out["up_axis"] = "z" if to_zup else "y"
|
| 85 |
out["cams"] = {}
|
| 86 |
for name, c in cal["cams"].items():
|
| 87 |
T = np.asarray(c["T_mocap_to_cam"], float).copy()
|
|
|
|
| 97 |
one piece.
|
| 98 |
"""
|
| 99 |
return convert_poses(poses7, True), convert_calibration(cal, True)
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def require_up_axis(cal: dict, expected: str = UP_AXIS_RECORDED, where: str = ""):
|
| 103 |
+
"""Raise unless `cal` declares the up-axis convention `expected`.
|
| 104 |
+
|
| 105 |
+
WHY THIS EXISTS. Poses and calibration are two halves of one convention,
|
| 106 |
+
and they are read from two paths. Rotating only one half leaves every
|
| 107 |
+
self-consistency check green -- projections still recompute exactly from
|
| 108 |
+
the same wrong matrix -- while the pictures are wrong. That happened: the
|
| 109 |
+
probe test set drew its poses from the Z-up release and its calibration
|
| 110 |
+
from a Y-up tree, and every overlay was a median 153 px off.
|
| 111 |
+
|
| 112 |
+
So the halves must be paired loudly, not by convention. A file with no
|
| 113 |
+
declaration is treated as the pre-conversion Y-up it was.
|
| 114 |
+
"""
|
| 115 |
+
got = cal.get("up_axis")
|
| 116 |
+
if got != expected:
|
| 117 |
+
raise ValueError(
|
| 118 |
+
f"calibration up-axis mismatch{' in ' + where if where else ''}: "
|
| 119 |
+
f"declared {got!r}, need {expected!r}. Poses and calibration must "
|
| 120 |
+
f"come from the same release. A missing declaration means a "
|
| 121 |
+
f"pre-conversion Y-up file -- convert it with "
|
| 122 |
+
f"scripts/convert_release_zup.py, or take the calibration from "
|
| 123 |
+
f"the release the poses came from.")
|
| 124 |
+
return cal
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
def as_up_axis(cal: dict, want: str) -> dict:
|
| 128 |
+
"""Return `cal` in the convention `want`, converting only if it must.
|
| 129 |
+
|
| 130 |
+
`require_up_axis` refuses a mismatch; this one repairs it. Use it when the
|
| 131 |
+
caller genuinely knows which convention its POSES are in -- a raw-HDF5
|
| 132 |
+
reader wants "y", a release reader wants "z" -- and should not have to care
|
| 133 |
+
which directory the calibration happened to come from.
|
| 134 |
+
|
| 135 |
+
An undeclared calibration is the pre-conversion Y-up it was, so this
|
| 136 |
+
converts it rather than trusting it.
|
| 137 |
+
"""
|
| 138 |
+
if want not in ("y", "z"):
|
| 139 |
+
raise ValueError(f"up axis must be 'y' or 'z', got {want!r}")
|
| 140 |
+
got = cal.get("up_axis") or "y"
|
| 141 |
+
if got == want:
|
| 142 |
+
return cal
|
| 143 |
+
return convert_calibration(cal, to_zup=(want == "z"))
|
toolbox/viz.py
CHANGED
|
@@ -334,3 +334,74 @@ def draw_collision_circle(frame_rgb, sensor_pose7, gel_center_mm, cam_calib,
|
|
| 334 |
if 0 <= cx < w and 0 <= cy < h and rad_px < max(w, h):
|
| 335 |
cv2.circle(out, (cx, cy), int(round(rad_px)), color, 1, cv2.LINE_AA)
|
| 336 |
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 334 |
if 0 <= cx < w and 0 <= cy < h and rad_px < max(w, h):
|
| 335 |
cv2.circle(out, (cx, cy), int(round(rad_px)), color, 1, cv2.LINE_AA)
|
| 336 |
return out
|
| 337 |
+
|
| 338 |
+
def draw_world_gizmo(frame_rgb, cam_calib, corner="tl", size=44, margin=12,
|
| 339 |
+
labels=("x", "y", "z"), title=None):
|
| 340 |
+
"""World-frame orientation gizmo in a corner. Returns a copy.
|
| 341 |
+
|
| 342 |
+
Shows which way the WORLD axes point in this camera, the way a 3D viewport
|
| 343 |
+
corner axis does. Directions come from the rotation of `T_mocap_to_cam`
|
| 344 |
+
only — a gizmo is deliberately orthographic, because a perspective one
|
| 345 |
+
would change as you moved it around the image and stop being a legend.
|
| 346 |
+
|
| 347 |
+
THE PART A NAIVE VERSION GETS WRONG: these cameras look down at the table,
|
| 348 |
+
so world +z (up) points almost AT the camera and its screen projection is
|
| 349 |
+
nearly zero length. Drawn as a plain arrow it would read as "z does not
|
| 350 |
+
exist". So each axis also carries its out-of-plane sign — a filled dot for
|
| 351 |
+
pointing toward the viewer, a cross for away — the standard convention, and
|
| 352 |
+
the arrow length is the in-plane component only.
|
| 353 |
+
"""
|
| 354 |
+
import cv2
|
| 355 |
+
|
| 356 |
+
out = np.ascontiguousarray(frame_rgb).copy()
|
| 357 |
+
h, w = out.shape[:2]
|
| 358 |
+
T = np.asarray(cam_calib["T_mocap_to_cam"], float)[:3, :3]
|
| 359 |
+
# Inset by the FULL reach of the drawing, not by the arrow: the label sits
|
| 360 |
+
# 13 px past the tip and its glyph another ~9. The axis pointing straight
|
| 361 |
+
# up is the one that runs off the top edge, and that is the axis this
|
| 362 |
+
# gizmo exists to show.
|
| 363 |
+
reach = size + 22
|
| 364 |
+
ox = margin + reach if "l" in corner else w - margin - reach
|
| 365 |
+
oy = margin + reach if "t" in corner else h - margin - reach
|
| 366 |
+
# title BELOW the disc: at the top corner there is no room above it and the
|
| 367 |
+
# text clipped against the frame edge.
|
| 368 |
+
if title:
|
| 369 |
+
ty = oy + size + 22
|
| 370 |
+
for c, th in (((0, 0, 0), 3), ((215, 215, 215), 1)):
|
| 371 |
+
cv2.putText(out, title, (ox - size - 6, ty),
|
| 372 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.34, c, th, cv2.LINE_AA)
|
| 373 |
+
# a faint disc so the gizmo reads against any background
|
| 374 |
+
ov = out.copy()
|
| 375 |
+
cv2.circle(ov, (ox, oy), size + 8, (18, 22, 34), -1)
|
| 376 |
+
cv2.addWeighted(ov, 0.55, out, 0.45, 0, out)
|
| 377 |
+
cv2.circle(out, (ox, oy), size + 8, (70, 80, 100), 1, cv2.LINE_AA)
|
| 378 |
+
|
| 379 |
+
for i, col in enumerate(AXIS_BGR_RGB):
|
| 380 |
+
e = np.zeros(3); e[i] = 1.0
|
| 381 |
+
d = T @ e # world axis, in camera coordinates
|
| 382 |
+
# camera x right, y down, z into the scene
|
| 383 |
+
px, py, pz = float(d[0]), float(d[1]), float(d[2])
|
| 384 |
+
inplane = float(np.hypot(px, py))
|
| 385 |
+
tipx, tipy = int(round(ox + px * size)), int(round(oy + py * size))
|
| 386 |
+
if inplane > 0.12:
|
| 387 |
+
cv2.arrowedLine(out, (ox, oy), (tipx, tipy), col, 2, cv2.LINE_AA,
|
| 388 |
+
tipLength=0.28)
|
| 389 |
+
lx = int(round(ox + px * (size + 13)))
|
| 390 |
+
ly = int(round(oy + py * (size + 13)))
|
| 391 |
+
else:
|
| 392 |
+
lx, ly = ox + 14, oy - 14
|
| 393 |
+
# out-of-plane sign: toward the viewer is -z in camera coordinates
|
| 394 |
+
if abs(pz) > 0.55:
|
| 395 |
+
if pz < 0: # toward the viewer
|
| 396 |
+
cv2.circle(out, (ox, oy), 6, col, 2, cv2.LINE_AA)
|
| 397 |
+
cv2.circle(out, (ox, oy), 2, col, -1, cv2.LINE_AA)
|
| 398 |
+
else: # away from the viewer
|
| 399 |
+
cv2.circle(out, (ox, oy), 6, col, 2, cv2.LINE_AA)
|
| 400 |
+
r = 4
|
| 401 |
+
cv2.line(out, (ox-r, oy-r), (ox+r, oy+r), col, 1, cv2.LINE_AA)
|
| 402 |
+
cv2.line(out, (ox-r, oy+r), (ox+r, oy-r), col, 1, cv2.LINE_AA)
|
| 403 |
+
for c, th in (((0, 0, 0), 3), (col, 1)):
|
| 404 |
+
cv2.putText(out, labels[i], (lx - 4, ly + 4),
|
| 405 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.42, c, th, cv2.LINE_AA)
|
| 406 |
+
return out
|
| 407 |
+
|
toolbox/world_frame.py
CHANGED
|
@@ -69,7 +69,8 @@ def projection_fingerprint(pose7, gel_center_mm, cams) -> dict:
|
|
| 69 |
return out
|
| 70 |
|
| 71 |
|
| 72 |
-
def verify_world_frame(pose7, side: str, task_root, declaration
|
|
|
|
| 73 |
"""Worst per-camera pixel distance from the declared fingerprint.
|
| 74 |
|
| 75 |
WORST, not mean: a frame error along one camera's optical axis is
|
|
@@ -77,9 +78,17 @@ def verify_world_frame(pose7, side: str, task_root, declaration) -> float:
|
|
| 77 |
dilute exactly the evidence that matters.
|
| 78 |
|
| 79 |
`task_root` is the directory holding `calibration/` — the same argument
|
| 80 |
-
`load_calibration` takes.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
"""
|
| 82 |
from .calibration import load_calibration
|
|
|
|
| 83 |
|
| 84 |
if not declaration or "fingerprint" not in declaration:
|
| 85 |
raise ValueError("declaration has no fingerprint; this episode "
|
|
@@ -92,7 +101,8 @@ def verify_world_frame(pose7, side: str, task_root, declaration) -> float:
|
|
| 92 |
f"share the names left/right, so selecting by hand is easy to "
|
| 93 |
f"get wrong — an earlier version of this check did, and returned "
|
| 94 |
f"0.0 for every input as a result.)")
|
| 95 |
-
|
|
|
|
| 96 |
got = projection_fingerprint(pose7, cal[f"gel_{side}"], cal["cams"])
|
| 97 |
common = [v for v in VIEWS if v in got and v in stored]
|
| 98 |
if not common:
|
|
|
|
| 69 |
return out
|
| 70 |
|
| 71 |
|
| 72 |
+
def verify_world_frame(pose7, side: str, task_root, declaration, *,
|
| 73 |
+
up_axis: str | None = None) -> float:
|
| 74 |
"""Worst per-camera pixel distance from the declared fingerprint.
|
| 75 |
|
| 76 |
WORST, not mean: a frame error along one camera's optical axis is
|
|
|
|
| 78 |
dilute exactly the evidence that matters.
|
| 79 |
|
| 80 |
`task_root` is the directory holding `calibration/` — the same argument
|
| 81 |
+
`load_calibration` takes. Its calibration is converted to the convention
|
| 82 |
+
the DECLARATION names, so a working tree that still holds the recorded
|
| 83 |
+
Y-up extrinsics gives the same answer as the published Z-up ones. Pass
|
| 84 |
+
`up_axis` only to override that.
|
| 85 |
+
|
| 86 |
+
Poses in one convention with extrinsics in the other project 165 px away
|
| 87 |
+
and raise nothing, which is why this reads the convention instead of
|
| 88 |
+
assuming it.
|
| 89 |
"""
|
| 90 |
from .calibration import load_calibration
|
| 91 |
+
from .frames import as_up_axis
|
| 92 |
|
| 93 |
if not declaration or "fingerprint" not in declaration:
|
| 94 |
raise ValueError("declaration has no fingerprint; this episode "
|
|
|
|
| 101 |
f"share the names left/right, so selecting by hand is easy to "
|
| 102 |
f"get wrong — an earlier version of this check did, and returned "
|
| 103 |
f"0.0 for every input as a result.)")
|
| 104 |
+
want = up_axis or declaration.get("up_axis") or "y"
|
| 105 |
+
cal = as_up_axis(load_calibration(task_root), want)
|
| 106 |
got = projection_fingerprint(pose7, cal[f"gel_{side}"], cal["cams"])
|
| 107 |
common = [v for v in VIEWS if v in got and v in stored]
|
| 108 |
if not common:
|