Switch to pyjson5 as chompjs needs manual building (especially annoyin on Windows), detect Lens errors
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
12
owocr/ocr.py
12
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,10 +176,12 @@ class GoogleLens:
|
||||
|
||||
x = ''
|
||||
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)
|
||||
if match != None:
|
||||
text = chompjs.parse_js_object(match.group(1))["data"][3][4][0]
|
||||
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:
|
||||
|
||||
Reference in New Issue
Block a user