From c5bc6e82ea1414accdae935bf5e821384efc0666 Mon Sep 17 00:00:00 2001 From: Dmitriy Anisimov Date: Tue, 21 Oct 2014 12:49:46 +0400 Subject: [PATCH] added fr_adience dataset loader --- .../include/opencv2/datasets/fr_adience.hpp | 93 ++++++++++ modules/datasets/samples/fr_adience.cpp | 105 +++++++++++ modules/datasets/src/fr_adience.cpp | 175 ++++++++++++++++++ 3 files changed, 373 insertions(+) create mode 100644 modules/datasets/include/opencv2/datasets/fr_adience.hpp create mode 100644 modules/datasets/samples/fr_adience.cpp create mode 100644 modules/datasets/src/fr_adience.cpp diff --git a/modules/datasets/include/opencv2/datasets/fr_adience.hpp b/modules/datasets/include/opencv2/datasets/fr_adience.hpp new file mode 100644 index 000000000..7d58b0958 --- /dev/null +++ b/modules/datasets/include/opencv2/datasets/fr_adience.hpp @@ -0,0 +1,93 @@ +/*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*/ + +#ifndef OPENCV_DATASETS_FR_ADIENCE_HPP +#define OPENCV_DATASETS_FR_ADIENCE_HPP + +#include +#include + +#include "opencv2/datasets/dataset.hpp" + +#include + +namespace cv +{ +namespace datasets +{ + +enum genderType +{ + male = 0, + female, + none +}; + +struct FR_adienceObj : public Object +{ + std::string user_id; + std::string original_image; + int face_id; + std::string age; + genderType gender; + int x; + int y; + int dx; + int dy; + int tilt_ang; + int fiducial_yaw_angle; + int fiducial_score; +}; + +class CV_EXPORTS FR_adience : public Dataset +{ +public: + virtual void load(const std::string &path) = 0; + + static Ptr create(); + + std::vector paths; +}; + +} +} + +#endif diff --git a/modules/datasets/samples/fr_adience.cpp b/modules/datasets/samples/fr_adience.cpp new file mode 100644 index 000000000..ece94f1c3 --- /dev/null +++ b/modules/datasets/samples/fr_adience.cpp @@ -0,0 +1,105 @@ +/*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*/ + +#include "opencv2/datasets/fr_adience.hpp" + +#include + +#include + +#include +#include + +using namespace std; +using namespace cv; +using namespace cv::datasets; + +int main(int argc, char *argv[]) +{ + const char *keys = + "{ help h usage ? | | show this message }" + "{ path p |true| path to dataset folder and splits }"; + CommandLineParser parser(argc, argv, keys); + string path(parser.get("path")); + if (parser.has("help") || path=="true") + { + parser.printMessage(); + return -1; + } + + Ptr dataset = FR_adience::create(); + dataset->load(path); + + // *************** + // dataset contains for each object its images. + // For example, let output splits number, dataset size and last image. + int numSplits = dataset->getNumSplits(); + printf("splits number: %u\n", numSplits); + printf("dataset size: %u\n", (unsigned int)dataset->getTrain().size()); + + FR_adienceObj *example = static_cast(dataset->getTrain().back().get()); + printf("last image:\n"); + printf("user_id: %s\n", example->user_id.c_str()); + printf("original_image: %s\n", example->original_image.c_str()); + printf("face_id: %u\n", example->face_id); + printf("age: %s\n", example->age.c_str()); + printf("gender: "); + if (example->gender == male) + { + printf("m\n"); + } else + if (example->gender == female) + { + printf("f\n"); + } else + { + printf("none\n"); + } + printf("x: %u\n", example->x); + printf("y: %u\n", example->y); + printf("dx: %u\n", example->dx); + printf("dy: %u\n", example->dy); + printf("tilt_ang: %u\n", example->tilt_ang); + printf("fiducial_yaw_angle: %u\n", example->fiducial_yaw_angle); + printf("fiducial_score: %u\n", example->fiducial_score); + + return 0; +} diff --git a/modules/datasets/src/fr_adience.cpp b/modules/datasets/src/fr_adience.cpp new file mode 100644 index 000000000..470a1fbcd --- /dev/null +++ b/modules/datasets/src/fr_adience.cpp @@ -0,0 +1,175 @@ +/*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*/ + +#include "opencv2/datasets/fr_adience.hpp" +#include "opencv2/datasets/util.hpp" +#include "precomp.hpp" + +namespace cv +{ +namespace datasets +{ + +using namespace std; + +class CV_EXPORTS FR_adienceImp : public FR_adience +{ +public: + FR_adienceImp() {} + //FR_adienceImp(const string &path); + virtual ~FR_adienceImp() {} + + virtual void load(const string &path); + +private: + void loadDataset(const string &path); + + void loadFile(const string &filename, vector< Ptr > &out); + void cv5ToSplits(vector< Ptr > fileList[5]); +}; + +/*FR_adienceImp::FR_adienceImp(const string &path) +{ + loadDataset(path); +}*/ + +void FR_adienceImp::load(const string &path) +{ + loadDataset(path); +} + +void FR_adienceImp::loadFile(const string &filename, vector< Ptr > &out) +{ + string line; + ifstream infile(filename.c_str()); + while (getline(infile, line)) + { + Ptr curr(new FR_adienceObj); + + vector elems; + split(line, elems, ','); + + curr->user_id = elems[0]; + curr->original_image = elems[1]; + curr->face_id = atoi(elems[2].c_str()); + curr->age = elems[3]; + if (elems[4]=="m") + { + curr->gender = male; + } else + if (elems[4]=="f") + { + curr->gender = female; + } else + { + curr->gender = none; + } + curr->x = atoi(elems[5].c_str()); + curr->y = atoi(elems[6].c_str()); + curr->dx = atoi(elems[7].c_str()); + curr->dy = atoi(elems[8].c_str()); + curr->tilt_ang = atoi(elems[9].c_str()); + curr->fiducial_yaw_angle = atoi(elems[10].c_str()); + curr->fiducial_score = atoi(elems[11].c_str()); + + out.push_back(curr); + } +} + +void FR_adienceImp::cv5ToSplits(vector< Ptr > fileList[5]) +{ + for (unsigned int i=0; i<5; ++i) + { + train.push_back(vector< Ptr >()); + test.push_back(vector< Ptr >()); + validation.push_back(vector< Ptr >()); + for (unsigned int j=0; j<5; ++j) + { + vector< Ptr > &currlist = fileList[j]; + if (i!=j) + { + for (vector< Ptr >::iterator it=currlist.begin(); it!=currlist.end(); ++it) + { + train.back().push_back(*it); + } + } else + { + for (vector< Ptr >::iterator it=currlist.begin(); it!=currlist.end(); ++it) + { + test.back().push_back(*it); + } + } + } + } +} + +void FR_adienceImp::loadDataset(const string &path) +{ + vector< Ptr > fileList[5]; + for (unsigned int i=0; i<5; ++i) + { + char tmp[3]; + sprintf(tmp, "%u", i); + string filename(path+"fold_"+string(tmp)+"_data.txt"); + + loadFile(filename, fileList[i]); + } + cv5ToSplits(fileList); + + for (unsigned int i=0; i<5; ++i) + { + char tmp[3]; + sprintf(tmp, "%u", i); + string filename(path+"fold_frontal_"+string(tmp)+"_data.txt"); + + fileList[i].clear(); + loadFile(filename, fileList[i]); + } + cv5ToSplits(fileList); +} + +Ptr FR_adience::create() +{ + return Ptr(new FR_adienceImp); +} + +} +}