Răsfoiți Sursa

feat(ocr) top ocr

runhey 3 ani în urmă
părinte
comite
959749c0f5
5 a modificat fișierele cu 303 adăugiri și 49 ștergeri
  1. 30 31
      module/atom/ocr.py
  2. 79 6
      module/ocr/base_ocr.py
  3. 3 3
      module/ocr/ocr_test.py
  4. 168 9
      module/ocr/sub_ocr.py
  5. 23 0
      module/ocr/utils.py

+ 30 - 31
module/atom/ocr.py

@@ -6,39 +6,38 @@
 import cv2
 
 from module.ocr.base_ocr import BaseCor, OcrMode, OcrMethod
-from module.ocr.sub_ocr import Full, SingleLine, Digit, DigitCounter, Duration
+from module.ocr.sub_ocr import Full, Single, Digit, DigitCounter, Duration
 from module.logger import logger
 
 
 
-class RuleOcr(Full, SingleLine, Digit, DigitCounter, Duration):
-
-    def __init__(self,
-                 name: str,
-                 mode: str,
-                 method: str,
-                 roi: tuple,
-                 area: tuple,
-                 keyword: str) -> None:
-        """
-
-        :param name:
-        :param mode:
-        :param method:
-        :param roi:
-        :param area:
-        :param keyword:
-        """
-        self.name = name
-        if isinstance(mode, str):
-            self.mode = OcrMode[mode.upper()]
-        elif isinstance(mode, OcrMode):
-            self.mode = mode
-        if isinstance(method, str):
-            self.method = OcrMethod[method.upper()]
-        elif isinstance(method, OcrMethod):
-            self.method = method
-        self.roi: list = list(roi)
-        self.area: list = list(area)
-        self.keyword = keyword
+class RuleOcr(Full, Single, Digit, DigitCounter, Duration):
+
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+
+
+    def after_process(self, result):
+        match self.mode:
+            case OcrMode.FULL: return Full.after_process(self, result)
+            case OcrMode.SINGLE: return Single.after_process(self, result)
+            case OcrMode.DIGIT: return Digit.after_process(self, result)
+            case OcrMode.DIGIT_COUNTER: return DigitCounter.after_process(self, result)
+            case OcrMode.DURATION: return Duration.after_process(self, result)
+            case _: return result
+
+    def ocr(self, image, keyword=None):
+
+        match self.mode:
+            case OcrMode.FULL: return Full.ocr_full(self, image, keyword)
+            case OcrMode.SINGLE: return Single.ocr_single_line(self, image)
+            case OcrMode.DIGIT: return Digit.ocr_digit(self, image)
+            case OcrMode.DIGIT_COUNTER: return DigitCounter.ocr_digit_counter(self, image)
+            case OcrMode.DURATION: return Duration.ocr_duration(self, image)
+            case _: return None
+
+
+
+if __name__ == "__main__":
+    o = RuleOcr("ocr", "Single", "Default", (1, 2, 3, 4), (1, 2, 3, 4), "keyword")
 

+ 79 - 6
module/ocr/base_ocr.py

@@ -16,7 +16,7 @@ from module.logger import logger
 
 class OcrMode(Enum):
     FULL = 1  # str: "Full"
-    SINGLE_LINE = 2  # str: "SingleLine"
+    SINGLE = 2  # str: "Single"
     DIGIT = 3  # str: "Digit"
     DIGIT_COUNTER = 4  # str: "DigitCounter"
     DURATION = 5  # str: "Duration"
@@ -27,7 +27,7 @@ class OcrMethod(Enum):
 class BaseCor:
 
     lang: str = "cn"
-    score: float = 0.5  # 阈值默认为0.5
+    score: float = 0.6  # 阈值默认为0.5
 
     name: str = "ocr"
     mode: OcrMode = OcrMode.FULL
@@ -36,6 +36,36 @@ class BaseCor:
     area: list = []  # [x, y, width, height]
     keyword: str = ""  # 默认为空
 
+
+    def __init__(self,
+                 name: str,
+                 mode: str,
+                 method: str,
+                 roi: tuple,
+                 area: tuple,
+                 keyword: str) -> None:
+        """
+
+        :param name:
+        :param mode:
+        :param method:
+        :param roi:
+        :param area:
+        :param keyword:
+        """
+        self.name = name
+        if isinstance(mode, str):
+            self.mode = OcrMode[mode.upper()]
+        elif isinstance(mode, OcrMode):
+            self.mode = mode
+        if isinstance(method, str):
+            self.method = OcrMethod[method.upper()]
+        elif isinstance(method, OcrMethod):
+            self.method = method
+        self.roi: list = list(roi)
+        self.area: list = list(area)
+        self.keyword = keyword
+
     @cached_property
     def model(self) -> TextSystem:
         return OCR_MODEL.__getattribute__(self.lang)
@@ -68,7 +98,9 @@ class BaseCor:
         image = crop(image, self.roi, copy=False)
         image = self.pre_process(image)
         # ocr
-        result, _ = self.model.ocr_single_line(image)
+        result, score = self.model.ocr_single_line(image)
+        if score < self.score:
+            result = ""
         # after proces
         result = self.after_process(result)
         logger.attr(name='%s %ss' % (self.name, float2str(time.time() - start_time)),
@@ -85,10 +117,14 @@ class BaseCor:
         start_time = time.time()
         image = self.pre_process(image)
         # ocr
-        results: list[BoxedResult] = self.model.detect_and_ocr(image)
+        boxed_results: list[BoxedResult] = self.model.detect_and_ocr(image)
+        results = []
         # after proces
-        for result in results:
-            result.ocr_text = self.after_process(result.ocr_text)
+        for result in boxed_results:
+            if result.score < self.score:
+                continue
+            boxed_results.ocr_text = self.after_process(boxed_results.ocr_text)
+            results.append(result)
 
         logger.attr(name='%s %ss' % (self.name, float2str(time.time() - start_time)),
                     text=str([result.ocr_text for result in results]))
@@ -107,3 +143,40 @@ class BaseCor:
             return self.keyword == result
 
 
+    def filter(self, boxed_results: list[BoxedResult], keyword: str=None) -> list:
+        """
+        使用ocr获取结果后和keyword进行匹配. 返回匹配的index list
+        :param keyword: 如果不指定默认适用对象的keyword
+        :param boxed_results:
+        :return:
+        """
+        # 首先先将所有的ocr的str顺序拼接起来, 然后再进行匹配
+        result = None
+        strings = [boxed_result.ocr_text for boxed_result in boxed_results]
+        concatenated_string = "".join(strings)
+        if keyword is None:
+            keyword = self.keyword
+        if keyword in concatenated_string:
+            result = [index for index, _ in enumerate(strings)]
+        else:
+            result = None
+
+        if result is not None:
+            # logger.info("Filter result: %s" % result)
+            return result
+
+        # 如果适用顺序拼接还是没有匹配到,那可能是竖排的,使用单个字节的keyword进行匹配
+        indices = []
+        for index, char in enumerate(keyword):
+            for i, string in enumerate(strings):
+                if char not in string:
+                    break
+                if i == len(strings) - 1:
+                    indices.append(index)
+        if indices:
+            return indices
+        else:
+            indices = None
+            return indices
+
+

+ 3 - 3
module/ocr/ocr_test.py

@@ -14,7 +14,7 @@ if __name__ == "__main__":
     text_sys = TextSystem()
 
     # 检测并识别文本
-    img = cv2.imread('D:/33357.jpg')
+    img = cv2.imread('D:/test_single1.jpg')
     if img is None:
         print("Error: img is None")
         exit(1)
@@ -28,5 +28,5 @@ if __name__ == "__main__":
         print("{}, {:.3f}".format(boxed_result.ocr_text, boxed_result.score))
         print(boxed_result.box)
 
-    # res = text_sys.ocr_single_line(img)
-    # print(res)
+    res = text_sys.ocr_single_line(img)
+    print(res)

+ 168 - 9
module/ocr/sub_ocr.py

@@ -3,26 +3,185 @@
 # github https://github.com/runhey
 
 import cv2
+import re
+
+from datetime import timedelta
+
 from module.ocr.ppocr import TextSystem
 from module.exception import ScriptError
+from module.base.utils import area_pad, crop, float2str
 from module.ocr.base_ocr import BaseCor, OcrMode, OcrMethod
+from module.ocr.utils import merge_area
 from module.logger import logger
 
 
 class Full(BaseCor):
-    pass
+    """
+    这个类适用于大ROI范围的文本识别。可以支持多条文本识别, 默认不支持竖方向的文本识别
+    """
+    def after_process(self, result):
+        return result
+
+    def ocr_full(self, image, keyword: str=None) -> tuple:
+        """
+        检测整个图片的文本,并对结果进行过滤。返回的是匹配到的keyword的左边。如果没有匹配到返回(0, 0, 0, 0)
+        :param image:
+        :param keyword:
+        :return:
+        """
+        boxed_results = self.detect_and_ocr(image)
+        if not boxed_results:
+            return 0, 0, 0, 0
+
+        index_list = self.filter(boxed_results, keyword)
+        # 如果一个都没有匹配到
+        if not index_list:
+            return 0, 0, 0, 0
+
+        # 如果匹配到了多个,则合并所有的坐标,返回合并后的坐标
+        if len(index_list) > 1:
+            area_list = [(
+                boxed_results[index].box[0, 0],  # x
+                boxed_results[index].box[0, 1],  # y
+                boxed_results[index].box[1, 0] - boxed_results[index].box[0, 0],     # width
+                boxed_results[index].box[2, 1] - boxed_results[index].box[0, 1],     # height
+            ) for index in index_list]
+            box = merge_area(area_list)
+            return box
+        else:
+            box = boxed_results[index_list[0]].box
+            return box[0, 0], box[0, 1], box[1, 0] - box[0, 0], box[2, 1] - box[0, 1]
+
+class Single(BaseCor):
+    """
+    这个类使用于单行文本识别(所识别的ROI不会动)
+    """
+    def after_process(self, result):
+        return result
+
+    def ocr_single(self, image) -> str:
+        """
+        检测某个固定位置的roi的文本。可以是横方向也可以是竖方向
+        :param image:
+        :return:
+        """
+        if self.roi:
+            result = self.ocr_single_line(image)
+            if result != "":
+                return result
+
+            # 如果没有识别到,就尝试逆时针旋转90度再识别一次。这个时候考虑到可能是竖方向的文本
+            image = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)
+            result = self.ocr_single_line(image)
+            if result != "":
+                return result
+
+            # 如果还是没有识别到。那可能就是真的没有识别到了
+            return ""
+        else:
+            raise ScriptError("Roi is empty")
+
+class Digit(Single):
+
+    def after_process(self, result):
+        print('this is digit after_process')
+        result = super().after_process(result)
+        result = result.replace('I', '1').replace('D', '0').replace('S', '5')
+        result = result.replace('B', '8')
+
+        prev = result
+        result = int(result) if result else 0
+        if str(result) != prev:
+            logger.warning(f'OCR {self.name}: Result "{prev}" is revised to "{result}"')
+
+        return result
+
+    def ocr_digit(self, image) -> int:
+        """
+        返回数字
+        :param image:
+        :return:
+        """
+        result = self.ocr_single(image)
+
+        if result == "":
+            return 0
+        else:
+            return int(result)
+
+class DigitCounter(Single):
+    def after_process(self, result):
+        result = super().after_process(result)
+        result = result.replace('I', '1').replace('D', '0').replace('S', '5')
+        result = result.replace('B', '8')
+        return result
+
+    @classmethod
+    def ocr_str_digit_counter(cls, result: str) -> tuple[int, int, int]:
+        result = re.search(r'(\d+)/(\d+)', result)
+        if result:
+            result = [int(s) for s in result.groups()]
+            current, total = int(result[0]), int(result[1])
+            current = min(current, total)
+            return current, total - current, total
+        else:
+            logger.warning(f'Unexpected ocr result: {result}')
+            return 0, 0, 0
+
+
+    def ocr_digit_counter(self, image) -> tuple[int, int, int]:
+        """
+        获取计数的结果
+        :param image:
+        :return: 例如 14/15,返回 (14, 1, 15) 。如果没有识别到,返回 (0, 0, 0)
+        """
+        result = self.ocr_single(image)
+        if result == "":
+            return 0, 0, 0
+        return self.ocr_str_digit_counter(result)
+
+class Duration(Single):
+    def after_process(self, result):
+        result = super().after_process(result)
+        result = result.replace('I', '1').replace('D', '0').replace('S', '5')
+        result = result.replace('B', '8')
+        return result
+
+    @staticmethod
+    def parse_time(string):
+        """
+        Args:
+            string (str): `01:30:00`
+
+        Returns:
+            datetime.timedelta:
+        """
+        result = re.search(r'(\d{1,2}):?(\d{2}):?(\d{2})', string)
+        if result:
+            result = [int(s) for s in result.groups()]
+            return timedelta(hours=result[0], minutes=result[1], seconds=result[2])
+        else:
+            logger.warning(f'Invalid duration: {string}')
+            return timedelta(hours=0, minutes=0, seconds=0)
+
+    def ocr_duration(self, image) -> timedelta:
+        """
 
+        :param image:
+        :return:
+        """
+        result = self.ocr_single(image)
 
-class SingleLine(BaseCor):
-    pass
+        if result == "":
+            return timedelta(hours=0, minutes=0, seconds=0)
 
-class Digit(BaseCor):
-    pass
+        return self.parse_time(result)
 
-class DigitCounter(BaseCor):
-    pass
 
-class Duration(BaseCor):
-    pass
+if __name__ == '__main__':
+    d = DigitCounter.ocr_str_digit_counter('14/77')
+    print(d)
 
+    u = Duration.parse_time('01:30:00')
+    print(u)
 

+ 23 - 0
module/ocr/utils.py

@@ -0,0 +1,23 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+
+
+def _merge_area(area1, area2):
+    xa1, ya1, xa2, ya2 = area1
+    xb1, yb1, xb2, yb2 = area2
+    return min(xa1, xb1), min(ya1, yb1), max(xa2, xb2), max(ya2, yb2)
+
+def merge_area(areas: list[tuple]) -> tuple:
+    """
+    合并多个区域
+    :param areas:
+    :return:
+    """
+    if not areas:
+        return 0, 0, 0, 0
+    area = areas[0]
+    for i in range(1, len(areas)):
+        area = _merge_area(area, areas[i])
+    return area
+