tesserocrには、Windows環境でのさまざまな非互換性の問題や、pycharm仮想環境との非互換性があるため、Windowsシステム環境では、インストールするpytesseractモジュールを選択します。本当にインストールする場合は、whlファイルインストールを使用するか、condaインストールを使用してください。
pip install pytesseract
pytesseractの実行中にtesseractインタープリターが見つからない場合、この状況は通常、仮想環境で発生します。tesseract-OCR実行可能ファイルtesseract.extをWindowsシステムのPATH環境に構成するか、pytesseractを変更する必要があります。 pyファイルの場合、「tesseract_cmd」フィールドをtesseract.exeのフルパスとして指定します
テスト認識機能:
import pytesseract
from PIL import Image
image = Image.open('tesseracttest.png') # 图片名
text = pytesseract.image_to_string(image)
print(text)
Ubuntu、Linuxシステムでは、インストールコマンドは次のとおりです
#安装tesseract
sudo apt-get install -y tesseract-ocr libtesseract-dev libleptonica-dev
#安装语言包
git clone https://github.com/tesseract-ocr/tessdata.git
sudo mv tessdata/* /usr/share/tesseract-ocr/tessdata
#安装pytesseract
pip3 install pytesseract
写真の内容を特定し、別の写真に書き込みます
from PIL import Image
import subprocess
def cleanFile(filePath, newFilePath):
image = Image.open(filePath)
# 对图片进行阈值过滤(低于143的置为黑色,否则为白色)
image = image.point(lambda x: 0 if x < 143 else 255)
# 重新保存图片
image.save(newFilePath)
# 调用系统的tesseract命令对图片进行OCR识别
subprocess.call(["tesseract", newFilePath, "output"])
# 打开文件读取结果
with open("output.txt", 'r') as f:
print(f.read())
if __name__ == "__main__":
cleanFile("tesseracttest.jpg", "123.jpg") # 读取tesseracttest内的文字,再把文字写入123中
今回は簡単な識別と次回の更新