Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,72 +1,72 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
|
|
|
| 3 |
import mediapipe as mp
|
| 4 |
-
import
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Initialize MediaPipe Hands model
|
| 7 |
mp_hands = mp.solutions.hands
|
| 8 |
mp_drawing = mp.solutions.drawing_utils
|
| 9 |
-
hands = mp_hands.Hands(
|
| 10 |
-
|
| 11 |
-
def detect_hands(video):
|
| 12 |
-
if not video or 'name' not in video:
|
| 13 |
-
return "Error: No video uploaded."
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
|
| 18 |
-
if
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
|
|
|
|
| 21 |
cap = cv2.VideoCapture(video_path)
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
while cap.isOpened():
|
| 26 |
ret, frame = cap.read()
|
| 27 |
if not ret:
|
| 28 |
-
break #
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 31 |
results = hands.process(frame_rgb)
|
| 32 |
|
| 33 |
-
# Draw landmarks if hands are detected
|
| 34 |
if results.multi_hand_landmarks:
|
| 35 |
-
|
| 36 |
-
|
| 37 |
for hand_landmarks in results.multi_hand_landmarks:
|
| 38 |
mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
|
| 39 |
-
else:
|
| 40 |
-
print("No hand detected in this frame.")
|
| 41 |
-
|
| 42 |
-
frames.append(frame)
|
| 43 |
|
| 44 |
cap.release()
|
| 45 |
-
print(f"Total frames processed: {len(frames)}")
|
| 46 |
-
|
| 47 |
-
if not hand_detected:
|
| 48 |
-
return "Error: No hands detected. Try adjusting lighting or hand position."
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
for frame in frames:
|
| 58 |
-
out.write(frame)
|
| 59 |
-
|
| 60 |
-
out.release()
|
| 61 |
-
return output_path
|
| 62 |
|
|
|
|
| 63 |
iface = gr.Interface(
|
| 64 |
fn=detect_hands,
|
| 65 |
-
inputs=gr.Video(
|
| 66 |
-
outputs=
|
| 67 |
-
title="Sign Language
|
| 68 |
-
description="
|
| 69 |
)
|
| 70 |
|
| 71 |
if __name__ == "__main__":
|
| 72 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
import mediapipe as mp
|
| 5 |
+
import logging
|
| 6 |
+
|
| 7 |
+
# Enable logging for debugging
|
| 8 |
+
logging.basicConfig(level=logging.DEBUG)
|
| 9 |
|
| 10 |
# Initialize MediaPipe Hands model
|
| 11 |
mp_hands = mp.solutions.hands
|
| 12 |
mp_drawing = mp.solutions.drawing_utils
|
| 13 |
+
hands = mp_hands.Hands(static_image_mode=False, max_num_hands=1, min_detection_confidence=0.5)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
def detect_hands(video_path):
|
| 16 |
+
logging.info(f"Received video path: {video_path}")
|
| 17 |
|
| 18 |
+
# Check if video path is valid
|
| 19 |
+
if not video_path:
|
| 20 |
+
logging.error("No video file received.")
|
| 21 |
+
return "Error: No video file received."
|
| 22 |
|
| 23 |
+
# Open video file
|
| 24 |
cap = cv2.VideoCapture(video_path)
|
| 25 |
+
|
| 26 |
+
if not cap.isOpened():
|
| 27 |
+
logging.error("Error opening video file.")
|
| 28 |
+
return "Error: Unable to open video."
|
| 29 |
+
|
| 30 |
+
frame_count = 0
|
| 31 |
+
detected_frames = 0
|
| 32 |
|
| 33 |
while cap.isOpened():
|
| 34 |
ret, frame = cap.read()
|
| 35 |
if not ret:
|
| 36 |
+
break # Exit loop if video ends
|
| 37 |
|
| 38 |
+
frame_count += 1
|
| 39 |
+
logging.debug(f"Processing frame {frame_count}")
|
| 40 |
+
|
| 41 |
+
# Convert frame to RGB
|
| 42 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 43 |
results = hands.process(frame_rgb)
|
| 44 |
|
|
|
|
| 45 |
if results.multi_hand_landmarks:
|
| 46 |
+
detected_frames += 1
|
| 47 |
+
logging.info(f"Hand detected in frame {frame_count}")
|
| 48 |
for hand_landmarks in results.multi_hand_landmarks:
|
| 49 |
mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
cap.release()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
# Log summary
|
| 54 |
+
logging.info(f"Total frames processed: {frame_count}")
|
| 55 |
+
logging.info(f"Frames with hand detected: {detected_frames}")
|
| 56 |
|
| 57 |
+
if detected_frames > 0:
|
| 58 |
+
return "Hand detected in the video!"
|
| 59 |
+
else:
|
| 60 |
+
return "No hand detected in the video."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
# Gradio Interface
|
| 63 |
iface = gr.Interface(
|
| 64 |
fn=detect_hands,
|
| 65 |
+
inputs=gr.Video(),
|
| 66 |
+
outputs="text",
|
| 67 |
+
title="Sign Language Translator",
|
| 68 |
+
description="Upload a video of a sign, and the model will detect hands."
|
| 69 |
)
|
| 70 |
|
| 71 |
if __name__ == "__main__":
|
| 72 |
+
iface.launch(debug=True)
|