瀏覽代碼

update lineparser

Zhang Li 2 年之前
父節點
當前提交
710d632ecd
共有 2 個文件被更改,包括 15 次插入8 次删除
  1. 15 6
      core/line_parser.py
  2. 0 2
      testing/true_test.py

+ 15 - 6
core/line_parser.py

@@ -30,12 +30,20 @@ class OcrResult(object):
         r, b = self.rb
         return [r - l, b - t]
 
-    def one_line(self, b, is_horizontal, eps: float = 20.0) -> bool:
-        if is_horizontal:
-            return abs(self.lt[1] - b.lt[1]) < eps
-        else:
-            return abs(self.rb[0] - b.rb[0]) < eps
+    @property
+    def center(self):
+        l, t = self.lt
+        r, b = self.rb
+        return [(r + l) / 2, (b + t) / 2]
 
+    def one_line(self, b, is_horizontal, eps: float = 20.0) -> bool:
+        y_idx = 0 + is_horizontal
+        x_idx = 1 - y_idx
+        if b.lt[x_idx] < self.lt[x_idx] < self.rb[x_idx] < b.rb[x_idx]: return False
+        if self.lt[x_idx] < b.lt[x_idx] < b.rb[x_idx] < self.rb[x_idx]: return False
+        eps = 0.5 * (self.wh[y_idx] + b.wh[y_idx])
+        dist = abs(self.center[y_idx] - b.center[y_idx])
+        return dist < eps
 
 # 行处理器
 class LineParser(object):
@@ -87,8 +95,9 @@ class LineParser(object):
             for j in range(i, length):
                 res_j = self.ocr_res[j]
                 if res_i.one_line(res_j, self.is_horizontal, self.eps):
+                    if any(map(lambda x: res_j in x, res)): continue
                     # LineParser 对象  不可以直接加入字典
                     res_row.add(res_j)
             res.append(res_row)
         idx = self.is_horizontal + 0
-        return sorted([list(r) for r in res], key=lambda x: x[0].lt[idx])
+        return sorted([sorted(list(r), key=lambda x: x.lt[1-idx]) for r in res], key=lambda x: x[0].lt[idx])

+ 0 - 2
testing/true_test.py

@@ -1,8 +1,6 @@
-import base64
 import unittest
 from pathlib import Path
 
-import requests
 
 from testing.utils import send_request