Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to know my svm is trained accordingly

//FlannBasedMatcher matcher;
std::vector< DMatch > matches;
double max_dist = 0; 
double min_dist = 100;

Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased");
Ptr<DescriptorExtractor> extractor = new SurfDescriptorExtractor();

//SurfDescriptorExtractor extractor;
SurfFeatureDetector detector(500);
std::vector<KeyPoint> keypoints;
int dictionarySize = 1500;
TermCriteria tc(CV_TERMCRIT_ITER, 10, 0.001);
int retries = 1;
int flags = KMEANS_PP_CENTERS;
BOWKMeansTrainer bow(dictionarySize, tc, retries, flags);
BOWImgDescriptorExtractor dextract(extractor,matcher);

Mat img_keypoints_1;
Mat descriptors_1;
string YourImagesDirectory="D:\\Cars\\";
vector<string> files=listFilesInDirectory(YourImagesDirectory+"*.jpg");
//Load NOT cars!
string YourImagesDirectory_2="D:\\not_cars\\";
vector<string> files_no=listFilesInDirectory(YourImagesDirectory_2+"*.jpg");

// Initialize constant values
const int nb_cars = files.size();
const int not_cars = files_no.size();
const int num_img = nb_cars + not_cars; // Get the number of images
 const int image_area = 40*30;
// Initialize your training set.
cv::Mat training_mat(num_img,image_area,CV_32FC1);
cv::Mat labels(num_img,1,CV_32FC1);
cv::Mat tmp_dst( 500, 450, CV_8UC1 ); // to the right size for resize
// Set temp matrices
cv::Mat tmp_img;

std::vector<string> all_names;

all_names.assign(files.begin(),files.end());
all_names.insert(all_names.end(), files_no.begin(), files_no.end());

