Update app.py
Browse files
app.py
CHANGED
@@ -2,54 +2,62 @@ import gradio as gr
|
|
2 |
from roboflow import Roboflow
|
3 |
import tempfile
|
4 |
import os
|
5 |
-
import
|
6 |
-
|
7 |
-
|
8 |
|
9 |
-
# Inisialisasi Roboflow
|
10 |
rf = Roboflow(api_key="Otg64Ra6wNOgDyjuhMYU")
|
11 |
project = rf.workspace("alat-pelindung-diri").project("nescafe-4base")
|
12 |
model = project.version(16).model
|
13 |
|
14 |
-
# Fungsi untuk deteksi objek
|
15 |
def detect_objects(image):
|
16 |
-
#
|
17 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
|
18 |
image.save(temp_file, format="JPEG")
|
19 |
temp_file_path = temp_file.name
|
20 |
|
21 |
-
#
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
#
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
#
|
31 |
-
|
|
|
|
|
|
|
|
|
32 |
|
33 |
-
#
|
34 |
-
|
35 |
|
36 |
-
#
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
# Annotasi gambar dengan deteksi
|
41 |
-
annotated_image = sv.BoxAnnotator().annotate(scene=img.copy(), detections=filtered_detections)
|
42 |
-
|
43 |
-
# Simpan gambar dengan prediksi
|
44 |
-
output_path = "/tmp/prediction.jpg"
|
45 |
-
cv2.imwrite(output_path, annotated_image)
|
46 |
-
|
47 |
-
# Hapus file sementara
|
48 |
-
os.remove(temp_file_path)
|
49 |
|
50 |
# Menghitung jumlah objek per kelas
|
51 |
class_count = {}
|
52 |
-
for detection in
|
53 |
class_name = detection.class_name
|
54 |
if class_name in class_count:
|
55 |
class_count[class_name] += 1
|
@@ -61,7 +69,10 @@ def detect_objects(image):
|
|
61 |
for class_name, count in class_count.items():
|
62 |
result_text += f"{class_name}: {count} objek\n"
|
63 |
|
64 |
-
|
|
|
|
|
|
|
65 |
|
66 |
# Membuat antarmuka Gradio
|
67 |
iface = gr.Interface(
|
|
|
2 |
from roboflow import Roboflow
|
3 |
import tempfile
|
4 |
import os
|
5 |
+
from sahi.slicing import slice_image
|
6 |
+
from sahi import get_sliced_prediction
|
7 |
+
from sahi.postprocess import postprocess_predictions
|
8 |
|
9 |
+
# Inisialisasi Roboflow (for model path)
|
10 |
rf = Roboflow(api_key="Otg64Ra6wNOgDyjuhMYU")
|
11 |
project = rf.workspace("alat-pelindung-diri").project("nescafe-4base")
|
12 |
model = project.version(16).model
|
13 |
|
14 |
+
# Fungsi untuk deteksi objek menggunakan SAHI
|
15 |
def detect_objects(image):
|
16 |
+
# Menyimpan gambar sementara
|
17 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
|
18 |
image.save(temp_file, format="JPEG")
|
19 |
temp_file_path = temp_file.name
|
20 |
|
21 |
+
# Slice gambar menjadi potongan-potongan kecil
|
22 |
+
slice_image_result = slice_image(
|
23 |
+
image=temp_file_path,
|
24 |
+
output_file_name="sliced_image",
|
25 |
+
output_dir="/tmp/sliced/",
|
26 |
+
slice_height=256,
|
27 |
+
slice_width=256,
|
28 |
+
overlap_height_ratio=0.1,
|
29 |
+
overlap_width_ratio=0.1
|
30 |
+
)
|
31 |
|
32 |
+
# Jalankan prediksi pada setiap potongan gambar
|
33 |
+
predictions = get_sliced_prediction(
|
34 |
+
image_path=temp_file_path,
|
35 |
+
model_path="path_to_your_model",
|
36 |
+
model_type="yolov5", # Adjust based on your model
|
37 |
+
slice_height=256,
|
38 |
+
slice_width=256,
|
39 |
+
overlap_height_ratio=0.1,
|
40 |
+
overlap_width_ratio=0.1,
|
41 |
+
model_confidence_threshold=0.25
|
42 |
+
)
|
43 |
|
44 |
+
# Postprocess dan gabungkan hasil prediksi
|
45 |
+
postprocessed_predictions = postprocess_predictions(
|
46 |
+
predictions=predictions,
|
47 |
+
postprocess_type='NMS',
|
48 |
+
iou_threshold=0.5
|
49 |
+
)
|
50 |
|
51 |
+
# Annotate gambar dengan hasil prediksi
|
52 |
+
annotated_image = model.annotate_image_with_predictions(temp_file_path, postprocessed_predictions)
|
53 |
|
54 |
+
# Simpan gambar hasil annotasi
|
55 |
+
output_image_path = "/tmp/prediction.jpg"
|
56 |
+
annotated_image.save(output_image_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
# Menghitung jumlah objek per kelas
|
59 |
class_count = {}
|
60 |
+
for detection in postprocessed_predictions:
|
61 |
class_name = detection.class_name
|
62 |
if class_name in class_count:
|
63 |
class_count[class_name] += 1
|
|
|
69 |
for class_name, count in class_count.items():
|
70 |
result_text += f"{class_name}: {count} objek\n"
|
71 |
|
72 |
+
# Hapus file sementara
|
73 |
+
os.remove(temp_file_path)
|
74 |
+
|
75 |
+
return output_image_path, result_text
|
76 |
|
77 |
# Membuat antarmuka Gradio
|
78 |
iface = gr.Interface(
|