Update app.py
Browse files
app.py
CHANGED
@@ -2,43 +2,65 @@ import gradio as gr
|
|
2 |
from roboflow import Roboflow
|
3 |
import tempfile
|
4 |
import os
|
|
|
|
|
|
|
5 |
|
6 |
# Inisialisasi Roboflow
|
7 |
rf = Roboflow(api_key="Otg64Ra6wNOgDyjuhMYU")
|
8 |
project = rf.workspace("alat-pelindung-diri").project("nescafe-4base")
|
9 |
model = project.version(16).model
|
10 |
|
11 |
-
# Fungsi untuk
|
12 |
def detect_objects(image):
|
13 |
-
#
|
14 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
|
15 |
image.save(temp_file, format="JPEG")
|
16 |
temp_file_path = temp_file.name
|
17 |
|
18 |
-
#
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
# Menghitung jumlah objek per kelas
|
22 |
class_count = {}
|
23 |
-
for
|
24 |
-
class_name =
|
25 |
if class_name in class_count:
|
26 |
class_count[class_name] += 1
|
27 |
else:
|
28 |
class_count[class_name] = 1
|
29 |
|
30 |
-
#
|
31 |
result_text = "Jumlah objek per kelas:\n"
|
32 |
for class_name, count in class_count.items():
|
33 |
result_text += f"{class_name}: {count} objek\n"
|
34 |
-
|
35 |
-
|
36 |
-
output_image = model.predict(temp_file_path, confidence=50, overlap=30).save("/tmp/prediction.jpg")
|
37 |
-
|
38 |
-
# Hapus file sementara setelah prediksi
|
39 |
-
os.remove(temp_file_path)
|
40 |
-
|
41 |
-
return "/tmp/prediction.jpg", result_text
|
42 |
|
43 |
# Membuat antarmuka Gradio
|
44 |
iface = gr.Interface(
|
|
|
2 |
from roboflow import Roboflow
|
3 |
import tempfile
|
4 |
import os
|
5 |
+
import cv2
|
6 |
+
import supervision as sv
|
7 |
+
import numpy as np
|
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 dengan supervision InferenceSlicer
|
15 |
def detect_objects(image):
|
16 |
+
# Simpan 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 |
+
# Membaca gambar dengan OpenCV
|
22 |
+
img = cv2.imread(temp_file_path)
|
23 |
+
|
24 |
+
# Callback function untuk model prediksi
|
25 |
+
def callback(image_slice: np.ndarray) -> sv.Detections:
|
26 |
+
# Lakukan inferensi pada setiap potongan gambar
|
27 |
+
predictions = model.predict(image_slice, confidence=50, overlap=30).json()
|
28 |
+
return sv.Detections.from_inference(predictions)
|
29 |
+
|
30 |
+
# Menggunakan InferenceSlicer
|
31 |
+
slicer = sv.InferenceSlicer(callback=callback)
|
32 |
+
|
33 |
+
# Proses gambar dengan slicer
|
34 |
+
detections = slicer(img)
|
35 |
+
|
36 |
+
# Filter deteksi yang tumpang tindih (gunakan NMM atau tanpa filter)
|
37 |
+
filtered_detections = detections.filter(strategy=sv.OverlapFilter.NON_MAX_MERGE, iou_threshold=0.5)
|
38 |
+
|
39 |
+
# Annotasi gambar dengan deteksi
|
40 |
+
annotated_image = sv.BoxAnnotator().annotate(scene=img.copy(), detections=filtered_detections)
|
41 |
+
|
42 |
+
# Simpan gambar dengan prediksi
|
43 |
+
output_path = "/tmp/prediction.jpg"
|
44 |
+
cv2.imwrite(output_path, annotated_image)
|
45 |
+
|
46 |
+
# Hapus file sementara
|
47 |
+
os.remove(temp_file_path)
|
48 |
+
|
49 |
# Menghitung jumlah objek per kelas
|
50 |
class_count = {}
|
51 |
+
for detection in filtered_detections:
|
52 |
+
class_name = detection.class_name
|
53 |
if class_name in class_count:
|
54 |
class_count[class_name] += 1
|
55 |
else:
|
56 |
class_count[class_name] = 1
|
57 |
|
58 |
+
# Hasil perhitungan objek
|
59 |
result_text = "Jumlah objek per kelas:\n"
|
60 |
for class_name, count in class_count.items():
|
61 |
result_text += f"{class_name}: {count} objek\n"
|
62 |
+
|
63 |
+
return output_path, result_text
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
# Membuat antarmuka Gradio
|
66 |
iface = gr.Interface(
|