CoolFace
Apppublic

Piyush23890/Sign_Language_Decoder

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes
run_setup_wizard.py212 linesDownload Raw Back to root
1"""2run_setup_wizard.py3===================4All-in-one first-time setup wizard:5  1. Collect static landmark data for every letter A–Z6  2. Collect dynamic gesture sequences for HELLO and THANK YOU7  3. Train static Random Forest model  → isl_alphabet_model.pkl8  4. Train dynamic LSTM model          → dynamic_sign_model.h59  5. Convert LSTM to ONNX              → dynamic_sign_model.onnx10 11Usage12-----13    python run_setup_wizard.py14 15Controls (during data collection)16----------------------------------17    SPACE  — start recording18    ESC    — abort entire wizard19"""20 21import csv22import os23import subprocess24import sys25import time26 27import cv228import mediapipe as mp29import numpy as np30 31# ── Config ──────────────────────────────────────────────────────────────────────32STATIC_SIGNS   = [chr(i) for i in range(65, 91)]   # A–Z33DYNAMIC_SIGNS  = ["hello", "thank_you"]34STATIC_SAMPLES = 50    # samples per letter  (increase for better accuracy)35DYNAMIC_SEQ    = 30    # frames per sequence36DYNAMIC_SAMPLES= 30    # sequences per word37 38DATASET_DIR = "dataset"39DYNAMIC_DIR = "dynamic_dataset"40 41# ── MediaPipe ───────────────────────────────────────────────────────────────────42mp_hands_mod = mp.solutions.hands43mp_draw_mod  = mp.solutions.drawing_utils44hands = mp_hands_mod.Hands(45    max_num_hands=2,46    min_detection_confidence=0.70,47    min_tracking_confidence=0.70,48)49 50 51# ── Helpers ──────────────────────────────────────────────────────────────────────52def show_message(cap, message: str, wait_key: str = ' ') -> bool:53    """Show *message* on camera frame; return True when *wait_key* is pressed."""54    while True:55        ok, frame = cap.read()56        if not ok:57            return False58        frame = cv2.flip(frame, 1)59        cv2.rectangle(frame, (0, 0), (680, 65), (0, 0, 0), -1)60        cv2.putText(frame, message, (10, 42),61                    cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 255, 255), 2)62        cv2.imshow("SignBridge Setup", frame)63        key = cv2.waitKey(1) & 0xFF64        if key == ord(wait_key):65            return True66        if key == 27:   # ESC67            return False68 69 70def pad_row(row: list, length: int = 126) -> list:71    while len(row) < length:72        row.extend([0.0, 0.0, 0.0])73    return row[:length]74 75 76# ── Static collection ────────────────────────────────────────────────────────────77def collect_static(cap) -> bool:78    os.makedirs(DATASET_DIR, exist_ok=True)79    for sign in STATIC_SIGNS:80        sign_dir = os.path.join(DATASET_DIR, sign)81        os.makedirs(sign_dir, exist_ok=True)82        csv_path = os.path.join(sign_dir, "data.csv")83        is_new   = not os.path.isfile(csv_path)84 85        msg = f"SPACE to start  '{sign}'  (static sign)"86        if not show_message(cap, msg):87            return False88 89        with open(csv_path, mode="a", newline="") as f:90            writer = csv.writer(f)91            if is_new:92                header = []93                for h in ["L", "R"]:94                    for i in range(21):95                        header += [f"{h}_{i}_x", f"{h}_{i}_y", f"{h}_{i}_z"]96                writer.writerow(header)97 98            done = 099            while done < STATIC_SAMPLES:100                ok, frame = cap.read()101                if not ok:102                    break103                frame = cv2.flip(frame, 1)104                rgb   = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)105                res   = hands.process(rgb)106 107                row = []108                if res.multi_hand_landmarks:109                    for hl in res.multi_hand_landmarks:110                        mp_draw_mod.draw_landmarks(111                            frame, hl, mp_hands_mod.HAND_CONNECTIONS)112                        for lm in hl.landmark:113                            row.extend([lm.x, lm.y, lm.z])114                    row = pad_row(row)115                    writer.writerow(row)116                    done += 1117 118                cv2.rectangle(frame, (0, 0), (680, 65), (0, 0, 0), -1)119                cv2.putText(frame,120                            f"Recording '{sign}': {done}/{STATIC_SAMPLES}",121                            (10, 42), cv2.FONT_HERSHEY_SIMPLEX, 0.85, (0, 255, 0), 2)122                cv2.imshow("SignBridge Setup", frame)123                if cv2.waitKey(10) & 0xFF == 27:124                    return False125    return True126 127 128# ── Dynamic collection ───────────────────────────────────────────────────────────129def collect_dynamic(cap) -> bool:130    os.makedirs(DYNAMIC_DIR, exist_ok=True)131    for sign in DYNAMIC_SIGNS:132        sign_dir = os.path.join(DYNAMIC_DIR, sign)133        os.makedirs(sign_dir, exist_ok=True)134        done = 0135 136        while done < DYNAMIC_SAMPLES:137            msg = f"SPACE → record '{sign}' seq {done + 1}/{DYNAMIC_SAMPLES}"138            if not show_message(cap, msg):139                return False140 141            seq = []142            for fidx in range(DYNAMIC_SEQ):143                ok, frame = cap.read()144                if not ok:145                    break146                frame = cv2.flip(frame, 1)147                rgb   = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)148                res   = hands.process(rgb)149 150                row = []151                for i in range(2):152                    if res.multi_hand_landmarks and i < len(res.multi_hand_landmarks):153                        for lm in res.multi_hand_landmarks[i].landmark:154                            row.extend([lm.x, lm.y, lm.z])155                    else:156                        row.extend([0.0] * 63)157                seq.append(row[:126])158 159                if res.multi_hand_landmarks:160                    for hl in res.multi_hand_landmarks:161                        mp_draw_mod.draw_landmarks(162                            frame, hl, mp_hands_mod.HAND_CONNECTIONS)163 164                cv2.rectangle(frame, (0, 0), (680, 65), (0, 0, 0), -1)165                cv2.putText(frame,166                            f"Seq {done + 1}: frame {fidx + 1}/{DYNAMIC_SEQ}",167                            (10, 42), cv2.FONT_HERSHEY_SIMPLEX, 0.85, (0, 255, 0), 2)168                cv2.imshow("SignBridge Setup", frame)169                if cv2.waitKey(20) & 0xFF == 27:170                    return False171 172            np.save(os.path.join(sign_dir, f"{done}.npy"),173                    np.array(seq, dtype=np.float32))174            done += 1175    return True176 177 178# ── Main ─────────────────────────────────────────────────────────────────────────179if __name__ == "__main__":180    cap = cv2.VideoCapture(0)181    if not cap.isOpened():182        sys.exit("[ERROR] Cannot open webcam. Plug in camera and retry.")183 184    print("\n=== SignBridge First-Time Setup ===\n")185 186    if not show_message(cap, "SPACE to begin data collection  |  ESC to abort"):187        cap.release(); cv2.destroyAllWindows(); sys.exit()188 189    print("Phase 1/2 — Collecting static signs (A–Z) …")190    if not collect_static(cap):191        cap.release(); cv2.destroyAllWindows()192        sys.exit("[Aborted] Static collection cancelled.")193 194    print("Phase 2/2 — Collecting dynamic signs (Hello, Thank You) …")195    if not collect_dynamic(cap):196        cap.release(); cv2.destroyAllWindows()197        sys.exit("[Aborted] Dynamic collection cancelled.")198 199    cap.release(); cv2.destroyAllWindows()200 201    print("\n--- Data Collection Complete ---")202    print("Training static model …")203    subprocess.run([sys.executable, "train_model.py"], check=True)204 205    print("Training dynamic model …")206    subprocess.run([sys.executable, "train_dynamic_model.py"], check=True)207 208    print("Converting to ONNX …")209    subprocess.run([sys.executable, "convert_to_onnx.py"], check=True)210 211    print("\n✅  Setup complete!  Run:  python app.py")212