wolfmanstout / screen-ocr

Easily perform OCR on portions of the screen, choosing from a selection of backends.
Apache License 2.0
40 stars 7 forks source link

AttributeError: 'NoneType' object has no attribute 'recognize_async' #8

Closed DavraYoung closed 2 years ago

DavraYoung commented 2 years ago

I tried to install this package with winrt, but on import I got this error:

Traceback (most recent call last):
  File "C:\Users\Davron\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Users\Davron\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\projects\ocr\venv\lib\site-packages\screen_ocr\__main__.py", line 10, in <module>       
    results = ocr_reader.read_nearby(screen_coordinates=(0, 0))
  File "C:\projects\ocr\venv\lib\site-packages\screen_ocr\_screen_ocr.py", line 154, in read_nearby
    screenshot, offset=bounding_box[0:2], screen_coordinates=screen_coordinates
  File "C:\projects\ocr\venv\lib\site-packages\screen_ocr\_screen_ocr.py", line 160, in read_image
    result = self._backend.run_ocr(preprocessed_image)
  File "C:\projects\ocr\venv\lib\site-packages\screen_ocr\_winrt.py", line 68, in run_ocr
    lambda: asyncio.run(self._run_ocr_async(image))
  File "C:\Users\Davron\AppData\Local\Programs\Python\Python37\lib\concurrent\futures\_base.py", line 432, in result
    return self.__get_result()
  File "C:\Users\Davron\AppData\Local\Programs\Python\Python37\lib\concurrent\futures\_base.py", line 384, in __get_result
    raise self._exception
  File "C:\Users\Davron\AppData\Local\Programs\Python\Python37\lib\concurrent\futures\thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "C:\projects\ocr\venv\lib\site-packages\screen_ocr\_winrt.py", line 68, in <lambda>
    lambda: asyncio.run(self._run_ocr_async(image))
  File "C:\Users\Davron\AppData\Local\Programs\Python\Python37\lib\asyncio\runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "C:\Users\Davron\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py", line 568, in run_until_complete
    return future.result()
  File "C:\projects\ocr\venv\lib\site-packages\screen_ocr\_winrt.py", line 46, in run_ocr_async
    result = await engine.recognize_async(bitmap)
AttributeError: 'NoneType' object has no attribute 'recognize_async'

Steps:

  1. Install using pip install screen-ocr[winrt]
  2. Download and install Tesseract
  3. Try to run python -m screen_ocr

OC: Windows 10 Python: 3.7.0

wolfmanstout commented 2 years ago

Sounds like this line is returning None: engine = ocr.OcrEngine.try_create_from_user_profile_languages()

This is a Windows API. You should be able to reproduce this by running the following in a Python REPL:

import winrt
import winrt.windows.media.ocr as ocr
engine = ocr.OcrEngine.try_create_from_user_profile_languages()
engine

If engine is None, then that confirms that this is the problem. I'm not sure why this would be. The documentation indicates that this could be because your configured language is not supported. Are you using some language other than English?

DavraYoung commented 2 years ago

Issue solved. The problem was in language pack.

For any one who faces this issue:

image

wolfmanstout commented 2 years ago

Thank you for debugging this! Improved the error message to point here.