.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/transforms/plot_keypoints_transforms.py" .. LINE NUMBERS ARE GIVEN BELOW. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_transforms_plot_keypoints_transforms.py: =============================================================== Transforms on KeyPoints =============================================================== This example illustrates how to define and use keypoints. For this tutorial, we use this picture of a ceramic figure from the pre-columbian period. The image is specified "public domain" (https://www.metmuseum.org/art/collection/search/502727). .. note:: Support for keypoints was released in TorchVision 0.23 and is currently a BETA feature. We don't expect the API to change, but there may be some rare edge-cases. If you find any issues, please report them on our bug tracker: https://github.com/pytorch/vision/issues?q=is:open+is:issue First, a bit of setup code: .. GENERATED FROM PYTHON SOURCE LINES 20-41 .. code-block:: Python from PIL import Image from pathlib import Path import matplotlib.pyplot as plt import torch from torchvision.tv_tensors import KeyPoints from torchvision.transforms import v2 from helpers import plot plt.rcParams["figure.figsize"] = [10, 5] plt.rcParams["savefig.bbox"] = "tight" # if you change the seed, make sure that the transformed output # still make sense torch.manual_seed(0) # If you're trying to run that on Colab, you can download the assets and the # helpers from https://github.com/pytorch/vision/tree/main/gallery/ orig_img = Image.open(Path('../assets') / 'pottery.jpg') .. GENERATED FROM PYTHON SOURCE LINES 42-46 Creating KeyPoints ------------------------------- Key points are created by instantiating the :class:`~torchvision.tv_tensors.KeyPoints` class. .. GENERATED FROM PYTHON SOURCE LINES 46-73 .. code-block:: Python orig_pts = KeyPoints( [ [ [445, 700], # nose [320, 660], [370, 660], [420, 660], # left eye [300, 620], [420, 620], # left eyebrow [475, 665], [515, 665], [555, 655], # right eye [460, 625], [560, 600], # right eyebrow [370, 780], [450, 760], [540, 780], [450, 820], # mouth ], ], canvas_size=(orig_img.size[1], orig_img.size[0]), ) plot([(orig_img, orig_pts)]) .. image-sg:: /auto_examples/transforms/images/sphx_glr_plot_keypoints_transforms_001.png :alt: plot keypoints transforms :srcset: /auto_examples/transforms/images/sphx_glr_plot_keypoints_transforms_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 74-78 Transforms illustrations ------------------------ Using :class:`~torchvision.transforms.RandomRotation`: .. GENERATED FROM PYTHON SOURCE LINES 78-82 .. code-block:: Python rotater = v2.RandomRotation(degrees=(0, 180), expand=True) rotated_imgs = [rotater((orig_img, orig_pts)) for _ in range(4)] plot([(orig_img, orig_pts)] + rotated_imgs) .. image-sg:: /auto_examples/transforms/images/sphx_glr_plot_keypoints_transforms_002.png :alt: plot keypoints transforms :srcset: /auto_examples/transforms/images/sphx_glr_plot_keypoints_transforms_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 83-84 Using :class:`~torchvision.transforms.Pad`: .. GENERATED FROM PYTHON SOURCE LINES 84-90 .. code-block:: Python padded_imgs_and_points = [ v2.Pad(padding=padding)(orig_img, orig_pts) for padding in (30, 50, 100, 200) ] plot([(orig_img, orig_pts)] + padded_imgs_and_points) .. image-sg:: /auto_examples/transforms/images/sphx_glr_plot_keypoints_transforms_003.png :alt: plot keypoints transforms :srcset: /auto_examples/transforms/images/sphx_glr_plot_keypoints_transforms_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 91-92 Using :class:`~torchvision.transforms.Resize`: .. GENERATED FROM PYTHON SOURCE LINES 92-98 .. code-block:: Python resized_imgs = [ v2.Resize(size=size)(orig_img, orig_pts) for size in (300, 500, 1000, orig_img.size) ] plot([(orig_img, orig_pts)] + resized_imgs) .. image-sg:: /auto_examples/transforms/images/sphx_glr_plot_keypoints_transforms_004.png :alt: plot keypoints transforms :srcset: /auto_examples/transforms/images/sphx_glr_plot_keypoints_transforms_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 99-100 Using :class:`~torchvision.transforms.RandomPerspective`: .. GENERATED FROM PYTHON SOURCE LINES 100-104 .. code-block:: Python perspective_transformer = v2.RandomPerspective(distortion_scale=0.6, p=1.0) perspective_imgs = [perspective_transformer(orig_img, orig_pts) for _ in range(4)] plot([(orig_img, orig_pts)] + perspective_imgs) .. image-sg:: /auto_examples/transforms/images/sphx_glr_plot_keypoints_transforms_005.png :alt: plot keypoints transforms :srcset: /auto_examples/transforms/images/sphx_glr_plot_keypoints_transforms_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 105-106 Using :class:`~torchvision.transforms.CenterCrop`: .. GENERATED FROM PYTHON SOURCE LINES 106-112 .. code-block:: Python center_crops_and_points = [ v2.CenterCrop(size=size)(orig_img, orig_pts) for size in (300, 500, 1000, orig_img.size) ] plot([(orig_img, orig_pts)] + center_crops_and_points) .. image-sg:: /auto_examples/transforms/images/sphx_glr_plot_keypoints_transforms_006.png :alt: plot keypoints transforms :srcset: /auto_examples/transforms/images/sphx_glr_plot_keypoints_transforms_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 113-114 Using :class:`~torchvision.transforms.RandomRotation`: .. GENERATED FROM PYTHON SOURCE LINES 114-117 .. code-block:: Python rotater = v2.RandomRotation(degrees=(0, 180)) rotated_imgs = [rotater((orig_img, orig_pts)) for _ in range(4)] plot([(orig_img, orig_pts)] + rotated_imgs) .. image-sg:: /auto_examples/transforms/images/sphx_glr_plot_keypoints_transforms_007.png :alt: plot keypoints transforms :srcset: /auto_examples/transforms/images/sphx_glr_plot_keypoints_transforms_007.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.855 seconds) .. _sphx_glr_download_auto_examples_transforms_plot_keypoints_transforms.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_keypoints_transforms.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_keypoints_transforms.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_keypoints_transforms.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_