最细致讲解yolov8模型推理完整代码--(前处理,后处理) - 博客-中国极客 (chinageek.org)
直接用resize,图片会变形,宽高比会不对
letterbox函数就是把图片弄到想要的大小,保持宽高比,然后少掉的部分,用灰边填充
def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32):# Resize and pad image while meeting stride-multiple constraintsshape = im.shape[:2] # current shape [height, width]if isinstance(new_shape, int):new_shape = (new_shape, new_shape)# Scale ratio (new / old)r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])if not scaleup: # only scale down, do not scale up (for better val mAP)r = min(r, 1.0)# Compute paddingratio = r, r # width, height ratiosnew_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh paddingif auto: # minimum rectangledw, dh = np.mod(dw, stride), np.mod(dh, stride) # wh paddingelif scaleFill: # stretchdw, dh = 0.0, 0.0new_unpad = (new_shape[1], new_shape[0])ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # width, height ratiosdw /= 2 # divide padding into 2 sidesdh /= 2if shape[::-1] != new_unpad: # resizeim = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR)top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))left, right = int(round(dw - 0.1)), int(round(dw + 0.1))im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add borderreturn im, ratio, (dw, dh)
letterbox的源代码中, stride默认是32,有个auto参数要注意,如果auto是true,它会给你最小能被32整除的大小来resize,就算我们给的resize是640*640,它最后给你的图可能是640*512,因为512就能被32整除了,然而onnx模型中,size是固定的640,所以我们就不能auto=True,不然尺寸不对,image_array格式的取[0]
# Padded resize
img_array640 = letterbox(img_array, (640, 640), stride=32,auto=False)[0]
Scale Boxes
用了letter box之后,这个函数的padding参数要是True
scale_box=scale_boxes(img1_shape=im.shape, boxes=xyxy, img0_shape=img0_shape, ratio_pad=None, padding=True, xywh=False)
def scale_boxes(img1_shape, boxes, img0_shape, ratio_pad=None, padding=True, xywh=False):"""Rescales bounding boxes (in the format of xyxy by default) from the shape of the image they were originallyspecified in (img1_shape) to the shape of a different image (img0_shape).Args:img1_shape (tuple): The shape of the image that the bounding boxes are for, in the format of (height, width).boxes (torch.Tensor): the bounding boxes of the objects in the image, in the format of (x1, y1, x2, y2)img0_shape (tuple): the shape of the target image, in the format of (height, width).ratio_pad (tuple): a tuple of (ratio, pad) for scaling the boxes. If not provided, the ratio and pad will becalculated based on the size difference between the two images.padding (bool): If True, assuming the boxes is based on image augmented by yolo style. If False then do regularrescaling.xywh (bool): The box format is xywh or not, default=False.Returns:boxes (torch.Tensor): The scaled bounding boxes, in the format of (x1, y1, x2, y2)"""if ratio_pad is None: # calculate from img0_shapegain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1]) # gain = old / newpad = (round((img1_shape[1] - img0_shape[1] * gain) / 2 - 0.1),round((img1_shape[0] - img0_shape[0] * gain) / 2 - 0.1),) # wh paddingelse:gain = ratio_pad[0][0]pad = ratio_pad[1]if padding:boxes[..., 0] -= pad[0] # x paddingboxes[..., 1] -= pad[1] # y paddingif not xywh:boxes[..., 2] -= pad[0] # x paddingboxes[..., 3] -= pad[1] # y paddingprint(boxes)boxes[..., :4] /= gainreturn clip_boxes_1(boxes, img0_shape)