Spaces:
Sleeping
Sleeping
Download app.py from MSGod/sign_language_detection: direct link, hf CLI and curl.
- Browser
- Download file 2.77 kB
-
https://huggingface.co/spaces/MSGod/sign_language_detection/resolve/main/app.py
- Command line
-
hf download hf://spaces/MSGod/sign_language_detection/app.py
-
curl -L -o app.py https://huggingface.co/spaces/MSGod/sign_language_detection/resolve/main/app.py
2.77 kB
| 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) | |