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