Anushree1 commited on
Commit
ea706c4
·
verified ·
1 Parent(s): fb6c986

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -70
app.py CHANGED
@@ -1,75 +1,24 @@
 
 
 
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()
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import numpy as np
4
  import cv2
5
  import mediapipe as mp
 
 
 
6
 
7
+ # Load your model (Make sure the model is uploaded to the space or use a path to the model file)
8
  model = tf.keras.models.load_model("sign_language_model.h5")
9
 
10
+ # Define your inference function
11
+ def predict(image):
12
+ # Preprocess the image here
13
+ # For example, resizing or normalizing
14
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
15
+ image = cv2.resize(image, (224, 224)) # Adjust based on your model input size
16
+ image = np.expand_dims(image, axis=0) # Add batch dimension
17
+ prediction = model.predict(image)
18
+ # Post-process the output if necessary (e.g., applying a threshold or mapping to class labels)
19
+ return prediction
20
+
21
+ # Set up the Gradio interface
22
+ iface = gr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(224, 224)), outputs="text")
23
+
24
+ iface.launch()