1+ """ 
2+ =============================================================== 
3+ Transforms on Rotated Bounding Boxes 
4+ =============================================================== 
5+ 
6+ This example illustrates how to define and use rotated bounding boxes. 
7+ 
8+ .. note:: 
9+     Support for rotated bounding boxes was released in TorchVision 0.23 and is 
10+     currently a BETA feature. We don't expect the API to change, but there may 
11+     be some rare edge-cases. If you find any issues, please report them on 
12+     our bug tracker: https://github.com/pytorch/vision/issues?q=is:open+is:issue 
13+ 
14+ First, a bit of setup code: 
15+ """ 
16+ 
17+ # %% 
18+ from  PIL  import  Image 
19+ from  pathlib  import  Path 
20+ import  matplotlib .pyplot  as  plt 
21+ 
22+ 
23+ import  torch 
24+ from  torchvision .tv_tensors  import  KeyPoints 
25+ from  torchvision .transforms  import  v2 
26+ from  helpers  import  plot 
27+ 
28+ plt .rcParams ["figure.figsize" ] =  [10 , 5 ]
29+ plt .rcParams ["savefig.bbox" ] =  "tight" 
30+ 
31+ # if you change the seed, make sure that the randomly-applied transforms 
32+ # properly show that the image can be both transformed and *not* transformed! 
33+ torch .manual_seed (0 )
34+ 
35+ # If you're trying to run that on Colab, you can download the assets and the 
36+ # helpers from https://github.com/pytorch/vision/tree/main/gallery/ 
37+ orig_img  =  Image .open (Path ('../assets' ) /  'pottery.jpg' )
38+ 
39+ # %% 
40+ # Creating KeyPoints 
41+ # ------------------------------- 
42+ # Key points are created by instantiating the 
43+ # :class:`~torchvision.tv_tensors.KeyPoints` class. 
44+ 
45+ 
46+ orig_pts  =  KeyPoints (
47+     [
48+         [
49+             [445 , 700 ],  # nose 
50+             [320 , 660 ],
51+             [370 , 660 ],
52+             [420 , 660 ],  # left eye 
53+             [300 , 620 ],
54+             [420 , 620 ],  # left eyebrow 
55+             [475 , 665 ],
56+             [515 , 665 ],
57+             [555 , 655 ],  # right eye 
58+             [460 , 625 ],
59+             [560 , 600 ],  # right eyebrow 
60+             [370 , 780 ],
61+             [450 , 760 ],
62+             [540 , 780 ],
63+             [450 , 820 ],  # mouth 
64+         ],
65+     ],
66+     canvas_size = (orig_img .size [1 ], orig_img .size [0 ]),
67+ )
68+ 
69+ plot ([(orig_img , orig_pts )])
70+ 
71+ # %% 
72+ # Transforms illustrations 
73+ # ------------------------ 
74+ # 
75+ # Using :class:`~torchvision.transforms.RandomRotation`: 
76+ rotater  =  v2 .RandomRotation (degrees = (0 , 180 ), expand = True )
77+ rotated_imgs  =  [rotater ((orig_img , orig_pts )) for  _  in  range (4 )]
78+ plot ([(orig_img , orig_pts )] +  rotated_imgs , bbox_width = 10 )
79+ 
80+ # %% 
81+ # Using :class:`~torchvision.transforms.Pad`: 
82+ padded_imgs_and_points  =  [
83+     v2 .Pad (padding = padding )(orig_img , orig_pts )
84+     for  padding  in  (30 , 50 , 100 , 200 )
85+ ]
86+ plot ([(orig_img , orig_pts )] +  padded_imgs_and_points , bbox_width = 10 )
87+ 
88+ # %% 
89+ # Using :class:`~torchvision.transforms.Resize`: 
90+ resized_imgs  =  [
91+     v2 .Resize (size = size )(orig_img , orig_pts )
92+     for  size  in  (300 , 500 , 1000 , orig_img .size )
93+ ]
94+ plot ([(orig_img , orig_pts )] +  resized_imgs , bbox_width = 5 )
95+ 
96+ # %% 
97+ # Using :class:`~torchvision.transforms.RandomPerspective`: 
98+ perspective_transformer  =  v2 .RandomPerspective (distortion_scale = 0.6 , p = 1.0 )
99+ perspective_imgs  =  [perspective_transformer (orig_img , orig_pts ) for  _  in  range (4 )]
100+ plot ([(orig_img , orig_pts )] +  perspective_imgs , bbox_width = 10 )
101+ 
102+ # %% 
103+ # Using :class:`~torchvision.transforms.CenterCrop`: 
104+ center_crops_and_points  =  [
105+     v2 .CenterCrop (size = size )(orig_img , orig_pts )
106+     for  size  in  (300 , 500 , 1000 , orig_img .size )
107+ ]
108+ plot ([(orig_img , orig_pts )] +  center_crops_and_points )
109+ 
110+ # %% 
111+ # Using :class:`~torchvision.transforms.RandomRotation`: 
112+ rotater  =  v2 .RandomRotation (degrees = (0 , 180 ))
113+ rotated_imgs  =  [rotater ((orig_img , orig_pts )) for  _  in  range (4 )]
114+ plot ([(orig_img , orig_pts )] +  rotated_imgs )
0 commit comments