python opencv pytesseract 验证码识别的实现


Posted in Python onAugust 28, 2020

一、环境配置

需要 pillow 和 pytesseract 这两个库,pip install 安装就好了。

install pillow -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
pip install pytesseract -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

安装好Tesseract-OCR.exe

pytesseract 库的配置:搜索找到pytesseract.py,打开该.py文件,找到 tesseract_cmd,改变它的值为刚才安装 tesseract.exe 的路径。

python opencv pytesseract 验证码识别的实现

二、验证码识别

识别验证码,需要先对图像进行预处理,去除会影响识别准确度的线条或噪点,提高识别准确度。

实例1

import cv2 as cv
import pytesseract
from PIL import Image


def recognize_text(image):
  # 边缘保留滤波 去噪
  dst = cv.pyrMeanShiftFiltering(image, sp=10, sr=150)
  # 灰度图像
  gray = cv.cvtColor(dst, cv.COLOR_BGR2GRAY)
  # 二值化
  ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
  # 形态学操作  腐蚀 膨胀
  erode = cv.erode(binary, None, iterations=2)
  dilate = cv.dilate(erode, None, iterations=1)
  cv.imshow('dilate', dilate)
  # 逻辑运算 让背景为白色 字体为黑 便于识别
  cv.bitwise_not(dilate, dilate)
  cv.imshow('binary-image', dilate)
  # 识别
  test_message = Image.fromarray(dilate)
  text = pytesseract.image_to_string(test_message)
  print(f'识别结果:{text}')


src = cv.imread(r'./test/044.png')
cv.imshow('input image', src)
recognize_text(src)
cv.waitKey(0)
cv.destroyAllWindows()

运行效果如下:

识别结果:3n3D

Process finished with exit code 0

python opencv pytesseract 验证码识别的实现

实例2

import cv2 as cv
import pytesseract
from PIL import Image


def recognize_text(image):
  # 边缘保留滤波 去噪
  blur =cv.pyrMeanShiftFiltering(image, sp=8, sr=60)
  cv.imshow('dst', blur)
  # 灰度图像
  gray = cv.cvtColor(blur, cv.COLOR_BGR2GRAY)
  # 二值化
  ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
  print(f'二值化自适应阈值:{ret}')
  cv.imshow('binary', binary)
  # 形态学操作 获取结构元素 开操作
  kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 2))
  bin1 = cv.morphologyEx(binary, cv.MORPH_OPEN, kernel)
  cv.imshow('bin1', bin1)
  kernel = cv.getStructuringElement(cv.MORPH_OPEN, (2, 3))
  bin2 = cv.morphologyEx(bin1, cv.MORPH_OPEN, kernel)
  cv.imshow('bin2', bin2)
  # 逻辑运算 让背景为白色 字体为黑 便于识别
  cv.bitwise_not(bin2, bin2)
  cv.imshow('binary-image', bin2)
  # 识别
  test_message = Image.fromarray(bin2)
  text = pytesseract.image_to_string(test_message)
  print(f'识别结果:{text}')


src = cv.imread(r'./test/045.png')
cv.imshow('input image', src)
recognize_text(src)
cv.waitKey(0)
cv.destroyAllWindows()

运行效果如下:

二值化自适应阈值:181.0
识别结果:8A62N1

Process finished with exit code 0

python opencv pytesseract 验证码识别的实现

实例3

import cv2 as cv
import pytesseract
from PIL import Image


def recognize_text(image):
  # 边缘保留滤波 去噪
  blur = cv.pyrMeanShiftFiltering(image, sp=8, sr=60)
  cv.imshow('dst', blur)
  # 灰度图像
  gray = cv.cvtColor(blur, cv.COLOR_BGR2GRAY)
  # 二值化 设置阈值 自适应阈值的话 黄色的4会提取不出来
  ret, binary = cv.threshold(gray, 185, 255, cv.THRESH_BINARY_INV)
  print(f'二值化设置的阈值:{ret}')
  cv.imshow('binary', binary)
  # 逻辑运算 让背景为白色 字体为黑 便于识别
  cv.bitwise_not(binary, binary)
  cv.imshow('bg_image', binary)
  # 识别
  test_message = Image.fromarray(binary)
  text = pytesseract.image_to_string(test_message)
  print(f'识别结果:{text}')


