前言
本文训练数据部分参考:https://blog.csdn.net/ruyulin/article/details/89046148
只能识别字母与数字比较正的图片内容,不正的基本无法识别,就算训练也是一样
这种基本能识别:
需要更精准的识别,可以使用tensorflow框架,进行模型建立及训练,但是需要学习的东西会比较多。
一、环境准备
1.安装jTessBoxEditor
官网:https://sourceforge.net/projects/vietocr/files/jTessBoxEditor/
- 下载jTessBoxEditor,我是在脚本之家下载的(网上随便找的)
- 解压jTessBoxEditor放在自己的软件目录
- 将jTessBoxEditorFX的tesseract-ocr文件夹内容清空(用于安装tesseract)
我的路径(E盘根目录下):E:\jTessBoxEditorFX
注意:
- 使用此软件需要安装JDK
- 启动文件train.bat
2.安装tesseract
官网:https://digi.bib.uni-mannheim.de/tesseract/
下载tesseract
-
安装到jTessBoxEditorFX的tesseract-ocr文件夹下
-
添加环境变量:TESSDATA_PREFIX,路径为:tesseract-ocr的tessdata文件夹
将tesseract-ocr目录加入path
3.安装pytesseract模块
pip install pytesseract
- 在Python安装目录下,找到pytesseract.py 文件
- 修改pytesseract.py文件的第二十七行tesseract_cmd的值为实际安装路径:
注意:需要使用r转为原始字符串,或者使用双\
若未安装tesseract,在Python中使用时会出现如下错误信息提示:
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your PATH. See README file for more information.
二、准备数据
(一)获取数据
以古诗文网的登录页面为例,获取验证码数据:
from PIL import Image
import pytesseract
import re
from selenium import webdriver
driver=webdriver.Chrome()
driver.maximize_window()
driver.get('https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx')
for i in range(10):
#注意拼接路径,前面的点号 . 表示相对当前路径
driver.find_element_by_xpath('//*[@id="imgCode"]').screenshot('.\\verification_code\\' + 'code' + str(i) + '.png')
driver.refresh()
结果图片:
(二)处理数据
三、训练数据
1.训练数据
打开 jTessBoxEditor(启动文件为train.bat),合并图片产生一个tif文件
产生一个box文件用于训练
tesseract VeriCode.font.exp1.tif VeriCode.font.exp1 batch.nochop makebox人工检查不能识别的数据,进行修正。(具体如何训练查看其他文章)
产生一个tr尾缀文件
tesseract VeriCode.font.exp1.tif VeriCode.font.exp1 nobatch box.train新建一个名为font_properties的文件(注意:不需要尾缀),内容为:
font 0 0 0 0 0
生成一个unicharset文件
unicharset_extractor VeriCode.font.exp1.box生成一个shapetable文件
shapeclustering -F font_properties -U unicharset VeriCode.font.exp1.tr生成字符特征文件(得到inttemp,pffmtable)
mftraining -F font_properties -U unicharset -O unicharset VeriCode.font.exp1.tr合并处理后的tr尾缀文件(得到normproto)
cntraining VeriCode.font.exp1.tr
注意:合并多个直接后面加空格再加文件名就行修改文件名
rename normproto VeriCode.normproto
rename unicharset VeriCode.unicharset
rename inttemp VeriCode.inttemp
rename pffmtable VeriCode.pffmtable
rename shapetable VeriCode.shapetablecombine_tessdata VeriCode.(得到训练结果:VeriCode.traineddata文件)
注意VeriCode后面有个点号
2.迁移训练结果
复制VeriCode.traineddata,放到tessdata(Tesseract安装目录的一个文件夹)文件夹下
3.训练数据注意事项
- 生成tif尾缀文件时,严格按照name.fontname.expNum命名,否则会报加载字体错误
- 不要漏掉重命名文件的步骤
- 如果自己把步骤加入bat文件中自动执行,需要确定文件路劲与文件名是否正确
4.如何训练数据
4.1合并出tif尾缀文件,用于产生box文件(训练用)
tesseract VeriCode.font.exp1.tif VeriCode.font.exp1 batch.nochop makebox
4.1产生box文件用于训练
- 打开cmd,切换到存放tr尾缀文件的目录
- 执行tesseract VeriCode.font.exp1.tif VeriCode.font.exp1 batch.nochop makebox
执行指令图:
结果图
4.3 打开tr尾缀文件
4.4训练详细步骤
4.4.1修改char值
双击需要修改值的char,修改之后需要回车
4.4.2修改单个char的坐标与参数
- 鼠标选中需要修改的char行
- 点击character的>>
- 鼠标选中需要修改的参数,上下方向键修改(鼠标点击变化一个数据就消失了)
4.4.3预览修改数据
- 红色方框内的小绿色框就是设置的字符宽度,可以用于查看宽度是否合适
-
点击不同数字,可以切换到不同的字符view页
4.4.4字符的合并、分割、插入、删除
例如,该D字符,宽度很宽,占据多个字符位置,可以使用分割split,分割成多个再删除不需要的,或者修改宽度,再调整坐标
四、Python中使用
前面步骤全部完成,我们就可以使用pytesseract进行图片识别了
from PIL import Image
import pytesseract
import re
from selenium import webdriver
#下面两个函数用于处理图片,进行灰度处理,与降噪
def clear_image(image):
image = image.convert('RGB')
width = image.size[0]
height = image.size[1]
noise_color = get_noise_color(image)
for x in range(width):
for y in range(height):
# 清除边框和干扰色
rgb = image.getpixel((x, y))
if (x == 0 or y == 0 or x == width - 1 or y == height - 1
or rgb == noise_color or rgb[1] > 100):
image.putpixel((x, y), (255, 255, 255))
return image
def get_noise_color(image):
for y in range(1, image.size[1] - 1):
# 获取第2列非白的颜色
(r, g, b) = image.getpixel((2, y))
if r < 255 and g < 255 and b < 255:
return (r, g, b)
driver=webdriver.Chrome()
driver.maximize_window()
driver.get('https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx')
driver.find_element_by_xpath('//*[@id="imgCode"]').screenshot('verification_code.png')
image = Image.open('verification_code.png')
image = clear_image(image)
# 转化为灰度图
imgry = image.convert('L')
imgry.save('verification_code_grey.png')
print('未使用训练数据源处理之前:')
code=pytesseract.image_to_string('verification_code_grey.png')
print(code)
print('使用训练数据源处理之后:')
text=pytesseract.image_to_string('verification_code_grey.png',lang='VeriCode')
print('使用训练数据源处理之后:未使用正则处理')
print(text)
#正则表达式提取
print('使用训练数据源处理之后:使用正则处理')
result=re.findall(r'[\dA-Za-z]{4}',text)
print(result)
2.使用注意
若未安装:tesseract,则会报错
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your PATH. See README file for more information.