imgaug_support.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import numpy as np
  15. def execute_imgaug(augmenter, im, bboxes=None, polygons=None,
  16. segment_map=None):
  17. # 预处理,将bboxes, polygons转换成imgaug格式
  18. import imgaug.augmentables.polys as polys
  19. import imgaug.augmentables.bbs as bbs
  20. aug_im = im.astype('uint8')
  21. aug_bboxes = None
  22. if bboxes is not None:
  23. aug_bboxes = list()
  24. for i in range(len(bboxes)):
  25. x1 = bboxes[i, 0] - 1
  26. y1 = bboxes[i, 1]
  27. x2 = bboxes[i, 2]
  28. y2 = bboxes[i, 3]
  29. aug_bboxes.append(bbs.BoundingBox(x1, y1, x2, y2))
  30. aug_polygons = None
  31. lod_info = list()
  32. if polygons is not None:
  33. aug_polygons = list()
  34. for i in range(len(polygons)):
  35. num = len(polygons[i])
  36. lod_info.append(num)
  37. for j in range(num):
  38. points = np.reshape(polygons[i][j], (-1, 2))
  39. aug_polygons.append(polys.Polygon(points))
  40. aug_segment_map = None
  41. if segment_map is not None:
  42. if len(segment_map.shape) == 2:
  43. h, w = segment_map.shape
  44. aug_segment_map = np.reshape(segment_map, (1, h, w, 1))
  45. elif len(segment_map.shape) == 3:
  46. h, w, c = segment_map.shape
  47. aug_segment_map = np.reshape(segment_map, (1, h, w, c))
  48. else:
  49. raise Exception(
  50. "Only support 2-dimensions for 3-dimensions for segment_map")
  51. aug_im, aug_bboxes, aug_polygons, aug_seg_map = augmenter.augment(
  52. image=aug_im,
  53. bounding_boxes=aug_bboxes,
  54. polygons=aug_polygons,
  55. segmentation_maps=aug_segment_map)
  56. aug_im = aug_im.astype('float32')
  57. if aug_polygons is not None:
  58. assert len(aug_bboxes) == len(
  59. lod_info
  60. ), "Number of aug_bboxes should be equal to number of aug_polygons"
  61. if aug_bboxes is not None:
  62. # 裁剪掉在图像之外的bbox和polygon
  63. for i in range(len(aug_bboxes)):
  64. aug_bboxes[i] = aug_bboxes[i].clip_out_of_image(aug_im)
  65. if aug_polygons is not None:
  66. for i in range(len(aug_polygons)):
  67. aug_polygons[i] = aug_polygons[i].clip_out_of_image(aug_im)
  68. # 过滤掉无效的bbox和polygon,并转换为训练数据格式
  69. converted_bboxes = list()
  70. converted_polygons = list()
  71. poly_index = 0
  72. for i in range(len(aug_bboxes)):
  73. # 过滤width或height不足1像素的框
  74. if aug_bboxes[i].width < 1 or aug_bboxes[i].height < 1:
  75. continue
  76. if aug_polygons is None:
  77. converted_bboxes.append([
  78. aug_bboxes[i].x1, aug_bboxes[i].y1, aug_bboxes[i].x2,
  79. aug_bboxes[i].y2
  80. ])
  81. continue
  82. # 如若有polygons,将会继续执行下面代码
  83. polygons_this_box = list()
  84. for ps in aug_polygons[poly_index:poly_index + lod_info[i]]:
  85. if len(ps) == 0:
  86. continue
  87. for p in ps:
  88. # 没有3个point的polygon被过滤
  89. if len(p.exterior) < 3:
  90. continue
  91. polygons_this_box.append(p.exterior.flatten().tolist())
  92. poly_index += lod_info[i]
  93. if len(polygons_this_box) == 0:
  94. continue
  95. converted_bboxes.append([
  96. aug_bboxes[i].x1, aug_bboxes[i].y1, aug_bboxes[i].x2,
  97. aug_bboxes[i].y2
  98. ])
  99. converted_polygons.append(polygons_this_box)
  100. if len(converted_bboxes) == 0:
  101. aug_im = im
  102. converted_bboxes = bboxes
  103. converted_polygons = polygons
  104. result = [aug_im]
  105. if bboxes is not None:
  106. result.append(np.array(converted_bboxes))
  107. if polygons is not None:
  108. result.append(converted_polygons)
  109. if segment_map is not None:
  110. n, h, w, c = aug_seg_map.shape
  111. if len(segment_map.shape) == 2:
  112. aug_seg_map = np.reshape(aug_seg_map, (h, w))
  113. elif len(segment_map.shape) == 3:
  114. aug_seg_map = np.reshape(aug_seg_map, (h, w, c))
  115. result.append(aug_seg_map)
  116. return result