Ask Your Question
1

How to load an image dataset

asked 2017-03-16 12:55:33 -0600

krisscer gravatar image

Hi guys , i am building an image classification application. I have the image dataset in separate folders , any idea how can i load the images so i will be able to train the classifier ? I am really new to this so a few pointers could be helpful!

Thanks

edit retag flag offensive close merge delete

Comments

how does your folder structure look like ? how many classes are there ?

is that "your own data" ? or a "well known dataset" (like mnist, for which actual receipes might exist) ?

berak gravatar imageberak ( 2017-03-16 12:59:51 -0600 )edit

It might alsof be useful to know what you are actually using as classifier...

StevenPuttemans gravatar imageStevenPuttemans ( 2017-03-16 13:07:57 -0600 )edit
1

I am using SVM , I have tree classes which are child , adult and senior . Each class has a dedicated folder filled with images

krisscer gravatar imagekrisscer ( 2017-03-17 03:52:49 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-03-17 04:16:44 -0600

berak gravatar image

updated 2017-03-17 08:29:26 -0600

to train the SVM (or any other ml classifier), you will need a single float Mat with the image features, each on a row, and a integer Mat with the resp. class ids per image. we'll use cv::glob(), to traverse the image directories:

Mat loadImg(const String &name, const Size &siz) {
    Mat img = imread(name, 0); // load grayscale
    if (img.empty()) return img; // or bust
    resize(img, img, siz); // all images need to be of same size
    img.convertTo(img, CV_32F); // float data
    return img.reshape(1,1); //flattened to a single row
}

int loadClass(const String &dir, int label, Mat &trainData, Mat &trainLabels, const Size &siz) {
    vector<String> files;
    glob(dir, files);
    for (size_t i=0; i<files.size(); i++) {
        Mat img = loadImg(files[i], siz);
        if (img.empty()) continue;
        trainData.push_back(img); // add it as a single, flat row
        trainLabels.push_back(label); // integer label
    }
    return (int)trainLabels.size();
}

...

Mat trainData, trainLabels;
Size siz(60,60); // 3600 elements
int child  = loadClass("./images/child/*.png", 0, trainData, trainLabels, siz);
int adult  = loadClass("./images/adult/*.png", 1, trainData, trainLabels, siz);
int senior = loadClass("./images/senior/*.png", 2, trainData, trainLabels, siz);

model->train(ml::TrainData::create(trainData, ml::ROW_SAMPLES, trainLabels));

...

Mat query = loadImg("some/test.png", siz); // same processing for train & test !
int predicted = (int)model->predict(query);
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-03-16 12:55:33 -0600

Seen: 1,660 times

Last updated: Mar 17 '17