make_border_map.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import cv2
  2. import numpy as np
  3. np.seterr(divide='ignore', invalid='ignore')
  4. import pyclipper
  5. from shapely.geometry import Polygon
  6. __all__ = ['MakeBorderMap']
  7. class MakeBorderMap():
  8. def __init__(self, shrink_ratio=0.4, thresh_min=0.3, thresh_max=0.7):
  9. self.shrink_ratio = shrink_ratio
  10. self.thresh_min = thresh_min
  11. self.thresh_max = thresh_max
  12. def __call__(self, data: dict) -> dict:
  13. """
  14. 从scales中随机选择一个尺度,对图片和文本框进行缩放
  15. :param data: {'img':,'text_polys':,'texts':,'ignore_tags':}
  16. :return:
  17. """
  18. im = data['img']
  19. text_polys = data['text_polys']
  20. ignore_tags = data['ignore_tags']
  21. canvas = np.zeros(im.shape[:2], dtype=np.float32)
  22. mask = np.zeros(im.shape[:2], dtype=np.float32)
  23. for i in range(len(text_polys)):
  24. if ignore_tags[i]:
  25. continue
  26. self.draw_border_map(text_polys[i], canvas, mask=mask)
  27. canvas = canvas * (self.thresh_max - self.thresh_min) + self.thresh_min
  28. data['threshold_map'] = canvas
  29. data['threshold_mask'] = mask
  30. return data
  31. def draw_border_map(self, polygon, canvas, mask):
  32. polygon = np.array(polygon)
  33. assert polygon.ndim == 2
  34. assert polygon.shape[1] == 2
  35. polygon_shape = Polygon(polygon)
  36. if polygon_shape.area <= 0:
  37. return
  38. distance = polygon_shape.area * (1 - np.power(self.shrink_ratio, 2)) / polygon_shape.length
  39. subject = [tuple(l) for l in polygon]
  40. padding = pyclipper.PyclipperOffset()
  41. padding.AddPath(subject, pyclipper.JT_ROUND,
  42. pyclipper.ET_CLOSEDPOLYGON)
  43. try:
  44. padded_polygon = np.array(padding.Execute(distance)[0])
  45. except:
  46. return
  47. cv2.fillPoly(mask, [padded_polygon.astype(np.int32)], 1.0)
  48. xmin = padded_polygon[:, 0].min()
  49. xmax = padded_polygon[:, 0].max()
  50. ymin = padded_polygon[:, 1].min()
  51. ymax = padded_polygon[:, 1].max()
  52. width = xmax - xmin + 1
  53. height = ymax - ymin + 1
  54. polygon[:, 0] = polygon[:, 0] - xmin
  55. polygon[:, 1] = polygon[:, 1] - ymin
  56. xs = np.broadcast_to(
  57. np.linspace(0, width - 1, num=width).reshape(1, width), (height, width))
  58. ys = np.broadcast_to(
  59. np.linspace(0, height - 1, num=height).reshape(height, 1), (height, width))
  60. distance_map = np.zeros(
  61. (polygon.shape[0], height, width), dtype=np.float32)
  62. for i in range(polygon.shape[0]):
  63. j = (i + 1) % polygon.shape[0]
  64. absolute_distance = self.distance(xs, ys, polygon[i], polygon[j])
  65. distance_map[i] = np.clip(absolute_distance / distance, 0, 1)
  66. distance_map = distance_map.min(axis=0)
  67. xmin_valid = min(max(0, xmin), canvas.shape[1] - 1)
  68. xmax_valid = min(max(0, xmax), canvas.shape[1] - 1)
  69. ymin_valid = min(max(0, ymin), canvas.shape[0] - 1)
  70. ymax_valid = min(max(0, ymax), canvas.shape[0] - 1)
  71. rever_distance = 1 - distance_map[
  72. ymin_valid - ymin:ymax_valid - ymax + height,
  73. xmin_valid - xmin:xmax_valid - xmax + width]
  74. rever_distance[np.isnan(rever_distance)] = 0.99
  75. canvas[ymin_valid:ymax_valid + 1, xmin_valid:xmax_valid + 1] = np.fmax(
  76. rever_distance,
  77. canvas[ymin_valid:ymax_valid + 1, xmin_valid:xmax_valid + 1])
  78. def distance(self, xs, ys, point_1, point_2):
  79. '''
  80. compute the distance from point to a line
  81. ys: coordinates in the first axis
  82. xs: coordinates in the second axis
  83. point_1, point_2: (x, y), the end of the line
  84. '''
  85. height, width = xs.shape[:2]
  86. square_distance_1 = np.square(xs - point_1[0]) + np.square(ys - point_1[1])
  87. square_distance_2 = np.square(xs - point_2[0]) + np.square(ys - point_2[1])
  88. square_distance = np.square(point_1[0] - point_2[0]) + np.square(point_1[1] - point_2[1])
  89. cosin = (square_distance - square_distance_1 - square_distance_2) / (2 * np.sqrt(square_distance_1 * square_distance_2))
  90. square_sin = 1 - np.square(cosin)
  91. square_sin = np.nan_to_num(square_sin)
  92. result = np.sqrt(square_distance_1 * square_distance_2 * square_sin / square_distance)
  93. result[cosin < 0] = np.sqrt(np.fmin(square_distance_1, square_distance_2))[cosin < 0]
  94. # self.extend_line(point_1, point_2, result)
  95. return result
  96. def extend_line(self, point_1, point_2, result):
  97. ex_point_1 = (int(round(point_1[0] + (point_1[0] - point_2[0]) * (1 + self.shrink_ratio))),
  98. int(round(point_1[1] + (point_1[1] - point_2[1]) * (1 + self.shrink_ratio))))
  99. cv2.line(result, tuple(ex_point_1), tuple(point_1), 4096.0, 1, lineType=cv2.LINE_AA, shift=0)
  100. ex_point_2 = (int(round(point_2[0] + (point_2[0] - point_1[0]) * (1 + self.shrink_ratio))),
  101. int(round(point_2[1] + (point_2[1] - point_1[1]) * (1 + self.shrink_ratio))))
  102. cv2.line(result, tuple(ex_point_2), tuple(point_2), 4096.0, 1, lineType=cv2.LINE_AA, shift=0)
  103. return ex_point_1, ex_point_2