issues/523

This commit is contained in:
恍兮惚兮 2024-02-21 23:59:46 +08:00
parent f922532b44
commit 3e06c382f1
26 changed files with 131 additions and 93 deletions

View File

@ -16,7 +16,7 @@ class rangeadjust(Mainw) :
self.drag_label = QLabel(self)
self.drag_label.setGeometry(0, 0, 4000, 2000)
self._isTracking=False
self._rect=None
self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint | Qt.Tool)
self.setAttribute(Qt.WA_TranslucentBackground)
@ -39,11 +39,7 @@ class rangeadjust(Mainw) :
self._endPos = None
def moveEvent(self,e):
rect = self.geometry()
try:
if self.isVisible():
gobject.baseobject.textsource.rect=[(rect.left(),rect.top()),(rect.right(),rect.bottom())]
except:
pass
self._rect=[(rect.left()+globalconfig['ocrrangewidth'],rect.top()+globalconfig['ocrrangewidth']),(rect.right()-globalconfig['ocrrangewidth'],rect.bottom()-globalconfig['ocrrangewidth'])]
def enterEvent(self, QEvent) :
self.drag_label.setStyleSheet("background-color:rgba(0,0,0, 0.1)")
def leaveEvent(self, QEvent):
@ -52,12 +48,10 @@ class rangeadjust(Mainw) :
self.label.setGeometry(0, 0, self.width(), self.height())
rect = self.geometry()
try:
if self.isVisible():
gobject.baseobject.textsource.rect=[(rect.left(),rect.top()),(rect.right(),rect.bottom())]
except:pass
self._rect=[(rect.left()+globalconfig['ocrrangewidth'],rect.top()+globalconfig['ocrrangewidth']),(rect.right()-globalconfig['ocrrangewidth'],rect.bottom()-globalconfig['ocrrangewidth'])]
super(rangeadjust, self).resizeEvent(a0)
def getrect(self):
return self._rect
class rangeselct(QMainWindow) :
def __init__(self, parent ) :

View File

@ -49,6 +49,7 @@ def getocrgrid(self) :
[(("图像一致性阈值"),8),(getspinbox(0,1,globalconfig,'ocr_diff_sim' ,double=True,step=0.01 ,dec=3),4),(self.threshold2label,4)],
[],
[(("多重区域模式"),12),getsimpleswitch(globalconfig ,'multiregion')],
[(("OCR范围框颜色"),12),(getcolorbutton(globalconfig,'ocrrangecolor',callback=lambda : selectcolor(self,globalconfig,'ocrrangecolor',self.ocrrangecolor_button,callback=lambda :gobject.baseobject.textsource.setstyle()),name='ocrrangecolor_button',parent=self),1)],
[(("OCR范围框宽度"),12),(getspinbox(1,100,globalconfig,'ocrrangewidth' ,callback=lambda x: gobject.baseobject.textsource.setstyle() ),4)],
[(("选取OCR范围后立即进行一次识别"),12),getsimpleswitch(globalconfig ,'ocrafterrangeselect')],

View File

@ -531,6 +531,7 @@ class QUnFrameWindow(resizableframeless):
rangeselct_function(self,self.afterrange,auto,auto)
def afterrange(self,rect):
gobject.baseobject.textsource.newrangeadjustor()
gobject.baseobject.textsource.setrect(rect)
self.showhideocrrange()
if globalconfig['showrangeafterrangeselect']==False:

View File

