00001 #ifndef Impala_Core_Trec_ThreadVisualSimilarity_h
00002 #define Impala_Core_Trec_ThreadVisualSimilarity_h
00003
00004 #include "Basis/NativeTypes.h"
00005 #include "Core/Table/TableTem.h"
00006 #include "Core/Vector/NormalizationMax.h"
00007 #include "Core/Vector/Similarity.h"
00008 #include "Core/Vector/Norm1Dist.h"
00009 #include "Core/Feature/FeatureTable.h"
00010 #include "Core/Table/SimilarityTableSet.h"
00011 #include "Core/Table/Read.h"
00012 #include "Core/Column/Find.h"
00013 #include "Core/Trec/Thread.h"
00014 #include "Core/VideoSet/Keyframes.h"
00015 #include "Core/Feature/VisSem.h"
00016
00017 namespace Impala
00018 {
00019 namespace Core
00020 {
00021 namespace Trec
00022 {
00023
00024
00025 typedef Table::TableTem<Column::ColumnTem<Int32>,
00026 Column::ColumnTem<Int32>,
00027 Column::ColumnTem<Real64> > TableShotsIndexScore;
00028
00029 class ThreadVisualSimilarity : public Thread
00030 {
00031 public:
00032 typedef Vector::VectorTem<Real64> VectorReal64;
00033
00034 ThreadVisualSimilarity(CString feature, VideoSet::Keyframes* keyframes)
00035 : Thread(String("visual_") + feature)
00036 {
00037 String qualName = feature;
00038 if (feature == "vissem" || feature == "vissemgabor" ||
00039 feature == "fusionvissemgabor")
00040 qualName += "_proto_annotation_nrScales_2_nrRects_130";
00041 Feature::FeatureDefinition def(qualName);
00042 mFeatureTable = new Feature::FeatureTable(def);
00043
00044 mKeyframes = keyframes;
00045 String fName =
00046 mKeyframes->GetVideoSet()->GetFilePathFeatureIndex(def, "", false,
00047 false);
00048 if (fName.empty())
00049 {
00050 ILOG_DEBUG("fName is empty, attempting alternative.");
00051 Feature::FeatureDefinition def_alt(feature + "_proto_clusters_keyframes_all_maxlen_120_sp_0");
00052 mFeatureTable = new Feature::FeatureTable(def_alt);
00053 fName =
00054 mKeyframes->GetVideoSet()->GetFilePathFeatureIndex(def_alt, "", false,
00055 false);
00056 if (fName.empty()) {
00057 ILOG_DEBUG("fName is empty, failed.");
00058 mValid = false;
00059 return;
00060 }
00061 }
00062
00063 mShotsCache = new TableShotsIndexScore(0);
00064
00065 Table::Read(mFeatureTable, fName,
00066 mKeyframes->GetVideoSet()->GetDatabase());
00067 mKeyEqualShot = (mFeatureTable->Size() == mKeyframes->GetNrShots());
00068 Vector::NormalizationMax(mFeatureTable->GetColumn2());
00069 mOrigin = -1;
00070 mTopN = Min(mFeatureTable->Size() - 1, 1000);
00071 ILOG_DEBUG("mTopN for " << feature << " = " << mTopN);
00072 std::vector<String> names;
00073 names.push_back("test");
00074 mSimSet = new Table::SimilarityTableSet(names, mFeatureTable->Size());
00075 Core::Table::Copy(mSimSet->GetQuidTable(), mFeatureTable);
00076 mValid = true;
00077 }
00078
00079 bool
00080 GetValid() const
00081 {
00082 return mValid;
00083 }
00084
00085 virtual void
00086 SetOrigin(int shot)
00087 {
00088
00089
00090
00091 if (shot == mOrigin) {
00092 ILOG_DEBUG("SetOrigin: " << shot << " length: " << mTopN << " NO RECALC");
00093 return;
00094 }
00095
00096 int key = Shot2Key(shot);
00097 Quid quid = mKeyframes->GetQuidFrame(key);
00098 ILOG_DEBUG("SetOrigin: " << shot << " key=" << key << " quid=" <<
00099 quid << " length: " << mTopN);
00100
00101 Table::SimilarityTableSet::SimTableType* sTable =
00102 mSimSet->GetSimTable(0);
00103 Real64* sim = sTable->GetColumn1()->GetData();
00104 Vector::Similarity(sim, mFeatureTable->FindFeature(quid),
00105 mFeatureTable->GetColumn2(), 0, 1,
00106 Vector::Norm1Dist<Real64>);
00107 sTable->SetSize(mFeatureTable->Size());
00108 int nr = Min(mFeatureTable->Size(), mTopN);
00109 mSimSet->ComputeRanksTopN(nr, false);
00110
00111
00112 mShotsCache->SetEmpty();
00113 std::map<int,bool> noduplicates;
00114 Table::SimilarityTableSet::RankTableType* ranktable = mSimSet->GetRankTable(0);
00115 Table::SimilarityTableSet::SimTableType* scoretable = mSimSet->GetSimTable(0);
00116 for (int i = 0; i < nr; i ++)
00117 {
00118 Quid thisresult = ranktable->Get1(i);
00119 int keyframe = mKeyframes->GetFrameId(thisresult);
00120 Real64 score = scoretable->Get1(mSimSet->FindQuid(thisresult));
00121
00122 if (keyframe < 0)
00123 continue;
00124
00125 int shot = mKeyframes->GetShotId(keyframe);
00126
00127 if (shot >= 0 &&
00128 noduplicates.find(shot) == noduplicates.end())
00129 {
00130 mShotsCache->Add(shot, i, score);
00131 noduplicates[shot] = true;
00132 }
00133 }
00134
00135 mOrigin = shot;
00136 }
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 Quid
00165 GetNearestFrameQuid(VectorReal64 exampleFeatureVector)
00166 {
00167 Table::SimilarityTableSet::SimTableType* sTable =
00168 mSimSet->GetSimTable(0);
00169 Real64* sim = sTable->GetColumn1()->GetData();
00170 Vector::Similarity(sim, exampleFeatureVector,
00171 mFeatureTable->GetColumn2(), 0, 1,
00172 Vector::Norm1Dist<Real64>);
00173 sTable->SetSize(mFeatureTable->Size());
00174 int nr = Min(mFeatureTable->Size(), 8);
00175
00176 mSimSet->ComputeRanksTopN(nr, false);
00177
00178
00179 Table::SimilarityTableSet::RankTableType* ranktable = mSimSet->GetRankTable(0);
00180 Table::SimilarityTableSet::SimTableType* scoretable = mSimSet->GetSimTable(0);
00181
00182 Quid rquid = ranktable->Get1(0);
00183
00184
00185
00186 int ver_keyframe = mKeyframes->GetFrameId(rquid);
00187 ILOG_INFO("GetNearestFrameQuid:" << QuidObj(rquid) << " keyframe: " << ver_keyframe );
00188 if (mKeyframes->IsRKF(ver_keyframe))
00189 {
00190 ILOG_INFO("is rkf");
00191 }
00192
00193 if (ver_keyframe > mKeyframes->GetNrKeyframes())
00194 {
00195 ILOG_ERROR("Keyframe does not exist!");
00196 }
00197
00198
00199 int shot = mKeyframes->GetShotId(ver_keyframe);
00200 int rkf_keyframe = Shot2Key(shot);
00201 Quid rrquid = mKeyframes->GetQuidFrame(rkf_keyframe);
00202 if (rrquid != rquid)
00203 ILOG_INFO("Quid " << QuidObj(rrquid) << " differs fromr result!");
00204 mOrigin = -1;
00205 return rrquid;
00206 }
00207
00208 void
00209 SetMaxReturned(int nr)
00210 {
00211 mTopN = Min(nr, mFeatureTable->Size() - 1);
00212 }
00213
00214 int
00215 GetMaxReturned()
00216 {
00217 return mTopN;
00218 }
00219
00220 Real64
00221 GetSimilarity(int shot)
00222 {
00223 int rankIndex = GetShotPosition(shot);
00224 if (rankIndex < 0)
00225 return -1.0;
00226
00227 return mShotsCache->Get3(rankIndex);
00228 }
00229
00230 virtual int
00231 GetShot(int position)
00232 {
00233 if ((position < 0) || (position >= mShotsCache->Size()))
00234 return -1;
00235 return mShotsCache->Get1(position);
00236 }
00237
00238
00239
00240
00241
00242
00243
00244 virtual int
00245 GetLength()
00246 {
00247 return mShotsCache->Size();
00248 }
00249
00250 virtual int
00251 GetShotAt(int shot, int offset)
00252 {
00253 int r = Column::Find(mShotsCache->GetColumn1(), shot, 0, mShotsCache->Size());
00254 int dst = r + offset;
00255 if ((dst < 0) || (dst >= mShotsCache->Size())) {
00256 return -1;
00257 }
00258 return mShotsCache->Get1(dst);
00259 }
00260
00261 virtual int
00262 GetShotPosition(int shot)
00263 {
00264 int r = Column::Find(mShotsCache->GetColumn1(), shot, 0, mShotsCache->Size());
00265 if (r == mShotsCache->Size())
00266 return -1;
00267 return r;
00268 }
00269
00270 virtual int
00271 GetType()
00272 {
00273 return VISUAL;
00274 }
00275
00276 private:
00277
00278 int
00279 Shot2Key(int shot)
00280 {
00281
00282 return mKeyframes->GetShotRKF(shot);
00283 }
00284
00285 int
00286 Key2Shot(int key)
00287 {
00288 return (mKeyEqualShot) ? key : mKeyframes->GetShotId(key);
00289 }
00290
00291 VideoSet::Keyframes* mKeyframes;
00292 Feature::FeatureTable* mFeatureTable;
00293 int mOrigin;
00294 int mTopN;
00295 Table::SimilarityTableSet* mSimSet;
00296 bool mValid;
00297 bool mKeyEqualShot;
00298
00299 TableShotsIndexScore* mShotsCache;
00300
00301 ILOG_VAR_DEC;
00302
00303 };
00304
00305 ILOG_VAR_INIT(ThreadVisualSimilarity, Core.Trec);
00306
00307
00308 }
00309 }
00310 }
00311
00312 #endif