make_shrink_map.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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, **kwargs):
  33. self.min_text_size = min_text_size
  34. self.shrink_ratio = shrink_ratio
  35. if "total_epoch" in kwargs and "epoch" in kwargs and kwargs["epoch"] != "None":
  36. self.shrink_ratio = self.shrink_ratio + 0.2 * kwargs["epoch"] / float(
  37. kwargs["total_epoch"]
  38. )
  39. def __call__(self, data):
  40. image = data["image"]
  41. text_polys = data["polys"]
  42. ignore_tags = data["ignore_tags"]
  43. h, w = image.shape[:2]
  44. text_polys, ignore_tags = self.validate_polygons(text_polys, ignore_tags, h, w)
  45. gt = np.zeros((h, w), dtype=np.float32)
  46. mask = np.ones((h, w), dtype=np.float32)
  47. for i in range(len(text_polys)):
  48. polygon = text_polys[i]
  49. height = max(polygon[:, 1]) - min(polygon[:, 1])
  50. width = max(polygon[:, 0]) - min(polygon[:, 0])
  51. if ignore_tags[i] or min(height, width) < self.min_text_size:
  52. cv2.fillPoly(mask, polygon.astype(np.int32)[np.newaxis, :, :], 0)
  53. ignore_tags[i] = True
  54. else:
  55. polygon_shape = Polygon(polygon)
  56. subject = [tuple(l) for l in polygon]
  57. padding = pyclipper.PyclipperOffset()
  58. padding.AddPath(subject, pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
  59. shrunk = []
  60. # Increase the shrink ratio every time we get multiple polygon returned back
  61. possible_ratios = np.arange(self.shrink_ratio, 1, self.shrink_ratio)
  62. np.append(possible_ratios, 1)
  63. # print(possible_ratios)
  64. for ratio in possible_ratios:
  65. # print(f"Change shrink ratio to {ratio}")
  66. distance = (
  67. polygon_shape.area
  68. * (1 - np.power(ratio, 2))
  69. / polygon_shape.length
  70. )
  71. shrunk = padding.Execute(-distance)
  72. if len(shrunk) == 1:
  73. break
  74. if shrunk == []:
  75. cv2.fillPoly(mask, polygon.astype(np.int32)[np.newaxis, :, :], 0)
  76. ignore_tags[i] = True
  77. continue
  78. for each_shrink in shrunk:
  79. shrink = np.array(each_shrink).reshape(-1, 2)
  80. cv2.fillPoly(gt, [shrink.astype(np.int32)], 1)
  81. data["shrink_map"] = gt
  82. data["shrink_mask"] = mask
  83. return data
  84. def validate_polygons(self, polygons, ignore_tags, h, w):
  85. """
  86. polygons (numpy.array, required): of shape (num_instances, num_points, 2)
  87. """
  88. if len(polygons) == 0:
  89. return polygons, ignore_tags
  90. assert len(polygons) == len(ignore_tags)
  91. for polygon in polygons:
  92. polygon[:, 0] = np.clip(polygon[:, 0], 0, w - 1)
  93. polygon[:, 1] = np.clip(polygon[:, 1], 0, h - 1)
  94. for i in range(len(polygons)):
  95. area = self.polygon_area(polygons[i])
  96. if abs(area) < 1:
  97. ignore_tags[i] = True
  98. if area > 0:
  99. polygons[i] = polygons[i][::-1, :]
  100. return polygons, ignore_tags
  101. def polygon_area(self, polygon):
  102. """
  103. compute polygon area
  104. """
  105. area = 0
  106. q = polygon[-1]
  107. for p in polygon:
  108. area += p[0] * q[1] - p[1] * q[0]
  109. q = p
  110. return area / 2.0