@ -36,7 +36,7 @@ def imagesolve(image):
elif globalconfig['ocr_presolve_method']==3:
image2=otsu_threshold_fast(image,-1)
return image2
def imageCut(hwnd,x1,y1,x2,y2):
def imageCut(hwnd,x1,y1,x2,y2,viscompare=True):
screen = QApplication.primaryScreen()
for _ in range(2):
@ -48,10 +48,11 @@ def imageCut(hwnd,x1,y1,x2,y2):
if rect is None:
continue
x1,y1=windows.ScreenToClient(hwnd,x1,y1)
x2,y2=windows.ScreenToClient(hwnd,x2,y2)
rate=dynamic_rate(hwnd,rect)
pix = screen.grabWindow(hwnd, (x1)/rate, (y1)/rate, (x2-x1)/rate, (y2-y1)/rate)
hwndrate=windows.GetDpiForWindow(hwnd)/96
x1,y1=windows.ScreenToClient(hwnd,x1*rate*hwndrate,y1*rate*hwndrate)
x2,y2=windows.ScreenToClient(hwnd,x2*rate*hwndrate,y2*rate*hwndrate)
pix = screen.grabWindow(hwnd, int(x1/rate/rate/hwndrate), int(y1/rate/rate/hwndrate), int((x2-x1)/rate/rate/hwndrate), int((y2-y1)/rate/rate/hwndrate))
if pix.toImage().allGray():
continue
break
@ -62,7 +63,8 @@ def imageCut(hwnd,x1,y1,x2,y2):
image= pix.toImage()
image2=imagesolve(image)
gobject.baseobject.showocrimage.setimage.emit([image,image2])
if viscompare:
gobject.baseobject.showocrimage.setimage.emit([image,image2])
return image2
_nowuseocr=None

View File

@ -34,7 +34,7 @@ def _2_f(line,args):
while i<len(line):
newline+=line[i]
nextn=line[i:i+guesstimes]
print(guesstimes,nextn,len(set(nextn)))
#print(guesstimes,nextn,len(set(nextn)))
if len(nextn)==guesstimes and len(set(nextn))==1:
i+=guesstimes
else:

View File

@ -9,6 +9,7 @@ import ctypes
import time
import ctypes.wintypes
import time
from myutils.hwnd import getScreenRate
from traceback import print_exc
from myutils.config import globalconfig,static_data,savehook_new_list,savehook_new_data,getdefaultsavehook,translatorsetting
import threading,queue
@ -267,7 +268,8 @@ def minmaxmoveobservefunc(self):
elif event == windows.EVENT_SYSTEM_MOVESIZEEND: #
if globalconfig['movefollow']:
if self.lastpos:
self.hookfollowsignal.emit(5,(rect[0]-self.lastpos[0],rect[1]-self.lastpos[1]))
rate=getScreenRate()
self.hookfollowsignal.emit(5,(int((rect[0]-self.lastpos[0])/rate),int((rect[1]-self.lastpos[1])/rate)))
except:
print_exc()

View File

