Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

transforms_test.py 20 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
  1. import copy
  2. import unittest
  3. import cv2
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. from super_gradients.training.transforms.keypoint_transforms import (
  7. KeypointsRandomHorizontalFlip,
  8. KeypointsRandomVerticalFlip,
  9. KeypointsRandomAffineTransform,
  10. KeypointsPadIfNeeded,
  11. KeypointsLongestMaxSize,
  12. PoseEstimationSample,
  13. )
  14. from super_gradients.training.transforms.keypoints import KeypointsBrightnessContrast, KeypointsMosaic
  15. from super_gradients.training.transforms.transforms import (
  16. DetectionImagePermute,
  17. DetectionPadToSize,
  18. DetectionHorizontalFlip,
  19. DetectionVerticalFlip,
  20. )
  21. from super_gradients.training.transforms.utils import (
  22. _rescale_image,
  23. _rescale_bboxes,
  24. _pad_image,
  25. _shift_bboxes,
  26. _rescale_and_pad_to_size,
  27. _rescale_xyxy_bboxes,
  28. _get_center_padding_coordinates,
  29. _get_bottom_right_padding_coordinates,
  30. PaddingCoordinates,
  31. )
  32. class TestTransforms(unittest.TestCase):
  33. def test_keypoints_random_affine(self):
  34. image = np.random.rand(640, 480, 3)
  35. mask = np.random.rand(640, 480)
  36. # Cover all image pixels with keypoints. This would guarantee test coverate of all possible keypoint locations
  37. # without relying on randomly generated keypoints.
  38. x = np.arange(image.shape[1])
  39. y = np.arange(image.shape[0])
  40. xv, yv = np.meshgrid(x, y, indexing="xy")
  41. joints = np.stack([xv.flatten(), yv.flatten(), np.ones_like(yv.flatten())], axis=-1) # [N, 3]
  42. joints = joints.reshape((-1, 1, 3)).repeat(17, axis=1) # [N, 17, 3]
  43. aug = KeypointsRandomAffineTransform(min_scale=0.8, max_scale=1.2, max_rotation=30, max_translate=0.5, prob=1, image_pad_value=0, mask_pad_value=0)
  44. aug_image, aug_mask, aug_joints, _, _ = aug(image, mask, joints, None, None)
  45. joints_outside_image = (
  46. (aug_joints[:, :, 0] < 0) | (aug_joints[:, :, 1] < 0) | (aug_joints[:, :, 0] >= aug_image.shape[1]) | (aug_joints[:, :, 1] >= aug_image.shape[0])
  47. )
  48. # Ensure that keypoints outside the image are not visible
  49. self.assertTrue((aug_joints[joints_outside_image, 2] == 0).all(), msg=f"{aug_joints[joints_outside_image]}")
  50. # Ensure that all keypoints with visible status are inside the image
  51. # (There is no intersection of two sets: keypoints outside the image and keypoints with visible status)
  52. self.assertFalse((joints_outside_image & (aug_joints[:, :, 2] == 1)).any())
  53. def test_keypoints_horizontal_flip(self):
  54. image = np.random.rand(640, 480, 3)
  55. mask = np.random.rand(640, 480)
  56. joints = np.random.randint(0, 100, size=(1, 17, 3))
  57. aug = KeypointsRandomHorizontalFlip(flip_index=[16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0], prob=1)
  58. aug_image, aug_mask, aug_joints, _, _ = aug(image, mask, joints, None, None)
  59. np.testing.assert_array_equal(aug_image, image[:, ::-1, :])
  60. np.testing.assert_array_equal(aug_mask, mask[:, ::-1])
  61. np.testing.assert_array_equal(image.shape[1] - aug_joints[:, ::-1, 0] - 1, joints[..., 0])
  62. np.testing.assert_array_equal(aug_joints[:, ::-1, 1], joints[..., 1])
  63. np.testing.assert_array_equal(aug_joints[:, ::-1, 2], joints[..., 2])
  64. def test_keypoints_vertical_flip(self):
  65. image = np.random.rand(640, 480, 3)
  66. mask = np.random.rand(640, 480)
  67. joints = np.random.randint(0, 100, size=(1, 17, 3))
  68. aug = KeypointsRandomVerticalFlip(prob=1)
  69. aug_image, aug_mask, aug_joints, _, _ = aug(image, mask, joints, None, None)
  70. np.testing.assert_array_equal(aug_image, image[::-1, :, :])
  71. np.testing.assert_array_equal(aug_mask, mask[::-1, :])
  72. np.testing.assert_array_equal(aug_joints[..., 0], joints[..., 0])
  73. np.testing.assert_array_equal(image.shape[0] - aug_joints[..., 1] - 1, joints[..., 1])
  74. np.testing.assert_array_equal(aug_joints[..., 2], joints[..., 2])
  75. def test_keypoints_pad_if_needed(self):
  76. image = np.random.rand(640, 480, 3)
  77. mask = np.random.rand(640, 480)
  78. joints = np.random.randint(0, 100, size=(1, 17, 3))
  79. aug = KeypointsPadIfNeeded(min_width=768, min_height=768, image_pad_value=0, mask_pad_value=0)
  80. aug_image, aug_mask, aug_joints, _, _ = aug(image, mask, joints, None, None)
  81. self.assertEqual(aug_image.shape, (768, 768, 3))
  82. self.assertEqual(aug_mask.shape, (768, 768))
  83. np.testing.assert_array_equal(aug_joints, joints)
  84. def test_keypoints_longest_max_size(self):
  85. image = np.random.rand(640, 480, 3)
  86. mask = np.random.rand(640, 480)
  87. joints = np.random.randint(0, 480, size=(1, 17, 3))
  88. aug = KeypointsLongestMaxSize(max_height=512, max_width=512)
  89. aug_image, aug_mask, aug_joints, _, _ = aug(image, mask, joints, None, None)
  90. self.assertEqual(aug_image.shape[:2], aug_mask.shape[:2])
  91. self.assertLessEqual(aug_image.shape[0], 512)
  92. self.assertLessEqual(aug_image.shape[1], 512)
  93. self.assertTrue((aug_joints[..., 0] < aug_image.shape[1]).all())
  94. self.assertTrue((aug_joints[..., 1] < aug_image.shape[0]).all())
  95. def test_detection_image_permute(self):
  96. aug = DetectionImagePermute(dims=(2, 1, 0))
  97. image = np.random.rand(640, 480, 3)
  98. sample = {"image": image}
  99. output = aug(sample)
  100. self.assertEqual(output["image"].shape, (3, 480, 640))
  101. def test_detection_pad_to_size(self):
  102. aug = DetectionPadToSize(output_size=(640, 640), pad_value=123)
  103. image = np.ones((512, 480, 3))
  104. # Boxes in format (x1, y1, x2, y2, class_id)
  105. boxes = np.array([[0, 0, 100, 100, 0], [100, 100, 200, 200, 1]])
  106. sample = {"image": image, "target": boxes}
  107. output = aug(sample)
  108. shift_x = (640 - 480) // 2
  109. shift_y = (640 - 512) // 2
  110. expected_boxes = np.array(
  111. [[0 + shift_x, 0 + shift_y, 100 + shift_x, 100 + shift_y, 0], [100 + shift_x, 100 + shift_y, 200 + shift_x, 200 + shift_y, 1]]
  112. )
  113. self.assertEqual(output["image"].shape, (640, 640, 3))
  114. np.testing.assert_array_equal(output["target"], expected_boxes)
  115. def test_rescale_image(self):
  116. image = np.random.randint(0, 256, size=(640, 480, 3), dtype=np.uint8)
  117. target_shape = (320, 240)
  118. rescaled_image = _rescale_image(image, target_shape)
  119. # Check if the rescaled image has the correct target shape
  120. self.assertEqual(rescaled_image.shape[:2], target_shape)
  121. def test_detection_horizontal_flip(self):
  122. aug = DetectionHorizontalFlip(prob=1)
  123. image = np.random.rand(100, 100, 3)
  124. image_original = image.copy()
  125. # [x0, y0, x1, y1]
  126. bboxes = np.array(
  127. (
  128. (10, 10, 20, 20),
  129. (90, 90, 100, 100),
  130. )
  131. )
  132. bboxes_expected = np.array(
  133. (
  134. (80, 10, 90, 20),
  135. (0, 90, 10, 100),
  136. )
  137. )
  138. # run transform
  139. sample = {"image": image}
  140. sample["target"] = bboxes
  141. sample["crowd_targets"] = bboxes.copy()
  142. output = aug(sample)
  143. image = output["image"]
  144. target = output["target"]
  145. crowd_targets = output["crowd_targets"]
  146. # check image hasn't changed shape
  147. self.assertEqual(image.shape, image_original.shape)
  148. # check the first two cols of original image
  149. # match last two rows of flipped image
  150. self.assertTrue(np.array_equal(image_original[:, 0], image[:, -1]))
  151. self.assertTrue(np.array_equal(image_original[:, 1], image[:, -2]))
  152. # check bboxes as expected
  153. self.assertTrue(np.array_equal(target, bboxes_expected))
  154. self.assertTrue(np.array_equal(crowd_targets, bboxes_expected))
  155. def test_detection_vertical_flip(self):
  156. aug = DetectionVerticalFlip(prob=1)
  157. image = np.random.rand(100, 100, 3)
  158. image_original = image.copy()
  159. # [x0, y0, x1, y1]
  160. bboxes = np.array(
  161. (
  162. (10, 10, 20, 20),
  163. (90, 90, 100, 100),
  164. )
  165. )
  166. bboxes_expected = np.array(
  167. (
  168. (10, 80, 20, 90),
  169. (90, 0, 100, 10),
  170. )
  171. )
  172. # run transform
  173. sample = {"image": image}
  174. sample["target"] = bboxes
  175. sample["crowd_targets"] = bboxes.copy()
  176. output = aug(sample)
  177. image = output["image"]
  178. target = output["target"]
  179. crowd_targets = output["crowd_targets"]
  180. # check image hasn't changed shape
  181. self.assertEqual(image.shape, image_original.shape)
  182. # check top two rows of original image
  183. # matches bottom rows of flipped image
  184. self.assertTrue(np.array_equal(image_original[0], image[-1]))
  185. self.assertTrue(np.array_equal(image_original[1], image[-2]))
  186. # check bboxes as expected
  187. self.assertTrue(np.array_equal(target, bboxes_expected))
  188. self.assertTrue(np.array_equal(crowd_targets, bboxes_expected))
  189. def test_rescale_bboxes(self):
  190. sy, sx = (2.0, 0.5)
  191. # Empty bboxes
  192. bboxes = np.zeros((0, 4))
  193. expected_bboxes = np.zeros((0, 4))
  194. rescaled_bboxes = _rescale_bboxes(targets=bboxes, scale_factors=(sy, sx))
  195. np.testing.assert_array_equal(rescaled_bboxes, expected_bboxes)
  196. # Not empty bboxes
  197. bboxes = np.array([[10, 20, 50, 60, 1], [30, 40, 80, 90, 2]], dtype=np.float32)
  198. expected_bboxes = np.array([[5.0, 40.0, 25.0, 120.0, 1.0], [15.0, 80.0, 40.0, 180.0, 2.0]], dtype=np.float32)
  199. rescaled_bboxes = _rescale_bboxes(targets=bboxes, scale_factors=(sy, sx))
  200. np.testing.assert_array_equal(rescaled_bboxes, expected_bboxes)
  201. def test_pad_image(self):
  202. image = np.random.randint(0, 256, size=(640, 480, 3), dtype=np.uint8)
  203. padding_coordinates = PaddingCoordinates(top=80, bottom=80, left=60, right=60)
  204. pad_value = 0
  205. shifted_image = _pad_image(image, padding_coordinates, pad_value)
  206. # Check if the shifted image has the correct shape
  207. self.assertEqual(shifted_image.shape, (800, 600, 3))
  208. # Check if the padding values are correct
  209. self.assertTrue((shifted_image[: padding_coordinates.top, :, :] == pad_value).all())
  210. self.assertTrue((shifted_image[-padding_coordinates.bottom :, :, :] == pad_value).all())
  211. self.assertTrue((shifted_image[:, : padding_coordinates.left, :] == pad_value).all())
  212. self.assertTrue((shifted_image[:, -padding_coordinates.right :, :] == pad_value).all())
  213. def test_shift_bboxes(self):
  214. bboxes = np.array([[10, 20, 50, 60, 1], [30, 40, 80, 90, 2]], dtype=np.float32)
  215. shift_w, shift_h = 60, 80
  216. shifted_bboxes = _shift_bboxes(bboxes, shift_w, shift_h)
  217. # Check if the shifted bboxes have the correct values
  218. expected_bboxes = np.array([[70, 100, 110, 140, 1], [90, 120, 140, 170, 2]], dtype=np.float32)
  219. np.testing.assert_array_equal(shifted_bboxes, expected_bboxes)
  220. def test_rescale_xyxy_bboxes(self):
  221. bboxes = np.array([[10, 20, 50, 60, 1], [30, 40, 80, 90, 2]], dtype=np.float32)
  222. r = 0.5
  223. rescaled_bboxes = _rescale_xyxy_bboxes(bboxes, r)
  224. # Check if the rescaled bboxes have the correct values
  225. expected_bboxes = np.array([[5.0, 10.0, 25.0, 30.0, 1.0], [15.0, 20.0, 40.0, 45.0, 2.0]], dtype=np.float32)
  226. np.testing.assert_array_equal(rescaled_bboxes, expected_bboxes)
  227. def test_padding(self):
  228. # Test Case 1: Padding needed
  229. image = np.array([[1, 2], [3, 4]])
  230. padding_coordinates = PaddingCoordinates(top=0, left=0, bottom=1, right=2)
  231. expected_padded_image = np.array([[1, 2, 114, 114], [3, 4, 114, 114], [114, 114, 114, 114]])
  232. padded_image = _pad_image(image=image, padding_coordinates=padding_coordinates, pad_value=114)
  233. np.testing.assert_array_equal(padded_image, expected_padded_image)
  234. # Test Case 2: No padding needed
  235. image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  236. padding_coordinates = PaddingCoordinates(top=0, left=0, bottom=0, right=0)
  237. expected_padded_image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  238. padded_image = _pad_image(image=image, padding_coordinates=padding_coordinates, pad_value=114)
  239. np.testing.assert_array_equal(padded_image, expected_padded_image)
  240. # Test Case 3: Image with channel dimension
  241. image = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
  242. padding_coordinates = PaddingCoordinates(top=0, left=0, bottom=1, right=2)
  243. expected_padded_image = np.array(
  244. [
  245. [[1, 2, 3], [4, 5, 6], [0, 0, 0], [0, 0, 0]],
  246. [[7, 8, 9], [10, 11, 12], [0, 0, 0], [0, 0, 0]],
  247. [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
  248. ],
  249. )
  250. padded_image = _pad_image(image=image, padding_coordinates=padding_coordinates, pad_value=0)
  251. np.testing.assert_array_equal(padded_image, expected_padded_image)
  252. def test_get_padding_coordinates(self):
  253. # Test Case 1: Width padding required
  254. image = np.zeros((640, 480))
  255. output_size = (640, 640)
  256. expected_center_padding = PaddingCoordinates(top=0, bottom=0, left=80, right=80)
  257. expected_bottom_right_padding = PaddingCoordinates(top=0, bottom=0, left=0, right=160)
  258. center_padding_coordinates = _get_center_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  259. bottom_right_padding_coordinates = _get_bottom_right_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  260. self.assertEqual(center_padding_coordinates, expected_center_padding)
  261. self.assertEqual(bottom_right_padding_coordinates, expected_bottom_right_padding)
  262. # Test Case 2: Height padding required
  263. image = np.zeros((480, 640))
  264. output_size = (640, 640)
  265. expected_center_padding = PaddingCoordinates(top=80, bottom=80, left=0, right=0)
  266. expected_bottom_right_padding = PaddingCoordinates(top=0, bottom=160, left=0, right=0)
  267. center_padding_coordinates = _get_center_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  268. bottom_right_padding_coordinates = _get_bottom_right_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  269. self.assertEqual(center_padding_coordinates, expected_center_padding)
  270. self.assertEqual(bottom_right_padding_coordinates, expected_bottom_right_padding)
  271. # Test Case 3: Width and Height padding required
  272. image = np.zeros((480, 640))
  273. output_size = (800, 800)
  274. expected_center_padding = PaddingCoordinates(top=160, bottom=160, left=80, right=80)
  275. expected_bottom_right_padding = PaddingCoordinates(top=0, bottom=320, left=0, right=160)
  276. center_padding_coordinates = _get_center_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  277. bottom_right_padding_coordinates = _get_bottom_right_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  278. self.assertEqual(center_padding_coordinates, expected_center_padding)
  279. self.assertEqual(bottom_right_padding_coordinates, expected_bottom_right_padding)
  280. # Test Case 4: Image shape is bigger than output shape
  281. image = np.zeros((800, 800))
  282. output_size = (640, 640)
  283. expected_center_padding = PaddingCoordinates(top=-80, bottom=-80, left=-80, right=-80)
  284. expected_bottom_right_padding = PaddingCoordinates(top=0, bottom=-160, left=0, right=-160)
  285. center_padding_coordinates = _get_center_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  286. bottom_right_padding_coordinates = _get_bottom_right_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  287. self.assertEqual(center_padding_coordinates, expected_center_padding)
  288. self.assertEqual(bottom_right_padding_coordinates, expected_bottom_right_padding)
  289. # Test Case 5: Width and Height padding required with an image of 3 channels
  290. image = np.zeros((480, 640, 3))
  291. output_size = (800, 800)
  292. expected_center_padding = PaddingCoordinates(top=160, bottom=160, left=80, right=80)
  293. expected_bottom_right_padding = PaddingCoordinates(top=0, bottom=320, left=0, right=160)
  294. center_padding_coordinates = _get_center_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  295. bottom_right_padding_coordinates = _get_bottom_right_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  296. self.assertEqual(center_padding_coordinates, expected_center_padding)
  297. self.assertEqual(bottom_right_padding_coordinates, expected_bottom_right_padding)
  298. def test_rescale_and_pad_to_size(self):
  299. image = np.random.randint(0, 256, size=(640, 480, 3), dtype=np.uint8)
  300. output_size = (800, 500)
  301. pad_val = 114
  302. rescaled_padded_image, r = _rescale_and_pad_to_size(image, output_size, pad_val=pad_val)
  303. # Check if the rescaled and padded image has the correct shape
  304. self.assertEqual(rescaled_padded_image.shape, (3, *output_size))
  305. # Check if the image is rescaled with the correct ratio
  306. resized_image_shape = (int(image.shape[0] * r), int(image.shape[1] * r))
  307. # Check if the padding is correctly applied
  308. padded_area = rescaled_padded_image[:, resized_image_shape[0] :, :] # Right padding area
  309. self.assertTrue((padded_area == pad_val).all())
  310. padded_area = rescaled_padded_image[:, :, resized_image_shape[1] :] # Bottom padding area
  311. self.assertTrue((padded_area == pad_val).all())
  312. def test_keypoints_brightness_contrast(self):
  313. image = np.random.randint(0, 255, (640, 480, 3), dtype=np.uint8)
  314. image = cv2.boxFilter(image, -1, (13, 13))
  315. plt.figure()
  316. plt.imshow(image)
  317. plt.title("Original image")
  318. plt.show()
  319. aug = KeypointsBrightnessContrast(brightness_range=(1.1, 1.5), contrast_range=(1, 1), prob=1)
  320. sample = aug(PoseEstimationSample(image=image, joints=None, bboxes=None, areas=None, mask=None, is_crowd=None))
  321. plt.figure()
  322. plt.imshow(sample.image)
  323. plt.title("Augmented image")
  324. plt.show()
  325. def test_keypoints_mosaic(self):
  326. sample1 = PoseEstimationSample(
  327. image=np.zeros((128, 128, 3), dtype=np.uint8) + 255,
  328. mask=np.zeros((128, 128), dtype=np.uint8),
  329. joints=np.random.randint(0, 128, size=(1, 17, 3)),
  330. is_crowd=np.zeros((1,), dtype=np.bool),
  331. areas=None,
  332. bboxes=np.random.randint(0, 64, size=(2, 4)),
  333. )
  334. sample2 = PoseEstimationSample(
  335. image=np.zeros((256, 256, 3), dtype=np.uint8) + np.array([55, 0, 0], dtype=np.uint8),
  336. mask=np.zeros((256, 256), dtype=np.uint8),
  337. joints=np.random.randint(0, 256, size=(1, 17, 3)),
  338. is_crowd=np.zeros((1,), dtype=np.bool),
  339. areas=None,
  340. bboxes=np.random.randint(32, 64, size=(2, 4)),
  341. )
  342. sample3 = PoseEstimationSample(
  343. image=np.zeros((512, 512, 3), dtype=np.uint8) + np.array([0, 55, 0], dtype=np.uint8),
  344. mask=np.zeros((512, 512), dtype=np.uint8),
  345. joints=np.random.randint(0, 512, size=(1, 17, 3)),
  346. is_crowd=np.zeros((1,), dtype=np.bool),
  347. areas=None,
  348. bboxes=np.random.randint(128, 256, size=(2, 4)),
  349. )
  350. sample4 = PoseEstimationSample(
  351. image=np.zeros((64, 64, 3), dtype=np.uint8) + np.array([0, 0, 55], dtype=np.uint8),
  352. mask=np.zeros((64, 64), dtype=np.uint8),
  353. joints=np.random.randint(0, 64, size=(1, 17, 3)),
  354. is_crowd=np.zeros((1,), dtype=np.bool),
  355. areas=None,
  356. bboxes=np.random.randint(0, 32, size=(2, 4)),
  357. )
  358. input_mixup = copy.deepcopy(sample4)
  359. input_mixup.additional_samples = [sample1, sample2, sample3]
  360. self.show_sample(sample1)
  361. self.show_sample(sample2)
  362. self.show_sample(sample3)
  363. self.show_sample(sample4)
  364. aug = KeypointsMosaic(prob=1)
  365. sample = aug(input_mixup)
  366. self.show_sample(sample)
  367. def show_sample(self, sample: PoseEstimationSample):
  368. image = sample.image.copy()
  369. poses = sample.joints
  370. for joints in poses:
  371. for joint in joints:
  372. cv2.circle(image, (int(joint[0]), int(joint[1])), 3, (0, 0, 255), -1)
  373. for (x, y, w, h) in sample.bboxes:
  374. x = int(x)
  375. y = int(y)
  376. w = int(w)
  377. h = int(h)
  378. cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)
  379. plt.figure()
  380. plt.imshow(image)
  381. plt.show()
  382. if __name__ == "__main__":
  383. unittest.main()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...