// Load image and add them to the training set
int count = 0;
vector<string>::const_iterator i;
string Dir;
for (i = all_names.begin(); i != all_names.end(); ++i)
{
    Dir=( (count < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2);

    tmp_img = cv::imread( Dir +*i, 0 );

    resize( tmp_img, tmp_dst, tmp_dst.size() );

    Mat row_img = tmp_dst; // get a one line image.

    detector.detect( row_img, keypoints);

    extractor->compute( row_img, keypoints, descriptors_1);

    bow.add(descriptors_1);

    ++count;
}

int count_2=0;

Mat vocabulary = bow.cluster();
dextract.setVocabulary(vocabulary);
for (i = all_names.begin(); i != all_names.end(); ++i)
{
    Dir=( (count_2 < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2);

    tmp_img = cv::imread( Dir +*i, 0 );

    resize( tmp_img, tmp_dst, tmp_dst.size() );

    Mat row_img = tmp_dst; // get a one line image.

    detector.detect( row_img, keypoints);

    dextract.compute( row_img, keypoints, descriptors_1);

    training_mat.push_back(descriptors_1);

    labels.at< float >(count, 0) = (count<nb_cars)?1:-1; // 1 for car, -1 otherwise*/

    ++count_2;
}

// Train your SVM
CvSVMParams Params;
Params.svm_type=CvSVM::C_SVC;
Params.kernel_type=CvSVM::LINEAR;
Params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);
Params.gamma=3;
CvSVM svm;
svm.train(training_mat,labels,cv::Mat(),cv::Mat(),Params);
svm.save("trainsvm.txt");

This is my code , which i try to summarize from the psuedo code from here , my code is running and its not showing me the error , but how do i know that now my data is trained according to my need and now i can implement on my real time app to detect objects from video

How to know my svm is trained accordingly

//FlannBasedMatcher matcher;
std::vector< DMatch > matches;
double max_dist = 0; 
double min_dist = 100;

Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased");
Ptr<DescriptorExtractor> extractor = new SurfDescriptorExtractor();

//SurfDescriptorExtractor extractor;
SurfFeatureDetector detector(500);
std::vector<KeyPoint> keypoints;
int dictionarySize = 1500;
TermCriteria tc(CV_TERMCRIT_ITER, 10, 0.001);
int retries = 1;
int flags = KMEANS_PP_CENTERS;
BOWKMeansTrainer bow(dictionarySize, tc, retries, flags);
BOWImgDescriptorExtractor dextract(extractor,matcher);

Mat img_keypoints_1;
Mat descriptors_1;
string YourImagesDirectory="D:\\Cars\\";
vector<string> files=listFilesInDirectory(YourImagesDirectory+"*.jpg");
//Load NOT cars!
string YourImagesDirectory_2="D:\\not_cars\\";
vector<string> files_no=listFilesInDirectory(YourImagesDirectory_2+"*.jpg");

// Initialize constant values
const int nb_cars = files.size();
const int not_cars = files_no.size();
const int num_img = nb_cars + not_cars; // Get the number of images
 const int image_area = 40*30;
// Initialize your training set.
cv::Mat training_mat(num_img,image_area,CV_32FC1);
cv::Mat labels(num_img,1,CV_32FC1);
cv::Mat tmp_dst( 500, 450, CV_8UC1 ); // to the right size for resize
// Set temp matrices
cv::Mat tmp_img;

std::vector<string> all_names;

all_names.assign(files.begin(),files.end());
all_names.insert(all_names.end(), files_no.begin(), files_no.end());

// Load image and add them to the training set
int count = 0;
vector<string>::const_iterator i;
string Dir;
for (i = all_names.begin(); i != all_names.end(); ++i)
{
    Dir=( (count < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2);

    tmp_img = cv::imread( Dir +*i, 0 );

    resize( tmp_img, tmp_dst, tmp_dst.size() );

    Mat row_img = tmp_dst; // get a one line image.

    detector.detect( row_img, keypoints);

    extractor->compute( row_img, keypoints, descriptors_1);

    bow.add(descriptors_1);

    ++count;
}

int count_2=0;

Mat vocabulary = bow.cluster();
dextract.setVocabulary(vocabulary);
for (i = all_names.begin(); i != all_names.end(); ++i)
{
    Dir=( (count_2 < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2);

    tmp_img = cv::imread( Dir +*i, 0 );

    resize( tmp_img, tmp_dst, tmp_dst.size() );

    Mat row_img = tmp_dst; // get a one line image.

    detector.detect( row_img, keypoints);

    dextract.compute( row_img, keypoints, descriptors_1);

    training_mat.push_back(descriptors_1);

    labels.at< float >(count, 0) = (count<nb_cars)?1:-1; // 1 for car, -1 otherwise*/

    ++count_2;
}

// Train your SVM
CvSVMParams Params;
Params.svm_type=CvSVM::C_SVC;
Params.kernel_type=CvSVM::LINEAR;
Params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);
Params.gamma=3;
CvSVM svm;
svm.train(training_mat,labels,cv::Mat(),cv::Mat(),Params);
svm.save("trainsvm.txt");

This is my code , which i try to summarize from the psuedo code from here , in answer, my code is running and its not showing me the syntax error , but after a long time processing it show me the following run time error

image description

how do i know that now my data is trained according to my need and now i can implement on my real time app to detect objects from video

How to know my svm is trained accordingly

//FlannBasedMatcher matcher;
std::vector< DMatch > matches;
double max_dist = 0; 
double min_dist = 100;

Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased");
Ptr<DescriptorExtractor> extractor = new SurfDescriptorExtractor();

//SurfDescriptorExtractor extractor;
SurfFeatureDetector detector(500);
std::vector<KeyPoint> keypoints;
int dictionarySize = 1500;
TermCriteria tc(CV_TERMCRIT_ITER, 10, 0.001);
int retries = 1;
int flags = KMEANS_PP_CENTERS;
BOWKMeansTrainer bow(dictionarySize, tc, retries, flags);
BOWImgDescriptorExtractor dextract(extractor,matcher);

Mat img_keypoints_1;
Mat descriptors_1;
string YourImagesDirectory="D:\\Cars\\";
vector<string> files=listFilesInDirectory(YourImagesDirectory+"*.jpg");
//Load NOT cars!
string YourImagesDirectory_2="D:\\not_cars\\";
vector<string> files_no=listFilesInDirectory(YourImagesDirectory_2+"*.jpg");

// Initialize constant values
const int nb_cars = files.size();
const int not_cars = files_no.size();
const int num_img = nb_cars + not_cars; // Get the number of images
 const int image_area = 40*30;
// Initialize your training set.
cv::Mat training_mat(num_img,image_area,CV_32FC1);
cv::Mat labels(num_img,1,CV_32FC1);
cv::Mat tmp_dst( 500, 450, CV_8UC1 ); // to the right size for resize
// Set temp matrices
cv::Mat tmp_img;

std::vector<string> all_names;

all_names.assign(files.begin(),files.end());
all_names.insert(all_names.end(), files_no.begin(), files_no.end());

// Load image and add them to the training set
int count = 0;
vector<string>::const_iterator i;
string Dir;
for (i = all_names.begin(); i != all_names.end(); ++i)
{
    Dir=( (count < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2);

    tmp_img = cv::imread( Dir +*i, 0 );

    resize( tmp_img, tmp_dst, tmp_dst.size() );

    Mat row_img = tmp_dst; // get a one line image.

    detector.detect( row_img, keypoints);

    extractor->compute( row_img, keypoints, descriptors_1);

    bow.add(descriptors_1);

    ++count;
}

int count_2=0;

Mat vocabulary = bow.cluster();
dextract.setVocabulary(vocabulary);
for (i = all_names.begin(); i != all_names.end(); ++i)
{
    Dir=( (count_2 < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2);

    tmp_img = cv::imread( Dir +*i, 0 );

    resize( tmp_img, tmp_dst, tmp_dst.size() );

    Mat row_img = tmp_dst; // get a one line image.

    detector.detect( row_img, keypoints);

    dextract.compute( row_img, keypoints, descriptors_1);

    training_mat.push_back(descriptors_1);

    labels.at< float >(count, >(count_2, 0) = (count<nb_cars)?1:-1; (count_2<nb_cars)?1:-1; // 1 for car, -1 otherwise*/

    ++count_2;
}

// Train your SVM
CvSVMParams Params;
Params.svm_type=CvSVM::C_SVC;
Params.kernel_type=CvSVM::LINEAR;
Params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);
Params.gamma=3;
CvSVM svm;
svm.train(training_mat,labels,cv::Mat(),cv::Mat(),Params);
svm.save("trainsvm.txt");

This is my code , which i try to summarize from the psuedo code from here in answer, my code is running and its not showing me the syntax error but after a long time processing it show me the following run time error

image description

how do i know that now my data is trained according to my need and now i can implement on my real time app to detect objects from video

How to know my svm is trained accordingly

//FlannBasedMatcher matcher;
std::vector< DMatch > matches;
double max_dist = 0; 
double min_dist = 100;

Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased");
Ptr<DescriptorExtractor> extractor = new SurfDescriptorExtractor();

//SurfDescriptorExtractor extractor;
SurfFeatureDetector detector(500);
std::vector<KeyPoint> keypoints;
int dictionarySize = 1500;
TermCriteria tc(CV_TERMCRIT_ITER, 10, 0.001);
int retries = 1;
int flags = KMEANS_PP_CENTERS;
BOWKMeansTrainer bow(dictionarySize, tc, retries, flags);
BOWImgDescriptorExtractor dextract(extractor,matcher);

Mat img_keypoints_1;
Mat descriptors_1;
string YourImagesDirectory="D:\\Cars\\";
vector<string> files=listFilesInDirectory(YourImagesDirectory+"*.jpg");
//Load NOT cars!
string YourImagesDirectory_2="D:\\not_cars\\";
vector<string> files_no=listFilesInDirectory(YourImagesDirectory_2+"*.jpg");

// Initialize constant values
const int nb_cars = files.size();
const int not_cars = files_no.size();
const int num_img = nb_cars + not_cars; // Get the number of images
 const int image_area = 40*30;
// Initialize your training set.
cv::Mat training_mat(num_img,image_area,CV_32FC1);
cv::Mat labels(num_img,1,CV_32FC1);
cv::Mat tmp_dst( 500, 450, CV_8UC1 ); // to the right size for resize
// Set temp matrices
cv::Mat tmp_img;

std::vector<string> all_names;

all_names.assign(files.begin(),files.end());
all_names.insert(all_names.end(), files_no.begin(), files_no.end());

// Load image and add them to the training set
int count = 0;
vector<string>::const_iterator i;
string Dir;
for (i = all_names.begin(); i != all_names.end(); ++i)
{
    Dir=( (count < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2);

    tmp_img = cv::imread( Dir +*i, 0 );

    resize( tmp_img, tmp_dst, tmp_dst.size() );

    Mat row_img = tmp_dst; // get a one line image.
 
    detector.detect( row_img, keypoints);

    extractor->compute( row_img, keypoints, descriptors_1);

    bow.add(descriptors_1);

    ++count;
}

int count_2=0;

Mat vocabulary = bow.cluster();
dextract.setVocabulary(vocabulary);
for (i = all_names.begin(); i != all_names.end(); ++i)
{
    Dir=( (count_2 < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2);

    tmp_img = cv::imread( Dir +*i, 0 );

    resize( tmp_img, tmp_dst, tmp_dst.size() );

    Mat row_img = tmp_dst; // get a one line image.
tmp_dst;

    detector.detect( row_img, keypoints);

    dextract.compute( row_img, keypoints, descriptors_1);

    training_mat.push_back(descriptors_1);

    labels.at< float >(count_2, 0) = (count_2<nb_cars)?1:-1; // 1 for car, -1 otherwise*/

    ++count_2;
}

// Train your SVM
CvSVMParams Params;
Params.svm_type=CvSVM::C_SVC;
Params.kernel_type=CvSVM::LINEAR;
Params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);
Params.gamma=3;
CvSVM svm;
svm.train(training_mat,labels,cv::Mat(),cv::Mat(),Params);
svm.save("trainsvm.txt");

This is my code , which i try to summarize from the psuedo code from here in answer, my code is running and its not showing me the syntax error but after a long time processing it show me the following run time error

image description

how do i know that now my data is trained according to my need and now i can implement on my real time app to detect objects from video

How to know my svm OpenCv without using OpenCv manager is trained accordinglynot working

//FlannBasedMatcher matcher;
std::vector< DMatch > matches;
double max_dist = 0; 
double min_dist = 100;

Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased");
Ptr<DescriptorExtractor> extractor = new SurfDescriptorExtractor();

//SurfDescriptorExtractor extractor;
SurfFeatureDetector detector(500);
std::vector<KeyPoint> keypoints;
int dictionarySize = 1500;
TermCriteria tc(CV_TERMCRIT_ITER, 10, 0.001);
int retries = 1;
int flags = KMEANS_PP_CENTERS;
BOWKMeansTrainer bow(dictionarySize, tc, retries, flags);
BOWImgDescriptorExtractor dextract(extractor,matcher);

Mat img_keypoints_1;
Mat descriptors_1;
string YourImagesDirectory="D:\\Cars\\";
vector<string> files=listFilesInDirectory(YourImagesDirectory+"*.jpg");
//Load NOT cars!
string YourImagesDirectory_2="D:\\not_cars\\";
vector<string> files_no=listFilesInDirectory(YourImagesDirectory_2+"*.jpg");

// Initialize constant values
const int nb_cars = files.size();
const int not_cars = files_no.size();
const int num_img = nb_cars + not_cars; // Get 

I am using OpenCv on Android , when I run the number application it give me option of images const int image_area = 40*30; // Initialize your training set. cv::Mat training_mat(num_img,image_area,CV_32FC1); cv::Mat labels(num_img,1,CV_32FC1); cv::Mat tmp_dst( 500, 450, CV_8UC1 ); // installing OpenCv manager , I don't want to the right size use this option for resize // Set temp matrices cv::Mat tmp_img; std::vector<string> all_names; all_names.assign(files.begin(),files.end()); all_names.insert(all_names.end(), files_no.begin(), files_no.end()); // Load image and add them to the training set int count = 0; vector<string>::const_iterator i; string Dir; for (i = all_names.begin(); i != all_names.end(); ++i) { Dir=( (count < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2); tmp_img = cv::imread( Dir +*i, 0 ); resize( tmp_img, tmp_dst, tmp_dst.size() ); Mat row_img = tmp_dst; detector.detect( row_img, keypoints); extractor->compute( row_img, keypoints, descriptors_1); bow.add(descriptors_1); ++count; } int count_2=0; Mat vocabulary = bow.cluster(); dextract.setVocabulary(vocabulary); for (i = all_names.begin(); i != all_names.end(); ++i) { Dir=( (count_2 < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2); tmp_img = cv::imread( Dir +*i, 0 ); resize( tmp_img, tmp_dst, tmp_dst.size() ); Mat row_img = tmp_dst; detector.detect( row_img, keypoints); dextract.compute( row_img, keypoints, descriptors_1); training_mat.push_back(descriptors_1); labels.at< float >(count_2, 0) = (count_2<nb_cars)?1:-1; // 1 for car, -1 otherwise*/ ++count_2; } // Train your SVM CvSVMParams Params; Params.svm_type=CvSVM::C_SVC; Params.kernel_type=CvSVM::LINEAR; Params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6); Params.gamma=3; CvSVM svm; svm.train(training_mat,labels,cv::Mat(),cv::Mat(),Params); svm.save("trainsvm.txt");

This is my code , which i try to summarize application. For this I followed this answer from the psuedo code from herethis forum in answer, my code is running and , I follow the steps but still its not showing asking me the syntax error but after a long time processing it show me the following run time errorfor OpenCv manager.

image descriptionBelow is my android.mk

how do i know that now my data is trained according to my need Android.mk

and now i can implement on my real time app to detect objects from videoProperties option

Project Properties

Followed OpenCv this link.

OpenCv on Android without using OpenCv manager is not working

I am using OpenCv on Android , when I run the application it give me option of installing OpenCv manager , I don't want to use this option for my application. For this I followed this answer from this forum , I follow the steps but still its asking me for OpenCv manager.

Below is my android.mk

Android.mk

and Properties option

Project Properties

Followed OpenCv this link.