]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - main_vot.cpp
travis: Print backtraces on crashes or assertion failures
[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             {"visualDebug", no_argument, 0, 'p'},
47             {"help",      no_argument,       0,  'h' },
48             {"output",    required_argument, 0,  'o' },
49             {"visualize", optional_argument, 0,  'v' },
50             {"fit",       optional_argument, 0,  'f' },
51             {0,           0,                 0,  0 }
52         };
53
54         int c = getopt_long(argc, argv, "dphv::f::o:", 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 'p':
63             tracker.m_visual_debug = true;
64             visualize_delay = 500;
65             break;
66         case 'h':
67             std::cerr << "Usage: \n"
68                       << argv[0] << " [options]\n"
69                       << argv[0] << " [options] <directory>\n"
70                       << argv[0] << " [options] <path/to/region.txt or groundtruth.txt> <path/to/images.txt> [path/to/output.txt]\n"
71                       << "Options:\n"
72                       << " --visualize | -v[delay_ms]\n"
73                       << " --output    | -o <output.txt>\n"
74                       << " --debug     | -d\n"
75                       << " --fit       | -f[W[xH]]\n";
76             exit(0);
77             break;
78         case 'o':
79             output = optarg;
80             break;
81         case 'v':
82             visualize_delay = optarg ? atol(optarg) : 1;
83             break;
84         case 'f':
85             if (!optarg) {
86                 fit_size_x = fit_size_y = 0;
87             } else {
88                 char tail;
89                 if (sscanf(optarg, "%d%c", &fit_size_x, &tail) == 1) {
90                     fit_size_y = fit_size_x;
91                 } else if (sscanf(optarg, "%dx%d%c", &fit_size_x, &fit_size_y, &tail) != 2) {
92                     fprintf(stderr, "Cannot parse -f argument: %s\n", optarg);
93                     return 1;
94                 }
95             }
96             break;
97         }
98     }
99
100     switch (argc - optind) {
101     case 1:
102         if (chdir(argv[optind]) == -1) {
103             perror(argv[optind]);
104             exit(1);
105         }
106         // Fall through
107     case 0:
108         region = access("groundtruth.txt", F_OK) == 0 ? "groundtruth.txt" : "region.txt";
109         images = "images.txt";
110         if (output.empty())
111             output = "output.txt";
112         break;
113     case 2:
114         // Fall through
115     case 3:
116         region = std::string(argv[optind + 0]);
117         images = std::string(argv[optind + 1]);
118         if (output.empty()) {
119             if ((argc - optind) == 3)
120                 output = std::string(argv[optind + 2]);
121             else
122                 output = std::string(dirname(argv[optind + 0])) + "/output.txt";
123         }
124         break;
125     default:
126         std::cerr << "Too many arguments\n";
127         return 1;
128     }
129     VOT vot_io(region, images, output);
130
131     // if groundtruth.txt is used use intersection over union (IOU) to calculate tracker accuracy
132     std::ifstream groundtruth_stream;
133     if (region.compare("groundtruth.txt") == 0) {
134         groundtruth_stream.open(region.c_str());
135         std::string line;
136         std::getline(groundtruth_stream, line);
137     }
138
139     cv::Mat image;
140
141     //img = firts frame, initPos = initial position in the first frame
142     cv::Rect init_rect = vot_io.getInitRectangle();
143     vot_io.outputBoundingBox(init_rect);
144     vot_io.getNextImage(image);
145
146     tracker.init(image, init_rect, fit_size_x, fit_size_y);
147
148     BBox_c bb;
149     cv::Rect bb_rect;
150     double avg_time = 0., sum_accuracy = 0.;
151     int frames = 0;
152
153     std::cout << std::fixed << std::setprecision(2);
154
155     while (vot_io.getNextImage(image) == 1){
156         double time_profile_counter = cv::getCPUTickCount();
157         tracker.track(image);
158         time_profile_counter = cv::getCPUTickCount() - time_profile_counter;
159          std::cout << "  -> speed : " <<  time_profile_counter/((double)cvGetTickFrequency()*1000) << "ms per frame, "
160                       "response : " << tracker.getFilterResponse();
161         avg_time += time_profile_counter/((double)cvGetTickFrequency()*1000);
162         frames++;
163
164         bb = tracker.getBBox();
165         bb_rect = cv::Rect(bb.cx - bb.w/2., bb.cy - bb.h/2., bb.w, bb.h);
166         vot_io.outputBoundingBox(bb_rect);
167
168         if (groundtruth_stream.is_open()) {
169             std::string line;
170             std::getline(groundtruth_stream, line);
171
172             cv::Rect groundtruthRect;
173             double accuracy = calcAccuracy(line, bb_rect, groundtruthRect);
174             if (visualize_delay >= 0)
175                 cv::rectangle(image, groundtruthRect, CV_RGB(255, 0,0), 1);
176             std::cout << ", accuracy: " << accuracy;
177             sum_accuracy += accuracy;
178         }
179
180         std::cout << std::endl;
181
182         if (visualize_delay >= 0) {
183             cv::Point pt(bb.cx, bb.cy);
184             cv::Size size(bb.w, bb.h);
185             cv::RotatedRect rotatedRectangle(pt, size, bb.a);
186
187             cv::Point2f vertices[4];
188             rotatedRectangle.points(vertices);
189
190             for (int i = 0; i < 4; i++)
191                 cv::line(image, vertices[i], vertices[(i + 1) % 4], cv::Scalar(0, 255, 0), 2);
192             cv::imshow("KCF output", image);
193             int ret = cv::waitKey(visualize_delay);
194             if (visualize_delay > 0 && ret != -1 && ret < 128)
195                 break;
196         }
197
198 //        std::stringstream s;
199 //        std::string ss;
200 //        int countTmp = frames;
201 //        s << "imgs" << "/img" << (countTmp/10000);
202 //        countTmp = countTmp%10000;
203 //        s << (countTmp/1000);
204 //        countTmp = countTmp%1000;
205 //        s << (countTmp/100);
206 //        countTmp = countTmp%100;
207 //        s << (countTmp/10);
208 //        countTmp = countTmp%10;
209 //        s << (countTmp);
210 //        s << ".jpg";
211 //        s >> ss;
212 //        //set image output parameters
213 //        std::vector<int> compression_params;
214 //        compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
215 //        compression_params.push_back(90);
216 //        cv::imwrite(ss.c_str(), image, compression_params);
217     }
218
219     std::cout << "Average processing speed: " << avg_time / frames << "ms (" << 1. / (avg_time / frames) * 1000 << " fps)";
220     if (groundtruth_stream.is_open()) {
221         std::cout << "; Average accuracy: " << sum_accuracy/frames << std::endl;
222         groundtruth_stream.close();
223     }
224     std::cout << std::endl;
225
226     return EXIT_SUCCESS;
227 }