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.showocrimage = None
self.showocrimage_cached = None
self.showocrimage_cached2 = None
self.autoswitchgameuid = True
self.istriggertoupdate = False
self.thishastranslated = True
@ -93,9 +94,19 @@ class MAINUI:
print_exc()
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):
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:
self.showocrimage.show()
except:

View File

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

View File

@ -2151,11 +2151,18 @@ class pixmapviewer(QWidget):
super().__init__(p)
self.pix = None
self._pix = None
self.boxtext = [], []
def showpixmap(self, pix: QPixmap):
pix.setDevicePixelRatio(self.devicePixelRatioF())
self.pix = pix
self._pix = None
self.boxtext = [], []
self.update()
def showboxtext(self, box, text):
self._pix = None
self.boxtext = box, text
self.update()
def paintEvent(self, e):
@ -2164,6 +2171,7 @@ class pixmapviewer(QWidget):
if self._pix.size() != self.size() * self.devicePixelRatioF():
self._pix = None
if not self._pix:
rate = self.devicePixelRatioF()
self._pix = QPixmap(self.size() * rate)
self._pix.setDevicePixelRatio(rate)
@ -2181,6 +2189,36 @@ class pixmapviewer(QWidget):
x = self.width() / 2 - pix.width() / 2 / self.devicePixelRatioF()
y = self.height() / 2 - pix.height() / 2 / self.devicePixelRatioF()
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.drawPixmap(0, 0, self._pix)
return super().paintEvent(e)

View File

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

View File

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

View File

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

View File

@ -20,13 +20,16 @@ class baseocr(commonbase):
if globalconfig["ocrmergelines"] == False:
space = "\n"
else:
space = getlanguagespace(self.srclang_1)
space = self.space_1
return space
@property
def space_1(self):
return getlanguagespace(self.srclang_1)
############################################################
_globalconfig_key = "ocr"
_setting_dict = ocrsetting
isocrtranslate = False
def flatten4point(self, boxs):
return [
@ -53,7 +56,7 @@ class baseocr(commonbase):
whs *= w / h
return whs < 1
def common_solve_text_orientation(self, boxs, texts):
def sort_text_lines(self, boxs, texts):
vertical = int(globalconfig["verticalocr"])
def norm48(box):
@ -101,7 +104,7 @@ class baseocr(commonbase):
lines = []
for _j in juhe:
lines.append(" ".join([texts[_] for _ in _j]))
return self.space.join(lines)
return lines
########################################################
def level2init(self):
@ -117,12 +120,39 @@ class baseocr(commonbase):
self.level2init()
try:
text = self.ocr(imagebinary)
if text is None:
text = ""
except Exception as e:
self.needinit = True
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):
if ocrerrorfix["use"] == False:

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -42,4 +42,4 @@ class OCR(baseocr):
if len(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
)
# print(r.text)
if r.status_code == 200:
try:
boxs = [
[
_["Polygon"][0]["X"],
_["Polygon"][0]["Y"],
_["Polygon"][1]["X"],
_["Polygon"][1]["Y"],
_["Polygon"][2]["X"],
_["Polygon"][2]["Y"],
_["Polygon"][3]["X"],
_["Polygon"][3]["Y"],
]
for _ in r.json()["Response"]["TextDetections"]
try:
boxs = [
[
_["Polygon"][0]["X"],
_["Polygon"][0]["Y"],
_["Polygon"][1]["X"],
_["Polygon"][1]["Y"],
_["Polygon"][2]["X"],
_["Polygon"][2]["Y"],
_["Polygon"][3]["X"],
_["Polygon"][3]["Y"],
]
texts = [
_["DetectedText"] for _ in r.json()["Response"]["TextDetections"]
]
return self.common_solve_text_orientation(boxs, texts)
except:
raise Exception(r.json())
return r.text
for _ in r.json()["Response"]["TextDetections"]
]
texts = [_["DetectedText"] for _ in r.json()["Response"]["TextDetections"]]
return {"box": boxs, "text": texts}
except:
raise Exception(r.text)

View File

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

View File

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

View File

@ -116,5 +116,5 @@ class OCR(baseocr):
global globalonce
if not globalonce:
raise Exception
return self.common_solve_text_orientation(*globalonce.ocr(imagebinary))
boxs, texts = 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])
)
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]
texts = [_[0] for _ in ret]
return self.common_solve_text_orientation(boxs, texts)
return {"box": boxs, "text": texts}

View File

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

View File

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

View File

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

View File

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