Team Ai
Apppublic

Piyush23890/Sign_Language_Decoder

sourceHugging Facemitupdated 7mo agoView on Hugging Face
0likes
live_predict.py88 linesDownload Raw Back to root
1"""2live_predict.py3===============4Standalone webcam demo — shows real-time ISL static letter prediction5overlaid on the frame.  Does NOT start the Flask server.6 7Useful for quick model sanity-checks before running app.py.8 9Usage10-----11    python live_predict.py12 13Controls14--------15    Q  — quit16"""17 18import cv219import joblib20import mediapipe as mp21import numpy as np22 23MODEL_PATH    = "isl_alphabet_model.pkl"24STATIC_LABELS = [chr(i) for i in range(65, 91)]   # A–Z25 26# ── Load model ──────────────────────────────────────────────────────────────────27print(f"Loading {MODEL_PATH} …")28model = joblib.load(MODEL_PATH)29 30# ── MediaPipe ───────────────────────────────────────────────────────────────────31mp_hands = mp.solutions.hands32mp_draw  = mp.solutions.drawing_utils33 34hands = mp_hands.Hands(35    static_image_mode=False,36    max_num_hands=2,37    min_detection_confidence=0.70,38    min_tracking_confidence=0.70,39)40 41cap = cv2.VideoCapture(0)42print("Webcam started — press Q to quit.")43 44 45def extract_keypoints(results) -> np.ndarray:46    left  = np.zeros(63)47    right = np.zeros(63)48    if results.multi_hand_landmarks and results.multi_handedness:49        for i, hl in enumerate(results.multi_hand_landmarks):50            label = results.multi_handedness[i].classification[0].label51            pts   = []52            for lm in hl.landmark:53                pts.extend([lm.x, lm.y, lm.z])54            if label == "Left":55                left  = np.array(pts)56            else:57                right = np.array(pts)58    return np.concatenate([left, right])59 60 61while True:62    ok, frame = cap.read()63    if not ok:64        break65 66    frame = cv2.flip(frame, 1)67    rgb   = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)68    res   = hands.process(rgb)69 70    if res.multi_hand_landmarks:71        for hl in res.multi_hand_landmarks:72            mp_draw.draw_landmarks(frame, hl, mp_hands.HAND_CONNECTIONS)73 74        kp   = extract_keypoints(res)75        pred = model.predict(kp.reshape(1, -1))[0]76        if 0 <= pred < len(STATIC_LABELS):77            letter = STATIC_LABELS[pred]78            cv2.putText(frame, f"Sign: {letter}",79                        (30, 60), cv2.FONT_HERSHEY_SIMPLEX,80                        2.0, (0, 255, 0), 3)81 82    cv2.imshow("ISL Live Prediction", frame)83    if cv2.waitKey(1) & 0xFF == ord('q'):84        break85 86cap.release()87cv2.destroyAllWindows()88