Ask Your Question
0

Error while converting csv file into matrices form using CvMLData

asked 2017-03-31 03:18:50 -0600

vidushig2 gravatar image

updated 2017-03-31 03:27:28 -0600

berak gravatar image

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.

edit retag flag offensive close merge delete

Comments

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 ?

berak gravatar imageberak ( 2017-03-31 03:34:17 -0600 )edit
1

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.

vidushig2 gravatar imagevidushig2 ( 2017-03-31 03:43:29 -0600 )edit

is the "image number" your "class label" ?

berak gravatar imageberak ( 2017-03-31 03:52:05 -0600 )edit

No image number is not my class label

vidushig2 gravatar imagevidushig2 ( 2017-04-01 01:26:35 -0600 )edit

so, what should be done about it ?

(indeed, machine learning is 80% cleaning data ..)

berak gravatar imageberak ( 2017-04-01 01:30:15 -0600 )edit

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

vidushig2 gravatar imagevidushig2 ( 2017-04-01 03:52:31 -0600 )edit

if you have more than 1 row in data1, you have to use a result Mat for the predictions, like:

Mat result;
svm->predict(data1, result); // will hold n predictions for n rows in data1
berak gravatar imageberak ( 2017-04-01 04:03:17 -0600 )edit

1 answer

Sort by » oldest newest most voted
0

answered 2017-03-31 04:00:13 -0600

berak gravatar image

assuming, the 1st number in your csv is the class label, you should try like this:

Ptr<ml::TrainData> tdata = ml::TrainData::loadFromCSV("my.csv",0,0,1);
Mat data   = tdata->getTrainSamples();
Mat labels = tdata->getTrainResponses();
labels.convertTo(labels, CV_32S); // integer labels needed for classification

svm->train(data, ROW_SAMPLE, labels);
edit flag offensive delete link more

Comments

I wrote the following code int main(int,char **){ Ptr<ml::traindata> tdata = ml::TrainData::loadFromCSV("/home/vidushi/Desktop/new/training.csv",0,-2,0); Mat data = tdata->getTrainSamples(); int labels[11] = {1,1,1,1,1,1,1,1,1,1,1}; Mat labelsMat(11, 1, CV_32SC1, labels); 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(data, ROW_SAMPLE, labelsMat); Ptr<ml::traindata> tdata1 = ml::TrainData::loadFromCSV("/home/vidushi/Desktop/new/testing.csv",0,-2,0); Mat data1 = tdata1->getTrainSamples(); float res = svm->predict(data1); cout<<"result = "<<res&lt;&lt;"\n"; return="" 0;}error="" generated="" is="" :="" opencv="" error:="" assertion="" failed="" (nvars="=" (int)rowvals.size())="" in="" loadcsv,<="" p="">

vidushig2 gravatar imagevidushig2 ( 2017-04-01 00:54:53 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-03-31 03:18:50 -0600

Seen: 1,105 times

Last updated: Mar 31 '17