Anushree1 commited on
Commit
7728777
·
verified ·
1 Parent(s): 7fbcbe9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -77
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
- # MediaPipe setup for hand and face landmark detection
8
- mp_face_mesh = mp.solutions.face_mesh
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
- # Prediction using the loaded model
48
- prediction = model.predict(image)
49
- predicted_class = np.argmax(prediction, axis=1)
50
-
51
- # Mapping the predicted class to corresponding ASL alphabet
52
- asl_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
53
- return asl_alphabet[predicted_class[0]]
54
-
55
- # Gradio Interface for live webcam input and prediction
56
- def webcam_input():
57
- cap = cv2.VideoCapture(0) # Open the webcam (try 1 or 2 if 0 doesn't work)
58
- if not cap.isOpened():
59
- return "Error: Cannot open camera"
60
-
61
- while True:
62
- ret, frame = cap.read()
63
- if not ret:
64
- break
65
-
66
- # Process the frame to detect landmarks and make predictions
67
- processed_frame = process_frame(frame)
68
- prediction = predict_gesture(processed_frame)
69
-
70
- # Display predicted gesture on the frame
71
- cv2.putText(processed_frame, f"Prediction: {prediction}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
72
-
73
- # Show the frame with predictions
74
- cv2.imshow("Hand and Facial Landmark Recognition", processed_frame)
75
-
76
- if cv2.waitKey(1) & 0xFF == ord('q'):
77
- break
78
-
79
- cap.release()
80
- cv2.destroyAllWindows()
81
-
82
- # Start the Gradio interface
83
- gr.Interface(fn=webcam_input, live=True, capture_video=True).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()