@ -38,102 +38,112 @@ class ocrtext(basetext):
def __init__(self) :
self.screen = QApplication.primaryScreen()
self.savelastimg=None
self.savelastrecimg=None
self.savelasttext=None
self.lastocrtime=0
self.rect=None
self.range_ui = rangeadjust(gobject.baseobject.translation_ui)
self.savelastimg=[]
self.savelastrecimg=[]
self.savelasttext=[]
self.lastocrtime=[]
self.range_ui = []
self.timestamp=time.time()
super(ocrtext,self ).__init__('0','ocr')
def resetrect(self):
self.rect=None
self.range_ui.hide()
def newrangeadjustor(self):
if len(self.range_ui)==0 or globalconfig['multiregion']:
self.range_ui.append(rangeadjust(gobject.baseobject.translation_ui))
self.savelastimg.append(None)
self.savelastrecimg.append(None)
self.lastocrtime.append(0)
self.savelasttext.append(None)
def moveui(self,x,y):
_r=self.range_ui
_r.move(_r.pos().x()+ x,_r.pos().y()+ y)
for _r in self.range_ui:
_r.move(_r.pos().x()+ x,_r.pos().y()+ y)
def setrect(self,rect):
(x1,y1),(x2,y2)=rect
self.rect=[(x1,y1),(x2,y2)]
self.range_ui.setGeometry(x1-globalconfig['ocrrangewidth'],y1-globalconfig['ocrrangewidth'],x2-x1+2*globalconfig['ocrrangewidth'],y2-y1+2*globalconfig['ocrrangewidth'])
self.range_ui.show()
self.range_ui[-1].setGeometry(x1-globalconfig['ocrrangewidth'],y1-globalconfig['ocrrangewidth'],x2-x1+2*globalconfig['ocrrangewidth'],y2-y1+2*globalconfig['ocrrangewidth'])
self.range_ui[-1].show()
def setstyle(self):
self.range_ui.setstyle()
[_.setstyle() for _ in self.range_ui]
def showhiderangeui(self,b):
self.range_ui.setVisible(b)
[_.setVisible(b) for _ in self.range_ui]
def gettextthread(self ):
if all([_.getrect() is None for _ in self.range_ui]):
time.sleep(1)
return None
time.sleep(0.1)
__text=[]
for i,range_ui in enumerate(self.range_ui):
rect=range_ui.getrect()
if self.rect is None:
time.sleep(1)
return None
time.sleep(0.1)
#img=ImageGrab.grab((self.rect[0][0],self.rect[0][1],self.rect[1][0],self.rect[1][1]))
#imgr = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
if self.rect is None:
return None
imgr=imageCut(self.hwnd,self.rect[0][0],self.rect[0][1],self.rect[1][0],self.rect[1][1])
if rect is None:
continue
imgr=imageCut(self.hwnd,rect[0][0],rect[0][1],rect[1][0],rect[1][1],i==0)
ok=True
if globalconfig['ocr_auto_method'] in [0,2]:
imgr1=qimge2np(imgr)
h,w,c=imgr1.shape
if self.savelastimg is not None and (imgr1.shape==self.savelastimg.shape) :
if self.savelastimg[i] is not None and (imgr1.shape==self.savelastimg[i].shape) :
image_score=compareImage(imgr1 ,self.savelastimg )
image_score=compareImage(imgr1 ,self.savelastimg[i] )
else:
image_score=0
gobject.baseobject.settin_ui.threshold1label.setText(str(image_score))
self.savelastimg=imgr1
if i==0:
gobject.baseobject.settin_ui.threshold1label.setText(str(image_score))
self.savelastimg[i]=imgr1
if image_score>globalconfig['ocr_stable_sim'] :
if self.savelastrecimg is not None and (imgr1.shape==self.savelastrecimg.shape ) :
image_score2=compareImage(imgr1 ,self.savelastrecimg )
if self.savelastrecimg[i] is not None and (imgr1.shape==self.savelastrecimg[i].shape ) :
image_score2=compareImage(imgr1 ,self.savelastrecimg[i] )
else:
image_score2=0
gobject.baseobject.settin_ui.threshold2label.setText(str(image_score2))
if i==0:
gobject.baseobject.settin_ui.threshold2label.setText(str(image_score2))
if image_score2>globalconfig['ocr_diff_sim']:
ok=False
else:
self.savelastrecimg=imgr1
self.savelastrecimg[i]=imgr1
else:
ok=False
if globalconfig['ocr_auto_method'] in [1,2]:
if time.time()-self.lastocrtime>globalconfig['ocr_interval']:
if time.time()-self.lastocrtime[i]>globalconfig['ocr_interval']:
ok=True
else:
ok=False
if ok==False:
return None
continue
text=self.ocrtest(imgr)
self.lastocrtime=time.time()
self.lastocrtime[i]=time.time()
if self.savelasttext is not None:
sim=getEqualRate(self.savelasttext,text)
if self.savelasttext[i] is not None:
sim=getEqualRate(self.savelasttext[i],text)
#print('text',sim)
if sim>0.9:
return None
self.savelasttext=text
continue
self.savelasttext[i]=text
return (text)
__text.append(text)
return '\n'.join(__text)
def gettextonce(self):
if self.rect is None:
return
if self.rect[0][0]>self.rect[1][0] or self.rect[0][1]>self.rect[1][1]:
return
img=imageCut(self.hwnd,self.rect[0][0],self.rect[0][1],self.rect[1][0],self.rect[1][1])
__text=[]
for i,range_ui in enumerate(self.range_ui):
rect=range_ui.getrect()
if rect is None:
continue
if rect[0][0]>rect[1][0] or rect[0][1]>rect[1][1]:
return
img=imageCut(self.hwnd,rect[0][0],rect[0][1],rect[1][0],rect[1][1])
text=self.ocrtest(img)
imgr1=qimge2np(img)
self.savelastimg=imgr1
self.savelastrecimg=imgr1
self.lastocrtime=time.time()
self.savelasttext=text
return text
text=self.ocrtest(img)
imgr1=qimge2np(img)
self.savelastimg[i]=imgr1
self.savelastrecimg[i]=imgr1
self.lastocrtime[i]=time.time()
self.savelasttext[i]=text
__text.append(text)
return '\n'.join(__text)
def ocrtest(self,img):
fname='./cache/ocr/{}.png'.format(self.timestamp)
@ -147,5 +157,4 @@ class ocrtext(basetext):
def end(self):
super().end()
ocr_end()
self.range_ui.close()
[_.close() for _ in self.range_ui]

