|
@@ -1,13 +1,18 @@
|
|
|
+import logging
|
|
|
from .parts import *
|
|
|
from .utility import *
|
|
|
+from time import sleep
|
|
|
+from numpy import ndarray
|
|
|
from copy import deepcopy
|
|
|
from .argument import ArgType
|
|
|
-from concurrent.futures import ThreadPoolExecutor
|
|
|
+from threading import Thread, Lock
|
|
|
|
|
|
-__all__ = ["HuiMvOcr"]
|
|
|
+__all__ = ["Engine", "HuiMvOCR"]
|
|
|
+logger = logging.getLogger("hm-ocr")
|
|
|
+logger.setLevel(logging.INFO)
|
|
|
|
|
|
|
|
|
-class HuiMvOcr:
|
|
|
+class Engine:
|
|
|
__worker_count = 1
|
|
|
|
|
|
def __init__(self, args: "ArgType"):
|
|
@@ -19,7 +24,7 @@ class HuiMvOcr:
|
|
|
self.drop_score = args.drop_score
|
|
|
self.crop_image_res_index = 0
|
|
|
|
|
|
- def ocr_one(self, img, cls: "bool" = False, use_space: "bool" = True):
|
|
|
+ def __call__(self, img, cls: "bool" = True, use_space: "bool" = True):
|
|
|
ori_im = img.copy()
|
|
|
dt_boxes = self.det(img)
|
|
|
if dt_boxes is None:
|
|
@@ -48,9 +53,50 @@ class HuiMvOcr:
|
|
|
|
|
|
return filter_rec_res
|
|
|
|
|
|
- def ocr_multi(self, img_list, cls: "bool" = False, use_space: "bool" = True):
|
|
|
- pool = ThreadPoolExecutor(HuiMvOcr.__worker_count)
|
|
|
- loop = range(len(img_list))
|
|
|
- tasks = [pool.submit(self.ocr_one, img_list[i], cls, use_space) for i in loop]
|
|
|
|
|
|
- return [tasks[i].result() for i in loop]
|
|
|
+class HuiMvOCR:
|
|
|
+ __lock = Lock()
|
|
|
+ __tasks = [] # item: [img: "ndarray", ocr_args: "dict", callback: "fn", callback_args: "dict"]
|
|
|
+
|
|
|
+ def __init__(self, args: "ArgType"):
|
|
|
+ self.interval = args.interval
|
|
|
+
|
|
|
+ for i in range(args.workers):
|
|
|
+ Thread(target=self.__processor, args=(Engine(args), i), daemon=True).start()
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def __processor(ocr: "Engine", eid: "int"):
|
|
|
+ logger.info(f"================ Engine[{eid}] initialized ================")
|
|
|
+ while True:
|
|
|
+ if HuiMvOCR.__tasks:
|
|
|
+ HuiMvOCR.__lock.acquire()
|
|
|
+ img, ocr_args, callback, callback_args = HuiMvOCR.__tasks.pop(0)
|
|
|
+ HuiMvOCR.__lock.release()
|
|
|
+ res = ocr(img)
|
|
|
+ callback(res, **callback_args)
|
|
|
+ sleep(0.1)
|
|
|
+
|
|
|
+ def rec_one(self, img: "ndarray", cls: "bool" = True, use_space: "bool" = True):
|
|
|
+ def callback(res):
|
|
|
+ foo[1] = res
|
|
|
+ foo[0] = 1
|
|
|
+
|
|
|
+ foo = [0, None] # finish_count, result
|
|
|
+ args = {"cls": cls, "use_space": use_space}
|
|
|
+ HuiMvOCR.__tasks.append([img, args, callback, {}])
|
|
|
+ while foo[0] < 1:
|
|
|
+ sleep(self.interval)
|
|
|
+ return foo[1]
|
|
|
+
|
|
|
+ def rec_multi(self, images: "list[ndarray]", cls: "bool" = False, use_space: "bool" = False):
|
|
|
+ def callback(res, index):
|
|
|
+ foo[1][index] = res
|
|
|
+ foo[0] += 1
|
|
|
+
|
|
|
+ size, args = len(images), {"cls": cls, "use_space": use_space}
|
|
|
+ foo = [0, [...] * size] # finish_count, result
|
|
|
+ for i in range(size):
|
|
|
+ HuiMvOCR.__tasks.append([images[i], args, callback, {"index": i}])
|
|
|
+ while foo[0] < size:
|
|
|
+ sleep(self.interval)
|
|
|
+ return foo[1]
|