The expansion for the browser recognizes better than through the API

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:

# --- 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? :slight_smile:

Hi,

Your screenshot is from Copyfish. This extension uses scale=true and ocrengine=2 as the default settings.

So two suggestions for your API call:

(1) Use scale = true

'scale': "true",

(2) And maybe also use OcrEngine 2:

'ocrengine': "2",

These parameters worked for me.

    payload = {
               'isOverlayRequired': overlay,
               'apikey': api_key,
               'language': language,
                'scale': "true",       
                'ocrengine': "2"       
               }

Thanks it worked for me, now it recognizes more complex texts. Good luck to you.

1 Like