• 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 文件 内容如下

    1. rem 执行改批处理前先要目录下创建font_properties文件
    2. echo Run Tesseract for Training..
    3. tesseract.exe jtbnum.font.exp0.tif jtbnum.font.exp0 nobatch box.train
    4. echo Compute the Character Set..
    5. unicharset_extractor.exe jtbnum.font.exp0.box
    6. mftraining -F font_properties -U unicharset -O jtbnum.unicharset jtbnum.font.exp0.tr
    7. echo Clustering..
    8. cntraining.exe jtbnum.font.exp0.tr
    9. echo Rename Files..
    10. del jtbnum.normproto
    11. rename normproto jtbnum.normproto
    12. del jtbnum.inttemp
    13. rename inttemp jtbnum.inttemp
    14. del jtbnum.pffmtable
    15. rename pffmtable jtbnum.pffmtable
    16. del jtbnum.shapetable
    17. rename shapetable jtbnum.shapetable
    18. echo Create Tessdata..
    19. combine_tessdata.exe jtbnum.
    20. pause

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

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

    5)测试识别率:

     识别的图片内容为:

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

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

    五、通过Opencv清除图片的多余杂质(Java实现)

    1. if(!hasLoad){
    2. System.load(opencvPath+"/build/java/x64/opencv_java440.dll");
    3. hasLoad = true;
    4. }
    5. byte [] bytes = Base64Utils.decodeFromString(base64);
    6. String path = savePath+"/"+System.currentTimeMillis()+".png";
    7. try {
    8. OutputStream outputStream = new FileOutputStream(new File(path));
    9. outputStream.write(bytes);
    10. outputStream.flush();
    11. outputStream.close();
    12. } catch (Exception e) {
    13. e.printStackTrace();
    14. }
    15. Mat image0 = Imgcodecs.imread(path);
    16. Mat image1 = new Mat();
    17. //灰度处理
    18. Imgproc.cvtColor(image0, image1, Imgproc.COLOR_BGR2GRAY);
    19. Imgproc.adaptiveThreshold(image1,image1,255,Imgproc.ADAPTIVE_THRESH_MEAN_C,Imgproc.THRESH_BINARY,11, 2);
    20. Core.bitwise_not(image1,image1);
    21. Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2, 2), new Point(-1, -1));
    22. Mat temp = new Mat();
    23. Imgproc.erode(image1, temp, kernel);
    24. Imgproc.dilate(temp, temp, kernel);
    25. String newPath = path.substring(0,path.lastIndexOf(".")) +"_1.png";
    26. Imgcodecs.imwrite(newPath,temp);

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

    5)测试识别率:

     识别的图片内容为:

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

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

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

  • 相关阅读:
    SpringBoot 自定义注解 + SpringBoot Aop 实现切面日志处理
    p19.matplotlib:Animation 动画
    bash 条件语句
    电子统计台账:设置能自动合并数据的垂直过滤模板
    Spark SQL函数
    简单的博客系统(CSS+HTML)
    卡方分布的构造和画图
    51单片机晶振频率与定时中断产生pwn占空比
    笔试题积累
    如何破解安全难题?另一个商用车ADAS市场窗口开启
  • 原文地址:https://blog.csdn.net/peng_wei_kang/article/details/125482305