codrizsain commited on
Commit
c3cabfc
·
verified ·
1 Parent(s): 60914cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -35
app.py CHANGED
@@ -1,68 +1,66 @@
1
  import gradio as gr
2
  import cv2
3
- import numpy as np
4
  import time
5
 
6
- # Load detektor wajah bawaan OpenCV
7
- face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
 
 
 
 
 
 
 
8
 
9
- # Aset tetap sama
10
  AUDIO_FILE = "suara.mp3"
11
  GIF_FILE = "animasi.gif"
12
 
13
- def detect_logic(image, prev_frame, last_trigger_time):
14
  if image is None:
15
- return None, None, None, prev_frame, last_trigger_time
16
 
17
- # 1. Deteksi Wajah (OpenCV Haar Cascade)
18
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
19
- faces = face_cascade.detectMultiScale(gray, 1.3, 5)
20
 
21
- face_detected = len(faces) > 0
22
- for (x, y, w, h) in faces:
23
- cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)
 
 
 
24
 
25
- # 2. Deteksi Gerakan (Frame Differencing)
26
- motion_detected = False
27
- gray_blur = cv2.GaussianBlur(gray, (21, 21), 0)
28
-
29
- if prev_frame is not None:
30
- frame_delta = cv2.absdiff(prev_frame, gray_blur)
31
- thresh = cv2.threshold(frame_delta, 25, 255, cv2.THRESH_BINARY)[1]
32
- motion_score = np.sum(thresh) / 255
33
- if motion_score > 500:
34
- motion_detected = True
35
-
36
- prev_frame = gray_blur
37
-
38
- # 3. Logika Trigger
39
  current_time = time.time()
40
  show_gif = None
41
  play_audio = None
42
 
43
- if (face_detected or motion_detected) and (current_time - last_trigger_time > 3):
 
44
  show_gif = GIF_FILE
45
  play_audio = AUDIO_FILE
46
  last_trigger_time = current_time
47
 
48
- return image, show_gif, play_audio, prev_frame, last_trigger_time
49
 
 
50
  with gr.Blocks() as demo:
51
- gr.Markdown("# 🛡️ Detektor Stabil v2")
52
- state_prev_frame = gr.State(None)
53
  state_last_time = gr.State(0)
54
 
55
  with gr.Row():
56
  input_cam = gr.Image(sources=["webcam"], streaming=True)
57
  with gr.Column():
58
- output_cam = gr.Image(label="Hasil")
59
- output_gif = gr.Image(label="Respon")
60
  output_audio = gr.Audio(autoplay=True)
61
 
62
  input_cam.stream(
63
- fn=detect_logic,
64
- inputs=[input_cam, state_prev_frame, state_last_time],
65
- outputs=[output_cam, output_gif, output_audio, state_prev_frame, state_last_time],
66
  show_progress="hidden"
67
  )
68
 
 
1
  import gradio as gr
2
  import cv2
3
+ import mediapipe as mp
4
  import time
5
 
6
+ # Inisialisasi MediaPipe Hands
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_draw = mp.solutions.drawing_utils
15
 
16
+ # File aset kamu
17
  AUDIO_FILE = "suara.mp3"
18
  GIF_FILE = "animasi.gif"
19
 
20
+ def detect_hand(image, last_trigger_time):
21
  if image is None:
22
+ return None, None, None, last_trigger_time
23
 
24
+ # Deteksi Tangan
25
+ image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
26
+ results = hands.process(image_rgb)
27
 
28
+ hand_detected = False
29
+ if results.multi_hand_landmarks:
30
+ hand_detected = True
31
+ # Gambar kerangka tangan di monitor
32
+ for hand_landmarks in results.multi_hand_landmarks:
33
+ mp_draw.draw_landmarks(image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
34
 
35
+ # Logika Trigger Suara & GIF
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  current_time = time.time()
37
  show_gif = None
38
  play_audio = None
39
 
40
+ # Jika tangan muncul & sudah lewat cooldown 3 detik agar suara tidak berisik
41
+ if hand_detected and (current_time - last_trigger_time > 3):
42
  show_gif = GIF_FILE
43
  play_audio = AUDIO_FILE
44
  last_trigger_time = current_time
45
 
46
+ return image, show_gif, play_audio, last_trigger_time
47
 
48
+ # Antarmuka UI
49
  with gr.Blocks() as demo:
50
+ gr.Markdown("## ✋ Detektor Gerakan Tangan")
 
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="Monitor AI")
57
+ output_gif = gr.Image(label="Respon 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"
65
  )
66