From 88c08dd822cea67b11bf2c5c26e0567054eaa822 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, 5 May 2024 11:48:28 +0800 Subject: [PATCH] ... --- .../ocrengines/googlecloudvision.py | 47 +++++++++++++++++++ .../files/defaultconfig/config.json | 4 ++ .../files/defaultconfig/ocrsetting.json | 5 ++ 3 files changed, 56 insertions(+) create mode 100644 LunaTranslator/LunaTranslator/ocrengines/googlecloudvision.py diff --git a/LunaTranslator/LunaTranslator/ocrengines/googlecloudvision.py b/LunaTranslator/LunaTranslator/ocrengines/googlecloudvision.py new file mode 100644 index 00000000..e7b4a8dd --- /dev/null +++ b/LunaTranslator/LunaTranslator/ocrengines/googlecloudvision.py @@ -0,0 +1,47 @@ +from ocrengines.baseocrclass import baseocr +import base64 + + +class OCR(baseocr): + + def ocr(self, imgfile): + # https://github.com/dmotz/thing-translator/blob/d1fec3f38d24e973af49766669f9ee00bd9e98a8/src/effects/snap.js + # https://cloud.google.com/vision/docs/ocr?hl=zh-cn + # https://cloud.google.com/vision/docs/reference/rest/v1/AnnotateImageResponse#EntityAnnotation + self.checkempty(["googlecloudvision"]) + ocr_url = ( + "https://vision.googleapis.com/v1/images:annotate?key=" + self.config["key"] + ) + with open(imgfile, "rb") as f: + data = f.read() + encodestr = str(base64.b64encode(data), "utf-8") + data = { + "requests": [ + { + "image": {"content": encodestr}, + "features": [{"type": "TEXT_DETECTION"}], + } + ] + } + response = self.session.post(ocr_url, json=data) + try: + boxs = [] + texts = [] + for anno in response.json()["responses"][0]["textAnnotations"]: + + texts.append(anno["description"]) + boxs.append( + [ + anno["boundingPoly"]["vertices"][0]["x"], + anno["boundingPoly"]["vertices"][0]["y"], + anno["boundingPoly"]["vertices"][1]["x"], + anno["boundingPoly"]["vertices"][1]["y"], + anno["boundingPoly"]["vertices"][2]["x"], + anno["boundingPoly"]["vertices"][2]["y"], + anno["boundingPoly"]["vertices"][3]["x"], + anno["boundingPoly"]["vertices"][3]["y"], + ] + ) + return self.common_solve_text_orientation(boxs, texts) + except: + raise Exception(response.text) diff --git a/LunaTranslator/files/defaultconfig/config.json b/LunaTranslator/files/defaultconfig/config.json index 1561934f..ba086ede 100644 --- a/LunaTranslator/files/defaultconfig/config.json +++ b/LunaTranslator/files/defaultconfig/config.json @@ -787,6 +787,10 @@ "use": false, "name": "docsumo" }, + "googlecloudvision": { + "use": false, + "name": "googlecloudvision" + }, "youdaocr": { "use": false, "name": "有道OCR" diff --git a/LunaTranslator/files/defaultconfig/ocrsetting.json b/LunaTranslator/files/defaultconfig/ocrsetting.json index fefd7376..8ed2c142 100644 --- a/LunaTranslator/files/defaultconfig/ocrsetting.json +++ b/LunaTranslator/files/defaultconfig/ocrsetting.json @@ -212,6 +212,11 @@ } } }, + "googlecloudvision": { + "args": { + "key": "" + } + }, "mangaocr": { "args": { "项目仓库": "https://github.com/kha-white/manga-ocr",