Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import mediapipe as mp
|
| 4 |
+
import numpy as np
|
| 5 |
+
import time
|
| 6 |
+
|
| 7 |
+
# Inisialisasi MediaPipe Face Detection
|
| 8 |
+
mp_face_detection = mp.solutions.face_detection
|
| 9 |
+
face_detection = mp_face_detection.FaceDetection(model_selection=0, min_detection_confidence=0.5)
|
| 10 |
+
|
| 11 |
+
# Path ke aset multimedia (Pastikan Anda sudah upload file dengan nama ini)
|
| 12 |
+
AUDIO_FILE = "suara.mp3"
|
| 13 |
+
GIF_FILE = "animasi.gif"
|
| 14 |
+
|
| 15 |
+
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 |
+
# --- 1. Deteksi Wajah ---
|
| 20 |
+
# Convert BGR ke RGB
|
| 21 |
+
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 22 |
+
results = face_detection.process(image_rgb)
|
| 23 |
+
|
| 24 |
+
face_detected = False
|
| 25 |
+
if results.detections:
|
| 26 |
+
face_detected = True
|
| 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 |
+
# --- 2. Deteksi Gerakan ---
|
| 35 |
+
motion_detected = False
|
| 36 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 37 |
+
gray = cv2.GaussianBlur(gray, (21, 21), 0)
|
| 38 |
+
|
| 39 |
+
if prev_frame is not None:
|
| 40 |
+
frame_delta = cv2.absdiff(prev_frame, gray)
|
| 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: # Batas sensitivitas gerakan
|
| 47 |
+
motion_detected = True
|
| 48 |
+
|
| 49 |
+
# Update frame sebelumnya
|
| 50 |
+
prev_frame = gray
|
| 51 |
+
|
| 52 |
+
# --- 3. Trigger Logika ---
|
| 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
|
| 61 |
+
last_trigger_time = current_time
|
| 62 |
+
|
| 63 |
+
return image, show_gif, play_audio, prev_frame, last_trigger_time
|
| 64 |
+
|
| 65 |
+
# Antarmuka Gradio
|
| 66 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 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 |
+
input_cam = gr.Image(sources=["webcam"], streaming=True, label="Webcam Real-time")
|
| 76 |
+
|
| 77 |
+
with gr.Column():
|
| 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],
|
| 86 |
+
outputs=[output_cam, output_gif, output_audio, state_prev_frame, state_last_time],
|
| 87 |
+
show_progress="hidden"
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
demo.launch()
|
| 92 |
+
|
| 93 |
+
### Langkah Terakhir (Sangat Penting):
|
| 94 |
+
1. **Upload File Aset:** Pastikan Anda mengunggah file `suara.mp3` dan `animasi.gif` ke tab **Files**.
|
| 95 |
+
2. **Commit:** Klik tombol **Commit changes** untuk menyimpan file `app.py`.
|
| 96 |
+
3. **Cek Status:** Perhatikan pojok kanan atas, tunggu sampai statusnya hijau bertuliskan **Running**.
|
| 97 |
+
|
| 98 |
+
Begitu statusnya Running, klik tab **App**, berikan izin akses kamera, dan program Anda akan langsung aktif! Beri tahu saya jika muncul error atau jika Anda ingin mengubah sensitivitas deteksinya.
|