forked from Kitware/kwant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscore_tracks_loader.cxx
More file actions
1572 lines (1368 loc) · 54.8 KB
/
score_tracks_loader.cxx
File metadata and controls
1572 lines (1368 loc) · 54.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*ckwg +5
* Copyright 2011-2017 by Kitware, Inc. All Rights Reserved. Please refer to
* KITWARE_LICENSE.TXT for licensing information, or contact General Counsel,
* Kitware, Inc., 28 Corporate Drive, Clifton Park, NY 12065.
*/
#include "score_tracks_loader.h"
#include <map>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <limits>
#include <stdexcept>
#include <vul/vul_file.h>
#include <vul/vul_reg_exp.h>
#include <vgl/vgl_area.h>
#include <scoring_framework/score_core.h>
#include <track_oracle/aries_interface/aries_interface.h>
#include <track_oracle/file_formats/track_kwxml/file_format_kwxml.h>
#include <track_oracle/file_formats/track_kw18/file_format_kw18.h>
#ifdef SHAPELIB_ENABLED
#include <track_oracle/file_formats/track_apix/file_format_apix.h>
#endif
#include <track_oracle/file_formats/track_xgtf/file_format_xgtf.h>
#include <track_oracle/file_formats/track_vpd/track_vpd_event.h>
#include <track_oracle/file_formats/track_vpd/file_format_vpd_track.h>
#include <track_oracle/file_formats/track_comms_xml/file_format_comms_xml.h>
#include <track_oracle/file_formats/file_format_manager.h>
#ifdef KWANT_ENABLE_MGRS
#include <track_oracle/file_formats/track_scorable_mgrs/track_scorable_mgrs.h>
#endif
#include <track_oracle/file_formats/file_format_schema.h>
#include <scoring_framework/time_window_filter.h>
#include <scoring_framework/timestamp_utilities.h>
#include <scoring_framework/virat_scenario_utilities.h>
#include <track_oracle/core/state_flags.h>
#include <tinyxml.h>
#include <boost/lexical_cast.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <utilities/shell_comments_filter.h>
#include <utilities/blank_line_filter.h>
#include <vital/logger/logger.h>
static kwiver::vital::logger_handle_t main_logger( kwiver::vital::get_logger( __FILE__ ) );
using std::endl;
using std::exit;
using std::getline;
using std::ifstream;
using std::istringstream;
using std::make_pair;
using std::map;
using std::ofstream;
using std::ostringstream;
using std::pair;
using std::runtime_error;
using std::setprecision;
using std::string;
using std::vector;
using kwiver::track_oracle::frame_handle_list_type;
using kwiver::track_oracle::frame_handle_type;
using kwiver::track_oracle::track_handle_list_type;
using kwiver::track_oracle::track_handle_type;
using kwiver::track_oracle::track_oracle_core;
using kwiver::track_oracle::file_format_schema_type;
using kwiver::track_oracle::file_format_manager;
using kwiver::track_oracle::track_vpd_track_type;
using kwiver::track_oracle::track_vpd_event_type;
using kwiver::track_oracle::track_comms_xml_type;
using kwiver::track_oracle::comms_xml_reader_opts;
using kwiver::track_oracle::kwxml_reader_opts;
using kwiver::track_oracle::xgtf_reader_opts;
using kwiver::track_oracle::track_field;
using kwiver::track_oracle::oracle_entry_handle_type;
using kwiver::track_oracle::aries_interface;
using kwiver::track_oracle::track_base_impl;
using namespace kwiver::kwant;
using namespace kwiver::kwant::timestamp_utilities;
class track_record_type
{
public:
track_record_type() {}
const string& src_fn() const { return this_src_fn; }
void set_src_fn( const string& s ) { this_src_fn = s; }
const track_handle_list_type& tracks() const { return this_tracks; }
void set_tracks( const track_handle_list_type& t )
{
this_tracks = t;
recompute_stats();
}
void recompute_stats()
{
this_stats.reset();
this_stats.set_from_tracks( this_tracks );
}
const track_timestamp_stats_type& stats() const { return this_stats; }
bool timestamp_overlaps( const track_record_type& other ) const;
private:
string this_src_fn;
track_handle_list_type this_tracks;
track_timestamp_stats_type this_stats;
};
//
// This object is the expansion of the track source argument, for both
// ground-truth and computed tracks. If timestamp_generator_map is
// empty, then the source time already had timestamps (e.g. kw18).
//
// When the input file is a VIRAT scenario, also return a map of
// query_id -> activity_list. When the other input is a comms_xml
// from the test harness, which only has query_ids, we can use this
// map to convert to activities.
//
struct input_source_type
{
input_source_type( const string& arg, vul_arg< string >& path_override, double fps, size_t min_track_length_param );
vector< string > fn_list;
timestamp_generator_map_type timestamp_generator_map;
map< string, vector< size_t > > qid2activity_map;
size_t min_track_length;
};
///
/// Until the alignment relationship branch lands, the quick way
/// to support scoring file formats which don't contain timestamps
/// is to use the --fn2ts flag to convert frame numbers to timestamps.
/// Converts frame numbers straight into seconds. (If the format
/// doesn't support frame numbers, log an error but do nothing else;
/// the caller will decide if it's worth it to carry on.)
///
/// There are a couple of ways to do this. One would be to look up
/// the source format of the track, obtain an instance its schema,
/// and see if it contains the timestamp and frame number fields.
///
/// The other way is to directly query the tracks to see if they contain
/// the timestamp / frame number fields. (Rather, query the first track
/// in the record and assume the answer holds for the other tracks.)
///
/// I went with the second way because it works better with the idea of
/// starting with a source format and "decorating" it with the fields as
/// we move down the processing chain. This way, for example, you can
/// call this routine twice in a row on the same data and it does what you'd
/// expect (add timestamps the first time, do nothing the second time.)
/// The first way, it would add the timestamps redundantly the second time.
///
void
promote_frame_number_to_timestamp( vector< track_record_type >& tr )
{
scorable_track_type stt;
for (size_t i=0; i<tr.size(); ++i)
{
// need to find a frame to examine; assume that what holds for
// one frame will hold for all
track_record_type& r = tr[i];
const track_handle_list_type& r_tracks = r.tracks();
if (r_tracks.empty()) continue;
frame_handle_list_type frames;
for (size_t j=0; j<r_tracks.size() && frames.empty(); ++j)
{
frames = track_oracle_core::get_frames( r_tracks[j] );
}
// couldn't find any frames... nothing to promote!
if (frames.empty()) continue;
// if the frame does NOT define frame numbers, log an error and bail
if ( ! stt.timestamp_frame.exists( frames[0].row ))
{
LOG_ERROR( main_logger, "frame-number-to-timestamp promotion requested for " << r.src_fn()
<< ", but format does not does not contain frame numbers; skipping" );
continue;
}
// if we get this far, we (a) don't have a timestamp, but (b) have frame numbers.
// Loop over all tracks / frames and plug in timestamps
size_t c=0, total_f=0;
for (size_t j=0; j<r_tracks.size(); ++j)
{
frames = track_oracle_core::get_frames( r_tracks[j] );
for (size_t f=0; f<frames.size(); ++f)
{
++total_f;
if (stt.timestamp_frame.exists( frames[f].row ))
{
ts_type new_timestamp = stt[ frames[f] ].timestamp_frame() * 1000 * 1000;
stt[ frames[f] ].timestamp_usecs() = new_timestamp;
++c;
}
}
}
LOG_INFO( main_logger, "frame-number-to-timestamp for " << r.src_fn() << ": promoted "
<< c << " of " << total_f << " frames" );
// reset the all-have-timestamps flag
r.recompute_stats();
} // ... for each element in the track record
}
///
/// The VIRAT Public Data "object tracks" don't have an external ID, but
/// they do have an object ID. One might argue that this should've been
/// directly coded as the external ID in the first place, but on the assumption
/// that the schemas should hew as closely as possible to the external
/// definition of the format, and clients (such as us) are responsible for
/// any conversions required for our application, this method goes down the
/// track records and copies any object IDs over to external IDs.
///
/// It is tempting to generalize both this and the fn2ts routine to a more
/// general transformation paradigm, but we'll defer that for now.
///
void
promote_object_id_to_external_id( const vector< track_record_type >& tr )
{
file_format_schema_type ffs;
track_vpd_track_type vpd;
scorable_track_type stt;
for (size_t i=0; i<tr.size(); ++i)
{
const track_record_type& r = tr[i];
const track_handle_list_type& r_tracks = r.tracks();
if (r_tracks.empty()) continue;
// only process TF_VPD_TRACK types
if ( ffs( r_tracks[0] ).format() != kwiver::track_oracle::TF_VPD_TRACK ) continue;
for (size_t j=0; j<r_tracks.size(); ++j)
{
stt( r_tracks[j] ).external_id() = vpd( r_tracks[j] ).object_id();
}
}
}
void
promote_vpd_event_to_virat_event( const vector< track_record_type >& tr )
{
file_format_schema_type ffs;
track_vpd_event_type vpd;
track_field<int> virat_activity_index( "activity" );
track_field<double> virat_activity_probability( "activity_probability" );
size_t prob_to_one_count = 0;
size_t activity_promotion_count = 0;
for (size_t i=0; i<tr.size(); ++i)
{
const track_record_type& r = tr[i];
const track_handle_list_type& r_tracks = r.tracks();
if (r_tracks.empty()) continue;
// only process TF_VPD_TRACK types
if ( ffs( r_tracks[0] ).format() != kwiver::track_oracle::TF_VPD_TRACK ) continue;
for (size_t j=0; j<r_tracks.size(); ++j)
{
oracle_entry_handle_type row = r_tracks[j].row;
pair< bool, unsigned > vpd_event_probe = vpd.event_type.get( row );
if ( ! vpd_event_probe.first) continue;
string n = aries_interface::vpd_index_to_activity( vpd_event_probe.second );
if ( n == "" )
{
LOG_WARN( main_logger, "VPD index " << vpd_event_probe.second << " has no VIRAT equivalent" );
continue;
}
virat_activity_index( row ) = aries_interface::activity_to_index( n );
++activity_promotion_count;
// If we don't already have a probability, assume this is loaded as ground truth
// and set the probability to one
if ( ! virat_activity_probability.exists( row ))
{
virat_activity_probability( row ) = 1.0;
++prob_to_one_count;
}
}
}
if (activity_promotion_count > 0)
{
LOG_INFO( main_logger, "VPD->VIRAT event promotion: assumed probability == 1 for " << prob_to_one_count
<< " out of " << activity_promotion_count << " tracks" );
}
}
///
/// We now support THREE ways of obtaining a set of track files on the command
/// line:
///
/// 1) a single filename: "-gt filename.kw18"
///
/// 2) a file which in turn contains other filenames: "-gt @groundtruth-list.txt"
///
/// 3) a VIRAT scenario file (which also contains timestamps) "-gt scenario.xml"
///
///
/// ...and with the truth_path / computed_path options as well.
///
string
resolve_filename( const string& given_path,
vul_arg< string >& path_override )
{
if ( path_override() == "" ) return given_path;
string base_path = vul_file::basename( given_path );
string final_path = path_override()+"/"+base_path;
if ( base_path != given_path )
{
LOG_INFO( main_logger, "Overriding " << given_path << " to " << final_path );
}
return final_path;
}
input_source_type
::input_source_type( const string& arg,
vul_arg< string >& path_override,
double fps,
size_t min_track_length_param )
: min_track_length( min_track_length_param )
{
// Either arg starts with "@", in which case it's a list,
// or it can be loaded as a scenario file,
// else it will be interpreted downstream as a filename.
// Only load the timestamps if it's a scenario file.
// empty vector of filenames signals error.
if (arg.empty()) return;
if (arg[0] == '@')
{
//
// Case 1: argument is a pointer to a list of filenames,
// which are read without further interpretation.
//
string fn = arg.substr(1, arg.size() - 1);
ifstream is( fn.c_str() );
if ( ! is )
{
LOG_ERROR( main_logger, "Couldn't open filename list '" << fn << "'");
this->fn_list.clear();
return;
}
// build boost filter to remove comments and blank lines
boost::iostreams::filtering_istream in_stream;
in_stream.push (vidtk::blank_line_filter());
in_stream.push (vidtk::shell_comments_filter());
in_stream.push (is);
string tmp;
while (getline(in_stream, tmp ))
{
this->fn_list.push_back( resolve_filename( tmp, path_override ));
}
}
else
{
// It's a single string; does that string name a VIRAT scenario?
if ( ! virat_scenario_utilities::fn_is_virat_scenario( arg ))
{
//
// Case 2: Doesn't start with '@', can't load as a scenario;
// treat as a single filename.
//
this->fn_list.push_back( resolve_filename( arg, path_override ));
}
else
{
//
// Case 3: it seems to be a VIRAT scenario file.
//
timestamp_generator_map_type full_pathname_map;
if ( ! virat_scenario_utilities::process( arg, this->qid2activity_map, full_pathname_map, fps ) )
{
return;
}
// populate fn_list with the keys of the timestamp_generator_map
for (timestamp_generator_cit i = full_pathname_map.begin();
i != full_pathname_map.end();
++i)
{
this->fn_list.push_back( resolve_filename( i->first, path_override ));
this->timestamp_generator_map[ vul_file::basename( i->first ) ] = i->second;
}
} // ... conclude case 2 vs 3
} // ... conclude case 1 vs 2/3
// all done!
}
#ifdef KWANT_ENABLE_MGRS
//
// MGRS data will be appended if:
// (a) a track is APIX, regardless of whether or not the user asked for it,
// (b) the track is any other format, if the user asked for it.
//
// Note that if the user asked for it, but set_from_tracklist fails, we throw
// an exception.
//
// Otherwise, this routine is a no-op.
//
void
set_mgrs_data( const vector< track_record_type >& tr_list,
bool user_requested_mgrs_conversion,
const string& lon_field,
const string& lat_field )
{
file_format_schema_type tfs;
for (size_t i=0; i<tr_list.size(); ++i)
{
const track_handle_list_type& r_tracks = tr_list[i].tracks();
if (r_tracks.empty()) continue;
const track_handle_type& r0 = r_tracks[0];
string fn = file_format_schema_type::source_id_to_filename( tfs( r0 ).source_file_id() );
bool add_to_this_record = false;
if (tfs( r0 ).format() == kwiver::track_oracle::TF_APIX)
{
if ( ! user_requested_mgrs_conversion )
{
LOG_INFO( main_logger, "No user-requested MGRS conversion for APIX file '" << fn << "'; adding MGRS anyway" );
}
add_to_this_record = true;
}
else
{
add_to_this_record = user_requested_mgrs_conversion;
}
if ( ! add_to_this_record ) continue;
if ( ! track_scorable_mgrs_type::set_from_tracklist( r_tracks, lon_field, lat_field ))
{
ostringstream oss;
oss << "Could not add MGRS data to tracks from '" << fn << "'";
LOG_ERROR( main_logger, oss.str() );
throw runtime_error( oss.str().c_str() );
}
LOG_INFO( main_logger, "Added MGRS data to " << r_tracks.size() << " tracks from " << fn );
}
}
#endif
#ifdef SHAPELIB_ENABLED
//
// Two things need to be done to APIX tracks:
// 1) their timestamps need to be converted from vidtk timestamps to timestamp_usecs
// 2) Per Juda's request, optionally log how we read them
//
void
convert_apix_timestamps( vector< track_record_type >& tr_list,
const string& dbg_fn )
{
file_format_schema_type tfs;
track_apix_type apix_schema;
track_field< ts_type > timestamp_usecs( "timestamp_usecs" );
ofstream* os = 0;
for (size_t i=0; i<tr_list.size(); ++i)
{
const track_handle_list_type& r_tracks = tr_list[i].tracks();
if (r_tracks.empty()) continue;
const track_handle_type& r0 = r_tracks[0];
if ( tfs( r0 ).format() != kwiver::track_oracle::TF_APIX ) continue;
string fn = file_format_schema_type::source_id_to_filename( tfs( r0 ).source_file_id() );
if ((dbg_fn != "") && ( ! os ))
{
os = new ofstream();
os->open( dbg_fn.c_str(), ofstream::out | ofstream::app );
if ( ! (*os) )
{
ostringstream oss;
oss << "Couldn't append to APIX debug log file '" << dbg_fn << "'";
throw runtime_error( oss.str() );
}
(*os) << setprecision(9);
}
for (size_t j = 0; j<r_tracks.size(); ++j)
{
frame_handle_list_type frames = track_oracle_core::get_frames( r_tracks[j] );
for (size_t f = 0; f<frames.size(); ++f )
{
frame_handle_type frame = frames[f];
// convert timestamp
const timestamp& ts = apix_schema[ frame ].utc_timestamp();
if ( ! ts.has_time() )
{
ostringstream oss;
oss << "APIX track in " << fn << " id " << apix_schema[frame].external_id()
<< " @ time " << ts << " has no time?";
throw runtime_error( oss.str() );
}
timestamp_usecs( frame.row ) = static_cast< ts_type >( ts.time() );
// optionally log info
if (os)
{
(*os) << fn << " track " << apix_schema[ frame ].external_id()
<< " frame " << f << " @ " << ts << " : lon " << apix_schema[ frame ].lon()
<< " ; lat " << apix_schema[ frame ].lat()
<< endl;
}
} // ...each frame
} // .. each track
} // ...each track record
delete os;
}
#endif
void
set_virat_scenario_activities( const input_source_type& truth_src,
vector< track_record_type >& computed_track_records )
{
// quick exit if truth_src has no qid2activity records and computed_track_records are not comm files.
if (computed_track_records.empty()) return;
if (computed_track_records[0].tracks().empty()) return;
track_handle_type track0 = computed_track_records[0].tracks()[0];
file_format_schema_type ffs;
if ( ffs( track0 ).format() != kwiver::track_oracle::TF_COMMS_XML ) return;
if ( truth_src.qid2activity_map.empty() )
{
LOG_WARN( main_logger, string("\n")+
"Computed tracks are from a comms file but no query IDs were found in\n"+
"the truth tracks. The computed tracks will contain no activity labels\n"+
"and can't be used for event scoring." );
return;
}
LOG_INFO( main_logger, "VIRAT scenario / comms: populating activity information from scenario queries" );
//
// This is a little ugly. The standard paradigm inherited from XGTF
// is: one-to-one mapping between an activity and a track. If a
// single comms query result, from a single query ID, has multiple
// activities in its corresponding entry in the scenario file, we
// copy the comms track here and rely on score_events upstream
// filtering to make the numbers all work out.
track_comms_xml_type comms, comms_copy;
track_field< int > activity( "activity" );
for (size_t i=0; i<computed_track_records.size(); ++i)
{
track_record_type& r = computed_track_records[i];
const track_handle_list_type& r_tracks = r.tracks();
track_handle_list_type new_tracks;
for (size_t j=0; j<r_tracks.size(); ++j)
{
track_handle_type t = r_tracks[j];
if ( ! comms.query_id.exists( t.row ))
{
new_tracks.push_back( t );
continue;
}
string qid = comms( t ).query_id();
map< string, vector< size_t > >::const_iterator probe
= truth_src.qid2activity_map.find( qid );
if ( probe == truth_src.qid2activity_map.end() )
{
ostringstream oss;
oss << "comms / scenario mismatch? Comms query " << qid << " not in scenario?";
throw runtime_error( oss.str() );
}
const vector< size_t >& activity_indices = probe->second;
if (activity_indices.empty())
{
ostringstream oss;
oss << "comms / scenario mismatch? Comms query " << qid << " doesn't define any activities?";
throw runtime_error( oss.str() );
}
// just add the first activity to the original track
activity( t.row ) = activity_indices[0];
new_tracks.push_back( t );
// make copies for the other activities
for (size_t k=1; k<activity_indices.size(); ++k)
{
// This is where a generic copy-the-track method would come in handy.
// But would that be at the track_base_impl level? Or at the element_descriptor level?
track_handle_type new_t = comms_copy.create();
comms_copy( new_t ).track_source() = comms( t ).track_source();
comms_copy( new_t ).probability() = comms( t ).probability();
comms_copy( new_t ).query_id() = comms( t ).query_id();
frame_handle_list_type frames = track_oracle_core::get_frames( t );
for (size_t f = 0; f<frames.size(); ++f)
{
const frame_handle_type& src_f = frames[f];
frame_handle_type new_f = comms_copy( new_t ).create_frame();
comms_copy[ new_f ].bounding_box() = comms[ src_f ].bounding_box();
comms_copy[ new_f ].timestamp() = comms[ src_f ].timestamp();
}
// the next activity
activity( new_t.row ) = activity_indices[k];
new_tracks.push_back( new_t );
}
} // for all tracks
LOG_INFO( main_logger, "Track record " << i << ": " << r_tracks.size() << " in; " << new_tracks.size() << " out" );
r.set_tracks( new_tracks );
} // for all track records
}
bool
parse_mgrs_ll_fields( const string& s,
string& lon_field,
string& lat_field )
{
vul_reg_exp re_ll( "([^:]+):([^:]+)" );
if ( ! re_ll.find( s ))
{
LOG_ERROR ( main_logger,"Couldn't parse longitude / latitude field names from '" << s << "'" );
return false;
}
lon_field = re_ll.match( 1 );
lat_field = re_ll.match( 2 );
return true;
}
bool
parse_min_track_length_parameter( const string& s,
size_t& min_truth_length,
size_t& min_computed_length )
{
vul_reg_exp re_tl( "([0-9]+):([0-9]+)" );
if (! re_tl.find( s ))
{
LOG_ERROR( main_logger, "Couldn't parse minimum track lengths from '" << s << "'" );
return false;
}
min_truth_length = boost::lexical_cast<size_t>( re_tl.match(1) );
min_computed_length = boost::lexical_cast<size_t>( re_tl.match(2) );
return true;
}
bool
track_record_type
::timestamp_overlaps( const track_record_type& other ) const
{
const track_timestamp_stats_type& my_stats = this->stats();
const track_timestamp_stats_type& other_stats = other.stats();
// empty stats never overlap
if ( my_stats.is_empty || other_stats.is_empty) return false;
if ( my_stats.minmax_ts.first > other_stats.minmax_ts.second ) return false;
if ( my_stats.minmax_ts.second < other_stats.minmax_ts.first ) return false;
return true;
}
bool
check_vectors_for_timestamp_overlap( vector< track_record_type >& records )
{
// need at least two to overlap
if ( records.size() < 2) return false;
for (unsigned i=0; i<records.size()-1; ++i)
{
const track_record_type& r = records[i];
for (unsigned j=i+1; j<records.size(); ++j)
{
if ( r.timestamp_overlaps( records[j] ))
{
LOG_INFO( main_logger, "Timestamp overlap: " << i << " vs " << j << "\n"
<< r.src_fn() << ": " << r.stats() << "\n"
<< records[j].src_fn() << ": " << records[j].stats() );
return true;
}
}
}
return false;
}
void
check_for_zero_area_boxes( const vector< track_record_type >& records )
{
size_t warn_count = 0;
scorable_track_type stt;
for (size_t i=0; i<records.size(); ++i)
{
const track_record_type& r = records[i];
const track_handle_list_type& r_tracks = r.tracks();
for (size_t j=0; j<r_tracks.size(); ++j)
{
frame_handle_list_type frames = track_oracle_core::get_frames( r_tracks[j] );
for (size_t k=0; k<frames.size(); ++k)
{
if ( vgl_area( stt[ frames[k] ].bounding_box() ) <= 0)
{
if (warn_count < 5 )
{
LOG_WARN( main_logger, "Zero-area-box: file " << r.src_fn()
<< " track " << stt( r_tracks[j] ).external_id()
<< " frame " << stt[ frames[k] ].timestamp_frame()
<< " box " << stt[ frames[k] ].bounding_box() );
}
if (warn_count == 5)
{
LOG_WARN( main_logger, "(Further zero-area-box warnings suppressed)" );
}
++warn_count;
}
}
}
}
if (warn_count > 5)
{
LOG_WARN( main_logger,"Total of " << warn_count << " zero-area-boxes found" );
}
}
void
check_for_increasing_frame_number_and_timestamps( const vector< track_record_type >& records )
{
scorable_track_type stt;
for (size_t i=0; i<records.size(); ++i)
{
const track_record_type& r = records[i];
const track_handle_list_type& r_tracks = r.tracks();
for (size_t j=0; j<r_tracks.size(); ++j)
{
frame_handle_list_type frames = track_oracle_core::get_frames( r_tracks[j] );
if (frames.size() < 2) continue;
unsigned last_frame_num = stt[ frames[0] ].timestamp_frame();
ts_type last_ts = stt[ frames[0] ].timestamp_usecs();
for (size_t k=1; k<frames.size(); ++k)
{
if ( ! stt.timestamp_frame.exists( frames[k].row )) continue;
if ( ! stt.timestamp_usecs.exists( frames[k].row )) continue;
unsigned this_frame_num = stt[ frames[k] ].timestamp_frame();
ts_type this_ts = stt[ frames[k]] .timestamp_usecs();
bool fn_increases = (last_frame_num < this_frame_num);
bool ts_increases = (last_ts < this_ts);
// sometimes the files are written in last-to-first order;
// we just want to make sure that if the frame number increases,
// the timestamp increases, and vice versa
bool fn_and_ts_consistent = (fn_increases == ts_increases);
if ( ! fn_and_ts_consistent )
{
LOG_WARN( main_logger, "Timebases heading in different directions: file " << r.src_fn()
<< " track " << stt( r_tracks[j] ).external_id()
<< " last frame / ts " << last_frame_num << " / " << last_ts
<< " this frame / ts " << this_frame_num << " / " << this_ts );
}
last_frame_num = this_frame_num;
last_ts = this_ts;
}
}
}
}
track_timestamp_stats_type
track_set_minmax_timestamps( const vector< track_record_type >& records )
{
track_timestamp_stats_type stats;
for (size_t i=0; i<records.size(); ++i)
{
stats.combine_with_other( records[i].stats() );
}
return stats;
}
pair< size_t, size_t >
filter_on_timestamp_window( const time_window_filter& twf,
vector< track_record_type >& records )
{
if ( ! twf.is_valid() )
{
throw runtime_error( "Filter-on-timestamp-window called with invalid time window filter" );
}
size_t in_c(0), out_c(0);
for (size_t i=0; i<records.size(); ++i)
{
track_record_type& r = records[i];
const track_handle_list_type& r_tracks = r.tracks();
in_c += r_tracks.size();
track_handle_list_type out;
for (size_t j=0; j<r_tracks.size(); ++j)
{
if ( twf.track_passes_filter( r_tracks[j] ))
{
out.push_back( r_tracks[j] );
}
}
out_c += out.size();
r.set_tracks( out );
}
return make_pair( in_c, out_c );
}
vector< track_record_type >
load_tracks_from_file( const input_source_type& src )
{
vector< track_record_type > ret;
for (unsigned i=0; i<src.fn_list.size(); ++i)
{
track_record_type r;
r.set_src_fn( src.fn_list[i] );
LOG_INFO( main_logger, "About to load file " << i+1 << " of " << src.fn_list.size() << " : " << r.src_fn() << "...");
track_handle_list_type input_tracks;
if ( ! file_format_manager::read( r.src_fn(), input_tracks ))
{
LOG_ERROR( main_logger, "Couldn't load tracks from '" << r.src_fn() << "'");
ret.clear();
return ret;
}
LOG_INFO( main_logger, "read " << input_tracks.size() << " tracks");
// only keep tracks longer than the requested number of states
if ( src.min_track_length > 0 )
{
track_handle_list_type filtered;
for (size_t j=0; j<input_tracks.size(); ++j)
{
size_t n = track_oracle_core::get_n_frames( input_tracks[j] );
if (n >= src.min_track_length )
{
filtered.push_back( input_tracks[j] );
}
}
LOG_INFO( main_logger, "Track length filtering requested; kept " << filtered.size()
<< " tracks with length >= " << src.min_track_length );
input_tracks = filtered;
}
r.set_tracks( input_tracks );
ret.push_back( r );
}
return ret;
}
bool
timestamp_paired_gtct( vector< track_record_type >& truth_track_records,
double truth_fps,
vector< track_record_type >& computed_track_records,
double computed_fps )
{
if ( truth_track_records.size() != computed_track_records.size() )
{
LOG_ERROR( main_logger, "ERROR: paired gt/ct requested; loaded " << truth_track_records.size()
<< " truth track files, but " << computed_track_records.size()
<< " computed track files; should be equal");
return false;
}
ts_type ts_offset = 0;
scorable_track_type trk;
const ts_type two_second_gap = static_cast<ts_type>(2.0e6); // in usecs
for (unsigned i=0; i<truth_track_records.size(); ++i)
{
// skip pathological cases
if (truth_track_records[i].tracks().empty() && computed_track_records[i].tracks().empty())
{
continue;
}
// not refs because we recompute at the end of the loop
track_timestamp_stats_type truth_stats = truth_track_records[i].stats();
track_timestamp_stats_type computed_stats = computed_track_records[i].stats();
track_timestamp_stats_type combined_stats( truth_track_records[i].stats() );
combined_stats.combine_with_other( computed_stats );
LOG_INFO( main_logger, "Processing gt/ct pair " << i << " : starting at:\n"
<< "truth: " << truth_stats.minmax_ts.first << " to " << truth_stats.minmax_ts.second
<< " ( " << truth_stats.minmax_ts.second - truth_stats.minmax_ts.first << " )\n"
<< "computed: " << computed_stats.minmax_ts.first << " to " << computed_stats.minmax_ts.second
<< " ( " << computed_stats.minmax_ts.second - computed_stats.minmax_ts.first << " )\n"
<< "combined: " << combined_stats.minmax_ts.first << " to " << combined_stats.minmax_ts.second
<< " ( " << combined_stats.minmax_ts.second - combined_stats.minmax_ts.first << " )" );
//
// if we have computed tracks but haven't managed to get them timestamps, that's
// a dealbreaker
//
bool computed_tracks_okay = computed_track_records[i].tracks().empty() || computed_stats.has_timestamps;
if ( ! computed_tracks_okay )
{
LOG_ERROR( main_logger, "Processing gt/ct pair " << i << ": computed tracks exist, but have no timestamps?" );
return false;
}
//
// If truth tracks don't have timestamps yet, we need to interpolate them in
// so we can rebase them
//
if ( ! truth_stats.has_timestamps )
{
timestamp_generator tg =
timestamp_generator_factory::from_tts( computed_stats, computed_fps, truth_fps );
tg.set_timestamps( truth_track_records[i].tracks() );
truth_track_records[i].recompute_stats();
truth_stats = truth_track_records[i].stats();
combined_stats.reset();
combined_stats.combine_with_other( truth_stats );
combined_stats.combine_with_other( computed_stats );
}
LOG_INFO( main_logger, "Processing gt/ct pair " << i << " : rebased at:\n"
<< "truth: " << truth_stats.minmax_ts.first << " to " << truth_stats.minmax_ts.second
<< " ( " << truth_stats.minmax_ts.second - truth_stats.minmax_ts.first << " )\n"
<< "computed: " << computed_stats.minmax_ts.first << " to " << computed_stats.minmax_ts.second
<< " ( " << computed_stats.minmax_ts.second - computed_stats.minmax_ts.first << " )\n"
<< "combined: " << combined_stats.minmax_ts.first << " to " << combined_stats.minmax_ts.second
<< " ( " << combined_stats.minmax_ts.second - combined_stats.minmax_ts.first << " )" );
vector< track_handle_list_type > tr_list;
tr_list.push_back( truth_track_records[i].tracks() );
tr_list.push_back( computed_track_records[i].tracks() );
ts_type min_ts = combined_stats.minmax_ts.first;
LOG_INFO( main_logger, "Processing gt/ct pair " << i << " : offset " << ts_offset
<< "; min ts " << min_ts );
ts_type max_ts = 0;
for (unsigned tr = 0; tr<tr_list.size(); ++tr )
{
const track_handle_list_type& tl = tr_list[ tr ];
for (unsigned j=0; j<tl.size(); ++j)
{
frame_handle_list_type f = track_oracle_core::get_frames( tl[j] );
for (unsigned k=0; k<f.size(); ++k)
{
pair< bool, ts_type > ts_probe = trk.timestamp_usecs.get( f[k].row );
if ( ! ts_probe.first )
{
LOG_ERROR( main_logger, "Processing gt/ct pair " << i << " : frame without timestamp?" );
return false;
}
ts_type this_ts = ts_probe.second;
this_ts -= min_ts;
this_ts += ts_offset;
trk[ f[k] ].timestamp_usecs() = this_ts;
if ( this_ts > max_ts ) max_ts = this_ts;
}
}
}
ts_offset = max_ts + two_second_gap; // space out the tracks between sets
LOG_INFO( main_logger, "Processing gt/ct pair " << i << " : before: "
<< combined_stats.minmax_ts.first << " to " << combined_stats.minmax_ts.second
<< " ( " << combined_stats.minmax_ts.second - combined_stats.minmax_ts.first << " )" );
combined_stats.reset();
for ( const auto& t: tr_list )
{
combined_stats.set_from_tracks( t );