fr_lfw.cpp 6.51 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/fr_lfw.hpp"
43
#include "opencv2/datasets/util.hpp"
dmitriy.anisimov's avatar
dmitriy.anisimov committed
44

45 46
#include <map>

47 48
namespace cv
{
49
namespace datasets
50 51
{

dmitriy.anisimov's avatar
dmitriy.anisimov committed
52 53
using namespace std;

54
class FR_lfwImp CV_FINAL : public FR_lfw
55 56 57
{
public:
    FR_lfwImp() {}
58
    //FR_lfwImp(const string &path);
59 60
    virtual ~FR_lfwImp() {}

61
    virtual void load(const string &path) CV_OVERRIDE;
62 63

private:
64
    void loadDataset(const string &path);
65 66

    map< string, vector<string> > faces;
67 68 69
};

/*FR_lfwImp::FR_lfwImp(const string &path)
dmitriy.anisimov's avatar
dmitriy.anisimov committed
70 71
{
    loadDataset(path);
72
}*/
dmitriy.anisimov's avatar
dmitriy.anisimov committed
73

74
void FR_lfwImp::load(const string &path)
75 76 77 78
{
    loadDataset(path);
}

79
void FR_lfwImp::loadDataset(const string &path)
dmitriy.anisimov's avatar
dmitriy.anisimov committed
80 81 82 83 84
{
    vector<string> fileNames;
    getDirList(path, fileNames);
    for (vector<string>::iterator it=fileNames.begin(); it!=fileNames.end(); ++it)
    {
85 86 87
        string &name = *it;

        if (name.length()>3 && name.substr(name.length()-4) == ".txt")
88 89 90
        {
            continue;
        }
dmitriy.anisimov's avatar
dmitriy.anisimov committed
91

92 93 94
        vector<string> images;

        string pathFace(path + name + "/");
dmitriy.anisimov's avatar
dmitriy.anisimov committed
95 96 97 98
        vector<string> faceNames;
        getDirList(pathFace, faceNames);
        for (vector<string>::iterator itFace=faceNames.begin(); itFace!=faceNames.end(); ++itFace)
        {
99
            images.push_back(*itFace);
dmitriy.anisimov's avatar
dmitriy.anisimov committed
100 101
        }

102 103 104 105 106 107 108
        faces.insert(make_pair(name, images));
    }

    // test loading
    ifstream infile((path + "pairs.txt").c_str());
    string line;
    getline(infile, line); // should be 10 300
Hyunjun Kim's avatar
Hyunjun Kim committed
109
    CV_Assert(line=="10\t300");
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    unsigned int num = 0;
    while (getline(infile, line))
    {
        if (0 == (num % 600))
        {
            train.push_back(vector< Ptr<Object> >());
            test.push_back(vector< Ptr<Object> >());
        }

        vector<string> elems;
        split(line, elems, '\t');

        Ptr<FR_lfwObj> curr(new FR_lfwObj);
        string &person1 = elems[0];
        unsigned int imageNumber1 = atoi(elems[1].c_str())-1;
        curr->image1 = person1 + "/" + faces[person1][imageNumber1];

        string person2;
        unsigned int imageNumber2;
        if (3 == elems.size())
        {
            person2 = elems[0];
            imageNumber2 = atoi(elems[2].c_str())-1;
            curr->same = true;
        } else
        {
            person2 = elems[2];
            imageNumber2 = atoi(elems[3].c_str())-1;
            curr->same = false;
        }
        curr->image2 = person2 + "/" + faces[person2][imageNumber2];

        test.back().push_back(curr);

        num++;
dmitriy.anisimov's avatar
dmitriy.anisimov committed
145
    }
Hyunjun Kim's avatar
Hyunjun Kim committed
146
    infile.close();
147 148 149 150

    // dev train loading to train[0]
    ifstream infile2((path + "pairsDevTrain.txt").c_str());
    getline(infile2, line); // should 1100
Hyunjun Kim's avatar
Hyunjun Kim committed
151
    CV_Assert(line=="1100");
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    while (getline(infile2, line))
    {
        vector<string> elems;
        split(line, elems, '\t');

        Ptr<FR_lfwObj> curr(new FR_lfwObj);
        string &person1 = elems[0];
        unsigned int imageNumber1 = atoi(elems[1].c_str())-1;
        curr->image1 = person1 + "/" + faces[person1][imageNumber1];

        string person2;
        unsigned int imageNumber2;
        if (3 == elems.size())
        {
            person2 = elems[0];
            imageNumber2 = atoi(elems[2].c_str())-1;
            curr->same = true;
        } else
        {
            person2 = elems[2];
            imageNumber2 = atoi(elems[3].c_str())-1;
            curr->same = false;
        }
        curr->image2 = person2 + "/" + faces[person2][imageNumber2];

        train[0].push_back(curr);
    }
Hyunjun Kim's avatar
Hyunjun Kim committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
    infile2.close();

    // dev train loading to validation[0]
    ifstream infile3((path + "pairsDevTest.txt").c_str());
    getline(infile3, line); // should 500
    CV_Assert(line=="500");
    validation.push_back(vector< Ptr<Object> >());
    while (getline(infile3, line))
    {
        vector<string> elems;
        split(line, elems, '\t');

        Ptr<FR_lfwObj> curr(new FR_lfwObj);
        string &person1 = elems[0];
        unsigned int imageNumber1 = atoi(elems[1].c_str())-1;
        curr->image1 = person1 + "/" + faces[person1][imageNumber1];

        string person2;
        unsigned int imageNumber2;
        if (3 == elems.size())
        {
            person2 = elems[0];
            imageNumber2 = atoi(elems[2].c_str())-1;
            curr->same = true;
        } else
        {
            person2 = elems[2];
            imageNumber2 = atoi(elems[3].c_str())-1;
            curr->same = false;
        }
        curr->image2 = person2 + "/" + faces[person2][imageNumber2];

        validation[0].push_back(curr);
    }
    infile3.close();
dmitriy.anisimov's avatar
dmitriy.anisimov committed
214
}
215

216 217 218 219 220
Ptr<FR_lfw> FR_lfw::create()
{
    return Ptr<FR_lfwImp>(new FR_lfwImp);
}

221 222
}
}