View File

@ -350,6 +350,14 @@ def SetWindowLong(hwnd,nIndex,value):
return _SetWindowLongW(hwnd,nIndex,value)
def GetDC(hwnd):
return _GetDC(hwnd)
def GetDpiForWindow(hwnd):
try:
_GetDpiForWindow=_user32.GetDpiForWindow
_GetDpiForWindow.argtypes=HWND,
_GetDpiForWindow.restype=UINT
return _GetDpiForWindow(hwnd)
except:
return 96
def GetCursorPos():
_p=POINT()
_GetCursorPos(pointer(_p))
@ -733,8 +741,8 @@ _ScreenToClient=_user32.ScreenToClient
_ScreenToClient.argtypes=c_void_p,POINTER(POINT)
def ScreenToClient(hwnd,x,y):
P=POINT()
P.x=x
P.y=y
P.x=int(x)
P.y=int(y)
_ScreenToClient(hwnd,pointer(P))
return (P.x,P.y)

View File

@ -96,6 +96,7 @@
500,
500
],
"multiregion":false,
"textboxreadonly":true,
"ocr_stable_sim": 0,
"ocr_diff_sim": 0.95,

View File

@ -1,5 +1,5 @@
{
"version":"v2.36.5",
"version":"v2.37.0",
"language_list_show":["简体中文","日本語","English","Русский язык","Español","한국어","Français","繁體中文","Tiếng Việt","Türkçe","Polski","Українська Мова","Italiano","اللغة العربية","ภาษาไทย"] ,
"language_list_translator":["简体中文","日文","英文","俄语","西班牙语","韩语","法语","繁体中文","越南语","土耳其语","波兰语","乌克兰语","意大利语","阿拉伯语","泰语"],
"language_list_translator_inner":["zh", "ja", "en","ru","es","ko","fr","cht","vi","tr","pl","uk","it","ar","th"],

View File

@ -559,6 +559,7 @@
"TGW": {
"args": {
"TGW懒人包": "https://www.bilibili.com/video/BV1Te411U7me",
"TGW懒人包1": "https://pan.baidu.com/s/1fe7iiHIAtoXW80Twsrv8Nw?pwd=pato",
"Github仓库": "https://github.com/oobabooga/text-generation-webui",
"API接口地址(默认为http://127.0.0.1:5000/)": "http://127.0.0.1:5000/",
"API超时(秒)": 30,
@ -586,6 +587,10 @@
"type":"label",
"islink": true
},
"TGW懒人包1":{
"type":"label",
"islink": true
},
"Github仓库":{
"type":"label",
"islink": true

View File

@ -707,5 +707,6 @@
"Hook LosslessScaling进程使其不会退出缩放": "هوك looslessscaling العملية بحيث لا تخرج من التكبير",
"正在使用日语模型作为代替,可能效果不佳": "استخدام النموذج الياباني بدلا من ذلك ، قد لا تعمل بشكل جيد",
"保持非重复字符": "الحفاظ على الطابع غير المتكرر",
"选择文本窗口中文本框只读": "حدد مربع النص للقراءة فقط في نافذة النص"
"选择文本窗口中文本框只读": "حدد مربع النص للقراءة فقط في نافذة النص",
"多重区域模式": "وضع منطقة متعددة"
}

View File

@ -707,5 +707,6 @@
"Hook LosslessScaling进程使其不会退出缩放": "Hook LosslessScaling行程使其不會退出縮放",
"正在使用日语模型作为代替,可能效果不佳": "正在使用日語模型作為代替,可能效果不佳",
"保持非重复字符": "保持非重複字元",
"选择文本窗口中文本框只读": "選擇文字視窗中文字方塊只讀"
"选择文本窗口中文本框只读": "選擇文字視窗中文字方塊只讀",
"多重区域模式": "多重區域模式"
}

View File

@ -707,5 +707,6 @@
"Hook LosslessScaling进程使其不会退出缩放": "Hook LosslessScaling process prevents it from exiting scaling",
"正在使用日语模型作为代替,可能效果不佳": "Using Japanese models as a substitute may not be effective",
"保持非重复字符": "Keep non repeating characters",
"选择文本窗口中文本框只读": "Select read-only text boxes in the text window"
"选择文本窗口中文本框只读": "Select read-only text boxes in the text window",
"多重区域模式": "Multi region mode"
}

