This commit is contained in:
Glenn Jocher 2019-02-10 20:32:04 +01:00
parent d5b17c93ff
commit e057f52780
2 changed files with 12 additions and 21 deletions

View File

@ -8,6 +8,10 @@ from utils.utils import *
from utils import torch_utils
def unletterbox(img0_shape, letterbox_shape):
return None
def detect(cfg, weights, images, output='output', img_size=416, conf_thres=0.3, nms_thres=0.45,
save_txt=False, save_images=True):
device = torch_utils.select_device()
@ -69,12 +73,10 @@ def detect(cfg, weights, images, output='output', img_size=416, conf_thres=0.3,
for x1, y1, x2, y2, conf, cls_conf, cls_pred in detections:
# Rescale coordinates to original dimensions
box_h = ((y2 - y1) / unpad_h) * im0.shape[0]
box_w = ((x2 - x1) / unpad_w) * im0.shape[1]
y1 = (((y1 - pad_y // 2) / unpad_h) * im0.shape[0]).round()
x1 = (((x1 - pad_x // 2) / unpad_w) * im0.shape[1]).round()
x2 = (x1 + box_w).round()
y2 = (y1 + box_h).round()
y2 = (((y2 - pad_y // 2) / unpad_h) * im0.shape[0]).round()
x2 = (((x2 - pad_x // 2) / unpad_w) * im0.shape[1]).round()
x1, y1, x2, y2 = max(x1, 0), max(y1, 0), max(x2, 0), max(y2, 0)
if save_txt: # Write to file

View File

@ -193,24 +193,13 @@ class load_images_and_labels(): # for training
def letterbox(img, height=416, color=(0, 0, 0)): # resize a rectangular image to a padded square
shape = img.shape[:2] # shape = [height, width]
ratio = float(height) / max(shape) # ratio = old / new
new_shape = [round(shape[0] * ratio), round(shape[1] * ratio)]
dw = height - new_shape[1] # width padding
dh = height - new_shape[0] # height padding
top, bottom = dh // 2, dh - (dh // 2)
left, right = dw // 2, dw - (dw // 2)
img = cv2.resize(img, (new_shape[1], new_shape[0]), interpolation=cv2.INTER_AREA) # resized, no border
return cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color), ratio, dw // 2, dh // 2
new_shape = (round(shape[1] * ratio), round(shape[0] * ratio))
padw = (height - new_shape[0]) // 2 # width padding
padh = (height - new_shape[1]) // 2 # height padding
img = cv2.resize(img, new_shape, interpolation=cv2.INTER_AREA) # resized, no border
img = cv2.copyMakeBorder(img, padh, padh, padw, padw, cv2.BORDER_CONSTANT, value=color) # padded square
return img, ratio, padw, padh
def letterbox_undo(img, height=416, color=(0, 0, 0)): # resize a rectangular image to a padded square
shape = img.shape[:2] # shape = [height, width]
ratio = float(height) / max(shape) # ratio = old / new
new_shape = [round(shape[0] * ratio), round(shape[1] * ratio)]
dw = height - new_shape[1] # width padding
dh = height - new_shape[0] # height padding
top, bottom = dh // 2, dh - (dh // 2)
left, right = dw // 2, dw - (dw // 2)
img = cv2.resize(img, (new_shape[1], new_shape[0]), interpolation=cv2.INTER_AREA) # resized, no border
return cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color), ratio, dw // 2, dh // 2
def random_affine(img, targets=None, degrees=(-10, 10), translate=(.1, .1), scale=(.9, 1.1), shear=(-2, 2),
borderValue=(127.5, 127.5, 127.5)):