00001 #include <vector>
00002 #include "Basis/CmdOptions.h"
00003 #include "Core/Stream/RgbDataSrcFactory.h"
00004 #include "Core/Array/WritePng.h"
00005
00006 #include "Core/Tracking/TrackerFactory.h"
00007 #include "Core/Tracking/FrameRegionTable.h"
00008 #include "Core/Tracking/Classifier.h"
00009
00010 using namespace Impala;
00011 using namespace Core;
00012 using namespace Util;
00013 using namespace Stream;
00014 using namespace Tracking;
00015
00016 CmdOptions& SetUpOptions(int argc, char *argv[])
00017 {
00018 CmdOptions& options = CmdOptions::GetInstance();
00019
00020 options.AddOption(0, "tracker", "type", "fore_back", "fore_back|appearance_kalman");
00021 options.AddOption(0, "tracker-dump-im", "filename", "");
00022
00023 options.AddOption(0, "fore_back.sampleSpacing", "int", "5");
00024 options.AddOption(0, "fore_back.filterSize", "int", "19");
00025 options.AddOption(0, "fore_back.gabor.scale", "int", "4");
00026 options.AddOption(0, "fore_back.gabor.frequency", "int", "2");
00027 options.AddOption(0, "fore_back.gabor.directionCount", "int", "2");
00028 options.AddOption(0, "fore_back.classifier.gamma", "double", "0.05");
00029 options.AddOption(0, "fore_back.classifier.lambdafactor", "double", "0.004");
00030 options.AddOption(0, "fore_back.tracker-debug-file", "filename", "debug.txt");
00031
00032 options.AddOption(0, "appearance_kalman.kalman.chi^2", "double", "13");
00033 options.AddOption(0, "appearance_kalman.kalman.gamma", "double", "0.3");
00034 options.AddOption(0, "appearance_kalman.kalman.N_omax", "int", "40");
00035 options.AddOption(0, "appearance_kalman.kalman.K", "int", "20");
00036
00037 options.ParseArgs(argc, argv, "vindeoname inputfile outputfile", 3);
00038 return options;
00039 }
00040
00041 int main(int argc, char* argv[])
00042 {
00043 CmdOptions& options = SetUpOptions(argc, argv);
00044 ILOG_VAR(main);
00045 std::string vidname = options.GetArg(0);
00046 std::string input = options.GetArg(1);
00047 std::string output = options.GetArg(2);
00048 ILOG_INFO(vidname);
00049 String imageDumpFilename = options.GetString("tracker-dump-im");
00050 bool doDump = false;
00051 if(imageDumpFilename != "")
00052 doDump = true;
00053
00054 RgbDataSrcFactory& factory = RgbDataSrcFactory::Instance();
00055 RgbDataSrc* src = factory.Construct(RgbDataSrcFactory::SRC_LAVC, vidname);
00056 if (! src->Valid())
00057 {
00058 ILOG_ERROR("RgbDataSrc failed");
00059 return 0;
00060 }
00061 ILOG_INFO("opened movie: << " << src->LastFrame()+1 << " frames of " << src->FrameWidth() << " x " << src->FrameHeight());
00062
00063
00064 FrameRegionTable* regions = new FrameRegionTable;
00065 std::ifstream ifs(input.c_str());
00066 if(ifs.is_open())
00067 {
00068 regions->ReadFrom(ifs);
00069 ifs.close();
00070 }
00071
00072 Array::Array2dVec3UInt8* image=0;
00073
00074 for(int i=0 ; i<regions->Size() ; i++)
00075 {
00076 FrameRegion region = regions->GetRegion(i);
00077 ILOG_INFO("starting tracker for region " << i);
00078 int startFrame = region.GetFrameNumber();
00079 src->GotoFrame(startFrame);
00080
00081
00082 FrameRegionTable* track = new FrameRegionTable;
00083 Tracker* tracker = TrackerFactory::CreateTracker(options);
00084 image = Array::MakeFromData<Array::Array2dVec3UInt8>(src->DataPtr(), src->FrameWidth(), src->FrameHeight());
00085 tracker->Initialize(image, ®ion, track);
00086
00087
00088 while(!src->TheEnd() && tracker->ObjectVisible())
00089 {
00090 ILOG_DEBUG(".");
00091 delete image;
00092 image = Array::MakeFromData<Array::Array2dVec3UInt8>(src->DataPtr(), src->FrameWidth(), src->FrameHeight());
00093 tracker->Process(image);
00094 if(doDump)
00095 {
00096 static int frame = 0;
00097 WritePng(image, imageDumpFilename + MakeString(frame) + ".png",
00098 &Util::Database::GetInstance());
00099 frame++;
00100 }
00101 src->NextFrame();
00102 }
00103
00104 delete tracker;
00105 delete image;
00106 ILOG_INFO("done tracking, saving data...");
00107
00108
00109 std::ostringstream oss;
00110 track->WriteTo(oss);
00111 Util::Database& db = Util::Database::GetInstance();
00112 Util::IOBuffer* buf = db.GetIOBuffer(output + MakeString(i) + ".txt", false, false, "tmp");
00113 buf->Puts(oss.str());
00114 delete buf;
00115
00116 track->WriteTo(std::cout);
00117 delete track;
00118 }
00119 ILOG_INFO("all frame regions tracked succesfully");
00120 return 0;
00121 }