View File

@ -707,5 +707,6 @@
"Hook LosslessScaling进程使其不会退出缩放": "El proceso Hook losslessscaling hace que no salga del zoom",
"正在使用日语模型作为代替,可能效果不佳": "Se está utilizando el modelo japonés como sustituto y puede que no funcione bien",
"保持非重复字符": "Mantener caracteres no repetitivos",
"选择文本窗口中文本框只读": "Seleccione la ventana de texto en la que el cuadro de texto es de solo lectura"
"选择文本窗口中文本框只读": "Seleccione la ventana de texto en la que el cuadro de texto es de solo lectura",
"多重区域模式": "Modelo multiregional"
}

View File

@ -707,5 +707,6 @@
"Hook LosslessScaling进程使其不会退出缩放": "Hook losslesscaling processus pour ne pas quitter le zoom",
"正在使用日语模型作为代替,可能效果不佳": "Le modèle japonais est utilisé à la place, peut ne pas fonctionner aussi bien",
"保持非重复字符": "Garder les caractères non répétitifs",
"选择文本窗口中文本框只读": "Sélectionner une zone de texte en lecture seule dans la fenêtre de texte"
"选择文本窗口中文本框只读": "Sélectionner une zone de texte en lecture seule dans la fenêtre de texte",
"多重区域模式": "Mode Multi - zones"
}

View File

@ -707,5 +707,6 @@
"Hook LosslessScaling进程使其不会退出缩放": "Hook LosslessScaling processo impedisce di uscire dalla scalabilità",
"正在使用日语模型作为代替,可能效果不佳": "Utilizzare modelli giapponesi come sostituto potrebbe non essere efficace",
"保持非重复字符": "Mantieni caratteri non ripetuti",
"选择文本窗口中文本框只读": "Selezionare le caselle di testo di sola lettura nella finestra di testo"
"选择文本窗口中文本框只读": "Selezionare le caselle di testo di sola lettura nella finestra di testo",
"多重区域模式": "Modalità multi regione"
}

View File

@ -707,5 +707,6 @@
"Hook LosslessScaling进程使其不会退出缩放": "Hook LosslessScalingプロセスはスケーリングを終了しないようにする",
"正在使用日语模型作为代替,可能效果不佳": "代替として日本語モデルを使用していますが、効果が低いかもしれません",
"保持非重复字符": "非反復文字の維持",
"选择文本窗口中文本框只读": "テキストウィンドウのテキストボックス読み取り専用を選択"
"选择文本窗口中文本框只读": "テキストウィンドウのテキストボックス読み取り専用を選択",
"多重区域模式": "複数領域モード"
}

View File

@ -707,5 +707,6 @@
"Hook LosslessScaling进程使其不会退出缩放": "Hook LosslessScaling 프로세스로 인해 줌이 종료되지 않습니다.",
"正在使用日语模型作为代替,可能效果不佳": "일본어 모델을 대체하는 중입니다. 효과가 없을 수 있습니다",
"保持非重复字符": "비반복 문자 유지",
"选择文本窗口中文本框只读": "텍스트 선택 창의 텍스트 상자 읽기 전용"
"选择文本窗口中文本框只读": "텍스트 선택 창의 텍스트 상자 읽기 전용",
"多重区域模式": "다중 영역 모드"
}

View File

