Spaces:
Running
Running
Upload folder using huggingface_hub
Browse filesFiles changed (3)
367 unmodified lines368369370371372373127 unmodified lines50150250350450550650750850951051151251316 unmodified lines530531532533534535536367 unmodified linesprecise-rotation) frame; pass ``rotation_matrix`` to align with an imagethat had the finger rotation applied."""if not seed_debug:return imagefrom src.geometry import transform_points_rotation127 unmodified lineshand_mask = hand_data.get("mask")landmarks = hand_data.get("landmarks")if hand_mask is None or landmarks is None or len(landmarks) <= 10:return Nonemiddle_pip = landmarks[10, :2]anchor_xy = (int(round(middle_pip[0])), int(round(middle_pip[1])))seed_info = suggest_card_seeds(hand_mask, image_canonical.shape[:2], anchor_xy)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),367 unmodified lines368369370371372373374375376377127 unmodified lines50550650750850951051151251351451551651716 unmodified lines534535536537538539367 unmodified linesprecise-rotation) frame; pass ``rotation_matrix`` to align with an imagethat had the finger rotation applied."""# temprary bypass by Fengreturn imageif not seed_debug:return imagefrom src.geometry import transform_points_rotation127 unmodified lineshand_mask = hand_data.get("mask")landmarks = hand_data.get("landmarks")if hand_mask is None or landmarks is None or len(landmarks) <= 9:return Nonemiddle_mcp = landmarks[9, :2]y_limit = int(round(middle_mcp[1]))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 = {"seeds": list(seeds),"dropped": list(dropped_seeds),"negatives": list(negatives),
77 unmodified lines787980818283848586878877 unmodified linesif 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:return recmp = 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)seeds = seed_info["kept"]rec["prompt_n_seeds"] = len(seeds)negs = _negatives_from_landmarks(hand_data["landmarks"])77 unmodified lines787980818283848586878877 unmodified linesif 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) <= 9:return recmiddle_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 lines139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171138 unmodified linesdef suggest_card_seeds(hand_mask: np.ndarray,image_shape: Tuple[int, int],anchor_xy: Tuple[int, int],) -> Dict[str, List[Tuple[int, int]]]:"""Cross-shaped seed points through the anchor (middle-finger PIP).Users place the credit card either above/below or left/right of themiddle-finger PIP, so a rake along the horizontal and vertical linesthrough PIP catches it with far fewer prompts than a dense grid.5 points per arm (step 0.15·dim), shared center deduped.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 andwere filtered out. Retained purely for debug visualization."""h, w = image_shapemask_bool = hand_mask.astype(bool) if hand_mask.dtype != bool else hand_maskax = int(round(anchor_xy[0]))ay = int(round(anchor_xy[1]))dx = int(round(0.15 * w))dy = int(round(0.15 * h))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))kept: List[Tuple[int, int]] = []dropped: List[Tuple[int, int]] = []138 unmodified lines139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188138 unmodified linesdef suggest_card_seeds(hand_mask: np.ndarray,image_shape: Tuple[int, int],y_limit: int,) -> Dict[str, List[Tuple[int, int]]]:"""Uniform 4x4 grid seeds in the top band of the canonical image.In canonical orientation the fingertips point up, so the middle-fingerMCP is the lowest landmark of the middle finger. Users overwhelminglyplace the card in the band between the top of the frame and the MCProw (beside or above the fingers), so sampling that band catches thecard with a handful of prompts. The grid excludes a 10% border paddingon all four sides of the band and drops any seed that lands on thehand 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: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 andwere filtered out. Retained purely for debug visualization."""h, w = image_shapemask_bool = hand_mask.astype(bool) if hand_mask.dtype != bool else hand_maskx_min = 0.1 * wx_max = 0.9 * wy_min = 0.1 * hy_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.0n = 4candidates: List[Tuple[int, int]] = []for iy in range(n):fy = (iy + 0.5) / npy = int(round(y_min + fy * (y_max - y_min)))for ix in range(n):fx = (ix + 0.5) / npx = 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