This commit is contained in:
恍兮惚兮 2024-09-29 19:22:03 +08:00
parent 6e68471958
commit 6f9f384943
22 changed files with 149 additions and 72 deletions

View File

@ -81,6 +81,7 @@ class MAINUI:
self.gameuid = 0 self.gameuid = 0
self.showocrimage = None self.showocrimage = None
self.showocrimage_cached = None self.showocrimage_cached = None
self.showocrimage_cached2 = None
self.autoswitchgameuid = True self.autoswitchgameuid = True
self.istriggertoupdate = False self.istriggertoupdate = False
self.thishastranslated = True self.thishastranslated = True
@ -93,9 +94,19 @@ class MAINUI:
print_exc() print_exc()
self.showocrimage_cached = pair self.showocrimage_cached = pair
def maybesetocrresult(self, pair):
if self.showocrimage:
try:
self.showocrimage.setresult.emit(pair)
except:
print_exc()
self.showocrimage_cached2 = pair
def createshowocrimage(self): def createshowocrimage(self):
try: try:
self.showocrimage = showocrimage(self.settin_ui, self.showocrimage_cached) self.showocrimage = showocrimage(
self.settin_ui, self.showocrimage_cached, self.showocrimage_cached2
)
if self.showocrimage: if self.showocrimage:
self.showocrimage.show() self.showocrimage.show()
except: except:

View File

@ -287,6 +287,7 @@ def _ocrparam(self):
@Singleton_close @Singleton_close
class showocrimage(saveposwindow): class showocrimage(saveposwindow):
setimage = pyqtSignal(QImage) setimage = pyqtSignal(QImage)
setresult = pyqtSignal(dict)
def closeEvent(self, e): def closeEvent(self, e):
gobject.baseobject.showocrimage = None gobject.baseobject.showocrimage = None
@ -328,7 +329,7 @@ class showocrimage(saveposwindow):
if len(files): if len(files):
self.ocrfile(files[0]) self.ocrfile(files[0])
def __init__(self, parent, cached): def __init__(self, parent, cached, cached2):
self.originimage = None self.originimage = None
super().__init__(parent, poslist=globalconfig["showocrgeo"]) super().__init__(parent, poslist=globalconfig["showocrgeo"])
self.setWindowIcon(qtawesome.icon("fa.picture-o")) self.setWindowIcon(qtawesome.icon("fa.picture-o"))
@ -353,8 +354,11 @@ class showocrimage(saveposwindow):
self.layout1.addLayout(hb) self.layout1.addLayout(hb)
self.layout1.addWidget(self.originlabel) self.layout1.addWidget(self.originlabel)
self.setimage.connect(self.setimagefunction) self.setimage.connect(self.setimagefunction)
self.setresult.connect(self.setocr)
if cached: if cached:
self.setimagefunction(cached) self.setimagefunction(cached)
if cached2:
self.setocr(cached2)
def onValueChanged(self, value): def onValueChanged(self, value):
if not self.originimage: if not self.originimage:
@ -379,6 +383,9 @@ class showocrimage(saveposwindow):
self.originimage = originimage self.originimage = originimage
self.originlabel.showpixmap(QPixmap.fromImage(originimage)) self.originlabel.showpixmap(QPixmap.fromImage(originimage))
def setocr(self, result):
self.originlabel.showboxtext(result.get("box"), result.get("text"))
def getocrgrid(self): def getocrgrid(self):

View File

