From b16e6d758b5fd571955fe11b30a2246b07377722 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, 14 Jul 2024 01:27:50 +0800 Subject: [PATCH] glens --- .../LunaTranslator/ocrengines/googlelens.py | 52 +++++++++++++++++++ .../files/defaultconfig/config.json | 4 ++ 2 files changed, 56 insertions(+) create mode 100644 LunaTranslator/LunaTranslator/ocrengines/googlelens.py diff --git a/LunaTranslator/LunaTranslator/ocrengines/googlelens.py b/LunaTranslator/LunaTranslator/ocrengines/googlelens.py new file mode 100644 index 00000000..deed9675 --- /dev/null +++ b/LunaTranslator/LunaTranslator/ocrengines/googlelens.py @@ -0,0 +1,52 @@ +from ocrengines.baseocrclass import baseocr +import re, time + + +class OCR(baseocr): + + def ocr(self, imagebinary): + # https://github.com/AuroraWright/owocr/blob/master/owocr/ocr.py + + regex = re.compile(r">AF_initDataCallback\(({key: 'ds:1'.*?)\);") + + timestamp = int(time.time() * 1000) + url = f"https://lens.google.com/v3/upload?stcs={timestamp}" + headers = { + "content-type": "multipart/form-data; boundary=----WebKitFormBoundaryUjYOv45hug6CFh3t", + "User-Agent": "Mozilla/5.0 (Linux; Android 13; RMX3771) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.6167.144 Mobile Safari/537.36", + } + cookies = {"SOCS": "CAESEwgDEgk0ODE3Nzk3MjQaAmVuIAEaBgiA_LyaBg"} + + data = ( + '------WebKitFormBoundaryUjYOv45hug6CFh3t\r\nContent-Disposition: form-data; name="encoded_image"; filename="screenshot.png"\r\nContent-Type: image/png\r\n\r\n'.encode( + "latin-1" + ) + + imagebinary + + "\r\n------WebKitFormBoundaryUjYOv45hug6CFh3t--\r\n".encode("latin-1") + ) + res = self.proxysession.post( + url, data=data, headers=headers, cookies=cookies, timeout=20 + ) + match = regex.search(res.text) + if match == None: + raise Exception(False, "Regex error!") + sideChannel = "sideChannel" + null = None + key = "key" + # hash="hash" + data = "data" + true = True + false = False + lens_object = eval(match.group(1)) + if "errorHasStatus" in lens_object: + raise Exception(False, "Unknown Lens error!") + + res = "" + text = lens_object["data"][3][4][0] + print(text) + if len(text) > 0: + lines = text[0] + for line in lines: + res += line + "\n" + + return "\n".join(["\n".join(_) for _ in text]) diff --git a/LunaTranslator/files/defaultconfig/config.json b/LunaTranslator/files/defaultconfig/config.json index 877306b9..90344cec 100644 --- a/LunaTranslator/files/defaultconfig/config.json +++ b/LunaTranslator/files/defaultconfig/config.json @@ -1434,6 +1434,10 @@ "use": false, "name": "Google Cloud Vision" }, + "googlelens": { + "use": false, + "name": "Google Lens" + }, "xunfei": { "use": false, "name": "讯飞OCR"