tr_chars.cpp 6.71 KB
Newer Older
dmitriy.anisimov's avatar
dmitriy.anisimov committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2014, Itseez Inc, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Itseez Inc or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

42
#include "opencv2/datasets/tr_chars.hpp"
43
#include "opencv2/datasets/util.hpp"
dmitriy.anisimov's avatar
dmitriy.anisimov committed
44

45 46
namespace cv
{
47
namespace datasets
48 49
{

dmitriy.anisimov's avatar
dmitriy.anisimov committed
50 51
using namespace std;

Dmitriy Anisimov's avatar
Dmitriy Anisimov committed
52
class TR_charsImp : public TR_chars
53 54 55
{
public:
    TR_charsImp() {}
dmitriy.anisimov's avatar
dmitriy.anisimov committed
56
    //TR_charsImp(const string &path, int number = 0);
57 58
    virtual ~TR_charsImp() {}

59
    virtual void load(const string &path);
60 61

private:
62 63 64
    void loadDatasetSplit(const string &path, int number);

    void loadDataset(const string &path);
65

dmitriy.anisimov's avatar
dmitriy.anisimov committed
66 67 68 69 70
    void parseLine(const string &line, vector<int> &currSet, int number);

    inline void convert(vector<int> &from, vector< Ptr<Object> > &to, vector<int> &allLabels, vector<string> &allNames);

    inline void parseSet(const string &line, const string &pattern, bool &flag, vector<int> &set, int number);
71 72 73
};

void TR_charsImp::parseLine(const string &line, vector<int> &currSet, int number)
dmitriy.anisimov's avatar
dmitriy.anisimov committed
74 75 76
{
    vector<string> elems;
    split(line, elems, ' ');
77
    if (number >= (int)elems.size())
dmitriy.anisimov's avatar
dmitriy.anisimov committed
78 79 80 81 82
    {
        return;
    }

    unsigned int ind = atoi(elems[number].c_str());
83
    if (ind > 0)
dmitriy.anisimov's avatar
dmitriy.anisimov committed
84
    {
85
        currSet.push_back(ind-1);
dmitriy.anisimov's avatar
dmitriy.anisimov committed
86 87 88
    }
}

89
inline void TR_charsImp::convert(vector<int> &from, vector< Ptr<Object> > &to, vector<int> &allLabels, vector<string> &allNames)
dmitriy.anisimov's avatar
dmitriy.anisimov committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
{
    for (vector<int>::iterator it=from.begin(); it!=from.end(); ++it)
    {
        if (*it>=(int)allNames.size() || *it>=(int)allLabels.size())
        {
            printf("incorrect index: %u\n", *it);
            continue;
        }

        Ptr<TR_charsObj> curr(new TR_charsObj);
        curr->imgName = allNames[*it];
        curr->label = allLabels[*it];
        to.push_back(curr);
    }
}

inline void TR_charsImp::parseSet(const string &line, const string &pattern, bool &flag, vector<int> &set, int number)
{
    size_t pos = line.find(pattern);
    if (string::npos != pos)
    {
        flag = true;
        string s(line.substr(pos + pattern.length()));
        parseLine(s, set, number);
    } else
    if (flag)
    {
        parseLine(line, set, number);
    }
}

121
/*TR_charsImp::TR_charsImp(const string &path, int number)
dmitriy.anisimov's avatar
dmitriy.anisimov committed
122 123
{
    loadDataset(path, number);
124
}*/
dmitriy.anisimov's avatar
dmitriy.anisimov committed
125

126
void TR_charsImp::load(const string &path)
127
{
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    loadDataset(path);
}

void TR_charsImp::loadDataset(const string &path)
{
    int number = 0;
    do
    {
        loadDatasetSplit(path, number);
        number++;
    } while (train.back().size()>0);

    train.pop_back(); // remove last empty split
    test.pop_back(); // remove last empty split
    validation.pop_back(); // remove last empty split
143 144
}

145
void TR_charsImp::loadDatasetSplit(const string &path, int number)
dmitriy.anisimov's avatar
dmitriy.anisimov committed
146
{
147 148 149 150
    train.push_back(vector< Ptr<Object> >());
    test.push_back(vector< Ptr<Object> >());
    validation.push_back(vector< Ptr<Object> >());

dmitriy.anisimov's avatar
dmitriy.anisimov committed
151
    vector<int> allLabels, trainSet, testSet, validationSet;
dmitriy.anisimov's avatar
dmitriy.anisimov committed
152 153 154 155
    vector<string> allNames;

    ifstream infile((path + "list_English_Img.m").c_str());
    string line;
dmitriy.anisimov's avatar
dmitriy.anisimov committed
156
    bool labels = false, names = false, isTrain = false, isTest = false, isValidation = false;
dmitriy.anisimov's avatar
dmitriy.anisimov committed
157 158 159
    while (getline(infile, line))
    {
        size_t pos = line.find("];");
dmitriy.anisimov's avatar
dmitriy.anisimov committed
160
        if (string::npos != pos)
dmitriy.anisimov's avatar
dmitriy.anisimov committed
161 162 163 164 165
        {
            labels = false;
            names = false;
            isTrain = false;
            isTest = false;
dmitriy.anisimov's avatar
dmitriy.anisimov committed
166
            isValidation = false;
dmitriy.anisimov's avatar
dmitriy.anisimov committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
        }

        string slabels("list.ALLlabels = [");
        pos = line.find(slabels);
        if (string::npos != pos)
        {
            labels = true;
            string s(line.substr(pos+slabels.length()));
            allLabels.push_back(atoi(s.c_str()));
        } else
        if (labels)
        {
            allLabels.push_back(atoi(line.c_str()));
        }

        string snames("list.ALLnames = [");
        pos = line.find(snames);
dmitriy.anisimov's avatar
dmitriy.anisimov committed
184
        if (string::npos != pos)
dmitriy.anisimov's avatar
dmitriy.anisimov committed
185 186 187 188 189 190 191 192 193 194 195 196
        {
            names = true;
            size_t start = pos+snames.length();
            string s(line.substr(start+1, line.length()-start-2));
            allNames.push_back(s);
        } else
        if (names)
        {
            string s(line.substr(1, line.length()-2));
            allNames.push_back(s);
        }

dmitriy.anisimov's avatar
dmitriy.anisimov committed
197 198
        string trainStr("list.TRNind = [");
        parseSet(line, trainStr, isTrain, trainSet, number);
dmitriy.anisimov's avatar
dmitriy.anisimov committed
199

dmitriy.anisimov's avatar
dmitriy.anisimov committed
200 201 202 203 204
        string testStr("list.TSTind = [");
        parseSet(line, testStr, isTest, testSet, number);

        string validationStr("list.VALind = [");
        parseSet(line, validationStr, isValidation, validationSet, number);
dmitriy.anisimov's avatar
dmitriy.anisimov committed
205 206 207 208 209 210 211

        /*"list.classlabels = ["
        "list.classnames = ["
        "list.NUMclasses = 62;"
        "list.TXNind = ["*/
    }

212 213 214
    convert(trainSet, train.back(), allLabels, allNames);
    convert(testSet, test.back(), allLabels, allNames);
    convert(validationSet, validation.back(), allLabels, allNames);
dmitriy.anisimov's avatar
dmitriy.anisimov committed
215
}
216

217 218 219 220 221
Ptr<TR_chars> TR_chars::create()
{
    return Ptr<TR_charsImp>(new TR_charsImp);
}

222 223
}
}