refactor(grasp): extract _points_via_bbox helper

Pure refactor in preparation for the SAM21 branch. _object_points_in_base
keeps its signature and the bbox→ROI→base-frame logic moves into the
private helper. Meta now includes source='bbox_roi'.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Adriano Dal PastroandClaude Opus 4.7 committed 2026-05-11 14:46:47 +02:00
1 parent e3f6ff2908
commit 2f76df4c06
1 file changed
+20 -4
+20 -4
View File
@@ -65,9 +65,26 @@ def _object_points_in_base(conf: float | None, class_name: str | None, sw: Stopw
raise HTTPException(status_code=404, detail="no object detected") raise HTTPException(status_code=404, detail="no object detected")
dets.sort(key=lambda d: d["confidence"], reverse=True) dets.sort(key=lambda d: d["confidence"], reverse=True)
top = dets[0] top = dets[0]
b = top["bbox"] bbox = _bbox_to_roi(top["bbox"])
u_min, v_min, u_max, v_max = _bbox_to_roi(b) pts_base = _points_via_bbox(nuwa, T_base_cam, bbox, sw)
meta = {
"detection": top,
"T_base_camera": T_base_cam.tolist(),
"n_points": int(len(pts_base)),
"source": "bbox_roi",
}
return pts_base, meta
def _points_via_bbox(nuwa, T_base_cam: np.ndarray, bbox: tuple[int, int, int, int],
sw: Stopwatch) -> np.ndarray:
"""Legacy path: bbox → Nuwa ROI cloud → filter outliers → base frame.
Raises HTTPException(422) on <10 valid depth points (existing behaviour).
"""
cfg = get_config()
u_min, v_min, u_max, v_max = bbox
try: try:
with sw.step("pointcloud_roi"): with sw.step("pointcloud_roi"):
pc, _meta = nuwa.pointcloud_roi( pc, _meta = nuwa.pointcloud_roi(
@@ -81,11 +98,10 @@ def _object_points_in_base(conf: float | None, class_name: str | None, sw: Stopw
cropped = filter_outliers(pc) cropped = filter_outliers(pc)
if len(cropped) < 10: if len(cropped) < 10:
raise HTTPException(status_code=422, detail=f"too few valid depth points in bbox: {len(cropped)}") raise HTTPException(status_code=422, detail=f"too few valid depth points in bbox: {len(cropped)}")
with sw.step("to_base_frame"): with sw.step("to_base_frame"):
pts_cv = nuwa_to_cv_frame(cropped[:, :3]) pts_cv = nuwa_to_cv_frame(cropped[:, :3])
pts_base = transform_points(T_base_cam, pts_cv) pts_base = transform_points(T_base_cam, pts_cv)
return pts_base, {"detection": top, "T_base_camera": T_base_cam.tolist(), "n_points": len(pts_base)} return pts_base
@router.post("/plan") @router.post("/plan")