Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ import gradio as gr
|
|
| 7 |
# Muat model pre-trained YOLOv5
|
| 8 |
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
|
| 9 |
|
| 10 |
-
# Fungsi untuk memproses video
|
| 11 |
def process_video(video_path):
|
| 12 |
# Direktori output
|
| 13 |
output_dir = "output_videos"
|
|
@@ -26,7 +26,6 @@ def process_video(video_path):
|
|
| 26 |
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
| 27 |
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
|
| 28 |
|
| 29 |
-
# Proses video
|
| 30 |
while cap.isOpened():
|
| 31 |
ret, frame = cap.read()
|
| 32 |
if not ret:
|
|
@@ -56,21 +55,55 @@ def process_video(video_path):
|
|
| 56 |
cap.release()
|
| 57 |
out.release()
|
| 58 |
|
| 59 |
-
# Mengembalikan video yang telah diproses (tidak menjumlahkan seluruh frame)
|
| 60 |
return output_path
|
| 61 |
|
| 62 |
-
# Fungsi
|
| 63 |
-
def
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
return output_path
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
# Antarmuka Gradio
|
| 68 |
iface = gr.Interface(
|
| 69 |
fn=gradio_interface,
|
| 70 |
-
inputs=
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
| 72 |
title="Person Counter using YOLOv5",
|
| 73 |
-
description="Upload a video file to detect and count the number of people
|
| 74 |
)
|
| 75 |
|
| 76 |
# Menjalankan aplikasi
|
|
|
|
| 7 |
# Muat model pre-trained YOLOv5
|
| 8 |
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
|
| 9 |
|
| 10 |
+
# Fungsi untuk memproses video
|
| 11 |
def process_video(video_path):
|
| 12 |
# Direktori output
|
| 13 |
output_dir = "output_videos"
|
|
|
|
| 26 |
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
| 27 |
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
|
| 28 |
|
|
|
|
| 29 |
while cap.isOpened():
|
| 30 |
ret, frame = cap.read()
|
| 31 |
if not ret:
|
|
|
|
| 55 |
cap.release()
|
| 56 |
out.release()
|
| 57 |
|
|
|
|
| 58 |
return output_path
|
| 59 |
|
| 60 |
+
# Fungsi untuk memproses gambar
|
| 61 |
+
def process_image(image_path):
|
| 62 |
+
# Baca gambar
|
| 63 |
+
image = cv2.imread(image_path)
|
| 64 |
+
|
| 65 |
+
# Inferensi dengan YOLOv5
|
| 66 |
+
results = model(image)
|
| 67 |
+
detections = results.pred[0]
|
| 68 |
+
names = model.names
|
| 69 |
+
|
| 70 |
+
# Filter hanya label 'person'
|
| 71 |
+
person_detections = [d for d in detections if names[int(d[-1])] == "person"]
|
| 72 |
+
person_count = len(person_detections)
|
| 73 |
+
|
| 74 |
+
# Render frame dan buat salinan eksplisit
|
| 75 |
+
annotated_image = results.render()[0]
|
| 76 |
+
annotated_image = np.copy(annotated_image)
|
| 77 |
+
|
| 78 |
+
# Tambahkan teks ke gambar
|
| 79 |
+
cv2.putText(annotated_image, f"Person Count: {person_count}", (10, 30),
|
| 80 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
| 81 |
+
|
| 82 |
+
# Simpan gambar hasil
|
| 83 |
+
output_dir = "output_images"
|
| 84 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 85 |
+
output_path = os.path.join(output_dir, "person_counter_output.jpg")
|
| 86 |
+
cv2.imwrite(output_path, annotated_image)
|
| 87 |
+
|
| 88 |
return output_path
|
| 89 |
|
| 90 |
+
# Fungsi Gradio untuk antarmuka
|
| 91 |
+
def gradio_interface(file, is_video):
|
| 92 |
+
if is_video:
|
| 93 |
+
return process_video(file)
|
| 94 |
+
else:
|
| 95 |
+
return process_image(file)
|
| 96 |
+
|
| 97 |
# Antarmuka Gradio
|
| 98 |
iface = gr.Interface(
|
| 99 |
fn=gradio_interface,
|
| 100 |
+
inputs=[
|
| 101 |
+
gr.File(type="filepath", label="Upload File (Image/Video)"),
|
| 102 |
+
gr.Checkbox(label="Is Video?", value=True),
|
| 103 |
+
],
|
| 104 |
+
outputs=gr.File(label="Processed File"),
|
| 105 |
title="Person Counter using YOLOv5",
|
| 106 |
+
description="Upload a video or image file to detect and count the number of people using YOLOv5."
|
| 107 |
)
|
| 108 |
|
| 109 |
# Menjalankan aplikasi
|