diff --git a/LunaTranslator/LunaTranslator/gui/setting_textinput_ocr.py b/LunaTranslator/LunaTranslator/gui/setting_textinput_ocr.py index 91317110..3bc1e6ad 100644 --- a/LunaTranslator/LunaTranslator/gui/setting_textinput_ocr.py +++ b/LunaTranslator/LunaTranslator/gui/setting_textinput_ocr.py @@ -17,7 +17,7 @@ from gui.usefulwidget import ( from gui.dynalang import LPushButton import qtawesome, gobject from myutils.ocrutil import imagesolve, ocr_end, ocr_init -from myutils.wrapper import Singleton_close +from myutils.wrapper import Singleton_close, threader @Singleton_close @@ -79,6 +79,7 @@ def __label2(self): return self.threshold2label +@threader def __directinitend(engine, _ok): if _ok: ocr_init() diff --git a/LunaTranslator/LunaTranslator/myutils/ocrutil.py b/LunaTranslator/LunaTranslator/myutils/ocrutil.py index eb2a3d01..2ba2939d 100644 --- a/LunaTranslator/LunaTranslator/myutils/ocrutil.py +++ b/LunaTranslator/LunaTranslator/myutils/ocrutil.py @@ -6,7 +6,7 @@ from myutils.commonbase import ArgsEmptyExc from myutils.hwnd import screenshot from myutils.utils import stringfyerror from traceback import print_exc -import gobject, winsharedutils +import gobject, winsharedutils, threading def qimage2binary(qimage: QImage, fmt="BMP"): @@ -82,15 +82,22 @@ def imageCut(hwnd, x1, y1, x2, y2, viscompare=True, rawimage=False) -> QImage: _nowuseocr = None _ocrengine = None +_initlock = threading.Lock() def ocr_end(): global _ocrengine, _nowuseocr - _nowuseocr = None - _ocrengine = None + with _initlock: + _nowuseocr = None + _ocrengine = None def ocr_init(): + with _initlock: + __ocr_init() + + +def __ocr_init(): global _nowuseocr, _ocrengine use = None for k in globalconfig["ocr"]: @@ -126,7 +133,7 @@ def ocr_run(qimage: QImage): msg = stringfyerror(e) msg = ( "" - + (_TR(globalconfig["ocr"][_nowuseocr]["name"]) if _nowuseocr else "") + + _TR(globalconfig["ocr"][_nowuseocr]["name"]) + " " + msg )