python-3.xocrlstmtesseract

In tesserocr, When I initailize api with oem and psm options to run DetectOS, it raises error(Fatal Python error: Aborted). Why?


Issue:

In tesserocr, When I initialize api with oem and psm options to run DetectOS or DetectOrientationScript, it raises error(Fatal Python error: Aborted). Why this happened? codes:

from tesserocr import PyTessBaseAPI, PSM, OEM

img_path = r'C:\Users\zl\Desktop\asd.png'
tessdata = r'E:\Workspace\Pycharm\ocr-test\data\tessdata\tessdata-4.1.0\\'
def test3_4():
    with PyTessBaseAPI(path=tessdata, lang='chi_sim+eng', psm=PSM.OSD_ONLY, oem=OEM.LSTM_ONLY) as api:
        api.SetImageFile(img_path)
        os = api.DetectOrientationScript()
        print("Orientation: {orient_deg}\nOrientation confidence: {orient_conf}\n"
              "Script: {script_name}\nScript confidence: {script_conf}".format(**os))

output:

D:\Anaconda3\envs\dev-ocr-test\python.exe "D:/JetBrains/PyCharm 2023.3.2/plugins/python/helpers/pycharm/_jb_pytest_runner.py" --target test_g_lts/test_ocr_algorithm/test_ocr_engine/test_tesseract_ocr_engine/test_tesseract_ocr_api.py::test3_4 
Testing started at 23:16 ...
Launching pytest with arguments test_g_lts/test_ocr_algorithm/test_ocr_engine/test_tesseract_ocr_engine/test_tesseract_ocr_api.py::test3_4 --no-header --no-summary -q in E:\Workspace\Pycharm\ocr-test\test

============================= test session starts =============================
collecting ... collected 1 item

test_g_lts/test_ocr_algorithm/test_ocr_engine/test_tesseract_ocr_engine/test_tesseract_ocr_api.py::test3_4 Fatal Python error: Aborted

Current thread 0x000009fc (most recent call first):
  File "E:\Workspace\Pycharm\ocr-test\test\test_g_lts\test_ocr_algorithm\test_ocr_engine\test_tesseract_ocr_engine\test_tesseract_ocr_api.py", line 241 in test3_4
  File "D:\Anaconda3\envs\dev-ocr-test\lib\site-packages\_pytest\python.py", line 159 in pytest_pyfunc_call
  File "D:\Anaconda3\envs\dev-ocr-test\lib\site-packages\pluggy\_callers.py", line 103 in _multicall
  File "D:\Anaconda3\envs\dev-ocr-test\lib\site-packages\pluggy\_manager.py", line 120 in _hookexec
  File "D:\Anaconda3\envs\dev-ocr-test\lib\site-packages\pluggy\_hooks.py", line 513 in __call__
  File "D:\Anaconda3\envs\dev-ocr-test\lib\site-packages\_pytest\python.py", line 1627 in runtest
  File "D:\Anaconda3\envs\dev-ocr-test\lib\site-packages\_pytest\runner.py", line 174 in pytest_runtest_call
  File "D:\Anaconda3\envs\dev-ocr-test\lib\site-packages\pluggy\_callers.py", line 103 in _multicall
  File "D:\Anaconda3\envs\dev-ocr-test\lib\site-packages\pluggy\_manager.py", line 120 in _hookexec
  File "D:\Anaconda3\envs\dev-ocr-test\lib\site-packages\pluggy\_hooks.py", line 513 in __call__
  File "D:\Anaconda3\envs\dev-ocr-test\lib\site-packages\_pytest\runner.py", line 242 in <lambda>
  File "D:\Anaconda3\envs\dev-ocr-test\lib\site-packages\_pytest\runner.py", line 341 in from_call
  File "D:\Anaconda3\envs\dev-ocr-test\lib\site-packages\_pytest\runner.py", line 241 in call_and_report
  File "D:\Anaconda3\envs\dev-ocr-test\lib\site-packages\_pytest\runner.py", line 132 in runtestprotocol
  File "D:\Anaconda3\envs\dev-ocr-test\lib\site-packages\_pytest\runner.py", line 113 in pytest_runtest_protocol
  File "D:\Anaconda3\envs\dev-ocr-test\lib\site-packages\pluggy\_callers.py", line 103 in _multicall
  File "D:\Anaconda3\envs\dev-ocr-test\lib\site-packages\pluggy\_manager.py", line 120 in _hookexec
  File "D:\Anaconda3\envs\dev-ocr-test\lib\site-packages\pluggy\_hooks.py", line 513 in __call__
  File "D:\Anaconda3\envs\dev-ocr-test\lib\site-packages\_pytest\main.py", line 362 in pytest_runtestloop
  File "D:\Anaconda3\envs\dev-ocr-test\lib\site-packages\pluggy\_callers.py", line 103 in _multicall
  File "D:\Anaconda3\envs\dev-ocr-test\lib\site-packages\pluggy\_manager.py", line 120 in _hookexec
  File "D:\Anaconda3\envs\dev-ocr-test\lib\site-packages\pluggy\_hooks.py", line 513 in __call__
  File "D:\Anaconda3\envs\dev-ocr-test\lib\site-packages\_pytest\main.py", line 337 in _main
  File "D:\Anaconda3\envs\dev-ocr-test\lib\site-packages\_pytest\main.py", line 283 in wrap_session
  File "D:\Anaconda3\envs\dev-ocr-test\lib\site-packages\_pytest\main.py", line 330 in pytest_cmdline_main
  File "D:\Anaconda3\envs\dev-ocr-test\lib\site-packages\pluggy\_callers.py", line 103 in _multicall
  File "D:\Anaconda3\envs\dev-ocr-test\lib\site-packages\pluggy\_manager.py", line 120 in _hookexec
  File "D:\Anaconda3\envs\dev-ocr-test\lib\site-packages\pluggy\_hooks.py", line 513 in __call__
  File "D:\Anaconda3\envs\dev-ocr-test\lib\site-packages\_pytest\config\__init__.py", line 175 in main
  File "D:\JetBrains\PyCharm 2023.3.2\plugins\python\helpers\pycharm\_jb_pytest_runner.py", line 60 in <module>

