00001
00002 #include "strk_feature_capture_process.h"
00003 #include <vcl_iostream.h>
00004 #include <vcl_vector.h>
00005 #include <vtol/vtol_topology_object.h>
00006 #include <vtol/vtol_edge.h>
00007 #include <vtol/vtol_face_2d.h>
00008 #include <strk/strk_io.h>
00009 #include <strk/strk_tracking_face_2d.h>
00010 strk_feature_capture_process::strk_feature_capture_process(strk_info_tracker_params & tp)
00011 : tracker_(tp)
00012 {
00013 failure_ = false;
00014 first_frame_ = true;
00015 face_index_ =0;
00016 }
00017
00018 strk_feature_capture_process::~strk_feature_capture_process()
00019 {
00020 }
00021
00022 bool strk_feature_capture_process::execute()
00023 {
00024 if (failure_)
00025 return false;
00026 if (this->get_N_input_images()!=1)
00027 {
00028 vcl_cout << "In strk_feature_capture_process::execute() -"
00029 << " not exactly one input image\n";
00030 failure_ = true;
00031 return false;
00032 }
00033 vil1_image img = vpro_video_process::get_input_image(0);
00034 input_images_.clear();
00035 static int n_faces = 0;
00036 if (first_frame_)
00037 {
00038 face_index_ = 0;
00039 tracker_.set_image_0(img);
00040 n_faces = tracked_faces_.size();
00041 if (!n_faces)
00042 {
00043 vcl_cout << "In strk_feature_capture_process::execute() -"
00044 << " no faces found in track file\n";
00045 failure_ = true;
00046 }
00047 vtol_face_2d_sptr f = tracked_faces_[face_index_];
00048 tracker_.set_initial_model(f);
00049 tracker_.init();
00050 first_frame_ = false;
00051 tracked_hist_.push_back(tracker_.capture_histograms(true));
00052 return true;
00053 }
00054 face_index_++;
00055 tracker_.set_image_i(img);
00056 vtol_face_2d_sptr f = tracked_faces_[face_index_];
00057 tracker_.set_capture_face(f);
00058 tracked_hist_.push_back(tracker_.capture_histograms());
00059 vcl_vector<vtol_edge_sptr> edges;
00060 f->edges(edges);
00061 output_topo_objs_.clear();
00062 for (vcl_vector<vtol_edge_sptr>::iterator eit = edges.begin();
00063 eit != edges.end(); eit++)
00064 {
00065 vtol_topology_object_sptr to = (*eit)->cast_to_edge();
00066 output_topo_objs_.push_back(to);
00067 }
00068 return true;
00069 }
00070
00071 bool strk_feature_capture_process::finish()
00072 {
00073 first_frame_ = true;
00074 face_index_ = 0;
00075 failure_ = false;
00076 vcl_ofstream strm(hist_file_.c_str());
00077 if (!strm)
00078 {
00079 vcl_cout << "In strk_feature_capture_process::set_input_file() -"
00080 << " could not open file " << hist_file_ << '\n';
00081 return false;
00082 }
00083 strk_tracking_face_2d_sptr tf = tracker_.capture_tf();
00084 if (!tf)
00085 return false;
00086 int n_pix = tf->face()->Npix();
00087 float dia = tf->face()->Diameter();
00088 float r = tf->face()->AspectRatio();
00089 vcl_vector<vcl_vector<float> > junk;
00090 if (!strk_io::write_histogram_data(start_frame_, n_pix, dia, r,
00091 tracker_.intensity_hist_bins_,
00092 tracker_.gradient_dir_hist_bins_,
00093 tracker_.color_hist_bins_,
00094 tracked_hist_,
00095 strm))
00096 return false;
00097 return true;
00098 }
00099
00100 bool strk_feature_capture_process::set_input_file(vcl_string const& file_name)
00101 {
00102 start_frame_ = 0;
00103 track_file_ = file_name;
00104 if (track_file_=="")
00105 return false;
00106 vcl_ifstream str(track_file_.c_str());
00107 if (!str)
00108 {
00109 vcl_cout << "In strk_feature_capture_process::set_input_file() -"
00110 << " could not open file " << track_file_ << '\n';
00111 return false;
00112 }
00113 vcl_vector<vgl_point_2d<double> > tracked_cogs;
00114 unsigned int n_frames = 0;
00115 if (!strk_io::read_track_data(str, start_frame_, n_frames,
00116 tracked_cogs, tracked_faces_))
00117 {
00118 str.close();
00119 return false;
00120 }
00121 end_frame_ = start_frame_+n_frames -1;
00122 str.close();
00123 return true;
00124 }
00125
00126 bool strk_feature_capture_process::set_output_file(vcl_string const& file_name)
00127 {
00128 hist_file_ = file_name;
00129 return true;
00130 }