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