visual.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  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. import numpy as np
  15. import cv2
  16. import time
  17. def resize_image(im, max_side_len=512):
  18. """
  19. resize image to a size multiple of max_stride which is required by the network
  20. :param im: the resized image
  21. :param max_side_len: limit of max image size to avoid out of memory in gpu
  22. :return: the resized image and the resize ratio
  23. """
  24. h, w, _ = im.shape
  25. resize_w = w
  26. resize_h = h
  27. if resize_h > resize_w:
  28. ratio = float(max_side_len) / resize_h
  29. else:
  30. ratio = float(max_side_len) / resize_w
  31. resize_h = int(resize_h * ratio)
  32. resize_w = int(resize_w * ratio)
  33. max_stride = 128
  34. resize_h = (resize_h + max_stride - 1) // max_stride * max_stride
  35. resize_w = (resize_w + max_stride - 1) // max_stride * max_stride
  36. im = cv2.resize(im, (int(resize_w), int(resize_h)))
  37. ratio_h = resize_h / float(h)
  38. ratio_w = resize_w / float(w)
  39. return im, (ratio_h, ratio_w)
  40. def resize_image_min(im, max_side_len=512):
  41. """
  42. """
  43. h, w, _ = im.shape
  44. resize_w = w
  45. resize_h = h
  46. if resize_h < resize_w:
  47. ratio = float(max_side_len) / resize_h
  48. else:
  49. ratio = float(max_side_len) / resize_w
  50. resize_h = int(resize_h * ratio)
  51. resize_w = int(resize_w * ratio)
  52. max_stride = 128
  53. resize_h = (resize_h + max_stride - 1) // max_stride * max_stride
  54. resize_w = (resize_w + max_stride - 1) // max_stride * max_stride
  55. im = cv2.resize(im, (int(resize_w), int(resize_h)))
  56. ratio_h = resize_h / float(h)
  57. ratio_w = resize_w / float(w)
  58. return im, (ratio_h, ratio_w)
  59. def resize_image_for_totaltext(im, max_side_len=512):
  60. """
  61. """
  62. h, w, _ = im.shape
  63. resize_w = w
  64. resize_h = h
  65. ratio = 1.25
  66. if h * ratio > max_side_len:
  67. ratio = float(max_side_len) / resize_h
  68. resize_h = int(resize_h * ratio)
  69. resize_w = int(resize_w * ratio)
  70. max_stride = 128
  71. resize_h = (resize_h + max_stride - 1) // max_stride * max_stride
  72. resize_w = (resize_w + max_stride - 1) // max_stride * max_stride
  73. im = cv2.resize(im, (int(resize_w), int(resize_h)))
  74. ratio_h = resize_h / float(h)
  75. ratio_w = resize_w / float(w)
  76. return im, (ratio_h, ratio_w)
  77. def point_pair2poly(point_pair_list):
  78. """
  79. Transfer vertical point_pairs into poly point in clockwise.
  80. """
  81. pair_length_list = []
  82. for point_pair in point_pair_list:
  83. pair_length = np.linalg.norm(point_pair[0] - point_pair[1])
  84. pair_length_list.append(pair_length)
  85. pair_length_list = np.array(pair_length_list)
  86. pair_info = (pair_length_list.max(), pair_length_list.min(),
  87. pair_length_list.mean())
  88. point_num = len(point_pair_list) * 2
  89. point_list = [0] * point_num
  90. for idx, point_pair in enumerate(point_pair_list):
  91. point_list[idx] = point_pair[0]
  92. point_list[point_num - 1 - idx] = point_pair[1]
  93. return np.array(point_list).reshape(-1, 2), pair_info
  94. def shrink_quad_along_width(quad, begin_width_ratio=0., end_width_ratio=1.):
  95. """
  96. Generate shrink_quad_along_width.
  97. """
  98. ratio_pair = np.array(
  99. [[begin_width_ratio], [end_width_ratio]], dtype=np.float32)
  100. p0_1 = quad[0] + (quad[1] - quad[0]) * ratio_pair
  101. p3_2 = quad[3] + (quad[2] - quad[3]) * ratio_pair
  102. return np.array([p0_1[0], p0_1[1], p3_2[1], p3_2[0]])
  103. def expand_poly_along_width(poly, shrink_ratio_of_width=0.3):
  104. """
  105. expand poly along width.
  106. """
  107. point_num = poly.shape[0]
  108. left_quad = np.array(
  109. [poly[0], poly[1], poly[-2], poly[-1]], dtype=np.float32)
  110. left_ratio = -shrink_ratio_of_width * np.linalg.norm(left_quad[0] - left_quad[3]) / \
  111. (np.linalg.norm(left_quad[0] - left_quad[1]) + 1e-6)
  112. left_quad_expand = shrink_quad_along_width(left_quad, left_ratio, 1.0)
  113. right_quad = np.array(
  114. [
  115. poly[point_num // 2 - 2], poly[point_num // 2 - 1],
  116. poly[point_num // 2], poly[point_num // 2 + 1]
  117. ],
  118. dtype=np.float32)
  119. right_ratio = 1.0 + \
  120. shrink_ratio_of_width * np.linalg.norm(right_quad[0] - right_quad[3]) / \
  121. (np.linalg.norm(right_quad[0] - right_quad[1]) + 1e-6)
  122. right_quad_expand = shrink_quad_along_width(right_quad, 0.0, right_ratio)
  123. poly[0] = left_quad_expand[0]
  124. poly[-1] = left_quad_expand[-1]
  125. poly[point_num // 2 - 1] = right_quad_expand[1]
  126. poly[point_num // 2] = right_quad_expand[2]
  127. return poly
  128. def norm2(x, axis=None):
  129. if axis:
  130. return np.sqrt(np.sum(x**2, axis=axis))
  131. return np.sqrt(np.sum(x**2))
  132. def cos(p1, p2):
  133. return (p1 * p2).sum() / (norm2(p1) * norm2(p2))