SVM model training and classification return error
ZJDATY opened this issue · 2 comments
System Information
OpenCV version: 4.10.0
Operating System:win10
Compiler & compiler version: VS2022
Detailed description
An exception was raised: read access permission conflict.
SVM. * * Ptr * * is nullptr.
Steps to reproduce
#include <stdio.h>
#include <time.h>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ml/ml.hpp>
#include <io.h>
#include <vector>
#include <string>
#include <filesystem>
using namespace std;
using namespace cv;
namespace fs = std::filesystem;
void getFiles(std::string& path, vector<string>& files) {
for (const auto& entry : fs::directory_iterator(path)) {
if (entry.is_regular_file()) {
files.push_back(entry.path().string());
}
}
}
int main()
{
int result = 0;
string filePath = "D:\\vcworkspace\\digits_svm\\digits_svm\\data\\test_image\\0";
vector<string> files;
getFiles(filePath, files);
int number = files.size();
cout << number << endl;
Ptr<ml::SVM> svm;
string modelpath = "digits_svm.yml";
vector<float> predictions;
FileStorage svm_fs(modelpath, FileStorage::READ);
if (svm_fs.isOpened())
{
svm->load(modelpath.c_str());
}
for (int i = 0; i < number; i++)
{
Mat inMat = imread(files[i].c_str());
Mat p = inMat.reshape(1, 1);
p.convertTo(p, CV_32FC1);
int response = (int)svm->predict(p);
if (response == 0)
{
result++;
}
}
cout << result << endl;
getchar();
return 0;
}
All the projects are here.
https://github.com/ZJDATY/digits_svm/tree/master
Issue submission checklist
- I report the issue, it's not a question
- I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
- I updated to the latest OpenCV version and the issue is still there
- There is reproducer code and related data files (videos, images, onnx, etc)
Hello. if you have "question", please could you ask at QA forum ? https://forum.opencv.org/
In your code, return value of load function is not used. Please could you confirm how to use ?
https://docs.opencv.org/4.10.0/d1/d2d/classcv_1_1ml_1_1SVM.html#af82a9714aecd48d61d88f0ce6f6035ed
This is sample code to load SVM model. model
is Ptr<StatModel>
, which is super class of Ptr<SVM>
.
opencv/modules/ml/test/test_save_load.cpp
Lines 63 to 64 in 71d3237
So I believe your code should be updated with following.
- svm->load(modelpath.c_str());
+ svm = SVM::load(modelpath.c_str());
(because svm
is not created, it is better to not calling svm->load()
)
Hello. if you have "question", please could you ask at QA forum ? https://forum.opencv.org/
In your code, return value of load function is not used. Please could you confirm how to use ?
https://docs.opencv.org/4.10.0/d1/d2d/classcv_1_1ml_1_1SVM.html#af82a9714aecd48d61d88f0ce6f6035ed
This is sample code to load SVM model. is , which is super class of .
model``Ptr<StatModel>``Ptr<SVM>
opencv/modules/ml/test/test_save_load.cpp
Lines 63 to 64 in 71d3237
So I believe your code should be updated with following.
- svm->load(modelpath.c_str()); + svm = SVM::load(modelpath.c_str());(because is not created, it is better to not calling
svm``svm->load()
)
thank you