From 56c0b4fabd8b13407bee51dbc1d322f3a997c9f6 Mon Sep 17 00:00:00 2001 From: yeric1789 <76454253+yeric1789@users.noreply.github.com> Date: Fri, 21 May 2021 14:05:51 -0400 Subject: [PATCH 1/2] Changing save_one_box Made to work with other changes to common.py --- utils/general.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/utils/general.py b/utils/general.py index 4b3d4ab3b189..dc0d30c67416 100755 --- a/utils/general.py +++ b/utils/general.py @@ -657,8 +657,9 @@ def apply_classifier(x, model, img, im0): return x -def save_one_box(xyxy, im, file='image.jpg', gain=1.02, pad=10, square=False, BGR=False): - # Save an image crop as {file} with crop size multiplied by {gain} and padded by {pad} pixels +def save_one_box(xyxy, im, file='image.jpg', gain=1.02, pad=10, square=False, BGR=False, write=True): + # Save an image crop as {file} with crop size multiplied by {gain} and padded by {pad} pixels, only if write is True + # Returns cropped image xyxy = torch.tensor(xyxy).view(-1, 4) b = xyxy2xywh(xyxy) # boxes if square: @@ -667,7 +668,9 @@ def save_one_box(xyxy, im, file='image.jpg', gain=1.02, pad=10, square=False, BG xyxy = xywh2xyxy(b).long() clip_coords(xyxy, im.shape) crop = im[int(xyxy[0, 1]):int(xyxy[0, 3]), int(xyxy[0, 0]):int(xyxy[0, 2])] - cv2.imwrite(str(increment_path(file, mkdir=True).with_suffix('.jpg')), crop if BGR else crop[..., ::-1]) + if not BGR: crop = crop[..., ::-1] + if write: cv2.imwrite(str(increment_path(file, mkdir=True).with_suffix('.jpg')), crop) + return crop def increment_path(path, exist_ok=False, sep='', mkdir=False): From 55db42c4b353bb14b68043cad54294f9715cdda3 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Sun, 23 May 2021 15:57:30 +0200 Subject: [PATCH 2/2] PEP8 and single line BGR --- utils/general.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/utils/general.py b/utils/general.py index dc0d30c67416..47eea78eda30 100755 --- a/utils/general.py +++ b/utils/general.py @@ -657,9 +657,8 @@ def apply_classifier(x, model, img, im0): return x -def save_one_box(xyxy, im, file='image.jpg', gain=1.02, pad=10, square=False, BGR=False, write=True): - # Save an image crop as {file} with crop size multiplied by {gain} and padded by {pad} pixels, only if write is True - # Returns cropped image +def save_one_box(xyxy, im, file='image.jpg', gain=1.02, pad=10, square=False, BGR=False, save=True): + # Save image crop as {file} with crop size multiple {gain} and {pad} pixels. Save and/or return crop xyxy = torch.tensor(xyxy).view(-1, 4) b = xyxy2xywh(xyxy) # boxes if square: @@ -667,9 +666,9 @@ def save_one_box(xyxy, im, file='image.jpg', gain=1.02, pad=10, square=False, BG b[:, 2:] = b[:, 2:] * gain + pad # box wh * gain + pad xyxy = xywh2xyxy(b).long() clip_coords(xyxy, im.shape) - crop = im[int(xyxy[0, 1]):int(xyxy[0, 3]), int(xyxy[0, 0]):int(xyxy[0, 2])] - if not BGR: crop = crop[..., ::-1] - if write: cv2.imwrite(str(increment_path(file, mkdir=True).with_suffix('.jpg')), crop) + crop = im[int(xyxy[0, 1]):int(xyxy[0, 3]), int(xyxy[0, 0]):int(xyxy[0, 2]), ::(1 if BGR else -1)] + if save: + cv2.imwrite(str(increment_path(file, mkdir=True).with_suffix('.jpg')), crop) return crop