Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,21 +3,14 @@ import cv2
|
|
| 3 |
import numpy as np
|
| 4 |
import time
|
| 5 |
|
| 6 |
-
#
|
| 7 |
try:
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
from mediapipe.python.solutions import hands as mp_hands
|
| 15 |
-
from mediapipe.python.solutions import drawing_utils as mp_drawing
|
| 16 |
-
except (ImportError, AttributeError):
|
| 17 |
-
# Cara 3: Import standar
|
| 18 |
-
import mediapipe as mp
|
| 19 |
-
mp_hands = mp.solutions.hands
|
| 20 |
-
mp_drawing = mp.solutions.drawing_utils
|
| 21 |
|
| 22 |
# Inisialisasi Detektor Tangan
|
| 23 |
hands_detector = mp_hands.Hands(
|
|
@@ -27,25 +20,25 @@ hands_detector = mp_hands.Hands(
|
|
| 27 |
min_tracking_confidence=0.5
|
| 28 |
)
|
| 29 |
|
| 30 |
-
def
|
| 31 |
if image is None:
|
| 32 |
return None, None, None, last_trigger_time
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
results = hands_detector.process(
|
| 37 |
|
| 38 |
hand_detected = False
|
| 39 |
if results.multi_hand_landmarks:
|
| 40 |
hand_detected = True
|
| 41 |
-
for
|
| 42 |
-
mp_drawing.draw_landmarks(image,
|
| 43 |
|
| 44 |
current_time = time.time()
|
| 45 |
show_gif = None
|
| 46 |
play_audio = None
|
| 47 |
|
| 48 |
-
# Trigger
|
| 49 |
if hand_detected and (current_time - last_trigger_time > 3):
|
| 50 |
show_gif = "animasi.gif"
|
| 51 |
play_audio = "suara.mp3"
|
|
@@ -54,18 +47,18 @@ def detect_hand_only(image, last_trigger_time):
|
|
| 54 |
return image, show_gif, play_audio, last_trigger_time
|
| 55 |
|
| 56 |
with gr.Blocks() as demo:
|
| 57 |
-
gr.Markdown("# ✋
|
| 58 |
state_last_time = gr.State(0)
|
| 59 |
|
| 60 |
with gr.Row():
|
| 61 |
input_cam = gr.Image(sources=["webcam"], streaming=True)
|
| 62 |
with gr.Column():
|
| 63 |
output_cam = gr.Image(label="Monitoring")
|
| 64 |
-
output_gif = gr.Image(label="
|
| 65 |
output_audio = gr.Audio(autoplay=True)
|
| 66 |
|
| 67 |
input_cam.stream(
|
| 68 |
-
fn=
|
| 69 |
inputs=[input_cam, state_last_time],
|
| 70 |
outputs=[output_cam, output_gif, output_audio, state_last_time],
|
| 71 |
show_progress="hidden"
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import time
|
| 5 |
|
| 6 |
+
# Pengecekan Import secara paksa
|
| 7 |
try:
|
| 8 |
+
import mediapipe as mp
|
| 9 |
+
from mediapipe.solutions import hands as mp_hands
|
| 10 |
+
from mediapipe.solutions import drawing_utils as mp_drawing
|
| 11 |
+
print("MediaPipe loaded successfully")
|
| 12 |
+
except ImportError as e:
|
| 13 |
+
print(f"Error loading MediaPipe: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# Inisialisasi Detektor Tangan
|
| 16 |
hands_detector = mp_hands.Hands(
|
|
|
|
| 20 |
min_tracking_confidence=0.5
|
| 21 |
)
|
| 22 |
|
| 23 |
+
def detect_hand(image, last_trigger_time):
|
| 24 |
if image is None:
|
| 25 |
return None, None, None, last_trigger_time
|
| 26 |
|
| 27 |
+
# Proses deteksi
|
| 28 |
+
img_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 29 |
+
results = hands_detector.process(img_rgb)
|
| 30 |
|
| 31 |
hand_detected = False
|
| 32 |
if results.multi_hand_landmarks:
|
| 33 |
hand_detected = True
|
| 34 |
+
for landmarks in results.multi_hand_landmarks:
|
| 35 |
+
mp_drawing.draw_landmarks(image, landmarks, mp_hands.HAND_CONNECTIONS)
|
| 36 |
|
| 37 |
current_time = time.time()
|
| 38 |
show_gif = None
|
| 39 |
play_audio = None
|
| 40 |
|
| 41 |
+
# Trigger (Jeda 3 detik)
|
| 42 |
if hand_detected and (current_time - last_trigger_time > 3):
|
| 43 |
show_gif = "animasi.gif"
|
| 44 |
play_audio = "suara.mp3"
|
|
|
|
| 47 |
return image, show_gif, play_audio, last_trigger_time
|
| 48 |
|
| 49 |
with gr.Blocks() as demo:
|
| 50 |
+
gr.Markdown("# ✋ Hand Detector - Stable Version")
|
| 51 |
state_last_time = gr.State(0)
|
| 52 |
|
| 53 |
with gr.Row():
|
| 54 |
input_cam = gr.Image(sources=["webcam"], streaming=True)
|
| 55 |
with gr.Column():
|
| 56 |
output_cam = gr.Image(label="Monitoring")
|
| 57 |
+
output_gif = gr.Image(label="Visual")
|
| 58 |
output_audio = gr.Audio(autoplay=True)
|
| 59 |
|
| 60 |
input_cam.stream(
|
| 61 |
+
fn=detect_hand,
|
| 62 |
inputs=[input_cam, state_last_time],
|
| 63 |
outputs=[output_cam, output_gif, output_audio, state_last_time],
|
| 64 |
show_progress="hidden"
|