5feb2bef5c01a4d0fc544aa2b6a5ee1ae666daad
[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 <string.h>
30
31 using namespace std;
32
33 #include "mtp_tracker.h"
34
35 #define FILENAME_SIZE 1024
36
37 char trajectory_filename[FILENAME_SIZE];
38 char graph_filename[FILENAME_SIZE];
39 int verbose;
40
41 void usage(ostream *os) {
42   (*os) << "mtp [-h|--help] [-v|--verbose] [-t|--trajectory-filename <trajectory filename>] [-g|--graph-filename <graph filename>] [<tracking parameter file>]" << endl;
43   (*os) << endl;
44   (*os) << "The mtp command processes a file containing the description of a topology" << endl;
45   (*os) << "and detection scores, and prints the optimal set of trajectories." << endl;
46   (*os) << "If no filename is provided, it will read the parameters from the stdin." << endl;
47   (*os) << "If no trajectory filename is provided, it will write the result to" << endl;
48   (*os) << "stdout." << endl;
49   (*os) << endl;
50   (*os) << "Written by Francois Fleuret. (C) Idiap Research Institute, 2012." << endl;
51 }
52
53 scalar_t diff_in_second(struct timeval *start, struct timeval *end) {
54   return
55     scalar_t(end->tv_sec - start->tv_sec) +
56     scalar_t(end->tv_usec - start->tv_usec)/1000000;
57 }
58
59 void do_tracking(istream *in_tracker) {
60   timeval start_time, end_time;
61   MTPTracker *tracker = new MTPTracker();
62
63   if(verbose) { cout << "Reading the tracking parameters." << endl; }
64   tracker->read(in_tracker);
65
66   if(verbose) {
67     cout << "Building the graph ... "; cout.flush();
68     gettimeofday(&start_time, 0);
69   }
70   tracker->build_graph();
71   if(verbose) { 
72     gettimeofday(&end_time, 0);
73     cout << "done (" << diff_in_second(&start_time, &end_time) << "s)." << endl;
74   }
75
76   if(verbose) {
77     cout << "Tracking ... "; cout.flush();
78     gettimeofday(&start_time, 0);
79   }
80   tracker->track();
81   if(verbose) {
82     gettimeofday(&end_time, 0);
83     cout << "done (" << diff_in_second(&start_time, &end_time) << "s)." << endl;
84   }
85
86   if(strcmp(trajectory_filename, "")) {
87     ofstream out_traj(trajectory_filename);
88     tracker->write_trajectories(&out_traj);
89     if(verbose) { cout << "Wrote " << trajectory_filename << "." << endl; }
90   } else {
91     tracker->write_trajectories(&cout);
92   }
93
94   if(strcmp(graph_filename, "") != 0) {
95     ofstream out_dot(graph_filename);
96     tracker->print_graph_dot(&out_dot);
97     if(verbose) { cout << "Wrote " << graph_filename << "." << endl; }
98   }
99
100   delete tracker;
101 }
102
103 static struct option long_options[] = {
104   { "trajectory-file", 1, 0, 't' },
105   { "graph-file", 1, 0, 'g' },
106   { "help", no_argument, 0, 'h' },
107   { "verbose", no_argument, 0, 'v' },
108   { 0, 0, 0, 0 }
109 };
110
111 int main(int argc, char **argv) {
112   int c;
113   int error = 0, show_help = 0;
114
115   strncpy(trajectory_filename, "", FILENAME_SIZE);
116   strncpy(graph_filename, "", FILENAME_SIZE);
117   verbose = 0;
118
119   while ((c = getopt_long(argc, argv, "t:g:hv",
120                           long_options, NULL)) != -1) {
121
122     switch(c) {
123
124     case 't':
125       strncpy(trajectory_filename, optarg, FILENAME_SIZE);
126       break;
127
128     case 'g':
129       strncpy(graph_filename, optarg, FILENAME_SIZE);
130       break;
131
132     case 'h':
133       show_help = 1;
134       break;
135
136     case 'v':
137       verbose = 1;
138       break;
139
140     default:
141       error = 1;
142       break;
143     }
144   }
145
146   if(error) {
147     usage(&cerr);
148     exit(EXIT_FAILURE);
149   }
150
151   if(show_help) {
152     usage(&cout);
153     exit(EXIT_SUCCESS);
154   }
155
156   if(optind < argc) {
157     ifstream *file_in_tracker = new ifstream(argv[optind]);
158     if(file_in_tracker->good()) {
159       do_tracking(file_in_tracker);
160     } else {
161       cerr << "Can not open " << argv[optind] << endl;
162       exit(EXIT_FAILURE);
163     }
164     delete file_in_tracker;
165   } else {
166     do_tracking(&cin);
167   }
168
169   exit(EXIT_SUCCESS);
170 }