File size: 2,768 Bytes
70e9583
fdcb57e
 
 
 
 
 
70e9583
fdcb57e
 
 
40553b6
fdcb57e
70e9583
 
fdcb57e
 
 
40553b6
fdcb57e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40553b6
f59722b
bd8f62d
 
 
40553b6
bd8f62d
 
fdcb57e
 
bd8f62d
fdcb57e
40553b6
fdcb57e
 
70e9583
40553b6
bd8f62d
 
40553b6
 
 
 
 
70e9583
f59722b
40553b6
 
 
 
565002c
40553b6
 
 
 
fdcb57e
40553b6
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
import cv2
import copy
import string
import numpy as np
import pandas as pd
import itertools
import gradio as gr
from tensorflow import keras
import mediapipe as mp

# ---- Load Model ----
model = keras.models.load_model("model.h5", compile=False)

# ---- Constants ----
mp_hands = mp.solutions.hands
alphabet = ['1','2','3','4','5','6','7','8','9'] + list(string.ascii_uppercase)

# ---- Landmark Helpers ----
def calc_landmark_list(image, landmarks):
    image_width, image_height = image.shape[1], image.shape[0]
    landmark_point = []
    for landmark in landmarks.landmark:
        x = min(int(landmark.x * image_width), image_width - 1)
        y = min(int(landmark.y * image_height), image_height - 1)
        landmark_point.append([x, y])
    return landmark_point

def pre_process_landmark(landmark_list):
    temp = copy.deepcopy(landmark_list)
    base_x, base_y = temp[0]
    for i in range(len(temp)):
        temp[i][0] -= base_x
        temp[i][1] -= base_y
    temp = list(itertools.chain.from_iterable(temp))
    max_value = max(list(map(abs, temp))) or 1
    return [n / max_value for n in temp]

# ---- Prediction Logic ----
def predict(image):
    if image.shape[2] == 3:
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    with mp_hands.Hands(static_image_mode=False, max_num_hands=1, min_detection_confidence=0.5) as hands:
        results = hands.process(image_rgb)
        if results.multi_hand_landmarks:
            hand_landmarks = results.multi_hand_landmarks[0]
            landmark_list = calc_landmark_list(image_rgb, hand_landmarks)
            processed = pre_process_landmark(landmark_list)
            df = pd.DataFrame([processed])
            predictions = model.predict(df, verbose=0)
            predicted_class = np.argmax(predictions, axis=1)[0]
            return alphabet[predicted_class]

    return "No hand detected. Try again."

# ---- Combined Interface Function ----
def choose_input(image):
    if image is not None:
        return predict(image)
    return "Please provide an image using camera or upload."

# ---- Gradio Interface ----
with gr.Blocks() as interface:
    gr.Markdown("## Sign Language Detector")
    gr.Markdown("Upload a hand gesture image or use your webcam to detect the corresponding sign.\n[Indian Sign Language Dataset](https://www.kaggle.com/datasets/vaishnaviasonawane/indian-sign-language-dataset)")

    image_input = gr.Image(type="numpy", label="Upload Image or Use Webcam", sources=["upload", "webcam"])
    output = gr.Textbox(label="Predicted Sign")

    submit_btn = gr.Button("Detect")
    submit_btn.click(fn=choose_input, inputs=image_input, outputs=output)

# ---- Launch ----
interface.launch(share=True)