no hwnd check

This commit is contained in:
恍兮惚兮 2024-08-21 18:37:01 +08:00
parent 5ddd0f6fc5
commit 1ff31e4ad7

View File

@ -3,18 +3,15 @@ from myutils.config import globalconfig
import winsharedutils, windows import winsharedutils, windows
from gui.rangeselect import rangeadjust from gui.rangeselect import rangeadjust
from myutils.wrapper import threader from myutils.wrapper import threader
from myutils.ocrutil import imageCut, ocr_run, ocr_init from myutils.ocrutil import imageCut, ocr_run, ocr_init, qimage2binary
import time, gobject import time, gobject
from qtsymbols import * from qtsymbols import *
from collections import Counter from collections import Counter
from textsource.textsourcebase import basetext from textsource.textsourcebase import basetext
def qimge2np(img: QImage): def normqimage(img: QImage):
# img=img.convertToFormat(QImage.Format_Grayscale8)
shape = img.height(), img.width(), 1
img = img.scaled(128, 8 * 3) img = img.scaled(128, 8 * 3)
img.shape = shape
return img return img
@ -121,21 +118,28 @@ class ocrtext(basetext):
# 松开 # 松开
triggered = True triggered = True
laststate = this laststate = this
if triggered and gobject.baseobject.hwnd: if triggered:
for _ in range(2): if gobject.baseobject.hwnd:
# 切换前台窗口 for _ in range(2):
p1 = windows.GetWindowThreadProcessId(gobject.baseobject.hwnd) # 切换前台窗口
p2 = windows.GetWindowThreadProcessId( p1 = windows.GetWindowThreadProcessId(
windows.GetForegroundWindow() gobject.baseobject.hwnd
) )
triggered = p1 == p2 p2 = windows.GetWindowThreadProcessId(
if triggered: windows.GetForegroundWindow()
break )
time.sleep(0.1) triggered = p1 == p2
if triggered:
break
time.sleep(0.1)
for _ in range(len(self.savelastimg)):
self.savelastimg[_] = None
else:
triggered = self.waitforstablex(strict=True)
if triggered: if triggered:
time.sleep(globalconfig["ocr_trigger_delay"]) time.sleep(globalconfig["ocr_trigger_delay"])
for _ in range(len(self.savelastimg)):
self.savelastimg[_] = None
while (not self.ending) and (globalconfig["ocr_auto_method"] == 3): while (not self.ending) and (globalconfig["ocr_auto_method"] == 3):
if self.waitforstablex(): if self.waitforstablex():
break break
@ -151,24 +155,35 @@ class ocrtext(basetext):
self.dispatchtext(t) self.dispatchtext(t)
time.sleep(0.1) time.sleep(0.1)
def waitforstable(self, i, imgr): def waitforstable(self, i, imgr, strict):
imgr1 = qimge2np(imgr) if strict:
if self.savelastimg[i] is not None and ( imgr1 = imgr
imgr1.shape == self.savelastimg[i].shape else:
imgr1 = normqimage(imgr)
if (self.savelastimg[i] is not None) and (
imgr1.size() == self.savelastimg[i].size()
): ):
image_score = compareImage(imgr1, self.savelastimg[i]) if strict:
a = qimage2binary(imgr1)
b = qimage2binary(self.savelastimg[i])
image_score = a != b
else:
image_score = compareImage(imgr1, self.savelastimg[i])
else: else:
image_score = 0 image_score = 0
if i == 0: if i == 0:
try: try:
gobject.baseobject.settin_ui.threshold1label.setText(str(image_score)) gobject.baseobject.settin_ui.threshold1label.setText(str(image_score))
except: except:
pass pass
self.savelastimg[i] = imgr1 self.savelastimg[i] = imgr1
ok = image_score > globalconfig["ocr_stable_sim2"] if strict:
return ok return image_score != 0
else:
return image_score > globalconfig["ocr_stable_sim2"]
def waitforstablex(self): def waitforstablex(self, strict=False):
for i, range_ui in enumerate(self.range_ui): for i, range_ui in enumerate(self.range_ui):
rect = range_ui.getrect() rect = range_ui.getrect()
if rect is None: if rect is None:
@ -176,17 +191,17 @@ class ocrtext(basetext):
img = imageCut( img = imageCut(
gobject.baseobject.hwnd, rect[0][0], rect[0][1], rect[1][0], rect[1][1] gobject.baseobject.hwnd, rect[0][0], rect[0][1], rect[1][0], rect[1][1]
) )
if not self.waitforstable(i, img): if not self.waitforstable(i, img, strict):
return False return False
return True return True
def getresauto(self, i, imgr): def getresauto(self, i, imgr):
ok = True ok = True
if globalconfig["ocr_auto_method"] in [0, 2]: if globalconfig["ocr_auto_method"] in [0, 2]:
imgr1 = qimge2np(imgr) imgr1 = normqimage(imgr)
h, w, c = imgr1.shape
if self.savelastimg[i] is not None and ( if self.savelastimg[i] is not None and (
imgr1.shape == self.savelastimg[i].shape imgr1.size() == self.savelastimg[i].size()
): ):
image_score = compareImage(imgr1, self.savelastimg[i]) image_score = compareImage(imgr1, self.savelastimg[i])
@ -204,7 +219,7 @@ class ocrtext(basetext):
if image_score > globalconfig["ocr_stable_sim"]: if image_score > globalconfig["ocr_stable_sim"]:
if self.savelastrecimg[i] is not None and ( if self.savelastrecimg[i] is not None and (
imgr1.shape == self.savelastrecimg[i].shape imgr1.size() == self.savelastrecimg[i].size()
): ):
image_score2 = compareImage(imgr1, self.savelastrecimg[i]) image_score2 = compareImage(imgr1, self.savelastrecimg[i])
else: else:
@ -242,7 +257,7 @@ class ocrtext(basetext):
def getresmanual(self, i, imgr): def getresmanual(self, i, imgr):
text, infotype = ocr_run(imgr) text, infotype = ocr_run(imgr)
imgr1 = qimge2np(imgr) imgr1 = normqimage(imgr)
self.savelastimg[i] = imgr1 self.savelastimg[i] = imgr1
self.savelastrecimg[i] = imgr1 self.savelastrecimg[i] = imgr1
self.lastocrtime[i] = time.time() self.lastocrtime[i] = time.time()