Anushree1's picture
Update app.py
7728777 verified
raw
history blame
2.73 kB
import cv2
import mediapipe as mp
import numpy as np
import tensorflow as tf
import gradio as gr
# Load pre-trained model (replace 'model.h5' with the actual model path)
model = tf.keras.models.load_model("sign_language_model.h5")
# MediaPipe Hands setup
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
# Define labels (replace with your model's classes)
labels = ["A", "B", "C", "D", "E"] # Example labels
# Process webcam frame and predict sign language gesture
def recognize_sign(image):
# Convert the input image to RGB
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# MediaPipe Hand Detection
with mp_hands.Hands(static_image_mode=False,
max_num_hands=1,
min_detection_confidence=0.7,
min_tracking_confidence=0.7) as hands:
result = hands.process(rgb_image)
if result.multi_hand_landmarks:
for hand_landmarks in result.multi_hand_landmarks:
# Draw landmarks on the image
mp_drawing.draw_landmarks(image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
# Extract hand landmark coordinates as features
landmarks = []
for lm in hand_landmarks.landmark:
landmarks.extend([lm.x, lm.y, lm.z])
# Reshape data for prediction
features = np.array(landmarks).reshape(1, -1)
# Predict gesture
prediction = model.predict(features)
gesture = labels[np.argmax(prediction)]
# Display the predicted gesture on the image
h, w, _ = image.shape
cv2.putText(image, gesture, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
return gesture, image
return "No hand detected", image
# Gradio interface wrapper
def gradio_wrapper(image):
# Convert Gradio input to OpenCV format
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
gesture, annotated_image = recognize_sign(image)
# Convert the annotated image back to RGB for display
annotated_image = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
return gesture, annotated_image
# Create Gradio Interface
interface = gr.Interface(
fn=gradio_wrapper,
inputs=gr.inputs.Image(source="webcam", tool=None),
outputs=[gr.outputs.Textbox(label="Predicted Gesture"),
gr.outputs.Image(label="Annotated Image")],
live=True,
title="Sign Language Recognition",
description="Predicts sign language gestures using TensorFlow and MediaPipe."
)
# Launch the Gradio app
if __name__ == "__main__":
interface.launch()