utils.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import json
  2. import socket
  3. import traceback
  4. import cv2
  5. import requests
  6. from PIL import Image
  7. import numpy as np
  8. def request_post(url, param, time_out=1000, use_zlib=False):
  9. fails = 0
  10. text = json.dumps([-2])
  11. session_ocr = requests.Session()
  12. while True:
  13. try:
  14. if fails >= 1:
  15. break
  16. result = session_ocr.post(url, data=param, timeout=time_out)
  17. if result.status_code == 200:
  18. text = result.text
  19. break
  20. else:
  21. print('result.status_code', result.status_code)
  22. print('result.text', result.text)
  23. fails += 1
  24. continue
  25. except socket.timeout:
  26. fails += 1
  27. print('timeout! fail times:', fails)
  28. except:
  29. fails += 1
  30. print('fail! fail times:', fails)
  31. traceback.print_exc()
  32. return text
  33. def line_iou(line1, line2, axis=0):
  34. if line1[0][axis] <= line2[0][axis] <= line2[1][axis] <= line1[1][axis]:
  35. return 1.
  36. if line2[0][axis] <= line1[0][axis] <= line1[1][axis] <= line2[1][axis]:
  37. return 1.
  38. inter = min(line1[1][axis], line2[1][axis]) - max(line1[0][axis], line2[0][axis])
  39. # union = max(line1[1][axis], line2[1][axis]) - min(line1[0][axis], line2[0][axis])
  40. union = min(abs(line1[0][axis]-line1[1][axis]), abs(line2[0][axis]-line2[1][axis]))
  41. if union in [0, 0.]:
  42. iou = 0.
  43. else:
  44. iou = inter / union
  45. return iou
  46. def pil_resize(image_np, height, width):
  47. image_pil = Image.fromarray(cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB))
  48. image_pil = image_pil.resize((int(width), int(height)), Image.BICUBIC)
  49. image_np = cv2.cvtColor(np.asarray(image_pil), cv2.COLOR_RGB2BGR)
  50. return image_np
  51. def get_best_predict_size2(image_np, threshold=3000):
  52. h, w = image_np.shape[:2]
  53. scale = threshold / max(h, w)
  54. h = int(h * scale)
  55. w = int(w * scale)
  56. return h, w
  57. def line_overlap(a1, a2, b1, b2):
  58. start = max(a1, b1)
  59. end = min(a2, b2)
  60. d = end - start
  61. if d < 0:
  62. return 0
  63. else:
  64. return d
  65. def get_table_iou(x1_min, y1_min, x1_max, y1_max, x2_min, y2_min, x2_max, y2_max):
  66. # 计算矩形框1的宽度、高度和面积
  67. width1 = x1_max - x1_min
  68. height1 = y1_max - y1_min
  69. area1 = width1 * height1
  70. # 计算矩形框2的宽度、高度和面积
  71. width2 = x2_max - x2_min
  72. height2 = y2_max - y2_min
  73. area2 = width2 * height2
  74. # 计算相交矩形框的左上角和右下角坐标
  75. x_intersection_min = max(x1_min, x2_min)
  76. y_intersection_min = max(y1_min, y2_min)
  77. x_intersection_max = min(x1_max, x2_max)
  78. y_intersection_max = min(y1_max, y2_max)
  79. # 计算相交矩形框的宽度和高度
  80. intersection_width = max(0, x_intersection_max - x_intersection_min)
  81. intersection_height = max(0, y_intersection_max - y_intersection_min)
  82. # 计算相交矩形框的面积
  83. intersection_area = intersection_width * intersection_height
  84. # 判断包含关系并调整相交面积
  85. if (x1_min <= x2_min) and (y1_min <= y2_min) and (x1_max >= x2_max) and (y1_max >= y2_max):
  86. union_area = area2
  87. elif (x2_min <= x1_min) and (y2_min <= y1_min) and (x2_max >= x1_max) and (y2_max >= y1_max):
  88. union_area = area1
  89. else:
  90. # 计算并集矩形框的面积
  91. # union_area = area1 + area2 - intersection_area
  92. union_area = min(area1, area2)
  93. # 计算IoU
  94. if int(union_area) == 0:
  95. iou = 0
  96. else:
  97. iou = intersection_area / union_area
  98. return iou
  99. if __name__ == '__main__':
  100. print(get_table_iou(1, 1, 4, 4, 0, 0, 3, 3))