Search code examples
pythoneasyocr

How to install easyocr library so the following error doesn`t arise


I am using easyocr library in my project to get text from screenshots my program is making. I have installed the library with pip install easyocr I have added text recognition to my code:

def text_recognition(im, languages):
    reader = easyocr.Reader(languages)
    language = reader.readtextlang(im, paragraph=True)
    if language == 'en':
        result = reader.readtext(im, paragraph=True)
        print(result)
        return result
text = text_recognition('screenshot1.png', ['en', 'ru'])

But when I try to execute it, the following error arises:

Traceback (most recent call last):
  File "C:\Users\childoflogos\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 1038, in _bootstrap_inner
    self.run()
  File "C:\Users\childoflogos\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 975, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\childoflogos\Desktop\auto_standoff_software\main3.py", line 225, in parse
    text = text_recognition('screenshot1.png', ['en', 'ru'])
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\childoflogos\Desktop\auto_standoff_software\main3.py", line 16, in text_recognition
    language = reader.readtextlang(im, paragraph=True)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\childoflogos\Desktop\auto_standoff_software\venv\Lib\site-packages\easyocr\easyocr.py", line 500, in readtextlang
    result = self.recognize(img_cv_grey, horizontal_list, free_list,\
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\childoflogos\Desktop\auto_standoff_software\venv\Lib\site-packages\easyocr\easyocr.py", line 383, in recognize
    image_list, max_width = get_image_list(h_list, f_list, img_cv_grey, model_height = imgH)
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\childoflogos\Desktop\auto_standoff_software\venv\Lib\site-packages\easyocr\utils.py", line 613, in get_image_list
    crop_img,ratio = compute_ratio_and_resize(crop_img,width,height,model_height)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\childoflogos\Desktop\auto_standoff_software\venv\Lib\site-packages\easyocr\utils.py", line 576, in compute_ratio_and_resize
    img = cv2.resize(img,(int(model_height*ratio),model_height),interpolation=Image.ANTIALIAS)
                                                                              ^^^^^^^^^^^^^^^
AttributeError: module 'PIL.Image' has no attribute 'ANTIALIAS'

I tried reinstalling the library with:

pip uninstall easyocr
y
pip install easyocr

Yet the error doesn`t dissapear. What should I do in order for easyocr to work correctly?


Solution

  • same problem, antialias is deprecated in version 10+ of PIL, you can change script of easyocr lib, that causes this problem, commonly IDE shows the source of the mistake in Exeption message, so if you see the path of exact "*.py" file, try to click on it,

    in yours example click on

    "C:\Users\childoflogos\Desktop\auto_standoff_software\venv\Lib\site-packages\easyocr\utils.py"

    or open that file, stored in the path, appeared in Exeption message, then open "utils.py" in your IDE or other suitable application(e.g. Notepad)

    find line №574 and line №576 ("if, else" statement), just comment with "#" parameter "interpolation=Image.ANTIALIAS" so it will be changed to "#interpolation=Image.ANTIALIAS)"

    you can even delete the part of code:"interpolation=Image.ANTIALIAS" but i prefer to leave it as a comment, to be able to recover it.

    don't forget to close brackets by typing ")" before comment sign "#", or you might choose other methods of interpolation mentioned in cv2 docs:

    (interpolation=cv2.INTER_AREA), or one of [cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_LANCZOS4, cv2.INTER_LANCZOS4].

    as for me i just commented and left it work by deffault, i haven't tried to use this cv2. interpolation methods, also i've wrote to developer of easyocr about this issue.