Spaces:
Running
Running
Upload folder using huggingface_hub
Browse filesFiles changed (3)
367 unmodified lines368369370371372373374375376377127 unmodified lines50550650750450850951050750851151251351051451551651716 unmodified lines534535536533537538539367 unmodified lines precise-rotation) frame; pass ``rotation_matrix`` to align with an image that had the finger rotation applied. """
# temprary bypass by Feng return image
if not seed_debug: return image from src.geometry import transform_points_rotation127 unmodified lines hand_mask = hand_data.get("mask") landmarks = hand_data.get("landmarks")
if hand_mask is None or landmarks is None or len(landmarks) <= 10: if hand_mask is None or landmarks is None or len(landmarks) <= 9: return None
middle_pip = landmarks[10, :2] anchor_xy = (int(round(middle_pip[0])), int(round(middle_pip[1]))) middle_mcp = landmarks[9, :2] y_limit = int(round(middle_mcp[1]))
seed_info = suggest_card_seeds(hand_mask, image_canonical.shape[:2], anchor_xy) seed_info = suggest_card_seeds(hand_mask, image_canonical.shape[:2], y_limit) seeds = seed_info["kept"] dropped_seeds = seed_info["dropped"] if not seeds:16 unmodified lines # Stash seed geometry so the final result PNG can visualize what was # prompted into SAM, even when card detection fails. seed_debug = { "anchor": anchor_xy, "seeds": list(seeds), "dropped": list(dropped_seeds), "negatives": list(negatives),77 unmodified lines78798081818283848583848586878877 unmodified lines if hand_data is not None: prompt_debug = OUT_DIR / img_path.stem / "sam_card_prompt" landmarks = hand_data.get("landmarks") if landmarks is None or len(landmarks) <= 10: if landmarks is None or len(landmarks) <= 9: return rec mp = landmarks[10, :2] anchor_xy = (int(round(mp[0])), int(round(mp[1]))) seed_info = suggest_card_seeds(hand_data["mask"], canonical.shape[:2], anchor_xy) middle_mcp = landmarks[9, :2] y_limit = int(round(middle_mcp[1])) seed_info = suggest_card_seeds(hand_data["mask"], canonical.shape[:2], y_limit) seeds = seed_info["kept"] rec["prompt_n_seeds"] = len(seeds) negs = _negatives_from_landmarks(hand_data["landmarks"])138 unmodified lines139140141142142143144144145146147148149146147148149150151152153154155156157158159160151152153154161162163164165166167168159160161162169170171172173174175176177178165166167168179180181182183184185186187188138 unmodified linesdef suggest_card_seeds( hand_mask: np.ndarray, image_shape: Tuple[int, int], anchor_xy: Tuple[int, int], y_limit: int,) -> Dict[str, List[Tuple[int, int]]]: """Cross-shaped seed points through the anchor (middle-finger PIP). """Uniform 4x4 grid seeds in the top band of the canonical image.
Users place the credit card either above/below or left/right of the middle-finger PIP, so a rake along the horizontal and vertical lines through PIP catches it with far fewer prompts than a dense grid. 5 points per arm (step 0.15·dim), shared center deduped. In canonical orientation the fingertips point up, so the middle-finger MCP is the lowest landmark of the middle finger. Users overwhelmingly place the card in the band between the top of the frame and the MCP row (beside or above the fingers), so sampling that band catches the card with a handful of prompts. The grid excludes a 10% border padding on all four sides of the band and drops any seed that lands on the hand mask.
Args: hand_mask: SAM hand mask (HxW, bool or uint8). image_shape: (H, W) of the canonical image. y_limit: Y coordinate of the middle-finger MCP; the grid spans [0.1·H, y_limit] vertically.
Returns: Returns a dict with two lists: - "kept": seeds that passed the hand-mask filter (sent to SAM). - "dropped": seeds whose (x, y) landed inside the hand mask and were filtered out. Retained purely for debug visualization. Dict with two lists: - "kept": seeds that passed the hand-mask filter (sent to SAM). - "dropped": seeds whose (x, y) landed inside the hand mask and were filtered out. Retained purely for debug visualization. """ h, w = image_shape mask_bool = hand_mask.astype(bool) if hand_mask.dtype != bool else hand_mask
ax = int(round(anchor_xy[0])) ay = int(round(anchor_xy[1])) dx = int(round(0.15 * w)) dy = int(round(0.15 * h)) x_min = 0.1 * w x_max = 0.9 * w y_min = 0.1 * h y_max = float(y_limit) # Guard against degenerate bands (e.g., MCP above the 10% top padding). if y_max <= y_min: y_max = y_min + 1.0
n = 4 candidates: List[Tuple[int, int]] = [] for k in (-2, -1, 0, 1, 2): candidates.append((ax + k * dx, ay)) for k in (-2, -1, 1, 2): candidates.append((ax, ay + k * dy)) for iy in range(n): fy = (iy + 0.5) / n py = int(round(y_min + fy * (y_max - y_min))) for ix in range(n): fx = (ix + 0.5) / n px = int(round(x_min + fx * (x_max - x_min))) candidates.append((px, py))
kept: List[Tuple[int, int]] = [] dropped: List[Tuple[int, int]] = []measure_finger.py+8-5
script/validate_sam_card.py+4-4
src/sam_card_detection.py+35-18