make_shrink_map.py 4.9 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. """
  15. This code is refer from:
  16. https://github.com/WenmuZhou/DBNet.pytorch/blob/master/data_loader/modules/make_shrink_map.py
  17. """
  18. from __future__ import absolute_import
  19. from __future__ import division
  20. from __future__ import print_function
  21. from __future__ import unicode_literals
  22. import numpy as np
  23. import cv2
  24. from shapely.geometry import Polygon
  25. import pyclipper
  26. __all__ = ['MakeShrinkMap']
  27. class MakeShrinkMap(object):
  28. r'''
  29. Making binary mask from detection data with ICDAR format.
  30. Typically following the process of class `MakeICDARData`.
  31. '''
  32. def __init__(self, min_text_size=8, shrink_ratio=0.4, num_classes=1, **kwargs):
  33. self.min_text_size = min_text_size
  34. self.shrink_ratio = shrink_ratio
  35. self.num_classes = num_classes
  36. def __call__(self, data):
  37. image = data['image']
  38. text_polys = data['polys']
  39. ignore_tags = data['ignore_tags']
  40. if self.num_classes > 1:
  41. classes = data['classes']
  42. h, w = image.shape[:2]
  43. text_polys, ignore_tags = self.validate_polygons(text_polys,
  44. ignore_tags, h, w)
  45. gt = np.zeros((h, w), dtype=np.float32)
  46. gt_class = np.zeros((h, w), dtype=np.float32)
  47. mask = np.ones((h, w), dtype=np.float32)
  48. for i in range(len(text_polys)):
  49. polygon = text_polys[i]
  50. height = max(polygon[:, 1]) - min(polygon[:, 1])
  51. width = max(polygon[:, 0]) - min(polygon[:, 0])
  52. if ignore_tags[i] or min(height, width) < self.min_text_size:
  53. cv2.fillPoly(mask,
  54. polygon.astype(np.int32)[np.newaxis, :, :], 0)
  55. ignore_tags[i] = True
  56. else:
  57. polygon_shape = Polygon(polygon)
  58. subject = [tuple(l) for l in polygon]
  59. padding = pyclipper.PyclipperOffset()
  60. padding.AddPath(subject, pyclipper.JT_ROUND,
  61. pyclipper.ET_CLOSEDPOLYGON)
  62. shrinked = []
  63. # Increase the shrink ratio every time we get multiple polygon returned back
  64. possible_ratios = np.arange(self.shrink_ratio, 1,
  65. self.shrink_ratio)
  66. np.append(possible_ratios, 1)
  67. # print(possible_ratios)
  68. for ratio in possible_ratios:
  69. # print(f"Change shrink ratio to {ratio}")
  70. distance = polygon_shape.area * (
  71. 1 - np.power(ratio, 2)) / polygon_shape.length
  72. shrinked = padding.Execute(-distance)
  73. if len(shrinked) == 1:
  74. break
  75. if shrinked == []:
  76. cv2.fillPoly(mask,
  77. polygon.astype(np.int32)[np.newaxis, :, :], 0)
  78. ignore_tags[i] = True
  79. continue
  80. for each_shirnk in shrinked:
  81. shirnk = np.array(each_shirnk).reshape(-1, 2)
  82. cv2.fillPoly(gt, [shirnk.astype(np.int32)], 1)
  83. if self.num_classes > 1:
  84. cv2.fillPoly(gt_class, polygon.astype(np.int32)[np.newaxis, :, :], classes[i])
  85. data['shrink_map'] = gt
  86. if self.num_classes > 1:
  87. data['class_mask'] = gt_class
  88. data['shrink_mask'] = mask
  89. return data
  90. def validate_polygons(self, polygons, ignore_tags, h, w):
  91. '''
  92. polygons (numpy.array, required): of shape (num_instances, num_points, 2)
  93. '''
  94. if len(polygons) == 0:
  95. return polygons, ignore_tags
  96. assert len(polygons) == len(ignore_tags)
  97. for polygon in polygons:
  98. polygon[:, 0] = np.clip(polygon[:, 0], 0, w - 1)
  99. polygon[:, 1] = np.clip(polygon[:, 1], 0, h - 1)
  100. for i in range(len(polygons)):
  101. area = self.polygon_area(polygons[i])
  102. if abs(area) < 1:
  103. ignore_tags[i] = True
  104. if area > 0:
  105. polygons[i] = polygons[i][::-1, :]
  106. return polygons, ignore_tags
  107. def polygon_area(self, polygon):
  108. """
  109. compute polygon area
  110. """
  111. area = 0
  112. q = polygon[-1]
  113. for p in polygon:
  114. area += p[0] * q[1] - p[1] * q[0]
  115. q = p
  116. return area / 2.0