(img, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, auto_size=32)
| 1105 | |
| 1106 | |
| 1107 | def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, auto_size=32): |
| 1108 | # Resize image to a 32-pixel-multiple rectangle https://github.com/ultralytics/yolov3/issues/232 |
| 1109 | shape = img.shape[:2] # current shape [height, width] |
| 1110 | if isinstance(new_shape, int): |
| 1111 | new_shape = (new_shape, new_shape) |
| 1112 | |
| 1113 | # Scale ratio (new / old) |
| 1114 | r = min(new_shape[0] / shape[0], new_shape[1] / shape[1]) |
| 1115 | if not scaleup: # only scale down, do not scale up (for better test mAP) |
| 1116 | r = min(r, 1.0) |
| 1117 | |
| 1118 | # Compute padding |
| 1119 | ratio = r, r # width, height ratios |
| 1120 | new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r)) |
| 1121 | dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding |
| 1122 | if auto: # minimum rectangle |
| 1123 | dw, dh = np.mod(dw, auto_size), np.mod(dh, auto_size) # wh padding |
| 1124 | elif scaleFill: # stretch |
| 1125 | dw, dh = 0.0, 0.0 |
| 1126 | new_unpad = (new_shape[1], new_shape[0]) |
| 1127 | ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # width, height ratios |
| 1128 | |
| 1129 | dw /= 2 # divide padding into 2 sides |
| 1130 | dh /= 2 |
| 1131 | |
| 1132 | if shape[::-1] != new_unpad: # resize |
| 1133 | img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR) |
| 1134 | top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1)) |
| 1135 | left, right = int(round(dw - 0.1)), int(round(dw + 0.1)) |
| 1136 | img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border |
| 1137 | return img, ratio, (dw, dh) |
| 1138 | |
| 1139 | |
| 1140 | def random_perspective(img, targets=(), degrees=10, translate=.1, scale=.1, shear=10, perspective=0.0, border=(0, 0)): |
no outgoing calls
no test coverage detected