Spaces:
Sleeping
Sleeping
up
Browse files
app.py
CHANGED
|
@@ -1,21 +1,22 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
import cv2
|
| 4 |
import copy
|
| 5 |
import string
|
| 6 |
import numpy as np
|
| 7 |
import pandas as pd
|
| 8 |
import itertools
|
|
|
|
| 9 |
from tensorflow import keras
|
| 10 |
import mediapipe as mp
|
| 11 |
-
import gradio as gr
|
| 12 |
|
| 13 |
-
# Load
|
| 14 |
model = keras.models.load_model("model.h5", compile=False)
|
|
|
|
|
|
|
| 15 |
mp_hands = mp.solutions.hands
|
| 16 |
alphabet = ['1','2','3','4','5','6','7','8','9'] + list(string.ascii_uppercase)
|
| 17 |
|
| 18 |
-
# Helpers
|
| 19 |
def calc_landmark_list(image, landmarks):
|
| 20 |
image_width, image_height = image.shape[1], image.shape[0]
|
| 21 |
landmark_point = []
|
|
@@ -35,11 +36,14 @@ def pre_process_landmark(landmark_list):
|
|
| 35 |
max_value = max(list(map(abs, temp))) or 1
|
| 36 |
return [n / max_value for n in temp]
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
def
|
| 40 |
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
|
|
|
|
|
|
| 41 |
with mp_hands.Hands(static_image_mode=True, max_num_hands=1, min_detection_confidence=0.5) as hands:
|
| 42 |
results = hands.process(image)
|
|
|
|
| 43 |
if results.multi_hand_landmarks:
|
| 44 |
hand_landmarks = results.multi_hand_landmarks[0]
|
| 45 |
landmark_list = calc_landmark_list(image, hand_landmarks)
|
|
@@ -47,14 +51,25 @@ def predict_sign(image):
|
|
| 47 |
df = pd.DataFrame(processed).transpose()
|
| 48 |
predictions = model.predict(df, verbose=0)
|
| 49 |
predicted_class = np.argmax(predictions, axis=1)[0]
|
| 50 |
-
return
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
# Gradio
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
|
|
|
| 2 |
import cv2
|
| 3 |
import copy
|
| 4 |
import string
|
| 5 |
import numpy as np
|
| 6 |
import pandas as pd
|
| 7 |
import itertools
|
| 8 |
+
import gradio as gr
|
| 9 |
from tensorflow import keras
|
| 10 |
import mediapipe as mp
|
|
|
|
| 11 |
|
| 12 |
+
# ---- Load Model Safely for Inference Only ----
|
| 13 |
model = keras.models.load_model("model.h5", compile=False)
|
| 14 |
+
|
| 15 |
+
# ---- Constants ----
|
| 16 |
mp_hands = mp.solutions.hands
|
| 17 |
alphabet = ['1','2','3','4','5','6','7','8','9'] + list(string.ascii_uppercase)
|
| 18 |
|
| 19 |
+
# ---- Hand Landmark Helpers ----
|
| 20 |
def calc_landmark_list(image, landmarks):
|
| 21 |
image_width, image_height = image.shape[1], image.shape[0]
|
| 22 |
landmark_point = []
|
|
|
|
| 36 |
max_value = max(list(map(abs, temp))) or 1
|
| 37 |
return [n / max_value for n in temp]
|
| 38 |
|
| 39 |
+
# ---- Hand Detection and Prediction Function ----
|
| 40 |
+
def predict_hand_sign(image):
|
| 41 |
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 42 |
+
|
| 43 |
+
# MediaPipe hand tracking
|
| 44 |
with mp_hands.Hands(static_image_mode=True, max_num_hands=1, min_detection_confidence=0.5) as hands:
|
| 45 |
results = hands.process(image)
|
| 46 |
+
|
| 47 |
if results.multi_hand_landmarks:
|
| 48 |
hand_landmarks = results.multi_hand_landmarks[0]
|
| 49 |
landmark_list = calc_landmark_list(image, hand_landmarks)
|
|
|
|
| 51 |
df = pd.DataFrame(processed).transpose()
|
| 52 |
predictions = model.predict(df, verbose=0)
|
| 53 |
predicted_class = np.argmax(predictions, axis=1)[0]
|
| 54 |
+
return alphabet[predicted_class]
|
| 55 |
+
else:
|
| 56 |
+
return "No hand detected"
|
| 57 |
+
|
| 58 |
+
# ---- Gradio Interface Setup ----
|
| 59 |
+
def gradio_interface(image):
|
| 60 |
+
prediction = predict_hand_sign(image)
|
| 61 |
+
return prediction
|
| 62 |
|
| 63 |
+
# Create Gradio interface
|
| 64 |
+
iface = gr.Interface(
|
| 65 |
+
fn=gradio_interface,
|
| 66 |
+
inputs=gr.Image(type="numpy", label="Upload Image"),
|
| 67 |
+
outputs=gr.Textbox(label="Predicted Sign"),
|
| 68 |
+
live=True,
|
| 69 |
+
title="Sign Language Recognition",
|
| 70 |
+
description="Upload an image of a hand gesture and get the predicted sign language character.",
|
| 71 |
+
)
|
| 72 |
|
| 73 |
+
# Run the Gradio app
|
| 74 |
+
if __name__ == "__main__":
|
| 75 |
+
iface.launch()
|