mac-python-tesseract-亦可用于验证码识别

想识别图片中的一些文字,最终还是选择了谷歌的tesseract,即使它对中文的识别度还不是特别高,比如左右偏旁的字会识别成两个字,比如“呐”识别成“口内”,又或者说一些比较偏的字也会识别错,当然可以多训练一点汉字,如何训练数据暂且不说。

环境

macOS 10.12.5、 python 2.7、homebrew、pip

步骤

  1. 用homebrew 在电脑上安装tesseract库 brew install tesseract
  2. 用pip安装支持python的tesseract 接口pip install pytesseract
  3. 去往https://github.com/tesseract-ocr/tessdata下载中文数据集chi_sim.traineddata,把它放到这目录下:
     
    image.png

如何使用tesseract

import pytesseract
from PIL import Image

# open image
image = Image.open('test.png')
code = pytesseract.image_to_string(image, lang='chi_sim')
print(code)

遇到的问题

  • 提示brew update,代表homebrew需要更新
  • 提示must be writable!或者Permission denied之类问题,试试前面加sudo
  • 提示Please check the permissions and owner of that directory,说明权限有问题,那么使用sudo chown root 文件路径命令获得临时root权限
  • 提示Xcode alone is not sufficient on Sierra,使用xcode-select --install

总结

没有走步骤1,是没有步骤3的路径,运行python文件时也会报找不到tesseract文件路径的错。实际识别图片时可以先给图片做一些处理,例如高斯去噪,直方图均衡化加清晰,二值化去掉不相关信息,我用到了二值化,设置好阈值达到自己想要的效果。




转载于:https://www.jianshu.com/p/2c9459059400

原文地址:https://www.cnblogs.com/cheflone/p/13504370.html