Centos7版本
# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
# yum install -y automake autoconf libtool gcc gcc-c++ libpng-devel libjpeg-devel libtiff-devel
1、编译安装leptonica
需要先安装leptonica,下载地址 http://www.leptonica.org/download.html
# tar -zxvf leptonica-1.77.0.tar.gz
# cd leptonica-1.77.0
# ./configure
# make && make install
# vi /etc/profile 在文件最后添加如下三行
export LD_LIBRARY_PATH=$LD_LIBRARY_PAYT:/usr/local/lib
export LIBLEPT_HEADERSDIR=/usr/local/include
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
# source /etc/profile
2、编译安装tesseract
tesseract下载地址 https://github.com/tesseract-ocr/tesseract/releases
# tar -zxvf tesseract-4.0.0.tar.gz
# cd tesseract-4.0.0
# ./autogen.sh
# ./configure --with-extra-includes=/usr/local/include --with-extra-libraries=/usr/local/include
这时就不会再提示 leptonica 版本的问题了。再进行
# make && make install
3、安装tessdata多语言
tesseract的多语言项目是tessdata,下载地址 https://github.com/tesseract-ocr/tessdata/releases
# mkdir -p /usr/local/share/tessdata/lang
# vi /etc/profile 在文件最后添加如下2行
export TESSDATA_PREFIX=/usr/local/share/tessdata/lang
export LC_ALL=C
# source /etc/profile
# tar -zxvf tessdata-4.0.0.tar.gz
# mv tessdata-4.0.0/* /usr/local/share/tessdata/lang
# tesseract --list-langs
4、识别验证码
# yum install -y wget
# wget https://raw.githubusercontent.com/Python3WebSpider/TestTess/master/image.png
# tesseract image.png image -l chi_sim+eng 这条命令是使用中文和英文语言识别image.png中的文本
# vi image.txt 查看内容
5、使用python接口识别
# pip install tesserocr pillow 很慢,耐心等待
# vi test.py 输入如下内容
import tesserocr
from PIL import Image
image = Image.open('image.png')
print(tesserocr.image_to_text(image)) #或者tesserocr.image_to_text(image, lang='chi_sim')
# python test.py 即可识别图片中的文本
如果需要识别带中文的内容,代码需要调整:
# encoding=utf8
# -*- coding: utf8 -*-
import tesserocr
from PIL import Image
image = Image.open('h1.jpg')
msg = tesserocr.image_to_text(image, lang='chi_sim')
with open('hello.txt', 'w', encoding='utf-8') as file_object:
file_object.write(msg)