Linux安装Leptonica、Tesseract-OCR Python执行(识别验证码)

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)

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值