Added README.md
[mtp.git] / mtp.cc
1
2 /*
3  *  mtp is the ``Multi Tracked Paths'', an implementation of the
4  *  k-shortest paths algorithm for multi-target tracking.
5  *
6  *  Copyright (c) 2012 Idiap Research Institute, http://www.idiap.ch/
7  *  Written by Francois Fleuret <francois.fleuret@idiap.ch>
8  *
9  *  This file is part of mtp.
10  *
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.
14  *
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.
19  *
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/>.
22  *
23  */
24
25 #include <iostream>
26 #include <fstream>
27 #include <sys/time.h>
28 #include <getopt.h>
29 #include <limits.h>
30 #include <string.h>
31
32 using namespace std;
33
34 #include "mtp_tracker.h"
35
36 #define FILENAME_SIZE 1024
37
38 struct Global {
39   char trajectory_filename[FILENAME_SIZE];
40   char graph_filename[FILENAME_SIZE];
41   int verbose;
42 } global;
43
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;
46   (*os) << 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;
49   (*os) << 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;
53   (*os) << endl;
54   (*os) << "Written by Francois Fleuret. (C) Idiap Research Institute, 2012." << endl;
55 }
56
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;
61   cout << endl;
62   cout << "---------------------------- snip snip -------------------------------" << endl;
63   cout << "  int:L int:T" << endl;
64   cout << 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;
68   cout << 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;
72   cout << 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;
76   cout << 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;
81   cout << 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;
84   cout << 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;
88 }
89
90 scalar_t diff_in_second(struct timeval *start, struct timeval *end) {
91   return
92     scalar_t(end->tv_sec - start->tv_sec) +
93     scalar_t(end->tv_usec - start->tv_usec)/1000000;
94 }
95
96 void do_tracking(istream *in_tracker) {
97   timeval start_time, end_time;
98   MTPTracker *tracker = new MTPTracker();
99
100   if(global.verbose) { cout << "Reading the tracking parameters." << endl; }
101   tracker->read(in_tracker);
102
103   if(global.verbose) {
104     cout << "Building the graph ... "; cout.flush();
105     gettimeofday(&start_time, 0);
106   }
107   tracker->build_graph();
108   if(global.verbose) {
109     gettimeofday(&end_time, 0);
110     cout << "done (" << diff_in_second(&start_time, &end_time) << "s)." << endl;
111   }
112
113   if(global.verbose) {
114     cout << "Tracking ... "; cout.flush();
115     gettimeofday(&start_time, 0);
116   }
117   tracker->track();
118   if(global.verbose) {
119     gettimeofday(&end_time, 0);
120     cout << "done (" << diff_in_second(&start_time, &end_time) << "s)." << endl;
121   }
122
123   if(global.trajectory_filename[0]) {
124     ofstream out_traj(global.trajectory_filename);
125     tracker->write_trajectories(&out_traj);
126     if(global.verbose) { cout << "Wrote " << global.trajectory_filename << "." << endl; }
127   } else {
128     tracker->write_trajectories(&cout);
129   }
130
131   if(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; }
135   }
136
137   delete tracker;
138 }
139
140 enum
141 {
142   OPT_HELP_FORMATS = CHAR_MAX + 1
143 };
144
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 },
151   { 0, 0, 0, 0 }
152 };
153
154 int main(int argc, char **argv) {
155   int c;
156   int error = 0, show_help = 0;
157
158   strncpy(global.trajectory_filename, "", FILENAME_SIZE);
159   strncpy(global.graph_filename, "", FILENAME_SIZE);
160   global.verbose = 0;
161
162   while ((c = getopt_long(argc, argv, "t:g:hv", long_options, NULL)) != -1) {
163
164     switch(c) {
165
166     case 't':
167       strncpy(global.trajectory_filename, optarg, FILENAME_SIZE);
168       break;
169
170     case 'g':
171       strncpy(global.graph_filename, optarg, FILENAME_SIZE);
172       break;
173
174     case 'h':
175       show_help = 1;
176       break;
177
178     case OPT_HELP_FORMATS:
179       print_help_formats();
180       exit(EXIT_SUCCESS);
181       break;
182
183     case 'v':
184       global.verbose = 1;
185       break;
186
187     default:
188       error = 1;
189       break;
190     }
191   }
192
193   if(error) {
194     usage(&cerr);
195     exit(EXIT_FAILURE);
196   }
197
198   if(show_help) {
199     usage(&cout);
200     exit(EXIT_SUCCESS);
201   }
202
203   if(optind < argc) {
204     ifstream *file_in_tracker = new ifstream(argv[optind]);
205     if(file_in_tracker->good()) {
206       do_tracking(file_in_tracker);
207     } else {
208       cerr << "Can not open " << argv[optind] << endl;
209       exit(EXIT_FAILURE);
210     }
211     delete file_in_tracker;
212   } else {
213     do_tracking(&cin);
214   }
215
216   exit(EXIT_SUCCESS);
217 }