首页 > 其他分享 >opencv SVM 训练ocr模型

opencv SVM 训练ocr模型

时间:2023-09-14 12:11:45浏览次数:45  
标签:svm mat jpg opencv SVM train imgs ocr

实现0-6字符分类

数据准备: 训练数据:

train_data.txt 

查看代码
 D:/ocr/svm/train/imgs/0/0.png
0
D:/ocr/svm/train/imgs/0/0_1.jpg
0
D:/ocr/svm/train/imgs/1/1.png
1
D:/ocr/svm/train/imgs/1/1_1.jpg
1
D:/ocr/svm/train/imgs/1/1_2.jpg
1
D:/ocr/svm/train/imgs/1/1_3.jpg
1
D:/ocr/svm/train/imgs/2/2.png
2
D:/ocr/svm/train/imgs/2/2_1.jpg
2
D:/ocr/svm/train/imgs/2/2_2.jpg
2
D:/ocr/svm/train/imgs/3/3.png
3
D:/ocr/svm/train/imgs/3/3_1.jpg
3
D:/ocr/svm/train/imgs/3/3_2.jpg
3
D:/ocr/svm/train/imgs/4/4.png
4
D:/ocr/svm/train/imgs/4/4_1.jpg
4
D:/ocr/svm/train/imgs/4/4_2.jpg
4
D:/ocr/svm/train/imgs/5/5.png
5
D:/ocr/svm/train/imgs/5/5_1.jpg
5
D:/ocr/svm/train/imgs/5/5_2.jpg
5
D:/ocr/svm/train/imgs/6/6.png
6
D:/ocr/svm/train/imgs/6/6_1.jpg
6
D:/ocr/svm/train/imgs/6/6_2.jpg
6

数据处理及训练:

查看代码
 #include <iostream>
#include <opencv2/ml/ml.hpp>
#include <opencv2/objdetect/objdetect.hpp>

void OcrTrain()
{
    using namespace cv::ml;
    using namespace std;
    vector<string> imgpath;  // path of train image
    vector<int> imglabel;    // label of train image
    int nLine = 0;
    string buf;
    ifstream svm_data;
    svm_data.open("d:/ocr/svm/train/train_data.txt", ios::in);
    if (!svm_data.is_open())
    {
        cout << "read error" << endl;
        exit(EXIT_FAILURE);
    }
    unsigned long n;
    while (svm_data)
    {
        if (getline(svm_data, buf))
        {
            nLine++;
            if (nLine % 2 == 0)
            {
                imglabel.push_back(atoi(buf.c_str()));
            }
            else
            {
                imgpath.push_back(buf);
            }
        }
    }
    svm_data.close();    //close file
    /// <summary>
    /// 训练
    /// </summary>

    Mat data_mat, res_mat;
    int nImgNum = nLine / 2;
    data_mat = Mat::zeros(nImgNum, 324, CV_32FC1); // store hog feature 324=9*4*9 single channel float
    res_mat = Mat::zeros(nImgNum, 1, CV_32S);    // store label 注意这里的数据类型32F不行
    Mat src;
    for (string::size_type i = 0; i != imgpath.size(); i++)
    {
        src = imread(imgpath[i], 1);  // read train image
        if (src.empty())
        {
            cout << "can not read the image: " << imgpath[i] << endl;
            continue;
        }
        cout << "processing:" << endl;
        Mat trainImg;
        resize(src, trainImg, Size(28, 28)); // resize to 28*28
        HOGDescriptor* hog = new HOGDescriptor(Size(28, 28), Size(14, 14), Size(7, 7), Size(7, 7), 9); // hog descriptor
        vector<float> descriptors; // store result
        hog->compute(trainImg, descriptors, Size(1, 1), Size(0, 0)); // compute hog descriptor
        cout << "HOG dims:";
        n = 0;
        for (vector<float>::iterator iter = descriptors.begin(); iter != descriptors.end(); iter++)
        {
            data_mat.at<float>(i, n) = (*iter); // put hog descriptor into data_mat
            n++;
        }
        res_mat.at<int>(i, 0) = imglabel[i]; // put label into res_mat
        cout << "processing done:" << " " << endl;
    }
    Ptr<cv::ml::SVM> svm = SVM::create();//创建一个svm对象
    svm->setType(cv::ml::SVM::C_SVC);
    svm->setKernel(SVM::LINEAR);
    svm->setDegree(0);
    svm->setGamma(1);
    svm->setCoef0(0);
    svm->setC(1);
    svm->setNu(0);
    svm->setP(0);
    svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 1000, TermCriteria::EPS));//设置SVM训练时迭代终止条件  10的12次方
    //训练
    cout << "开始进行训练..." << endl;
    Ptr<TrainData> tData = TrainData::create(data_mat, ROW_SAMPLE, res_mat);
    //svm->train(tData);  //这两行代码和下面一行代码等效
    svm->train(data_mat, cv::ml::SampleTypes::ROW_SAMPLE, res_mat);
    Mat resp;
    float err = svm->calcError(tData, false, resp);
    //CvSVM svm;
    //CvSVMParams param;
    //param = CvSVMParams(CvSVM::C_SVC, CvSVM::RBF, 10.0, 0.09, 1.0, 10.0, 0.5, 1.0, NULL, criteria); // svm parameter
    //svm->train(data_mat, res_mat, Mat(), Mat(), param); //train
    svm->save("D:/ocr/svm/HOG_SVM_OCR.xml"); // preserve result
}

加载模型预测:

