Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,51 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
-
import
|
| 5 |
-
from transformers import ViTForImageClassification, ViTImageProcessor
|
| 6 |
-
import mediapipe as mp
|
| 7 |
|
| 8 |
-
# Load pretrained model (
|
| 9 |
model_name = "sokaina55/xclip-base-patch32-finetuned-ssl-sign-language-recognition"
|
| 10 |
-
model =
|
| 11 |
-
processor =
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
asl_words = model.config.id2label # Dictionary mapping index to ASL words
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 24 |
-
results = hands.process(image_rgb)
|
| 25 |
-
|
| 26 |
-
if results.multi_hand_landmarks:
|
| 27 |
-
for hand_landmarks in results.multi_hand_landmarks:
|
| 28 |
-
landmarks = np.array([[lm.x, lm.y, lm.z] for lm in hand_landmarks.landmark]).flatten()
|
| 29 |
-
return landmarks
|
| 30 |
-
return None
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
landmarks = extract_hand_landmarks(image)
|
| 35 |
-
if landmarks is None:
|
| 36 |
-
return "No hand detected"
|
| 37 |
-
|
| 38 |
-
# Convert image into a format suitable for the model
|
| 39 |
-
inputs = processor(images=image, return_tensors="pt")
|
| 40 |
|
| 41 |
with torch.no_grad():
|
| 42 |
outputs = model(**inputs)
|
| 43 |
-
predicted_class_idx = outputs.logits.argmax(-1).item()
|
| 44 |
-
|
| 45 |
-
return asl_words.get(predicted_class_idx, "Unknown sign")
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
detected_words = []
|
| 51 |
|
| 52 |
-
|
| 53 |
-
ret, frame = cap.read()
|
| 54 |
-
if not ret:
|
| 55 |
-
break
|
| 56 |
-
word = classify_asl_word(frame)
|
| 57 |
-
if word not in detected_words and word != "No hand detected":
|
| 58 |
-
detected_words.append(word)
|
| 59 |
-
|
| 60 |
-
cap.release()
|
| 61 |
-
return ", ".join(detected_words) if detected_words else "No ASL words detected"
|
| 62 |
|
| 63 |
-
# Gradio
|
| 64 |
iface = gr.Interface(
|
| 65 |
-
fn=
|
| 66 |
-
inputs=gr.Video(
|
| 67 |
-
outputs=gr.Textbox(label="
|
| 68 |
-
title="
|
| 69 |
-
description="Upload a video of
|
| 70 |
)
|
| 71 |
|
| 72 |
if __name__ == "__main__":
|
| 73 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
import cv2
|
| 4 |
import numpy as np
|
| 5 |
+
from transformers import VideoMAEForVideoClassification, VideoMAEImageProcessor
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Load the pretrained model (VideoMAE)
|
| 8 |
model_name = "sokaina55/xclip-base-patch32-finetuned-ssl-sign-language-recognition"
|
| 9 |
+
model = VideoMAEForVideoClassification.from_pretrained(model_name)
|
| 10 |
+
processor = VideoMAEImageProcessor.from_pretrained(model_name)
|
| 11 |
|
| 12 |
+
# Function to process video frames and make predictions
|
| 13 |
+
def predict(video_path):
|
| 14 |
+
cap = cv2.VideoCapture(video_path)
|
| 15 |
+
frames = []
|
| 16 |
+
|
| 17 |
+
while cap.isOpened():
|
| 18 |
+
ret, frame = cap.read()
|
| 19 |
+
if not ret:
|
| 20 |
+
break
|
| 21 |
+
frame = cv2.resize(frame, (224, 224)) # Resize for model compatibility
|
| 22 |
+
frames.append(frame)
|
| 23 |
|
| 24 |
+
cap.release()
|
|
|
|
| 25 |
|
| 26 |
+
if len(frames) == 0:
|
| 27 |
+
return "No frames detected in video!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# Convert frames to tensor
|
| 30 |
+
inputs = processor(images=frames, return_tensors="pt")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
with torch.no_grad():
|
| 33 |
outputs = model(**inputs)
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
logits = outputs.logits
|
| 36 |
+
predicted_class_idx = logits.argmax(-1).item()
|
| 37 |
+
predicted_label = model.config.id2label[predicted_class_idx] # Convert index to label
|
|
|
|
| 38 |
|
| 39 |
+
return f"Predicted Sign: {predicted_label}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# Gradio UI
|
| 42 |
iface = gr.Interface(
|
| 43 |
+
fn=predict,
|
| 44 |
+
inputs=gr.Video(),
|
| 45 |
+
outputs=gr.Textbox(label="Recognized Sign"),
|
| 46 |
+
title="Sign Language Translator",
|
| 47 |
+
description="Upload a video of a hand gesture, and the model will predict the corresponding sign."
|
| 48 |
)
|
| 49 |
|
| 50 |
if __name__ == "__main__":
|
| 51 |
+
iface.launch(debug=True)
|