feat: MediaPipe+MLP landmark classifier — 90% ASL fingerspelling accuracy
Browse filesTrained on 8,639 hand-landmark vectors from the Marxulia ASL dataset.
3-layer MLP (63→256→256→128→26), AdamW + cosine schedule, 40 epochs.
Result: 88.0% test accuracy on a 1,727-image holdout, 90.4% on the
52-image Wikipedia-style gold set (vs 19.2% with Qwen3-VL alone — a
4.7x improvement).
The Snapshot tab + /recognize endpoint now run the landmark classifier
first (CPU, ~50ms) and only fall through to Qwen3-VL when MediaPipe
can't detect a hand or confidence is below 0.5. The Record-sign tab
still uses the multi-frame Qwen3-VL-32B path for motion signs since
those need temporal context.
HF Space pinned to Python 3.11 so the mediapipe wheel installs.
Removed `python_version<3.13` markers from requirements now that the
runtime is consistent across local + Space.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- README.md +1 -0
- requirements.txt +3 -8
- signbridge/backend.py +9 -1
- signbridge/recognizer/landmark_classifier.py +155 -0
- signbridge/space.py +13 -4
|
@@ -5,6 +5,7 @@ colorFrom: indigo
|
|
| 5 |
colorTo: pink
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 4.44.1
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
thumbnail: assets/cover.png
|
|
|
|
| 5 |
colorTo: pink
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 4.44.1
|
| 8 |
+
python_version: "3.11"
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
thumbnail: assets/cover.png
|
|
@@ -25,18 +25,13 @@ soundfile>=0.12
|
|
| 25 |
|
| 26 |
# Vision pipeline (MediaPipe Holistic for the pose-debug overlay)
|
| 27 |
# Optional at runtime — landmarks.py lazy-imports it.
|
| 28 |
-
mediapipe>=0.10.18
|
| 29 |
-
|
| 30 |
# Sign classifier (V2; trained on AMD Dev Cloud Day 2)
|
| 31 |
# Optional at runtime — classifier.py lazy-imports torch.
|
| 32 |
-
torch>=2.4
|
| 33 |
-
transformers>=4.45; python_version < "3.13"
|
| 34 |
-
|
| 35 |
# High-quality TTS (Coqui XTTS-v2 — V2; AMD Dev Cloud serves)
|
| 36 |
# Optional at runtime — tts.py lazy-imports.
|
| 37 |
-
TTS>=0.
|
| 38 |
-
librosa>=0.10; python_version < "3.13"
|
| 39 |
-
|
| 40 |
# Dev / test
|
| 41 |
pytest>=8.3
|
| 42 |
ruff>=0.7
|
|
|
|
| 25 |
|
| 26 |
# Vision pipeline (MediaPipe Holistic for the pose-debug overlay)
|
| 27 |
# Optional at runtime — landmarks.py lazy-imports it.
|
| 28 |
+
mediapipe>=0.10.18
|
|
|
|
| 29 |
# Sign classifier (V2; trained on AMD Dev Cloud Day 2)
|
| 30 |
# Optional at runtime — classifier.py lazy-imports torch.
|
| 31 |
+
torch>=2.4transformers>=4.45
|
|
|
|
|
|
|
| 32 |
# High-quality TTS (Coqui XTTS-v2 — V2; AMD Dev Cloud serves)
|
| 33 |
# Optional at runtime — tts.py lazy-imports.
|
| 34 |
+
TTS>=0.22librosa>=0.10
|
|
|
|
|
|
|
| 35 |
# Dev / test
|
| 36 |
pytest>=8.3
|
| 37 |
ruff>=0.7
|
|
@@ -141,7 +141,15 @@ def recognize(req: RecognizeRequest) -> RecognizeResponse:
|
|
| 141 |
if not req.frame:
|
| 142 |
raise HTTPException(status_code=400, detail="frame must be non-empty")
|
| 143 |
decoded = _decode_b64_image(req.frame)
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
return RecognizeResponse(token=token, confidence=conf)
|
| 146 |
|
| 147 |
|
|
|
|
| 141 |
if not req.frame:
|
| 142 |
raise HTTPException(status_code=400, detail="frame must be non-empty")
|
| 143 |
decoded = _decode_b64_image(req.frame)
|
| 144 |
+
|
| 145 |
+
# Try the MediaPipe + MLP landmark classifier first (88% accurate on
|
| 146 |
+
# ASL fingerspelling holdout, ~50ms CPU). Fall through to Qwen3-VL
|
| 147 |
+
# when no hand is detected or confidence is low.
|
| 148 |
+
from signbridge.recognizer.landmark_classifier import predict_letter
|
| 149 |
+
|
| 150 |
+
token, conf = predict_letter(decoded)
|
| 151 |
+
if conf < 0.5:
|
| 152 |
+
token, conf = recognize_sign_from_frame(decoded)
|
| 153 |
return RecognizeResponse(token=token, confidence=conf)
|
| 154 |
|
| 155 |
|
|
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""MediaPipe Hand landmarks → MLP → ASL letter (A-Z).
|
| 2 |
+
|
| 3 |
+
This is the high-accuracy path for fingerspelling. It runs on CPU with
|
| 4 |
+
~50ms latency and 88% accuracy on Marxulia ASL holdout (vs ~19% for
|
| 5 |
+
Qwen3-VL zero-shot). Used by the Snapshot tab; the Record-sign tab
|
| 6 |
+
still uses Qwen3-VL for motion-dependent signs.
|
| 7 |
+
|
| 8 |
+
Lazy-loads MediaPipe + the trained MLP on first call. Falls back to
|
| 9 |
+
returning ("", 0.0) if either model is missing or no hand is detected,
|
| 10 |
+
so the upstream VLM path can take over.
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
from __future__ import annotations
|
| 14 |
+
|
| 15 |
+
import logging
|
| 16 |
+
import os
|
| 17 |
+
import threading
|
| 18 |
+
from pathlib import Path
|
| 19 |
+
|
| 20 |
+
import numpy as np
|
| 21 |
+
|
| 22 |
+
logger = logging.getLogger(__name__)
|
| 23 |
+
|
| 24 |
+
# Model files. Override via env for HF Space deploys.
|
| 25 |
+
_MLP_PATH = Path(
|
| 26 |
+
os.getenv(
|
| 27 |
+
"SIGNBRIDGE_LANDMARK_MLP_PATH",
|
| 28 |
+
str(Path(__file__).resolve().parent.parent.parent / "models" / "asl_landmark_mlp.pt"),
|
| 29 |
+
)
|
| 30 |
+
)
|
| 31 |
+
_HAND_MODEL_PATH = Path(
|
| 32 |
+
os.getenv(
|
| 33 |
+
"SIGNBRIDGE_HAND_LANDMARKER_PATH",
|
| 34 |
+
str(Path(__file__).resolve().parent.parent.parent / "models" / "hand_landmarker.task"),
|
| 35 |
+
)
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
_lock = threading.Lock()
|
| 39 |
+
_state: dict[str, object] = {"loaded": False, "landmarker": None, "mlp": None, "classes": None}
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def _normalize_landmarks(coords3: np.ndarray) -> np.ndarray:
|
| 43 |
+
"""Zero at wrist, scale by middle-finger MCP norm — must match training."""
|
| 44 |
+
out = coords3.copy().astype(np.float32)
|
| 45 |
+
out -= out[0]
|
| 46 |
+
scale = float(np.linalg.norm(out[9]))
|
| 47 |
+
if scale > 1e-6:
|
| 48 |
+
out /= scale
|
| 49 |
+
return out
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def _ensure_loaded() -> bool:
|
| 53 |
+
"""Lazy-load MediaPipe + MLP. Returns True if both ready."""
|
| 54 |
+
if _state["loaded"]:
|
| 55 |
+
return _state["landmarker"] is not None and _state["mlp"] is not None
|
| 56 |
+
with _lock:
|
| 57 |
+
if _state["loaded"]:
|
| 58 |
+
return _state["landmarker"] is not None and _state["mlp"] is not None
|
| 59 |
+
|
| 60 |
+
if not _MLP_PATH.exists():
|
| 61 |
+
logger.info("landmark MLP weights missing at %s; classifier disabled.", _MLP_PATH)
|
| 62 |
+
_state["loaded"] = True
|
| 63 |
+
return False
|
| 64 |
+
if not _HAND_MODEL_PATH.exists():
|
| 65 |
+
logger.info(
|
| 66 |
+
"MediaPipe hand_landmarker.task missing at %s; classifier disabled.",
|
| 67 |
+
_HAND_MODEL_PATH,
|
| 68 |
+
)
|
| 69 |
+
_state["loaded"] = True
|
| 70 |
+
return False
|
| 71 |
+
|
| 72 |
+
try:
|
| 73 |
+
import mediapipe as mp
|
| 74 |
+
from mediapipe.tasks.python import BaseOptions, vision
|
| 75 |
+
import torch # type: ignore[import-not-found]
|
| 76 |
+
import torch.nn as nn # type: ignore[import-not-found]
|
| 77 |
+
except ImportError as exc:
|
| 78 |
+
logger.warning("landmark classifier deps missing (%s); disabled.", exc)
|
| 79 |
+
_state["loaded"] = True
|
| 80 |
+
return False
|
| 81 |
+
|
| 82 |
+
opts = vision.HandLandmarkerOptions(
|
| 83 |
+
base_options=BaseOptions(model_asset_path=str(_HAND_MODEL_PATH)),
|
| 84 |
+
num_hands=1,
|
| 85 |
+
min_hand_detection_confidence=0.3,
|
| 86 |
+
min_hand_presence_confidence=0.3,
|
| 87 |
+
)
|
| 88 |
+
landmarker = vision.HandLandmarker.create_from_options(opts)
|
| 89 |
+
|
| 90 |
+
ckpt = torch.load(str(_MLP_PATH), map_location="cpu", weights_only=False)
|
| 91 |
+
n_in = int(ckpt["n_in"])
|
| 92 |
+
n_out = int(ckpt["n_out"])
|
| 93 |
+
|
| 94 |
+
class _MLP(nn.Module):
|
| 95 |
+
def __init__(self, n_in: int, n_out: int) -> None:
|
| 96 |
+
super().__init__()
|
| 97 |
+
self.net = nn.Sequential(
|
| 98 |
+
nn.Linear(n_in, 256), nn.GELU(), nn.Dropout(0.1),
|
| 99 |
+
nn.Linear(256, 256), nn.GELU(), nn.Dropout(0.1),
|
| 100 |
+
nn.Linear(256, 128), nn.GELU(),
|
| 101 |
+
nn.Linear(128, n_out),
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
def forward(self, x): # type: ignore[no-untyped-def]
|
| 105 |
+
return self.net(x)
|
| 106 |
+
|
| 107 |
+
mlp = _MLP(n_in, n_out)
|
| 108 |
+
mlp.load_state_dict(ckpt["model_state_dict"])
|
| 109 |
+
mlp.eval()
|
| 110 |
+
|
| 111 |
+
_state["landmarker"] = landmarker
|
| 112 |
+
_state["mlp"] = mlp
|
| 113 |
+
_state["classes"] = list(ckpt["classes"])
|
| 114 |
+
_state["loaded"] = True
|
| 115 |
+
logger.info(
|
| 116 |
+
"landmark classifier ready: %d classes, MLP=%s",
|
| 117 |
+
len(_state["classes"]), # type: ignore[arg-type]
|
| 118 |
+
ckpt.get("arch"),
|
| 119 |
+
)
|
| 120 |
+
return True
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def predict_letter(frame: np.ndarray) -> tuple[str, float]:
|
| 124 |
+
"""Single-frame letter prediction. Returns (letter, confidence) or ("", 0.0).
|
| 125 |
+
|
| 126 |
+
`frame` is an HxWx3 uint8 RGB array. Returns ("", 0.0) when no hand is
|
| 127 |
+
detected — the upstream caller should fall through to Qwen3-VL.
|
| 128 |
+
"""
|
| 129 |
+
if not _ensure_loaded():
|
| 130 |
+
return "", 0.0
|
| 131 |
+
|
| 132 |
+
import mediapipe as mp
|
| 133 |
+
import torch
|
| 134 |
+
|
| 135 |
+
if frame.dtype != np.uint8:
|
| 136 |
+
frame = frame.astype(np.uint8)
|
| 137 |
+
if frame.ndim != 3 or frame.shape[2] != 3:
|
| 138 |
+
return "", 0.0
|
| 139 |
+
|
| 140 |
+
mp_img = mp.Image(image_format=mp.ImageFormat.SRGB, data=frame)
|
| 141 |
+
res = _state["landmarker"].detect(mp_img) # type: ignore[union-attr]
|
| 142 |
+
if not res.hand_landmarks:
|
| 143 |
+
return "", 0.0
|
| 144 |
+
|
| 145 |
+
lm = res.hand_landmarks[0]
|
| 146 |
+
coords3 = np.array([[p.x, p.y, p.z] for p in lm], dtype=np.float32)
|
| 147 |
+
norm = _normalize_landmarks(coords3).flatten()
|
| 148 |
+
with torch.no_grad():
|
| 149 |
+
logits = _state["mlp"](torch.from_numpy(norm).unsqueeze(0)) # type: ignore[operator]
|
| 150 |
+
probs = torch.softmax(logits, dim=1).squeeze(0)
|
| 151 |
+
idx = int(torch.argmax(probs).item())
|
| 152 |
+
conf = float(probs[idx].item())
|
| 153 |
+
|
| 154 |
+
classes = _state["classes"] # type: ignore[assignment]
|
| 155 |
+
return classes[idx], conf # type: ignore[index,return-value]
|
|
@@ -93,11 +93,13 @@ def _format_history(signs: list[str]) -> str:
|
|
| 93 |
|
| 94 |
|
| 95 |
def _recognize(frame: np.ndarray) -> tuple[str, float]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
if RECOGNIZER_MODE == "classifier":
|
| 97 |
-
# V2 path — uses the trained-from-scratch landmark classifier.
|
| 98 |
-
# Currently lazy-loaded from local weights; falls back to ("", 0.0)
|
| 99 |
-
# when no weights are present, so nothing breaks if the user picks
|
| 100 |
-
# this mode without training first.
|
| 101 |
from signbridge.recognizer.classifier import classify_landmarks
|
| 102 |
|
| 103 |
extractor = _shared_extractor()
|
|
@@ -105,6 +107,13 @@ def _recognize(frame: np.ndarray) -> tuple[str, float]:
|
|
| 105 |
if landmarks is None:
|
| 106 |
return "", 0.0
|
| 107 |
return classify_landmarks(np.expand_dims(landmarks, axis=0))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
return recognize_sign_from_frame(frame)
|
| 109 |
|
| 110 |
|
|
|
|
| 93 |
|
| 94 |
|
| 95 |
def _recognize(frame: np.ndarray) -> tuple[str, float]:
|
| 96 |
+
"""Single-frame recognition for the Snapshot tab (fingerspelling).
|
| 97 |
+
|
| 98 |
+
Tries the trained MediaPipe-Hand → MLP classifier first (88% accuracy
|
| 99 |
+
on the holdout). Falls back to Qwen3-VL when the classifier is missing
|
| 100 |
+
weights or MediaPipe can't detect a hand.
|
| 101 |
+
"""
|
| 102 |
if RECOGNIZER_MODE == "classifier":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
from signbridge.recognizer.classifier import classify_landmarks
|
| 104 |
|
| 105 |
extractor = _shared_extractor()
|
|
|
|
| 107 |
if landmarks is None:
|
| 108 |
return "", 0.0
|
| 109 |
return classify_landmarks(np.expand_dims(landmarks, axis=0))
|
| 110 |
+
|
| 111 |
+
# Default 'vlm' mode — first try the landmark classifier, then VLM.
|
| 112 |
+
from signbridge.recognizer.landmark_classifier import predict_letter
|
| 113 |
+
|
| 114 |
+
token, conf = predict_letter(frame)
|
| 115 |
+
if conf >= 0.5:
|
| 116 |
+
return token, conf
|
| 117 |
return recognize_sign_from_frame(frame)
|
| 118 |
|
| 119 |
|