codrizsain commited on
Commit
094c129
·
verified ·
1 Parent(s): 85e6bbc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -7
app.py CHANGED
@@ -1,17 +1,24 @@
1
  import gradio as gr
2
  import cv2
3
- import mediapipe as mp
4
  import time
5
 
6
- # Gunakan inisialisasi yang lebih spesifik untuk tangan
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_drawing = mp.solutions.drawing_utils
15
 
16
  AUDIO_FILE = "suara.mp3"
17
  GIF_FILE = "animasi.gif"
@@ -22,7 +29,7 @@ def detect_hand_only(image, last_trigger_time):
22
 
23
  # Deteksi
24
  image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
25
- results = hands.process(image_rgb)
26
 
27
  hand_detected = False
28
  if results.multi_hand_landmarks:
@@ -34,6 +41,7 @@ def detect_hand_only(image, last_trigger_time):
34
  show_gif = None
35
  play_audio = None
36
 
 
37
  if hand_detected and (current_time - last_trigger_time > 3):
38
  show_gif = GIF_FILE
39
  play_audio = AUDIO_FILE
@@ -42,7 +50,7 @@ def detect_hand_only(image, last_trigger_time):
42
  return image, show_gif, play_audio, last_trigger_time
43
 
44
  with gr.Blocks() as demo:
45
- gr.Markdown("## ✋ Detektor Tangan v3 (Stable)")
46
  state_last_time = gr.State(0)
47
 
48
  with gr.Row():
 
1
  import gradio as gr
2
  import cv2
3
+ import numpy as np
4
  import time
5
 
6
+ # Import langsung ke sub-modul untuk menghindari AttributeError
7
+ try:
8
+ from mediapipe.python.solutions import hands as mp_hands
9
+ from mediapipe.python.solutions import drawing_utils as mp_drawing
10
+ except ImportError:
11
+ import mediapipe as mp
12
+ mp_hands = mp.solutions.hands
13
+ mp_drawing = mp.solutions.drawing_utils
14
+
15
+ # Inisialisasi Detektor Tangan
16
+ hands_detector = mp_hands.Hands(
17
  static_image_mode=False,
18
  max_num_hands=1,
19
  min_detection_confidence=0.7,
20
  min_tracking_confidence=0.5
21
  )
 
22
 
23
  AUDIO_FILE = "suara.mp3"
24
  GIF_FILE = "animasi.gif"
 
29
 
30
  # Deteksi
31
  image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
32
+ results = hands_detector.process(image_rgb)
33
 
34
  hand_detected = False
35
  if results.multi_hand_landmarks:
 
41
  show_gif = None
42
  play_audio = None
43
 
44
+ # Trigger jika tangan terdeteksi (Jeda 3 detik)
45
  if hand_detected and (current_time - last_trigger_time > 3):
46
  show_gif = GIF_FILE
47
  play_audio = AUDIO_FILE
 
50
  return image, show_gif, play_audio, last_trigger_time
51
 
52
  with gr.Blocks() as demo:
53
+ gr.Markdown("## ✋ Detektor Tangan v4 (Anti-Error)")
54
  state_last_time = gr.State(0)
55
 
56
  with gr.Row():