@ -2151,11 +2151,18 @@ class pixmapviewer(QWidget):
super().__init__(p) super().__init__(p)
self.pix = None self.pix = None
self._pix = None self._pix = None
self.boxtext = [], []
def showpixmap(self, pix: QPixmap): def showpixmap(self, pix: QPixmap):
pix.setDevicePixelRatio(self.devicePixelRatioF()) pix.setDevicePixelRatio(self.devicePixelRatioF())
self.pix = pix self.pix = pix
self._pix = None self._pix = None
self.boxtext = [], []
self.update()
def showboxtext(self, box, text):
self._pix = None
self.boxtext = box, text
self.update() self.update()
def paintEvent(self, e): def paintEvent(self, e):
@ -2164,6 +2171,7 @@ class pixmapviewer(QWidget):
if self._pix.size() != self.size() * self.devicePixelRatioF(): if self._pix.size() != self.size() * self.devicePixelRatioF():
self._pix = None self._pix = None
if not self._pix: if not self._pix:
rate = self.devicePixelRatioF() rate = self.devicePixelRatioF()
self._pix = QPixmap(self.size() * rate) self._pix = QPixmap(self.size() * rate)
self._pix.setDevicePixelRatio(rate) self._pix.setDevicePixelRatio(rate)
@ -2181,6 +2189,36 @@ class pixmapviewer(QWidget):
x = self.width() / 2 - pix.width() / 2 / self.devicePixelRatioF() x = self.width() / 2 - pix.width() / 2 / self.devicePixelRatioF()
y = self.height() / 2 - pix.height() / 2 / self.devicePixelRatioF() y = self.height() / 2 - pix.height() / 2 / self.devicePixelRatioF()
painter.drawPixmap(int(x), int(y), pix) painter.drawPixmap(int(x), int(y), pix)
boxs, texts = self.boxtext
try:
scale = pix.height() / self.pix.height() / rate
parsex = lambda xx: (xx) * scale + x
parsey = lambda yy: (yy) * scale + y
font = QFont()
font.setFamily(globalconfig["fonttype"])
font.setPointSizeF(globalconfig["fontsizeori"])
pen = QPen()
pen.setColor(QColor(globalconfig["rawtextcolor"]))
painter.setFont(font)
painter.setPen(pen)
for i in range(len(boxs)):
painter.drawText(
QPointF(parsex(boxs[i][0]), parsey(boxs[i][1])),
texts[i],
)
for j in range(len(boxs[i]) // 2):
painter.drawLine(
QPointF(
parsex(boxs[i][j * 2]),
parsey(boxs[i][j * 2 + 1]),
),
QPointF(
parsex(boxs[i][(j * 2 + 2) % len(boxs[i])]),
parsey(boxs[i][(j * 2 + 3) % len(boxs[i])]),
),
)
except:
print_exc()
painter = QPainter(self) painter = QPainter(self)
painter.drawPixmap(0, 0, self._pix) painter.drawPixmap(0, 0, self._pix)
return super().paintEvent(e) return super().paintEvent(e)

View File

@ -105,9 +105,10 @@ def ocr_run(qimage: QImage):
global _nowuseocrx, _ocrengine global _nowuseocrx, _ocrengine
try: try:
ocr_init() ocr_init()
text = _ocrengine._private_ocr(image) res = _ocrengine._private_ocr(image)
isocrtranslate = _ocrengine.isocrtranslate gobject.baseobject.maybesetocrresult(res)
if isocrtranslate: text = res["textonly"]
if res["isocrtranslate"]:
return text, "<notrans>" return text, "<notrans>"
else: else:
return text, None return text, None

View File

@ -80,9 +80,7 @@ class OCR(baseocr):
self.countnum() self.countnum()
if interfacetype in [0, 2]: if interfacetype in [0, 2]:
return self.space.join( return {"text": [x["words"] for x in response.json()["words_result"]]}
[x["words"] for x in response.json()["words_result"]]
)
else: else:
texts = [x["words"] for x in response.json()["words_result"]] texts = [x["words"] for x in response.json()["words_result"]]
boxs = [ boxs = [
@ -94,6 +92,6 @@ class OCR(baseocr):
) )
for x in response.json()["words_result"] for x in response.json()["words_result"]
] ]
return self.common_solve_text_orientation(boxs, texts) return {"box": boxs, "text": texts}
except: except:
raise Exception(response.text) raise Exception(response.text)

View File

