centos7下tesserocr安装使用及python识别验证码

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)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值