10 double calcAccuracy(std::string line, cv::Rect bb_rect, cv::Rect &groundtruth_rect)
12 std::vector<float> numbers;
13 std::istringstream s(line);
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])));
26 groundtruth_rect = cv::Rect(x1, y1, x2 - x1, y2 - y1);
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;
35 int main(int argc, char *argv[])
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;
44 static struct option long_options[] = {
45 {"debug", no_argument, 0, 'd' },
46 {"help", no_argument, 0, 'h' },
47 {"output", required_argument, 0, 'o' },
48 {"visualize", optional_argument, 0, 'v' },
49 {"fit", optional_argument, 0, 'f' },
53 int c = getopt_long(argc, argv, "dhv::f::o:", long_options, &option_index);
59 tracker.m_debug = true;
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"
67 << " --visualize | -v[delay_ms]\n"
68 << " --output | -o <output.txt>\n"
70 << " --fit | -f[WxH]\n";
77 visualize_delay = optarg ? atol(optarg) : 1;
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());
86 fit_size_x = stol(first_argument);
87 fit_size_y = stol(sizes);
92 switch (argc - optind) {
94 if (chdir(argv[optind]) == -1) {
100 region = access("groundtruth.txt", F_OK) == 0 ? "groundtruth.txt" : "region.txt";
101 images = "images.txt";
103 output = "output.txt";
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]);
114 output = std::string(dirname(argv[optind + 0])) + "/output.txt";
118 std::cerr << "Too many arguments\n";
121 VOT vot_io(region, images, output);
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());
128 std::getline(groundtruth_stream, line);
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);
138 tracker.init(image, init_rect, fit_size_x, fit_size_y);
142 double avg_time = 0., sum_accuracy = 0.;
145 std::cout << std::fixed << std::setprecision(2);
147 while (vot_io.getNextImage(image) == 1){
148 double time_profile_counter = cv::getCPUTickCount();
149 tracker.track(image);
150 time_profile_counter = cv::getCPUTickCount() - time_profile_counter;
151 std::cout << " -> speed : " << time_profile_counter/((double)cvGetTickFrequency()*1000) << "ms per frame, "
152 "response : " << tracker.getFilterResponse();
153 avg_time += time_profile_counter/((double)cvGetTickFrequency()*1000);
156 bb = tracker.getBBox();
157 bb_rect = cv::Rect(bb.cx - bb.w/2., bb.cy - bb.h/2., bb.w, bb.h);
158 vot_io.outputBoundingBox(bb_rect);
160 if (groundtruth_stream.is_open()) {
162 std::getline(groundtruth_stream, line);
164 cv::Rect groundtruthRect;
165 double accuracy = calcAccuracy(line, bb_rect, groundtruthRect);
166 if (visualize_delay >= 0)
167 cv::rectangle(image, groundtruthRect, CV_RGB(255, 0,0), 1);
168 std::cout << ", accuracy: " << accuracy;
169 sum_accuracy += accuracy;
172 std::cout << std::endl;
174 if (visualize_delay >= 0) {
175 cv::rectangle(image, bb_rect, CV_RGB(0,255,0), 2);
176 cv::imshow("output", image);
177 int ret = cv::waitKey(visualize_delay);
178 if (visualize_delay > 0 && ret != -1 && ret != 255)
182 // std::stringstream s;
184 // int countTmp = frames;
185 // s << "imgs" << "/img" << (countTmp/10000);
186 // countTmp = countTmp%10000;
187 // s << (countTmp/1000);
188 // countTmp = countTmp%1000;
189 // s << (countTmp/100);
190 // countTmp = countTmp%100;
191 // s << (countTmp/10);
192 // countTmp = countTmp%10;
196 // //set image output parameters
197 // std::vector<int> compression_params;
198 // compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
199 // compression_params.push_back(90);
200 // cv::imwrite(ss.c_str(), image, compression_params);
203 std::cout << "Average processing speed: " << avg_time / frames << "ms (" << 1. / (avg_time / frames) * 1000 << " fps)";
204 if (groundtruth_stream.is_open()) {
205 std::cout << "; Average accuracy: " << sum_accuracy/frames << std::endl;
206 groundtruth_stream.close();
208 std::cout << std::endl;