Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,9 @@ import supervision as sv
|
|
5 |
from roboflow import Roboflow
|
6 |
import tempfile
|
7 |
import os
|
8 |
-
import
|
|
|
|
|
9 |
from dotenv import load_dotenv
|
10 |
|
11 |
# Load environment variables from .env file
|
@@ -20,24 +22,37 @@ rf = Roboflow(api_key=api_key)
|
|
20 |
project = rf.workspace(workspace).project(project_name)
|
21 |
model = project.version(model_version).model
|
22 |
|
|
|
|
|
|
|
23 |
def detect_objects(image):
|
24 |
# Save the uploaded image to a temporary file
|
25 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
|
26 |
image.save(temp_file, format="JPEG")
|
27 |
temp_file_path = temp_file.name
|
28 |
-
|
|
|
|
|
|
|
29 |
try:
|
30 |
-
# Perform inference
|
31 |
-
predictions =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
# Initialize Supervision annotations
|
34 |
detections = []
|
35 |
-
for prediction in predictions
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
# Add detection to Supervision Detections list
|
42 |
detections.append(
|
43 |
sv.Detection(
|
@@ -57,9 +72,8 @@ def detect_objects(image):
|
|
57 |
label_annotator = sv.LabelAnnotator()
|
58 |
box_annotator = sv.BoxAnnotator()
|
59 |
|
60 |
-
#
|
61 |
-
|
62 |
-
annotated_image = box_annotator.annotate(scene=image_cv.copy(), detections=detections)
|
63 |
annotated_image = label_annotator.annotate(scene=annotated_image, detections=detections)
|
64 |
|
65 |
# Count detected objects per class
|
@@ -81,9 +95,6 @@ def detect_objects(image):
|
|
81 |
output_image_path = "/tmp/prediction.jpg"
|
82 |
cv2.imwrite(output_image_path, annotated_image)
|
83 |
|
84 |
-
except requests.exceptions.HTTPError as http_err:
|
85 |
-
result_text = f"HTTP error occurred: {http_err}"
|
86 |
-
output_image_path = temp_file_path # Return original image on error
|
87 |
except Exception as err:
|
88 |
result_text = f"An error occurred: {err}"
|
89 |
output_image_path = temp_file_path # Return original image on error
|
|
|
5 |
from roboflow import Roboflow
|
6 |
import tempfile
|
7 |
import os
|
8 |
+
from sahi import AutoDetectionModel
|
9 |
+
from sahi.utils import read_image
|
10 |
+
from sahi.predict import predict
|
11 |
from dotenv import load_dotenv
|
12 |
|
13 |
# Load environment variables from .env file
|
|
|
22 |
project = rf.workspace(workspace).project(project_name)
|
23 |
model = project.version(model_version).model
|
24 |
|
25 |
+
# Initialize SAHI model for inference
|
26 |
+
detection_model = AutoDetectionModel.from_pretrained(model)
|
27 |
+
|
28 |
def detect_objects(image):
|
29 |
# Save the uploaded image to a temporary file
|
30 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
|
31 |
image.save(temp_file, format="JPEG")
|
32 |
temp_file_path = temp_file.name
|
33 |
+
|
34 |
+
# Read the image using OpenCV
|
35 |
+
original_image = cv2.imread(temp_file_path)
|
36 |
+
|
37 |
try:
|
38 |
+
# Perform inference using SAHI Slicer
|
39 |
+
predictions = predict(
|
40 |
+
detection_model=detection_model,
|
41 |
+
image=original_image,
|
42 |
+
slice_height=800, # Height of the slice
|
43 |
+
slice_width=800, # Width of the slice
|
44 |
+
overlap_height_ratio=0.2,
|
45 |
+
overlap_width_ratio=0.2,
|
46 |
+
return_slice_result=False,
|
47 |
+
)
|
48 |
|
49 |
# Initialize Supervision annotations
|
50 |
detections = []
|
51 |
+
for prediction in predictions:
|
52 |
+
bbox = prediction.bbox
|
53 |
+
class_name = prediction.category
|
54 |
+
confidence = prediction.score
|
55 |
+
|
|
|
56 |
# Add detection to Supervision Detections list
|
57 |
detections.append(
|
58 |
sv.Detection(
|
|
|
72 |
label_annotator = sv.LabelAnnotator()
|
73 |
box_annotator = sv.BoxAnnotator()
|
74 |
|
75 |
+
# Annotate and create the final result
|
76 |
+
annotated_image = box_annotator.annotate(scene=original_image.copy(), detections=detections)
|
|
|
77 |
annotated_image = label_annotator.annotate(scene=annotated_image, detections=detections)
|
78 |
|
79 |
# Count detected objects per class
|
|
|
95 |
output_image_path = "/tmp/prediction.jpg"
|
96 |
cv2.imwrite(output_image_path, annotated_image)
|
97 |
|
|
|
|
|
|
|
98 |
except Exception as err:
|
99 |
result_text = f"An error occurred: {err}"
|
100 |
output_image_path = temp_file_path # Return original image on error
|