LoginSignup
shoukaiseki00
@shoukaiseki00

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

pyocrを使おうとするとエラーが起きます。

解決したいこと

pyocrの利用ができない。

例)
pyocrを利用する過程で、下記のようなエラーが(ターミナルで)発生しました。
ディレクトリに問題があると思うのですが、解決方法がわからないので、教えてください。
おそらくコードに問題はないと思いますが、一応コードも載せます。

*使用環境
vscode バージョン: 1.52.1
python バージョン:3.9.1
macOS Big Sur バージョン:11.2.1

発生している問題・エラー

raise TesseractError(status, errors)
pyocr.error.TesseractError: (1, b'Error opening data file /usr/local/share/tessdata/jpn.traineddata\nPlease make sure the TESSDATA_PREFIX environment variable is set to your "tessdata" directory.\nFailed loading language \'jpn\'\nTesseract couldn\'t load any languages!\nCould not initialize tesseract.\n')

該当するソースコード

#***には名前が入ります。
#色々書いてるので見にくいと思います。すいません。

import pyautogui as pag
import pyocr
from PIL import Image
import sys
import subprocess

import pyperclip
import os
import time
from pyscreeze import ImageNotFoundException 

CLOSE_BUTTON_X = 13
CLOSE_BUTTON_Y = 40

acr_path = '/Applications/Adobe Acrobat Reader DC.app'
todekede_path = '/Users/***/Downloads/lec_rpa/todokede_data/'

todokede_list = os.listdir(todekede_path)

NAME_W = 500

def detect_name_posi():
    pag.moveTo(1, 1)
    for count in range(50):
        try:
            #x, y, w, h = pag.locateOnScreen('/Users/***/Desktop/lec_rpa/shimei.png')
            x, y, w, h = pag.locateOnScreen('/Users/***/Downloads/lec_rpa/pdf_icon.png')
            break
        except ImageNotFoundException:
            time.sleep(1)
    return x, y, w, h


def get_name_img(x, y, w, h, NAME_W):
    start_x = 1350
    start_y = 1222
    name_img = pag.screenshot(region=(start_x, start_y, 350, 70))
    return name_img

def run_ocr(tool, name_img):
    result = tool.image_to_string(name_img, lang='jpn')
    result = result.replace(' ', '')
    print(result)
    return result


if __name__ == '__main__':
    tools = pyocr.get_available_tools()
    if len(tools) == 0:
        print("No OCR tool found")
        sys.exit(1)
    tool = tools[0]


    for idx, file in enumerate(todokede_list):
        print('open:', file)

        pdf_pro = subprocess.Popen(['open', acr_path, todekede_path+file])
        time.sleep(1)   

        x, y, w, h = detect_name_posi()
        print("start_x, start_y, NAME_W, 400") 

        name_img = get_name_img(677, 616, 200, 400, 200)

        result = run_ocr(tool, name_img)

        pag.click(CLOSE_BUTTON_X, CLOSE_BUTTON_Y)
        time.sleep(1) 

        if idx == 0:
            break 

自分で試したこと

発生しているエラーを調べて、ディレクトリ?に問題があるんじゃないかなというところまでしかわかりませんでした。

0

No Answers yet.

Your answer might help someone💌