查看代码
   Mat test;
    char result[512];
    vector<string> img_tst_path;
    ifstream img_tst("D:/ocr/svm/test_data.txt");
    string test_dir = "D:/ocr/svm/test/";
    while (img_tst)
    {
        if (getline(img_tst, buf))
        {
            buf = test_dir + buf;
            img_tst_path.push_back(buf);
        }
    }
    img_tst.close();
    // 预测阶段
    Ptr<cv::ml::SVM> svmLoad = StatModel::load<SVM>("D:/ocr/svm/HOG_SVM_OCR.xml");
    ofstream predict_txt("D:/ocr/svm/SVM_PREDICT1.txt");
    for (string::size_type j = 0; j != img_tst_path.size(); j++)
    {
        test = imread(img_tst_path[j], 1);
        if (test.empty())
        {
            cout << "can not load the image:" << endl;
            continue;
        }
        Mat trainTempImg;
        resize(test, trainTempImg, Size(28, 28));
        HOGDescriptor* hog = new HOGDescriptor(Size(28, 28), Size(14, 14), Size(7, 7), Size(7, 7), 9);
        vector<float> descriptors;
        hog->compute(trainTempImg, descriptors, Size(1, 1), Size(0, 0));
        cout << "HOG dims:" << endl;
        Mat SVMtrainMat(1, descriptors.size(), CV_32FC1);
        int n = 0;
        for (vector<float>::iterator iter = descriptors.begin(); iter != descriptors.end(); iter++)
        {
            SVMtrainMat.at<float>(0, n) = (*iter);
            n++;
        }
        int ret = svmLoad->predict(SVMtrainMat); // predict by svm
        sprintf_s(result, "%s %d\r\n", img_tst_path[j], ret);
        cout << img_tst_path[j]<<"   " <<ret << endl;
        predict_txt << result;//predict result
    }
    predict_txt.close();

结果输出:

标签:svm,mat,jpg,opencv,SVM,train,imgs,ocr
From: https://www.cnblogs.com/hakula/p/17702189.html

相关文章

  • 【lssvm回归预测】基于变模态结合秃鹰算法优化最小二乘支持向量机VMD-BES-LSSVM实现数
    ✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。......
  • 19C RAC更换OCR磁盘组后,ASM密码认证导致集群CRSD服务无法启动
    前言一套19.19的RAC,OCR所在的ASM磁盘组从+GRID更换为+DG_GRID,然后强制删除了原来的+GRID磁盘组,最终导致该集群无法启动。 过程1、启动过程中,CSS服务正常启动,但CRS服务无法启动。此时,节点2的alertasm2.log日志中提示如下错误。2023-06-23T17:44:33.667188+08:00Errorsi......
  • opencv resize
    opencvresize importcv2fromccvimportccv2#读取图像image=cv2.imread("example.jpg")#调整图像大小resized_image=ccv2.resize(image,300,300,ccv2.INTER_LINEAR)#显示调整大小后的图像cv2.imshow("ResizedImage",resized_image)cv2.waitKey(0......
  • [转]Windows10+VS2013+OpenCV3.0开发环境搭建配置
    首先要说明一下:不管VS(VisualStudio)的版本是什么,也不管OpenCV的版本是什么,配置都大同小异。所以如果您希望在Window下配置OpenCV的开发环境,那么仔细阅读学习这篇文章即可。之前在介绍OpenCV的组件模块时,我是以OpenCV4来说明的,详情见链接 https://www.hhai.cc/thread-19-1-1.htm......
  • C#结合OpenCVSharp4使用直方图算法比较图片相似度
    C#结合OpenCVSharp4使用直方图算法比较图片相似度直方图有灰度直方图、颜色直方图,如果是灰度图像,那么就用灰度直方图,这里使用颜色直方图来计算两个图片的相似度。这里只记录如何使用,至于算法原理,问就是不会。直方图算法效率高,但精度不够,适合快速比较,例如以图搜图1.下载O......
  • C#结合OpenCVSharp4图片相似度识别
    OpenCVSharp4图片相似度识别需求背景:需要计算两个图片的相似度,然后将相似的图片进行归纳1.图片相似度算法由于我是CRUD后端仔,对图像处理没什么概念。因此网上调研了几种相似度算法分析其适用场景。直方图算法获取要比较的2个图片的直方图数据,然后再将直方图数据归一化比较,最......
  • selenium 解析验证码(普通的字符数字的验证码),解决方式:先将验证码保存为图片,然后使用ddd
     fromseleniumimportwebdriverfromselenium.webdriver.common.byimportByfromPILimportImagefromioimportBytesIOimportpytesseractimporttimeimportddddocrfromselenium.webdriver.supportimportexpected_conditionsfromselenium.webdriver.sup......
  • cv2.error: Unknown C++ exception from OpenCV code.报错解决
    问题原因:opencv版本太高,python版本太低解决办法:打开opencv下载网站找到自己的python版本对应的opencv的版本,然后通过下面代码安装pipinstallopencv-python==4.1.2.30-ihttps://pypi.tuna.tsinghua.edu.cn/simple ......
  • ubuntu20安装opencv4.5.5
     https://github.com/opencv/opencv/releases/tag/4.5.5  https://github.com/opencv/opencv_contrib/releases/tag/4.5.5 ......
  • 简化车辆登记流程:利用腾讯云OCR实现自动化信息识别
     项目中有一块,需要用到上传车牌车牌号到系统里,用了下腾讯云的ocr车牌号识别做了个小功能。通过腾讯云的orc识别,将车牌号录入到后台。 一,首先我们需要登录到腾讯云,然后搜索一下orc或者车牌号https://curl.qcloud.com/dPX3dJRL   或者直接访问这个链接 二,进入后就是......