1 /*M/////////////////////////////////////////////////////////////////////////////////////// 2 // 3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. 4 // 5 // By downloading, copying, installing or using the software you agree to this license. 6 // If you do not agree to this license, do not download, install, 7 // copy or use the software. 8 // 9 // 10 // License Agreement 11 // For Open Source Computer Vision Library 12 // 13 // Copyright (C) 2014, Itseez Inc, all rights reserved. 14 // Third party copyrights are property of their respective owners. 15 // 16 // Redistribution and use in source and binary forms, with or without modification, 17 // are permitted provided that the following conditions are met: 18 // 19 // * Redistribution's of source code must retain the above copyright notice, 20 // this list of conditions and the following disclaimer. 21 // 22 // * Redistribution's in binary form must reproduce the above copyright notice, 23 // this list of conditions and the following disclaimer in the documentation 24 // and/or other materials provided with the distribution. 25 // 26 // * The name of the copyright holders may not be used to endorse or promote products 27 // derived from this software without specific prior written permission. 28 // 29 // This software is provided by the copyright holders and contributors "as is" and 30 // any express or implied warranties, including, but not limited to, the implied 31 // warranties of merchantability and fitness for a particular purpose are disclaimed. 32 // In no event shall the Itseez Inc or contributors be liable for any direct, 33 // indirect, incidental, special, exemplary, or consequential damages 34 // (including, but not limited to, procurement of substitute goods or services; 35 // loss of use, data, or profits; or business interruption) however caused 36 // and on any theory of liability, whether in contract, strict liability, 37 // or tort (including negligence or otherwise) arising in any way out of 38 // the use of this software, even if advised of the possibility of such damage. 39 // 40 //M*/ 41 42 #include "opencv2/datasets/track_vot.hpp" 43 44 #include <sys/stat.h> 45 #include <opencv2/core.hpp> 46 #include "opencv2/imgcodecs.hpp" 47 48 using namespace std; 49 50 namespace cv 51 { 52 namespace datasets 53 { 54 55 class TRACK_votImpl CV_FINAL : public TRACK_vot 56 { 57 public: 58 //Constructor TRACK_votImpl()59 TRACK_votImpl() 60 { 61 activeDatasetID = 1; 62 frameCounter = 0; 63 } 64 //Destructor ~TRACK_votImpl()65 virtual ~TRACK_votImpl() CV_OVERRIDE {} 66 67 //Load Dataset 68 virtual void load(const string &path) CV_OVERRIDE; 69 70 protected: 71 virtual int getDatasetsNum() CV_OVERRIDE; 72 73 virtual int getDatasetLength(int id) CV_OVERRIDE; 74 75 virtual bool initDataset(int id) CV_OVERRIDE; 76 77 virtual bool getNextFrame(Mat &frame) CV_OVERRIDE; 78 79 virtual vector <Point2d> getGT() CV_OVERRIDE; 80 81 void loadDataset(const string &path); 82 83 string numberToString(int number); 84 }; 85 load(const string & path)86 void TRACK_votImpl::load(const string &path) 87 { 88 loadDataset(path); 89 } 90 numberToString(int number)91 string TRACK_votImpl::numberToString(int number) 92 { 93 string out; 94 char numberStr[20]; 95 sprintf(numberStr, "%u", number); 96 for (unsigned int i = 0; i < 8 - strlen(numberStr); ++i) 97 { 98 out += "0"; 99 } 100 out += numberStr; 101 return out; 102 } 103 fileExists(const std::string & name)104 inline bool fileExists(const std::string& name) 105 { 106 struct stat buffer; 107 return (stat(name.c_str(), &buffer) == 0); 108 } 109 loadDataset(const string & rootPath)110 void TRACK_votImpl::loadDataset(const string &rootPath) 111 { 112 string nameListPath = rootPath + "/list.txt"; 113 ifstream namesList(nameListPath.c_str()); 114 vector <int> datasetsLengths; 115 string datasetName; 116 117 if (namesList.is_open()) 118 { 119 int currDatasetID = 0; 120 121 //All datasets/folders loop 122 while (getline(namesList, datasetName)) 123 { 124 currDatasetID++; 125 vector <Ptr<TRACK_votObj> > objects; 126 127 //All frames/images loop 128 Ptr<TRACK_votObj> currDataset(new TRACK_votObj); 129 130 //Open dataset's ground truth file 131 string gtListPath = rootPath + "/" + datasetName + "/groundtruth.txt"; 132 ifstream gtList(gtListPath.c_str()); 133 if (!gtList.is_open()) 134 printf("Error to open groundtruth.txt!!!"); 135 136 //Make a list of datasets lengths 137 int currFrameID = 0; 138 if (currDatasetID == 0) 139 printf("VOT Dataset Initialization...\n"); 140 bool trFLG = true; 141 do 142 { 143 currFrameID++; 144 string fullPath = rootPath + "/" + datasetName + "/" + numberToString(currFrameID) + ".jpg"; 145 if (!fileExists(fullPath)) 146 break; 147 148 //Make VOT Object 149 Ptr<TRACK_votObj> currObj(new TRACK_votObj); 150 currObj->imagePath = fullPath; 151 currObj->id = currFrameID; 152 153 //Get Ground Truth data 154 double x1 = 0, y1 = 0, 155 x2 = 0, y2 = 0, 156 x3 = 0, y3 = 0, 157 x4 = 0, y4 = 0; 158 string tmp; 159 getline(gtList, tmp); 160 sscanf(tmp.c_str(), "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4); 161 currObj->gtbb.push_back(Point2d(x1, y1)); 162 currObj->gtbb.push_back(Point2d(x2, y2)); 163 currObj->gtbb.push_back(Point2d(x3, y3)); 164 currObj->gtbb.push_back(Point2d(x4, y4)); 165 166 //Add object to storage 167 objects.push_back(currObj); 168 169 } while (trFLG); 170 171 datasetsLengths.push_back(currFrameID - 1); 172 data.push_back(objects); 173 } 174 } 175 else 176 { 177 printf("Couldn't find a *list.txt* in VOT Dataset folder!!!"); 178 } 179 180 namesList.close(); 181 return; 182 } 183 getDatasetsNum()184 int TRACK_votImpl::getDatasetsNum() 185 { 186 return (int)(data.size()); 187 } 188 getDatasetLength(int id)189 int TRACK_votImpl::getDatasetLength(int id) 190 { 191 if (id > 0 && id <= (int)data.size()) 192 return (int)(data[id - 1].size()); 193 else 194 { 195 printf("Dataset ID is out of range...\nAllowed IDs are: 1~%d\n", (int)data.size()); 196 return -1; 197 } 198 } 199 initDataset(int id)200 bool TRACK_votImpl::initDataset(int id) 201 { 202 if (id > 0 && id <= (int)data.size()) 203 { 204 activeDatasetID = id; 205 frameCounter = 0; 206 return true; 207 } 208 else 209 { 210 printf("Dataset ID is out of range...\nAllowed IDs are: 1~%d\n", (int)data.size()); 211 return false; 212 } 213 } 214 getNextFrame(Mat & frame)215 bool TRACK_votImpl::getNextFrame(Mat &frame) 216 { 217 if (frameCounter >= (int)data[activeDatasetID - 1].size()) 218 return false; 219 string imgPath = data[activeDatasetID - 1][frameCounter]->imagePath; 220 frame = imread(imgPath); 221 frameCounter++; 222 return !frame.empty(); 223 } 224 create()225 Ptr<TRACK_vot> TRACK_vot::create() 226 { 227 return Ptr<TRACK_votImpl>(new TRACK_votImpl); 228 } 229 getGT()230 vector <Point2d> TRACK_votImpl::getGT() 231 { 232 Ptr <TRACK_votObj> currObj = data[activeDatasetID - 1][frameCounter - 1]; 233 return currObj->gtbb; 234 } 235 236 } 237 } 238