line_parser.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import numpy as np
  2. from dataclasses import dataclass
  3. # result 对象
  4. @dataclass
  5. class OcrResult(object):
  6. box: np.ndarray
  7. txt: str
  8. conf: float
  9. def __hash__(self):
  10. return hash(repr(self))
  11. def __repr__(self):
  12. return f'txt: {self.txt}, box: {self.box.tolist()}, conf: {self.conf}'
  13. @property
  14. def lt(self):
  15. l, t = np.min(self.box, 0)
  16. return [l, t]
  17. @property
  18. def rb(self):
  19. r, b = np.max(self.box, 0)
  20. return [r, b]
  21. @property
  22. def wh(self):
  23. l, t = self.lt
  24. r, b = self.rb
  25. return [r - l, b - t]
  26. @property
  27. def area(self):
  28. w, h = self.wh
  29. return w * h
  30. @property
  31. def is_slope(self):
  32. p0 = self.box[0]
  33. p1 = self.box[1]
  34. if p0[0] == p1[0]:
  35. return False
  36. slope = abs(1. * (p0[1] - p1[1]) / (p0[0] - p1[0]))
  37. return 0.4 < slope < 2.5
  38. @property
  39. def center(self):
  40. l, t = self.lt
  41. r, b = self.rb
  42. return [(r + l) / 2, (b + t) / 2]
  43. def one_line(self, b, is_horizontal, eps: float = 20.0) -> bool:
  44. y_idx = 0 + is_horizontal
  45. x_idx = 1 - y_idx
  46. if b.lt[x_idx] < self.lt[x_idx] < self.rb[x_idx] < b.rb[x_idx]: return False
  47. if self.lt[x_idx] < b.lt[x_idx] < b.rb[x_idx] < self.rb[x_idx]: return False
  48. eps = 0.25 * (self.wh[y_idx] + b.wh[y_idx])
  49. dist = abs(self.center[y_idx] - b.center[y_idx])
  50. return dist < eps
  51. # 行处理器
  52. class LineParser(object):
  53. def __init__(self, ocr_raw_result, filters=None):
  54. if filters is None:
  55. filters = [lambda x: x.is_slope]
  56. self.ocr_res = []
  57. for re in ocr_raw_result:
  58. o = OcrResult(np.array(re[0]), re[1][0], re[1][1])
  59. if any([f(o) for f in filters]): continue
  60. self.ocr_res.append(o)
  61. # for f in filters:
  62. # self.ocr_res = list(filter(f, self.ocr_res))
  63. self.ocr_res = sorted(self.ocr_res, key=lambda x: x.area, reverse=True)
  64. self.eps = self.avg_height * 0.7
  65. @property
  66. def is_horizontal(self):
  67. res = self.ocr_res
  68. wh = np.stack([np.abs(np.array(r.lt) - np.array(r.rb)) for r in res])
  69. return np.sum(wh[:, 0] > wh[:, 1]) > np.sum(wh[:, 0] < wh[:, 1])
  70. @property
  71. def avg_height(self):
  72. idx = self.is_horizontal + 0
  73. return np.mean(np.array([r.wh[idx] for r in self.ocr_res]))
  74. # 整体置信度
  75. @property
  76. def confidence(self):
  77. return np.mean([r.conf for r in self.ocr_res])
  78. # 处理器函数
  79. def parse(self, eps=40.0):
  80. # 存返回值
  81. res = []
  82. # 需要 处理的 OcrResult 对象 的长度
  83. length = len(self.ocr_res)
  84. # 如果字段数 小于等于1 就抛出异常
  85. if length <= 1:
  86. raise Exception('无法识别')
  87. # 遍历数组 并处理他
  88. for i in range(length):
  89. res_i = self.ocr_res[i]
  90. # 这次的 res_i 之前已经在结果集中,就继续下一个
  91. if any(map(lambda x: res_i in x, res)): continue
  92. res_row = set()
  93. for j in range(i, length):
  94. res_j = self.ocr_res[j]
  95. # 这次的 res_i 之前已经在结果集中,就继续下一个
  96. if any(map(lambda x: res_j in x, res)): continue
  97. if res_i.one_line(res_j, self.is_horizontal, self.eps):
  98. res_row.add(res_j)
  99. res.append(res_row)
  100. idx = self.is_horizontal + 0
  101. res = sorted([sorted(list(r), key=lambda x: x.lt[1 - idx]) for r in res], key=lambda x: x[0].lt[idx])
  102. for row in res:
  103. print('---')
  104. print(''.join([r.txt for r in row]))
  105. return res