visual.py 4.6 KB

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