From f89f98eab859b2df990eec76a46e1b19f0c87c89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <101191390+HIllya51@users.noreply.github.com> Date: Wed, 14 Aug 2024 16:01:56 +0800 Subject: [PATCH] lock --- .../LunaTranslator/gui/setting_textinput_ocr.py | 3 ++- LunaTranslator/LunaTranslator/myutils/ocrutil.py | 15 +++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) 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 )