Rajadhurai commited on
Commit
5054ee4
·
verified ·
1 Parent(s): 8a45bc7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -60
app.py CHANGED
@@ -2,11 +2,9 @@ import cv2
2
  import mediapipe as mp
3
  import numpy as np
4
  import gradio as gr
5
- import tempfile
6
 
7
- # Load model
8
  MODEL_PATH = "hand_landmarker.task"
9
-
10
  BaseOptions = mp.tasks.BaseOptions
11
  HandLandmarker = mp.tasks.vision.HandLandmarker
12
  HandLandmarkerOptions = mp.tasks.vision.HandLandmarkerOptions
@@ -46,61 +44,42 @@ def get_finger_color(start_idx):
46
  else:
47
  return FINGER_COLORS['palm']
48
 
49
- def process_video(video_path):
50
- cap = cv2.VideoCapture(video_path)
51
-
52
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
53
- tmp_out = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
54
- out_path = tmp_out.name
55
-
56
- fps = cap.get(cv2.CAP_PROP_FPS)
57
- w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
58
- h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
59
- out = cv2.VideoWriter(out_path, fourcc, fps, (w, h))
60
-
61
- options = HandLandmarkerOptions(
62
- base_options=BaseOptions(model_asset_path=MODEL_PATH),
63
- running_mode=VisionRunningMode.IMAGE,
64
- num_hands=2,
65
- min_hand_detection_confidence=0.5,
66
- min_hand_presence_confidence=0.5,
67
- min_tracking_confidence=0.5
68
- )
69
-
70
- with HandLandmarker.create_from_options(options) as landmarker:
71
- while cap.isOpened():
72
- ret, frame = cap.read()
73
- if not ret:
74
- break
75
-
76
- rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
77
- mp_img = mp_image(image_format=mp_format.SRGB, data=rgb_frame)
78
- results = landmarker.detect(mp_img)
79
-
80
- if results.hand_landmarks:
81
- for hand_landmarks in results.hand_landmarks:
82
- points = [(int(lm.x * w), int(lm.y * h)) for lm in hand_landmarks]
83
-
84
- for start, end in HAND_CONNECTIONS:
85
- color = get_finger_color(start)
86
- cv2.line(frame, points[start], points[end], color, 2)
87
-
88
- for i, (x, y) in enumerate(points):
89
- cv2.circle(frame, (x, y), 4, (0, 255, 255), -1)
90
-
91
- out.write(frame)
92
-
93
- cap.release()
94
- out.release()
95
- return out_path
96
-
97
- # Gradio interface
98
- demo = gr.Interface(
99
- fn=process_video,
100
- inputs=gr.Video(label="Upload Video or Use Webcam"),
101
- outputs=gr.Video(label="Hand Landmark Annotated Video"),
102
- title="Hand Detection ",
103
- description="Upload a video or use webcam to detect hands."
104
  )
105
-
106
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import mediapipe as mp
3
  import numpy as np
4
  import gradio as gr
 
5
 
6
+ # MediaPipe setup
7
  MODEL_PATH = "hand_landmarker.task"
 
8
  BaseOptions = mp.tasks.BaseOptions
9
  HandLandmarker = mp.tasks.vision.HandLandmarker
10
  HandLandmarkerOptions = mp.tasks.vision.HandLandmarkerOptions
 
44
  else:
45
  return FINGER_COLORS['palm']
46
 
47
+ # Load model only once
48
+ options = HandLandmarkerOptions(
49
+ base_options=BaseOptions(model_asset_path=MODEL_PATH),
50
+ running_mode=VisionRunningMode.IMAGE,
51
+ num_hands=2,
52
+ min_hand_detection_confidence=0.5,
53
+ min_hand_presence_confidence=0.5,
54
+ min_tracking_confidence=0.5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  )
56
+ landmarker = HandLandmarker.create_from_options(options)
57
+
58
+ # Main processing function
59
+ def detect_hand(frame):
60
+ rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
61
+ mp_img = mp_image(image_format=mp_format.SRGB, data=rgb_frame)
62
+ results = landmarker.detect(mp_img)
63
+
64
+ h, w, _ = frame.shape
65
+ if results.hand_landmarks:
66
+ for hand_landmarks in results.hand_landmarks:
67
+ points = [(int(lm.x * w), int(lm.y * h)) for lm in hand_landmarks]
68
+
69
+ for start, end in HAND_CONNECTIONS:
70
+ color = get_finger_color(start)
71
+ cv2.line(frame, points[start], points[end], color, 2)
72
+
73
+ for (x, y) in points:
74
+ cv2.circle(frame, (x, y), 4, (0, 255, 255), -1)
75
+
76
+ return frame
77
+
78
+ # Gradio UI
79
+ gr.Interface(
80
+ fn=detect_hand,
81
+ inputs=gr.Image(source="webcam", streaming=True, label="Webcam Input"),
82
+ outputs=gr.Image(label="Annotated Frame"),
83
+ title="Real-time Hand Detection with MediaPipe",
84
+ live=True
85
+ ).launch()