Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,11 +3,10 @@ import cv2
|
|
| 3 |
import mediapipe as mp
|
| 4 |
import time
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
-
# Inisialisasi Detektor Tangan
|
| 11 |
hands_detector = mp_hands.Hands(
|
| 12 |
static_image_mode=False,
|
| 13 |
max_num_hands=1,
|
|
@@ -15,14 +14,10 @@ hands_detector = mp_hands.Hands(
|
|
| 15 |
min_tracking_confidence=0.5
|
| 16 |
)
|
| 17 |
|
| 18 |
-
AUDIO_FILE = "suara.mp3"
|
| 19 |
-
GIF_FILE = "animasi.gif"
|
| 20 |
-
|
| 21 |
def detect_hand_only(image, last_trigger_time):
|
| 22 |
if image is None:
|
| 23 |
return None, None, None, last_trigger_time
|
| 24 |
|
| 25 |
-
# Konversi warna untuk MediaPipe
|
| 26 |
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 27 |
results = hands_detector.process(image_rgb)
|
| 28 |
|
|
@@ -30,31 +25,29 @@ def detect_hand_only(image, last_trigger_time):
|
|
| 30 |
if results.multi_hand_landmarks:
|
| 31 |
hand_detected = True
|
| 32 |
for hand_landmarks in results.multi_hand_landmarks:
|
| 33 |
-
# Gambar kerangka tangan di monitor
|
| 34 |
mp_drawing.draw_landmarks(image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
|
| 35 |
|
| 36 |
current_time = time.time()
|
| 37 |
show_gif = None
|
| 38 |
play_audio = None
|
| 39 |
|
| 40 |
-
# Jika tangan terlihat, pemicu suara dan GIF (jeda 3 detik)
|
| 41 |
if hand_detected and (current_time - last_trigger_time > 3):
|
| 42 |
-
show_gif =
|
| 43 |
-
play_audio =
|
| 44 |
last_trigger_time = current_time
|
| 45 |
|
| 46 |
return image, show_gif, play_audio, last_trigger_time
|
| 47 |
|
| 48 |
with gr.Blocks() as demo:
|
| 49 |
-
gr.Markdown("# ✋
|
| 50 |
state_last_time = gr.State(0)
|
| 51 |
|
| 52 |
with gr.Row():
|
| 53 |
input_cam = gr.Image(sources=["webcam"], streaming=True)
|
| 54 |
with gr.Column():
|
| 55 |
-
output_cam = gr.Image(label="Monitor
|
| 56 |
-
output_gif = gr.Image(label="
|
| 57 |
-
output_audio = gr.Audio(
|
| 58 |
|
| 59 |
input_cam.stream(
|
| 60 |
fn=detect_hand_only,
|
|
|
|
| 3 |
import mediapipe as mp
|
| 4 |
import time
|
| 5 |
|
| 6 |
+
# Inisialisasi secara bertahap agar tidak crash
|
| 7 |
+
mp_hands = mp.solutions.hands
|
| 8 |
+
mp_drawing = mp.solutions.drawing_utils
|
| 9 |
|
|
|
|
| 10 |
hands_detector = mp_hands.Hands(
|
| 11 |
static_image_mode=False,
|
| 12 |
max_num_hands=1,
|
|
|
|
| 14 |
min_tracking_confidence=0.5
|
| 15 |
)
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
def detect_hand_only(image, last_trigger_time):
|
| 18 |
if image is None:
|
| 19 |
return None, None, None, last_trigger_time
|
| 20 |
|
|
|
|
| 21 |
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 22 |
results = hands_detector.process(image_rgb)
|
| 23 |
|
|
|
|
| 25 |
if results.multi_hand_landmarks:
|
| 26 |
hand_detected = True
|
| 27 |
for hand_landmarks in results.multi_hand_landmarks:
|
|
|
|
| 28 |
mp_drawing.draw_landmarks(image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
|
| 29 |
|
| 30 |
current_time = time.time()
|
| 31 |
show_gif = None
|
| 32 |
play_audio = None
|
| 33 |
|
|
|
|
| 34 |
if hand_detected and (current_time - last_trigger_time > 3):
|
| 35 |
+
show_gif = "animasi.gif"
|
| 36 |
+
play_audio = "suara.mp3"
|
| 37 |
last_trigger_time = current_time
|
| 38 |
|
| 39 |
return image, show_gif, play_audio, last_trigger_time
|
| 40 |
|
| 41 |
with gr.Blocks() as demo:
|
| 42 |
+
gr.Markdown("# ✋ Hand Detector Final")
|
| 43 |
state_last_time = gr.State(0)
|
| 44 |
|
| 45 |
with gr.Row():
|
| 46 |
input_cam = gr.Image(sources=["webcam"], streaming=True)
|
| 47 |
with gr.Column():
|
| 48 |
+
output_cam = gr.Image(label="Monitor")
|
| 49 |
+
output_gif = gr.Image(label="Visual")
|
| 50 |
+
output_audio = gr.Audio(autoplay=True)
|
| 51 |
|
| 52 |
input_cam.stream(
|
| 53 |
fn=detect_hand_only,
|