codrizsain commited on
Commit
08be7e2
·
verified ·
1 Parent(s): 7da5b39

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -24
app.py CHANGED
@@ -3,21 +3,14 @@ import cv2
3
  import numpy as np
4
  import time
5
 
6
- # --- BLOK IMPORT AMAN ---
7
  try:
8
- # Cara 1: Import langsung dari sub-modul (Paling disarankan)
9
- import mediapipe.solutions.hands as mp_hands
10
- import mediapipe.solutions.drawing_utils as mp_drawing
11
- except (ImportError, AttributeError):
12
- try:
13
- # Cara 2: Import dari folder python (Untuk versi tertentu)
14
- from mediapipe.python.solutions import hands as mp_hands
15
- from mediapipe.python.solutions import drawing_utils as mp_drawing
16
- except (ImportError, AttributeError):
17
- # Cara 3: Import standar
18
- import mediapipe as mp
19
- mp_hands = mp.solutions.hands
20
- mp_drawing = mp.solutions.drawing_utils
21
 
22
  # Inisialisasi Detektor Tangan
23
  hands_detector = mp_hands.Hands(
@@ -27,25 +20,25 @@ hands_detector = mp_hands.Hands(
27
  min_tracking_confidence=0.5
28
  )
29
 
30
- def detect_hand_only(image, last_trigger_time):
31
  if image is None:
32
  return None, None, None, last_trigger_time
33
 
34
- # Konversi Warna
35
- image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
36
- results = hands_detector.process(image_rgb)
37
 
38
  hand_detected = False
39
  if results.multi_hand_landmarks:
40
  hand_detected = True
41
- for hand_landmarks in results.multi_hand_landmarks:
42
- mp_drawing.draw_landmarks(image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
43
 
44
  current_time = time.time()
45
  show_gif = None
46
  play_audio = None
47
 
48
- # Trigger Suara & GIF (Jeda 3 detik)
49
  if hand_detected and (current_time - last_trigger_time > 3):
50
  show_gif = "animasi.gif"
51
  play_audio = "suara.mp3"
@@ -54,18 +47,18 @@ def detect_hand_only(image, last_trigger_time):
54
  return image, show_gif, play_audio, last_trigger_time
55
 
56
  with gr.Blocks() as demo:
57
- gr.Markdown("# ✋ Detektor Tangan Stabil v8")
58
  state_last_time = gr.State(0)
59
 
60
  with gr.Row():
61
  input_cam = gr.Image(sources=["webcam"], streaming=True)
62
  with gr.Column():
63
  output_cam = gr.Image(label="Monitoring")
64
- output_gif = gr.Image(label="Respon Visual")
65
  output_audio = gr.Audio(autoplay=True)
66
 
67
  input_cam.stream(
68
- fn=detect_hand_only,
69
  inputs=[input_cam, state_last_time],
70
  outputs=[output_cam, output_gif, output_audio, state_last_time],
71
  show_progress="hidden"
 
3
  import numpy as np
4
  import time
5
 
6
+ # Pengecekan Import secara paksa
7
  try:
8
+ import mediapipe as mp
9
+ from mediapipe.solutions import hands as mp_hands
10
+ from mediapipe.solutions import drawing_utils as mp_drawing
11
+ print("MediaPipe loaded successfully")
12
+ except ImportError as e:
13
+ print(f"Error loading MediaPipe: {e}")
 
 
 
 
 
 
 
14
 
15
  # Inisialisasi Detektor Tangan
16
  hands_detector = mp_hands.Hands(
 
20
  min_tracking_confidence=0.5
21
  )
22
 
23
+ def detect_hand(image, last_trigger_time):
24
  if image is None:
25
  return None, None, None, last_trigger_time
26
 
27
+ # Proses deteksi
28
+ img_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
29
+ results = hands_detector.process(img_rgb)
30
 
31
  hand_detected = False
32
  if results.multi_hand_landmarks:
33
  hand_detected = True
34
+ for landmarks in results.multi_hand_landmarks:
35
+ mp_drawing.draw_landmarks(image, landmarks, mp_hands.HAND_CONNECTIONS)
36
 
37
  current_time = time.time()
38
  show_gif = None
39
  play_audio = None
40
 
41
+ # Trigger (Jeda 3 detik)
42
  if hand_detected and (current_time - last_trigger_time > 3):
43
  show_gif = "animasi.gif"
44
  play_audio = "suara.mp3"
 
47
  return image, show_gif, play_audio, last_trigger_time
48
 
49
  with gr.Blocks() as demo:
50
+ gr.Markdown("# ✋ Hand Detector - Stable Version")
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="Monitoring")
57
+ output_gif = gr.Image(label="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"