Process finished with exit code 3

Even though I actually ran it in the Main method, the program exited when I called DetectOrientationScript. Code:

from tesserocr import PyTessBaseAPI, PSM, OEM

img_path = r'C:\Users\zl\Desktop\asd.png'
tessdata = r'E:\Workspace\Pycharm\ocr-test\data\tessdata\tessdata-4.1.0\\'


def main():
    with PyTessBaseAPI(path=tessdata, lang='chi_sim+eng', psm=PSM.OSD_ONLY, oem=OEM.LSTM_ONLY) as api:
        api.SetImageFile(img_path)
        print('asdasdasdasdasdasdasd')
        os = api.DetectOrientationScript()
        print('asdasdasdasdasdasdasd')
        print("Orientation: {orient_deg}\nOrientation confidence: {orient_conf}\n"
              "Script: {script_name}\nScript confidence: {script_conf}".format(**os))
        print('asdasdasdasdasdasdasd')


if __name__ == "__main__":
    main()

output:

D:\Anaconda3\envs\dev-ocr-test\python.exe E:\Workspace\Pycharm\ocr-test\test\test_g_lts\test_ocr_algorithm\test_ocr_engine\test_tesseract_ocr_engine\tt.py 
asdasdasdasdasdasdasd

Process finished with exit code -1073740791 (0xC0000409)

exit code -1073740791 (0xC0000409): STATUS_STACK_BUFFER_OVERRUN.

Background:

Trying:

  1. trying 1:

    with PyTessBaseAPI(path=tessdata, lang='chi_sim+eng', oem=OEM.LSTM_ONLY) as api:
     api.SetImageFile(img_path)
     print(api.GetUTF8Text())
    

    case pass, which means:

    • the path of tessdata is right and language data are loaded successfully.
    • the language data is compatible with the LSTM mode.
  2. trying 2:

    with PyTessBaseAPI(path=tessdata, lang='chi_sim+eng', psm=PSM.OSD_ONLY, oem=OEM.TESSERACT_LSTM_COMBINED) as api:
     api.SetImageFile(img_path)
     os = api.DetectOS()
     print(
         "Orientation: {orientation}\nOrientation confidence: {oconfidence}\nScript: {script}\nScript confidence: {sconfidence}".format(
             **os))
    

    case pass, means:

    OSD is compatible with the LSTM mode.

    maybe? the doc says "Run the LSTM recognizer, but allow fallback to Tesseract when things get difficult.", maybe is Tesseract do the all of the work, while the LSTM is shutdown.

    And I asked GPT too, "The LSTM mode is incompatible with the OSD mode" is one of the answers. So the incompatibility between LSTM and OSD leads to STATUS_STACK_BUFFER_OVERRUN?

Expecting:

PS: the hover hints of PyTessBaseAPI is really different from the real code doc comment. The __init__ method only have two args, but the doc says there are more args. The hover hints really mislead me. hover hints image, doc comment image


Solution

  • This problem is indeed caused by the incompatibility between LSTM_ONLY and OSD_ONLY. If you do need to use OSD and LSTM, you need to use OEM.DEFAULT. And the another solution what sets the LSTM_ONLY through SetVariable method will change OEM to DEFAULT internally to ensure compatibility, rather than sets OEM as LSTM_ONLY.

    with PyTessBaseAPI(path=TESSDATA_DIRECTORY, lang='chi_sim+eng') as api:
        api.SetVariable('tessedit_ocr_engine_mode', str(OEM.LSTM_ONLY))
        api.SetPageSegMode(psm=PSM.OSD_ONLY)
        api.SetImageFile(TEST_IMAGE_ABSOLUTE_PATH[0])
        os = api.DetectOrientationScript()
        print(
            "Orientation: {orient_deg}\nOrientation confidence: {orient_conf}\nScript: {script_name}\nScript confidence: {script_conf}".format(
                **os))
        print(api.oem())  # 3 => OEM.DEFAULT
    

    @user898678 Thanks for your comments.