src = cv.imread(r'./test/045.jpg')
cv.imshow('input image', src)
recognize_text(src)
cv.waitKey(0)
cv.destroyAllWindows()

运行效果如下:

二值化设置的阈值:185.0
识别结果:7364

Process finished with exit code 0

python opencv pytesseract 验证码识别的实现

到此这篇关于python opencv pytesseract 验证码识别的实现的文章就介绍到这了,更多相关opencv pytesseract 验证码识别内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
全面了解python字符串和字典
Jul 07 Python
Python网络爬虫项目:内容提取器的定义
Oct 25 Python
python实现list元素按关键字相加减的方法示例
Jun 09 Python
详谈Python基础之内置函数和递归
Jun 21 Python
浅谈python numpy中nonzero()的用法
Apr 02 Python
Python+Pyqt实现简单GUI电子时钟
Feb 22 Python
Python 日志logging模块用法简单示例
Oct 18 Python
django使用xadmin的全局配置详解
Nov 15 Python
Jupyter notebook 启动闪退问题的解决
Apr 13 Python
Python定义一个函数的方法
Jun 15 Python
如何编写python的daemon程序
Jan 07 Python
python中%格式表达式实例用法
Jun 18 Python
简单的命令查看安装的python版本号
Aug 28 #Python
python进行OpenCV实战之画图(直线、矩形、圆形)
Aug 27 #Python
python opencv实现简易画图板
Aug 27 #Python
python实现画图工具
Aug 27 #Python
20行Python代码实现一款永久免费PDF编辑工具的实现
Aug 27 #Python
基于python实现操作redis及消息队列
Aug 27 #Python
Python3如何在服务器打印资产信息
Aug 27 #Python
You might like
基于ThinkPHP实现批量删除
2015/12/18 PHP
ubutu 16.04环境下,PHP与mysql数据库,网页登录验证实例讲解
2017/07/20 PHP
javascript实现的动态添加表单元素input,button等(appendChild)
2007/11/24 Javascript
js阻止冒泡及jquery阻止事件冒泡示例介绍
2013/11/19 Javascript
checkbox全选所涉及到的知识点介绍
2013/12/31 Javascript
JS中产生20位随机数以0-9为例也可以是a-z A-Z
2014/08/01 Javascript
JavaScript数据结构和算法之二叉树详解
2015/02/11 Javascript
JS动态给对象添加事件的简单方法
2016/07/19 Javascript
js时间控件只显示年月
2017/01/08 Javascript
详解Angular5 服务端渲染实战
2018/01/04 Javascript
vue2.0 使用element-ui里的upload组件实现图片预览效果方法
2018/09/04 Javascript
React学习之JSX与react事件实例分析
2020/01/06 Javascript
vue-cli3配置favicon.ico和title的流程
2020/10/27 Javascript
python执行shell获取硬件参数写入mysql的方法
2014/12/29 Python
用Python的pandas框架操作Excel文件中的数据教程
2015/03/31 Python
Python多线程编程(八):使用Event实现线程间通信
2015/04/05 Python
Python使用MONGODB入门实例
2015/05/11 Python
Python中利用sqrt()方法进行平方根计算的教程
2015/05/15 Python
python中的字典使用分享
2016/07/31 Python
详解Python装饰器由浅入深
2016/12/09 Python
Python实现拷贝/删除文件夹的方法详解
2018/08/29 Python
Python实现查找最小的k个数示例【两种解法】
2019/01/08 Python
python多线程与多进程及其区别详解
2019/08/08 Python
爬虫代理池Python3WebSpider源代码测试过程解析
2019/12/20 Python
Python实现i人事自动打卡的示例代码
2020/01/09 Python
python实现PCA降维的示例详解
2020/02/24 Python
python 链接sqlserver 写接口实例
2020/03/11 Python
Keras 快速解决OOM超内存的问题
2020/06/11 Python
深入浅析pycharm中 Make available to all projects的含义
2020/09/15 Python
python 实现波浪滤镜特效
2020/12/02 Python
测绘工程个人的自我评价
2013/11/23 职场文书
在校硕士自我鉴定
2014/01/23 职场文书
尊老爱亲美德少年事迹材料
2014/08/14 职场文书
年终晚会活动方案
2014/08/21 职场文书
刑事上诉状(无罪)
2015/05/23 职场文书
初中班长竞选稿
2015/11/20 职场文书