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
39 char trajectory_filename[FILENAME_SIZE];
40 char graph_filename[FILENAME_SIZE];
44 void usage(ostream *os) {
45 (*os) << "mtp [-h|--help] [--help-formats] [-v|--verbose] [-t|--trajectory-filename <trajectory filename>] [-g|--graph-filename <graph filename>] [<tracking parameter file>]" << endl;
47 (*os) << "The mtp command processes a file containing the description of a topology" << endl;
48 (*os) << "and detection scores, and prints the optimal set of trajectories." << endl;
50 (*os) << "If no filename is provided, it reads the parameters from the standard" << endl;
51 (*os) << "input. If no trajectory filename is provided, it writes the result to" << endl;
52 (*os) << "the standard output." << endl;
54 (*os) << "Written by Francois Fleuret. (C) Idiap Research Institute, 2012." << endl;
57 void print_help_formats() {
58 cout << "The tracking parameters the command takes as input have the following" << endl;
59 cout << "format, where L is the number of locations and T is the number of time" << endl;
60 cout << "steps:" << endl;
62 cout << "---------------------------- snip snip -------------------------------" << endl;
63 cout << " int:L int:T" << endl;
65 cout << " bool:allowed_motion_from_1_to_1 ... bool:allowed_motion_from_1_to_L" << endl;
66 cout << " ..." << endl;
67 cout << " bool:allowed_motion_from_L_to_1 ... bool:allowed_motion_from_L_to_L" << endl;
69 cout << " bool:entrance_1_1 ... bool:entrance_1_L" << endl;
70 cout << " ..." << endl;
71 cout << " bool:entrance_T_1 ... bool:entrance_T_L" << endl;
73 cout << " bool:exit_1_1 ... bool:exit_1_L" << endl;
74 cout << " ..." << endl;
75 cout << " bool:exit_T_1 ... bool:exit_T_L" << endl;
77 cout << " float:detection_score_1_1 ... float:detection_score_1_L" << endl;
78 cout << " ..." << endl;
79 cout << " float:detection_score_T_1 ... float:detection_score_T_L" << endl;
80 cout << "---------------------------- snip snip -------------------------------" << endl;
82 cout << "As results, the command writes first the number of trajectories," << endl;
83 cout << "followed by one line per trajectory with the following structure:" << endl;
85 cout << "---------------------------- snip snip -------------------------------" << endl;
86 cout << " int:traj_number int:entrance_time int:duration float:score int:location_1 ... int:location_duration" << endl;
87 cout << "---------------------------- snip snip -------------------------------" << endl;
90 scalar_t diff_in_second(struct timeval *start, struct timeval *end) {
92 scalar_t(end->tv_sec - start->tv_sec) +
93 scalar_t(end->tv_usec - start->tv_usec)/1000000;
96 void do_tracking(istream *in_tracker) {
97 timeval start_time, end_time;
98 MTPTracker *tracker = new MTPTracker();
100 if(global.verbose) { cout << "Reading the tracking parameters." << endl; }
101 tracker->read(in_tracker);
104 cout << "Building the graph ... "; cout.flush();
105 gettimeofday(&start_time, 0);
107 tracker->build_graph();
109 gettimeofday(&end_time, 0);
110 cout << "done (" << diff_in_second(&start_time, &end_time) << "s)." << endl;
114 cout << "Tracking ... "; cout.flush();
115 gettimeofday(&start_time, 0);
119 gettimeofday(&end_time, 0);
120 cout << "done (" << diff_in_second(&start_time, &end_time) << "s)." << endl;
123 if(strcmp(global.trajectory_filename, "")) {
124 ofstream out_traj(global.trajectory_filename);
125 tracker->write_trajectories(&out_traj);
126 if(global.verbose) { cout << "Wrote " << global.trajectory_filename << "." << endl; }
128 tracker->write_trajectories(&cout);
131 if(strcmp(global.graph_filename, "") != 0) {
132 ofstream out_dot(global.graph_filename);
133 tracker->print_graph_dot(&out_dot);
134 if(global.verbose) { cout << "Wrote " << global.graph_filename << "." << endl; }
142 OPT_HELP_FORMATS = CHAR_MAX + 1
145 static struct option long_options[] = {
146 { "trajectory-file", 1, 0, 't' },
147 { "graph-file", 1, 0, 'g' },
148 { "help", no_argument, 0, 'h' },
149 { "verbose", no_argument, 0, 'v' },
150 { "help-formats", no_argument, 0, OPT_HELP_FORMATS },
154 int main(int argc, char **argv) {
156 int error = 0, show_help = 0;
158 strncpy(global.trajectory_filename, "", FILENAME_SIZE);
159 strncpy(global.graph_filename, "", FILENAME_SIZE);
162 while ((c = getopt_long(argc, argv, "t:g:hv", long_options, NULL)) != -1) {
167 strncpy(global.trajectory_filename, optarg, FILENAME_SIZE);
171 strncpy(global.graph_filename, optarg, FILENAME_SIZE);
178 case OPT_HELP_FORMATS:
179 print_help_formats();
204 ifstream *file_in_tracker = new ifstream(argv[optind]);
205 if(file_in_tracker->good()) {
206 do_tracking(file_in_tracker);
208 cerr << "Can not open " << argv[optind] << endl;
211 delete file_in_tracker;