Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,13 +2,14 @@ import gradio as gr
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
import time
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
try:
|
| 8 |
from mediapipe.python.solutions import hands as mp_hands
|
| 9 |
from mediapipe.python.solutions import drawing_utils as mp_drawing
|
| 10 |
-
except
|
| 11 |
-
|
| 12 |
mp_hands = mp.solutions.hands
|
| 13 |
mp_drawing = mp.solutions.drawing_utils
|
| 14 |
|
|
@@ -27,7 +28,7 @@ def detect_hand_only(image, last_trigger_time):
|
|
| 27 |
if image is None:
|
| 28 |
return None, None, None, last_trigger_time
|
| 29 |
|
| 30 |
-
# Deteksi
|
| 31 |
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 32 |
results = hands_detector.process(image_rgb)
|
| 33 |
|
|
@@ -41,7 +42,7 @@ def detect_hand_only(image, last_trigger_time):
|
|
| 41 |
show_gif = None
|
| 42 |
play_audio = None
|
| 43 |
|
| 44 |
-
#
|
| 45 |
if hand_detected and (current_time - last_trigger_time > 3):
|
| 46 |
show_gif = GIF_FILE
|
| 47 |
play_audio = AUDIO_FILE
|
|
@@ -50,15 +51,15 @@ def detect_hand_only(image, last_trigger_time):
|
|
| 50 |
return image, show_gif, play_audio, last_trigger_time
|
| 51 |
|
| 52 |
with gr.Blocks() as demo:
|
| 53 |
-
gr.Markdown("#
|
| 54 |
state_last_time = gr.State(0)
|
| 55 |
|
| 56 |
with gr.Row():
|
| 57 |
input_cam = gr.Image(sources=["webcam"], streaming=True)
|
| 58 |
with gr.Column():
|
| 59 |
output_cam = gr.Image(label="Monitor AI")
|
| 60 |
-
output_gif = gr.Image(label="Respon Visual")
|
| 61 |
-
output_audio = gr.Audio(autoplay=True)
|
| 62 |
|
| 63 |
input_cam.stream(
|
| 64 |
fn=detect_hand_only,
|
|
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
import time
|
| 5 |
+
import mediapipe as mp
|
| 6 |
|
| 7 |
+
# Logika Anti-Error untuk memanggil modul tangan
|
| 8 |
try:
|
| 9 |
from mediapipe.python.solutions import hands as mp_hands
|
| 10 |
from mediapipe.python.solutions import drawing_utils as mp_drawing
|
| 11 |
+
except:
|
| 12 |
+
# Jika cara di atas gagal, gunakan cara alternatif ini
|
| 13 |
mp_hands = mp.solutions.hands
|
| 14 |
mp_drawing = mp.solutions.drawing_utils
|
| 15 |
|
|
|
|
| 28 |
if image is None:
|
| 29 |
return None, None, None, last_trigger_time
|
| 30 |
|
| 31 |
+
# Deteksi Tangan
|
| 32 |
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 33 |
results = hands_detector.process(image_rgb)
|
| 34 |
|
|
|
|
| 42 |
show_gif = None
|
| 43 |
play_audio = None
|
| 44 |
|
| 45 |
+
# Jika tangan terlihat, munculkan GIF dan Suara (jeda 3 detik)
|
| 46 |
if hand_detected and (current_time - last_trigger_time > 3):
|
| 47 |
show_gif = GIF_FILE
|
| 48 |
play_audio = AUDIO_FILE
|
|
|
|
| 51 |
return image, show_gif, play_audio, last_trigger_time
|
| 52 |
|
| 53 |
with gr.Blocks() as demo:
|
| 54 |
+
gr.Markdown("# ✋ Detektor Tangan (v5 - Anti-Error)")
|
| 55 |
state_last_time = gr.State(0)
|
| 56 |
|
| 57 |
with gr.Row():
|
| 58 |
input_cam = gr.Image(sources=["webcam"], streaming=True)
|
| 59 |
with gr.Column():
|
| 60 |
output_cam = gr.Image(label="Monitor AI")
|
| 61 |
+
output_gif = gr.Image(label="Respon Visual (GIF)")
|
| 62 |
+
output_audio = gr.Audio(label="Suara", autoplay=True)
|
| 63 |
|
| 64 |
input_cam.stream(
|
| 65 |
fn=detect_hand_only,
|