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       识别已经很精确

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

  • 4
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,我可以帮助您用 Java 使用 OpenCVTesseract 识别验证码文字并标注文字坐标。 首先,您需要安装 OpenCVTesseract 的 Java 绑定,并将它们添加到您的项目中。然后,您可以使用 OpenCV 读入图像并使用 Tesseract 识别文字。 下面是一个基本的示例代码,它可以帮助您开始使用 OpenCVTesseract 进行文字识别: ``` import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfRect; import org.opencv.core.Point; import org.opencv.core.Rect; import org.opencv.core.Scalar; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; import org.opencv.objdetect.CascadeClassifier; import com.google.common.io.Files; import net.sourceforge.tess4j.ITesseract; import net.sourceforge.tess4j.Tesseract; import net.sourceforge.tess4j.TesseractException; import java.io.File; import java.io.IOException; public class TextRecognition { public static void main(String[] args) throws IOException, TesseractException { // Load the OpenCV library System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // Load the image file Mat image = Imgcodecs.imread("captcha.png"); // Convert the image to grayscale Mat imageGray = new Mat(); Imgproc.cvtColor(image, imageGray, Imgproc.COLOR_BGR2GRAY); // Use a cascade classifier to detect the characters in the image CascadeClassifier classifier = new CascadeClassifier("char_classifier.xml"); MatOfRect characterRegions = new MatOfRect(); classifier.detectMultiScale(imageGray, characterRegions); // Loop through the character regions and recognize the text in each region ITesseract tesseract = new Tesseract(); tesseract.setLanguage("eng"); for (Rect rect : characterRegions.toArray()) { // Crop the character region from the image Mat characterRegion = new Mat(imageGray, rect); // Perform OTSU thresholding on the character region Mat characterRegionThreshold = new Mat(); Imgproc.th

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值