FasterRCNN的预测结果形式为:
>>> predictions = model([input_img_tensor])
[{'boxes': tensor([[419.6865, 170.0683, 536.0842, 493.7452],
[159.0727, 180.3606, 298.8194, 434.4604],
[439.7836, 222.6208, 452.0138, 271.8359],
[444.3562, 224.4628, 456.1511, 265.5336],
[437.7808, 226.5965, 446.2904, 271.2691]], grad_fn=<StackBackward>),
'labels': tensor([ 1, 1, 32, 32, 32]),
'scores': tensor([0.9997, 0.9996, 0.5827, 0.2102, 0.0943], grad_fn=<IndexBackward>)}]
预测的框是以[x1,y1,x2,y2]
格式表示的,其值介于0
和H
以及0
和W
之间。
您可以使用OpenCV的rectangle
函数在图像上叠加边界框。
import cv2
img = cv2.imread('input_iamge.png', cv2.COLOR_BGR2RGB)
for i in range(len(predictions[0]['boxes'])):
x1, x2, x3, x4 = map(int, predictions[0]['boxes'][i].tolist())
print(x1, x2, x3, x4)
image = cv2.rectangle(img, (x1, x2), (x3, x4), (255, 0, 0), 1)
cv2_imshow('img', image)