00001 #ifndef Impala_Core_VideoSet_IndexFeatures_Subset_h
00002 #define Impala_Core_VideoSet_IndexFeatures_Subset_h
00003
00004 #include "Core/Feature/FeatureTable.h"
00005 #include "Core/VideoSet/Reporter.h"
00006 #include "Core/VideoSet/Keyframes.h"
00007 #include "Core/Table/Select.h"
00008 #include "Core/Table/Write.h"
00009
00010 namespace Impala
00011 {
00012 namespace Core
00013 {
00014 namespace VideoSet
00015 {
00016
00017
00018 class IndexFeaturesSubset : public Listener
00019 {
00020 public:
00021
00022 IndexFeaturesSubset(Reporter* reporter, CmdOptions& options)
00023 {
00024 mReporter = reporter;
00025 mDef = Feature::FeatureDefinition(options.GetArg(2));
00026 mTable = 0;
00027 mKeyframes = 0;
00028 mKeyframeMask = 0;
00029 mWalkType = "unknown";
00030
00031 mQuidList = options.GetString("subset","");
00032 mAnnoQuidTab = NULL;
00033 mbInitialize = false;
00034
00035 mVerbose = options.GetBool("verbose", false);
00036
00037 }
00038
00039 ~IndexFeaturesSubset()
00040 {
00041 if (mAnnoQuidTab != NULL)
00042 delete mAnnoQuidTab;
00043 }
00044
00045 virtual void
00046 HandleNewWalk(VideoSet* vs, String walkType)
00047 {
00048 mWalkType = walkType;
00049 }
00050
00051 virtual void
00052 HandleNewWalk(VideoSet* vs, Keyframes* keyframes, bool* keyframeMask)
00053 {
00054 mKeyframes = keyframes;
00055 mKeyframeMask = keyframeMask;
00056 }
00057
00058 virtual void
00059 HandleDoneFile(VideoSet* vs, int fileId, Stream::RgbDataSrc* src)
00060 {
00061 String fName = vs->GetFilePathFeatureData(mWalkType, mDef,
00062 fileId, false, -1, false,
00063 false);
00064 if (fName.empty())
00065 {
00066 return;
00067 }
00068 Feature::FeatureTable tmp(mDef);
00069 Table::Read(&tmp, fName, vs->GetDatabase());
00070 if (mTable == 0)
00071 {
00072 mTable = new Feature::FeatureTable(mDef, 0,
00073 tmp.GetFeatureVectorLength());
00074 }
00075
00076
00077 if (!mbInitialize)
00078 {
00079 mAnnoQuidTab = new Core::Table::QuidTable;
00080
00081 Core::Table::Read(mAnnoQuidTab, mQuidList, vs->GetDatabase());
00082
00083 mAnnoTabSize = mAnnoQuidTab->Capacity();
00084
00085 mbInitialize = true;
00086 }
00087
00088 int nFeatureTabSize = tmp.Capacity();
00089
00090 bool* mask = new bool[nFeatureTabSize];
00091 int nMatched = 0;
00092
00093
00094 for (int i=0; i<nFeatureTabSize; i++)
00095 {
00096 Quid q = tmp.Get1(i);
00097
00098 bool bMatched = false;
00099
00100
00101 for (int j=0; j<mAnnoTabSize; j++)
00102 {
00103 Quid q2 = mAnnoQuidTab->Get1(j);
00104
00105 if (q == q2)
00106 {
00107 bMatched = true;
00108 break;
00109 }
00110 }
00111
00112
00113 if (bMatched)
00114 {
00115 mask[i] = true;
00116 nMatched ++;
00117 }
00118 else
00119 {
00120 mask[i] = false;
00121 }
00122 }
00123
00124 if (mVerbose)
00125 {
00126 std::cout << vs->GetFile(fileId) << " Mask: " << nMatched << " positives (out of ";
00127 std::cout << nFeatureTabSize << ")" << std::endl;
00128 for (int k=0; k<nFeatureTabSize; k++)
00129 std::cout << mask[k];
00130 std::cout << std::endl;
00131 }
00132
00133 Table::Select(mTable, &tmp, mask, false);
00134
00135 delete []mask;
00136
00137 }
00138
00139 virtual void
00140 HandleDoneWalk(VideoSet* vs)
00141 {
00142 String fName = vs->GetFilePathFeatureIndex(mDef, "", true, false);
00143 if (! fName.empty())
00144 Table::Write(mTable, fName, vs->GetDatabase(), true);
00145 }
00146
00147 private:
00148
00149 Reporter* mReporter;
00150 Feature::FeatureDefinition mDef;
00151 Feature::FeatureTable* mTable;
00152 Keyframes* mKeyframes;
00153 bool* mKeyframeMask;
00154 String mWalkType;
00155
00156 String mQuidList;
00157 Core::Table::QuidTable* mAnnoQuidTab;
00158 int mAnnoTabSize;
00159 bool mbInitialize;
00160
00161 bool mVerbose;
00162
00163 };
00164
00165 }
00166 }
00167 }
00168
00169 #endif