]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - main_vot.cpp
Merge branch 'master' of https://github.com/Shanigen/kcf
[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;
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             {0,           0,                 0,  0 }
24         };
25
26         int c = getopt_long(argc, argv, "dhv::o:",
27                         long_options, &option_index);
28         if (c == -1)
29             break;
30
31         switch (c) {
32         case 'd':
33             tracker.m_debug = true;
34             break;
35         case 'h':
36             std::cerr << "Usage: \n"
37                       << argv[0] << " [options]\n"
38                       << argv[0] << " [options] <directory>\n"
39                       << argv[0] << " [options] <path/to/region.txt or groundtruth.txt> <path/to/images.txt> [path/to/output.txt]\n"
40                       << "Options:\n"
41                       << " --visualize | -v [delay_ms]\n"
42                       << " --output    | -o <outout.txt>\n"
43                       << " --debug     | -d\n";
44             exit(0);
45             break;
46         case 'o':
47             output = optarg;
48             break;
49         case 'v':
50             visualize_delay = optarg ? atol(optarg) : 1;
51             break;
52         }
53     }
54
55     switch (argc - optind) {
56     case 1:
57         if (chdir(argv[optind]) == -1) {
58             perror(argv[optind]);
59             exit(1);
60         }
61         // Fall through
62     case 0:
63         region = access("groundtruth.txt", F_OK) == 0 ? "groundtruth.txt" : "region.txt";
64         images = "images.txt";
65         if (output.empty())
66             output = "output.txt";
67         break;
68     case 2:
69         // Fall through
70     case 3:
71         region = std::string(argv[optind + 0]);
72         images = std::string(argv[optind + 1]);
73         if (output.empty()) {
74             if ((argc - optind) == 3)
75                 output = std::string(argv[optind + 2]);
76             else
77                 output = std::string(dirname(argv[optind + 0])) + "/output.txt";
78         }
79         break;
80     default:
81         std::cerr << "Too many arguments\n";
82         return 1;
83     }
84     VOT vot_io(region, images, output);
85
86     cv::Mat image;
87
88     //img = firts frame, initPos = initial position in the first frame
89     cv::Rect init_rect = vot_io.getInitRectangle();
90     vot_io.outputBoundingBox(init_rect);
91     vot_io.getNextImage(image);
92
93     tracker.init(image, init_rect);
94
95     BBox_c bb;
96     double avg_time = 0.;
97     int frames = 0;
98     while (vot_io.getNextImage(image) == 1){
99         double time_profile_counter = cv::getCPUTickCount();
100         tracker.track(image);
101         time_profile_counter = cv::getCPUTickCount() - time_profile_counter;
102          std::cout << "  -> speed : " <<  time_profile_counter/((double)cvGetTickFrequency()*1000) << "ms. per frame" << std::endl;
103         avg_time += time_profile_counter/((double)cvGetTickFrequency()*1000);
104         frames++;
105
106         bb = tracker.getBBox();
107         vot_io.outputBoundingBox(cv::Rect(bb.cx - bb.w/2., bb.cy - bb.h/2., bb.w, bb.h));
108
109         if (visualize_delay >= 0) {
110             cv::rectangle(image, cv::Rect(bb.cx - bb.w/2., bb.cy - bb.h/2., bb.w, bb.h), CV_RGB(0,255,0), 2);
111             cv::imshow("output", image);
112             int ret = cv::waitKey(visualize_delay);
113             if (visualize_delay > 0 && ret != -1 && ret != 255)
114                 break;
115         }
116
117 //        std::stringstream s;
118 //        std::string ss;
119 //        int countTmp = frames;
120 //        s << "imgs" << "/img" << (countTmp/10000);
121 //        countTmp = countTmp%10000;
122 //        s << (countTmp/1000);
123 //        countTmp = countTmp%1000;
124 //        s << (countTmp/100);
125 //        countTmp = countTmp%100;
126 //        s << (countTmp/10);
127 //        countTmp = countTmp%10;
128 //        s << (countTmp);
129 //        s << ".jpg";
130 //        s >> ss;
131 //        //set image output parameters
132 //        std::vector<int> compression_params;
133 //        compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
134 //        compression_params.push_back(90);
135 //        cv::imwrite(ss.c_str(), image, compression_params);
136     }
137
138     std::cout << "Average processing speed " << avg_time/frames <<  "ms. (" << 1./(avg_time/frames)*1000 << " fps)" << std::endl;
139
140     return EXIT_SUCCESS;
141 }