Spaces:
Sleeping
Sleeping
File size: 2,032 Bytes
f609dda |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
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()
|