@ -4,7 +4,6 @@ from ocrengines.baseocrclass import baseocr
class OCR(baseocr): class OCR(baseocr):
isocrtranslate = True
def langmap(self): def langmap(self):
return { return {
@ -74,6 +73,6 @@ class OCR(baseocr):
) )
for l in js["data"]["content"] for l in js["data"]["content"]
] ]
return self.common_solve_text_orientation(box, text) return {"box": box, "text": text, "isocrtranslate": True}
except: except:
raise Exception(response.text) raise Exception(response.text)

View File

@ -20,13 +20,16 @@ class baseocr(commonbase):
if globalconfig["ocrmergelines"] == False: if globalconfig["ocrmergelines"] == False:
space = "\n" space = "\n"
else: else:
space = getlanguagespace(self.srclang_1) space = self.space_1
return space return space
@property
def space_1(self):
return getlanguagespace(self.srclang_1)
############################################################ ############################################################
_globalconfig_key = "ocr" _globalconfig_key = "ocr"
_setting_dict = ocrsetting _setting_dict = ocrsetting
isocrtranslate = False
def flatten4point(self, boxs): def flatten4point(self, boxs):
return [ return [
@ -53,7 +56,7 @@ class baseocr(commonbase):
whs *= w / h whs *= w / h
return whs < 1 return whs < 1
def common_solve_text_orientation(self, boxs, texts): def sort_text_lines(self, boxs, texts):
vertical = int(globalconfig["verticalocr"]) vertical = int(globalconfig["verticalocr"])
def norm48(box): def norm48(box):
@ -101,7 +104,7 @@ class baseocr(commonbase):
lines = [] lines = []
for _j in juhe: for _j in juhe:
lines.append(" ".join([texts[_] for _ in _j])) lines.append(" ".join([texts[_] for _ in _j]))
return self.space.join(lines) return lines
######################################################## ########################################################
def level2init(self): def level2init(self):
@ -117,12 +120,39 @@ class baseocr(commonbase):
self.level2init() self.level2init()
try: try:
text = self.ocr(imagebinary) text = self.ocr(imagebinary)
if text is None:
text = ""
except Exception as e: except Exception as e:
self.needinit = True self.needinit = True
raise e raise e
return self._100_f(text)
if isinstance(text, str):
text = {"text": [text]}
elif isinstance(text, (tuple, list)):
text = {"text": text}
elif not text:
text = {}
boxs = text.get("box")
texts = text.get("text")
if not boxs:
# 若无标注,则合并显示
boxs = [[0, 0, 0, 0, 0, 0, 0, 0]]
textonly = self.space.join(texts)
texts = [textonly]
else:
textonly = self.space.join(self.sort_text_lines(boxs, texts))
# 对齐box成4点格式
for i, box in enumerate(boxs):
if len(box) == 8:
continue
x1, y1, x2, y2 = box
boxs[i] = (x1, y1, x2, y1, x2, y2, x1, y2)
textonly = self._100_f(textonly)
text = {
"box": boxs,
"text": texts,
"textonly": textonly,
"isocrtranslate": text.get("isocrtranslate", False),
}
return text
def _100_f(self, line): def _100_f(self, line):
if ocrerrorfix["use"] == False: if ocrerrorfix["use"] == False:

View File

@ -38,6 +38,6 @@ class OCR(baseocr):
}, },
) )
try: try:
return self.space.join(res.json()["data"]["text_list"]) return res.json()["data"]["text_list"]
except: except:
raise Exception(res.text) raise Exception(res.text)

View File

@ -40,6 +40,6 @@ class OCR(baseocr):
anno["boundingPoly"]["vertices"][3]["y"], anno["boundingPoly"]["vertices"][3]["y"],
] ]
) )
return self.common_solve_text_orientation(boxs, texts) return {"box": boxs, "text": texts}
except: except:
raise Exception(response.text) raise Exception(response.text)

View File

