###pyocrにて長いエラーが出て検索のしようがない
pyocrを用いた簡単な文字認識コードを書いているのですが、
エラー文が長く検索の仕方も難しいため
非常に解決にたどり着けず困っています。
実行結果
<module 'pyocr.tesseract' from 'C:\Users\プライベート\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pyocr\tesseract.py'> Warning (from warnings module): File "C:\Users\プライベート\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\PIL\Image.py", line 975 warnings.warn( UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images Traceback (most recent call last): File "C:\Users\プライベート\Desktop\python\test.py", line 14, in <module> txt = tool.image_to_string( File "C:\Users\プライベート\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pyocr\tesseract.py", line 369, in image_to_string raise TesseractError(status, errors) pyocr.error.TesseractError: (1, b'Error opening data file C:\Program Files\Tesseract-OCR\tessdata/jpn.traineddata\r\nPlease make sure the TESSDATA_PREFIX environment variable is set to your "tessdata" directory.\r\nFailed loading language \'jpn\'\r\nTesseract couldn\'t load any languages!\r\nCould not initialize tesseract.\r\n') >>>
該当のソースコード
python
1from PIL import Image 2import sys 3import pyocr 4import pyocr.builders 5 6 7pyocr.tesseract.TESSERACT_CMD = r"C:\Program Files (x86)\Tesseract-OCR\tesseract.exe" 8 9tools = pyocr.get_available_tools() 10tool = tools[0] 11 12print(tool) 13picture = Image.open("test1.png") 14txt = tool.image_to_string( 15 picture, 16 lang="jpn", 17 builder = pyocr.builders.TextBuilder(tesseract_layout = 3) 18) 19print(txt)
###追記
環境変数「TESSDATA_PREFIX」を設定しようと試みましたが
改善の余地は見られませんでした。
以下ソースコード
python
1from PIL import Image 2import sys 3import os 4import pyocr 5import pyocr.builders 6 7 8 9TESSERACT_PATH = 'C:\Program Files (x86)\Tesseract-OCR' 10TESSDATA_PATH = 'C:\Program Files (x86)\Tesseract-OCR\tessdata' 11 12os.environ["PATH"] += os.pathsep +TESSERACT_PATH 13os.environ["TESSDATA_PREFIX"] = TESSDATA_PATH 14 15tools = pyocr.get_available_tools() 16if len(tools) == 0: 17 print("p") 18tool = tools[0] 19 20print(tool) 21picture = Image.open("test1.png") 22txt = tool.image_to_string( 23 picture, 24 lang="jpn", 25 builder = pyocr.builders.TextBuilder(tesseract_layout = 3) 26) 27print(txt) 28
以下エラー内容
<module 'pyocr.tesseract' from 'C:\Users\プライベート\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pyocr\tesseract.py'> Warning (from warnings module): File "C:\Users\プライベート\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\PIL\Image.py", line 975 warnings.warn( UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images Traceback (most recent call last): File "C:\Users\プライベート\Desktop\python\test.py", line 23, in <module> txt = tool.image_to_string( File "C:\Users\プライベート\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pyocr\tesseract.py", line 369, in image_to_string raise TesseractError(status, errors) pyocr.error.TesseractError: (1, b'Error opening data file C:\Program Files (x86)\Tesseract-OCR\tessdata/jpn.traineddata\r\nPlease make sure the TESSDATA_PREFIX environment variable is set to your "tessdata" directory.\r\nFailed loading language \'jpn\'\r\nTesseract couldn\'t load any languages!\r\nCould not initialize tesseract.\r\n')
回答1件
あなたの回答
tips
プレビュー