codrizsain commited on
Commit
ec0418a
·
verified ·
1 Parent(s): 9417f62

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -40
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
- # Panggil langsung dari modul utama
8
  mp_hands = mp.solutions.hands
9
  mp_drawing = mp.solutions.drawing_utils
 
10
 
11
- hands_detector = mp_hands.Hands(
12
- static_image_mode=False,
13
- max_num_hands=1,
14
- min_detection_confidence=0.7,
15
- min_tracking_confidence=0.5
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 landmarks in results.multi_hand_landmarks:
29
- mp_drawing.draw_landmarks(image, landmarks, mp_hands.HAND_CONNECTIONS)
30
 
31
- current_time = time.time()
32
- show_gif = None
33
- play_audio = None
34
-
35
- if hand_detected and (current_time - last_trigger_time > 3):
36
- show_gif = "animasi.gif"
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.Markdown("# ✋ Hand Detector Final - Rizki Edition")
44
- state_last_time = gr.State(0)
45
-
46
  with gr.Row():
47
- input_cam = gr.Image(sources=["webcam"], streaming=True)
48
  with gr.Column():
49
- output_cam = gr.Image(label="Monitor")
50
- output_gif = gr.Image(label="Visual")
51
- output_audio = gr.Audio(autoplay=True)
52
 
53
- input_cam.stream(
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()