栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

opencv+tesseract完成验证码识别(识别率99.99%)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

opencv+tesseract完成验证码识别(识别率99.99%)

一、需要识别的内容

需要识别的验证码内容如下  验证码下载下载地址。

二、直接调用tesseract来完成识别(识别率很差)

识别的图片内容为:

在window系统钟打开cmd命令窗口,执行识别命令如下:

tesseract.exe 01.png output.txt -l eng

识别结果为:519}       该识别准确率远远达不到预期

三、训练数据样本,提升识别率

1、下载10份样本(样本数量越多,识别率越高),然后通过jTessBoxEditor来进行样本数据矫正(该步骤耗时较长)。

 2、打开 jTessBoxEditor,将所有的样本数据生成一个总的tif文件(tif就是所有图片的集合)。操作如下:

1)jTessBoxEditor->Tools->Merge TIFF

2 )全选所有的样本文件,之后生成的tif命名为 jtbnum.font.exp0.tif

3)进行数据识别调整,如下图:

 

 

 四、生成样本库字体

将所有的样本识别内容都调整正确后(调整的参数保存在jtbnum.font.exp0.box文件钟),我们需要将我们生成的样本文件封装成我们的 jtbnum.traineddata 字体库,生成方式如下:

1)创建 font_properties 文件,内容为 font 0 0 0 0 0

2)在同级目录创建 run.bat 文件 内容如下

rem 执行改批处理前先要目录下创建font_properties文件  
  
echo Run Tesseract for Training..  
tesseract.exe jtbnum.font.exp0.tif jtbnum.font.exp0 nobatch box.train  
  
echo Compute the Character Set..  
unicharset_extractor.exe jtbnum.font.exp0.box  
mftraining -F font_properties -U unicharset -O jtbnum.unicharset jtbnum.font.exp0.tr  
  
echo Clustering..  
cntraining.exe jtbnum.font.exp0.tr  
  
echo Rename Files..  

del jtbnum.normproto
rename normproto jtbnum.normproto

del jtbnum.inttemp
rename inttemp jtbnum.inttemp

del jtbnum.pffmtable
rename pffmtable jtbnum.pffmtable

del jtbnum.shapetable
rename shapetable jtbnum.shapetable
  
echo Create Tessdata..  
combine_tessdata.exe jtbnum. 

pause

 3)双击执行 run.bat 文件,系统执行完成后,将会生成 jtbnum.traineddata 文件。

4)将 jtbnum.traineddata 拷贝到tesseract安装目录下的tessdata文件夹下。

5)测试识别率:

 识别的图片内容为:

tesseract.exe 01.png output.txt -l jtbnum

 识别结果为:51915       识别结果已经很准确率,但是验证码图片中的杂质没有清除,导致会识别出多余内容来。

五、通过Opencv清除图片的多余杂质(Java实现)
if(!hasLoad){
            System.load(opencvPath+"/build/java/x64/opencv_java440.dll");
            hasLoad = true;
        }

        byte [] bytes = Base64Utils.decodeFromString(base64);
        String path = savePath+"/"+System.currentTimeMillis()+".png";
        try {
            OutputStream outputStream = new FileOutputStream(new File(path));
            outputStream.write(bytes);
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        Mat image0 = Imgcodecs.imread(path);
        Mat image1 = new Mat();
        //灰度处理
        Imgproc.cvtColor(image0, image1, Imgproc.COLOR_BGR2GRAY);
        Imgproc.adaptiveThreshold(image1,image1,255,Imgproc.ADAPTIVE_THRESH_MEAN_C,Imgproc.THRESH_BINARY,11, 2);
        Core.bitwise_not(image1,image1);
        Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2, 2), new Point(-1, -1));
        Mat temp = new Mat();
        Imgproc.erode(image1, temp, kernel);
        Imgproc.dilate(temp, temp, kernel);
        String newPath = path.substring(0,path.lastIndexOf(".")) +"_1.png";
        Imgcodecs.imwrite(newPath,temp);

图片处理结果如下(杂质已经清除):

5)测试识别率:

 识别的图片内容为:

tesseract.exe 01.png output.txt -l jtbnum

 识别结果为:5191       识别已经很精确

 后期会附上各软件版本信息以及详细代码

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/994104.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号