Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
-
import numpy as np
|
| 4 |
-
import time
|
| 5 |
import mediapipe as mp
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 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 |
|
| 16 |
# Inisialisasi Detektor Tangan
|
| 17 |
hands_detector = mp_hands.Hands(
|
|
@@ -28,7 +22,7 @@ def detect_hand_only(image, last_trigger_time):
|
|
| 28 |
if image is None:
|
| 29 |
return None, None, None, last_trigger_time
|
| 30 |
|
| 31 |
-
#
|
| 32 |
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 33 |
results = hands_detector.process(image_rgb)
|
| 34 |
|
|
@@ -36,13 +30,14 @@ def detect_hand_only(image, last_trigger_time):
|
|
| 36 |
if results.multi_hand_landmarks:
|
| 37 |
hand_detected = True
|
| 38 |
for hand_landmarks in results.multi_hand_landmarks:
|
|
|
|
| 39 |
mp_drawing.draw_landmarks(image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
|
| 40 |
|
| 41 |
current_time = time.time()
|
| 42 |
show_gif = None
|
| 43 |
play_audio = None
|
| 44 |
|
| 45 |
-
# Jika tangan terlihat,
|
| 46 |
if hand_detected and (current_time - last_trigger_time > 3):
|
| 47 |
show_gif = GIF_FILE
|
| 48 |
play_audio = AUDIO_FILE
|
|
@@ -51,14 +46,14 @@ def detect_hand_only(image, last_trigger_time):
|
|
| 51 |
return image, show_gif, play_audio, last_trigger_time
|
| 52 |
|
| 53 |
with gr.Blocks() as demo:
|
| 54 |
-
gr.Markdown("# ✋ Detektor Tangan (
|
| 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
|
| 62 |
output_audio = gr.Audio(label="Suara", autoplay=True)
|
| 63 |
|
| 64 |
input_cam.stream(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
|
|
|
|
|
|
| 3 |
import mediapipe as mp
|
| 4 |
+
import time
|
| 5 |
|
| 6 |
+
# Cara import terbaru agar tidak AttributeError
|
| 7 |
+
from mediapipe.solutions import hands as mp_hands
|
| 8 |
+
from mediapipe.solutions import drawing_utils as mp_drawing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Inisialisasi Detektor Tangan
|
| 11 |
hands_detector = mp_hands.Hands(
|
|
|
|
| 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 |
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 = GIF_FILE
|
| 43 |
play_audio = AUDIO_FILE
|
|
|
|
| 46 |
return image, show_gif, play_audio, last_trigger_time
|
| 47 |
|
| 48 |
with gr.Blocks() as demo:
|
| 49 |
+
gr.Markdown("# ✋ Detektor Tangan (v6 - Final Clean)")
|
| 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 AI")
|
| 56 |
+
output_gif = gr.Image(label="Respon Visual")
|
| 57 |
output_audio = gr.Audio(label="Suara", autoplay=True)
|
| 58 |
|
| 59 |
input_cam.stream(
|