Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import cv2
|
|
| 3 |
import mediapipe as mp
|
| 4 |
import time
|
| 5 |
|
| 6 |
-
#
|
| 7 |
mp_hands = mp.solutions.hands
|
| 8 |
hands = mp_hands.Hands(
|
| 9 |
static_image_mode=False,
|
|
@@ -11,33 +11,29 @@ hands = mp_hands.Hands(
|
|
| 11 |
min_detection_confidence=0.7,
|
| 12 |
min_tracking_confidence=0.5
|
| 13 |
)
|
| 14 |
-
|
| 15 |
|
| 16 |
-
# File aset kamu
|
| 17 |
AUDIO_FILE = "suara.mp3"
|
| 18 |
GIF_FILE = "animasi.gif"
|
| 19 |
|
| 20 |
-
def
|
| 21 |
if image is None:
|
| 22 |
return None, None, None, last_trigger_time
|
| 23 |
|
| 24 |
-
# Deteksi
|
| 25 |
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 26 |
results = hands.process(image_rgb)
|
| 27 |
|
| 28 |
hand_detected = False
|
| 29 |
if results.multi_hand_landmarks:
|
| 30 |
hand_detected = True
|
| 31 |
-
# Gambar kerangka tangan di monitor
|
| 32 |
for hand_landmarks in results.multi_hand_landmarks:
|
| 33 |
-
|
| 34 |
|
| 35 |
-
# Logika Trigger Suara & GIF
|
| 36 |
current_time = time.time()
|
| 37 |
show_gif = None
|
| 38 |
play_audio = None
|
| 39 |
|
| 40 |
-
# Jika tangan muncul & sudah lewat cooldown 3 detik agar suara tidak berisik
|
| 41 |
if hand_detected and (current_time - last_trigger_time > 3):
|
| 42 |
show_gif = GIF_FILE
|
| 43 |
play_audio = AUDIO_FILE
|
|
@@ -45,9 +41,8 @@ def detect_hand(image, last_trigger_time):
|
|
| 45 |
|
| 46 |
return image, show_gif, play_audio, last_trigger_time
|
| 47 |
|
| 48 |
-
# Antarmuka UI
|
| 49 |
with gr.Blocks() as demo:
|
| 50 |
-
gr.Markdown("## ✋ Detektor
|
| 51 |
state_last_time = gr.State(0)
|
| 52 |
|
| 53 |
with gr.Row():
|
|
@@ -58,7 +53,7 @@ with gr.Blocks() as demo:
|
|
| 58 |
output_audio = gr.Audio(autoplay=True)
|
| 59 |
|
| 60 |
input_cam.stream(
|
| 61 |
-
fn=
|
| 62 |
inputs=[input_cam, state_last_time],
|
| 63 |
outputs=[output_cam, output_gif, output_audio, state_last_time],
|
| 64 |
show_progress="hidden"
|
|
|
|
| 3 |
import mediapipe as mp
|
| 4 |
import time
|
| 5 |
|
| 6 |
+
# Gunakan inisialisasi yang lebih spesifik untuk tangan
|
| 7 |
mp_hands = mp.solutions.hands
|
| 8 |
hands = mp_hands.Hands(
|
| 9 |
static_image_mode=False,
|
|
|
|
| 11 |
min_detection_confidence=0.7,
|
| 12 |
min_tracking_confidence=0.5
|
| 13 |
)
|
| 14 |
+
mp_drawing = mp.solutions.drawing_utils
|
| 15 |
|
|
|
|
| 16 |
AUDIO_FILE = "suara.mp3"
|
| 17 |
GIF_FILE = "animasi.gif"
|
| 18 |
|
| 19 |
+
def detect_hand_only(image, last_trigger_time):
|
| 20 |
if image is None:
|
| 21 |
return None, None, None, last_trigger_time
|
| 22 |
|
| 23 |
+
# Deteksi
|
| 24 |
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 25 |
results = hands.process(image_rgb)
|
| 26 |
|
| 27 |
hand_detected = False
|
| 28 |
if results.multi_hand_landmarks:
|
| 29 |
hand_detected = True
|
|
|
|
| 30 |
for hand_landmarks in results.multi_hand_landmarks:
|
| 31 |
+
mp_drawing.draw_landmarks(image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
|
| 32 |
|
|
|
|
| 33 |
current_time = time.time()
|
| 34 |
show_gif = None
|
| 35 |
play_audio = None
|
| 36 |
|
|
|
|
| 37 |
if hand_detected and (current_time - last_trigger_time > 3):
|
| 38 |
show_gif = GIF_FILE
|
| 39 |
play_audio = AUDIO_FILE
|
|
|
|
| 41 |
|
| 42 |
return image, show_gif, play_audio, last_trigger_time
|
| 43 |
|
|
|
|
| 44 |
with gr.Blocks() as demo:
|
| 45 |
+
gr.Markdown("## ✋ Detektor Tangan v3 (Stable)")
|
| 46 |
state_last_time = gr.State(0)
|
| 47 |
|
| 48 |
with gr.Row():
|
|
|
|
| 53 |
output_audio = gr.Audio(autoplay=True)
|
| 54 |
|
| 55 |
input_cam.stream(
|
| 56 |
+
fn=detect_hand_only,
|
| 57 |
inputs=[input_cam, state_last_time],
|
| 58 |
outputs=[output_cam, output_gif, output_audio, state_last_time],
|
| 59 |
show_progress="hidden"
|