3 * mtp is the ``Multi Tracked Paths'', an implementation of the
4 * k-shortest paths algorithm for multi-target tracking.
6 * Copyright (c) 2012 Idiap Research Institute, http://www.idiap.ch/
7 * Written by Francois Fleuret <francois.fleuret@idiap.ch>
9 * This file is part of mtp.
11 * mtp is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 3 as
13 * published by the Free Software Foundation.
15 * mtp is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
18 * License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with selector. If not, see <http://www.gnu.org/licenses/>.
34 #include "mtp_tracker.h"
36 #define FILENAME_SIZE 1024
38 char trajectory_filename[FILENAME_SIZE];
39 char graph_filename[FILENAME_SIZE];
42 void usage(ostream *os) {
43 (*os) << "mtp [-h|--help] [--help-formats] [-v|--verbose] [-t|--trajectory-filename <trajectory filename>] [-g|--graph-filename <graph filename>] [<tracking parameter file>]" << endl;
45 (*os) << "The mtp command processes a file containing the description of a topology" << endl;
46 (*os) << "and detection scores, and prints the optimal set of trajectories." << endl;
48 (*os) << "If no filename is provided, it reads the parameters from the standard" << endl;
49 (*os) << "input. If no trajectory filename is provided, it writes the result to" << endl;
50 (*os) << "the standard output." << endl;
52 (*os) << "Written by Francois Fleuret. (C) Idiap Research Institute, 2012." << endl;
55 void print_help_formats() {
56 cout << "The tracking parameters the command takes as input have the following" << endl;
57 cout << "format, where L is the number of locations and T is the number of time" << endl;
58 cout << "steps:" << endl;
60 cout << "---------------------------- snip snip -------------------------------" << endl;
61 cout << " int:L int:T" << endl;
63 cout << " bool:allowed_motion_from_1_to_1 ... bool:allowed_motion_from_1_to_L" << endl;
64 cout << " ..." << endl;
65 cout << " bool:allowed_motion_from_L_to_1 ... bool:allowed_motion_from_L_to_L" << endl;
67 cout << " bool:entrance_1_1 ... bool:entrance_1_L" << endl;
68 cout << " ..." << endl;
69 cout << " bool:entrance_T_1 ... bool:entrance_T_L" << endl;
71 cout << " bool:exit_1_1 ... bool:exit_1_L" << endl;
72 cout << " ..." << endl;
73 cout << " bool:exit_T_1 ... bool:exit_T_L" << endl;
75 cout << " float:detection_score_1_1 ... float:detection_score_1_L" << endl;
76 cout << " ..." << endl;
77 cout << " float:detection_score_T_1 ... float:detection_score_T_L" << endl;
78 cout << "---------------------------- snip snip -------------------------------" << endl;
80 cout << "As results, the command writes first the number of trajectories," << endl;
81 cout << "followed by one line per trajectory with the following structure:" << endl;
83 cout << "---------------------------- snip snip -------------------------------" << endl;
84 cout << " int:traj_number int:entrance_time int:duration float:score int:location_1 ... int:location_duration" << endl;
85 cout << "---------------------------- snip snip -------------------------------" << endl;
88 scalar_t diff_in_second(struct timeval *start, struct timeval *end) {
90 scalar_t(end->tv_sec - start->tv_sec) +
91 scalar_t(end->tv_usec - start->tv_usec)/1000000;
94 void do_tracking(istream *in_tracker) {
95 timeval start_time, end_time;
96 MTPTracker *tracker = new MTPTracker();
98 if(verbose) { cout << "Reading the tracking parameters." << endl; }
99 tracker->read(in_tracker);
102 cout << "Building the graph ... "; cout.flush();
103 gettimeofday(&start_time, 0);
105 tracker->build_graph();
107 gettimeofday(&end_time, 0);
108 cout << "done (" << diff_in_second(&start_time, &end_time) << "s)." << endl;
112 cout << "Tracking ... "; cout.flush();
113 gettimeofday(&start_time, 0);
117 gettimeofday(&end_time, 0);
118 cout << "done (" << diff_in_second(&start_time, &end_time) << "s)." << endl;
121 if(strcmp(trajectory_filename, "")) {
122 ofstream out_traj(trajectory_filename);
123 tracker->write_trajectories(&out_traj);
124 if(verbose) { cout << "Wrote " << trajectory_filename << "." << endl; }
126 tracker->write_trajectories(&cout);
129 if(strcmp(graph_filename, "") != 0) {
130 ofstream out_dot(graph_filename);
131 tracker->print_graph_dot(&out_dot);
132 if(verbose) { cout << "Wrote " << graph_filename << "." << endl; }
140 OPT_HELP_FORMATS = CHAR_MAX + 1
143 static struct option long_options[] = {
144 { "trajectory-file", 1, 0, 't' },
145 { "graph-file", 1, 0, 'g' },
146 { "help", no_argument, 0, 'h' },
147 { "verbose", no_argument, 0, 'v' },
148 { "help-formats", no_argument, 0, OPT_HELP_FORMATS },
152 int main(int argc, char **argv) {
154 int error = 0, show_help = 0;
156 strncpy(trajectory_filename, "", FILENAME_SIZE);
157 strncpy(graph_filename, "", FILENAME_SIZE);
160 while ((c = getopt_long(argc, argv, "t:g:hv",
161 long_options, NULL)) != -1) {
166 strncpy(trajectory_filename, optarg, FILENAME_SIZE);
170 strncpy(graph_filename, optarg, FILENAME_SIZE);
177 case OPT_HELP_FORMATS:
178 print_help_formats();
203 ifstream *file_in_tracker = new ifstream(argv[optind]);
204 if(file_in_tracker->good()) {
205 do_tracking(file_in_tracker);
207 cerr << "Can not open " << argv[optind] << endl;
210 delete file_in_tracker;