Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Deep Learning for Images with PyTorch

Cursus bekijken

Oefeninstructies

  • Import draw_bounding_boxes from torchvision.utils.
  • Define the bounding box bbox as list consisting of x_min, y_min, x_max, and y_max.
  • Pass image_tensor and bbox_tensor to draw_bounding_boxes to draw the box on top of the image and assign the output to img_bbox.
  • Convert the img_bbox tensor to image.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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()
Code bewerken en uitvoeren