Drawing a bounding box
Having prepared the image and bounding box tensors, you can now draw the box on top of the image and visually inspect its accuracy.
torch, torchvision,torchvision.transforms have been imported. Image has been already transformed to tensors as image_tensor. The coordinates have been assigned to the variables: x_min, y_min, x_max, y_max.
Questo esercizio fa parte del corso
Deep Learning for Images with PyTorch
Istruzioni dell'esercizio
- Import
draw_bounding_boxesfromtorchvision.utils. - Define the bounding box
bboxas list consisting ofx_min,y_min,x_max, andy_max. - Pass
image_tensorandbbox_tensortodraw_bounding_boxesto draw the box on top of the image and assign the output toimg_bbox. - Convert the
img_bboxtensor to image.
Esercizio pratico interattivo
Prova a risolvere questo esercizio completando il codice di esempio.
# 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()