Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,83 +1,75 @@
|
|
1 |
import cv2
|
2 |
import mediapipe as mp
|
3 |
-
import tensorflow as tf
|
4 |
import numpy as np
|
|
|
5 |
import gradio as gr
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
mp_hands = mp.solutions.hands
|
10 |
-
|
11 |
-
# Load the pre-trained sign language model
|
12 |
-
model = tf.keras.models.load_model('sign_language_model.h5')
|
13 |
-
|
14 |
-
# Initialize MediaPipe Hands and FaceMesh
|
15 |
-
hands = mp_hands.Hands(min_detection_confidence=0.7, min_tracking_confidence=0.7)
|
16 |
-
face_mesh = mp_face_mesh.FaceMesh(min_detection_confidence=0.7, min_tracking_confidence=0.7)
|
17 |
-
|
18 |
-
# Function for webcam input and processing landmarks
|
19 |
-
def process_frame(frame):
|
20 |
-
# Convert the frame to RGB (as MediaPipe uses RGB images)
|
21 |
-
image_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
22 |
-
results_hands = hands.process(image_rgb)
|
23 |
-
results_face = face_mesh.process(image_rgb)
|
24 |
-
|
25 |
-
# Draw landmarks for hands
|
26 |
-
if results_hands.multi_hand_landmarks:
|
27 |
-
for hand_landmarks in results_hands.multi_hand_landmarks:
|
28 |
-
mp.solutions.drawing_utils.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
|
29 |
-
|
30 |
-
# Draw landmarks for face
|
31 |
-
if results_face.multi_face_landmarks:
|
32 |
-
for face_landmarks in results_face.multi_face_landmarks:
|
33 |
-
mp.solutions.drawing_utils.draw_landmarks(frame, face_landmarks, mp_face_mesh.FACEMESH_CONTOURS)
|
34 |
-
|
35 |
-
# Convert back to BGR for OpenCV
|
36 |
-
return frame
|
37 |
-
|
38 |
-
# Function for performing sign language gesture prediction
|
39 |
-
def predict_gesture(frame):
|
40 |
-
# Preprocessing the frame for gesture recognition
|
41 |
-
image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
42 |
-
image = cv2.resize(image, (28, 28))
|
43 |
-
image = np.expand_dims(image, axis=-1)
|
44 |
-
image = image / 255.0
|
45 |
-
image = np.expand_dims(image, axis=0)
|
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 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import cv2
|
2 |
import mediapipe as mp
|
|
|
3 |
import numpy as np
|
4 |
+
import tensorflow as tf
|
5 |
import gradio as gr
|
6 |
|
7 |
+
# Load pre-trained model (replace 'model.h5' with the actual model path)
|
8 |
+
model = tf.keras.models.load_model("sign_language_model.h5")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
# MediaPipe Hands setup
|
11 |
+
mp_hands = mp.solutions.hands
|
12 |
+
mp_drawing = mp.solutions.drawing_utils
|
13 |
+
|
14 |
+
# Define labels (replace with your model's classes)
|
15 |
+
labels = ["A", "B", "C", "D", "E"] # Example labels
|
16 |
+
|
17 |
+
# Process webcam frame and predict sign language gesture
|
18 |
+
def recognize_sign(image):
|
19 |
+
# Convert the input image to RGB
|
20 |
+
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
21 |
+
|
22 |
+
# MediaPipe Hand Detection
|
23 |
+
with mp_hands.Hands(static_image_mode=False,
|
24 |
+
max_num_hands=1,
|
25 |
+
min_detection_confidence=0.7,
|
26 |
+
min_tracking_confidence=0.7) as hands:
|
27 |
+
result = hands.process(rgb_image)
|
28 |
+
|
29 |
+
if result.multi_hand_landmarks:
|
30 |
+
for hand_landmarks in result.multi_hand_landmarks:
|
31 |
+
# Draw landmarks on the image
|
32 |
+
mp_drawing.draw_landmarks(image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
|
33 |
+
|
34 |
+
# Extract hand landmark coordinates as features
|
35 |
+
landmarks = []
|
36 |
+
for lm in hand_landmarks.landmark:
|
37 |
+
landmarks.extend([lm.x, lm.y, lm.z])
|
38 |
+
|
39 |
+
# Reshape data for prediction
|
40 |
+
features = np.array(landmarks).reshape(1, -1)
|
41 |
+
|
42 |
+
# Predict gesture
|
43 |
+
prediction = model.predict(features)
|
44 |
+
gesture = labels[np.argmax(prediction)]
|
45 |
+
|
46 |
+
# Display the predicted gesture on the image
|
47 |
+
h, w, _ = image.shape
|
48 |
+
cv2.putText(image, gesture, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
49 |
+
return gesture, image
|
50 |
+
|
51 |
+
return "No hand detected", image
|
52 |
+
|
53 |
+
# Gradio interface wrapper
|
54 |
+
def gradio_wrapper(image):
|
55 |
+
# Convert Gradio input to OpenCV format
|
56 |
+
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
57 |
+
gesture, annotated_image = recognize_sign(image)
|
58 |
+
# Convert the annotated image back to RGB for display
|
59 |
+
annotated_image = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
|
60 |
+
return gesture, annotated_image
|
61 |
+
|
62 |
+
# Create Gradio Interface
|
63 |
+
interface = gr.Interface(
|
64 |
+
fn=gradio_wrapper,
|
65 |
+
inputs=gr.inputs.Image(source="webcam", tool=None),
|
66 |
+
outputs=[gr.outputs.Textbox(label="Predicted Gesture"),
|
67 |
+
gr.outputs.Image(label="Annotated Image")],
|
68 |
+
live=True,
|
69 |
+
title="Sign Language Recognition",
|
70 |
+
description="Predicts sign language gestures using TensorFlow and MediaPipe."
|
71 |
+
)
|
72 |
+
|
73 |
+
# Launch the Gradio app
|
74 |
+
if __name__ == "__main__":
|
75 |
+
interface.launch()
|