make_shrink_map.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # -*- coding:utf-8 -*-
  2. from __future__ import absolute_import
  3. from __future__ import division
  4. from __future__ import print_function
  5. from __future__ import unicode_literals
  6. import numpy as np
  7. import cv2
  8. from shapely.geometry import Polygon
  9. import pyclipper
  10. __all__ = ['MakeShrinkMap']
  11. class MakeShrinkMap(object):
  12. r'''
  13. Making binary mask from detection data with ICDAR format.
  14. Typically following the process of class `MakeICDARData`.
  15. '''
  16. def __init__(self, min_text_size=8, shrink_ratio=0.4, **kwargs):
  17. self.min_text_size = min_text_size
  18. self.shrink_ratio = shrink_ratio
  19. def __call__(self, data):
  20. image = data['image']
  21. text_polys = data['polys']
  22. ignore_tags = data['ignore_tags']
  23. h, w = image.shape[:2]
  24. text_polys, ignore_tags = self.validate_polygons(text_polys,
  25. ignore_tags, h, w)
  26. gt = np.zeros((h, w), dtype=np.float32)
  27. mask = np.ones((h, w), dtype=np.float32)
  28. for i in range(len(text_polys)):
  29. polygon = text_polys[i]
  30. height = max(polygon[:, 1]) - min(polygon[:, 1])
  31. width = max(polygon[:, 0]) - min(polygon[:, 0])
  32. if ignore_tags[i] or min(height, width) < self.min_text_size:
  33. cv2.fillPoly(mask,
  34. polygon.astype(np.int32)[np.newaxis, :, :], 0)
  35. ignore_tags[i] = True
  36. else:
  37. polygon_shape = Polygon(polygon)
  38. subject = [tuple(l) for l in polygon]
  39. padding = pyclipper.PyclipperOffset()
  40. padding.AddPath(subject, pyclipper.JT_ROUND,
  41. pyclipper.ET_CLOSEDPOLYGON)
  42. shrinked = []
  43. # Increase the shrink ratio every time we get multiple polygon returned back
  44. possible_ratios = np.arange(self.shrink_ratio, 1,
  45. self.shrink_ratio)
  46. np.append(possible_ratios, 1)
  47. # print(possible_ratios)
  48. for ratio in possible_ratios:
  49. # print(f"Change shrink ratio to {ratio}")
  50. distance = polygon_shape.area * (
  51. 1 - np.power(ratio, 2)) / polygon_shape.length
  52. shrinked = padding.Execute(-distance)
  53. if len(shrinked) == 1:
  54. break
  55. if shrinked == []:
  56. cv2.fillPoly(mask,
  57. polygon.astype(np.int32)[np.newaxis, :, :], 0)
  58. ignore_tags[i] = True
  59. continue
  60. for each_shirnk in shrinked:
  61. shirnk = np.array(each_shirnk).reshape(-1, 2)
  62. cv2.fillPoly(gt, [shirnk.astype(np.int32)], 1)
  63. # cv2.fillPoly(gt[0], [shrinked.astype(np.int32)], 1)
  64. data['shrink_map'] = gt
  65. data['shrink_mask'] = mask
  66. return data
  67. def validate_polygons(self, polygons, ignore_tags, h, w):
  68. '''
  69. polygons (numpy.array, required): of shape (num_instances, num_points, 2)
  70. '''
  71. if len(polygons) == 0:
  72. return polygons, ignore_tags
  73. assert len(polygons) == len(ignore_tags)
  74. for polygon in polygons:
  75. polygon[:, 0] = np.clip(polygon[:, 0], 0, w - 1)
  76. polygon[:, 1] = np.clip(polygon[:, 1], 0, h - 1)
  77. for i in range(len(polygons)):
  78. area = self.polygon_area(polygons[i])
  79. if abs(area) < 1:
  80. ignore_tags[i] = True
  81. if area > 0:
  82. polygons[i] = polygons[i][::-1, :]
  83. return polygons, ignore_tags
  84. def polygon_area(self, polygon):
  85. # return cv2.contourArea(polygon.astype(np.float32))
  86. edge = 0
  87. for i in range(polygon.shape[0]):
  88. next_index = (i + 1) % polygon.shape[0]
  89. edge += (polygon[next_index, 0] - polygon[i, 0]) * (
  90. polygon[next_index, 1] - polygon[i, 1])
  91. return edge / 2.