utils.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. inter = min(line1[1][axis], line2[1][axis]) - max(line1[0][axis], line2[0][axis])
  35. # union = max(line1[1][axis], line2[1][axis]) - min(line1[0][axis], line2[0][axis])
  36. union = min(abs(line1[0][axis]-line1[1][axis]), abs(line2[0][axis]-line2[1][axis]))
  37. if union in [0, 0.]:
  38. iou = 0.
  39. else:
  40. iou = inter / union
  41. return iou
  42. def pil_resize(image_np, height, width):
  43. image_pil = Image.fromarray(cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB))
  44. image_pil = image_pil.resize((int(width), int(height)), Image.BICUBIC)
  45. image_np = cv2.cvtColor(np.asarray(image_pil), cv2.COLOR_RGB2BGR)
  46. return image_np
  47. def get_best_predict_size2(image_np, threshold=3000):
  48. h, w = image_np.shape[:2]
  49. scale = threshold / max(h, w)
  50. h = int(h * scale)
  51. w = int(w * scale)
  52. return h, w
  53. def line_overlap(a1, a2, b1, b2):
  54. start = max(a1, b1)
  55. end = min(a2, b2)
  56. d = end - start
  57. if d < 0:
  58. return 0
  59. else:
  60. return d
  61. def get_table_iou(x1_min, y1_min, x1_max, y1_max, x2_min, y2_min, x2_max, y2_max):
  62. # 计算矩形框1的宽度、高度和面积
  63. width1 = x1_max - x1_min
  64. height1 = y1_max - y1_min
  65. area1 = width1 * height1
  66. # 计算矩形框2的宽度、高度和面积
  67. width2 = x2_max - x2_min
  68. height2 = y2_max - y2_min
  69. area2 = width2 * height2
  70. # 计算相交矩形框的左上角和右下角坐标
  71. x_intersection_min = max(x1_min, x2_min)
  72. y_intersection_min = max(y1_min, y2_min)
  73. x_intersection_max = min(x1_max, x2_max)
  74. y_intersection_max = min(y1_max, y2_max)
  75. # 计算相交矩形框的宽度和高度
  76. intersection_width = max(0, x_intersection_max - x_intersection_min)
  77. intersection_height = max(0, y_intersection_max - y_intersection_min)
  78. # 计算相交矩形框的面积
  79. intersection_area = intersection_width * intersection_height
  80. # 判断包含关系并调整相交面积
  81. if (x1_min <= x2_min) and (y1_min <= y2_min) and (x1_max >= x2_max) and (y1_max >= y2_max):
  82. union_area = area2
  83. elif (x2_min <= x1_min) and (y2_min <= y1_min) and (x2_max >= x1_max) and (y2_max >= y1_max):
  84. union_area = area1
  85. else:
  86. # 计算并集矩形框的面积
  87. # union_area = area1 + area2 - intersection_area
  88. union_area = min(area1, area2)
  89. # 计算IoU
  90. if int(union_area) == 0:
  91. iou = 0
  92. else:
  93. iou = intersection_area / union_area
  94. return iou
  95. if __name__ == '__main__':
  96. print(get_table_iou(1, 1, 4, 4, 0, 0, 3, 3))