wanghaoyang0106 commited on
Commit
b57f83d
1 Parent(s): 6ca3f35

[bug fix] potential problem if img fed to model is in rectangular shape

Browse files
Files changed (2) hide show
  1. utils/datasets.py +2 -2
  2. utils/utils.py +1 -1
utils/datasets.py CHANGED
@@ -679,8 +679,8 @@ def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=True, scale
679
  dw, dh = np.mod(dw, 64), np.mod(dh, 64) # wh padding
680
  elif scaleFill: # stretch
681
  dw, dh = 0.0, 0.0
682
- new_unpad = new_shape
683
- ratio = new_shape[0] / shape[1], new_shape[1] / shape[0] # width, height ratios
684
 
685
  dw /= 2 # divide padding into 2 sides
686
  dh /= 2
 
679
  dw, dh = np.mod(dw, 64), np.mod(dh, 64) # wh padding
680
  elif scaleFill: # stretch
681
  dw, dh = 0.0, 0.0
682
+ new_unpad = (new_shape[1], new_shape[0])
683
+ ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # width, height ratios
684
 
685
  dw /= 2 # divide padding into 2 sides
686
  dh /= 2
utils/utils.py CHANGED
@@ -173,7 +173,7 @@ def xywh2xyxy(x):
173
  def scale_coords(img1_shape, coords, img0_shape, ratio_pad=None):
174
  # Rescale coords (xyxy) from img1_shape to img0_shape
175
  if ratio_pad is None: # calculate from img0_shape
176
- gain = max(img1_shape) / max(img0_shape) # gain = old / new
177
  pad = (img1_shape[1] - img0_shape[1] * gain) / 2, (img1_shape[0] - img0_shape[0] * gain) / 2 # wh padding
178
  else:
179
  gain = ratio_pad[0][0]
 
173
  def scale_coords(img1_shape, coords, img0_shape, ratio_pad=None):
174
  # Rescale coords (xyxy) from img1_shape to img0_shape
175
  if ratio_pad is None: # calculate from img0_shape
176
+ gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1]) # gain = old / new
177
  pad = (img1_shape[1] - img0_shape[1] * gain) / 2, (img1_shape[0] - img0_shape[0] * gain) / 2 # wh padding
178
  else:
179
  gain = ratio_pad[0][0]