%%capture
!pip install kornia
!pip install kornia-rs matplotlib
Image and Keypoints augmentations
Intermediate
Keypoints
Data augmentation
2D
Augmentation container
Augmentation Sequential
kornia.augmentation
In this tutorial we leverage
kornia.augmentation.AugmentationSequential
to apply augmentations to image and transform reusing the applied geometric transformation to a set of associated keypoints. This is useful for detection networks or geometric problems.
Install and get data
import io
import requests
def download_image(url: str, filename: str = "") -> str:
= url.split("/")[-1] if len(filename) == 0 else filename
filename # Download
= io.BytesIO(requests.get(url).content)
bytesio # Save file
with open(filename, "wb") as outfile:
outfile.write(bytesio.getbuffer())
return filename
"https://github.com/kornia/data/raw/main/arturito.jpg") download_image(
'arturito.jpg'
import kornia as K
import torch
from matplotlib import pyplot as plt
= K.io.load_image("arturito.jpg", K.io.ImageLoadType.RGB32)
img = img[None] # 1xCxHxW / fp32 / [0, 1]
img print(img.shape)
torch.Size([1, 3, 144, 256])
Draw points and show image
= torch.tensor([[[125, 40.0], [185.0, 75.0]]]) # BxNx2 [x,y]
coords
= plt.subplots()
fig, ax
0, 0, 0], coords[0, 0, 1]), color="r"))
ax.add_patch(plt.Circle((coords[0, 1, 0], coords[0, 1, 1]), color="r"))
ax.add_patch(plt.Circle((coords[
ax.imshow(K.tensor_to_image(img))
Resize points and show
= K.augmentation.AugmentationSequential(
resize_op 100, 200), antialias=True), data_keys=["input", "keypoints"]
K.augmentation.Resize((
)
print(resize_op.transform_matrix)
= resize_op(img, coords)
img_resize, coords_resize
= plt.subplots()
fig, ax
0, 0, 0], coords_resize[0, 0, 1]), color="r"))
ax.add_patch(plt.Circle((coords_resize[0, 1, 0], coords_resize[0, 1, 1]), color="r"))
ax.add_patch(plt.Circle((coords_resize[
ax.imshow(K.tensor_to_image(img_resize))
None
Crop image and points
= K.augmentation.AugmentationSequential(K.augmentation.CenterCrop((100, 200)), data_keys=["input", "keypoints"])
crop_op print(crop_op.transform_matrix)
= crop_op(img, coords)
img_resize, coords_resize
= plt.subplots()
fig, ax
0, 0, 0], coords_resize[0, 0, 1]), color="r"))
ax.add_patch(plt.Circle((coords_resize[0, 1, 0], coords_resize[0, 1, 1]), color="r"))
ax.add_patch(plt.Circle((coords_resize[
ax.imshow(K.tensor_to_image(img_resize))
None