python3安装OCR识别库tesserocr过程图解

作者:广州萤火虫 时间:2021-12-08 04:50:08 

OCR简介

OCR,即Optical Character Recognition,光学字符识别,是指通过扫描字符,然后通过其形状将其翻译成电子文本的过程,对应图形验证码来说,它们都是一些不规则的字符,这些字符是由字符稍加扭曲变换得到的内容,我们可以使用OCR技术来讲其转化为电子文本,然后将结果提取交给服务器,便可以达到自动识别验证码的过程。

window环境

环境材料准备

  • Window10

  • Python-3.7.3.tgz

  • tesserocr安装包

安装tesserocr

1、打开链接,https://digi.bib.uni-mannheim.de/tesseract/,见下图。

python3安装OCR识别库tesserocr过程图解

下载最新版的tesseract-ocr-w64-setup-v5.0.0.20190623.exe,然后安装,本人直接安装在C盘目录下。安装完毕后,如下图。

python3安装OCR识别库tesserocr过程图解

配置环境变量,有两个步骤。

在系统变量里,修改path,如下图。

python3安装OCR识别库tesserocr过程图解

在系统变量里,创建一个新的变量名为:TESSDATA_PREFIX,值为:C:\Program Files\Tesseract-OCR\tessdata(根据自己安装的tesserocr安装路径为准),如下图。

python3安装OCR识别库tesserocr过程图解

检查Tesseract-OCR是否安装完成,如下图。

python3安装OCR识别库tesserocr过程图解

Python3.7加载tesserocr

1、安装Python的OCR识别库

pip install Pillow
pip install pytesseract

2、python加载Window的tesserocr应用,要修改pytesseract三方库的pytesseract.py脚本。
python3安装OCR识别库tesserocr过程图解

打开pytesseract.py,将Window的tesserocr应用的tesserocr.exe绑定好。

python3安装OCR识别库tesserocr过程图解

3、到这里Python的绑定window的tesserocr应用已经完成。

读取验证码图片

python3安装OCR识别库tesserocr过程图解


from PIL import Image
import pytesseract

def read_text(text_path):
 """
 传入文本(jpg、png)的绝对路径,读取文本
 :param text_path:
 :return: 文本内容
 """
 # 验证码图片转字符串
 im = Image.open(text_path)
 # 转化为8bit的黑白图片
 imgry = im.convert('L')
 # 二值化,采用阈值分割算法,threshold为分割点
 threshold = 140
 table = []
 for j in range(256):
   if j < threshold:
     table.append(0)
   else:
     table.append(1)
 out = imgry.point(table, '1')
 # 识别文本
 text = pytesseract.image_to_string(out, lang="eng", config='--psm 6')
 return text

if __name__ == '__main__':
 print(read_text("d://v3.png"))

输出:

python3安装OCR识别库tesserocr过程图解

读取中文文本图片

1、因为OCR读取不同语言需要加载语言包,因此需要下载简体中文语言包。
从这个链接下载:https://github.com/tesseract-ocr/tessdata,下载红圈的简体中文包。然后将此文件放置window的安装目录下。如下两个图。
python3安装OCR识别库tesserocr过程图解
python3安装OCR识别库tesserocr过程图解

现在,我们来读取如下图片的中文文本内容。

python3安装OCR识别库tesserocr过程图解

代码如下:


from PIL import Image
import pytesseract

def read_text(text_path):
 """
 传入文本(jpg、png)的绝对路径,读取文本
 :param text_path:
 :return: 文本内容
 """
 # 验证码图片转字符串
 im = Image.open(text_path)
 # 转化为8bit的黑白图片
 imgry = im.convert('L')
 # 二值化,采用阈值分割算法,threshold为分割点
 threshold = 140
 table = []
 for j in range(256):
   if j < threshold:
     table.append(0)
   else:
     table.append(1)
 out = imgry.point(table, '1')
 # 识别文本,lang参数改为chi_sim,其他代码与上面的读取验证码代码一致。
 text = pytesseract.image_to_string(out, lang="chi_sim", config='--psm 6')
 return text
if __name__ == '__main__':
 print(read_text("d://v7.png"))

python3安装OCR识别库tesserocr过程图解

来源:https://blog.csdn.net/lanxianghua/article/details/100516187?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

标签:python,安装,OCR,tesserocr
0
投稿

猜你喜欢

  • 学习ASP.NET八天入门:第三天

    2007-08-07 13:30:00
  • 一文带你搞懂Golang依赖注入的设计与实现

    2023-07-23 14:33:53
  • Python Flask RESTful使用demo演示

    2023-08-04 07:02:23
  • Python中SOAP项目的介绍及其在web开发中的应用

    2022-09-15 15:36:45
  • pandas分批读取大数据集教程

    2023-01-13 16:45:32
  • 数据库名词解释

    2008-09-12 17:28:00
  • Python图像读写方法对比

    2022-10-07 08:13:46
  • 浅谈pandas中shift和diff函数关系

    2022-11-15 08:48:42
  • python调试模式无响应解决案例

    2021-03-27 00:23:18
  • Perl初学笔记之Hello World

    2022-05-21 10:28:55
  • PL/SQL中编写Oracle数据库分页的存储过程

    2024-01-16 08:50:24
  • 检测SqlServer数据库是否能连接的小技巧

    2024-01-21 04:01:28
  • python实现批量修改文件名代码

    2023-05-04 14:44:41
  • PHP getNamespaces()函数讲解

    2023-06-11 07:32:45
  • python读取并显示图片的三种方法(opencv、matplotlib、PIL库)

    2023-03-24 19:43:02
  • 用Python编写生成树状结构的文件目录的脚本的教程

    2022-12-26 09:54:52
  • MySQL字段类型详解

    2009-01-05 09:23:00
  • PHP检查端口是否可以被绑定的方法示例

    2024-05-13 09:26:32
  • JS性能优化笔记搜索整理

    2024-04-17 09:54:55
  • Vue实现自定义视频和图片上传的示例代码

    2024-05-21 10:30:21
  • asp之家 网络编程 m.aspxhome.com