Hello!
Thank you very much for your hard work—it is highly appreciated.
I am using your product via the API and have the following question:
There is a type of image where text recognition works very well in the Opera/Chrome browser extension. However, recognition through the API is quite poor. Could you please let me know what I might be doing wrong?
In general, if the text is simple, it is recognized well, but more complex texts are recognized poorly, even though the browser extension handles them correctly.
I am attaching links to the images and code:
-
Original Image: https://img.youtube.com/vi/bz-t6AdzqMA/maxresdefault.jpg
-
Recognized by the Extension: Print1.png
-
How it was recognized API (text from the .txt file) Print2.png:
W/ UICHASZ gov HISUFZ2ZPZN20N
-
**My Code:
# --- OCR Function (Your Working Code) ---
def ocr_space_file(filename, overlay=False, api_key='11111111111111111', language='eng'):
""" OCR.space API request with a local file.
:param filename: Your file path & name.
:param overlay: Is OCR.space overlay required in your response.
Defaults to False.
:param api_key: OCR.space API key.
Defaults to 'helloworld'.
:param language: Language code to be used in OCR.
Defaults to 'eng'.
:return: Result in JSON format.
"""
payload = {
'isOverlayRequired': overlay,
'apikey': api_key,
'language': language,
}
try:
with open(filename, 'rb') as f:
r = requests.post(
'https://api.ocr.space/parse/image',
files={'file': f},
data=payload,
proxies=None # Disable proxies for OCR
)
# Check response status
if r.status_code != 200:
print(f"Error: Unable to connect to OCR.space API. Status code: {r.status_code}")
return None
# Parse JSON response
result = r.json()
return result
except requests.exceptions.RequestException as e:
print(f"Error while making a request to OCR.space API: {e}")
return None
except ValueError as e:
print(f"Error parsing JSON response: {e}")
print(r.text) # Output response for debugging
return None
# --- Function to Save Text to File ---
def save_text_to_file(text, file_path):
"""Saves text to a file."""
with open(file_path, 'w', encoding='utf-8') as f:
f.write(text)
What am I doing wrong?