Error while converting csv file into matrices form using CvMLData
I need to convert csv file into matrices.I wrote the following code.
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include "opencv2/imgcodecs.hpp"
#include <opencv2/highgui/highgui_c.h>
#include <opencv2/ml.hpp>
#include <cstdlib>
#include<algorithm>
#include<fstream>
#include<string>
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace cv;
using namespace cv::ml;
using namespace std;
int main(int, char**){
const char *CSV_FILE = "/home/vidushi/Desktop/new/training.csv";
const char *CSV_FILE1 = "/home/vidushi/Desktop/new/testing.csv";
CvMLData dataFile;
CvMLData dataFile1;
// Load matrix data in csv format
if (dataFile.read_csv(CSV_FILE) != 0)
{
fprintf(stderr, "Can't read csv file %s\n", CSV_FILE);
return -1;
}
Mat dataMat(dataFile.get_values()); // Default data type is float
int labels[11] = {1,1,1,1,1,1,1,1,1,1,1};
Mat labelsMat(11, 1, CV_32SC1, labels);
CvMLData dataFile1;
// Load matrix data in csv format
if (dataFile1.read_csv(CSV_FILE1) != 0)
{
fprintf(stderr, "Can't read csv file %s\n", CSV_FILE1);
return -1;
}
Mat dataMat1(dataFile1.get_values()); // Default data type is float
Ptr<SVM> svm = SVM::create();
svm->setType(SVM::C_SVC);
svm->setKernel(SVM::LINEAR);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));
svm->train(dataMat, ROW_SAMPLE, labelsMat);
float res = svm->predict(dataMat1);
cout << "- Result of prediction:" << res;
return 0;
}
When i try to compile this code.Error is generated -
/home/vidushi/Desktop/new/svm_class.cpp:29:5: error: ‘CvMLData’ was not declared in this scope
CvMLData dataFile;
^
/home/vidushi/Desktop/new/svm_class.cpp:30:14: error: expected ‘;’ before ‘dataFile1’
CvMLData dataFile1;
^
/home/vidushi/Desktop/new/svm_class.cpp:32:9: error: ‘dataFile’ was not declared in this scope
if (dataFile.read_csv(CSV_FILE) != 0)
^
/home/vidushi/Desktop/new/svm_class.cpp:37:17: error: ‘dataFile’ was not declared in this scope
Mat dataMat(dataFile.get_values()); // Default data type is float
^
/home/vidushi/Desktop/new/svm_class.cpp:41:14: error: expected ‘;’ before ‘dataFile1’
CvMLData dataFile1;
^
/home/vidushi/Desktop/new/svm_class.cpp:43:9: error: ‘dataFile1’ was not declared in this scope
if (dataFile1.read_csv(CSV_FILE1) != 0)
^
/home/vidushi/Desktop/new/svm_class.cpp:48:18: error: ‘dataFile1’ was not declared in this scope
Please tell how to resolve this error. I am using opencv 3.2.0, ubuntu 16.04 LTS.
first, please DO NOT use opencv's outdated and no more maintained c-api ! TrainData::loadFromCSV() should be used instead.
then, what kind of data is inside your csv ? could you add a description, and maybe a sample line to your question ?
It is comma seperated csv file and contains 10 decimal values in one row of csv file.The first column contains image number.Remaining 9 column contains features of the image in decimal form.Features could be like number of pixels in image,number of horizontal line in image. I tried using TrainData also but than also error was generated.I would be highly obliged if you could tell me how to use TrainData.
is the "image number" your "class label" ?
No image number is not my class label
so, what should be done about it ?
(indeed, machine learning is 80% cleaning data ..)
That problem is removed.Now the error is coming in this line float res = svm->predict(data1);
OpenCV Error: Assertion failed (nsamples == 1) in predict, file /home/vidushi/Desktop/OpenCV/modules/ml/src/svm.cpp, line 1939 terminate called after throwing an instance of 'cv::Exception' This is the error generated
if you have more than 1 row in data1, you have to use a result Mat for the predictions, like: