runhey 3 лет назад
Родитель
Сommit
3873564ae6
8 измененных файлов с 297 добавлено и 7 удалено
  1. 41 0
      module/atom/ocr.py
  2. 109 0
      module/ocr/base_ocr.py
  3. 11 0
      module/ocr/models.py
  4. 32 0
      module/ocr/ocr_test.py
  5. 56 0
      module/ocr/ppocr.py
  6. 28 0
      module/ocr/sub_ocr.py
  7. 2 1
      requirements-in.txt
  8. 18 6
      requirements.txt

+ 41 - 0
module/atom/ocr.py

@@ -1,3 +1,44 @@
 # This Python file uses the following encoding: utf-8
 # @author runhey
 # github https://github.com/runhey
+
+
+import cv2
+
+from module.ocr.base_ocr import BaseCor, OcrMode, OcrMethod
+from module.ocr.sub_ocr import Full, SingleLine, 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
+

+ 109 - 0
module/ocr/base_ocr.py

@@ -0,0 +1,109 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+import time
+import cv2
+from ppocronnx.predict_system import BoxedResult
+from enum import Enum
+
+from module.base.decorator import cached_property
+from module.base.utils import area_pad, crop, float2str
+from module.ocr.ppocr import TextSystem
+from module.ocr.models import OCR_MODEL
+from module.exception import ScriptError
+from module.logger import logger
+
+
+class OcrMode(Enum):
+    FULL = 1  # str: "Full"
+    SINGLE_LINE = 2  # str: "SingleLine"
+    DIGIT = 3  # str: "Digit"
+    DIGIT_COUNTER = 4  # str: "DigitCounter"
+    DURATION = 5  # str: "Duration"
+
+class OcrMethod(Enum):
+    DEFAULT = 1  # str: "Default"
+
+class BaseCor:
+
+    lang: str = "cn"
+    score: float = 0.5  # 阈值默认为0.5
+
+    name: str = "ocr"
+    mode: OcrMode = OcrMode.FULL
+    method: OcrMethod = OcrMethod.DEFAULT  # 占位符
+    roi: list = []  # [x, y, width, height]
+    area: list = []  # [x, y, width, height]
+    keyword: str = ""  # 默认为空
+
+    @cached_property
+    def model(self) -> TextSystem:
+        return OCR_MODEL.__getattribute__(self.lang)
+
+    def pre_process(self, image):
+        """
+        重写
+        :param image:
+        :return:
+        """
+        return image
+
+    def after_process(self, result):
+        """
+        重写
+        :param result:
+        :return:
+        """
+        return result
+
+    def ocr_single_line(self, image):
+        """
+        只支持横方向的单行ocr,不支持竖方向的单行ocr
+        注意:这里使用了预处理和后处理
+        :param image:
+        :return:
+        """
+        # pre process
+        start_time = time.time()
+        image = crop(image, self.roi, copy=False)
+        image = self.pre_process(image)
+        # ocr
+        result, _ = self.model.ocr_single_line(image)
+        # after proces
+        result = self.after_process(result)
+        logger.attr(name='%s %ss' % (self.name, float2str(time.time() - start_time)),
+                    text=str(result))
+        return result
+
+    def detect_and_ocr(self, image) -> list[BoxedResult]:
+        """
+        注意:这里使用了预处理和后处理
+        :param image:
+        :return:
+        """
+        # pre process
+        start_time = time.time()
+        image = self.pre_process(image)
+        # ocr
+        results: list[BoxedResult] = self.model.detect_and_ocr(image)
+        # after proces
+        for result in results:
+            result.ocr_text = self.after_process(result.ocr_text)
+
+        logger.attr(name='%s %ss' % (self.name, float2str(time.time() - start_time)),
+                    text=str([result.ocr_text for result in results]))
+        return results
+
+    def match(self, result: str, included: bool=False) -> bool:
+        """
+        使用ocr获取结果后和keyword进行匹配
+        :param result:
+        :param included:  ocr结果和keyword是否包含关系, 要么是包含关系,要么是相等关系
+        :return:
+        """
+        if included:
+            return self.keyword in result
+        else:
+            return self.keyword == result
+
+

+ 11 - 0
module/ocr/models.py

@@ -0,0 +1,11 @@
+from module.base.decorator import cached_property
+from module.ocr.ppocr import TextSystem
+
+
+class OcrModel:
+    @cached_property
+    def ch(self):
+        return TextSystem()
+
+
+OCR_MODEL = OcrModel()

+ 32 - 0
module/ocr/ocr_test.py

@@ -0,0 +1,32 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+
+import cv2
+from module.ocr.ppocr import TextSystem
+# from ppocronnx.predict_system import TextSystem
+
+
+
+
+if __name__ == "__main__":
+
+    text_sys = TextSystem()
+
+    # 检测并识别文本
+    img = cv2.imread('D:/33357.jpg')
+    if img is None:
+        print("Error: img is None")
+        exit(1)
+
+
+    res = text_sys.detect_and_ocr(img)
+    if res is None:
+        print("Error: res is None")
+        exit(1)
+    for boxed_result in res:
+        print("{}, {:.3f}".format(boxed_result.ocr_text, boxed_result.score))
+        print(boxed_result.box)
+
+    # res = text_sys.ocr_single_line(img)
+    # print(res)

+ 56 - 0
module/ocr/ppocr.py

@@ -0,0 +1,56 @@
+import ppocronnx.predict_system
+
+
+class TextSystem(ppocronnx.predict_system.TextSystem):
+    def __init__(
+            self,
+            use_angle_cls=False,
+            box_thresh=0.6,
+            unclip_ratio=1.6,
+            rec_model_path=None,
+            det_model_path=None,
+            ort_providers=None
+    ):
+        super().__init__(
+            use_angle_cls=use_angle_cls,
+            box_thresh=box_thresh,
+            unclip_ratio=unclip_ratio,
+            rec_model_path=rec_model_path,
+            det_model_path=det_model_path,
+            ort_providers=ort_providers
+        )
+
+    # def ocr_single_line(self, img):
+    #     img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
+    #     return super().ocr_single_line(img)
+    #
+    # def detect_and_ocr(self, img: np.ndarray,**kwargs):
+    #     img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
+    #     return super().detect_and_ocr(img, **kwargs)
+
+
+def sorted_boxes(dt_boxes):
+    """
+    Sort text boxes in order from top to bottom, left to right
+    args:
+        dt_boxes(array):detected text boxes with shape [4, 2]
+    return:
+        sorted boxes(array) with shape [4, 2]
+    """
+    num_boxes = dt_boxes.shape[0]
+    sorted_boxes = sorted(dt_boxes, key=lambda x: (x[0][1], x[0][0]))
+    _boxes = list(sorted_boxes)
+
+    for i in range(num_boxes - 1):
+        for j in range(i, -1, -1):
+            if abs(_boxes[j + 1][0][1] - _boxes[j][0][1]) < 10 and \
+                    (_boxes[j + 1][0][0] < _boxes[j][0][0]):
+                tmp = _boxes[j]
+                _boxes[j] = _boxes[j + 1]
+                _boxes[j + 1] = tmp
+            else:
+                break
+    return _boxes
+
+# sorted_boxes() from PaddleOCR 2.6, newer and better than the one in ppocr-onnx
+ppocronnx.predict_system.sorted_boxes = sorted_boxes

+ 28 - 0
module/ocr/sub_ocr.py

@@ -0,0 +1,28 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+
+import cv2
+from module.ocr.ppocr import TextSystem
+from module.exception import ScriptError
+from module.ocr.base_ocr import BaseCor, OcrMode, OcrMethod
+from module.logger import logger
+
+
+class Full(BaseCor):
+    pass
+
+
+class SingleLine(BaseCor):
+    pass
+
+class Digit(BaseCor):
+    pass
+
+class DigitCounter(BaseCor):
+    pass
+
+class Duration(BaseCor):
+    pass
+
+

+ 2 - 1
requirements-in.txt

@@ -13,4 +13,5 @@ zerorpc==0.6.3
 wrapt==1.15.0
 pydantic==1.10.7
 inflection==0.5.1
-tqdm==4.65.0
+tqdm==4.65.0
+ppocr-onnx==0.0.3.9

+ 18 - 6
requirements.txt

@@ -15,43 +15,55 @@ certifi==2023.5.7         # via requests
 cffi==1.15.1              # via gevent
 charset-normalizer==3.1.0  # via requests
 cigam==0.0.3              # via apkutils2
-colorama==0.4.6           # via logzero
+colorama==0.4.6           # via logzero, tqdm
+coloredlogs==15.0.1       # via onnxruntime
 decorator==5.1.1          # via retry
 deprecated==1.2.13        # via uiautomator2
 deprecation==2.1.0        # via adbutils
 filelock==3.12.0          # via uiautomator2
+flatbuffers==23.5.26      # via onnxruntime
 future==0.18.3            # via zerorpc
 gevent==22.10.2           # via zerorpc
 greenlet==2.0.2           # via gevent
+humanfriendly==10.0       # via coloredlogs
 idna==3.4                 # via requests
 inflection==0.5.1         # via -r requirements-in.txt
 logzero==1.7.0            # via uiautomator2
 lxml==4.9.2               # via uiautomator2
 markdown-it-py==2.2.0     # via rich
 mdurl==0.1.2              # via markdown-it-py
+mpmath==1.3.0             # via sympy
 msgpack==1.0.5            # via zerorpc
-numpy==1.24.3             # via -r requirements-in.txt, opencv-python
-opencv-python==4.7.0.72   # via -r requirements-in.txt
-packaging==20.9           # via deprecation, uiautomator2
-pillow==9.5.0             # via uiautomator2
+numpy==1.24.3             # via -r requirements-in.txt, onnxruntime, opencv-python, ppocr-onnx, shapely
+onnxruntime==1.15.0       # via ppocr-onnx
+opencv-python==4.7.0.72   # via -r requirements-in.txt, ppocr-onnx
+packaging==20.9           # via deprecation, onnxruntime, uiautomator2
+pillow==9.5.0             # via ppocr-onnx, uiautomator2
+ppocr-onnx==0.0.3.9       # via -r requirements-in.txt
 progress==1.6             # via uiautomator2
+protobuf==4.23.3          # via onnxruntime
 py==1.11.0                # via retry
+pyclipper==1.3.0.post4    # via ppocr-onnx
 pycparser==2.21           # via cffi
 pydantic==1.10.7          # via -r requirements-in.txt
 pyelftools==0.29          # via apkutils2
 pygments==2.15.1          # via rich
 pyparsing==3.0.9          # via packaging
+pyreadline3==3.4.1        # via humanfriendly
 pyside6==6.4.3            # via -r requirements-in.txt
 pyside6-addons==6.4.3     # via pyside6
 pyside6-essentials==6.4.3  # via pyside6, pyside6-addons
 pywin32==306              # via -r requirements-in.txt
 pyyaml==6.0               # via -r requirements-in.txt
 pyzmq==25.0.2             # via zerorpc
-requests==2.30.0          # via adbutils, uiautomator2
+requests==2.30.0          # via adbutils, ppocr-onnx, uiautomator2
 retry==0.9.2              # via adbutils, uiautomator2
 rich==13.3.5              # via -r requirements-in.txt
+shapely==2.0.1            # via ppocr-onnx
 shiboken6==6.4.3          # via pyside6, pyside6-addons, pyside6-essentials
 six==1.16.0               # via adbutils, anytree, uiautomator2
+sympy==1.12               # via onnxruntime
+tqdm==4.65.0              # via -r requirements-in.txt
 typing-extensions==4.5.0  # via pydantic
 uiautomator2==2.16.17     # via -r requirements-in.txt
 urllib3==2.0.2            # via requests