绘制边界框
在准备好图像和边界框张量后,您现在可以把框绘制到图像上,并直观检查其准确性。
已导入 torch、torchvision、torchvision.transforms。图像已转换为名为 image_tensor 的张量。坐标已赋值给变量:x_min、y_min、x_max、y_max。
本练习是课程的一部分
使用 PyTorch 进行图像深度学习
练习说明
- 从
torchvision.utils导入draw_bounding_boxes。 - 将边界框
bbox定义为由x_min、y_min、x_max和y_max组成的列表。 - 将
image_tensor和bbox_tensor传入draw_bounding_boxes,将框绘制到图像上,并把输出赋给img_bbox。 - 将
img_bbox张量转换为图像。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Import draw_bounding_boxes
____
# Define the bounding box coordinates
bbox = ____
bbox_tensor = torch.tensor(bbox).unsqueeze(0)
# Implement draw_bounding_boxes
img_bbox = ____(____, ____, width=3, colors="red")
# Tranform tensors to image
transform = transforms.Compose([
____
])
plt.imshow(transform(img_bbox))
plt.show()