db_label_encode.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import numpy as np
  2. import json
  3. import cv2
  4. np.seterr(divide='ignore', invalid='ignore')
  5. import pyclipper
  6. from shapely.geometry import Polygon
  7. import warnings
  8. warnings.simplefilter('ignore')
  9. class DetLabelEncode(object):
  10. def __init__(self, **kwargs):
  11. pass
  12. def __call__(self, data):
  13. label = data['label']
  14. label = json.loads(label)
  15. nBox = len(label)
  16. boxes, txts, txt_tags = [], [], []
  17. for bno in range(0, nBox):
  18. box = label[bno]['points']
  19. txt = label[bno]['transcription']
  20. boxes.append(box)
  21. txts.append(txt)
  22. if txt in ['*', '###']:
  23. txt_tags.append(True)
  24. else:
  25. txt_tags.append(False)
  26. if len(boxes) == 0:
  27. return None
  28. boxes = self.expand_points_num(boxes)
  29. boxes = np.array(boxes, dtype=np.float32)
  30. txt_tags = np.array(txt_tags, dtype=np.bool_)
  31. data['polys'] = boxes
  32. data['texts'] = txts
  33. data['ignore_tags'] = txt_tags
  34. return data
  35. def order_points_clockwise(self, pts):
  36. rect = np.zeros((4, 2), dtype='float32')
  37. s = pts.sum(axis=1)
  38. rect[0] = pts[np.argmin(s)]
  39. rect[2] = pts[np.argmax(s)]
  40. tmp = np.delete(pts, (np.argmin(s), np.argmax(s)), axis=0)
  41. diff = np.diff(np.array(tmp), axis=1)
  42. rect[1] = tmp[np.argmin(diff)]
  43. rect[3] = tmp[np.argmax(diff)]
  44. return rect
  45. def expand_points_num(self, boxes):
  46. max_points_num = 0
  47. for box in boxes:
  48. if len(box) > max_points_num:
  49. max_points_num = len(box)
  50. ex_boxes = []
  51. for box in boxes:
  52. ex_box = box + [box[-1]] * (max_points_num - len(box))
  53. ex_boxes.append(ex_box)
  54. return ex_boxes
  55. class MakeBorderMap(object):
  56. def __init__(self,
  57. shrink_ratio=0.4,
  58. thresh_min=0.3,
  59. thresh_max=0.7,
  60. **kwargs):
  61. self.shrink_ratio = shrink_ratio
  62. self.thresh_min = thresh_min
  63. self.thresh_max = thresh_max
  64. if 'total_epoch' in kwargs and 'epoch' in kwargs and kwargs[
  65. 'epoch'] != 'None':
  66. self.shrink_ratio = self.shrink_ratio + 0.2 * kwargs[
  67. 'epoch'] / float(kwargs['total_epoch'])
  68. def __call__(self, data):
  69. img = data['image']
  70. text_polys = data['polys']
  71. ignore_tags = data['ignore_tags']
  72. canvas = np.zeros(img.shape[:2], dtype=np.float32)
  73. mask = np.zeros(img.shape[:2], dtype=np.float32)
  74. for i in range(len(text_polys)):
  75. if ignore_tags[i]:
  76. continue
  77. self.draw_border_map(text_polys[i], canvas, mask=mask)
  78. canvas = canvas * (self.thresh_max - self.thresh_min) + self.thresh_min
  79. data['threshold_map'] = canvas
  80. data['threshold_mask'] = mask
  81. return data
  82. def draw_border_map(self, polygon, canvas, mask):
  83. polygon = np.array(polygon)
  84. assert polygon.ndim == 2
  85. assert polygon.shape[1] == 2
  86. polygon_shape = Polygon(polygon)
  87. if polygon_shape.area <= 0:
  88. return
  89. distance = (polygon_shape.area * (1 - np.power(self.shrink_ratio, 2)) /
  90. polygon_shape.length)
  91. subject = [tuple(l) for l in polygon]
  92. padding = pyclipper.PyclipperOffset()
  93. padding.AddPath(subject, pyclipper.JT_ROUND,
  94. pyclipper.ET_CLOSEDPOLYGON)
  95. padded_polygon = np.array(padding.Execute(distance)[0])
  96. cv2.fillPoly(mask, [padded_polygon.astype(np.int32)], 1.0)
  97. xmin = padded_polygon[:, 0].min()
  98. xmax = padded_polygon[:, 0].max()
  99. ymin = padded_polygon[:, 1].min()
  100. ymax = padded_polygon[:, 1].max()
  101. width = xmax - xmin + 1
  102. height = ymax - ymin + 1
  103. polygon[:, 0] = polygon[:, 0] - xmin
  104. polygon[:, 1] = polygon[:, 1] - ymin
  105. xs = np.broadcast_to(
  106. np.linspace(0, width - 1, num=width).reshape(1, width),
  107. (height, width))
  108. ys = np.broadcast_to(
  109. np.linspace(0, height - 1, num=height).reshape(height, 1),
  110. (height, width))
  111. distance_map = np.zeros((polygon.shape[0], height, width),
  112. dtype=np.float32)
  113. for i in range(polygon.shape[0]):
  114. j = (i + 1) % polygon.shape[0]
  115. absolute_distance = self._distance(xs, ys, polygon[i], polygon[j])
  116. distance_map[i] = np.clip(absolute_distance / distance, 0, 1)
  117. distance_map = distance_map.min(axis=0)
  118. xmin_valid = min(max(0, xmin), canvas.shape[1] - 1)
  119. xmax_valid = min(max(0, xmax), canvas.shape[1] - 1)
  120. ymin_valid = min(max(0, ymin), canvas.shape[0] - 1)
  121. ymax_valid = min(max(0, ymax), canvas.shape[0] - 1)
  122. canvas[ymin_valid:ymax_valid + 1, xmin_valid:xmax_valid + 1] = np.fmax(
  123. 1 - distance_map[ymin_valid - ymin:ymax_valid - ymax + height,
  124. xmin_valid - xmin:xmax_valid - xmax + width, ],
  125. canvas[ymin_valid:ymax_valid + 1, xmin_valid:xmax_valid + 1],
  126. )
  127. def _distance(self, xs, ys, point_1, point_2):
  128. """
  129. compute the distance from point to a line
  130. ys: coordinates in the first axis
  131. xs: coordinates in the second axis
  132. point_1, point_2: (x, y), the end of the line
  133. """
  134. height, width = xs.shape[:2]
  135. square_distance_1 = np.square(xs - point_1[0]) + np.square(ys -
  136. point_1[1])
  137. square_distance_2 = np.square(xs - point_2[0]) + np.square(ys -
  138. point_2[1])
  139. square_distance = np.square(point_1[0] -
  140. point_2[0]) + np.square(point_1[1] -
  141. point_2[1])
  142. cosin = (square_distance - square_distance_1 - square_distance_2) / (
  143. 2 * np.sqrt(square_distance_1 * square_distance_2))
  144. square_sin = 1 - np.square(cosin)
  145. square_sin = np.nan_to_num(square_sin)
  146. result = np.sqrt(square_distance_1 * square_distance_2 * square_sin /
  147. square_distance)
  148. result[cosin < 0] = np.sqrt(
  149. np.fmin(square_distance_1, square_distance_2))[cosin < 0]
  150. # self.extend_line(point_1, point_2, result)
  151. return result
  152. def extend_line(self, point_1, point_2, result, shrink_ratio):
  153. ex_point_1 = (
  154. int(
  155. round(point_1[0] + (point_1[0] - point_2[0]) *
  156. (1 + shrink_ratio))),
  157. int(
  158. round(point_1[1] + (point_1[1] - point_2[1]) *
  159. (1 + shrink_ratio))),
  160. )
  161. cv2.line(
  162. result,
  163. tuple(ex_point_1),
  164. tuple(point_1),
  165. 4096.0,
  166. 1,
  167. lineType=cv2.LINE_AA,
  168. shift=0,
  169. )
  170. ex_point_2 = (
  171. int(
  172. round(point_2[0] + (point_2[0] - point_1[0]) *
  173. (1 + shrink_ratio))),
  174. int(
  175. round(point_2[1] + (point_2[1] - point_1[1]) *
  176. (1 + shrink_ratio))),
  177. )
  178. cv2.line(
  179. result,
  180. tuple(ex_point_2),
  181. tuple(point_2),
  182. 4096.0,
  183. 1,
  184. lineType=cv2.LINE_AA,
  185. shift=0,
  186. )
  187. return ex_point_1, ex_point_2
  188. class MakeShrinkMap(object):
  189. r"""
  190. Making binary mask from detection data with ICDAR format.
  191. Typically following the process of class `MakeICDARData`.
  192. """
  193. def __init__(self, min_text_size=8, shrink_ratio=0.4, **kwargs):
  194. self.min_text_size = min_text_size
  195. self.shrink_ratio = shrink_ratio
  196. if 'total_epoch' in kwargs and 'epoch' in kwargs and kwargs[
  197. 'epoch'] != 'None':
  198. self.shrink_ratio = self.shrink_ratio + 0.2 * kwargs[
  199. 'epoch'] / float(kwargs['total_epoch'])
  200. def __call__(self, data):
  201. image = data['image']
  202. text_polys = data['polys']
  203. ignore_tags = data['ignore_tags']
  204. h, w = image.shape[:2]
  205. text_polys, ignore_tags = self.validate_polygons(
  206. text_polys, ignore_tags, h, w)
  207. gt = np.zeros((h, w), dtype=np.float32)
  208. mask = np.ones((h, w), dtype=np.float32)
  209. for i in range(len(text_polys)):
  210. polygon = text_polys[i]
  211. height = max(polygon[:, 1]) - min(polygon[:, 1])
  212. width = max(polygon[:, 0]) - min(polygon[:, 0])
  213. if ignore_tags[i] or min(height, width) < self.min_text_size:
  214. cv2.fillPoly(mask,
  215. polygon.astype(np.int32)[np.newaxis, :, :], 0)
  216. ignore_tags[i] = True
  217. else:
  218. polygon_shape = Polygon(polygon)
  219. subject = [tuple(l) for l in polygon]
  220. padding = pyclipper.PyclipperOffset()
  221. padding.AddPath(subject, pyclipper.JT_ROUND,
  222. pyclipper.ET_CLOSEDPOLYGON)
  223. shrunk = []
  224. # Increase the shrink ratio every time we get multiple polygon returned back
  225. possible_ratios = np.arange(self.shrink_ratio, 1,
  226. self.shrink_ratio)
  227. np.append(possible_ratios, 1)
  228. # print(possible_ratios)
  229. for ratio in possible_ratios:
  230. # print(f"Change shrink ratio to {ratio}")
  231. distance = (polygon_shape.area * (1 - np.power(ratio, 2)) /
  232. polygon_shape.length)
  233. shrunk = padding.Execute(-distance)
  234. if len(shrunk) == 1:
  235. break
  236. if shrunk == []:
  237. cv2.fillPoly(mask,
  238. polygon.astype(np.int32)[np.newaxis, :, :], 0)
  239. ignore_tags[i] = True
  240. continue
  241. for each_shrink in shrunk:
  242. shrink = np.array(each_shrink).reshape(-1, 2)
  243. cv2.fillPoly(gt, [shrink.astype(np.int32)], 1)
  244. data['shrink_map'] = gt
  245. data['shrink_mask'] = mask
  246. return data
  247. def validate_polygons(self, polygons, ignore_tags, h, w):
  248. """
  249. polygons (numpy.array, required): of shape (num_instances, num_points, 2)
  250. """
  251. if len(polygons) == 0:
  252. return polygons, ignore_tags
  253. assert len(polygons) == len(ignore_tags)
  254. for polygon in polygons:
  255. polygon[:, 0] = np.clip(polygon[:, 0], 0, w - 1)
  256. polygon[:, 1] = np.clip(polygon[:, 1], 0, h - 1)
  257. for i in range(len(polygons)):
  258. area = self.polygon_area(polygons[i])
  259. if abs(area) < 1:
  260. ignore_tags[i] = True
  261. if area > 0:
  262. polygons[i] = polygons[i][::-1, :]
  263. return polygons, ignore_tags
  264. def polygon_area(self, polygon):
  265. """
  266. compute polygon area
  267. """
  268. area = 0
  269. q = polygon[-1]
  270. for p in polygon:
  271. area += p[0] * q[1] - p[1] * q[0]
  272. q = p
  273. return area / 2.0