Update app.py
Browse files
app.py
CHANGED
@@ -3,14 +3,44 @@ from roboflow import Roboflow
|
|
3 |
import tempfile
|
4 |
import os
|
5 |
from sahi.slicing import slice_image
|
6 |
-
|
|
|
7 |
|
8 |
# Inisialisasi Roboflow (for model path)
|
9 |
rf = Roboflow(api_key="Otg64Ra6wNOgDyjuhMYU")
|
10 |
project = rf.workspace("alat-pelindung-diri").project("nescafe-4base")
|
11 |
model = project.version(16).model
|
12 |
|
13 |
-
# Fungsi untuk
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
def detect_objects(image):
|
15 |
# Menyimpan gambar sementara
|
16 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
|
@@ -39,12 +69,8 @@ def detect_objects(image):
|
|
39 |
predictions = model.predict(image_path=sliced_image_path).json()
|
40 |
all_predictions.extend(predictions['predictions'])
|
41 |
|
42 |
-
#
|
43 |
-
postprocessed_predictions =
|
44 |
-
predictions=all_predictions,
|
45 |
-
postprocess_type='NMS',
|
46 |
-
iou_threshold=0.5
|
47 |
-
)
|
48 |
|
49 |
# Annotate gambar dengan hasil prediksi
|
50 |
annotated_image = model.annotate_image_with_predictions(temp_file_path, postprocessed_predictions)
|
|
|
3 |
import tempfile
|
4 |
import os
|
5 |
from sahi.slicing import slice_image
|
6 |
+
import numpy as np
|
7 |
+
import cv2
|
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 melakukan Non-Maximum Suppression (NMS)
|
15 |
+
def apply_nms(predictions, iou_threshold=0.5):
|
16 |
+
boxes = []
|
17 |
+
scores = []
|
18 |
+
classes = []
|
19 |
+
|
20 |
+
# Extract boxes, scores, and class info
|
21 |
+
for prediction in predictions:
|
22 |
+
boxes.append(prediction['bbox'])
|
23 |
+
scores.append(prediction['confidence'])
|
24 |
+
classes.append(prediction['class'])
|
25 |
+
|
26 |
+
boxes = np.array(boxes)
|
27 |
+
scores = np.array(scores)
|
28 |
+
classes = np.array(classes)
|
29 |
+
|
30 |
+
# Perform NMS using OpenCV
|
31 |
+
indices = cv2.dnn.NMSBoxes(boxes.tolist(), scores.tolist(), score_threshold=0.25, nms_threshold=iou_threshold)
|
32 |
+
nms_predictions = []
|
33 |
+
|
34 |
+
for i in indices.flatten():
|
35 |
+
nms_predictions.append({
|
36 |
+
'class': classes[i],
|
37 |
+
'bbox': boxes[i],
|
38 |
+
'confidence': scores[i]
|
39 |
+
})
|
40 |
+
|
41 |
+
return nms_predictions
|
42 |
+
|
43 |
+
# Fungsi untuk deteksi objek menggunakan Roboflow Model
|
44 |
def detect_objects(image):
|
45 |
# Menyimpan gambar sementara
|
46 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
|
|
|
69 |
predictions = model.predict(image_path=sliced_image_path).json()
|
70 |
all_predictions.extend(predictions['predictions'])
|
71 |
|
72 |
+
# Aplikasikan NMS untuk menghapus duplikat deteksi
|
73 |
+
postprocessed_predictions = apply_nms(all_predictions, iou_threshold=0.5)
|
|
|
|
|
|
|
|
|
74 |
|
75 |
# Annotate gambar dengan hasil prediksi
|
76 |
annotated_image = model.annotate_image_with_predictions(temp_file_path, postprocessed_predictions)
|