123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- import json
- import socket
- import traceback
- import cv2
- import requests
- from PIL import Image
- import numpy as np
- def request_post(url, param, time_out=1000, use_zlib=False):
- fails = 0
- text = json.dumps([-2])
- session_ocr = requests.Session()
- while True:
- try:
- if fails >= 1:
- break
- result = session_ocr.post(url, data=param, timeout=time_out)
- if result.status_code == 200:
- text = result.text
- break
- else:
- print('result.status_code', result.status_code)
- print('result.text', result.text)
- fails += 1
- continue
- except socket.timeout:
- fails += 1
- print('timeout! fail times:', fails)
- except:
- fails += 1
- print('fail! fail times:', fails)
- traceback.print_exc()
- return text
- def line_iou(line1, line2, axis=0):
- inter = min(line1[1][axis], line2[1][axis]) - max(line1[0][axis], line2[0][axis])
- # union = max(line1[1][axis], line2[1][axis]) - min(line1[0][axis], line2[0][axis])
- union = min(abs(line1[0][axis]-line1[1][axis]), abs(line2[0][axis]-line2[1][axis]))
- if union in [0, 0.]:
- iou = 0.
- else:
- iou = inter / union
- return iou
- def pil_resize(image_np, height, width):
- image_pil = Image.fromarray(cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB))
- image_pil = image_pil.resize((int(width), int(height)), Image.BICUBIC)
- image_np = cv2.cvtColor(np.asarray(image_pil), cv2.COLOR_RGB2BGR)
- return image_np
- def get_best_predict_size2(image_np, threshold=3000):
- h, w = image_np.shape[:2]
- scale = threshold / max(h, w)
- h = int(h * scale)
- w = int(w * scale)
- return h, w
- def line_overlap(a1, a2, b1, b2):
- start = max(a1, b1)
- end = min(a2, b2)
- d = end - start
- if d < 0:
- return 0
- else:
- return d
- def get_table_iou(x1_min, y1_min, x1_max, y1_max, x2_min, y2_min, x2_max, y2_max):
- # 计算矩形框1的宽度、高度和面积
- width1 = x1_max - x1_min
- height1 = y1_max - y1_min
- area1 = width1 * height1
- # 计算矩形框2的宽度、高度和面积
- width2 = x2_max - x2_min
- height2 = y2_max - y2_min
- area2 = width2 * height2
- # 计算相交矩形框的左上角和右下角坐标
- x_intersection_min = max(x1_min, x2_min)
- y_intersection_min = max(y1_min, y2_min)
- x_intersection_max = min(x1_max, x2_max)
- y_intersection_max = min(y1_max, y2_max)
- # 计算相交矩形框的宽度和高度
- intersection_width = max(0, x_intersection_max - x_intersection_min)
- intersection_height = max(0, y_intersection_max - y_intersection_min)
- # 计算相交矩形框的面积
- intersection_area = intersection_width * intersection_height
- # 判断包含关系并调整相交面积
- if (x1_min <= x2_min) and (y1_min <= y2_min) and (x1_max >= x2_max) and (y1_max >= y2_max):
- union_area = area2
- elif (x2_min <= x1_min) and (y2_min <= y1_min) and (x2_max >= x1_max) and (y2_max >= y1_max):
- union_area = area1
- else:
- # 计算并集矩形框的面积
- # union_area = area1 + area2 - intersection_area
- union_area = min(area1, area2)
- # 计算IoU
- if int(union_area) == 0:
- iou = 0
- else:
- iou = intersection_area / union_area
- return iou
- if __name__ == '__main__':
- print(get_table_iou(1, 1, 4, 4, 0, 0, 3, 3))
|