Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,8 +7,13 @@ import gradio as gr
|
|
| 7 |
# Muat model pre-trained YOLOv5
|
| 8 |
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
|
| 9 |
|
| 10 |
-
# Fungsi untuk
|
| 11 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# Buka video input
|
| 13 |
cap = cv2.VideoCapture(video_path)
|
| 14 |
|
|
@@ -18,14 +23,11 @@ def count_people_in_video(video_path):
|
|
| 18 |
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 19 |
|
| 20 |
# Buat VideoWriter untuk menyimpan video output
|
| 21 |
-
output_dir = "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 |
-
|
| 28 |
-
|
| 29 |
while cap.isOpened():
|
| 30 |
ret, frame = cap.read()
|
| 31 |
if not ret:
|
|
@@ -39,34 +41,39 @@ def count_people_in_video(video_path):
|
|
| 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 |
-
|
| 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: {
|
| 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 |
-
|
|
|
|
|
|
|
| 60 |
def gradio_interface(video_file):
|
| 61 |
-
output_path,
|
| 62 |
-
return output_path, f"Total people detected: {
|
| 63 |
|
| 64 |
# Antarmuka Gradio
|
| 65 |
iface = gr.Interface(
|
| 66 |
fn=gradio_interface,
|
| 67 |
-
inputs=gr.File(type="filepath"),
|
| 68 |
-
outputs=[gr.File(), gr.Text()],
|
|
|
|
|
|
|
| 69 |
)
|
| 70 |
|
| 71 |
# Menjalankan aplikasi
|
| 72 |
-
|
|
|
|
|
|
| 7 |
# Muat model pre-trained YOLOv5
|
| 8 |
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
|
| 9 |
|
| 10 |
+
# Fungsi untuk memproses video dan menghitung jumlah manusia
|
| 11 |
+
def process_video(video_path):
|
| 12 |
+
# Direktori output
|
| 13 |
+
output_dir = "output_videos"
|
| 14 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 15 |
+
output_path = os.path.join(output_dir, "person_counter_output.mp4")
|
| 16 |
+
|
| 17 |
# Buka video input
|
| 18 |
cap = cv2.VideoCapture(video_path)
|
| 19 |
|
|
|
|
| 23 |
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 24 |
|
| 25 |
# Buat VideoWriter untuk menyimpan video output
|
|
|
|
|
|
|
|
|
|
| 26 |
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
| 27 |
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
|
| 28 |
|
| 29 |
+
total_person_count = 0
|
| 30 |
+
|
| 31 |
while cap.isOpened():
|
| 32 |
ret, frame = cap.read()
|
| 33 |
if not ret:
|
|
|
|
| 41 |
# Filter hanya label 'person'
|
| 42 |
person_detections = [d for d in detections if names[int(d[-1])] == "person"]
|
| 43 |
person_count = len(person_detections)
|
| 44 |
+
total_person_count += person_count
|
| 45 |
|
| 46 |
# Render frame dan buat salinan eksplisit
|
| 47 |
annotated_frame = results.render()[0]
|
| 48 |
annotated_frame = np.copy(annotated_frame)
|
| 49 |
|
| 50 |
# Tambahkan teks ke frame
|
| 51 |
+
cv2.putText(annotated_frame, f"Person Count: {person_count}", (10, 30),
|
| 52 |
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
| 53 |
|
| 54 |
# Tulis frame yang telah dianotasi ke video output
|
| 55 |
out.write(annotated_frame)
|
| 56 |
|
| 57 |
+
# Tutup video input dan output
|
| 58 |
cap.release()
|
| 59 |
out.release()
|
|
|
|
| 60 |
|
| 61 |
+
return output_path, total_person_count
|
| 62 |
+
|
| 63 |
+
# Fungsi Gradio untuk antarmuka
|
| 64 |
def gradio_interface(video_file):
|
| 65 |
+
output_path, total_person_count = process_video(video_file)
|
| 66 |
+
return output_path, f"Total people detected: {total_person_count}"
|
| 67 |
|
| 68 |
# Antarmuka Gradio
|
| 69 |
iface = gr.Interface(
|
| 70 |
fn=gradio_interface,
|
| 71 |
+
inputs=gr.File(type="filepath"), # Input berupa file video
|
| 72 |
+
outputs=[gr.File(label="Processed Video"), gr.Text(label="Total People Count")],
|
| 73 |
+
title="Person Counter using YOLOv5",
|
| 74 |
+
description="Upload a video file to detect and count the number of people using YOLOv5."
|
| 75 |
)
|
| 76 |
|
| 77 |
# Menjalankan aplikasi
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
iface.launch()
|