実現したいこと
画像を読み込んでその画像に記されている文字を結果として出力する
発生している問題・分からないこと
OCRの実行の部分で壁に当たっている
エラーメッセージ
error
1TesseractError: (1, b'Error opening data file /usr/share/tesseract-ocr/4.00/egn.traineddata\nPlease make sure the TESSDATA_PREFIX environment variable is set to your "tessdata" directory.\nFailed loading language \'egn\'\nTesseract couldn\'t load any languages!\nCould not initialize tesseract.\n')
該当のソースコード
python
1from PIL import Image 2import sys 3import pyocr 4import pyocr.builders 5import cv2 6import numpy as np 7import matplotlib.pyplot as plt 8 9# OpenCVの画像データをPIL形式に変換する関数 10def cv2pil(cv_img): 11 return Image.fromarray(cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB)) 12 13# 画像のパスのリスト 14image_paths = [ 15 '/content/drive/MyDrive/PHOTO/ORIGINAL_PHOTO.JPG', 16 '/content/drive/MyDrive/PHOTO/ORIGINAL_PHOTO1.png', 17 '/content/drive/MyDrive/PHOTO/ORIGINAL_PHOTO2.png', 18 '/content/drive/MyDrive/PHOTO/ORIGINAL_PHOTO3.png', 19 '/content/drive/MyDrive/PHOTO/ORIGINAL_PHOTO4.png' 20 # 他の画像のパスも追加 21] 22 23# OCRの準備 24tools = pyocr.get_available_tools() 25if len(tools) == 0: 26 print("No OCR tool found") 27 sys.exit(1) 28tool = tools[0] 29 30# 画像ごとに処理 31for image_path in image_paths: 32 # 画像の読み込み 33 img_or = cv2.imread(image_path) 34 35 # 画像が正しく読み込まれたか確認 36 if img_or is not None: 37 # 画像をMatplotlibを使って表示 (BGRからRGBに変換) 38 plt.imshow(cv2.cvtColor(img_or, cv2.COLOR_BGR2RGB)) 39 plt.axis('off') # 軸を非表示にする 40 plt.show() 41 else: 42 print(f"Error: Unable to read the image at path {image_path}.") 43 continue 44 45 # 以下に画像処理やOCRの処理を追加する 46 # 例:グレースケール化 47 img_gray = cv2.cvtColor(img_or, cv2.COLOR_BGR2GRAY) 48 # 他の処理も追加 49 50 # OCR実行 51 temp_pil_im = cv2pil(img_or) # 画像データをPIL形式に変換 52 txt = tool.image_to_string( 53 temp_pil_im, 54 lang="egn", # 英語の言語データを使用する 55 builder=pyocr.builders.TextBuilder(tesseract_layout=6)) 56 print(txt)
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
同様のエラーがないか調べたが複雑で分かりずらく照らし合わせられなかった。
補足
特になし
