Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ocr_google_lens.py #689

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions modules/ocr/ocr_google_lens.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import numpy as np
import time
import cv2
import random
import string
from typing import List

import httpx
Expand All @@ -19,13 +21,14 @@ class LensCore:
'image/x-icon', 'image/bmp', 'image/jpeg',
'image/png', 'image/tiff', 'image/webp', 'image/heic'
]
# https://github.com/AuroraWright/owocr/blob/master/owocr/ocr.py
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-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en-US,en;q=0.9',
'Origin': 'https://lens.google.com',
'Referer': 'https://lens.google.com/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'User-Agent': 'Mozilla/5.0 (SMART-TV; Linux; Tizen 6.0) AppleWebKit/538.1 (KHTML, like Gecko) Version/6.0 TV Safari/538.1 STvPlus/9e6462f14a056031e5b32ece2af7c3ca,gzip(gfe),gzip(gfe)'
}

def __init__(self, proxy=None):
Expand All @@ -36,6 +39,8 @@ def _send_request(self, url, headers, files):
try:
client = httpx.Client(proxies=self.proxy) if self.proxy else httpx.Client()
response = client.post(url, headers=headers, files=files)
if response.status_code == 303:
raise Exception("Error 303: See Other. Potential misconfiguration in headers or file upload.")
if response.status_code != 200:
raise Exception(f"Failed to upload image. Status code: {response.status_code}")
return response
Expand All @@ -44,8 +49,9 @@ def _send_request(self, url, headers, files):

def scan_by_data(self, data, mime, dimensions):
headers = self.HEADERS.copy()
random_filename = ''.join(random.choices(string.ascii_letters, k=8)) + '.jpg'
files = {
'encoded_image': ('image.jpg', data, mime),
'encoded_image': (random_filename, data, mime),
'original_width': (None, str(dimensions[0])),
'original_height': (None, str(dimensions[1])),
'processed_image_dimensions': (None, f"{dimensions[0]},{dimensions[1]}")
Expand Down