@ -36,5 +36,4 @@ class OCR(baseocr):
res = "" res = ""
text = lens_object["data"][3][4][0] text = lens_object["data"][3][4][0]
return text[0]
return self.space.join([self.space.join(_) for _ in text])

View File

@ -223,5 +223,4 @@ class OCR(baseocr):
imagebinary, imagebinary,
0, 0,
) )
return {"box": pss, "text": texts}
return self.common_solve_text_orientation(pss, texts)

View File

@ -70,6 +70,6 @@ class OCR(baseocr):
for _ in response.json()["ParsedResults"][0]["TextOverlay"]["Lines"] for _ in response.json()["ParsedResults"][0]["TextOverlay"]["Lines"]
] ]
self.countnum() self.countnum()
return self.common_solve_text_orientation(boxs, texts) return {"box": boxs, "text": texts}
except: except:
raise Exception(response.text) raise Exception(response.text)

View File

@ -42,4 +42,4 @@ class OCR(baseocr):
if len(err): if len(err):
raise Exception(err) raise Exception(err)
return self.space.join(res.split("\n")) return res.split("\n")

View File

@ -59,25 +59,22 @@ class OCR(baseocr):
url="https://ocr.tencentcloudapi.com/", params=req_para, timeout=10 url="https://ocr.tencentcloudapi.com/", params=req_para, timeout=10
) )
# print(r.text) # print(r.text)
if r.status_code == 200:
try: try:
boxs = [ boxs = [
[ [
_["Polygon"][0]["X"], _["Polygon"][0]["X"],
_["Polygon"][0]["Y"], _["Polygon"][0]["Y"],
_["Polygon"][1]["X"], _["Polygon"][1]["X"],
_["Polygon"][1]["Y"], _["Polygon"][1]["Y"],
_["Polygon"][2]["X"], _["Polygon"][2]["X"],
_["Polygon"][2]["Y"], _["Polygon"][2]["Y"],
_["Polygon"][3]["X"], _["Polygon"][3]["X"],
_["Polygon"][3]["Y"], _["Polygon"][3]["Y"],
]
for _ in r.json()["Response"]["TextDetections"]
] ]
texts = [ for _ in r.json()["Response"]["TextDetections"]
_["DetectedText"] for _ in r.json()["Response"]["TextDetections"] ]
] texts = [_["DetectedText"] for _ in r.json()["Response"]["TextDetections"]]
return self.common_solve_text_orientation(boxs, texts) return {"box": boxs, "text": texts}
except: except:
raise Exception(r.json()) raise Exception(r.text)
return r.text

View File

@ -4,7 +4,6 @@ from ocrengines.baseocrclass import baseocr
class OCR(baseocr): class OCR(baseocr):
isocrtranslate = True
def langmap(self): def langmap(self):
# https://cloud.tencent.com/document/product/551/17232 # https://cloud.tencent.com/document/product/551/17232
@ -127,6 +126,6 @@ class OCR(baseocr):
texts = [ texts = [
_["TargetText"] for _ in r.json()["Response"]["ImageRecord"]["Value"] _["TargetText"] for _ in r.json()["Response"]["ImageRecord"]["Value"]
] ]
return self.common_solve_text_orientation(boxs, texts) return {"box": boxs, "text": texts, "isocrtranslate": True}
except: except:
raise Exception(r.json()) raise Exception(r.json())

View File

@ -574,6 +574,6 @@ class OCR(baseocr):
boxs = self.flatten4point( boxs = self.flatten4point(
[box["rect"] for box in resp["data"]["ocr_infos"]] [box["rect"] for box in resp["data"]["ocr_infos"]]
) )
return self.common_solve_text_orientation(boxs, texts) return {"box": boxs, "text": texts}
except: except:
raise Exception(resp) raise Exception(resp)

View File

@ -116,5 +116,5 @@ class OCR(baseocr):
global globalonce global globalonce
if not globalonce: if not globalonce:
raise Exception raise Exception
boxs, texts = globalonce.ocr(imagebinary)
return self.common_solve_text_orientation(*globalonce.ocr(imagebinary)) return {"box": boxs, "text": texts}

