From 70687e6b0194de7d9dde523d0c36f3e363db77bf Mon Sep 17 00:00:00 2001 From: AuroraWright Date: Sun, 21 Jan 2024 18:30:54 +0100 Subject: [PATCH] Switch to pyjson5 as chompjs needs manual building (especially annoyin on Windows), detect Lens errors --- README.md | 2 +- owocr/ocr.py | 22 ++++++++++++---------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index a0c7059..fa75162 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ This has been tested with Python 3.11. Newer/older versions might work. For now - WinRT OCR: this will work on Windows 10 or later if winocr (`pip install winocr`) is installed. It can also be used by installing winocr on a Windows virtual machine and running the server (`winocr_serve`), installing requests (`pip install requests`) and specifying the IP address of the Windows VM/machine in the config file (see below) ("w" key) ## Cloud providers -- Google Lens: Google Vision in disguise (no need for API keys!), however it needs to download a couple megabytes of data for each request. You need to install chompjs and requests (`pip install chompjs requests`) ("l" key) +- Google Lens: Google Vision in disguise (no need for API keys!), however it needs to download a couple megabytes of data for each request. You need to install pyjson5 and requests (`pip install pyjson5 requests`) ("l" key) - Google Vision: you need a service account .json file named google_vision.json in `user directory/.config/` and installing google-cloud-vision (`pip install google-cloud-vision`) ("g" key) - Azure Computer Vision: you need to specify an api key and an endpoint in the config file (see below) and to install azure-cognitiveservices-vision-computervision (`pip install azure-cognitiveservices-vision-computervision`) ("v" key) diff --git a/owocr/ocr.py b/owocr/ocr.py index 1652aaf..2ec384e 100644 --- a/owocr/ocr.py +++ b/owocr/ocr.py @@ -57,7 +57,7 @@ except ImportError: pass try: - import chompjs + import pyjson5 except ImportError: pass @@ -150,8 +150,8 @@ class GoogleLens: available = False def __init__(self): - if 'chompjs' not in sys.modules: - logger.warning('chompjs not available, Google Lens will not work!') + if 'pyjson5' not in sys.modules: + logger.warning('pyjson5 not available, Google Lens will not work!') elif 'requests' not in sys.modules: logger.warning('requests not available, Google Lens will not work!') else: @@ -176,15 +176,17 @@ class GoogleLens: x = '' if res.status_code == 200: - regex = re.compile(r">AF_initDataCallback\(({key: 'ds:1'.*?);") + regex = re.compile(r">AF_initDataCallback\(({key: 'ds:1'.*?)\);") match = regex.search(res.text) if match != None: - text = chompjs.parse_js_object(match.group(1))["data"][3][4][0] - if len(text) > 0: - lines = text[0] - for line in lines: - x += line + ' ' - x = post_process(x) + lens_object = pyjson5.loads(match.group(1)) + if not 'errorHasStatus' in lens_object: + text = lens_object['data'][3][4][0] + if len(text) > 0: + lines = text[0] + for line in lines: + x += line + ' ' + x = post_process(x) return x