00001 #ifndef Impala_Core_VideoSet_IndexConcepts_h
00002 #define Impala_Core_VideoSet_IndexConcepts_h
00003
00004 #include "Core/VideoSet/Reporter.h"
00005 #include "Persistency/SimilarityTableSetRepository.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 typedef Persistency::SimilarityTableSetLocator SimilarityTableSetLocator;
00023 typedef Persistency::SimilarityTableSetRepository SimilarityTableSetRepository;
00024
00025 IndexConcepts(Reporter* reporter, CmdOptions& options, VideoSet* vs)
00026 {
00027 if (options.GetNrArg() < 5)
00028 {
00029 ILOG_ERROR("Missing argument");
00030 return;
00031 }
00032 mLoc = SimilarityTableSetLocator
00033 (vs->GetLocator(), false, "", options.GetArg(2), options.GetArg(3),
00034 options.GetArg(4), "");
00035 mSimSet = 0;
00036 mSegmentation = 0;
00037 }
00038
00039 virtual void
00040 HandleNewWalk(VideoSet* vs, String walkType)
00041 {
00042 mLoc.SetWalkType(walkType);
00043 }
00044
00045 virtual void
00046 HandleNewWalk(VideoSet* vs, Segmentation* segmentation)
00047 {
00048 mSegmentation = segmentation;
00049 }
00050
00051 virtual void
00052 HandleDoneFile(VideoSet* vs, int fileId, Stream::RgbDataSrc* src)
00053 {
00054 mLoc.SetContainer(vs->GetContainer(fileId));
00055 SimilarityTableSet* simSet = SimilarityTableSetRepository().Get(mLoc);
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 = mLoc.GetFeature();
00085 if (mLoc.GetWalkType() != "Keyframes")
00086 resName = mLoc.GetWalkType() + "-" + resName;
00087 if (mSegmentation)
00088 resName = "shot" + resName;
00089 mLoc.SetInIndex(true);
00090 mLoc.SetFeature(resName);
00091 SimilarityTableSetRepository().Add(mLoc, mSimSet);
00092 delete mSimSet;
00093 }
00094
00095 private:
00096
00097 SimilarityTableSet*
00098 MapSimilaritiesOnShots(SimilarityTableSet* simSet, int vidId)
00099 {
00100 Table::QuidTable* frameQuids = simSet->GetQuidTable();
00101 int firstShot = mSegmentation->GetFirstShotVideo(vidId);
00102 int nrShots = mSegmentation->GetNrShotsVideo(vidId);
00103 SimilarityTableSet* res = new SimilarityTableSet(simSet->GetNames(),
00104 nrShots);
00105 Table::QuidTable* shotQuids = res->GetQuidTable();
00106 for (int i=firstShot ; i<firstShot+nrShots ; i++)
00107 {
00108 Quid q = mSegmentation->GetQuidShot(i);
00109 shotQuids->Add(q);
00110 }
00111 for (int t=0 ; t<simSet->NrTables() ; t++)
00112 {
00113 SimTableType* frameSimTable = simSet->GetSimTable(t);
00114 SimTableType* shotSimTable = res->GetSimTable(t);
00115 int lastShot = -1;
00116 Real64 lastSim = 0;
00117 for (int i=0 ; i<frameSimTable->Size() ; i++)
00118 {
00119 Quid frameQ = frameQuids->Get1(i);
00120 Real64 frameSim = frameSimTable->Get1(i);
00121 int shot = mSegmentation->GetShotId(frameQ);
00122 if (shot != lastShot)
00123 {
00124 if (lastShot != -1)
00125 {
00126 Quid check = mSegmentation->GetQuidShot(lastShot);
00127 int rank = shotSimTable->Size();
00128 if (check != shotQuids->Get1(rank))
00129 {
00130 ILOG_ERROR("Quids do not match");
00131 }
00132 shotSimTable->Add(lastSim);
00133 }
00134 lastSim = frameSim;
00135 lastShot = shot;
00136 }
00137 else
00138 {
00139 if (frameSim > lastSim)
00140 {
00141 lastSim = frameSim;
00142 }
00143 }
00144 }
00145 if (lastShot != -1)
00146 {
00147 Quid check = mSegmentation->GetQuidShot(lastShot);
00148 int rank = shotSimTable->Size();
00149 if (check != shotQuids->Get1(rank))
00150 {
00151 ILOG_ERROR("Quids do not match");
00152 }
00153 shotSimTable->Add(lastSim);
00154 }
00155 if (shotSimTable->Size() < shotQuids->Size())
00156 {
00157 ILOG_ERROR("Missing shot similarities for " <<
00158 simSet->GetName(t));
00159 for (int i=shotSimTable->Size() ; i<shotQuids->Size() ; i++)
00160 shotSimTable->Add(0.0);
00161 }
00162 }
00163 return res;
00164 }
00165
00166 SimilarityTableSetLocator mLoc;
00167 SimilarityTableSet* mSimSet;
00168 Segmentation* mSegmentation;
00169
00170 ILOG_VAR_DEC;
00171
00172 };
00173
00174 ILOG_VAR_INIT(IndexConcepts, Impala.Core.VideoSet);
00175
00176 }
00177 }
00178 }
00179
00180 #endif