Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,11 @@
|
|
1 |
import gradio as gr
|
2 |
import tempfile
|
3 |
import os
|
|
|
4 |
from sahi import AutoDetectionModel
|
5 |
from sahi.predict import get_sliced_prediction
|
6 |
from PIL import Image
|
|
|
7 |
|
8 |
# Inisialisasi model deteksi menggunakan SAHI
|
9 |
model_path = "best.pt" # Ganti dengan path model YOLO lokal Anda
|
@@ -40,10 +42,32 @@ def detect_objects(image):
|
|
40 |
class_count = {}
|
41 |
total_count = 0 # Menyimpan total jumlah objek
|
42 |
|
|
|
|
|
|
|
43 |
for prediction in results.object_prediction_list:
|
|
|
44 |
class_name = prediction.category.name # Nama kelas objek
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
# Menyusun output berupa string hasil perhitungan
|
49 |
result_text = "Detected Objects:\n\n"
|
@@ -51,9 +75,10 @@ def detect_objects(image):
|
|
51 |
result_text += f"{class_name}: {count}\n"
|
52 |
result_text += f"\nTotal Objects: {total_count}"
|
53 |
|
54 |
-
#
|
|
|
55 |
output_image_path = "/tmp/prediction.jpg"
|
56 |
-
|
57 |
|
58 |
except Exception as err:
|
59 |
# Menangani kesalahan lain
|
|
|
1 |
import gradio as gr
|
2 |
import tempfile
|
3 |
import os
|
4 |
+
import cv2
|
5 |
from sahi import AutoDetectionModel
|
6 |
from sahi.predict import get_sliced_prediction
|
7 |
from PIL import Image
|
8 |
+
import numpy as np
|
9 |
|
10 |
# Inisialisasi model deteksi menggunakan SAHI
|
11 |
model_path = "best.pt" # Ganti dengan path model YOLO lokal Anda
|
|
|
42 |
class_count = {}
|
43 |
total_count = 0 # Menyimpan total jumlah objek
|
44 |
|
45 |
+
# Menggambar bounding boxes pada gambar
|
46 |
+
output_image = np.array(image) # Convert PIL Image to numpy array for OpenCV processing
|
47 |
+
|
48 |
for prediction in results.object_prediction_list:
|
49 |
+
bbox = prediction.bbox
|
50 |
class_name = prediction.category.name # Nama kelas objek
|
51 |
+
confidence = prediction.score.value # Skor prediksi
|
52 |
+
|
53 |
+
# Hanya gambar bounding box jika skor kepercayaan lebih besar dari threshold
|
54 |
+
if confidence >= confidence_threshold:
|
55 |
+
# Gambar bounding box
|
56 |
+
cv2.rectangle(output_image,
|
57 |
+
(int(bbox.minx), int(bbox.miny)),
|
58 |
+
(int(bbox.maxx), int(bbox.maxy)),
|
59 |
+
(0, 255, 0), 2) # Gambar kotak hijau
|
60 |
+
|
61 |
+
# Gambar label dan skor
|
62 |
+
cv2.putText(output_image,
|
63 |
+
f"{class_name} {confidence:.2f}",
|
64 |
+
(int(bbox.minx), int(bbox.miny) - 10),
|
65 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.9,
|
66 |
+
(0, 255, 0), 2)
|
67 |
+
|
68 |
+
# Hitung jumlah objek per kelas
|
69 |
+
class_count[class_name] = class_count.get(class_name, 0) + 1
|
70 |
+
total_count += 1 # Menambah jumlah objek
|
71 |
|
72 |
# Menyusun output berupa string hasil perhitungan
|
73 |
result_text = "Detected Objects:\n\n"
|
|
|
75 |
result_text += f"{class_name}: {count}\n"
|
76 |
result_text += f"\nTotal Objects: {total_count}"
|
77 |
|
78 |
+
# Convert output_image (numpy array) back to PIL Image to save
|
79 |
+
output_image_pil = Image.fromarray(output_image)
|
80 |
output_image_path = "/tmp/prediction.jpg"
|
81 |
+
output_image_pil.save(output_image_path) # Menyimpan gambar dengan prediksi
|
82 |
|
83 |
except Exception as err:
|
84 |
# Menangani kesalahan lain
|