centos 下使用 pytesseract 识别文字

偶发一个想法搭一个验证码识别工具,网上查了一下有Tesseract 这个工具可以识别,所以有了后面一小时的搭建过程

ps:Ubuntu 下似乎可以直接用包管理工具来安装,我使用的源码编译安装

前提

  1. 由于自己电脑是工作用的,所以一些常用编译工具齐全,不这里介绍, 另外最好使用root 来编译
  2. tesseract 依赖 leptonica, 而安装leptonica前前先安装常用图片库,
    因为leptonica其实是对那些常用库进行了封装,如果编译时没有找到这个库,后面使用的时候就不会支持了
yum install libtiff-devel libjpeg-devel libpng-devel -y
  1. 安装 leptonica: 上github 上下载源码后
 ./autogen.sh 
./configure --prefix=/usr/local
make -j2   # 如果更多核可以并发编译速度快
  1. 编译leptonica 后,再编译tesseract
    同样下载源码后执行三个命令
 ./autogen.sh 
./configure --prefix=/usr/local
make -j2   # 如果更多核可以并发编译速度快

其中make 可能会报错 ,直接删除 aclocal.m4,重新执行 ./autogen.sh

libtool: Version mismatch error.  This is libtool 2.4.6, but the
libtool: definition of this LT_INIT comes from libtool 2.4.2.
libtool: You should recreate aclocal.m4 with macros from libtool 2.4.6
libtool: and run autoconf again.

下载训练数据,可以直接在github 上下载, 保存在 =/usr/local/share/tessdata 下面

https://github.com/tesseract-ocr/tessdata

chi_sim.traineddata  中文
eng.traineddata      英文
enm.traineddata      数字

然后添加环境变量 : 添加 export TESSDATA_PREFIX=/usr/local/share/tessdata 到 /etc/bashrc

使用

  1. 安装好后可以直接使用tesseract命令
tesseract cde.png result -l chi_sim

但是我自己测试的很多问题,识别不出来,但是使用python 可以

  1. 安装python 库 pip install pytesseract 很简单可使用了
Python 3.7.3 (default, Mar 27 2019, 22:11:17) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pytesseract
>>> from PIL import Image
>>> image = Image.open("abc.png")
>>> text = pytesseract.image_to_string(image,lang='chi_sim')
>>> print(text)
Bai暨匡'
『 百 度
>>> 

这里识别的是百度首页logo

猜你喜欢

转载自www.cnblogs.com/hustcpp/p/11650813.html
今日推荐