feat(right_arm): /detect/wrist con YOLO + SAM21 + centroide camera
This commit is contained in:
1 parent
3e4e8055db
commit
bd4eb8b929
2 files changed
+111
No files matched your search
@@ -8,11 +8,15 @@ from fastapi import APIRouter, HTTPException
|
||||
from fastapi.responses import Response
|
||||
from pydantic import BaseModel
|
||||
|
||||
import numpy as np
|
||||
|
||||
from ..clients import G1Unavailable
|
||||
from ..config import update_config
|
||||
from ..geometry import quaternion_xyzw_to_rpy_deg
|
||||
from ..perception.sam21_bridge import Sam21Fallback, segment_with_sam21
|
||||
from ..timing import Stopwatch
|
||||
from . import _deps
|
||||
from .perception_routes import _bbox_to_roi
|
||||
|
||||
router = APIRouter()
|
||||
log = logging.getLogger(__name__)
|
||||
@@ -137,3 +141,49 @@ def detect_front(req: DetectFrontReq) -> dict[str, Any]:
|
||||
"image_size": [int(intr.get("width", 0)), int(intr.get("height", 0))],
|
||||
"timings_ms": sw.as_dict(),
|
||||
}
|
||||
|
||||
|
||||
class DetectWristReq(BaseModel):
|
||||
class_name: Optional[str] = None
|
||||
conf: Optional[float] = None
|
||||
|
||||
|
||||
def _detect_wrist_core(class_name: str | None, conf: float | None, sw: Stopwatch
|
||||
) -> dict[str, Any]:
|
||||
"""Funzione core riusata da /detect/wrist e da /move/toward."""
|
||||
nuwa = _deps.nuwa()
|
||||
image_url = nuwa.rgb_url()
|
||||
try:
|
||||
with sw.step("yolo"):
|
||||
yres = _deps.yolo().detect_url(image_url=image_url, conf=conf)
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=502, detail=f"YOLO unreachable: {e}")
|
||||
top = _top_detection(yres, class_name)
|
||||
if top is None:
|
||||
raise HTTPException(status_code=404, detail=f"no '{class_name or 'object'}' detected")
|
||||
bbox = _bbox_to_roi(top["bbox"])
|
||||
try:
|
||||
seg = segment_with_sam21(nuwa, _deps.sam21(), bbox, sw)
|
||||
except Sam21Fallback as e:
|
||||
raise HTTPException(status_code=422, detail=f"SAM21 fallback: {e}")
|
||||
pts = np.asarray(seg.points_cv_m, dtype=np.float64)
|
||||
if pts.shape[0] < 10:
|
||||
raise HTTPException(status_code=422, detail=f"too few sam21 points: {pts.shape[0]}")
|
||||
with sw.step("centroid"):
|
||||
centroid = pts.mean(axis=0)
|
||||
distance = float(np.linalg.norm(centroid))
|
||||
return {
|
||||
"detection": top,
|
||||
"bbox": list(bbox),
|
||||
"centroid_camera_m": [float(centroid[0]), float(centroid[1]), float(centroid[2])],
|
||||
"distance_m": distance,
|
||||
"n_sam21_points": int(pts.shape[0]),
|
||||
}
|
||||
|
||||
|
||||
@router.post("/detect/wrist")
|
||||
def detect_wrist(req: DetectWristReq) -> dict[str, Any]:
|
||||
sw = Stopwatch()
|
||||
body = _detect_wrist_core(req.class_name, req.conf, sw)
|
||||
body["timings_ms"] = sw.as_dict()
|
||||
return body
|
||||
Reference in new issue
Block a user