View File

@ -68,8 +68,7 @@ class OCR(baseocr):
+ ", ".join([_TR(getlang_inner2show(f)) for f in _allsupport]) + ", ".join([_TR(getlang_inner2show(f)) for f in _allsupport])
) )
ret = winrtutils.OCR_f(imagebinary, self.supportmap[self.srclang], self.space) ret = winrtutils.OCR_f(imagebinary, self.supportmap[self.srclang], self.space_1)
boxs = [_[1:] for _ in ret] boxs = [_[1:] for _ in ret]
texts = [_[0] for _ in ret] texts = [_[0] for _ in ret]
return {"box": boxs, "text": texts}
return self.common_solve_text_orientation(boxs, texts)

View File

@ -153,6 +153,6 @@ class OCR(baseocr):
] ]
) )
texts.append(line["content"]) texts.append(line["content"])
return self.common_solve_text_orientation(boxs, texts) return {"box": boxs, "text": texts}
except: except:
raise Exception(finalResult) raise Exception(finalResult)

View File

@ -38,13 +38,14 @@ class OCR(baseocr):
) )
try: try:
return self.common_solve_text_orientation(
[ return {
"box": [
[int(_) for _ in l["boundingBox"].split(",")] [int(_) for _ in l["boundingBox"].split(",")]
for l in response.json()["lines"] for l in response.json()["lines"]
], ],
[l["words"] for l in response.json()["lines"]], "text": [l["words"] for l in response.json()["lines"]],
) }
except: except:
raise Exception(response.text) raise Exception(response.text)
@ -88,10 +89,10 @@ class OCR(baseocr):
_ = [] _ = []
for l in response.json()["Result"]["regions"]: for l in response.json()["Result"]["regions"]:
_ += l["lines"] _ += l["lines"]
return self.common_solve_text_orientation( return {
[[int(_) for _ in l["boundingBox"].split(",")] for l in _], "box": [[int(_) for _ in l["boundingBox"].split(",")] for l in _],
[l["text"] for l in _], "text": [l["text"] for l in _],
) }
except: except:
raise Exception(response.text) raise Exception(response.text)

View File

@ -6,8 +6,6 @@ from ocrengines.baseocrclass import baseocr
class OCR(baseocr): class OCR(baseocr):
isocrtranslate = True
def langmap(self): def langmap(self):
return {"zh": "zh-CHS", "cht": "zh-CHT"} return {"zh": "zh-CHS", "cht": "zh-CHT"}
@ -40,7 +38,10 @@ class OCR(baseocr):
) )
try: try:
return self.space.join([l["tranContent"] for l in response.json()["lines"]]) return {
"text": [l["tranContent"] for l in response.json()["lines"]],
"isocrtranslate": True,
}
except: except:
raise Exception(response.text) raise Exception(response.text)
@ -150,7 +151,7 @@ class OCR(baseocr):
[int(_) for _ in l["boundingBox"].split(",")] [int(_) for _ in l["boundingBox"].split(",")]
for l in response.json()["resRegions"] for l in response.json()["resRegions"]
] ]
return self.common_solve_text_orientation(box, text) return {"box": box, "text": text, "isocrtranslate": True}
except: except:
raise Exception(response.text) raise Exception(response.text)

View File

@ -34,8 +34,6 @@ class OCR(baseocr):
boxs, transs, texts = doocr(imagebinary, self.srclang, self.tgtlang) boxs, transs, texts = doocr(imagebinary, self.srclang, self.tgtlang)
if self.config["Translate"]: if self.config["Translate"]:
self.isocrtranslate = True return {"box": boxs, "text": transs, "isocrtranslate": True}
return self.common_solve_text_orientation(boxs, transs)
else: else:
self.isocrtranslate = False return {"box": boxs, "text": texts}
return self.common_solve_text_orientation(boxs, texts)