00001 #ifndef Impala_Core_VideoSet_IndexConcepts_h
00002 #define Impala_Core_VideoSet_IndexConcepts_h
00003
00004 #include "Core/Table/SimilarityTableSet.h"
00005 #include "Core/VideoSet/Reporter.h"
00006 #include "Core/VideoSet/Segmentation.h"
00007
00008 namespace Impala
00009 {
00010 namespace Core
00011 {
00012 namespace VideoSet
00013 {
00014
00015
00016 class IndexConcepts : public Listener
00017 {
00018 public:
00019 typedef Feature::FeatureDefinition FeatureDefinition;
00020 typedef Table::SimilarityTableSet SimilarityTableSet;
00021 typedef Table::SimilarityTableSet::SimTableType SimTableType;
00022
00023 IndexConcepts(Reporter* reporter, CmdOptions& options)
00024 {
00025 mReporter = reporter;
00026 if (options.GetNrArg() < 5)
00027 {
00028 ILOG_ERROR("Missing argument");
00029 return;
00030 }
00031 mConceptFileName = options.GetArg(2);
00032 mModel = options.GetArg(3);
00033 mFeatureDef = FeatureDefinition(options.GetArg(4));
00034 mSimSet = 0;
00035 mSegmentation = 0;
00036 }
00037
00038 virtual void
00039 HandleNewWalk(VideoSet* vs, String walkType)
00040 {
00041 mWalkType = walkType;
00042 }
00043
00044 virtual void
00045 HandleNewWalk(VideoSet* vs, Segmentation* segmentation)
00046 {
00047 mSegmentation = segmentation;
00048 }
00049
00050 virtual void
00051 HandleDoneFile(VideoSet* vs, int fileId, Stream::RgbDataSrc* src)
00052 {
00053 SimilarityTableSet* simSet = SimilarityTableSet::MakeFromFile
00054 (vs, mWalkType, mConceptFileName, mModel, mFeatureDef.AsString(),
00055 fileId);
00056 if (!simSet)
00057 {
00058 ILOG_ERROR("Could not read SimilarityTableSet for file " << fileId);
00059 return;
00060 }
00061 simSet->ChangeQuidObject(fileId);
00062 if (mSimSet == 0)
00063 {
00064 std::vector<String> names = simSet->GetNames();
00065 mSimSet = new SimilarityTableSet(names, 1000000);
00066 }
00067 if (mSegmentation)
00068 {
00069 SimilarityTableSet* rkfSet = MapSimilaritiesOnShots(simSet, fileId);
00070 mSimSet->Append(rkfSet);
00071 delete rkfSet;
00072 }
00073 else
00074 {
00075 mSimSet->Append(simSet);
00076 }
00077 delete simSet;
00078 }
00079
00080 virtual void
00081 HandleDoneWalk(VideoSet* vs)
00082 {
00083 mSimSet->ComputeRanks(true);
00084 String resName = mFeatureDef.AsString();
00085 if (mWalkType != "Keyframes")
00086 resName = mWalkType + "-" + resName;
00087 if (mSegmentation)
00088 resName = "shot" + resName;
00089 mSimSet->Save(vs, mConceptFileName, mModel, resName, true);
00090 delete mSimSet;
00091 }
00092
00093 private:
00094
00095 SimilarityTableSet*
00096 MapSimilaritiesOnShots(SimilarityTableSet* simSet, int vidId)
00097 {
00098 Table::QuidTable* frameQuids = simSet->GetQuidTable();
00099 int firstShot = mSegmentation->GetFirstShotVideo(vidId);
00100 int nrShots = mSegmentation->GetNrShotsVideo(vidId);
00101 SimilarityTableSet* res = new SimilarityTableSet(simSet->GetNames(),
00102 nrShots);
00103 Table::QuidTable* shotQuids = res->GetQuidTable();
00104 for (int i=firstShot ; i<firstShot+nrShots ; i++)
00105 {
00106 Quid q = mSegmentation->GetQuidShot(i);
00107 shotQuids->Add(q);
00108 }
00109 for (int t=0 ; t<simSet->NrTables() ; t++)
00110 {
00111 SimTableType* frameSimTable = simSet->GetSimTable(t);
00112 SimTableType* shotSimTable = res->GetSimTable(t);
00113 int lastShot = -1;
00114 Real64 lastSim = 0;
00115 for (int i=0 ; i<frameSimTable->Size() ; i++)
00116 {
00117 Quid frameQ = frameQuids->Get1(i);
00118 Real64 frameSim = frameSimTable->Get1(i);
00119 int shot = mSegmentation->GetShotId(frameQ);
00120 if (shot != lastShot)
00121 {
00122 if (lastShot != -1)
00123 {
00124 Quid check = mSegmentation->GetQuidShot(lastShot);
00125 int rank = shotSimTable->Size();
00126 if (check != shotQuids->Get1(rank))
00127 {
00128 ILOG_ERROR("Quids do not match");
00129 }
00130 shotSimTable->Add(lastSim);
00131 }
00132 lastSim = frameSim;
00133 lastShot = shot;
00134 }
00135 else
00136 {
00137 if (frameSim > lastSim)
00138 {
00139 lastSim = frameSim;
00140 }
00141 }
00142 }
00143 if (lastShot != -1)
00144 {
00145 Quid check = mSegmentation->GetQuidShot(lastShot);
00146 int rank = shotSimTable->Size();
00147 if (check != shotQuids->Get1(rank))
00148 {
00149 ILOG_ERROR("Quids do not match");
00150 }
00151 shotSimTable->Add(lastSim);
00152 }
00153 if (shotSimTable->Size() < shotQuids->Size())
00154 {
00155 ILOG_ERROR("Missing shot similarities for " <<
00156 simSet->GetName(t));
00157 for (int i=shotSimTable->Size() ; i<shotQuids->Size() ; i++)
00158 shotSimTable->Add(0.0);
00159 }
00160 }
00161 return res;
00162 }
00163
00164
00165 Reporter* mReporter;
00166 String mConceptFileName;
00167 String mModel;
00168 FeatureDefinition mFeatureDef;
00169 SimilarityTableSet* mSimSet;
00170 Segmentation* mSegmentation;
00171 String mWalkType;
00172
00173 ILOG_VAR_DEC;
00174
00175 };
00176
00177 ILOG_VAR_INIT(IndexConcepts, Impala.Core.VideoSet);
00178
00179 }
00180 }
00181 }
00182
00183 #endif