Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ import tempfile
|
|
5 |
import os
|
6 |
import requests
|
7 |
from sahi.predict import get_sliced_prediction # SAHI slicing inference
|
|
|
8 |
|
9 |
# Muat variabel lingkungan dari file .env
|
10 |
load_dotenv()
|
@@ -30,10 +31,10 @@ def detect_objects(image):
|
|
30 |
result = get_sliced_prediction(
|
31 |
temp_file_path,
|
32 |
model,
|
33 |
-
slice_height=256, # Adjust as needed
|
34 |
-
slice_width=256, # Adjust as needed
|
35 |
-
overlap_height_ratio=0.2, # Adjust as needed
|
36 |
-
overlap_width_ratio=0.2 # Adjust as needed
|
37 |
)
|
38 |
|
39 |
# Menghitung jumlah objek per kelas
|
@@ -56,6 +57,19 @@ def detect_objects(image):
|
|
56 |
result.export_visuals(export_dir="/tmp/") # Export visuals for display
|
57 |
output_image_path = "/tmp/prediction_visual.png" # Assuming the visual output is saved here
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
except requests.exceptions.HTTPError as http_err:
|
60 |
# Menangani kesalahan HTTP
|
61 |
result_text = f"HTTP error occurred: {http_err}"
|
|
|
5 |
import os
|
6 |
import requests
|
7 |
from sahi.predict import get_sliced_prediction # SAHI slicing inference
|
8 |
+
import supervision as sv # For annotating images with results
|
9 |
|
10 |
# Muat variabel lingkungan dari file .env
|
11 |
load_dotenv()
|
|
|
31 |
result = get_sliced_prediction(
|
32 |
temp_file_path,
|
33 |
model,
|
34 |
+
slice_height=256, # Adjust slice height as needed
|
35 |
+
slice_width=256, # Adjust slice width as needed
|
36 |
+
overlap_height_ratio=0.2, # Adjust overlap height ratio as needed
|
37 |
+
overlap_width_ratio=0.2 # Adjust overlap width ratio as needed
|
38 |
)
|
39 |
|
40 |
# Menghitung jumlah objek per kelas
|
|
|
57 |
result.export_visuals(export_dir="/tmp/") # Export visuals for display
|
58 |
output_image_path = "/tmp/prediction_visual.png" # Assuming the visual output is saved here
|
59 |
|
60 |
+
# Annotating the image with the detections (optional)
|
61 |
+
label_annotator = sv.LabelAnnotator()
|
62 |
+
box_annotator = sv.BoxAnnotator()
|
63 |
+
|
64 |
+
annotated_image = box_annotator.annotate(
|
65 |
+
scene=image.copy(), detections=result.object_prediction_list)
|
66 |
+
|
67 |
+
annotated_image = label_annotator.annotate(
|
68 |
+
scene=annotated_image, detections=result.object_prediction_list)
|
69 |
+
|
70 |
+
# Save the annotated image
|
71 |
+
annotated_image.save(output_image_path)
|
72 |
+
|
73 |
except requests.exceptions.HTTPError as http_err:
|
74 |
# Menangani kesalahan HTTP
|
75 |
result_text = f"HTTP error occurred: {http_err}"
|