Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import cv2
|
| 3 |
+
import os
|
| 4 |
+
import numpy as np
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
# Muat model pre-trained YOLOv5
|
| 8 |
+
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
|
| 9 |
+
|
| 10 |
+
# Fungsi untuk deteksi dan menghitung objek manusia
|
| 11 |
+
def count_people_in_video(video_path):
|
| 12 |
+
# Buka video input
|
| 13 |
+
cap = cv2.VideoCapture(video_path)
|
| 14 |
+
|
| 15 |
+
# Dapatkan spesifikasi video
|
| 16 |
+
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 17 |
+
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 18 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 19 |
+
|
| 20 |
+
# Buat VideoWriter untuk menyimpan video output
|
| 21 |
+
output_dir = "/content/output_videos"
|
| 22 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 23 |
+
output_path = os.path.join(output_dir, "person_counter_output.mp4")
|
| 24 |
+
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
| 25 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
|
| 26 |
+
|
| 27 |
+
# Proses video
|
| 28 |
+
person_count_total = 0
|
| 29 |
+
while cap.isOpened():
|
| 30 |
+
ret, frame = cap.read()
|
| 31 |
+
if not ret:
|
| 32 |
+
break
|
| 33 |
+
|
| 34 |
+
# Inferensi dengan YOLOv5
|
| 35 |
+
results = model(frame)
|
| 36 |
+
detections = results.pred[0]
|
| 37 |
+
names = model.names
|
| 38 |
+
|
| 39 |
+
# Filter hanya label 'person'
|
| 40 |
+
person_detections = [d for d in detections if names[int(d[-1])] == "person"]
|
| 41 |
+
person_count = len(person_detections)
|
| 42 |
+
person_count_total += person_count
|
| 43 |
+
|
| 44 |
+
# Render frame dan buat salinan eksplisit
|
| 45 |
+
annotated_frame = results.render()[0]
|
| 46 |
+
annotated_frame = np.copy(annotated_frame)
|
| 47 |
+
|
| 48 |
+
# Tambahkan teks ke frame
|
| 49 |
+
cv2.putText(annotated_frame, f"Person Count: {person_count_total}", (10, 30),
|
| 50 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
| 51 |
+
|
| 52 |
+
# Tulis frame yang telah dianotasi ke video output
|
| 53 |
+
out.write(annotated_frame)
|
| 54 |
+
|
| 55 |
+
cap.release()
|
| 56 |
+
out.release()
|
| 57 |
+
return output_path, person_count_total
|
| 58 |
+
|
| 59 |
+
# Membuat antarmuka Gradio
|
| 60 |
+
def gradio_interface(video):
|
| 61 |
+
output_path, person_count = count_people_in_video(video)
|
| 62 |
+
return output_path, f"Total people detected: {person_count}"
|
| 63 |
+
|
| 64 |
+
# Antarmuka Gradio
|
| 65 |
+
iface = gr.Interface(
|
| 66 |
+
fn=gradio_interface,
|
| 67 |
+
inputs=gr.Video(source="upload", type="filepath"),
|
| 68 |
+
outputs=[gr.File(), gr.Text()],
|
| 69 |
+
live=True
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
# Menjalankan aplikasi
|
| 73 |
+
iface.launch()
|