Lens: add request timeout, handle no text being detected
This commit is contained in:
11
owocr/ocr.py
11
owocr/ocr.py
@@ -168,15 +168,20 @@ class GoogleLens:
|
|||||||
|
|
||||||
timestamp = int(time.time() * 1000)
|
timestamp = int(time.time() * 1000)
|
||||||
url = f"https://lens.google.com/v3/upload?stcs={timestamp}"
|
url = f"https://lens.google.com/v3/upload?stcs={timestamp}"
|
||||||
files = {"encoded_image": ('owo' + str(timestamp) +'.png', self._preprocess(img), 'image/png')}
|
files = {"encoded_image": ('owo' + str(timestamp) + '.png', self._preprocess(img), 'image/png')}
|
||||||
res = requests.post(url, files=files)
|
try:
|
||||||
|
res = requests.post(url, files=files, timeout=(3, 10))
|
||||||
|
except requests.exceptions.Timeout:
|
||||||
|
return "Request timeout!"
|
||||||
|
|
||||||
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:
|
||||||
lines = chompjs.parse_js_object(match.group(1))["data"][3][4][0][0]
|
text = chompjs.parse_js_object(match.group(1))["data"][3][4][0]
|
||||||
|
if len(text) > 0:
|
||||||
|
lines = text[0]
|
||||||
for line in lines:
|
for line in lines:
|
||||||
x += line + ' '
|
x += line + ' '
|
||||||
x = post_process(x)
|
x = post_process(x)
|
||||||
|
|||||||
Reference in New Issue
Block a user