Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,66 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
-
import
|
| 4 |
import time
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
AUDIO_FILE = "suara.mp3"
|
| 11 |
GIF_FILE = "animasi.gif"
|
| 12 |
|
| 13 |
-
def
|
| 14 |
if image is None:
|
| 15 |
-
return None, None, None,
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
motion_detected = False
|
| 27 |
-
gray_blur = cv2.GaussianBlur(gray, (21, 21), 0)
|
| 28 |
-
|
| 29 |
-
if prev_frame is not None:
|
| 30 |
-
frame_delta = cv2.absdiff(prev_frame, gray_blur)
|
| 31 |
-
thresh = cv2.threshold(frame_delta, 25, 255, cv2.THRESH_BINARY)[1]
|
| 32 |
-
motion_score = np.sum(thresh) / 255
|
| 33 |
-
if motion_score > 500:
|
| 34 |
-
motion_detected = True
|
| 35 |
-
|
| 36 |
-
prev_frame = gray_blur
|
| 37 |
-
|
| 38 |
-
# 3. Logika Trigger
|
| 39 |
current_time = time.time()
|
| 40 |
show_gif = None
|
| 41 |
play_audio = None
|
| 42 |
|
| 43 |
-
|
|
|
|
| 44 |
show_gif = GIF_FILE
|
| 45 |
play_audio = AUDIO_FILE
|
| 46 |
last_trigger_time = current_time
|
| 47 |
|
| 48 |
-
return image, show_gif, play_audio,
|
| 49 |
|
|
|
|
| 50 |
with gr.Blocks() as demo:
|
| 51 |
-
gr.Markdown("#
|
| 52 |
-
state_prev_frame = gr.State(None)
|
| 53 |
state_last_time = gr.State(0)
|
| 54 |
|
| 55 |
with gr.Row():
|
| 56 |
input_cam = gr.Image(sources=["webcam"], streaming=True)
|
| 57 |
with gr.Column():
|
| 58 |
-
output_cam = gr.Image(label="
|
| 59 |
-
output_gif = gr.Image(label="Respon")
|
| 60 |
output_audio = gr.Audio(autoplay=True)
|
| 61 |
|
| 62 |
input_cam.stream(
|
| 63 |
-
fn=
|
| 64 |
-
inputs=[input_cam,
|
| 65 |
-
outputs=[output_cam, output_gif, output_audio,
|
| 66 |
show_progress="hidden"
|
| 67 |
)
|
| 68 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
+
import mediapipe as mp
|
| 4 |
import time
|
| 5 |
|
| 6 |
+
# Inisialisasi MediaPipe Hands
|
| 7 |
+
mp_hands = mp.solutions.hands
|
| 8 |
+
hands = mp_hands.Hands(
|
| 9 |
+
static_image_mode=False,
|
| 10 |
+
max_num_hands=1,
|
| 11 |
+
min_detection_confidence=0.7,
|
| 12 |
+
min_tracking_confidence=0.5
|
| 13 |
+
)
|
| 14 |
+
mp_draw = mp.solutions.drawing_utils
|
| 15 |
|
| 16 |
+
# File aset kamu
|
| 17 |
AUDIO_FILE = "suara.mp3"
|
| 18 |
GIF_FILE = "animasi.gif"
|
| 19 |
|
| 20 |
+
def detect_hand(image, last_trigger_time):
|
| 21 |
if image is None:
|
| 22 |
+
return None, None, None, last_trigger_time
|
| 23 |
|
| 24 |
+
# Deteksi Tangan
|
| 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 |
+
mp_draw.draw_landmarks(image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
|
| 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
|
| 44 |
last_trigger_time = current_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 Gerakan Tangan")
|
|
|
|
| 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="Monitor AI")
|
| 57 |
+
output_gif = gr.Image(label="Respon 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"
|
| 65 |
)
|
| 66 |
|