toolbox: publish current viz.py (project_gel_frame was missing)
Browse files- toolbox/viz.py +181 -0
toolbox/viz.py
CHANGED
|
@@ -153,3 +153,184 @@ def draw_projection(frame_rgb, sensor_pose7, gel_center_mm, cam_calib,
|
|
| 153 |
cv2.FONT_HERSHEY_SIMPLEX, 0.42, (0, 200, 255), 1,
|
| 154 |
cv2.LINE_AA)
|
| 155 |
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
cv2.FONT_HERSHEY_SIMPLEX, 0.42, (0, 200, 255), 1,
|
| 154 |
cv2.LINE_AA)
|
| 155 |
return out
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
def draw_probe(frame_rgb, probe, gel_center_mm, cam_calib, color=None,
|
| 159 |
+
every: int = 6, label=True):
|
| 160 |
+
"""Overlay one synthetic probe's FUTURE trajectory on the start frame.
|
| 161 |
+
|
| 162 |
+
The whole evaluation protocol for these probes is "look at it": there is
|
| 163 |
+
no ground-truth future image, so a human compares the model's rollout
|
| 164 |
+
against where the sensor WOULD be. That comparison needs the ground truth
|
| 165 |
+
drawn in the same pixels, which is this.
|
| 166 |
+
|
| 167 |
+
Start is a filled dot, end is a ring, and the path between them is a
|
| 168 |
+
polyline sampled every `every` steps — dense enough to show curvature
|
| 169 |
+
under perspective, sparse enough that the workpiece stays visible.
|
| 170 |
+
|
| 171 |
+
Steps whose projection leaves the image are DROPPED from the polyline
|
| 172 |
+
rather than clamped to the border. A clamped point is a position the
|
| 173 |
+
sensor never occupies, and a line drawn to it says the trajectory went
|
| 174 |
+
somewhere it did not.
|
| 175 |
+
"""
|
| 176 |
+
import cv2
|
| 177 |
+
|
| 178 |
+
from .calibration import project_gel_to_pixel
|
| 179 |
+
|
| 180 |
+
out = np.ascontiguousarray(frame_rgb).copy()
|
| 181 |
+
h, w = out.shape[:2]
|
| 182 |
+
col = color or ((0, 220, 255) if probe.get("kind") == "translation"
|
| 183 |
+
else (255, 100, 220))
|
| 184 |
+
|
| 185 |
+
pts = []
|
| 186 |
+
for i, q in enumerate(np.asarray(probe["poses"], float)):
|
| 187 |
+
if i % every and i != len(probe["poses"]) - 1:
|
| 188 |
+
continue
|
| 189 |
+
uv = project_gel_to_pixel(q, gel_center_mm, cam_calib)
|
| 190 |
+
if uv is None or not (0 <= uv[0] < w and 0 <= uv[1] < h):
|
| 191 |
+
continue # dropped, never clamped
|
| 192 |
+
pts.append((int(round(uv[0])), int(round(uv[1]))))
|
| 193 |
+
|
| 194 |
+
for a, b in zip(pts, pts[1:]):
|
| 195 |
+
cv2.line(out, a, b, col, 1, cv2.LINE_AA)
|
| 196 |
+
if pts:
|
| 197 |
+
cv2.circle(out, pts[0], 4, (255, 255, 255), -1, cv2.LINE_AA)
|
| 198 |
+
cv2.circle(out, pts[0], 4, col, 1, cv2.LINE_AA)
|
| 199 |
+
cv2.circle(out, pts[-1], 6, col, 2, cv2.LINE_AA)
|
| 200 |
+
if label:
|
| 201 |
+
amp = (f"{probe['amplitude_m']*100:.0f}cm"
|
| 202 |
+
if probe.get("kind") == "translation"
|
| 203 |
+
else f"{probe['amplitude_deg']:.0f}deg")
|
| 204 |
+
cv2.putText(out, f"{probe['name']} {amp}",
|
| 205 |
+
(pts[-1][0] + 8, pts[-1][1] + 4),
|
| 206 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.38, col, 1, cv2.LINE_AA)
|
| 207 |
+
return out
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
def probe_contact_sheet(frame_rgb, probes, gel_center_mm, cam_calib,
|
| 211 |
+
cols: int = 4, tile=(320, 240)):
|
| 212 |
+
"""One tile per probe, plus the numbers a human needs to judge it.
|
| 213 |
+
|
| 214 |
+
A single frame with all twelve overlaid was the first version and it does
|
| 215 |
+
not work: every trajectory starts at the same pixel, so the labels stack
|
| 216 |
+
into an unreadable knot and no probe can be assessed on its own. The point
|
| 217 |
+
of this picture is per-probe comparison against a model rollout, and that
|
| 218 |
+
needs one probe per tile.
|
| 219 |
+
|
| 220 |
+
Each tile prints the amplitude, the horizon and the speed percentile,
|
| 221 |
+
because "is this like the dataset" is the first question a reader asks and
|
| 222 |
+
it should not require opening the metadata.
|
| 223 |
+
"""
|
| 224 |
+
import cv2
|
| 225 |
+
|
| 226 |
+
tw, th = tile
|
| 227 |
+
n = len(probes)
|
| 228 |
+
rows = int(np.ceil(n / cols))
|
| 229 |
+
sheet = np.zeros((rows * th, cols * tw, 3), np.uint8)
|
| 230 |
+
for i, p in enumerate(probes):
|
| 231 |
+
img = draw_probe(frame_rgb, p, gel_center_mm, cam_calib, label=False)
|
| 232 |
+
img = cv2.resize(img, (tw, th), interpolation=cv2.INTER_AREA)
|
| 233 |
+
amp = (f"{p['amplitude_m']*100:.0f}cm" if p["kind"] == "translation"
|
| 234 |
+
else f"{p['amplitude_deg']:.0f}deg")
|
| 235 |
+
tag = (f"{p['name']} {amp} {p['horizon_s']:.1f}s "
|
| 236 |
+
f"p{p['speed_percentile']:.0f}")
|
| 237 |
+
col = (0, 220, 255) if p["kind"] == "translation" else (255, 100, 220)
|
| 238 |
+
cv2.rectangle(img, (0, 0), (tw - 1, 14), (0, 0, 0), -1)
|
| 239 |
+
cv2.putText(img, tag, (3, 11), cv2.FONT_HERSHEY_SIMPLEX, 0.34, col, 1,
|
| 240 |
+
cv2.LINE_AA)
|
| 241 |
+
if not p.get("in_view", True):
|
| 242 |
+
cv2.rectangle(img, (0, 0), (tw - 1, th - 1), (0, 0, 255), 2)
|
| 243 |
+
cv2.putText(img, "LEAVES VIEW", (3, th - 6),
|
| 244 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.36, (0, 0, 255), 1,
|
| 245 |
+
cv2.LINE_AA)
|
| 246 |
+
r, c = divmod(i, cols)
|
| 247 |
+
sheet[r*th:(r+1)*th, c*tw:(c+1)*tw] = img
|
| 248 |
+
return sheet
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
AXIS_BGR_RGB = ((255, 60, 60), (60, 255, 60), (80, 160, 255)) # x, y, z
|
| 252 |
+
|
| 253 |
+
|
| 254 |
+
def draw_sensor_frame(frame_rgb, sensor_pose7, gel_center_mm, cam_calib,
|
| 255 |
+
axis_len_mm: float = 60.0, label=None, dim=False,
|
| 256 |
+
stem: bool = False):
|
| 257 |
+
"""Draw the sensor's gel centre and its three body axes, in perspective.
|
| 258 |
+
|
| 259 |
+
The axis tips are placed in 3D and projected, so the triad shrinks with
|
| 260 |
+
distance and foreshortens with orientation — the same convention the
|
| 261 |
+
React dataset previews use. A dot would show position and hide
|
| 262 |
+
orientation, which makes the six ROTATION probes unreadable: under a pure
|
| 263 |
+
rotation about the gel centre the dot does not move at all.
|
| 264 |
+
|
| 265 |
+
`dim` draws the held (non-moving) hand: same geometry, muted, so a viewer
|
| 266 |
+
can see BOTH hands and judge the collision clearance that the sampler
|
| 267 |
+
enforced numerically.
|
| 268 |
+
|
| 269 |
+
`stem` draws a line back to the OptiTrack marker cluster. WHY IT EXISTS:
|
| 270 |
+
the triad origin is the GEL CONTACT FACE, and the tool body extends 52 mm
|
| 271 |
+
behind it — a median 21 px on this rig, p90 28 px. The whole camera
|
| 272 |
+
calibration error budget is 4 px at 800 mm depth, so a viewer comparing
|
| 273 |
+
the marker against the middle of the visible tool sees a 20 px gap that is
|
| 274 |
+
geometry rather than error, and nothing on screen says which is which.
|
| 275 |
+
Measured on the release, the two gel centres agree to 1.1 mm against the
|
| 276 |
+
tracked board plane (95% CI [0.86, 1.39] over 21,337 contacts), so there
|
| 277 |
+
is no 20 px error to find. The stem says where the tip is.
|
| 278 |
+
"""
|
| 279 |
+
import cv2
|
| 280 |
+
|
| 281 |
+
from .calibration import project_gel_frame, project_rigid_origin
|
| 282 |
+
|
| 283 |
+
out = np.ascontiguousarray(frame_rgb).copy()
|
| 284 |
+
r = project_gel_frame(sensor_pose7, gel_center_mm, cam_calib, axis_len_mm)
|
| 285 |
+
if r is None:
|
| 286 |
+
return out
|
| 287 |
+
h, w = out.shape[:2]
|
| 288 |
+
cx, cy = int(round(r["centre"][0])), int(round(r["centre"][1]))
|
| 289 |
+
if not (0 <= cx < w and 0 <= cy < h):
|
| 290 |
+
return out
|
| 291 |
+
for tip, col in zip(r["tips"], AXIS_BGR_RGB):
|
| 292 |
+
if tip is None:
|
| 293 |
+
continue
|
| 294 |
+
tx, ty = int(round(tip[0])), int(round(tip[1]))
|
| 295 |
+
c = tuple(int(v * 0.45) for v in col) if dim else col
|
| 296 |
+
cv2.line(out, (cx, cy), (tx, ty), c, 1 if dim else 2, cv2.LINE_AA)
|
| 297 |
+
if stem:
|
| 298 |
+
o = project_rigid_origin(sensor_pose7, cam_calib)
|
| 299 |
+
if o is not None:
|
| 300 |
+
ox, oy = int(round(o[0])), int(round(o[1]))
|
| 301 |
+
if 0 <= ox < w and 0 <= oy < h:
|
| 302 |
+
sc = (90, 90, 90) if dim else (170, 170, 170)
|
| 303 |
+
cv2.line(out, (ox, oy), (cx, cy), sc, 1, cv2.LINE_AA)
|
| 304 |
+
cv2.circle(out, (ox, oy), 2, sc, 1, cv2.LINE_AA)
|
| 305 |
+
ring = (150, 150, 150) if dim else (255, 255, 255)
|
| 306 |
+
cv2.circle(out, (cx, cy), 3, ring, -1, cv2.LINE_AA)
|
| 307 |
+
if label:
|
| 308 |
+
cv2.putText(out, str(label), (cx + 7, cy - 7),
|
| 309 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.40, ring, 1, cv2.LINE_AA)
|
| 310 |
+
return out
|
| 311 |
+
|
| 312 |
+
|
| 313 |
+
def draw_collision_circle(frame_rgb, sensor_pose7, gel_center_mm, cam_calib,
|
| 314 |
+
diameter_m: float = 0.12, color=(255, 200, 80)):
|
| 315 |
+
"""The exclusion circle around a gel, drawn at its own depth.
|
| 316 |
+
|
| 317 |
+
Radius in PIXELS is derived from the radius in METRES at this gel's
|
| 318 |
+
depth — fx * r / z — so the circle shrinks with distance like everything
|
| 319 |
+
else. A fixed-pixel circle would claim a constant physical size at every
|
| 320 |
+
depth, which is the opposite of what it is for.
|
| 321 |
+
"""
|
| 322 |
+
import cv2
|
| 323 |
+
|
| 324 |
+
from .calibration import project_gel_frame, project_rigid_origin
|
| 325 |
+
|
| 326 |
+
out = np.ascontiguousarray(frame_rgb).copy()
|
| 327 |
+
r = project_gel_frame(sensor_pose7, gel_center_mm, cam_calib)
|
| 328 |
+
if r is None:
|
| 329 |
+
return out
|
| 330 |
+
K = cam_calib["intrinsics"]
|
| 331 |
+
rad_px = float(K["fx"] * (diameter_m / 2.0 * 1000.0) / max(r["depth_mm"], 1e-6))
|
| 332 |
+
cx, cy = int(round(r["centre"][0])), int(round(r["centre"][1]))
|
| 333 |
+
h, w = out.shape[:2]
|
| 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
|