Actually fix Bing image size issue
This commit is contained in:
14
owocr/ocr.py
14
owocr/ocr.py
@@ -337,6 +337,7 @@ class GoogleLensWeb:
|
|||||||
if 'pyjson5' not in sys.modules:
|
if 'pyjson5' not in sys.modules:
|
||||||
logger.warning('pyjson5 not available, Google Lens (web) will not work!')
|
logger.warning('pyjson5 not available, Google Lens (web) will not work!')
|
||||||
else:
|
else:
|
||||||
|
self.requests_session = requests.Session()
|
||||||
self.available = True
|
self.available = True
|
||||||
logger.info('Google Lens (web) ready')
|
logger.info('Google Lens (web) ready')
|
||||||
|
|
||||||
@@ -345,7 +346,6 @@ class GoogleLensWeb:
|
|||||||
if not img:
|
if not img:
|
||||||
return (False, 'Invalid image provided')
|
return (False, 'Invalid image provided')
|
||||||
|
|
||||||
requests_session = requests.Session()
|
|
||||||
url = 'https://lens.google.com/v3/upload'
|
url = 'https://lens.google.com/v3/upload'
|
||||||
files = {'encoded_image': ('image.png', self._preprocess(img), 'image/png')}
|
files = {'encoded_image': ('image.png', self._preprocess(img), 'image/png')}
|
||||||
headers = {
|
headers = {
|
||||||
@@ -368,7 +368,7 @@ class GoogleLensWeb:
|
|||||||
cookies = {'SOCS': 'CAESEwgDEgk0ODE3Nzk3MjQaAmVuIAEaBgiA_LyaBg'}
|
cookies = {'SOCS': 'CAESEwgDEgk0ODE3Nzk3MjQaAmVuIAEaBgiA_LyaBg'}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
res = requests_session.post(url, files=files, headers=headers, cookies=cookies, timeout=20, allow_redirects=False)
|
res = self.requests_session.post(url, files=files, headers=headers, cookies=cookies, timeout=20, allow_redirects=False)
|
||||||
except requests.exceptions.Timeout:
|
except requests.exceptions.Timeout:
|
||||||
return (False, 'Request timeout!')
|
return (False, 'Request timeout!')
|
||||||
except requests.exceptions.ConnectionError:
|
except requests.exceptions.ConnectionError:
|
||||||
@@ -388,7 +388,7 @@ class GoogleLensWeb:
|
|||||||
return (False, 'Unknown error!')
|
return (False, 'Unknown error!')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
res = requests_session.get(f"https://lens.google.com/qfmetadata?vsrid={query_params['vsrid'][0]}&gsessionid={query_params['gsessionid'][0]}", timeout=20)
|
res = self.requests_session.get(f"https://lens.google.com/qfmetadata?vsrid={query_params['vsrid'][0]}&gsessionid={query_params['gsessionid'][0]}", timeout=20)
|
||||||
except requests.exceptions.Timeout:
|
except requests.exceptions.Timeout:
|
||||||
return (False, 'Request timeout!')
|
return (False, 'Request timeout!')
|
||||||
except requests.exceptions.ConnectionError:
|
except requests.exceptions.ConnectionError:
|
||||||
@@ -429,6 +429,7 @@ class Bing:
|
|||||||
available = False
|
available = False
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
self.requests_session = requests.Session()
|
||||||
self.available = True
|
self.available = True
|
||||||
logger.info('Bing ready')
|
logger.info('Bing ready')
|
||||||
|
|
||||||
@@ -441,7 +442,6 @@ class Bing:
|
|||||||
if not img_bytes:
|
if not img_bytes:
|
||||||
return (False, 'Image is too big!')
|
return (False, 'Image is too big!')
|
||||||
|
|
||||||
requests_session = requests.Session()
|
|
||||||
upload_url = 'https://www.bing.com/images/search?view=detailv2&iss=sbiupload'
|
upload_url = 'https://www.bing.com/images/search?view=detailv2&iss=sbiupload'
|
||||||
upload_headers = {
|
upload_headers = {
|
||||||
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
|
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
|
||||||
@@ -459,7 +459,7 @@ class Bing:
|
|||||||
for _ in range(2):
|
for _ in range(2):
|
||||||
api_host = urlparse(upload_url).netloc
|
api_host = urlparse(upload_url).netloc
|
||||||
try:
|
try:
|
||||||
res = requests_session.post(upload_url, headers=upload_headers, files=files, timeout=20, allow_redirects=False)
|
res = self.requests_session.post(upload_url, headers=upload_headers, files=files, timeout=20, allow_redirects=False)
|
||||||
except requests.exceptions.Timeout:
|
except requests.exceptions.Timeout:
|
||||||
return (False, 'Request timeout!')
|
return (False, 'Request timeout!')
|
||||||
except requests.exceptions.ConnectionError:
|
except requests.exceptions.ConnectionError:
|
||||||
@@ -500,7 +500,7 @@ class Bing:
|
|||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
res = requests_session.post(api_url, headers=api_headers, files=files, timeout=20)
|
res = self.requests_session.post(api_url, headers=api_headers, files=files, timeout=20)
|
||||||
except requests.exceptions.Timeout:
|
except requests.exceptions.Timeout:
|
||||||
return (False, 'Request timeout!')
|
return (False, 'Request timeout!')
|
||||||
except requests.exceptions.ConnectionError:
|
except requests.exceptions.ConnectionError:
|
||||||
@@ -541,7 +541,7 @@ class Bing:
|
|||||||
res = None
|
res = None
|
||||||
|
|
||||||
if any(x > max_pixel_size for x in img.size):
|
if any(x > max_pixel_size for x in img.size):
|
||||||
resize_factor = max(max_pixel_size / img.width, max_pixel_size / img.height)
|
resize_factor = min(max_pixel_size / img.width, max_pixel_size / img.height)
|
||||||
new_w = int(img.width * resize_factor)
|
new_w = int(img.width * resize_factor)
|
||||||
new_h = int(img.height * resize_factor)
|
new_h = int(img.height * resize_factor)
|
||||||
img = img.resize((new_w, new_h), Image.Resampling.LANCZOS)
|
img = img.resize((new_w, new_h), Image.Resampling.LANCZOS)
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "owocr"
|
name = "owocr"
|
||||||
version = "1.14.5"
|
version = "1.14.6"
|
||||||
description = "Japanese OCR"
|
description = "Japanese OCR"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
requires-python = ">=3.11"
|
requires-python = ">=3.11"
|
||||||
|
|||||||
Reference in New Issue
Block a user