track_vot.cpp 7.99 KB
Newer Older
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 42 43
/*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/track_vot.hpp"

44 45
#include <sys/stat.h>
#include <opencv2/core.hpp>
46
#include "opencv2/imgcodecs.hpp"
47

48 49 50 51
using namespace std;

namespace cv
{
Vladimir's avatar
Vladimir committed
52 53 54
    namespace datasets
    {

55
        class TRACK_votImpl CV_FINAL : public TRACK_vot
Vladimir's avatar
Vladimir committed
56 57 58 59 60 61 62 63 64
        {
        public:
            //Constructor
            TRACK_votImpl()
            {
                activeDatasetID = 1;
                frameCounter = 0;
            }
            //Destructor
65
            virtual ~TRACK_votImpl() CV_OVERRIDE {}
Vladimir's avatar
Vladimir committed
66 67

            //Load Dataset
68
            virtual void load(const string &path) CV_OVERRIDE;
Vladimir's avatar
Vladimir committed
69

70
		protected:
71
            virtual int getDatasetsNum() CV_OVERRIDE;
Vladimir's avatar
Vladimir committed
72

73
            virtual int getDatasetLength(int id) CV_OVERRIDE;
Vladimir's avatar
Vladimir committed
74

75
            virtual bool initDataset(int id) CV_OVERRIDE;
Vladimir's avatar
Vladimir committed
76

77
            virtual bool getNextFrame(Mat &frame) CV_OVERRIDE;
Vladimir's avatar
Vladimir committed
78

79
            virtual vector <Point2d> getGT() CV_OVERRIDE;
Vladimir's avatar
Vladimir committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93

            void loadDataset(const string &path);

            string numberToString(int number);
        };

        void TRACK_votImpl::load(const string &path)
        {
            loadDataset(path);
        }

        string TRACK_votImpl::numberToString(int number)
        {
            string out;
94
            char numberStr[20];
Vladimir's avatar
Vladimir committed
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 121 122 123 124 125 126 127 128 129 130 131 132 133
            sprintf(numberStr, "%u", number);
            for (unsigned int i = 0; i < 8 - strlen(numberStr); ++i)
            {
                out += "0";
            }
            out += numberStr;
            return out;
        }

        inline bool fileExists(const std::string& name)
        {
            struct stat buffer;
            return (stat(name.c_str(), &buffer) == 0);
        }

        void TRACK_votImpl::loadDataset(const string &rootPath)
        {
            string nameListPath = rootPath + "/list.txt";
            ifstream namesList(nameListPath.c_str());
            vector <int> datasetsLengths;
            string datasetName;

            if (namesList.is_open())
            {
                int currDatasetID = 0;

                //All datasets/folders loop
                while (getline(namesList, datasetName))
                {
                    currDatasetID++;
                    vector <Ptr<TRACK_votObj> > objects;

                    //All frames/images loop
                    Ptr<TRACK_votObj> currDataset(new TRACK_votObj);

                    //Open dataset's ground truth file
                    string gtListPath = rootPath + "/" + datasetName + "/groundtruth.txt";
                    ifstream gtList(gtListPath.c_str());
                    if (!gtList.is_open())
134
                        printf("Error to open groundtruth.txt!!!");
Vladimir's avatar
Vladimir committed
135 136

                    //Make a list of datasets lengths
137
                    int currFrameID = 0;
Vladimir's avatar
Vladimir committed
138
                    if (currDatasetID == 0)
139
                        printf("VOT Dataset Initialization...\n");
Vladimir's avatar
Vladimir committed
140 141 142 143 144 145 146 147 148 149 150 151 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
                    bool trFLG = true;
                    do
                    {
                        currFrameID++;
                        string fullPath = rootPath + "/" + datasetName + "/" + numberToString(currFrameID) + ".jpg";
                        if (!fileExists(fullPath))
                            break;

                        //Make VOT Object
                        Ptr<TRACK_votObj> currObj(new TRACK_votObj);
                        currObj->imagePath = fullPath;
                        currObj->id = currFrameID;

                        //Get Ground Truth data
                        double	x1 = 0, y1 = 0,
                            x2 = 0, y2 = 0,
                            x3 = 0, y3 = 0,
                            x4 = 0, y4 = 0;
                        string tmp;
                        getline(gtList, tmp);
                        sscanf(tmp.c_str(), "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
                        currObj->gtbb.push_back(Point2d(x1, y1));
                        currObj->gtbb.push_back(Point2d(x2, y2));
                        currObj->gtbb.push_back(Point2d(x3, y3));
                        currObj->gtbb.push_back(Point2d(x4, y4));

                        //Add object to storage
                        objects.push_back(currObj);

                    } while (trFLG);

                    datasetsLengths.push_back(currFrameID - 1);
                    data.push_back(objects);
                }
            }
            else
            {
177
                printf("Couldn't find a *list.txt* in VOT Dataset folder!!!");
Vladimir's avatar
Vladimir committed
178 179 180 181 182 183 184 185
            }

            namesList.close();
            return;
        }

        int TRACK_votImpl::getDatasetsNum()
        {
186
            return (int)(data.size());
Vladimir's avatar
Vladimir committed
187 188 189 190 191
        }

        int TRACK_votImpl::getDatasetLength(int id)
        {
            if (id > 0 && id <= (int)data.size())
192
                return (int)(data[id - 1].size());
Vladimir's avatar
Vladimir committed
193 194
            else
            {
195
                printf("Dataset ID is out of range...\nAllowed IDs are: 1~%d\n", (int)data.size());
Vladimir's avatar
Vladimir committed
196 197 198 199 200 201 202 203 204
                return -1;
            }
        }

        bool TRACK_votImpl::initDataset(int id)
        {
            if (id > 0 && id <= (int)data.size())
            {
                activeDatasetID = id;
205
                frameCounter = 0;
Vladimir's avatar
Vladimir committed
206 207 208 209
                return true;
            }
            else
            {
210
                printf("Dataset ID is out of range...\nAllowed IDs are: 1~%d\n", (int)data.size());
Vladimir's avatar
Vladimir committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
                return false;
            }
        }

        bool  TRACK_votImpl::getNextFrame(Mat &frame)
        {
            if (frameCounter >= (int)data[activeDatasetID - 1].size())
                return false;
            string imgPath = data[activeDatasetID - 1][frameCounter]->imagePath;
            frame = imread(imgPath);
            frameCounter++;
            return !frame.empty();
        }

        Ptr<TRACK_vot> TRACK_vot::create()
        {
            return Ptr<TRACK_votImpl>(new TRACK_votImpl);
        }

        vector <Point2d> TRACK_votImpl::getGT()
        {
            Ptr <TRACK_votObj> currObj = data[activeDatasetID - 1][frameCounter - 1];
            return currObj->gtbb;
        }

    }
237
}