@ -707,5 +707,6 @@
"Hook LosslessScaling进程使其不会退出缩放": "Proces Hook Losslessless Scaling zapobiega zakończeniu skalowania",
"正在使用日语模型作为代替,可能效果不佳": "Używanie japońskich modeli jako substytutów może nie być skuteczne",
"保持非重复字符": "Zachowaj niepowtarzające się znaki",
"选择文本窗口中文本框只读": "Zaznaczanie pola tekstowe tylko do odczytu w oknie tekstowym"
"选择文本窗口中文本框只读": "Zaznaczanie pola tekstowe tylko do odczytu w oknie tekstowym",
"多重区域模式": "Tryb wielu regionów"
}

View File

@ -707,5 +707,6 @@
"Hook LosslessScaling进程使其不会退出缩放": "Процесс Hook LosslessScaling не выводит из масштабирования",
"正在使用日语模型作为代替,可能效果不佳": "В качестве замены используется японская модель, которая может быть неэффективной",
"保持非重复字符": "Сохранить символ без повторения",
"选择文本窗口中文本框只读": "Выберите текстовое окно только для чтения"
"选择文本窗口中文本框只读": "Выберите текстовое окно только для чтения",
"多重区域模式": "Многорегиональная модель"
}

View File

@ -707,5 +707,6 @@
"Hook LosslessScaling进程使其不会退出缩放": "Hook LosslessScaling กระบวนการทำให้มันไม่ออกจากซูม",
"正在使用日语模型作为代替,可能效果不佳": "ใช้แบบจำลองภาษาญี่ปุ่นแทนอาจไม่ได้ผล",
"保持非重复字符": "เก็บอักขระที่ไม่ซ้ำกัน",
"选择文本窗口中文本框只读": "เลือกกล่องข้อความในหน้าต่างข้อความ อ่านอย่างเดียว"
"选择文本窗口中文本框只读": "เลือกกล่องข้อความในหน้าต่างข้อความ อ่านอย่างเดียว",
"多重区域模式": "โหมดหลายโซน"
}

View File

@ -707,5 +707,6 @@
"Hook LosslessScaling进程使其不会退出缩放": "Hook LosslessScaling süreç ölçeklenmesini engelledi",
"正在使用日语模型作为代替,可能效果不佳": "Japon modellerini yerine koymak etkili olmayabilir.",
"保持非重复字符": "Tekrarlamayan karakterleri tut",
"选择文本窗口中文本框只读": "Metin penceresindeki sadece okuyulmuş metin kutularını seçin"
"选择文本窗口中文本框只读": "Metin penceresindeki sadece okuyulmuş metin kutularını seçin",
"多重区域模式": "Çok bölge modu"
}

View File

@ -707,5 +707,6 @@
"Hook LosslessScaling进程使其不会退出缩放": "Процес безвтратного розміруScaling забороняє їй вийти з розміру",
"正在使用日语模型作为代替,可能效果不佳": "Використання японських моделей як заміни може бути неефективним",
"保持非重复字符": "Зберегти символи, які не повторюються",
"选择文本窗口中文本框只读": "Виберіть текстові поля для лише читання у текстовому вікні"
"选择文本窗口中文本框只读": "Виберіть текстові поля для лише читання у текстовому вікні",
"多重区域模式": "Режим декількох регіонів"
}

View File

@ -707,5 +707,6 @@
"Hook LosslessScaling进程使其不会退出缩放": "Quá trình Hook LosslessScaling làm cho nó không thoát khỏi zoom",
"正在使用日语模型作为代替,可能效果不佳": "Các mô hình tiếng Nhật đang được sử dụng thay thế và có thể không hiệu quả",
"保持非重复字符": "Giữ các ký tự không trùng lặp",
"选择文本窗口中文本框只读": "Chọn hộp văn bản chỉ đọc trong cửa sổ văn bản"
"选择文本窗口中文本框只读": "Chọn hộp văn bản chỉ đọc trong cửa sổ văn bản",
"多重区域模式": "Chế độ đa vùng"
}

View File

@ -707,5 +707,6 @@
"Hook LosslessScaling进程使其不会退出缩放": "",
"正在使用日语模型作为代替,可能效果不佳": "",
"保持非重复字符": "",
"选择文本窗口中文本框只读": ""
"选择文本窗口中文本框只读": "",
"多重区域模式": ""
}