Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,41 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import mediapipe as mp
|
| 4 |
-
import numpy as np
|
| 5 |
import time
|
| 6 |
|
| 7 |
-
#
|
| 8 |
mp_hands = mp.solutions.hands
|
| 9 |
mp_drawing = mp.solutions.drawing_utils
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
)
|
| 17 |
-
|
| 18 |
-
def detect_hand(image, last_trigger_time):
|
| 19 |
-
if image is None:
|
| 20 |
-
return None, None, None, last_trigger_time
|
| 21 |
-
|
| 22 |
-
img_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 23 |
-
results = hands_detector.process(img_rgb)
|
| 24 |
|
| 25 |
hand_detected = False
|
| 26 |
if results.multi_hand_landmarks:
|
| 27 |
hand_detected = True
|
| 28 |
-
for
|
| 29 |
-
mp_drawing.draw_landmarks(image,
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
play_audio = "suara.mp3"
|
| 38 |
-
last_trigger_time = current_time
|
| 39 |
-
|
| 40 |
-
return image, show_gif, play_audio, last_trigger_time
|
| 41 |
|
| 42 |
with gr.Blocks() as demo:
|
| 43 |
-
gr.
|
| 44 |
-
state_last_time = gr.State(0)
|
| 45 |
-
|
| 46 |
with gr.Row():
|
| 47 |
-
|
| 48 |
with gr.Column():
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
-
|
| 54 |
-
fn=detect_hand,
|
| 55 |
-
inputs=[input_cam, state_last_time],
|
| 56 |
-
outputs=[output_cam, output_gif, output_audio, state_last_time],
|
| 57 |
-
show_progress="hidden"
|
| 58 |
-
)
|
| 59 |
|
| 60 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import mediapipe as mp
|
|
|
|
| 4 |
import time
|
| 5 |
|
| 6 |
+
# Inisialisasi
|
| 7 |
mp_hands = mp.solutions.hands
|
| 8 |
mp_drawing = mp.solutions.drawing_utils
|
| 9 |
+
hands_detector = mp_hands.Hands(min_detection_confidence=0.7, min_tracking_confidence=0.5)
|
| 10 |
|
| 11 |
+
def detect(image, last_trigger_time):
|
| 12 |
+
if image is None: return None, None, None, last_trigger_time
|
| 13 |
+
|
| 14 |
+
rgb_small = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 15 |
+
results = hands_detector.process(rgb_small)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
hand_detected = False
|
| 18 |
if results.multi_hand_landmarks:
|
| 19 |
hand_detected = True
|
| 20 |
+
for res in results.multi_hand_landmarks:
|
| 21 |
+
mp_drawing.draw_landmarks(image, res, mp_hands.HAND_CONNECTIONS)
|
| 22 |
|
| 23 |
+
curr = time.time()
|
| 24 |
+
gif, audio = None, None
|
| 25 |
+
if hand_detected and (curr - last_trigger_time > 3):
|
| 26 |
+
gif, audio, last_trigger_time = "animasi.gif", "suara.mp3", curr
|
| 27 |
+
|
| 28 |
+
return image, gif, audio, last_trigger_time
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
with gr.Blocks() as demo:
|
| 31 |
+
t_state = gr.State(0)
|
|
|
|
|
|
|
| 32 |
with gr.Row():
|
| 33 |
+
cam = gr.Image(sources=["webcam"], streaming=True)
|
| 34 |
with gr.Column():
|
| 35 |
+
out_cam = gr.Image()
|
| 36 |
+
out_gif = gr.Image()
|
| 37 |
+
out_aud = gr.Audio(autoplay=True)
|
| 38 |
|
| 39 |
+
cam.stream(detect, [cam, t_state], [out_cam, out_gif, out_aud, t_state], show_progress="hidden")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
demo.launch()
|