Skip to content

Commit 4ad653c

Browse files
committed
Making detector.py robust to other yolov5 versions
1 parent 910b526 commit 4ad653c

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

speciesnet/detector.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@
3030
import torch.backends.mps
3131
from yolov5.utils.augmentations import letterbox as yolov5_letterbox
3232
from yolov5.utils.general import non_max_suppression as yolov5_non_max_suppression
33-
from yolov5.utils.general import scale_boxes as yolov5_scale_boxes
3433
from yolov5.utils.general import xyxy2xywhn as yolov5_xyxy2xywhn
3534

35+
try:
36+
from yolov5.utils.general import scale_boxes as yolov5_scale_boxes
37+
except ImportError:
38+
from yolov5.utils.general import scale_coords as yolov5_scale_boxes
39+
3640
from speciesnet.constants import Detection
3741
from speciesnet.constants import Failure
3842
from speciesnet.utils import ModelInfo
@@ -196,7 +200,18 @@ def predict(
196200
).round()
197201
for result in results: # (x_min, y_min, x_max, y_max, conf, category)
198202
xyxy = result[:4]
203+
204+
# We want to support multiple versions of xyxy2xywhn, some of which
205+
# are agnostic to input dimensionality (so can support 1D or 2D arrays),
206+
# some of which require 2D input. To support both cases, we pass xyxy as
207+
# a 2D array, and convert back to 1D if necessary.
208+
ndims = xyxy.ndim
209+
if ndims == 1:
210+
xyxy = xyxy[None, :]
199211
xywhn = yolov5_xyxy2xywhn(xyxy, w=img.orig_width, h=img.orig_height)
212+
if ndims == 1:
213+
xywhn = xywhn[0]
214+
200215
bbox = self._convert_yolo_xywhn_to_md_xywhn(xywhn.tolist())
201216

202217
conf = result[4].item()

0 commit comments

Comments
 (0)