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