Micooz 10 years ago
parent 5870dcc924
commit 02eedbde55

@ -14,14 +14,6 @@
#define OS_LINUX
#endif
#ifdef min
#undef min
#endif
#ifdef max
#undef max
#endif
namespace easypr {
class Utils {
public:
@ -104,6 +96,12 @@ class Utils {
* if not, create it, then call cv::imwrite.
*/
static bool imwrite(const std::string& file, const cv::Mat& image);
private:
/*
* Get the last slash from a path, compatible with Windows and *unix.
*/
static std::size_t get_last_slash(const std::string& path);
};
typedef Utils utils;

@ -6,13 +6,13 @@
namespace easypr {
Svm::Svm(const char* forward_data_folder, const char* inverse_data_folder)
: forward_(forward_data_folder), inverse_(inverse_data_folder) {
: forward_(forward_data_folder), inverse_(inverse_data_folder) {
assert(forward_);
assert(inverse_);
}
void Svm::divide(const char* images_folder, float percentage /* = 0.7 */) {
auto files = Utils::getFiles(images_folder);
auto files = utils::getFiles(images_folder);
if (files.empty()) {
std::cout << "No file found in " << images_folder << std::endl;
return;
@ -25,14 +25,14 @@ void Svm::divide(const char* images_folder, float percentage /* = 0.7 */) {
for (size_t i = 0; i < files.size(); ++i) {
// TODO: Move files directly to improve efficiency.
auto f = files[i];
auto file_name = Utils::getFileName(f, true).c_str();
auto file_name = utils::getFileName(f, true);
auto image = cv::imread(f);
char save_to[255] = {0};
assert(!image.empty());
if (i < split_index) {
sprintf(save_to, "%s/train/%s", images_folder, file_name);
sprintf(save_to, "%s/train/%s", images_folder, file_name.c_str());
} else {
sprintf(save_to, "%s/test/%s", images_folder, file_name);
sprintf(save_to, "%s/test/%s", images_folder, file_name.c_str());
}
utils::imwrite(save_to, image);
std::cout << f << " -> " << save_to << std::endl;
@ -62,7 +62,7 @@ void Svm::get_train() {
features = features.reshape(1, 1);
this->trainingData_.push_back(features);
labels.push_back(Label::kForward); // Note here
labels.push_back(Label::kForward); // Note here
}
// iterate inverse data
@ -84,7 +84,7 @@ void Svm::get_train() {
features = features.reshape(1, 1);
this->trainingData_.push_back(features);
labels.push_back(Label::kInverse); // Note here
labels.push_back(Label::kInverse); // Note here
}
//
@ -154,8 +154,10 @@ void Svm::train(bool divide /* = true */, float divide_percentage /* = 0.7 */,
// need to be trained first
CvSVMParams SVM_params;
SVM_params.svm_type = CvSVM::C_SVC;
//SVM_params.kernel_type = CvSVM::LINEAR; //CvSVM::LINEAR; 线型,也就是无核
SVM_params.kernel_type = CvSVM::RBF; //CvSVM::RBF 径向基函数,也就是高斯核
// SVM_params.kernel_type = CvSVM::LINEAR; //CvSVM::LINEAR;
// 线型,也就是无核
SVM_params.kernel_type =
CvSVM::RBF; // CvSVM::RBF 径向基函数,也就是高斯核
SVM_params.degree = 0.1;
SVM_params.gamma = 1;
SVM_params.coef0 = 0.1;
@ -167,27 +169,25 @@ void Svm::train(bool divide /* = true */, float divide_percentage /* = 0.7 */,
std::cout << "Generating svm model file, please wait..." << std::endl;
try {
//CvSVM svm(trainingData, classes, cv::Mat(), cv::Mat(), SVM_params);
// CvSVM svm(trainingData, classes, cv::Mat(), cv::Mat(), SVM_params);
svm.train_auto(this->trainingData_, this->classes_, cv::Mat(),
cv::Mat(),
SVM_params,
10,
cv::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);
CvSVM::get_default_grid(CvSVM::DEGREE), true);
} catch (const cv::Exception& err) {
std::cout << err.what() << std::endl;
}
utils::mkdir(out_svm_path);
cv::FileStorage fsTo(out_svm_path, cv::FileStorage::WRITE);
svm.write(*fsTo, "svm");
std::cout << "Generate done! The model file is located at " <<
out_svm_path << std::endl;
std::cout << "Generate done! The model file is located at "
<< out_svm_path << std::endl;
} else {
// don't train, use ready-made model file
try {
@ -196,10 +196,10 @@ void Svm::train(bool divide /* = true */, float divide_percentage /* = 0.7 */,
std::cout << err.what() << std::endl;
}
}
} // if train
} // if train
// TODO Check whether the model file exists or not.
svm.load(out_svm_path, "svm"); // make sure svm model was loaded
svm.load(out_svm_path, "svm"); // make sure svm model was loaded
// 30% testing procedure
this->get_test();
@ -220,17 +220,13 @@ void Svm::train(bool divide /* = true */, float divide_percentage /* = 0.7 */,
cv::Mat out;
features.convertTo(out, CV_32FC1);
Label predict = ((int) svm.predict(out)) == 1 ? kForward : kInverse;
Label predict = ((int)svm.predict(out)) == 1 ? kForward : kInverse;
Label real = test_labels_[label_index++];
if (predict == kForward && real == kForward)
ptrue_rtrue++;
if (predict == kForward && real == kInverse)
ptrue_rfalse++;
if (predict == kInverse && real == kForward)
pfalse_rtrue++;
if (predict == kInverse && real == kInverse)
pfalse_rfalse++;
if (predict == kForward && real == kForward) ptrue_rtrue++;
if (predict == kForward && real == kInverse) ptrue_rfalse++;
if (predict == kInverse && real == kForward) pfalse_rtrue++;
if (predict == kInverse && real == kInverse) pfalse_rfalse++;
}
std::cout << "count_all: " << count_all << std::endl;
@ -244,7 +240,8 @@ void Svm::train(bool divide /* = true */, float divide_percentage /* = 0.7 */,
precise = ptrue_rtrue / (ptrue_rtrue + ptrue_rfalse);
std::cout << "precise: " << precise << std::endl;
} else {
std::cout << "precise: " << "NA" << std::endl;
std::cout << "precise: "
<< "NA" << std::endl;
}
double recall = 0;
@ -252,7 +249,8 @@ void Svm::train(bool divide /* = true */, float divide_percentage /* = 0.7 */,
recall = ptrue_rtrue / (ptrue_rtrue + pfalse_rtrue);
std::cout << "recall: " << recall << std::endl;
} else {
std::cout << "recall: " << "NA" << std::endl;
std::cout << "recall: "
<< "NA" << std::endl;
}
double Fsocre = 0;
@ -260,9 +258,9 @@ void Svm::train(bool divide /* = true */, float divide_percentage /* = 0.7 */,
Fsocre = 2 * (precise * recall) / (precise + recall);
std::cout << "Fsocre: " << Fsocre << std::endl;
} else {
std::cout << "Fsocre: " << "NA" << std::endl;
std::cout << "Fsocre: "
<< "NA" << std::endl;
}
}
} // namespace easypr
} // namespace easypr

@ -6,6 +6,13 @@
#include <direct.h>
#include <io.h>
#define PATH_DELIMITER '\\'
#ifdef min
#undef min
#endif
#ifdef max
#undef max
#endif
#elif defined(OS_LINUX) || defined(OS_UNIX)
#include <cstring>
@ -53,24 +60,7 @@ long Utils::getTimestamp() {
std::string Utils::getFileName(const std::string& path,
const bool postfix /* = false */) {
if (!path.empty()) {
#ifdef OS_WINDOWS
size_t last_slash_1 = path.find_last_of("\\");
size_t last_slash_2 = path.find_last_of("/");
size_t last_slash;
if (last_slash_1 != std::string::npos &&
last_slash_2 != std::string::npos) {
// C:/path\\to/file.postfix
last_slash = max(last_slash_1, last_slash_2);
} else {
// C:\\path\\to\\file.postfix
// C:/path/to/file.postfix
last_slash =
(last_slash_1 == std::string::npos) ? last_slash_2 : last_slash_1;
}
#else
size_t last_slash = path.find_last_of('/');
#endif
size_t last_slash = utils::get_last_slash(path);
size_t last_dot = path.find_last_of('.');
if (last_dot < last_slash || last_dot == std::string::npos) {
@ -150,7 +140,7 @@ std::vector<std::string> Utils::getFiles(const std::string& folder,
}
} else {
// it's a file
std::string file_path;
std::string file_path;
// current_folder.pop_back();
file_path.assign(current_folder.c_str()).pop_back();
file_path.append(file_info.name);
@ -234,13 +224,13 @@ bool Utils::mkdir(const std::string folder) {
if (c == PATH_DELIMITER || it == folder.end() - 1) {
folder_builder.append(sub);
#ifdef OS_WINDOWS
if (0 != ::_access(folder_builder.c_str(), 0)) {
if (0 != ::_access(folder_builder.c_str(), 0)) {
#else
if (0 != ::access(folder_builder.c_str(), 0)) {
#endif
// this folder not exist
// this folder not exist
#ifdef OS_WINDOWS
if (0 != ::_mkdir(folder_builder.c_str())) {
if (0 != ::_mkdir(folder_builder.c_str())) {
#else
if (0 != ::mkdir(folder_builder.c_str(), S_IRWXU)) {
#endif
@ -255,9 +245,30 @@ bool Utils::mkdir(const std::string folder) {
}
bool Utils::imwrite(const std::string& file, const cv::Mat& image) {
auto folder = file.substr(0, file.find_last_of(PATH_DELIMITER));
auto folder = file.substr(0, utils::get_last_slash(file));
Utils::mkdir(folder);
return cv::imwrite(file, image);
}
} // namespace easypr
std::size_t Utils::get_last_slash(const std::string& path) {
#ifdef OS_WINDOWS
size_t last_slash_1 = path.find_last_of("\\");
size_t last_slash_2 = path.find_last_of("/");
size_t last_slash;
if (last_slash_1 != std::string::npos && last_slash_2 != std::string::npos) {
// C:/path\\to/file.postfix
last_slash = std::max(last_slash_1, last_slash_2);
} else {
// C:\\path\\to\\file.postfix
// C:/path/to/file.postfix
last_slash =
(last_slash_1 == std::string::npos) ? last_slash_2 : last_slash_1;
}
#else
size_t last_slash = path.find_last_of('/');
#endif
return last_slash;
}
} // namespace easypr
Loading…
Cancel
Save