Spaces:
Runtime error
Runtime error
Sandeepa
commited on
Commit
•
1d6b8f2
1
Parent(s):
9d6280b
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import mediapipe as mp
|
4 |
+
from tensorflow.keras.models import load_model
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
# Load the sign language recognition model
|
8 |
+
model = load_model('isl.h5')
|
9 |
+
|
10 |
+
# Initialize Mediapipe
|
11 |
+
mp_holistic = mp.solutions.holistic
|
12 |
+
mp_drawing = mp.solutions.drawing_utils
|
13 |
+
|
14 |
+
# Define actions
|
15 |
+
actions = ['hello', 'me', 'no', 'please', 'sorry', 'thank you', 'welcome', 'what', 'yes', 'you']
|
16 |
+
|
17 |
+
# Function to perform Mediapipe detection
|
18 |
+
def mediapipe_detection(image, model):
|
19 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
20 |
+
image.flags.writeable = False
|
21 |
+
results = model.process(image)
|
22 |
+
image.flags.writeable = True
|
23 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
24 |
+
return image, results
|
25 |
+
|
26 |
+
# Function to extract keypoints
|
27 |
+
def extract_keypoints(results):
|
28 |
+
pose = np.array([[res.x, res.y, res.z, res.visibility] for res in results.pose_landmarks.landmark]).flatten() if results.pose_landmarks else np.zeros(33*4)
|
29 |
+
lh = np.array([[res.x, res.y, res.z] for res in results.left_hand_landmarks.landmark]).flatten() if results.left_hand_landmarks else np.zeros(21*3)
|
30 |
+
rh = np.array([[res.x, res.y, res.z] for res in results.right_hand_landmarks.landmark]).flatten() if results.right_hand_landmarks else np.zeros(21*3)
|
31 |
+
return np.concatenate([pose, lh, rh])
|
32 |
+
|
33 |
+
# Function to predict sign from video
|
34 |
+
def predict_sign_from_video(video_path):
|
35 |
+
cap = cv2.VideoCapture(video_path)
|
36 |
+
frames = []
|
37 |
+
with mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5) as holistic:
|
38 |
+
while cap.isOpened():
|
39 |
+
ret, frame = cap.read()
|
40 |
+
if not ret:
|
41 |
+
break
|
42 |
+
|
43 |
+
image, results = mediapipe_detection(frame, holistic)
|
44 |
+
keypoints = extract_keypoints(results)
|
45 |
+
frames.append(keypoints)
|
46 |
+
if len(frames) == 30:
|
47 |
+
sequence = np.array(frames)
|
48 |
+
res = model.predict(np.expand_dims(sequence, axis=0))[0]
|
49 |
+
sign = actions[np.argmax(res)]
|
50 |
+
frames = [] # Reset frames for next sequence
|
51 |
+
return sign
|
52 |
+
|
53 |
+
cap.release()
|
54 |
+
|
55 |
+
# Create Gradio Interface
|
56 |
+
iface = gr.Interface(predict_sign_from_video,
|
57 |
+
inputs="video",
|
58 |
+
outputs="text",
|
59 |
+
title="Sign Speak",
|
60 |
+
description="Upload a video and get the predicted sign.")
|
61 |
+
iface.launch()
|