Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import cv2 | |
| import numpy as np | |
| from tensorflow.keras.models import load_model | |
| import mediapipe as mp | |
| model = load_model('gesture_model.h5') | |
| actions = ['I', 'help', 'need', 'sleep', 'angry', 'urgent'] | |
| threshold = 0.8 | |
| mp_holistic = mp.solutions.holistic | |
| def extract_keypoints(results): | |
| pose = np.array([[res.x, res.y, res.z] for res in results.pose_landmarks.landmark]).flatten() if results.pose_landmarks else np.zeros(33 * 3) | |
| 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) | |
| 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) | |
| return np.concatenate([pose, lh, rh]) | |
| def predict_gesture(video_path): | |
| cap = cv2.VideoCapture(video_path) | |
| sequence = [] | |
| sentence = [] | |
| with mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5) as holistic: | |
| while cap.isOpened(): | |
| ret, frame = cap.read() | |
| if not ret: | |
| break | |
| image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) | |
| results = holistic.process(image) | |
| keypoints = extract_keypoints(results) | |
| sequence.append(keypoints) | |
| sequence = sequence[-30:] | |
| if len(sequence) == 30: | |
| res = model.predict(np.expand_dims(sequence, axis=0))[0] | |
| if res[np.argmax(res)] > threshold: | |
| action = actions[np.argmax(res)] | |
| if not sentence or sentence[-1] != action: | |
| sentence.append(action) | |
| cap.release() | |
| return ' '.join(sentence) | |
| iface = gr.Interface( | |
| fn=predict_gesture, | |
| inputs=gr.Video(label="Upload your gesture video"), | |
| outputs="text", | |
| title="Gesture Recognition AI", | |
| description="Upload a short gesture video (e.g., showing 'I need help') and get the recognized sentence." | |
| ) | |
| iface.launch() | |