This commit is contained in:
恍兮惚兮 2024-08-14 15:06:40 +08:00
parent 90de8735ad
commit 252f1bec1b
8 changed files with 42 additions and 29 deletions

View File

@ -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)

View File

@ -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",
),

View File

@ -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)

View File

@ -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 = "<msg_error_refresh>" + _TR(globalconfig["ocr"][use]["name"]) + " " + msg
msg = (
"<msg_error_refresh>"
+ (_TR(globalconfig["ocr"][_nowuseocr]["name"]) if _nowuseocr else "")
+ " "
+ msg
)
text = msg
return text

View File

@ -12,9 +12,6 @@ class baseocr(commonbase):
def ocr(self, imagebinary):
raise Exception
def end(self):
pass
############################################################
@property

View File

@ -189,7 +189,7 @@ def question(dialog: QDialog):
class OCR(baseocr):
def end(self):
def __del__(self):
self._ocr.trydestroy()
def initocr(self):

View File

@ -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,)

View File

@ -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()