|
|
|
@ -12,16 +12,17 @@ namespace easypr{
|
|
|
|
|
void learn2HasPlate(float bound = 0.7)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
char * filePath = "train/data/plate_detect_svm/learn/HasPlate";
|
|
|
|
|
const char * filePath = "train/data/plate_detect_svm/learn/HasPlate";
|
|
|
|
|
vector<string> files;
|
|
|
|
|
|
|
|
|
|
////获取该路径下的所有文件
|
|
|
|
|
getFiles(filePath, files );
|
|
|
|
|
|
|
|
|
|
int size = files.size();
|
|
|
|
|
if (0 == size)
|
|
|
|
|
cout << "No File Found in learn HasPlate!" << endl;
|
|
|
|
|
|
|
|
|
|
if (0 == size) {
|
|
|
|
|
cout << "File not found in " << filePath << endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
////随机选取70%作为训练数据,30%作为测试数据
|
|
|
|
|
srand(unsigned(time(NULL)));
|
|
|
|
|
random_shuffle(files.begin(), files.end());
|
|
|
|
@ -58,15 +59,16 @@ void learn2HasPlate(float bound = 0.7)
|
|
|
|
|
void learn2NoPlate(float bound = 0.7)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
char * filePath = "train/data/plate_detect_svm/learn/NoPlate";
|
|
|
|
|
const char * filePath = "train/data/plate_detect_svm/learn/NoPlate";
|
|
|
|
|
vector<string> files;
|
|
|
|
|
|
|
|
|
|
////获取该路径下的所有文件
|
|
|
|
|
getFiles(filePath, files );
|
|
|
|
|
int size = files.size();
|
|
|
|
|
if (0 == size)
|
|
|
|
|
cout << "No File Found in learn NoPlate!" << endl;
|
|
|
|
|
|
|
|
|
|
if (0 == size) {
|
|
|
|
|
cout << "File not found in " << filePath << endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
////随机选取70%作为训练数据,30%作为测试数据
|
|
|
|
|
srand(unsigned(time(NULL)));
|
|
|
|
|
random_shuffle(files.begin(), files.end());
|
|
|
|
@ -104,16 +106,17 @@ void getHasPlateTrain(Mat& trainingImages, vector<int>& trainingLabels,
|
|
|
|
|
svmCallback getFeatures = getHisteqFeatures)
|
|
|
|
|
{
|
|
|
|
|
int label = 1;
|
|
|
|
|
char * filePath = "train/data/plate_detect_svm/train/HasPlate";
|
|
|
|
|
const char * filePath = "train/data/plate_detect_svm/train/HasPlate";
|
|
|
|
|
vector<string> files;
|
|
|
|
|
|
|
|
|
|
////获取该路径下的所有文件
|
|
|
|
|
getFiles(filePath, files );
|
|
|
|
|
|
|
|
|
|
int size = files.size();
|
|
|
|
|
if (0 == size)
|
|
|
|
|
cout << "No File Found in train HasPlate!" << endl;
|
|
|
|
|
|
|
|
|
|
if (0 == size) {
|
|
|
|
|
cout << "File not found in " << filePath << endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
cout << "get HasPlate train!" << endl;
|
|
|
|
|
for (int i = 0; i < size; i++)
|
|
|
|
|
{
|
|
|
|
@ -135,16 +138,17 @@ void getNoPlateTrain(Mat& trainingImages, vector<int>& trainingLabels,
|
|
|
|
|
svmCallback getFeatures = getHisteqFeatures)
|
|
|
|
|
{
|
|
|
|
|
int label = 0;
|
|
|
|
|
char * filePath = "train/data/plate_detect_svm/train/NoPlate";
|
|
|
|
|
const char * filePath = "train/data/plate_detect_svm/train/NoPlate";
|
|
|
|
|
vector<string> files;
|
|
|
|
|
|
|
|
|
|
////获取该路径下的所有文件
|
|
|
|
|
getFiles(filePath, files );
|
|
|
|
|
|
|
|
|
|
int size = files.size();
|
|
|
|
|
if (0 == size)
|
|
|
|
|
cout << "No File Found in train HasPlate!" << endl;
|
|
|
|
|
|
|
|
|
|
if (0 == size) {
|
|
|
|
|
cout << "File not found in " << filePath << endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
cout << "get NoPlate train!" << endl;
|
|
|
|
|
for (int i = 0; i < size; i++)
|
|
|
|
|
{
|
|
|
|
@ -164,16 +168,17 @@ void getNoPlateTrain(Mat& trainingImages, vector<int>& trainingLabels,
|
|
|
|
|
void getHasPlateTest(vector<Mat>& testingImages, vector<int>& testingLabels)
|
|
|
|
|
{
|
|
|
|
|
int label = 1;
|
|
|
|
|
char * filePath = "train/data/plate_detect_svm/test/HasPlate";
|
|
|
|
|
const char * filePath = "train/data/plate_detect_svm/test/HasPlate";
|
|
|
|
|
vector<string> files;
|
|
|
|
|
|
|
|
|
|
////获取该路径下的所有文件
|
|
|
|
|
getFiles(filePath, files );
|
|
|
|
|
|
|
|
|
|
int size = files.size();
|
|
|
|
|
if (0 == size)
|
|
|
|
|
cout << "No File Found in test HasPlate!" << endl;
|
|
|
|
|
|
|
|
|
|
if (0 == size) {
|
|
|
|
|
cout << "File not found in " << filePath << endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
cout << "get HasPlate test!" << endl;
|
|
|
|
|
for (int i = 0; i < size; i++)
|
|
|
|
|
{
|
|
|
|
@ -188,16 +193,17 @@ void getHasPlateTest(vector<Mat>& testingImages, vector<int>& testingLabels)
|
|
|
|
|
void getNoPlateTest(vector<Mat>& testingImages, vector<int>& testingLabels)
|
|
|
|
|
{
|
|
|
|
|
int label = 0;
|
|
|
|
|
char * filePath = "train/data/plate_detect_svm/test/NoPlate";
|
|
|
|
|
const char * filePath = "train/data/plate_detect_svm/test/NoPlate";
|
|
|
|
|
vector<string> files;
|
|
|
|
|
|
|
|
|
|
////获取该路径下的所有文件
|
|
|
|
|
getFiles(filePath, files );
|
|
|
|
|
|
|
|
|
|
int size = files.size();
|
|
|
|
|
if (0 == size)
|
|
|
|
|
cout << "No File Found in test NoPlate!" << endl;
|
|
|
|
|
|
|
|
|
|
if (0 == size) {
|
|
|
|
|
cout << "File not found in " << filePath << endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
cout << "get NoPlate test!" << endl;
|
|
|
|
|
for (int i = 0; i < size; i++)
|
|
|
|
|
{
|
|
|
|
@ -331,7 +337,6 @@ int svmTrain(bool dividePrepared = true, bool trainPrepared = true,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Test SVM
|
|
|
|
|
Mat testingclasses_real;
|
|
|
|
|
vector<Mat> testingImages;
|
|
|
|
|
vector<int> testingLabels_real;
|
|
|
|
|
|
|
|
|
@ -341,7 +346,7 @@ int svmTrain(bool dividePrepared = true, bool trainPrepared = true,
|
|
|
|
|
getNoPlateTest(testingImages, testingLabels_real);
|
|
|
|
|
|
|
|
|
|
CvSVM svm;
|
|
|
|
|
if (trainPrepared == false)
|
|
|
|
|
if (trainPrepared == false && !classes.empty() && !trainingData.empty())
|
|
|
|
|
{
|
|
|
|
|
CvSVMParams SVM_params;
|
|
|
|
|
SVM_params.svm_type = CvSVM::C_SVC;
|
|
|
|
@ -358,15 +363,19 @@ int svmTrain(bool dividePrepared = true, bool trainPrepared = true,
|
|
|
|
|
//Train SVM
|
|
|
|
|
cout << "Begin to generate svm" << endl;
|
|
|
|
|
|
|
|
|
|
//CvSVM svm(trainingData, classes, Mat(), Mat(), SVM_params);
|
|
|
|
|
svm.train_auto(trainingData, classes, Mat(), Mat(), SVM_params, 10,
|
|
|
|
|
CvSVM::get_default_grid(CvSVM::C),
|
|
|
|
|
CvSVM::get_default_grid(CvSVM::GAMMA),
|
|
|
|
|
CvSVM::get_default_grid(CvSVM::P),
|
|
|
|
|
CvSVM::get_default_grid(CvSVM::NU),
|
|
|
|
|
CvSVM::get_default_grid(CvSVM::COEF),
|
|
|
|
|
CvSVM::get_default_grid(CvSVM::DEGREE),
|
|
|
|
|
true);
|
|
|
|
|
try {
|
|
|
|
|
//CvSVM svm(trainingData, classes, Mat(), Mat(), SVM_params);
|
|
|
|
|
svm.train_auto(trainingData, classes, Mat(), Mat(), SVM_params, 10,
|
|
|
|
|
CvSVM::get_default_grid(CvSVM::C),
|
|
|
|
|
CvSVM::get_default_grid(CvSVM::GAMMA),
|
|
|
|
|
CvSVM::get_default_grid(CvSVM::P),
|
|
|
|
|
CvSVM::get_default_grid(CvSVM::NU),
|
|
|
|
|
CvSVM::get_default_grid(CvSVM::COEF),
|
|
|
|
|
CvSVM::get_default_grid(CvSVM::DEGREE),
|
|
|
|
|
true);
|
|
|
|
|
} catch (const Exception &err) {
|
|
|
|
|
cout << err.what() << endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cout << "Svm generate done!" << endl;
|
|
|
|
|
|
|
|
|
@ -375,8 +384,13 @@ int svmTrain(bool dividePrepared = true, bool trainPrepared = true,
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
string path = "train/svm.xml";
|
|
|
|
|
svm.load(path.c_str(), "svm");
|
|
|
|
|
try {
|
|
|
|
|
string path = "train/svm.xml";
|
|
|
|
|
svm.load(path.c_str(), "svm");
|
|
|
|
|
} catch (const Exception &err) {
|
|
|
|
|
cout << err.what() << endl;
|
|
|
|
|
return 0; //next predict requires svm
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cout << "Begin to predict" << endl;
|
|
|
|
|