Tesseract:开源的OCR识别引擎
经测试: 英文数字识别,中文不行,验证码图片简单的可以复杂的识别不出来.
方案一(自己在干净系统上安装)
docker run --name i-centos --restart=always -v E:\Python:/www -itd daocloud.io/centos:7
https://blog.51cto.com/jschu/1702456
一.
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install gcc gcc-c++
yum install autoconf automake libtool
yum install libjpeg-devel libpng-devel libtiff-devel zlib-devel
yum install make
二.
安装leptonica
http://www.leptonica.org/download.html
./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
三.
tesseract-ocr安装
手动下载安装包。
https://github.com/tesseract-ocr/tesseract
cd tesseract-3.04.01
./autogen.sh
./configure
make && make install
ldconfig
四.
pip install pytesseract
pip install pillow
#!usr/bin/env python
# coding:utf-8
import pytesseract
from PIL import Image
image = Image.open(r'/www/1.jpg')
image = image.convert('L') #转化为灰度图
threshold = 127 #设定的二值化阈值
table = [] #table是设定的一个表,下面的for循环可以理解为一个规则,小于阈值的,就设定为0,大于阈值的,就设定为1
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
image = image.point(table,'1') #对灰度图进行二值化处理,按照table的规则(也就是上面的for循环)
result = pytesseract.image_to_string(image) #对去噪后的图片进行识别
print(result)
python /www/a.py
方案二:(用别人搭建好的)
docker pull ncino/tesseract-ocr
docker run --name i-ocr --restart=always -v E:\Python:/www -itd ncino/tesseract-ocr
tesseract 1.jpg result
pip install pytesseract
获得结果
docker exec -i i-ocr python3 /www/a.py
其他:
中文的话需要加语言库 docker exec -i i-ocr tesseract /www/1.png /www/1.txt –l chi_sim+eng
语言库地址为:https://github.com/tesseract-ocr/tessdata
如果报错wrong number of lut entries
#!usr/bin/env python
# coding:utf-8
import pytesseract
from PIL import Image
image = Image.open(r'/www/1.jpg')
image = image.convert('L') #转化为灰度图
threshold = 127 #设定的二值化阈值
#table = [] table是设定的一个表,下面的for循环可以理解为一个规则,小于阈值的,就设定为0,大于阈值的,就设定为1
#for i in range(256):
# if i < threshold:
# table.append(0)
#else:
# table.append(1)
#img = img.point(table,'1') 对灰度图进行二值化处理,按照table的规则(也就是上面的for循环)
filter_func = lambda x: 0 if x < threshold else 1
img = img.point(filter_func,'1')
result = pytesseract.image_to_string(image) #对去噪后的图片进行识别
print(result)