From 6f9f3849433b8b81603eaa36bbfc9c46544b4002 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <101191390+HIllya51@users.noreply.github.com> Date: Sun, 29 Sep 2024 19:22:03 +0800 Subject: [PATCH] vis --- .../LunaTranslator/LunaTranslator.py | 13 +++++- .../gui/setting_textinput_ocr.py | 9 +++- .../LunaTranslator/gui/usefulwidget.py | 38 ++++++++++++++++ .../LunaTranslator/myutils/ocrutil.py | 7 +-- .../LunaTranslator/ocrengines/baiduocr_X.py | 6 +-- .../LunaTranslator/ocrengines/baiduocr_ts.py | 3 +- .../LunaTranslator/ocrengines/baseocrclass.py | 44 ++++++++++++++++--- .../LunaTranslator/ocrengines/feishu.py | 2 +- .../ocrengines/googlecloudvision.py | 2 +- .../LunaTranslator/ocrengines/googlelens.py | 3 +- .../LunaTranslator/ocrengines/local.py | 3 +- .../LunaTranslator/ocrengines/ocrspace.py | 2 +- .../LunaTranslator/ocrengines/tesseract5.py | 2 +- .../LunaTranslator/ocrengines/txocr.py | 39 ++++++++-------- .../LunaTranslator/ocrengines/txocrtrans.py | 3 +- .../LunaTranslator/ocrengines/volcengine.py | 2 +- .../LunaTranslator/ocrengines/weixinocr.py | 4 +- .../LunaTranslator/ocrengines/windowsocr.py | 5 +-- .../LunaTranslator/ocrengines/xunfei.py | 2 +- .../LunaTranslator/ocrengines/youdaocr.py | 17 +++---- .../ocrengines/youdaocrtrans.py | 9 ++-- .../ocrengines/youdaodictocr.py | 6 +-- 22 files changed, 149 insertions(+), 72 deletions(-) diff --git a/LunaTranslator/LunaTranslator/LunaTranslator.py b/LunaTranslator/LunaTranslator/LunaTranslator.py index cc872d42..99324833 100644 --- a/LunaTranslator/LunaTranslator/LunaTranslator.py +++ b/LunaTranslator/LunaTranslator/LunaTranslator.py @@ -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: diff --git a/LunaTranslator/LunaTranslator/gui/setting_textinput_ocr.py b/LunaTranslator/LunaTranslator/gui/setting_textinput_ocr.py index 9b3efc30..ef588982 100644 --- a/LunaTranslator/LunaTranslator/gui/setting_textinput_ocr.py +++ b/LunaTranslator/LunaTranslator/gui/setting_textinput_ocr.py @@ -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): diff --git a/LunaTranslator/LunaTranslator/gui/usefulwidget.py b/LunaTranslator/LunaTranslator/gui/usefulwidget.py index e2b54019..7e84ba0c 100644 --- a/LunaTranslator/LunaTranslator/gui/usefulwidget.py +++ b/LunaTranslator/LunaTranslator/gui/usefulwidget.py @@ -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) diff --git a/LunaTranslator/LunaTranslator/myutils/ocrutil.py b/LunaTranslator/LunaTranslator/myutils/ocrutil.py index df68c879..5ff8c7ac 100644 --- a/LunaTranslator/LunaTranslator/myutils/ocrutil.py +++ b/LunaTranslator/LunaTranslator/myutils/ocrutil.py @@ -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, "" else: return text, None diff --git a/LunaTranslator/LunaTranslator/ocrengines/baiduocr_X.py b/LunaTranslator/LunaTranslator/ocrengines/baiduocr_X.py index 703f1c63..104c2315 100644 --- a/LunaTranslator/LunaTranslator/ocrengines/baiduocr_X.py +++ b/LunaTranslator/LunaTranslator/ocrengines/baiduocr_X.py @@ -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) diff --git a/LunaTranslator/LunaTranslator/ocrengines/baiduocr_ts.py b/LunaTranslator/LunaTranslator/ocrengines/baiduocr_ts.py index b66a6294..6b3a6259 100644 --- a/LunaTranslator/LunaTranslator/ocrengines/baiduocr_ts.py +++ b/LunaTranslator/LunaTranslator/ocrengines/baiduocr_ts.py @@ -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) diff --git a/LunaTranslator/LunaTranslator/ocrengines/baseocrclass.py b/LunaTranslator/LunaTranslator/ocrengines/baseocrclass.py index aeebf93b..ffe4e15a 100644 --- a/LunaTranslator/LunaTranslator/ocrengines/baseocrclass.py +++ b/LunaTranslator/LunaTranslator/ocrengines/baseocrclass.py @@ -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: diff --git a/LunaTranslator/LunaTranslator/ocrengines/feishu.py b/LunaTranslator/LunaTranslator/ocrengines/feishu.py index 2cf3bceb..907845b2 100644 --- a/LunaTranslator/LunaTranslator/ocrengines/feishu.py +++ b/LunaTranslator/LunaTranslator/ocrengines/feishu.py @@ -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) diff --git a/LunaTranslator/LunaTranslator/ocrengines/googlecloudvision.py b/LunaTranslator/LunaTranslator/ocrengines/googlecloudvision.py index 72581a96..7f7b33d6 100644 --- a/LunaTranslator/LunaTranslator/ocrengines/googlecloudvision.py +++ b/LunaTranslator/LunaTranslator/ocrengines/googlecloudvision.py @@ -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) diff --git a/LunaTranslator/LunaTranslator/ocrengines/googlelens.py b/LunaTranslator/LunaTranslator/ocrengines/googlelens.py index 1c05ee37..91d8327d 100644 --- a/LunaTranslator/LunaTranslator/ocrengines/googlelens.py +++ b/LunaTranslator/LunaTranslator/ocrengines/googlelens.py @@ -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] diff --git a/LunaTranslator/LunaTranslator/ocrengines/local.py b/LunaTranslator/LunaTranslator/ocrengines/local.py index 1e6357da..e63d587b 100644 --- a/LunaTranslator/LunaTranslator/ocrengines/local.py +++ b/LunaTranslator/LunaTranslator/ocrengines/local.py @@ -223,5 +223,4 @@ class OCR(baseocr): imagebinary, 0, ) - - return self.common_solve_text_orientation(pss, texts) + return {"box": pss, "text": texts} diff --git a/LunaTranslator/LunaTranslator/ocrengines/ocrspace.py b/LunaTranslator/LunaTranslator/ocrengines/ocrspace.py index 275fe315..392ce49c 100644 --- a/LunaTranslator/LunaTranslator/ocrengines/ocrspace.py +++ b/LunaTranslator/LunaTranslator/ocrengines/ocrspace.py @@ -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) diff --git a/LunaTranslator/LunaTranslator/ocrengines/tesseract5.py b/LunaTranslator/LunaTranslator/ocrengines/tesseract5.py index a44bd4b0..77efbcf9 100644 --- a/LunaTranslator/LunaTranslator/ocrengines/tesseract5.py +++ b/LunaTranslator/LunaTranslator/ocrengines/tesseract5.py @@ -42,4 +42,4 @@ class OCR(baseocr): if len(err): raise Exception(err) - return self.space.join(res.split("\n")) + return res.split("\n") diff --git a/LunaTranslator/LunaTranslator/ocrengines/txocr.py b/LunaTranslator/LunaTranslator/ocrengines/txocr.py index b8c1dfcf..b50cb895 100644 --- a/LunaTranslator/LunaTranslator/ocrengines/txocr.py +++ b/LunaTranslator/LunaTranslator/ocrengines/txocr.py @@ -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) diff --git a/LunaTranslator/LunaTranslator/ocrengines/txocrtrans.py b/LunaTranslator/LunaTranslator/ocrengines/txocrtrans.py index 9861f7e5..e6516422 100644 --- a/LunaTranslator/LunaTranslator/ocrengines/txocrtrans.py +++ b/LunaTranslator/LunaTranslator/ocrengines/txocrtrans.py @@ -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()) diff --git a/LunaTranslator/LunaTranslator/ocrengines/volcengine.py b/LunaTranslator/LunaTranslator/ocrengines/volcengine.py index dbdde4fc..d1f92a1e 100644 --- a/LunaTranslator/LunaTranslator/ocrengines/volcengine.py +++ b/LunaTranslator/LunaTranslator/ocrengines/volcengine.py @@ -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) diff --git a/LunaTranslator/LunaTranslator/ocrengines/weixinocr.py b/LunaTranslator/LunaTranslator/ocrengines/weixinocr.py index aa463f01..dafcc80c 100644 --- a/LunaTranslator/LunaTranslator/ocrengines/weixinocr.py +++ b/LunaTranslator/LunaTranslator/ocrengines/weixinocr.py @@ -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} diff --git a/LunaTranslator/LunaTranslator/ocrengines/windowsocr.py b/LunaTranslator/LunaTranslator/ocrengines/windowsocr.py index abc64ee9..278820d2 100644 --- a/LunaTranslator/LunaTranslator/ocrengines/windowsocr.py +++ b/LunaTranslator/LunaTranslator/ocrengines/windowsocr.py @@ -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} diff --git a/LunaTranslator/LunaTranslator/ocrengines/xunfei.py b/LunaTranslator/LunaTranslator/ocrengines/xunfei.py index 00ac4690..4aa2a9b9 100644 --- a/LunaTranslator/LunaTranslator/ocrengines/xunfei.py +++ b/LunaTranslator/LunaTranslator/ocrengines/xunfei.py @@ -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) diff --git a/LunaTranslator/LunaTranslator/ocrengines/youdaocr.py b/LunaTranslator/LunaTranslator/ocrengines/youdaocr.py index 7665fa0d..5220b901 100644 --- a/LunaTranslator/LunaTranslator/ocrengines/youdaocr.py +++ b/LunaTranslator/LunaTranslator/ocrengines/youdaocr.py @@ -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) diff --git a/LunaTranslator/LunaTranslator/ocrengines/youdaocrtrans.py b/LunaTranslator/LunaTranslator/ocrengines/youdaocrtrans.py index 0bf551f0..b56774b4 100644 --- a/LunaTranslator/LunaTranslator/ocrengines/youdaocrtrans.py +++ b/LunaTranslator/LunaTranslator/ocrengines/youdaocrtrans.py @@ -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) diff --git a/LunaTranslator/LunaTranslator/ocrengines/youdaodictocr.py b/LunaTranslator/LunaTranslator/ocrengines/youdaodictocr.py index 464d6334..1715b1cb 100644 --- a/LunaTranslator/LunaTranslator/ocrengines/youdaodictocr.py +++ b/LunaTranslator/LunaTranslator/ocrengines/youdaodictocr.py @@ -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}