CoolFace
Apppublic

Piyush23890/Sign_Language_Decoder

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes
hand_landmarks_dataset.py112 linesDownload Raw Back to root
1"""2hand_landmarks_dataset.py3=========================4Collect static ISL alphabet landmark data via webcam.5 6Usage7-----8    python hand_landmarks_dataset.py --sign A --samples 2009 10Every CAPTURE_DELAY seconds a 126-feature row (21 landmarks × 3 coords × 2 hands)11is auto-written to  dataset/<SIGN>/data.csv.12 13Press ESC to stop early.14"""15 16import argparse17import csv18import os19import time20 21import cv222import mediapipe as mp23 24# ── CLI args ────────────────────────────────────────────────────────────────────25parser = argparse.ArgumentParser(description="Collect static ISL hand-landmark data")26parser.add_argument("--sign",    default="A",      help="ISL sign label (A–Z)")27parser.add_argument("--samples", default=200, type=int, help="Number of samples to capture")28parser.add_argument("--delay",   default=0.3, type=float,29                    help="Seconds between auto-captures")30args = parser.parse_args()31 32SIGN_NAME    = args.sign.upper()33TARGET       = args.samples34CAPTURE_DELAY= args.delay35DATASET_DIR  = "dataset"36 37# ── MediaPipe ───────────────────────────────────────────────────────────────────38mp_hands = mp.solutions.hands39mp_draw  = mp.solutions.drawing_utils40 41hands = mp_hands.Hands(42    static_image_mode=False,43    max_num_hands=2,44    min_detection_confidence=0.70,45    min_tracking_confidence=0.70,46)47 48# ── CSV setup ───────────────────────────────────────────────────────────────────49sign_dir  = os.path.join(DATASET_DIR, SIGN_NAME)50os.makedirs(sign_dir, exist_ok=True)51csv_path  = os.path.join(sign_dir, "data.csv")52file_new  = not os.path.isfile(csv_path)53 54cap     = cv2.VideoCapture(0)55captured= 056last_t  = time.time()57 58print(f"[DataCollect] Sign='{SIGN_NAME}'  Target={TARGET}  Delay={CAPTURE_DELAY}s")59print("[DataCollect] Press ESC to stop early.")60 61with open(csv_path, mode="a", newline="") as f:62    writer = csv.writer(f)63 64    # Header (written once)65    if file_new:66        header = []67        for hand in ["L", "R"]:68            for i in range(21):69                header.extend([f"{hand}_{i}_x", f"{hand}_{i}_y", f"{hand}_{i}_z"])70        writer.writerow(header)71 72    while cap.isOpened() and captured < TARGET:73        ok, frame = cap.read()74        if not ok:75            break76 77        frame = cv2.flip(frame, 1)78        rgb   = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)79        res   = hands.process(rgb)80 81        row = []82        if res.multi_hand_landmarks:83            for hl in res.multi_hand_landmarks:84                mp_draw.draw_landmarks(frame, hl, mp_hands.HAND_CONNECTIONS)85                for lm in hl.landmark:86                    row.extend([lm.x, lm.y, lm.z])87 88            # Pad / truncate to exactly 12689            while len(row) < 126:90                row.extend([0.0, 0.0, 0.0])91            row = row[:126]92 93            now = time.time()94            if now - last_t >= CAPTURE_DELAY:95                writer.writerow(row)96                captured += 197                last_t = now98                print(f"  [{captured:>4}/{TARGET}] captured", end="\r")99 100        # HUD101        cv2.putText(frame,102                    f"Sign: {SIGN_NAME}  Captured: {captured}/{TARGET}",103                    (10, 35), cv2.FONT_HERSHEY_SIMPLEX, 0.85, (0, 255, 0), 2)104        cv2.imshow("ISL Data Collection", frame)105 106        if cv2.waitKey(1) & 0xFF == 27:   # ESC107            break108 109cap.release()110cv2.destroyAllWindows()111print(f"\n[DataCollect] Done — {captured} samples saved to {csv_path}")112