visual.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. h, w, _ = im.shape
  43. resize_w = w
  44. resize_h = h
  45. if resize_h < resize_w:
  46. ratio = float(max_side_len) / resize_h
  47. else:
  48. ratio = float(max_side_len) / resize_w
  49. resize_h = int(resize_h * ratio)
  50. resize_w = int(resize_w * ratio)
  51. max_stride = 128
  52. resize_h = (resize_h + max_stride - 1) // max_stride * max_stride
  53. resize_w = (resize_w + max_stride - 1) // max_stride * max_stride
  54. im = cv2.resize(im, (int(resize_w), int(resize_h)))
  55. ratio_h = resize_h / float(h)
  56. ratio_w = resize_w / float(w)
  57. return im, (ratio_h, ratio_w)
  58. def resize_image_for_totaltext(im, max_side_len=512):
  59. """ """
  60. h, w, _ = im.shape
  61. resize_w = w
  62. resize_h = h
  63. ratio = 1.25
  64. if h * ratio > max_side_len:
  65. ratio = float(max_side_len) / resize_h
  66. resize_h = int(resize_h * ratio)
  67. resize_w = int(resize_w * ratio)
  68. max_stride = 128
  69. resize_h = (resize_h + max_stride - 1) // max_stride * max_stride
  70. resize_w = (resize_w + max_stride - 1) // max_stride * max_stride
  71. im = cv2.resize(im, (int(resize_w), int(resize_h)))
  72. ratio_h = resize_h / float(h)
  73. ratio_w = resize_w / float(w)
  74. return im, (ratio_h, ratio_w)
  75. def point_pair2poly(point_pair_list):
  76. """
  77. Transfer vertical point_pairs into poly point in clockwise.
  78. """
  79. pair_length_list = []
  80. for point_pair in point_pair_list:
  81. pair_length = np.linalg.norm(point_pair[0] - point_pair[1])
  82. pair_length_list.append(pair_length)
  83. pair_length_list = np.array(pair_length_list)
  84. pair_info = (
  85. pair_length_list.max(),
  86. pair_length_list.min(),
  87. pair_length_list.mean(),
  88. )
  89. point_num = len(point_pair_list) * 2
  90. point_list = [0] * point_num
  91. for idx, point_pair in enumerate(point_pair_list):
  92. point_list[idx] = point_pair[0]
  93. point_list[point_num - 1 - idx] = point_pair[1]
  94. return np.array(point_list).reshape(-1, 2), pair_info
  95. def shrink_quad_along_width(quad, begin_width_ratio=0.0, end_width_ratio=1.0):
  96. """
  97. Generate shrink_quad_along_width.
  98. """
  99. ratio_pair = np.array([[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([poly[0], poly[1], poly[-2], poly[-1]], dtype=np.float32)
  109. left_ratio = (
  110. -shrink_ratio_of_width
  111. * np.linalg.norm(left_quad[0] - left_quad[3])
  112. / (np.linalg.norm(left_quad[0] - left_quad[1]) + 1e-6)
  113. )
  114. left_quad_expand = shrink_quad_along_width(left_quad, left_ratio, 1.0)
  115. right_quad = np.array(
  116. [
  117. poly[point_num // 2 - 2],
  118. poly[point_num // 2 - 1],
  119. poly[point_num // 2],
  120. poly[point_num // 2 + 1],
  121. ],
  122. dtype=np.float32,
  123. )
  124. right_ratio = 1.0 + shrink_ratio_of_width * np.linalg.norm(
  125. right_quad[0] - right_quad[3]
  126. ) / (np.linalg.norm(right_quad[0] - right_quad[1]) + 1e-6)
  127. right_quad_expand = shrink_quad_along_width(right_quad, 0.0, right_ratio)
  128. poly[0] = left_quad_expand[0]
  129. poly[-1] = left_quad_expand[-1]
  130. poly[point_num // 2 - 1] = right_quad_expand[1]
  131. poly[point_num // 2] = right_quad_expand[2]
  132. return poly
  133. def norm2(x, axis=None):
  134. if axis:
  135. return np.sqrt(np.sum(x**2, axis=axis))
  136. return np.sqrt(np.sum(x**2))
  137. def cos(p1, p2):
  138. return (p1 * p2).sum() / (norm2(p1) * norm2(p2))