From 252f1bec1b6c12bad8b7e22b1914b20abcac018a 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 15:06:40 +0800 Subject: [PATCH] fix --- .../LunaTranslator/gui/rangeselect.py | 2 + .../gui/setting_textinput_ocr.py | 11 ++++- .../LunaTranslator/gui/usefulwidget.py | 1 - .../LunaTranslator/myutils/ocrutil.py | 41 ++++++++++--------- .../LunaTranslator/ocrengines/baseocrclass.py | 3 -- .../LunaTranslator/ocrengines/local.py | 2 +- .../LunaTranslator/ocrengines/weixinocr.py | 2 +- .../LunaTranslator/textsource/ocrtext.py | 9 +++- 8 files changed, 42 insertions(+), 29 deletions(-) diff --git a/LunaTranslator/LunaTranslator/gui/rangeselect.py b/LunaTranslator/LunaTranslator/gui/rangeselect.py index 5bbe0152..e2dd79d7 100644 --- a/LunaTranslator/LunaTranslator/gui/rangeselect.py +++ b/LunaTranslator/LunaTranslator/gui/rangeselect.py @@ -6,6 +6,7 @@ from gui.dynalang import LAction class rangeadjust(Mainw): + closesignal = pyqtSignal() traceoffsetsignal = pyqtSignal(QPoint) def starttrace(self, pos): @@ -42,6 +43,7 @@ class rangeadjust(Mainw): self.traceoffsetsignal.connect(self.traceoffset) self.label = QLabel(self) self.setstyle() + self.closesignal.connect(self.close) self.tracepos = QPoint() self.drag_label = QLabel(self) self.drag_label.setGeometry(0, 0, 4000, 2000) diff --git a/LunaTranslator/LunaTranslator/gui/setting_textinput_ocr.py b/LunaTranslator/LunaTranslator/gui/setting_textinput_ocr.py index 5d3746d3..91317110 100644 --- a/LunaTranslator/LunaTranslator/gui/setting_textinput_ocr.py +++ b/LunaTranslator/LunaTranslator/gui/setting_textinput_ocr.py @@ -16,7 +16,7 @@ from gui.usefulwidget import ( ) from gui.dynalang import LPushButton import qtawesome, gobject -from myutils.ocrutil import imagesolve +from myutils.ocrutil import imagesolve, ocr_end, ocr_init from myutils.wrapper import Singleton_close @@ -79,6 +79,13 @@ def __label2(self): return self.threshold2label +def __directinitend(engine, _ok): + if _ok: + ocr_init() + else: + ocr_end() + + def initgridsources(self, names): line = [] i = 0 @@ -111,7 +118,7 @@ def initgridsources(self, names): globalconfig["ocr"], "ocrswitchs", name, - None, + __directinitend, ), pair="ocrswitchs", ), diff --git a/LunaTranslator/LunaTranslator/gui/usefulwidget.py b/LunaTranslator/LunaTranslator/gui/usefulwidget.py index 13049946..3b23ee66 100644 --- a/LunaTranslator/LunaTranslator/gui/usefulwidget.py +++ b/LunaTranslator/LunaTranslator/gui/usefulwidget.py @@ -970,7 +970,6 @@ def yuitsu_switch(parent, configdict, dictobjectn, key, callback, checked): for k in dictobject: configdict[k]["use"] = k == key dictobject[k].setChecked(k == key) - if callback: callback(key, checked) diff --git a/LunaTranslator/LunaTranslator/myutils/ocrutil.py b/LunaTranslator/LunaTranslator/myutils/ocrutil.py index 2576cb8b..eb2a3d01 100644 --- a/LunaTranslator/LunaTranslator/myutils/ocrutil.py +++ b/LunaTranslator/LunaTranslator/myutils/ocrutil.py @@ -86,20 +86,12 @@ _ocrengine = None def ocr_end(): global _ocrengine, _nowuseocr - try: - _ocrengine.end() - except: - pass _nowuseocr = None _ocrengine = None -def ocr_run(qimage: QImage): - image = qimage2binary(qimage, "PNG") - if not image: - return "" +def ocr_init(): global _nowuseocr, _ocrengine - use = None for k in globalconfig["ocr"]: if globalconfig["ocr"][k]["use"] == True and os.path.exists( @@ -108,17 +100,23 @@ def ocr_run(qimage: QImage): use = k break if use is None: - return "" + raise Exception("no engine") + if _nowuseocr == use: + return + _ocrengine = None + _nowuseocr = None + aclass = importlib.import_module("ocrengines." + use).OCR + _ocrengine = aclass(use) + _nowuseocr = use + +def ocr_run(qimage: QImage): + image = qimage2binary(qimage, "PNG") + if not image: + return "" + global _nowuseocr, _ocrengine try: - if _nowuseocr != use: - try: - _ocrengine.end() - except: - pass - aclass = importlib.import_module("ocrengines." + use).OCR - _ocrengine = aclass(use) - _nowuseocr = use + ocr_init() text = _ocrengine._private_ocr(image) except Exception as e: if isinstance(e, ArgsEmptyExc): @@ -126,7 +124,12 @@ def ocr_run(qimage: QImage): else: print_exc() msg = stringfyerror(e) - msg = "" + _TR(globalconfig["ocr"][use]["name"]) + " " + msg + msg = ( + "" + + (_TR(globalconfig["ocr"][_nowuseocr]["name"]) if _nowuseocr else "") + + " " + + msg + ) text = msg return text diff --git a/LunaTranslator/LunaTranslator/ocrengines/baseocrclass.py b/LunaTranslator/LunaTranslator/ocrengines/baseocrclass.py index ac89faa6..edcc4fc1 100644 --- a/LunaTranslator/LunaTranslator/ocrengines/baseocrclass.py +++ b/LunaTranslator/LunaTranslator/ocrengines/baseocrclass.py @@ -12,9 +12,6 @@ class baseocr(commonbase): def ocr(self, imagebinary): raise Exception - def end(self): - pass - ############################################################ @property diff --git a/LunaTranslator/LunaTranslator/ocrengines/local.py b/LunaTranslator/LunaTranslator/ocrengines/local.py index 826fbf0b..51579807 100644 --- a/LunaTranslator/LunaTranslator/ocrengines/local.py +++ b/LunaTranslator/LunaTranslator/ocrengines/local.py @@ -189,7 +189,7 @@ def question(dialog: QDialog): class OCR(baseocr): - def end(self): + def __del__(self): self._ocr.trydestroy() def initocr(self): diff --git a/LunaTranslator/LunaTranslator/ocrengines/weixinocr.py b/LunaTranslator/LunaTranslator/ocrengines/weixinocr.py index 5ec3082c..7077518d 100644 --- a/LunaTranslator/LunaTranslator/ocrengines/weixinocr.py +++ b/LunaTranslator/LunaTranslator/ocrengines/weixinocr.py @@ -68,7 +68,7 @@ class OCR(baseocr): ) return wechatocr_path, wechat_path - def end(self): + def __del__(self): wcocr_destroy = self.wcocr.wcocr_destroy wcocr_destroy.argtypes = (c_void_p,) diff --git a/LunaTranslator/LunaTranslator/textsource/ocrtext.py b/LunaTranslator/LunaTranslator/textsource/ocrtext.py index ad17511e..75ff1cf3 100644 --- a/LunaTranslator/LunaTranslator/textsource/ocrtext.py +++ b/LunaTranslator/LunaTranslator/textsource/ocrtext.py @@ -2,9 +2,10 @@ import time from myutils.config import globalconfig import winsharedutils from gui.rangeselect import rangeadjust -from myutils.ocrutil import imageCut, ocr_run, ocr_end +from myutils.ocrutil import imageCut, ocr_run, ocr_end, ocr_init import time, gobject from qtsymbols import * +from traceback import print_exc from textsource.textsourcebase import basetext @@ -34,6 +35,10 @@ def compareImage(img1: QImage, img2): class ocrtext(basetext): def __init__(self): + try: + ocr_init() + except: + print_exc() self.screen = QApplication.primaryScreen() self.savelastimg = [] self.savelastrecimg = [] @@ -178,6 +183,6 @@ class ocrtext(basetext): def end(self): globalconfig["ocrregions"] = [_.getrect() for _ in self.range_ui] - [_.close() for _ in self.range_ui] + [_.closesignal.emit() for _ in self.range_ui] super().end() ocr_end()