Switch to pyjson5 as chompjs needs manual building (especially annoyin on Windows), detect Lens errors

This commit is contained in:
AuroraWright
2024-01-21 18:30:54 +01:00
parent 01d0a94a7c
commit 70687e6b01
2 changed files with 13 additions and 11 deletions

View File

@@ -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) - 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 ## 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) - 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) - 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)

View File

@@ -57,7 +57,7 @@ except ImportError:
pass pass
try: try:
import chompjs import pyjson5
except ImportError: except ImportError:
pass pass
@@ -150,8 +150,8 @@ class GoogleLens:
available = False available = False
def __init__(self): def __init__(self):
if 'chompjs' not in sys.modules: if 'pyjson5' not in sys.modules:
logger.warning('chompjs not available, Google Lens will not work!') logger.warning('pyjson5 not available, Google Lens will not work!')
elif 'requests' not in sys.modules: elif 'requests' not in sys.modules:
logger.warning('requests not available, Google Lens will not work!') logger.warning('requests not available, Google Lens will not work!')
else: else:
@@ -176,15 +176,17 @@ class GoogleLens:
x = '' x = ''
if res.status_code == 200: if res.status_code == 200:
regex = re.compile(r">AF_initDataCallback\(({key: 'ds:1'.*?);</script>") regex = re.compile(r">AF_initDataCallback\(({key: 'ds:1'.*?)\);</script>")
match = regex.search(res.text) match = regex.search(res.text)
if match != None: if match != None:
text = chompjs.parse_js_object(match.group(1))["data"][3][4][0] lens_object = pyjson5.loads(match.group(1))
if len(text) > 0: if not 'errorHasStatus' in lens_object:
lines = text[0] text = lens_object['data'][3][4][0]
for line in lines: if len(text) > 0:
x += line + ' ' lines = text[0]
x = post_process(x) for line in lines:
x += line + ' '
x = post_process(x)
return x return x