Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,12 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
-
import mediapipe as mp
|
| 4 |
import numpy as np
|
| 5 |
import time
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
face_detection = mp_face_detection.FaceDetection(model_selection=0, min_detection_confidence=0.5)
|
| 10 |
|
| 11 |
-
#
|
| 12 |
AUDIO_FILE = "suara.mp3"
|
| 13 |
GIF_FILE = "animasi.gif"
|
| 14 |
|
|
@@ -16,45 +14,32 @@ def detect_logic(image, prev_frame, last_trigger_time):
|
|
| 16 |
if image is None:
|
| 17 |
return None, None, None, prev_frame, last_trigger_time
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
results = face_detection.process(image_rgb)
|
| 23 |
|
| 24 |
-
face_detected =
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
# Gambar kotak di wajah (opsional)
|
| 28 |
-
for detection in results.detections:
|
| 29 |
-
bbox = detection.location_data.relative_bounding_box
|
| 30 |
-
h, w, c = image.shape
|
| 31 |
-
x, y, bw, bh = int(bbox.xmin * w), int(bbox.ymin * h), int(bbox.width * w), int(bbox.height * h)
|
| 32 |
-
cv2.rectangle(image, (x, y), (x + bw, y + bh), (0, 255, 0), 2)
|
| 33 |
|
| 34 |
-
#
|
| 35 |
motion_detected = False
|
| 36 |
-
|
| 37 |
-
gray = cv2.GaussianBlur(gray, (21, 21), 0)
|
| 38 |
|
| 39 |
if prev_frame is not None:
|
| 40 |
-
frame_delta = cv2.absdiff(prev_frame,
|
| 41 |
thresh = cv2.threshold(frame_delta, 25, 255, cv2.THRESH_BINARY)[1]
|
| 42 |
-
thresh = cv2.dilate(thresh, None, iterations=2)
|
| 43 |
-
|
| 44 |
-
# Hitung berapa banyak pixel yang berubah
|
| 45 |
motion_score = np.sum(thresh) / 255
|
| 46 |
-
if motion_score > 500:
|
| 47 |
motion_detected = True
|
| 48 |
|
| 49 |
-
|
| 50 |
-
prev_frame = gray
|
| 51 |
|
| 52 |
-
#
|
| 53 |
current_time = time.time()
|
| 54 |
show_gif = None
|
| 55 |
play_audio = None
|
| 56 |
|
| 57 |
-
# Jika ada wajah ATAU gerakan, dan sudah lewat cooldown 3 detik
|
| 58 |
if (face_detected or motion_detected) and (current_time - last_trigger_time > 3):
|
| 59 |
show_gif = GIF_FILE
|
| 60 |
play_audio = AUDIO_FILE
|
|
@@ -62,24 +47,18 @@ def detect_logic(image, prev_frame, last_trigger_time):
|
|
| 62 |
|
| 63 |
return image, show_gif, play_audio, prev_frame, last_trigger_time
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
gr.Markdown("# 🤖 AI Detector: Wajah & Gerakan")
|
| 68 |
-
|
| 69 |
-
# State untuk menyimpan data antar frame
|
| 70 |
state_prev_frame = gr.State(None)
|
| 71 |
state_last_time = gr.State(0)
|
| 72 |
|
| 73 |
with gr.Row():
|
|
|
|
| 74 |
with gr.Column():
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
output_cam = gr.Image(label="Monitoring AI")
|
| 79 |
-
output_gif = gr.Image(label="Respon Visual (GIF)")
|
| 80 |
-
output_audio = gr.Audio(label="Respon Suara", autoplay=True)
|
| 81 |
|
| 82 |
-
# Logika streaming
|
| 83 |
input_cam.stream(
|
| 84 |
fn=detect_logic,
|
| 85 |
inputs=[input_cam, state_prev_frame, state_last_time],
|
|
@@ -87,5 +66,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 87 |
show_progress="hidden"
|
| 88 |
)
|
| 89 |
|
| 90 |
-
|
| 91 |
-
demo.launch()
|
|
|
|
| 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 |
|
|
|
|
| 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
|
|
|
|
| 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],
|
|
|
|
| 66 |
show_progress="hidden"
|
| 67 |
)
|
| 68 |
|
| 69 |
+
demo.launch()
|
|
|