Spaces:
Sleeping
Sleeping
jovian commited on
Commit ·
ad117f3
1
Parent(s): 7c84458
iou
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ from sahi import AutoDetectionModel
|
|
| 6 |
from PIL import Image
|
| 7 |
import plotly.graph_objects as go
|
| 8 |
import torch
|
| 9 |
-
import spaces
|
| 10 |
|
| 11 |
import os
|
| 12 |
import shutil
|
|
@@ -64,12 +64,11 @@ class Detection:
|
|
| 64 |
yolov8_model_path1 = "./model/train_model.pt" # Update to your model path
|
| 65 |
yolov8_model_path2 = "./model/best_100epochs_latest.pt" # Update to the second model path
|
| 66 |
|
| 67 |
-
# Initialize the AutoDetectionModels
|
| 68 |
self.model1 = AutoDetectionModel.from_pretrained(
|
| 69 |
model_type='yolov8',
|
| 70 |
model_path=yolov8_model_path1,
|
| 71 |
confidence_threshold=0.3,
|
| 72 |
-
device='cuda:0'
|
| 73 |
)
|
| 74 |
|
| 75 |
self.model2 = AutoDetectionModel.from_pretrained(
|
|
@@ -80,7 +79,8 @@ class Detection:
|
|
| 80 |
)
|
| 81 |
|
| 82 |
def detect_from_image(self, image,slice_width_input,slice_height_input,overlap_width_input,overlap_height_input):
|
| 83 |
-
|
|
|
|
| 84 |
results1 = get_sliced_prediction(
|
| 85 |
image=image,
|
| 86 |
detection_model=self.model1,
|
|
@@ -116,10 +116,10 @@ class Detection:
|
|
| 116 |
|
| 117 |
return combined_annotations
|
| 118 |
|
| 119 |
-
def combine_results(self, annotations1, annotations2, iou_threshold=0.
|
| 120 |
"""
|
| 121 |
-
Combine the results of two sets of annotations, keeping
|
| 122 |
-
prediction when IoU between two bounding boxes is above the threshold.
|
| 123 |
|
| 124 |
:param annotations1: COCO annotations from model 1
|
| 125 |
:param annotations2: COCO annotations from model 2
|
|
@@ -143,9 +143,17 @@ class Detection:
|
|
| 143 |
|
| 144 |
iou = box_iou(torch.tensor(box1_array), torch.tensor(box2_array)).item()
|
| 145 |
|
| 146 |
-
#
|
| 147 |
-
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
break
|
| 150 |
|
| 151 |
if keep:
|
|
@@ -153,6 +161,8 @@ class Detection:
|
|
| 153 |
|
| 154 |
return combined
|
| 155 |
|
|
|
|
|
|
|
| 156 |
#-----------------------------------------------------------------------------------------------------------------------
|
| 157 |
|
| 158 |
def draw_annotations(self, image, annotations):
|
|
@@ -320,7 +330,7 @@ def upload_image(image):
|
|
| 320 |
"""Process the uploaded image (if needed) and display it."""
|
| 321 |
return image
|
| 322 |
|
| 323 |
-
@spaces.GPU
|
| 324 |
def apply_detection(image,slice_width_input,slice_height_input,overlap_width_input,overlap_height_input):
|
| 325 |
"""Run object detection on the uploaded image and return the annotated image."""
|
| 326 |
# Convert image from PIL to NumPy array
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
import plotly.graph_objects as go
|
| 8 |
import torch
|
| 9 |
+
#import spaces
|
| 10 |
|
| 11 |
import os
|
| 12 |
import shutil
|
|
|
|
| 64 |
yolov8_model_path1 = "./model/train_model.pt" # Update to your model path
|
| 65 |
yolov8_model_path2 = "./model/best_100epochs_latest.pt" # Update to the second model path
|
| 66 |
|
|
|
|
| 67 |
self.model1 = AutoDetectionModel.from_pretrained(
|
| 68 |
model_type='yolov8',
|
| 69 |
model_path=yolov8_model_path1,
|
| 70 |
confidence_threshold=0.3,
|
| 71 |
+
device='cuda:0'
|
| 72 |
)
|
| 73 |
|
| 74 |
self.model2 = AutoDetectionModel.from_pretrained(
|
|
|
|
| 79 |
)
|
| 80 |
|
| 81 |
def detect_from_image(self, image,slice_width_input,slice_height_input,overlap_width_input,overlap_height_input):
|
| 82 |
+
|
| 83 |
+
|
| 84 |
results1 = get_sliced_prediction(
|
| 85 |
image=image,
|
| 86 |
detection_model=self.model1,
|
|
|
|
| 116 |
|
| 117 |
return combined_annotations
|
| 118 |
|
| 119 |
+
def combine_results(self, annotations1, annotations2, iou_threshold=0.1):
|
| 120 |
"""
|
| 121 |
+
Combine the results of two sets of annotations, keeping the higher-confidence
|
| 122 |
+
prediction only when the IoU between two bounding boxes is above the threshold.
|
| 123 |
|
| 124 |
:param annotations1: COCO annotations from model 1
|
| 125 |
:param annotations2: COCO annotations from model 2
|
|
|
|
| 143 |
|
| 144 |
iou = box_iou(torch.tensor(box1_array), torch.tensor(box2_array)).item()
|
| 145 |
|
| 146 |
+
# Print IoU for debugging
|
| 147 |
+
print(f"IoU {iou:.4f}")
|
| 148 |
+
|
| 149 |
+
# Only check confidence if IoU is above the threshold
|
| 150 |
+
if iou > iou_threshold:
|
| 151 |
+
# Keep the annotation with higher confidence
|
| 152 |
+
if conf2 <= conf1:
|
| 153 |
+
keep = False
|
| 154 |
+
else:
|
| 155 |
+
# Remove the lower-confidence annotation from `combined`
|
| 156 |
+
combined.remove(ann1)
|
| 157 |
break
|
| 158 |
|
| 159 |
if keep:
|
|
|
|
| 161 |
|
| 162 |
return combined
|
| 163 |
|
| 164 |
+
|
| 165 |
+
|
| 166 |
#-----------------------------------------------------------------------------------------------------------------------
|
| 167 |
|
| 168 |
def draw_annotations(self, image, annotations):
|
|
|
|
| 330 |
"""Process the uploaded image (if needed) and display it."""
|
| 331 |
return image
|
| 332 |
|
| 333 |
+
#@spaces.GPU
|
| 334 |
def apply_detection(image,slice_width_input,slice_height_input,overlap_width_input,overlap_height_input):
|
| 335 |
"""Run object detection on the uploaded image and return the annotated image."""
|
| 336 |
# Convert image from PIL to NumPy array
|