AreebKhan commited on
Commit
80e943d
·
verified ·
1 Parent(s): e0b4055

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -40
app.py CHANGED
@@ -1,72 +1,72 @@
1
  import gradio as gr
2
  import cv2
 
3
  import mediapipe as mp
4
- import os
 
 
 
5
 
6
  # Initialize MediaPipe Hands model
7
  mp_hands = mp.solutions.hands
8
  mp_drawing = mp.solutions.drawing_utils
9
- hands = mp_hands.Hands(min_detection_confidence=0.3, min_tracking_confidence=0.5) # Lowered threshold
10
-
11
- def detect_hands(video):
12
- if not video or 'name' not in video:
13
- return "Error: No video uploaded."
14
 
15
- video_path = video['name']
16
- print(f"Received video path: {video_path}")
17
 
18
- if not os.path.exists(video_path):
19
- return "Error: Video file not found."
 
 
20
 
 
21
  cap = cv2.VideoCapture(video_path)
22
- frames = []
23
- hand_detected = False # Flag to check if hand detection is working
 
 
 
 
 
24
 
25
  while cap.isOpened():
26
  ret, frame = cap.read()
27
  if not ret:
28
- break # No more frames to read
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
- hand_detected = True # Set flag to True if a hand is detected
36
- print("Hand detected!")
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
- if not frames:
51
- return "Error: No frames were processed. Try converting your video to .mp4 and re-uploading."
 
52
 
53
- height, width, _ = frames[0].shape
54
- output_path = "hand_detected_video.mp4"
55
- out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), 20, (width, height))
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(label="Upload Sign Language Video"),
66
- outputs=gr.Video(label="Hand Landmarks Detected"),
67
- title="Sign Language Hand Tracking",
68
- description="Uploads a video, detects hand landmarks using MediaPipe, and returns the processed video."
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)