问题背景: 封装yolov5的时候,在预测阶段有一个函数如下:
for i, det in enumerate(pred):
# gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh
if webcam: # batch_size >= 1
p, s, im0 = Path(path[i]), '%g: ' % i, im0s[i].copy()
else:
p, s, im0 = Path(path), '', im0s
if len(det):
# Rescale boxes from img_size to im0 size
# im0是原图, img是经过letterbox() 函数后的图
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round() # 只转换坐标所以是[:, :4]
pred原始值如下:
经过scale_coords()函数处理后的结果如下:
可以看到scale_coords()函数处理后修改了原来的值,所以如果你需要pred做其他处理,这里要留意。