]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - main_vot.cpp
Use fixed format for output numbers
[hercules2020/kcf.git] / main_vot.cpp
1 #include <stdlib.h>
2 #include <getopt.h>
3 #include <libgen.h>
4 #include <unistd.h>
5 #include <iomanip>
6
7 #include "kcf.h"
8 #include "vot.hpp"
9
10 double calcAccuracy(std::string line, cv::Rect bb_rect, cv::Rect &groundtruth_rect)
11 {
12     std::vector<float> numbers;
13     std::istringstream s( line );
14     float x;
15     char ch;
16
17     while (s >> x){
18         numbers.push_back(x);
19         s >> ch;
20     }
21     double x1 = std::min(numbers[0], std::min(numbers[2], std::min(numbers[4], numbers[6])));
22     double x2 = std::max(numbers[0], std::max(numbers[2], std::max(numbers[4], numbers[6])));
23     double y1 = std::min(numbers[1], std::min(numbers[3], std::min(numbers[5], numbers[7])));
24     double y2 = std::max(numbers[1], std::max(numbers[3], std::max(numbers[5], numbers[7])));
25
26     groundtruth_rect = cv::Rect(x1, y1, x2-x1, y2-y1);
27
28     double rects_intersection = (groundtruth_rect & bb_rect).area();
29     double rects_union = (groundtruth_rect | bb_rect).area();
30     double accuracy = rects_intersection/rects_union;
31
32     return accuracy;
33 }
34
35 int main(int argc, char *argv[])
36 {
37     //load region, images and prepare for output
38     std::string region, images, output;
39     int visualize_delay = -1, fit_size_x = -1, fit_size_y = -1;
40     KCF_Tracker tracker;
41
42     while (1) {
43         int option_index = 0;
44         static struct option long_options[] = {
45             {"debug",     no_argument,       0,  'd' },
46             {"help",      no_argument,       0,  'h' },
47             {"output",    required_argument, 0,  'o' },
48             {"visualize", optional_argument, 0,  'v' },
49             {"fit",       optional_argument, 0,  'f' },
50             {0,           0,                 0,  0 }
51         };
52
53         int c = getopt_long(argc, argv, "dhv::f::o:",
54                         long_options, &option_index);
55         if (c == -1)
56             break;
57
58         switch (c) {
59         case 'd':
60             tracker.m_debug = true;
61             break;
62         case 'h':
63             std::cerr << "Usage: \n"
64                       << argv[0] << " [options]\n"
65                       << argv[0] << " [options] <directory>\n"
66                       << argv[0] << " [options] <path/to/region.txt or groundtruth.txt> <path/to/images.txt> [path/to/output.txt]\n"
67                       << "Options:\n"
68                       << " --visualize | -v[delay_ms]\n"
69                       << " --output    | -o <output.txt>\n"
70                       << " --debug     | -d\n"
71                       << " --fit       | -f[WxH]\n";
72             exit(0);
73             break;
74         case 'o':
75             output = optarg;
76             break;
77         case 'v':
78             visualize_delay = optarg ? atol(optarg) : 1;
79             break;
80         case 'f':
81             std::string sizes = optarg ? optarg : "128x128";
82             std::string delimiter = "x";
83             size_t pos = sizes.find(delimiter);
84             std::string first_argument = sizes.substr(0, pos);
85             sizes.erase(0, pos + delimiter.length());
86
87             fit_size_x = stol(first_argument);
88             fit_size_y = stol(sizes);
89             break;
90         }
91     }
92
93     switch (argc - optind) {
94     case 1:
95         if (chdir(argv[optind]) == -1) {
96             perror(argv[optind]);
97             exit(1);
98         }
99         // Fall through
100     case 0:
101         region = access("groundtruth.txt", F_OK) == 0 ? "groundtruth.txt" : "region.txt";
102         images = "images.txt";
103         if (output.empty())
104             output = "output.txt";
105         break;
106     case 2:
107         // Fall through
108     case 3:
109         region = std::string(argv[optind + 0]);
110         images = std::string(argv[optind + 1]);
111         if (output.empty()) {
112             if ((argc - optind) == 3)
113                 output = std::string(argv[optind + 2]);
114             else
115                 output = std::string(dirname(argv[optind + 0])) + "/output.txt";
116         }
117         break;
118     default:
119         std::cerr << "Too many arguments\n";
120         return 1;
121     }
122     VOT vot_io(region, images, output);
123
124     // if groundtruth.txt is used use intersection over union (IOU) to calculate tracker accuracy
125     std::ifstream groundtruth_stream;
126     if (region.compare("groundtruth.txt") == 0) {
127         groundtruth_stream.open(region.c_str());
128         std::string line;
129         std::getline(groundtruth_stream, line);
130     }
131
132     cv::Mat image;
133
134     //img = firts frame, initPos = initial position in the first frame
135     cv::Rect init_rect = vot_io.getInitRectangle();
136     vot_io.outputBoundingBox(init_rect);
137     vot_io.getNextImage(image);
138
139     tracker.init(image, init_rect, fit_size_x, fit_size_y);
140
141     BBox_c bb;
142     cv::Rect bb_rect;
143     double avg_time = 0., sum_accuracy = 0.;
144     int frames = 0;
145
146     std::cout << std::fixed << std::setprecision(2);
147
148     while (vot_io.getNextImage(image) == 1){
149         double time_profile_counter = cv::getCPUTickCount();
150         tracker.track(image);
151         time_profile_counter = cv::getCPUTickCount() - time_profile_counter;
152          std::cout << "  -> speed : " <<  time_profile_counter/((double)cvGetTickFrequency()*1000) << "ms per frame, "
153                       "response : " << tracker.getFilterResponse();
154         avg_time += time_profile_counter/((double)cvGetTickFrequency()*1000);
155         frames++;
156
157         bb = tracker.getBBox();
158         bb_rect = cv::Rect(bb.cx - bb.w/2., bb.cy - bb.h/2., bb.w, bb.h);
159         vot_io.outputBoundingBox(bb_rect);
160
161         if (groundtruth_stream.is_open()) {
162             std::string line;
163             std::getline(groundtruth_stream, line);
164
165             cv::Rect groundtruthRect;
166             double accuracy = calcAccuracy(line, bb_rect, groundtruthRect);
167             if (visualize_delay >= 0)
168                 cv::rectangle(image, groundtruthRect, CV_RGB(255, 0,0), 1);
169             std::cout << ", accuracy: " << accuracy;
170             sum_accuracy += accuracy;
171         }
172
173         std::cout << std::endl;
174
175         if (visualize_delay >= 0) {
176             cv::rectangle(image, bb_rect, CV_RGB(0,255,0), 2);
177             cv::imshow("output", image);
178             int ret = cv::waitKey(visualize_delay);
179             if (visualize_delay > 0 && ret != -1 && ret != 255)
180                 break;
181         }
182
183 //        std::stringstream s;
184 //        std::string ss;
185 //        int countTmp = frames;
186 //        s << "imgs" << "/img" << (countTmp/10000);
187 //        countTmp = countTmp%10000;
188 //        s << (countTmp/1000);
189 //        countTmp = countTmp%1000;
190 //        s << (countTmp/100);
191 //        countTmp = countTmp%100;
192 //        s << (countTmp/10);
193 //        countTmp = countTmp%10;
194 //        s << (countTmp);
195 //        s << ".jpg";
196 //        s >> ss;
197 //        //set image output parameters
198 //        std::vector<int> compression_params;
199 //        compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
200 //        compression_params.push_back(90);
201 //        cv::imwrite(ss.c_str(), image, compression_params);
202     }
203
204     std::cout << "Average processing speed: " << avg_time / frames << "ms (" << 1. / (avg_time / frames) * 1000 << " fps)";
205     if (groundtruth_stream.is_open()) {
206         std::cout << "; Average accuracy: " << sum_accuracy/frames << std::endl;
207         groundtruth_stream.close();
208     }
209     std::cout << std::endl;
210
211     return EXIT_SUCCESS;
212 }