MSGod commited on
Commit
70e9583
·
verified ·
1 Parent(s): 031a770
Files changed (1) hide show
  1. app.py +31 -16
app.py CHANGED
@@ -1,21 +1,22 @@
1
- # app.py
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 model
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
- # Inference function
39
- def predict_sign(image):
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 f"Predicted Sign: {alphabet[predicted_class]}"
51
- return "No hand detected. Try again."
 
 
 
 
 
 
52
 
53
- # Gradio Interface
54
- demo = gr.Interface(fn=predict_sign,
55
- inputs=gr.Image(type="numpy", label="Upload Hand Image"),
56
- outputs=gr.Textbox(label="Prediction"),
57
- title="Indian Sign Language Detector",
58
- description="Upload a hand gesture image to detect the sign (A-Z, 0-9)")
 
 
 
59
 
60
- demo.launch()
 
 
 
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()