Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,21 +3,31 @@ 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
if image is None: return None, None, None, last_trigger_time
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|
|
@@ -28,14 +38,20 @@ def detect(image, last_trigger_time):
|
|
| 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(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
demo.launch()
|
|
|
|
| 3 |
import mediapipe as mp
|
| 4 |
import time
|
| 5 |
|
|
|
|
| 6 |
mp_hands = mp.solutions.hands
|
| 7 |
mp_drawing = mp.solutions.drawing_utils
|
|
|
|
| 8 |
|
| 9 |
+
# OPTIMASI 1: Gunakan model_complexity=0 (Mode paling ringan dan cepat)
|
| 10 |
+
hands_detector = mp_hands.Hands(
|
| 11 |
+
model_complexity=0,
|
| 12 |
+
min_detection_confidence=0.5,
|
| 13 |
+
min_tracking_confidence=0.5
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
def detect_optimized(image, last_trigger_time):
|
| 17 |
if image is None: return None, None, None, last_trigger_time
|
| 18 |
|
| 19 |
+
# OPTIMASI 2: Perkecil gambar khusus untuk "dilihat" oleh AI
|
| 20 |
+
# Proses gambar ukuran 320x240 jauh lebih ringan untuk CPU gratisan
|
| 21 |
+
small_image = cv2.resize(image, (320, 240))
|
| 22 |
+
rgb_small = cv2.cvtColor(small_image, cv2.COLOR_BGR2RGB)
|
| 23 |
+
|
| 24 |
results = hands_detector.process(rgb_small)
|
| 25 |
|
| 26 |
hand_detected = False
|
| 27 |
if results.multi_hand_landmarks:
|
| 28 |
hand_detected = True
|
| 29 |
for res in results.multi_hand_landmarks:
|
| 30 |
+
# Garis kerangka tetap digambar di gambar ASLI agar tetap HD dilihat pengguna
|
| 31 |
mp_drawing.draw_landmarks(image, res, mp_hands.HAND_CONNECTIONS)
|
| 32 |
|
| 33 |
curr = time.time()
|
|
|
|
| 38 |
return image, gif, audio, last_trigger_time
|
| 39 |
|
| 40 |
with gr.Blocks() as demo:
|
| 41 |
+
gr.Markdown("# ✋ Hand Detector (Speed Optimized)")
|
| 42 |
t_state = gr.State(0)
|
| 43 |
with gr.Row():
|
| 44 |
cam = gr.Image(sources=["webcam"], streaming=True)
|
| 45 |
with gr.Column():
|
| 46 |
+
out_cam = gr.Image(label="Live Monitor")
|
| 47 |
+
out_gif = gr.Image(label="Respon Visual")
|
| 48 |
out_aud = gr.Audio(autoplay=True)
|
| 49 |
|
| 50 |
+
cam.stream(
|
| 51 |
+
fn=detect_optimized,
|
| 52 |
+
inputs=[cam, t_state],
|
| 53 |
+
outputs=[out_cam, out_gif, out_aud, t_state],
|
| 54 |
+
show_progress="hidden"
|
| 55 |
+
)
|
| 56 |
|
| 57 |
demo.launch()
|