Update app.py
Browse files
app.py
CHANGED
|
@@ -4,8 +4,11 @@ import mediapipe as mp
|
|
| 4 |
import tempfile
|
| 5 |
from micro_gestures import *
|
| 6 |
from composite_gestures import detect_composite_gesture
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
mp_hands = mp.solutions.hands
|
| 10 |
mp_pose = mp.solutions.pose
|
| 11 |
mp_drawing = mp.solutions.drawing_utils
|
|
@@ -25,15 +28,37 @@ pose = mp_pose.Pose(
|
|
| 25 |
min_tracking_confidence=0.5
|
| 26 |
)
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
cap = cv2.VideoCapture(video_path)
|
| 31 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 32 |
temp_output = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 33 |
out = None
|
| 34 |
|
| 35 |
-
sequence_buffer = []
|
| 36 |
-
last_label = "None"
|
| 37 |
|
| 38 |
while cap.isOpened():
|
| 39 |
ret, frame = cap.read()
|
|
@@ -41,21 +66,18 @@ def process_video(video_path, target_width=640):
|
|
| 41 |
break
|
| 42 |
|
| 43 |
h0, w0 = frame.shape[:2]
|
| 44 |
-
# resize frame keeping aspect ratio
|
| 45 |
scale = target_width / float(w0)
|
| 46 |
target_height = int(round(h0 * scale))
|
| 47 |
frame_small = cv2.resize(frame, (target_width, target_height), interpolation=cv2.INTER_AREA)
|
| 48 |
-
|
| 49 |
-
# convert to rgb
|
| 50 |
rgb_small = cv2.cvtColor(frame_small, cv2.COLOR_BGR2RGB)
|
| 51 |
|
| 52 |
-
#
|
| 53 |
hand_results = hands.process(rgb_small)
|
| 54 |
micro_label = ""
|
| 55 |
if hand_results.multi_hand_landmarks:
|
| 56 |
for hand_landmarks in hand_results.multi_hand_landmarks:
|
| 57 |
-
mp_drawing.draw_landmarks(frame_small, hand_landmarks, mp_hands.HAND_CONNECTIONS)
|
| 58 |
-
landmarks = [(lm.x, lm.y, lm.z) for lm in hand_landmarks.landmark]
|
| 59 |
if fist_closed(landmarks):
|
| 60 |
micro_label = "fist_closed"
|
| 61 |
elif palm_open(landmarks):
|
|
@@ -64,19 +86,19 @@ def process_video(video_path, target_width=640):
|
|
| 64 |
micro_label = "index_up"
|
| 65 |
elif thumb_up(landmarks):
|
| 66 |
micro_label = "thumb_up"
|
| 67 |
-
|
| 68 |
if micro_label:
|
| 69 |
sequence_buffer.append(micro_label)
|
| 70 |
if len(sequence_buffer) > 5:
|
| 71 |
sequence_buffer.pop(0)
|
| 72 |
|
| 73 |
-
#
|
| 74 |
if sequence_buffer:
|
| 75 |
detected = detect_composite_gesture(sequence_buffer)
|
| 76 |
if detected:
|
| 77 |
last_label = detected
|
| 78 |
|
| 79 |
-
#
|
| 80 |
if out is None:
|
| 81 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 82 |
if fps <= 0 or fps > 120:
|
|
@@ -88,29 +110,39 @@ def process_video(video_path, target_width=640):
|
|
| 88 |
(frame_small.shape[1], frame_small.shape[0])
|
| 89 |
)
|
| 90 |
|
| 91 |
-
# write processed frame
|
| 92 |
out.write(frame_small)
|
| 93 |
|
| 94 |
cap.release()
|
| 95 |
if out:
|
| 96 |
out.release()
|
| 97 |
|
| 98 |
-
#
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
-
#
|
| 102 |
iface = gr.Interface(
|
| 103 |
-
fn=
|
| 104 |
inputs=[
|
| 105 |
gr.Video(label="Upload or Record Video"),
|
| 106 |
gr.Slider(minimum=160, maximum=1280, value=640, step=20, label="Processing Width")
|
| 107 |
],
|
| 108 |
outputs=[
|
| 109 |
gr.Video(label="Processed Video with Landmarks"),
|
| 110 |
-
gr.Textbox(label="Detected Gesture", interactive=False)
|
|
|
|
| 111 |
],
|
| 112 |
-
title="Hand & Body Pose Detection",
|
| 113 |
-
description="Upload or record a video, see MediaPipe detect hand landmarks
|
| 114 |
)
|
| 115 |
|
| 116 |
if __name__ == "__main__":
|
|
|
|
| 4 |
import tempfile
|
| 5 |
from micro_gestures import *
|
| 6 |
from composite_gestures import detect_composite_gesture
|
| 7 |
+
from transformers import pipeline
|
| 8 |
+
from rapidfuzz import process, fuzz
|
| 9 |
+
import soundfile as sf
|
| 10 |
|
| 11 |
+
# --- Mediapipe initialization ---
|
| 12 |
mp_hands = mp.solutions.hands
|
| 13 |
mp_pose = mp.solutions.pose
|
| 14 |
mp_drawing = mp.solutions.drawing_utils
|
|
|
|
| 28 |
min_tracking_confidence=0.5
|
| 29 |
)
|
| 30 |
|
| 31 |
+
# --- ASR initialization (Persian) ---
|
| 32 |
+
asr = pipeline(
|
| 33 |
+
task="automatic-speech-recognition",
|
| 34 |
+
model="vhdm/whisper-large-fa-v1",
|
| 35 |
+
device=-1 # CPU
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# --- Custom vocabulary for high accuracy ---
|
| 39 |
+
custom_vocab = ["نرد", "کامپیوتر", "هوش مصنوعی", "ماشین"]
|
| 40 |
+
|
| 41 |
+
def replace_fuzzy(text, vocab, threshold=70):
|
| 42 |
+
"""
|
| 43 |
+
Replace similar words in text using fuzzy matching.
|
| 44 |
+
Uses partial_ratio to catch small variations.
|
| 45 |
+
"""
|
| 46 |
+
for term in vocab:
|
| 47 |
+
# find closest substring in text
|
| 48 |
+
match, score = process.extractOne(term, [text], scorer=fuzz.partial_ratio)
|
| 49 |
+
if score >= threshold:
|
| 50 |
+
text = text.replace(match, term)
|
| 51 |
+
return text
|
| 52 |
+
|
| 53 |
+
# --- Video processing function ---
|
| 54 |
+
def process_video_with_asr(video_path, target_width=640):
|
| 55 |
cap = cv2.VideoCapture(video_path)
|
| 56 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 57 |
temp_output = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 58 |
out = None
|
| 59 |
|
| 60 |
+
sequence_buffer = []
|
| 61 |
+
last_label = "None"
|
| 62 |
|
| 63 |
while cap.isOpened():
|
| 64 |
ret, frame = cap.read()
|
|
|
|
| 66 |
break
|
| 67 |
|
| 68 |
h0, w0 = frame.shape[:2]
|
|
|
|
| 69 |
scale = target_width / float(w0)
|
| 70 |
target_height = int(round(h0 * scale))
|
| 71 |
frame_small = cv2.resize(frame, (target_width, target_height), interpolation=cv2.INTER_AREA)
|
|
|
|
|
|
|
| 72 |
rgb_small = cv2.cvtColor(frame_small, cv2.COLOR_BGR2RGB)
|
| 73 |
|
| 74 |
+
# Hand detection
|
| 75 |
hand_results = hands.process(rgb_small)
|
| 76 |
micro_label = ""
|
| 77 |
if hand_results.multi_hand_landmarks:
|
| 78 |
for hand_landmarks in hand_results.multi_hand_landmarks:
|
| 79 |
+
mp_drawing.draw_landmarks(frame_small, hand_landmarks, mp_hands.HAND_CONNECTIONS)
|
| 80 |
+
landmarks = [(lm.x, lm.y, lm.z) for lm in hand_landmarks.landmark]
|
| 81 |
if fist_closed(landmarks):
|
| 82 |
micro_label = "fist_closed"
|
| 83 |
elif palm_open(landmarks):
|
|
|
|
| 86 |
micro_label = "index_up"
|
| 87 |
elif thumb_up(landmarks):
|
| 88 |
micro_label = "thumb_up"
|
| 89 |
+
|
| 90 |
if micro_label:
|
| 91 |
sequence_buffer.append(micro_label)
|
| 92 |
if len(sequence_buffer) > 5:
|
| 93 |
sequence_buffer.pop(0)
|
| 94 |
|
| 95 |
+
# Detect composite gesture
|
| 96 |
if sequence_buffer:
|
| 97 |
detected = detect_composite_gesture(sequence_buffer)
|
| 98 |
if detected:
|
| 99 |
last_label = detected
|
| 100 |
|
| 101 |
+
# Initialize video writer
|
| 102 |
if out is None:
|
| 103 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 104 |
if fps <= 0 or fps > 120:
|
|
|
|
| 110 |
(frame_small.shape[1], frame_small.shape[0])
|
| 111 |
)
|
| 112 |
|
|
|
|
| 113 |
out.write(frame_small)
|
| 114 |
|
| 115 |
cap.release()
|
| 116 |
if out:
|
| 117 |
out.release()
|
| 118 |
|
| 119 |
+
# --- Extract audio from video for ASR ---
|
| 120 |
+
temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
|
| 121 |
+
import moviepy.editor as mpv
|
| 122 |
+
clip = mpv.VideoFileClip(video_path)
|
| 123 |
+
clip.audio.write_audiofile(temp_audio.name, fps=16000, nbytes=2, codec='pcm_s16le')
|
| 124 |
+
|
| 125 |
+
# ASR
|
| 126 |
+
result = asr(temp_audio.name, chunk_length_s=30, stride_length_s=[5,5])
|
| 127 |
+
text = result["text"]
|
| 128 |
+
final_text = replace_fuzzy(text, custom_vocab, threshold=80)
|
| 129 |
+
|
| 130 |
+
return temp_output.name, last_label, final_text
|
| 131 |
|
| 132 |
+
# --- Gradio interface ---
|
| 133 |
iface = gr.Interface(
|
| 134 |
+
fn=process_video_with_asr,
|
| 135 |
inputs=[
|
| 136 |
gr.Video(label="Upload or Record Video"),
|
| 137 |
gr.Slider(minimum=160, maximum=1280, value=640, step=20, label="Processing Width")
|
| 138 |
],
|
| 139 |
outputs=[
|
| 140 |
gr.Video(label="Processed Video with Landmarks"),
|
| 141 |
+
gr.Textbox(label="Detected Gesture", interactive=False),
|
| 142 |
+
gr.Textbox(label="Detected Words (Persian ASR)", interactive=False)
|
| 143 |
],
|
| 144 |
+
title="Hand & Body Pose Detection + Persian ASR",
|
| 145 |
+
description="Upload or record a video, see MediaPipe detect hand landmarks and Persian speech recognition with custom vocabulary."
|
| 146 |
)
|
| 147 |
|
| 148 |
if __name__ == "__main__":
|