Update app.py
Browse files
app.py
CHANGED
|
@@ -3,9 +3,6 @@ import cv2
|
|
| 3 |
import mediapipe as mp
|
| 4 |
import tempfile
|
| 5 |
|
| 6 |
-
from micro_gestures import thumb_up, thumb_down, index_pointing_up, fist_closed, palm_open, ok_sign
|
| 7 |
-
from composite_gestures import detect_composite_gesture
|
| 8 |
-
|
| 9 |
# initialize mediapipe modules
|
| 10 |
mp_hands = mp.solutions.hands
|
| 11 |
mp_pose = mp.solutions.pose
|
|
@@ -32,6 +29,8 @@ def process_video(video_path, target_width=640):
|
|
| 32 |
temp_output = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 33 |
out = None
|
| 34 |
|
|
|
|
|
|
|
| 35 |
while cap.isOpened():
|
| 36 |
ret, frame = cap.read()
|
| 37 |
if not ret:
|
|
@@ -47,7 +46,6 @@ def process_video(video_path, target_width=640):
|
|
| 47 |
rgb_small = cv2.cvtColor(frame_small, cv2.COLOR_BGR2RGB)
|
| 48 |
|
| 49 |
# hand detection
|
| 50 |
-
detected_micro = []
|
| 51 |
hand_results = hands.process(rgb_small)
|
| 52 |
if hand_results.multi_hand_landmarks:
|
| 53 |
for hand_landmarks in hand_results.multi_hand_landmarks:
|
|
@@ -55,22 +53,9 @@ def process_video(video_path, target_width=640):
|
|
| 55 |
frame_small,
|
| 56 |
hand_landmarks,
|
| 57 |
mp_hands.HAND_CONNECTIONS,
|
| 58 |
-
mp_drawing.DrawingSpec(color=(0,0,255), thickness=1, circle_radius=1),
|
| 59 |
-
mp_drawing.DrawingSpec(color=(0,255,0), thickness=1, circle_radius=1)
|
| 60 |
)
|
| 61 |
-
# check micro gestures
|
| 62 |
-
if thumb_up(hand_landmarks.landmark):
|
| 63 |
-
detected_micro.append("thumb_up")
|
| 64 |
-
if thumb_down(hand_landmarks.landmark):
|
| 65 |
-
detected_micro.append("thumb_down")
|
| 66 |
-
if index_pointing_up(hand_landmarks.landmark):
|
| 67 |
-
detected_micro.append("index_pointing_up")
|
| 68 |
-
if fist_closed(hand_landmarks.landmark):
|
| 69 |
-
detected_micro.append("fist_closed")
|
| 70 |
-
if palm_open(hand_landmarks.landmark):
|
| 71 |
-
detected_micro.append("palm_open")
|
| 72 |
-
if ok_sign(hand_landmarks.landmark):
|
| 73 |
-
detected_micro.append("ok_sign")
|
| 74 |
|
| 75 |
# pose detection
|
| 76 |
pose_results = pose.process(rgb_small)
|
|
@@ -79,22 +64,19 @@ def process_video(video_path, target_width=640):
|
|
| 79 |
frame_small,
|
| 80 |
pose_results.pose_landmarks,
|
| 81 |
mp_pose.POSE_CONNECTIONS,
|
| 82 |
-
mp_drawing.DrawingSpec(color=(255,0,0), thickness=1, circle_radius=1),
|
| 83 |
-
mp_drawing.DrawingSpec(color=(0,255,255), thickness=1, circle_radius=1)
|
| 84 |
)
|
| 85 |
|
| 86 |
-
#
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
# put text label on the frame
|
| 90 |
-
cv2.putText(frame_small, composite_label, (10, target_height-10),
|
| 91 |
-
cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2, cv2.LINE_AA)
|
| 92 |
|
| 93 |
# initialize video writer
|
| 94 |
if out is None:
|
| 95 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 96 |
-
if fps <= 0 or fps > 120:
|
| 97 |
-
fps = 30
|
| 98 |
|
| 99 |
out = cv2.VideoWriter(
|
| 100 |
temp_output.name,
|
|
@@ -110,7 +92,8 @@ def process_video(video_path, target_width=640):
|
|
| 110 |
if out:
|
| 111 |
out.release()
|
| 112 |
|
| 113 |
-
return
|
|
|
|
| 114 |
|
| 115 |
# gradio interface
|
| 116 |
iface = gr.Interface(
|
|
@@ -119,9 +102,12 @@ iface = gr.Interface(
|
|
| 119 |
gr.Video(label="Upload or Record Video"),
|
| 120 |
gr.Slider(minimum=160, maximum=1280, value=640, step=20, label="Processing Width")
|
| 121 |
],
|
| 122 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
| 123 |
title="Hand & Body Pose Detection",
|
| 124 |
-
description="Upload or record a video, and see MediaPipe detect hand and body landmarks with connections
|
| 125 |
)
|
| 126 |
|
| 127 |
if __name__ == "__main__":
|
|
|
|
| 3 |
import mediapipe as mp
|
| 4 |
import tempfile
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
# initialize mediapipe modules
|
| 7 |
mp_hands = mp.solutions.hands
|
| 8 |
mp_pose = mp.solutions.pose
|
|
|
|
| 29 |
temp_output = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 30 |
out = None
|
| 31 |
|
| 32 |
+
last_label = "None" # store last detected gesture label
|
| 33 |
+
|
| 34 |
while cap.isOpened():
|
| 35 |
ret, frame = cap.read()
|
| 36 |
if not ret:
|
|
|
|
| 46 |
rgb_small = cv2.cvtColor(frame_small, cv2.COLOR_BGR2RGB)
|
| 47 |
|
| 48 |
# hand detection
|
|
|
|
| 49 |
hand_results = hands.process(rgb_small)
|
| 50 |
if hand_results.multi_hand_landmarks:
|
| 51 |
for hand_landmarks in hand_results.multi_hand_landmarks:
|
|
|
|
| 53 |
frame_small,
|
| 54 |
hand_landmarks,
|
| 55 |
mp_hands.HAND_CONNECTIONS,
|
| 56 |
+
mp_drawing.DrawingSpec(color=(0,0,255), thickness=1, circle_radius=1),
|
| 57 |
+
mp_drawing.DrawingSpec(color=(0,255,0), thickness=1, circle_radius=1)
|
| 58 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
# pose detection
|
| 61 |
pose_results = pose.process(rgb_small)
|
|
|
|
| 64 |
frame_small,
|
| 65 |
pose_results.pose_landmarks,
|
| 66 |
mp_pose.POSE_CONNECTIONS,
|
| 67 |
+
mp_drawing.DrawingSpec(color=(255,0,0), thickness=1, circle_radius=1),
|
| 68 |
+
mp_drawing.DrawingSpec(color=(0,255,255), thickness=1, circle_radius=1)
|
| 69 |
)
|
| 70 |
|
| 71 |
+
# here you would detect gesture label (example placeholder)
|
| 72 |
+
# last_label = detect_composite_gesture([...])
|
| 73 |
+
last_label = "example_label"
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
# initialize video writer
|
| 76 |
if out is None:
|
| 77 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 78 |
+
if fps <= 0 or fps > 120:
|
| 79 |
+
fps = 30
|
| 80 |
|
| 81 |
out = cv2.VideoWriter(
|
| 82 |
temp_output.name,
|
|
|
|
| 92 |
if out:
|
| 93 |
out.release()
|
| 94 |
|
| 95 |
+
# return both video path and last label for gradio
|
| 96 |
+
return temp_output.name, last_label
|
| 97 |
|
| 98 |
# gradio interface
|
| 99 |
iface = gr.Interface(
|
|
|
|
| 102 |
gr.Video(label="Upload or Record Video"),
|
| 103 |
gr.Slider(minimum=160, maximum=1280, value=640, step=20, label="Processing Width")
|
| 104 |
],
|
| 105 |
+
outputs=[
|
| 106 |
+
gr.Video(label="Processed Video with Landmarks"),
|
| 107 |
+
gr.Textbox(label="Detected Gesture", interactive=False)
|
| 108 |
+
],
|
| 109 |
title="Hand & Body Pose Detection",
|
| 110 |
+
description="Upload or record a video, and see MediaPipe detect hand and body landmarks with connections."
|
| 111 |
)
|
| 112 |
|
| 113 |
if __name__ == "__main__":
|