track_alov.cpp 10.8 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 44 45
/*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_alov.hpp"

#include <sys/stat.h>
#include <opencv2/core.hpp>
46
#include "opencv2/imgcodecs.hpp"
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 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 177 178 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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384

using namespace std;

namespace cv
{
namespace datasets
{

class TRACK_alovImpl : public TRACK_alov
{
public:
    //Constructor
    TRACK_alovImpl()
    {
        activeDatasetID = 1;
        frameCounter = 0;
    }
    //Destructor
    virtual ~TRACK_alovImpl() {}

    //Load Dataset
    virtual void load(const string &path);
    virtual void loadAnnotatedOnly(const std::string &path);

protected:
    virtual int getDatasetsNum();

    virtual int getDatasetLength(int id);

    virtual bool initDataset(int id);

    virtual bool getNextFrame(Mat &frame);
    virtual bool getFrame(Mat &frame, int datasetID, int frameID);

    virtual vector <Point2f> getNextGT();
    virtual vector <Point2f> getGT(int datasetID, int frameID);

    void loadDataset(const string &path);
    void loadDatasetAnnotatedOnly(const string &path);

    string fullFramePath(string rootPath, int sectionID, int videoID, int frameID);
    string fullAnnoPath(string rootPath, int sectionID, int videoID);
};


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

void TRACK_alovImpl::loadAnnotatedOnly(const string &path)
{
    loadDatasetAnnotatedOnly(path);
}

string TRACK_alovImpl::fullFramePath(string rootPath, int sectionID, int videoID, int frameID)
{
    string out;
    char videoNum[9];
    sprintf(videoNum, "%u", videoID+1);
    char frameNum[9];
    sprintf(frameNum, "%u", frameID);
    out = rootPath + "/imagedata++/" + sectionNames[sectionID] + "/" + sectionNames[sectionID] + "_video";

    for (unsigned int i = 0; i < 5 - strlen(videoNum); ++i)
    {
        out += "0";
    }

    out += videoNum;
    out += "/";

    for (unsigned int i = 0; i < 8 - strlen(frameNum); ++i)
    {
        out += "0";
    }

    out += frameNum;
    out += ".jpg";
    return out;
}

string TRACK_alovImpl::fullAnnoPath(string rootPath, int sectionID, int videoID)
{
    string out;
    char videoNum[9];
    sprintf(videoNum, "%u", videoID+1);
    out = rootPath + "/alov300++_rectangleAnnotation_full/" + sectionNames[sectionID] + "/" + sectionNames[sectionID] + "_video";

    for (unsigned int i = 0; i < 5 - strlen(videoNum); ++i)
    {
        out += "0";
    }

    out += videoNum;
    out += ".ann";
    return out;
}

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

void TRACK_alovImpl::loadDataset(const string &rootPath)
{
    vector <int> datasetsLengths;

    printf("ALOV300++ Dataset Initialization...\n");

    //Load frames
    //Loop for all sections of ALOV300++ (14 sections)
    for (int i = 0; i < 14; i++)
    {
        //Loop for all videos in section
        for (int k = 0; k < sectionSizes[i]; k++)
        {
            vector <Ptr<TRACK_alovObj> > objects;

            //Make a list of datasets lengths
            int currFrameID = 0;

            for (;;)
            {
                currFrameID++;
                string fullPath = fullFramePath(rootPath, i, k, currFrameID);
                if (!fileExists(fullPath))
                    break;

                //Make ALOV300++ Object
                Ptr<TRACK_alovObj> currObj(new TRACK_alovObj);
                currObj->imagePath = fullPath;
                currObj->id = currFrameID;

                currObj->gtbb.push_back(Point2d(0, 0));
                currObj->gtbb.push_back(Point2d(0, 0));
                currObj->gtbb.push_back(Point2d(0, 0));
                currObj->gtbb.push_back(Point2d(0, 0));

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

            }

            datasetsLengths.push_back(currFrameID - 1);
            data.push_back(objects);
        }
    }

    //Load annotations
    //Loop for all sections of ALOV300++ (14 sections)
    int currDatasetID = 0;
    for (int i = 0; i < 14; i++)
    {
        //Loop for all videos in section
        for (int k = 0; k < sectionSizes[i]; k++)
        {
            currDatasetID++;

            //Open dataset's ground truth (annotation) file
            string annoPath = fullAnnoPath(rootPath, i, k);
            ifstream annoList(annoPath.c_str());
            if (!annoList.is_open())
            {
                printf("Error: Can't open annotation file *.ANN!!!\n");
                break;
            }

            //Ground Truth data
            int n = 0;
            double    x1 = 0, y1 = 0,
                x2 = 0, y2 = 0,
                x3 = 0, y3 = 0,
                x4 = 0, y4 = 0;

            do
            {
                //Make ALOV300++ Object
                string tmp;

                getline(annoList, tmp);
                std::istringstream in(tmp);
                in >> n >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4;

                Ptr<TRACK_alovObj> currObj = data[currDatasetID-1][n-1];

                currObj->gtbb.clear();
                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));

            } while (annoList.good());
        }
    }

    return;
}

void TRACK_alovImpl::loadDatasetAnnotatedOnly(const string &rootPath)
{
    vector <int> datasetsLengths;
    int currDatasetID = 0;

    printf("ALOV300++ Annotated Dataset Initialization...\n");

    //Loop for all sections of ALOV300++ (14 sections)
    for (int i = 0; i < 14; i++)
    {
        //Loop for all videos in section
        for (int k = 0; k < sectionSizes[i]; k++)
        {
            vector <Ptr<TRACK_alovObj> > objects;
            currDatasetID++;

            //Open dataset's ground truth (annotation) file
            string annoPath = fullAnnoPath(rootPath, i, k);
            ifstream annoList(annoPath.c_str());
            if (!annoList.is_open())
            {
                printf("Error: Can't open annotation file *.ANN!!!\n");
                break;
            }

            int framesNum = 0;

            do
            {
                //Make  ALOV300++ Object
                Ptr<TRACK_alovObj> currObj(new TRACK_alovObj);
                string tmp;
                framesNum++;

                //Ground Truth data
                int    n = 0;
                double x1 = 0, y1 = 0,
                    x2 = 0, y2 = 0,
                    x3 = 0, y3 = 0,
                    x4 = 0, y4 = 0;

                getline(annoList, tmp);
                std::istringstream in(tmp);
                in >> n >> 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));

                string fullPath = fullFramePath(rootPath, i, k, n);
                if (!fileExists(fullPath))
                    break;

                currObj->imagePath = fullPath;
                currObj->id = n;

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

            } while (annoList.good());

            datasetsLengths.push_back(framesNum-1);
            data.push_back(objects);
        }
    }

    return;
}

int TRACK_alovImpl::getDatasetsNum()
{
    return (int)(data.size());
}

int TRACK_alovImpl::getDatasetLength(int id)
{
    if (id > 0 && id <= (int)data.size())
        return (int)(data[id - 1].size());
    else
    {
        printf("Dataset ID is out of range...\nAllowed IDs are: 1~%d\n", (int)data.size());
        return -1;
    }
}

bool TRACK_alovImpl::initDataset(int id)
{
    if (id > 0 && id <= (int)data.size())
    {
        activeDatasetID = id;
        return true;
    }
    else
    {
        printf("Dataset ID is out of range...\nAllowed IDs are: 1~%d\n", (int)data.size());
        return false;
    }
}

bool  TRACK_alovImpl::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();
}

bool  TRACK_alovImpl::getFrame(Mat &frame, int datasetID, int frameID)
{
    if (frameID > (int)data[datasetID-1].size())
        return false;
    string imgPath = data[datasetID-1][frameID-1]->imagePath;
    frame = imread(imgPath);
    return !frame.empty();
}

Ptr<TRACK_alov> TRACK_alov::create()
{
    return Ptr<TRACK_alovImpl>(new TRACK_alovImpl);
}

vector <Point2f> TRACK_alovImpl::getNextGT()
{
    Ptr <TRACK_alovObj> currObj = data[activeDatasetID - 1][frameCounter - 1];
    return currObj->gtbb;
}

vector <Point2f> TRACK_alovImpl::getGT(int datasetID, int frameID)
{
    Ptr <TRACK_alovObj> currObj = data[datasetID - 1][frameID - 1];
    return currObj->gtbb;
}

}
}