00001 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00002 #else // REPOSITORY_USED
00003 #define REPOSITORY_TODO
00004 #endif // REPOSITORY_USED
00005
00006 #include "Persistency/SegmentationRepository.h"
00007 #include "Persistency/FrameRepository.h"
00008 #include "Persistency/KeywordListRepository.h"
00009 #include "Util/Channel.h"
00010 #include "Basis/CmdOptions.h"
00011 #include "Util/SimpleMap.h"
00012 #include "Util/StringParser.h"
00013 #include "Core/Table/AveragePrecision.h"
00014 #include "Core/Table/AveragePrecisionConfusion.h"
00015 #include "Core/Table/AnnotationTableSet.h"
00016 #include "Core/Table/SimilarityTableSet.h"
00017 #include "Core/Table/SelectQuids.h"
00018 #include "Core/Trec/TrecTopic.h"
00019 #include "Core/Trec/Collection.h"
00020 #include "Core/Trec/SearchJudge.h"
00021 #include "Core/Array/Element/CheckNativeTypeSize.h"
00022 #include "Core/Array/WritePng.h"
00023 #include "Core/Column/AddAssign.h"
00024 #include "Core/Column/MulAssign.h"
00025 #include "Core/Column/DivAssign.h"
00026 #include "Core/Database/MakeRawDataSet.h"
00027 #include "Core/VideoSet/Stills.h"
00028 #include "Core/VideoSet/Keyframes.h"
00029 #include "Core/VideoSet/Mpeg7DocWrite.h"
00030
00031
00032
00033 #include "Link/ImpalaLib.cpp"
00034
00035 namespace Impala
00036 {
00037 namespace Application
00038 {
00039 namespace Util
00040 {
00041
00042
00043 using namespace Persistency;
00044
00045 void
00046 DoTrecTopicData()
00047 {
00048 typedef Core::VideoSet::VideoSet VideoSet;
00049 typedef Core::VideoSet::Keyframes Keyframes;
00050 typedef Core::Array::Array2dVec3UInt8 Array2dVec3UInt8;
00051
00052 ILOG_VAR(Impala.Application.Util.DoTrecTopicData);
00053 CmdOptions& options = CmdOptions::GetInstance();
00054 if (options.GetNrArg() < 4)
00055 {
00056 ILOG_ERROR("Need more parameters");
00057 return;
00058 }
00059 VideoSet* sVidSet = Core::VideoSet::MakeVideoSet(options.GetArg(1));
00060 Core::Trec::TrecTopic topic(options.GetArg(2));
00061 ILOG_DEBUG("Generating data sets");
00062 String dSetName = topic.GenerateDataSets(options.GetArg(3));
00063 bool doVideo = true;
00064 if (options.GetNrArg() > 4)
00065 if (options.GetArg(4) == "novideo")
00066 doVideo = false;
00067 VideoSet* dVidSet = Core::VideoSet::MakeVideoSet(dSetName);
00068 KeyframesLocator kLoc(dSetName, "keyframes", options);
00069 Keyframes* keyframes = KeyframesRepository().Get(kLoc, dVidSet);
00070 int curKey = -1;
00071 for (int i=0 ; i<topic.NrTopic() ; i++)
00072
00073 {
00074 for (int j=0 ; j<topic.GetNrExamples(i) ; j++)
00075 {
00076 curKey++;
00077 ILOG_INFO("nr " << curKey << " of " << keyframes->GetNrKeyframes());
00078 ILOG_DEBUG("topic = " << i << ", example = " << j);
00079 ILOG_DEBUG("topic = " << topic.GetTopicNum(i));
00080 String vidName = topic.GetVideoExampleSrcFile(i, j);
00081 ILOG_DEBUG("vidName = " << vidName);
00082 int vidId = sVidSet->GetFileId(vidName);
00083 if (vidId == -1)
00084 {
00085 ILOG_ERROR("Unable to locate video " << vidName);
00086 continue;
00087 }
00088 Core::Stream::RgbDataSrc* src = sVidSet->GetVideo(vidId);
00089 if (src == 0)
00090 {
00091 ILOG_ERROR("Unable to locate video " << vidName);
00092 continue;
00093 }
00094 int startFr = topic.GetVideoExampleStartFrame(i, j);
00095 int endFr = topic.GetVideoExampleEndFrame(i, j);
00096 src->GotoFrame((startFr + endFr) / 2);
00097 Array2dVec3UInt8* wrapper = Core::Array::ArrayCreate<Array2dVec3UInt8>
00098 (src->FrameWidth(), src->FrameHeight(), 0, 0, src->DataPtr(),
00099 true);
00100 int frameNr = keyframes->GetFrameNr(curKey);
00101 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00102 String fName = dVidSet->GetFilePathFrames(curKey, frameNr, ".png",
00103 true, false);
00104 if (fName.empty())
00105 {
00106 ILOG_ERROR("Cannot write keyframe");
00107 }
00108 else
00109 {
00110 Core::Array::WritePng(wrapper, fName, dVidSet->GetDatabase());
00111 }
00112 #else // REPOSITORY_USED
00113 String container = dVidSet->GetContainerFile(curKey);
00114 Persistency::FrameLocator loc(dVidSet->GetLocator(), container,
00115 frameNr, ".png");
00116 Persistency::FrameRepository().Add(loc, wrapper);
00117 #endif // REPOSITORY_USED
00118 delete wrapper;
00119 delete src;
00120
00121 if (!doVideo)
00122 continue;
00123
00124
00125 String exeName("mencoder -oac copy -ovc copy -of mpeg");
00126 Impala::Util::StringParser pStart(topic.GetVideoExampleStart(i, j));
00127 int startM = pStart.GetInt('m');
00128 pStart.AdvanceP();
00129 int startS = pStart.GetInt('.');
00130 pStart.AdvanceP();
00131 int startF = pStart.GetInt('s');
00132 String duration = topic.GetVideoExampleDuration(i, j);
00133 String vidFileName = sVidSet->GetResolvedVideoPath(vidId);
00134 char sysBuf[1024];
00135 sprintf(sysBuf, "%s %s -o %s -ss %d:%d.%d -endpos %s",
00136 exeName.c_str(), vidFileName.c_str(),
00137 topic.GetVideoExampleName(i, j, true).c_str(),
00138 startM, startS, startF, duration.c_str());
00139
00140 for (int c=0 ; c<1024 ; c++)
00141 if (sysBuf[c] == '/')
00142 sysBuf[c] = '\\';
00143
00144 ILOG_DEBUG("sysbuf = [" << sysBuf << "]");
00145 int sysRes = system(sysBuf);
00146 if (sysRes != 0)
00147 {
00148 ILOG_ERROR("system [" << sysBuf << "]");
00149 return;
00150 }
00151 }
00152 }
00153 delete sVidSet;
00154 delete dVidSet;
00155 }
00156
00157 void
00158 DoTrecSet()
00159 {
00160 ILOG_VAR(Impala.Application.Util.DoTrecSet);
00161 CmdOptions& options = CmdOptions::GetInstance();
00162 if (options.GetNrArg() < 4)
00163 {
00164 ILOG_ERROR("Need at least 4 arguments");
00165 return;
00166 }
00167 String setName = options.GetArg(1);
00168 String use = options.GetArg(2);
00169 String collName = options.GetArg(3);
00170 String clipsKeys("");
00171 if (options.GetNrArg() > 4)
00172 clipsKeys = options.GetArg(4);
00173 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00174 Core::Trec::Collection coll(collName, &Impala::Util::Database::GetInstance());
00175 #else // REPOSITORY_USED
00176 Persistency::FileLocator loc(collName);
00177 Core::Trec::Collection coll(loc);
00178 #endif // REPOSITORY_USED
00179
00180 coll.WriteVideoSet(setName, use, clipsKeys);
00181 }
00182
00183 void
00184 DoTrecJudge()
00185 {
00186 #ifndef REPOSITORY_TODO
00187 ILOG_VAR(Impala.Application.Util.DoTrecJudge);
00188 CmdOptions& options = CmdOptions::GetInstance();
00189 if (options.GetNrArg() < 4)
00190 {
00191 ILOG_ERROR("Need at least 4 arguments");
00192 return;
00193 }
00194 String judgeFile = options.GetArg(1);
00195 String shotFile = options.GetArg(2);
00196 String topic = options.GetArg(3);
00197 Core::Trec::SearchJudge judge(judgeFile);
00198 judge.Stats();
00199 std::vector<String> shots;
00200 Impala::Util::Database* db = &Impala::Util::Database::GetInstance();
00201 Impala::Util::DatabaseReadStrings(shots, shotFile, db);
00202
00203 Real64 infAP = judge.Judge(topic, shots);
00204 std::cout << "infAP = " << infAP << std::endl;
00205 #endif
00206 }
00207
00208 void
00209 DoDumpStills()
00210 {
00211 typedef Core::VideoSet::VideoSet VideoSet;
00212 typedef Core::VideoSet::Stills Stills;
00213
00214 ILOG_VAR(Impala.Application.Util.DoDumpStills);
00215 CmdOptions& options = CmdOptions::GetInstance();
00216 String fName = options.GetArg(1);
00217 VideoSet* vidSet = Core::VideoSet::MakeVideoSet(fName);
00218 Stills stills(vidSet, "stills");
00219 ILOG_INFO("nrVideos = " << stills.GetNrVideos());
00220 ILOG_INFO("nrShots = " << stills.GetNrShots());
00221 ILOG_INFO("nrStills = " << stills.GetNrStills());
00222
00223 stills.Dump(options.GetInt("start"), options.GetInt("end"));
00224 }
00225
00226 void
00227 DoExportShotMpeg7()
00228 {
00229 typedef Core::VideoSet::VideoSet VideoSet;
00230 typedef Core::VideoSet::Segmentation Segmentation;
00231 typedef Core::VideoSet::Keyframes Keyframes;
00232 typedef Core::VideoSet::Mpeg7Doc Mpeg7Doc;
00233
00234 ILOG_VAR(Impala.Application.Util.DoExportShotMpeg7);
00235 CmdOptions& options = CmdOptions::GetInstance();
00236 if (options.GetNrArg() < 5)
00237 {
00238 ILOG_ERROR("Need more parameters");
00239 return;
00240 }
00241 String videoSetName = options.GetArg(1);
00242 int fractions = Impala::atol(options.GetArg(2));
00243 if ((fractions != 25) && (fractions != 30000))
00244 ILOG_WARN("Never tried fractions = " << fractions);
00245 bool generateShotNames = Impala::atol(options.GetArg(3));
00246 bool withKeyframes = Impala::atol(options.GetArg(4));
00247
00248 VideoSet* vidSet = Core::VideoSet::MakeVideoSet(videoSetName);
00249 SegmentationLocator sLoc(videoSetName, "segmentation", options);
00250 Segmentation* segmentation = SegmentationRepository().Get(sLoc, vidSet);
00251 KeyframesLocator kLoc(videoSetName, "keyframes", options);
00252 Keyframes* keyframes = KeyframesRepository().Get(kLoc, vidSet);
00253 for (int v=0 ; v<vidSet->NrFiles() ; v++)
00254 {
00255 String fileName = vidSet->GetFile(v);
00256 ILOG_INFO("doing file " << fileName);
00257 String id = fileName;
00258 int nrFrames = segmentation->GetNrFramesVideo(v);
00259 Mpeg7Doc mp7(id, fileName, nrFrames, fractions, withKeyframes, "", "");
00260 for (int s=0 ; s<segmentation->GetNrShotsVideo(v) ; s++)
00261 {
00262 int shot = segmentation->GetFirstShotVideo(v) + s;
00263 String shotName = (generateShotNames)
00264 ? "shot" + MakeString(v+1) + "_" + MakeString(s+1)
00265 : segmentation->GetName(shot);
00266 if (withKeyframes)
00267 {
00268 std::vector<String> names;
00269 std::vector<int> frames;
00270 for (int k=0 ; k<keyframes->GetNrKeyframesShot(shot) ; k++)
00271 {
00272 int key = keyframes->GetFirstKeyframeShot(shot) + k;
00273 String keyName = (generateShotNames)
00274 ? "shot"+MakeString(v+1)+"_"+MakeString(s+1)+"_RKF"
00275 : Impala::FileNameBase(keyframes->GetName(key));
00276 names.push_back(keyName);
00277 frames.push_back(keyframes->GetFrameNr(key));
00278 }
00279 mp7.AddShot(shotName, segmentation->GetStart(shot),
00280 segmentation->GetEnd(shot), names, frames);
00281 }
00282 else
00283 {
00284 mp7.AddShot(shotName, segmentation->GetStart(shot),
00285 segmentation->GetEnd(shot));
00286 }
00287 }
00288 Core::VideoSet::Mpeg7DocWrite(&mp7, vidSet, v);
00289 }
00290 }
00291
00292
00293 void
00294 DoExportAnnoMpeg7()
00295 {
00296 typedef Core::VideoSet::VideoSet VideoSet;
00297 typedef Core::VideoSet::Segmentation Segmentation;
00298 typedef Core::VideoSet::Keyframes Keyframes;
00299 typedef Core::VideoSet::Mpeg7Doc Mpeg7Doc;
00300
00301 ILOG_VAR(Impala.Application.Util.DoExportAnnoMpeg7);
00302 CmdOptions& options = CmdOptions::GetInstance();
00303 if (options.GetNrArg() < 5)
00304 {
00305 ILOG_ERROR("Need more parameters");
00306 return;
00307 }
00308 String videoSetName = options.GetArg(1);
00309 int fractions = Impala::atol(options.GetArg(2));
00310 if ((fractions != 25) && (fractions != 30000))
00311 ILOG_WARN("Never tried fractions = " << fractions);
00312 String conceptSet = options.GetArg(3);
00313 String anno = options.GetArg(4);
00314 bool hasKeyframes = false;
00315 if (anno == "hasKeyframes")
00316 {
00317 hasKeyframes = true;
00318 }
00319 else if (anno != "hasShots")
00320 {
00321 ILOG_ERROR("Argument should be hasShots or hasKeyframes");
00322 return;
00323 }
00324
00325 VideoSet* vidSet = Core::VideoSet::MakeVideoSet(videoSetName);
00326 SegmentationLocator sLoc(videoSetName, "segmentation", options);
00327 Segmentation* segmentation = SegmentationRepository().Get(sLoc, vidSet);
00328 KeyframesLocator kLoc(videoSetName, "keyframes", options);
00329 Keyframes* keyframes = KeyframesRepository().Get(kLoc, vidSet);
00330 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00331 String fName = vidSet->GetFilePathAnnotation(conceptSet, false, false);
00332 if (fName.empty())
00333 {
00334 ILOG_ERROR("unable to read conceptSet");
00335 return;
00336 }
00337 std::vector<String> concepts;
00338 Impala::Util::Database* db = vidSet->GetDatabase();
00339 Impala::Util::DatabaseReadStrings(concepts, fName, db);
00340 #else // REPOSITORY_USED
00341 Persistency::KeywordListLocator loc(vidSet->GetLocator(), conceptSet);
00342 Core::Table::KeywordList concepts =
00343 *(Persistency::KeywordListRepository().Get(loc));
00344 #endif // REPOSITORY_USED
00345 for (int c=0 ; c<concepts.size() ; c++)
00346 {
00347 ILOG_INFO("doing concept " << concepts[c]);
00348 ILOG_NDC_PUSH(concepts[c]);
00349 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00350 fName = vidSet->GetFilePathGroundTruth(concepts[c] + ".truth", false,
00351 false);
00352 if (fName.empty())
00353 {
00354 ILOG_INFO("no annotations found for " << concepts[c]);
00355 ILOG_NDC_POP;
00356 continue;
00357 }
00358 std::vector<String> annos;
00359 Impala::Util::DatabaseReadStrings(annos, fName, db);
00360 #else // REPOSITORY_USED
00361 String fName = "Annotations/groundtruth/" + concepts[c] + ".truth";
00362 Persistency::FileLocator fLoc(vidSet->GetLocator(), fName);
00363 typedef Persistency::RepositoryInFileSystem FS;
00364 Persistency::File file = FS::GetInstance().GetFile(fLoc, false, false);
00365 std::vector<String> annos;
00366 file.ReadStrings(annos);
00367 #endif // REPOSITORY_USED
00368 std::sort(annos.begin(), annos.end());
00369 for (int v=0 ; v<vidSet->NrFiles() ; v++)
00370 {
00371 String vidFile = vidSet->GetFile(v);
00372 int nrFrames = segmentation->GetNrFramesVideo(v);
00373 Mpeg7Doc mp7(vidFile, vidFile, nrFrames, fractions, false,
00374 conceptSet, concepts[c]);
00375 if (hasKeyframes)
00376 {
00377 for (int k=0 ; k<keyframes->GetNrKeyframesVideo(v) ; k++)
00378 {
00379 int keyId = keyframes->GetFirstKeyframeVideo(v) + k;
00380 String keyName = FileNameBase(keyframes->GetName(keyId));
00381 int frameNr = keyframes->GetFrameNr(keyId);
00382 double relevance = 0;
00383 if (find(annos.begin(),annos.end(),keyName) != annos.end())
00384 relevance = 1;
00385 mp7.AddAnnotation(concepts[c], keyName, frameNr, frameNr,
00386 relevance, 1);
00387 }
00388 }
00389 else
00390 {
00391 for (int s=0 ; s<segmentation->GetNrShotsVideo(v) ; s++)
00392 {
00393 int shotId = segmentation->GetFirstShotVideo(v) + s;
00394 String shotName = segmentation->GetName(shotId);
00395 double relevance = 0;
00396 if (find(annos.begin(),annos.end(),shotName) != annos.end())
00397 relevance = 1;
00398 int firstKey = keyframes->GetFirstKeyframeShot(shotId);
00399 int nrKey = keyframes->GetNrKeyframesShot(shotId);
00400 for (int k=firstKey ; k<firstKey+nrKey ; k++)
00401 {
00402 String keyName = FileNameBase(keyframes->GetName(k));
00403 int frameNr = keyframes->GetFrameNr(k);
00404 mp7.AddAnnotation(concepts[c], keyName, frameNr,
00405 frameNr, relevance, 1);
00406 }
00407 }
00408 }
00409 Core::VideoSet::Mpeg7DocWrite(&mp7, vidSet, v);
00410 }
00411 ILOG_NDC_POP;
00412 }
00413 }
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424 void
00425 DoImportAnnotations()
00426 {
00427 #ifndef REPOSITORY_TODO
00428 typedef Core::VideoSet::VideoSet VideoSet;
00429 typedef Core::VideoSet::Segmentation Segmentation;
00430 typedef Core::VideoSet::Keyframes Keyframes;
00431 typedef Core::VideoSet::Mpeg7Doc Mpeg7Doc;
00432
00433 ILOG_VAR(Impala.Application.Util.DoImportAnnotations);
00434 CmdOptions& options = CmdOptions::GetInstance();
00435 if (options.GetNrArg() < 4)
00436 {
00437 ILOG_ERROR("Need more parameters");
00438 return;
00439 }
00440 String videoSetName = options.GetArg(1);
00441 int fractions = Impala::atol(options.GetArg(2));
00442 if ((fractions != 25) && (fractions != 30000))
00443 ILOG_WARN("Never tried fractions = " << fractions);
00444 String conceptSet = options.GetArg(3);
00445
00446 VideoSet* vidSet = Core::VideoSet::MakeVideoSet(videoSetName);
00447 SegmentationLocator sLoc(videoSetName, "segmentation", options);
00448 Segmentation* segmentation = SegmentationRepository().Get(sLoc, vidSet);
00449 KeyframesLocator kLoc(videoSetName, "keyframes", options);
00450 Keyframes* keyframes = KeyframesRepository().Get(kLoc, vidSet);
00451 std::vector<String> concepts;
00452 Impala::Util::Database* db = vidSet->GetDatabase();
00453 Impala::Util::DatabaseReadStrings(concepts, conceptSet, db);
00454 String path = FileNamePath(conceptSet) + "/";
00455 conceptSet = FileNameTail(conceptSet);
00456 for (int c=0 ; c<concepts.size() ; c++)
00457 {
00458 ILOG_INFO("doing concept " << concepts[c]);
00459 ILOG_NDC_PUSH(concepts[c]);
00460 String fName = path + concepts[c] + ".ann";
00461 std::vector<String> lines;
00462 Impala::Util::DatabaseReadStrings(lines, fName, db);
00463 int curVidId = -1;
00464 Mpeg7Doc* curDoc = 0;
00465 for (int l=0 ; l<lines.size() ; l++)
00466 {
00467 Impala::Util::StringParser p(lines[l]);
00468 String s = p.GetString(' ');
00469 s = p.GetString(' ');
00470 s = p.GetString(' ');
00471 s = p.GetString(' ');
00472 s = Impala::FileNameTail(p.GetString(' '));
00473 int key = keyframes->GetFrameId(s + ".jpg");
00474 if (key == -1)
00475 {
00476 ILOG_ERROR("Unable to find keyframe " << s);
00477 continue;
00478 }
00479 double relevance = (p.GetString(' ', false) == "P") ? 1.0 : 0.0;
00480 double confidence = 1.0;
00481 if (!p.TheEnd() && (p.GetString(' ', false) == "S"))
00482 relevance = 0.5;
00483
00484 if (keyframes->GetVideoId(key) != curVidId)
00485 {
00486 if (curDoc)
00487 {
00488 Core::VideoSet::Mpeg7DocWrite(curDoc, vidSet, curVidId);
00489 delete curDoc;
00490 curDoc = 0;
00491 }
00492 curVidId = keyframes->GetVideoId(key);
00493 String vidFile = vidSet->GetFile(curVidId);
00494 int nrFrames = segmentation->GetNrFramesVideo(curVidId);
00495 curDoc = new Mpeg7Doc(vidFile, vidFile, nrFrames, fractions,
00496 false, conceptSet, concepts[c]);
00497 }
00498 String name = FileNameBase(keyframes->GetName(key));
00499 int frameNr = keyframes->GetFrameNr(key);
00500 curDoc->AddAnnotation(concepts[c], name, frameNr, frameNr,
00501 relevance, confidence);
00502 }
00503 if (curDoc)
00504 {
00505 Core::VideoSet::Mpeg7DocWrite(curDoc, vidSet, curVidId);
00506 delete curDoc;
00507 }
00508 ILOG_NDC_POP;
00509 }
00510 #endif
00511 }
00512
00513
00514
00515
00516 void
00517 DoImportAnnotationsOrk()
00518 {
00519 #ifndef REPOSITORY_TODO
00520 typedef Core::VideoSet::VideoSet VideoSet;
00521 typedef Core::VideoSet::Segmentation Segmentation;
00522 typedef Core::VideoSet::Keyframes Keyframes;
00523 typedef Core::VideoSet::Mpeg7Doc Mpeg7Doc;
00524
00525 ILOG_VAR(Impala.Application.Util.DoImportAnnotations);
00526 CmdOptions& options = CmdOptions::GetInstance();
00527 if (options.GetNrArg() < 6)
00528 {
00529 ILOG_ERROR("Need more parameters");
00530 return;
00531 }
00532 String videoSetName = options.GetArg(1);
00533 int fractions = Impala::atol(options.GetArg(2));
00534 if ((fractions != 25) && (fractions != 30000))
00535 ILOG_WARN("Never tried fractions = " << fractions);
00536 String conceptSet = options.GetArg(3);
00537 String mappingName = options.GetArg(4);
00538 String inputPath = options.GetArg(5);
00539
00540 VideoSet* vidSet = Core::VideoSet::MakeVideoSet(videoSetName);
00541 SegmentationLocator sLoc(videoSetName, "segmentation", options);
00542 Segmentation* segmentation = SegmentationRepository().Get(sLoc, vidSet);
00543 KeyframesLocator kLoc(videoSetName, "keyframes", options);
00544 Keyframes* keyframes = KeyframesRepository().Get(kLoc, vidSet);
00545 Impala::Util::Database* db = vidSet->GetDatabase();
00546
00547 String fName = vidSet->GetFilePathAnnotation(conceptSet, false, false);
00548 if (fName.empty())
00549 {
00550 ILOG_ERROR("unable to read conceptSet");
00551 return;
00552 }
00553 std::vector<String> concepts;
00554 Impala::Util::DatabaseReadStrings(concepts, fName, db);
00555
00556 std::vector<String> lines;
00557 Impala::Util::SimpleMap<String, String> conceptMapping;
00558 Impala::Util::DatabaseReadStrings(lines, mappingName, db);
00559 for (int i=0 ; i<lines.size() ; i++)
00560 {
00561 Impala::Util::StringParser p(lines[i]);
00562 String topic = p.GetString(' ');
00563 String concept = p.GetString(' ', false);
00564 std::cout << topic << " = " << concept << std::endl;
00565 conceptMapping.Add(concept, topic);
00566 }
00567 for (int c=0 ; c<concepts.size() ; c++)
00568 {
00569 ILOG_INFO("doing concept " << concepts[c]);
00570 ILOG_NDC_PUSH(concepts[c]);
00571
00572 String topic;
00573 if (!conceptMapping.Get(concepts[c], topic))
00574 {
00575 ILOG_ERROR("no topic for " << concepts[c]);
00576 ILOG_NDC_POP;
00577 continue;
00578 }
00579
00580 std::vector<String> posAnnos;
00581 fName = inputPath + "." + topic + ".1";
00582 fName = db->GetFilePath(fName, false, true);
00583 if (!fName.empty())
00584 Impala::Util::DatabaseReadStrings(posAnnos, fName, db);
00585
00586 std::vector<String> negAnnos;
00587 fName = inputPath + "." + topic + ".0";
00588 fName = db->GetFilePath(fName, false, true);
00589 if (!fName.empty())
00590 Impala::Util::DatabaseReadStrings(negAnnos, fName, db);
00591 if ((posAnnos.size() == 0) || (negAnnos.size() == 0))
00592 {
00593 ILOG_ERROR("missing input for " << concepts[c]);
00594 ILOG_NDC_POP;
00595 continue;
00596 }
00597
00598 std::sort(posAnnos.begin(), posAnnos.end());
00599 std::sort(negAnnos.begin(), negAnnos.end());
00600 for (int v=0 ; v<vidSet->NrFiles() ; v++)
00601 {
00602 String vidFile = vidSet->GetFile(v);
00603 int nrFrames = segmentation->GetNrFramesVideo(v);
00604 Mpeg7Doc mp7(vidFile, vidFile, nrFrames, fractions, false,
00605 conceptSet, concepts[c]);
00606
00607 for (int s=0 ; s<segmentation->GetNrShotsVideo(v) ; s++)
00608 {
00609 int shotId = segmentation->GetFirstShotVideo(v) + s;
00610 String shotName = segmentation->GetName(shotId);
00611 double relevance = -1;
00612 if (find(posAnnos.begin(), posAnnos.end(), shotName) !=
00613 posAnnos.end())
00614 {
00615 relevance = 1;
00616 }
00617 else if (find(negAnnos.begin(), negAnnos.end(), shotName) !=
00618 negAnnos.end())
00619 {
00620 relevance = 0;
00621 }
00622 if (relevance < 0)
00623 continue;
00624 int key = keyframes->GetShotRKF(shotId);
00625
00626 String keyName = FileNameBase(keyframes->GetName(key));
00627 int frameNr = keyframes->GetFrameNr(key);
00628 mp7.AddAnnotation(concepts[c], keyName, frameNr, frameNr,
00629 relevance, 1);
00630 }
00631 if (mp7.HasAnnotations())
00632 Core::VideoSet::Mpeg7DocWrite(&mp7, vidSet, v);
00633 }
00634 ILOG_NDC_POP;
00635 }
00636 #endif
00637 }
00638
00639
00640
00641
00642
00643
00644 void
00645 DoImportAnnotationsActive()
00646 {
00647 #ifndef REPOSITORY_TODO
00648 typedef Core::VideoSet::VideoSet VideoSet;
00649 typedef Core::VideoSet::Segmentation Segmentation;
00650 typedef Core::VideoSet::Keyframes Keyframes;
00651 typedef Core::VideoSet::Mpeg7Doc Mpeg7Doc;
00652
00653 ILOG_VAR(Impala.Application.Util.DoImportAnnotationsActive);
00654 CmdOptions& options = CmdOptions::GetInstance();
00655 if (options.GetNrArg() < 4)
00656 {
00657 ILOG_ERROR("Need more parameters");
00658 return;
00659 }
00660 String videoSetName = options.GetArg(1);
00661 int fractions = Impala::atol(options.GetArg(2));
00662 if ((fractions != 25) && (fractions != 30000))
00663 ILOG_WARN("Never tried fractions = " << fractions);
00664 String conceptSet = options.GetArg(3);
00665
00666 VideoSet* vidSet = Core::VideoSet::MakeVideoSet(videoSetName);
00667 SegmentationLocator sLoc(videoSetName, "segmentation", options);
00668 Segmentation* segmentation = SegmentationRepository().Get(sLoc, vidSet);
00669 KeyframesLocator kLoc(videoSetName, "keyframes", options);
00670 Keyframes* keyframes = KeyframesRepository().Get(kLoc, vidSet);
00671 Impala::Util::Database* db = vidSet->GetDatabase();
00672
00673 String fName = vidSet->GetFilePathAnnotation(conceptSet, false, false);
00674 if (fName.empty())
00675 {
00676 ILOG_ERROR("unable to read conceptSet");
00677 return;
00678 }
00679 std::vector<String> concepts;
00680 Impala::Util::DatabaseReadStrings(concepts, fName, db);
00681 for (int c=0 ; c<concepts.size() ; c++)
00682 {
00683 ILOG_INFO("doing concept " << concepts[c]);
00684 ILOG_NDC_PUSH(concepts[c]);
00685
00686 std::vector<String> posAnnos;
00687 fName = "from_active/" + concepts[c] + "/shots_bookmarked.txt";
00688 fName = vidSet->GetFilePathAnnotation(fName, false, true);
00689 if (!fName.empty())
00690 Impala::Util::DatabaseReadStrings(posAnnos, fName, db);
00691
00692 std::vector<String> negAnnos;
00693 fName = "from_active/" + concepts[c] + "/shots_bookmarked_negative.txt";
00694 fName = vidSet->GetFilePathAnnotation(fName, false, true);
00695 if (!fName.empty())
00696 Impala::Util::DatabaseReadStrings(negAnnos, fName, db);
00697 if ((posAnnos.size() == 0) || (negAnnos.size() == 0))
00698 {
00699 ILOG_ERROR("missing input for " << concepts[c]);
00700 ILOG_NDC_POP;
00701 continue;
00702 }
00703
00704 std::sort(posAnnos.begin(), posAnnos.end());
00705 std::sort(negAnnos.begin(), negAnnos.end());
00706 for (int v=0 ; v<vidSet->NrFiles() ; v++)
00707 {
00708 String vidFile = vidSet->GetFile(v);
00709 int nrFrames = segmentation->GetNrFramesVideo(v);
00710 Mpeg7Doc mp7(vidFile, vidFile, nrFrames, fractions, false,
00711 conceptSet, concepts[c]);
00712
00713 for (int s=0 ; s<segmentation->GetNrShotsVideo(v) ; s++)
00714 {
00715 int shotId = segmentation->GetFirstShotVideo(v) + s;
00716 String shotName = segmentation->GetName(shotId);
00717 double relevance = -1;
00718 if (find(posAnnos.begin(), posAnnos.end(), shotName) !=
00719 posAnnos.end())
00720 {
00721 relevance = 1;
00722 }
00723 else if (find(negAnnos.begin(), negAnnos.end(), shotName) !=
00724 negAnnos.end())
00725 {
00726 relevance = 0;
00727 }
00728 if (relevance < 0)
00729 continue;
00730 int key = keyframes->GetShotRKF(shotId);
00731
00732 String keyName = FileNameBase(keyframes->GetName(key));
00733 int frameNr = keyframes->GetFrameNr(key);
00734 mp7.AddAnnotation(concepts[c], keyName, frameNr, frameNr,
00735 relevance, 1);
00736 }
00737 if (mp7.HasAnnotations())
00738 Core::VideoSet::Mpeg7DocWrite(&mp7, vidSet, v);
00739 }
00740 ILOG_NDC_POP;
00741 }
00742 #endif
00743 }
00744
00745 void
00746 DoExportConceptMpeg7()
00747 {
00748 ILOG_VAR(Impala.Application.Util.DoExportConceptMpeg7);
00749 ILOG_ERROR("Deprecated : use vidset exportconcepts");
00750 }
00751
00752 void
00753 DoDumpParameter()
00754 {
00755 #ifndef REPOSITORY_TODO
00756 typedef Core::Database::RawDataSet RawDataSet;
00757 typedef Core::Feature::FeatureDefinition FeatureDef;
00758
00759 ILOG_VAR(Impala.Application.Util.DoDumpParameter);
00760 CmdOptions& options = CmdOptions::GetInstance();
00761 if (options.GetNrArg() < 5)
00762 {
00763 ILOG_ERROR("Need more parameters");
00764 return;
00765 }
00766 String dataSetName = options.GetArg(1);
00767 String conceptSet = options.GetArg(2);
00768 String model = options.GetArg(3);
00769 FeatureDef featureDef(options.GetArg(4));
00770
00771 RawDataSet* dataSet = Core::Database::MakeRawDataSet(dataSetName);
00772
00773 String fName = dataSet->GetFilePathAnnotation(conceptSet, false, false);
00774 if (fName.empty())
00775 {
00776 ILOG_ERROR("unable to read conceptSet " << conceptSet);
00777 return;
00778 }
00779 std::vector<String> concepts;
00780 Impala::Util::Database* db = dataSet->GetDatabase();
00781 Impala::Util::DatabaseReadStrings(concepts, fName, db);
00782 Real64 map = 0;
00783 int nrMap = 0;
00784 std::cout << conceptSet << " " << model << " " << featureDef.AsString()
00785 << std::endl;
00786 for (int c=0 ; c<concepts.size() ; c++)
00787 {
00788 String con = concepts[c];
00789 fName = dataSet->GetFilePathConceptModel(conceptSet, model, featureDef,
00790 con + ".best", false, false);
00791 if (fName.empty())
00792 {
00793 ILOG_INFO("no .best found for " << con);
00794 continue;
00795 }
00796 Impala::Util::IOBuffer* ioBuf = db->GetIOBuffer(fName, true, false, "");
00797 String buffer = ioBuf->ReadLine();
00798 Impala::Util::StringParser p(buffer);
00799 p.Eat(':');
00800 Real64 ap = p.GetDouble();
00801 std::cout << con << " " << ap << std::endl;
00802 map += ap;
00803 nrMap++;
00804 }
00805 std::cout << "map = " << map / nrMap << std::endl << std::endl;
00806 #endif
00807 }
00808
00809 void
00810 DoAPConfusion()
00811 {
00812 #ifndef REPOSITORY_TODO
00813 typedef Core::Database::RawDataSet RawDataSet;
00814 typedef Core::Table::SimilarityTableSet SimilarityTableSet;
00815 typedef Core::Table::AnnotationTable AnnotationTable;
00816 using Impala::Core::Table::AveragePrecision;
00817
00818 ILOG_VAR(Impala.Application.Util.DoAPConfusion);
00819 CmdOptions& options = CmdOptions::GetInstance();
00820 if (options.GetNrArg() < 4)
00821 {
00822 ILOG_ERROR("Need more parameters");
00823 return;
00824 }
00825 String dataSetName = options.GetArg(1);
00826 String annoSet = options.GetArg(2);
00827 String namesPath = options.GetArg(3);
00828 String mappingName;
00829 if (options.GetNrArg() > 4)
00830 mappingName = options.GetArg(4);
00831
00832 RawDataSet* dataSet = Core::Database::MakeRawDataSet(dataSetName);
00833 Impala::Util::Database* db = dataSet->GetDatabase();
00834 SimilarityTableSet* simSet =
00835 Core::Table::SimilarityTableSet::MakeFromFile(
00836 db->GetFilePath(".", namesPath, false, false), db);
00837
00838 Impala::Util::SimpleMap<String, String> conceptMapping;
00839 if (!mappingName.empty())
00840 {
00841 std::vector<String> lines;
00842 Impala::Util::DatabaseReadStrings(lines, mappingName, db);
00843 for (int i=0 ; i<lines.size() ; i++)
00844 {
00845 Impala::Util::StringParser p(lines[i]);
00846 String src = p.GetString(' ');
00847 String dst = p.GetString(' ', false);
00848 std::cout << src << " => " << dst << std::endl;
00849 conceptMapping.Add(src, dst);
00850 }
00851 }
00852
00853 Core::Table::QuidTable* qTable = simSet->GetQuidTable();
00854 int qClass = QuidClass(qTable->Get1(0));
00855 Real64 map = 0;
00856 Real64 mapJ = 0;
00857 Real64 map2000 = 0;
00858 Real64 map2000J = 0;
00859 bool do2000 = (simSet->TableSize() > 2000);
00860 int nrMap = 0;
00861 std::cout << namesPath << ", size = " << simSet->TableSize() << std::endl;
00862
00863 AnnotationTable** allAnnotations = new AnnotationTable*[simSet->NrTables()];
00864
00865 for (int c=0; c<simSet->NrTables() ; c++){
00866 String concept = simSet->GetName(c);
00867 String mappedConcept;
00868 if (!mappingName.empty())
00869 {
00870 if (conceptMapping.Get(concept, mappedConcept))
00871 {
00872 concept = mappedConcept;
00873 }
00874 else
00875 {
00876 continue;
00877 }
00878 }
00879 String fName = dataSet->GetFilePathAnnotation(qClass, annoSet,
00880 concept + ".tab",
00881 false, false);
00882
00883 if (fName.empty())
00884 {
00885 ILOG_ERROR("No groundtruth for " << concept);
00886 continue;
00887 }
00888
00889 allAnnotations[c] = AnnotationTable::MakeFromFile(concept, fName, db);
00890 allAnnotations[c]->UpdateQuidMap();
00891 }
00892
00893 std::cout << "\nConfusion Matrix Average Precision" << std::endl;
00894 for (int c=0 ; c<simSet->NrTables() ; c++)
00895 {
00896 Real64* confEntry = AveragePrecisionConfusion(simSet->GetRankTable(c), allAnnotations,
00897 c, simSet->NrTables(), -1);
00898
00899 String concept = simSet->GetName(c);
00900 std::cout << MakeString(concept, 25) << "\t";
00901
00902 Real64 total = 0;
00903 for (int i=0; i<simSet->NrTables() ; i++)
00904 {
00905 total += confEntry[i];
00906 std::cout << confEntry[i] << "\t";
00907 }
00908
00909 std::cout << total << std::endl;
00910 }
00911
00912 std::cout << "\nConfusion Matrix Average Precision Judged" << std::endl;
00913 for (int c=0 ; c<simSet->NrTables() ; c++)
00914 {
00915 Real64* confEntry = AveragePrecisionJudgedConfusion(simSet->GetRankTable(c), allAnnotations,
00916 c, simSet->NrTables(), -1);
00917
00918 String concept = simSet->GetName(c);
00919 std::cout << MakeString(concept, 25) << "\t ";
00920
00921 Real64 total = 0;
00922 for (int i=0; i<simSet->NrTables() ; i++)
00923 {
00924 total += confEntry[i];
00925 std::cout << confEntry[i] << "\t ";
00926 }
00927
00928 std::cout << total << std::endl;
00929 }
00930
00931 for (int i=0; i < simSet->NrTables() ; i++)
00932 delete allAnnotations[i];
00933 delete allAnnotations;
00934 delete simSet;
00935 delete dataSet;
00936 #endif
00937 }
00938
00939 void
00940 DoScoreConcepts()
00941 {
00942 #ifndef REPOSITORY_TODO
00943 typedef Core::Database::RawDataSet RawDataSet;
00944 typedef Core::Table::SimilarityTableSet SimilarityTableSet;
00945 typedef Core::Table::AnnotationTable AnnotationTable;
00946 using Impala::Core::Table::AveragePrecision;
00947
00948 ILOG_VAR(Impala.Application.Util.DoScoreConcept);
00949 CmdOptions& options = CmdOptions::GetInstance();
00950 if (options.GetNrArg() < 4)
00951 {
00952 ILOG_ERROR("Need more parameters");
00953 return;
00954 }
00955 String dataSetName = options.GetArg(1);
00956 String annoSet = options.GetArg(2);
00957 String namesPath = options.GetArg(3);
00958 String mappingName;
00959 if (options.GetNrArg() > 4)
00960 mappingName = options.GetArg(4);
00961
00962 RawDataSet* dataSet = Core::Database::MakeRawDataSet(dataSetName);
00963 Impala::Util::Database* db = dataSet->GetDatabase();
00964 SimilarityTableSet* simSet =
00965 Core::Table::SimilarityTableSet::MakeFromFile(
00966 db->GetFilePath(".", namesPath, false, false), db);
00967
00968 Impala::Util::SimpleMap<String, String> conceptMapping;
00969 if (!mappingName.empty())
00970 {
00971 std::vector<String> lines;
00972 Impala::Util::DatabaseReadStrings(lines, mappingName, db);
00973 for (int i=0 ; i<lines.size() ; i++)
00974 {
00975 Impala::Util::StringParser p(lines[i]);
00976 String src = p.GetString(' ');
00977 String dst = p.GetString(' ', false);
00978 std::cout << src << " => " << dst << std::endl;
00979 conceptMapping.Add(src, dst);
00980 }
00981 }
00982
00983 Core::Table::QuidTable* qTable = simSet->GetQuidTable();
00984 int qClass = QuidClass(qTable->Get1(0));
00985 Real64 map = 0;
00986 Real64 mapJ = 0;
00987 Real64 map2000 = 0;
00988 Real64 map2000J = 0;
00989 bool do2000 = (simSet->TableSize() > 2000);
00990 int nrMap = 0;
00991 std::cout << namesPath << ", size = " << simSet->TableSize() << std::endl;
00992 for (int c=0 ; c<simSet->NrTables() ; c++)
00993 {
00994 String concept = simSet->GetName(c);
00995 String mappedConcept;
00996 if (!mappingName.empty())
00997 {
00998 if (conceptMapping.Get(concept, mappedConcept))
00999 {
01000 concept = mappedConcept;
01001 }
01002 else
01003 {
01004 continue;
01005 }
01006 }
01007 String fName = dataSet->GetFilePathAnnotation(qClass, annoSet,
01008 concept + ".tab",
01009 false, false);
01010 if (fName.empty())
01011 {
01012 ILOG_ERROR("No groundtruth for " << concept);
01013 continue;
01014 }
01015 AnnotationTable* truth = AnnotationTable::MakeFromFile(concept, fName,
01016 db);
01017 truth->UpdateQuidMap();
01018 Real64 ap = AveragePrecision(simSet->GetRankTable(c), truth, -1);
01019 Real64 apJ = AveragePrecisionJudged(simSet->GetRankTable(c), truth,
01020 false, -1);
01021 map += ap;
01022 mapJ += apJ;
01023 std::cout << MakeString(concept, 30)
01024 << " " << ap << "\t j: " << apJ;
01025
01026 if (do2000)
01027 {
01028 Real64 ap2000 = AveragePrecision(simSet->GetRankTable(c), truth,
01029 2000);
01030 Real64 ap2000J = AveragePrecisionJudged(simSet->GetRankTable(c),
01031 truth, false, 2000);
01032 map2000 += ap2000;
01033 map2000J += ap2000J;
01034 std::cout << "\t " << ap2000 << "\t j: " << ap2000J;
01035 }
01036
01037 std::cout << std::endl;
01038 nrMap++;
01039 delete truth;
01040 }
01041 std::cout << "map=" << map/nrMap << ", mapJudged=" << mapJ/nrMap;
01042 if (do2000)
01043 std::cout << " map2000=" << map2000/nrMap << ", map2000Judged="
01044 << map2000J/nrMap;
01045 std::cout << std::endl << std::endl;
01046 delete simSet;
01047 delete dataSet;
01048 #endif
01049 }
01050
01051 void
01052 DoScoreConceptsVideo()
01053 {
01054 #ifndef REPOSITORY_TODO
01055 typedef Core::Database::RawDataSet RawDataSet;
01056 typedef Core::Table::SimilarityTableSet SimilarityTableSet;
01057 typedef Core::Table::SimilarityTableSet::RankTableType RankTableType;
01058 typedef Core::Table::AnnotationTable AnnotationTable;
01059 using Impala::Core::Table::AveragePrecision;
01060
01061 ILOG_VAR(Impala.Application.Util.DoScoreConceptVideo);
01062 CmdOptions& options = CmdOptions::GetInstance();
01063 if (options.GetNrArg() < 5)
01064 {
01065 ILOG_ERROR("Need more parameters");
01066 return;
01067 }
01068 String dataSetName = options.GetArg(1);
01069 String annoSet = options.GetArg(2);
01070 String namesPath = options.GetArg(3);
01071 int vidStart = atol(options.GetArg(4));
01072 int vidEnd = atol(options.GetArg(5));
01073
01074 RawDataSet* dataSet = Core::Database::MakeRawDataSet(dataSetName);
01075 Impala::Util::Database* db = dataSet->GetDatabase();
01076 SimilarityTableSet* simSet =
01077 Core::Table::SimilarityTableSet::MakeFromFile(namesPath, db);
01078
01079 Core::Table::QuidTable* qTable = simSet->GetQuidTable();
01080 int qClass = QuidClass(qTable->Get1(0));
01081 Real64 mapJ = 0;
01082 int nrMap = 0;
01083 std::cout << namesPath << ", size = " << simSet->TableSize() << std::endl;
01084 std::vector<int> videos;
01085 std::cout << "videos: ";
01086 for (int i=vidStart ; i<vidEnd ; i++)
01087 {
01088 videos.push_back(i);
01089 std::cout << i << " ";
01090 }
01091 std::cout << std::endl;
01092 for (int c=0 ; c<simSet->NrTables() ; c++)
01093 {
01094 String concept = simSet->GetName(c);
01095 String fName = dataSet->GetFilePathAnnotation(qClass, annoSet,
01096 concept + ".tab",
01097 false, false);
01098 if (fName.empty())
01099 {
01100 ILOG_ERROR("No groundtruth for " << concept);
01101 continue;
01102 }
01103 AnnotationTable* truth = AnnotationTable::MakeFromFile(concept, fName,
01104 db);
01105 truth->UpdateQuidMap();
01106 RankTableType* rank = simSet->GetRankTable(c);
01107 if (videos.size() > 0)
01108 {
01109 Core::Table::CriterionQuidObjectInSet<RankTableType> crit(videos);
01110 Core::Table::Select(rank, rank, crit, true);
01111 }
01112 Real64 apJ = AveragePrecisionJudged(rank, truth, false, -1, true);
01113 mapJ += apJ;
01114 std::cout << MakeString(concept, 25) << " " << apJ << std::endl;
01115 nrMap++;
01116 delete truth;
01117 }
01118 std::cout << "mapJudged=" << mapJ/nrMap << std::endl;
01119 std::cout << std::endl;
01120 delete simSet;
01121 delete dataSet;
01122 #endif
01123 }
01124
01125 void
01126 DoCombineConcepts()
01127 {
01128 #ifndef REPOSITORY_TODO
01129 typedef Core::VideoSet::VideoSet VideoSet;
01130 typedef Core::Table::SimilarityTableSet SimilarityTableSet;
01131 typedef Core::Table::SimilarityTableSet::SimTableType SimTableType;
01132
01133 ILOG_VAR(Impala.Application.Util.DoCombineConcepts);
01134 CmdOptions& options = CmdOptions::GetInstance();
01135 if (options.GetNrArg() < 7)
01136 {
01137 ILOG_ERROR("Need more parameters");
01138 return;
01139 }
01140 String vidSetName = options.GetArg(1);
01141 String conceptSet = options.GetArg(2);
01142 String model = options.GetArg(3);
01143 String resultName = options.GetArg(4);
01144 String combi = options.GetArg(5);
01145 if ((combi != "mul") && (combi != "avg"))
01146 {
01147 ILOG_ERROR("Unknown combination method : " << combi);
01148 return;
01149 }
01150 std::vector<String> featureDefs;
01151 for (int i=6 ; i<options.GetNrArg() ; i++)
01152 featureDefs.push_back(options.GetArg(i));
01153
01154 VideoSet* vidSet = Core::VideoSet::MakeVideoSet(vidSetName);
01155
01156 SimilarityTableSet* combinedSet = SimilarityTableSet::MakeFromFile
01157 (vidSet, conceptSet, model, featureDefs[0]);
01158 for (int i=1 ; i<featureDefs.size() ; i++)
01159 {
01160 SimilarityTableSet* simSet = SimilarityTableSet::MakeFromFile
01161 (vidSet, conceptSet, model, featureDefs[i]);
01162 for (int t=0 ; t<simSet->NrTables() ; t++)
01163 {
01164 if (combi == "mul")
01165 MulAssign(combinedSet->GetSimTable(t)->GetColumn1(),
01166 simSet->GetSimTable(t)->GetColumn1(), 0, -1);
01167 else
01168 AddAssign(combinedSet->GetSimTable(t)->GetColumn1(),
01169 simSet->GetSimTable(t)->GetColumn1(), 0, -1);
01170 }
01171 delete simSet;
01172 }
01173 if (combi == "avg")
01174 for (int t=0 ; t<combinedSet->NrTables() ; t++)
01175 DivAssign(combinedSet->GetSimTable(t)->GetColumn1(),
01176 featureDefs.size(), 0, -1);
01177 combinedSet->ComputeRanks(true);
01178 combinedSet->Save(vidSet, conceptSet, model, resultName, true);
01179 delete combinedSet;
01180 delete vidSet;
01181 #endif
01182 }
01183
01184 void
01185 DoMapConcepts2Shots()
01186 {
01187 #ifndef REPOSITORY_TODO
01188 typedef Core::VideoSet::VideoSet VideoSet;
01189 typedef Core::VideoSet::Segmentation Segmentation;
01190 typedef Core::Table::SimilarityTableSet SimilarityTableSet;
01191 typedef Core::Table::SimilarityTableSet::SimTableType SimTableType;
01192
01193 ILOG_VAR(Impala.Application.Util.DoMapConcepts2Shots);
01194 CmdOptions& options = CmdOptions::GetInstance();
01195 if (options.GetNrArg() < 5)
01196 {
01197 ILOG_ERROR("Need more parameters");
01198 return;
01199 }
01200 String vidSetName = options.GetArg(1);
01201 String conceptSet = options.GetArg(2);
01202 String model = options.GetArg(3);
01203 String feature = options.GetArg(4);
01204 String aggregationMode = "max";
01205 if (options.GetNrArg() >= 6)
01206 {
01207 aggregationMode = options.GetArg(5);
01208 }
01209
01210 VideoSet* vidSet = Core::VideoSet::MakeVideoSet(vidSetName);
01211 SegmentationLocator sLoc(vidSetName, "segmentation", options);
01212 Segmentation* segmentation = SegmentationRepository().Get(sLoc, vidSet);
01213 SimilarityTableSet* simSet =
01214 Core::Table::SimilarityTableSet::MakeFromFile(vidSet, conceptSet,
01215 model, feature);
01216
01217 Core::Table::QuidTable* frameQuids = simSet->GetQuidTable();
01218 int nrShots = segmentation->GetNrShots();
01219 SimilarityTableSet* res = new SimilarityTableSet(simSet->GetNames(), nrShots);
01220 Core::Table::QuidTable* shotQuids = res->GetQuidTable();
01221
01222
01223
01224
01225
01226 for (int t=0 ; t<simSet->NrTables() ; t++)
01227 {
01228 SimTableType* frameSimTable = simSet->GetSimTable(t);
01229 SimTableType* shotSimTable = res->GetSimTable(t);
01230 int lastShot = -1;
01231 Real64 lastSim = 0;
01232 int lastCount = 0;
01233 for (int i=0 ; i<frameSimTable->Size() ; i++)
01234 {
01235 Quid frameQ = frameQuids->Get1(i);
01236 Real64 frameSim = frameSimTable->Get1(i);
01237 int shot = segmentation->GetShotId(frameQ);
01238 if (shot != lastShot)
01239 {
01240 if (lastShot != -1)
01241 {
01242 Quid check = segmentation->GetQuidShot(lastShot);
01243 int rank = shotSimTable->Size();
01244 if (shotQuids->Size() <= rank)
01245 shotQuids->Add(check);
01246 if (check != shotQuids->Get1(rank))
01247 {
01248 ILOG_ERROR("Quids do not match");
01249 }
01250 if(aggregationMode == "avg")
01251 {
01252 lastSim = lastSim / lastCount;
01253 }
01254 shotSimTable->Add(lastSim);
01255 }
01256 lastSim = frameSim;
01257 lastShot = shot;
01258 lastCount = 1;
01259 }
01260 else
01261 {
01262 lastCount++;
01263 if(aggregationMode == "max")
01264 {
01265 if (frameSim > lastSim)
01266 lastSim = frameSim;
01267 }
01268 else if(aggregationMode == "avg")
01269 {
01270 lastSim += frameSim;
01271 }
01272 }
01273 }
01274 if (lastShot != -1)
01275 {
01276 Quid check = segmentation->GetQuidShot(lastShot);
01277 int rank = shotSimTable->Size();
01278 if (shotQuids->Size() <= rank)
01279 shotQuids->Add(check);
01280 if (check != shotQuids->Get1(rank))
01281 {
01282 ILOG_ERROR("Quids do not match");
01283 }
01284 if(aggregationMode == "avg")
01285 {
01286 lastSim = lastSim / lastCount;
01287 }
01288 shotSimTable->Add(lastSim);
01289 }
01290 }
01291
01292 res->ComputeRanks(true);
01293 res->Save(vidSet, conceptSet, model, "shot" + feature, true);
01294 delete res;
01295 delete simSet;
01296 delete vidSet;
01297 #endif
01298 }
01299
01300 void
01301 DoConcepts2TrecResult()
01302 {
01303 #ifndef REPOSITORY_TODO
01304 typedef Core::VideoSet::VideoSet VideoSet;
01305 typedef Core::VideoSet::Segmentation Segmentation;
01306 typedef Core::VideoSet::Keyframes Keyframes;
01307 typedef Core::Table::SimilarityTableSet SimilarityTableSet;
01308 typedef Core::Table::SimilarityTableSet::RankTableType RankTableType;
01309
01310 ILOG_VAR(Impala.Application.Util.DoConcepts2TrecResult);
01311 CmdOptions& options = CmdOptions::GetInstance();
01312 if (options.GetNrArg() < 3)
01313 {
01314 ILOG_ERROR("Need more parameters");
01315 return;
01316 }
01317 String vidSetName = options.GetArg(1);
01318 String namesPath = options.GetArg(2);
01319
01320 VideoSet* vidSet = Core::VideoSet::MakeVideoSet(vidSetName);
01321 SegmentationLocator sLoc(vidSetName, "segmentation", options);
01322 Segmentation* segmentation = SegmentationRepository().Get(sLoc, vidSet);
01323 KeyframesLocator kLoc(vidSetName, "keyframes", options);
01324 Keyframes* keyframes = KeyframesRepository().Get(kLoc, vidSet);
01325 Impala::Util::Database* db = vidSet->GetDatabase();
01326 SimilarityTableSet* simSet =
01327 Core::Table::SimilarityTableSet::MakeFromFile(namesPath, db);
01328
01329 for (int c=0 ; c<simSet->NrTables() ; c++)
01330 {
01331 String concept = simSet->GetName(c);
01332 ILOG_INFO("Doing concept " << concept);
01333 std::vector<String> res;
01334 RankTableType* rTable = simSet->GetRankTable(c);
01335 for (int i=0 ; i<rTable->Size() ; i++)
01336 {
01337 Quid quid = rTable->Get1(i);
01338 int shot = -1;
01339 if (QuidClass(quid) == QUID_CLASS_FRAME)
01340 {
01341 int keyfr = keyframes->GetFrameId(quid);
01342 if (keyfr != -1)
01343 shot = keyframes->GetShotId(keyfr);
01344 }
01345 if (QuidClass(quid) == QUID_CLASS_SHOT)
01346 {
01347 shot = segmentation->GetShotId(quid);
01348 }
01349 if (shot == -1)
01350 {
01351 ILOG_ERROR("Unable to find shot for " << QuidObj(quid));
01352 continue;
01353 }
01354 String name = segmentation->GetName(shot);
01355 if (std::find(res.begin(), res.end(), name) == res.end())
01356 res.push_back(name);
01357 }
01358 String fName = concept + ".txt";
01359 Impala::Util::DatabaseWriteString(fName, db, res.begin(), res.end());
01360 }
01361 delete simSet;
01362 delete vidSet;
01363 #endif
01364 }
01365
01366 void
01367 DoJudgeConcepts()
01368 {
01369 #ifndef REPOSITORY_TODO
01370 typedef Core::VideoSet::VideoSet VideoSet;
01371 typedef Core::VideoSet::Segmentation Segmentation;
01372 typedef Core::VideoSet::Keyframes Keyframes;
01373 typedef Core::Table::SimilarityTableSet SimilarityTableSet;
01374 typedef Core::Table::SimilarityTableSet::RankTableType RankTableType;
01375
01376 ILOG_VAR(Impala.Application.Util.DoJudgeConcepts);
01377 CmdOptions& options = CmdOptions::GetInstance();
01378 if (options.GetNrArg() < 5)
01379 {
01380 ILOG_ERROR("Need at least 5 arguments");
01381 return;
01382 }
01383 String vidSetName = options.GetArg(1);
01384 String namesPath = options.GetArg(2);
01385 String mappingName = options.GetArg(3);
01386 String judgeFile = options.GetArg(4);
01387
01388 VideoSet* vidSet = Core::VideoSet::MakeVideoSet(vidSetName);
01389 SegmentationLocator sLoc(vidSetName, "segmentation", options);
01390 Segmentation* segmentation = SegmentationRepository().Get(sLoc, vidSet);
01391 KeyframesLocator kLoc(vidSetName, "keyframes", options);
01392 Keyframes* keyframes = KeyframesRepository().Get(kLoc, vidSet);
01393 Impala::Util::Database* db = vidSet->GetDatabase();
01394 SimilarityTableSet* simSet =
01395 Core::Table::SimilarityTableSet::MakeFromFile(namesPath, db);
01396
01397 std::vector<String> lines;
01398 Impala::Util::SimpleMap<String, String> conceptMapping;
01399 Impala::Util::DatabaseReadStrings(lines, mappingName, db);
01400 for (int i=0 ; i<lines.size() ; i++)
01401 {
01402 Impala::Util::StringParser p(lines[i]);
01403 String topic = p.GetString(' ');
01404 String concept = p.GetString(' ', false);
01405 std::cout << topic << " = " << concept << std::endl;
01406 conceptMapping.Add(concept, topic);
01407 }
01408 Core::Trec::SearchJudge judge(judgeFile);
01409 judge.Stats();
01410
01411 Real64 miap = 0;
01412 int nrMiap = 0;
01413 std::cout << namesPath << ", size = " << simSet->TableSize() << std::endl;
01414 for (int c=0 ; c<simSet->NrTables() ; c++)
01415 {
01416 String concept = simSet->GetName(c);
01417 ILOG_DEBUG("Doing concept " << concept);
01418 std::vector<String> res;
01419 RankTableType* rTable = simSet->GetRankTable(c);
01420 for (int i=0 ; i<rTable->Size() ; i++)
01421 {
01422 Quid quid = rTable->Get1(i);
01423 int shot = -1;
01424 if (QuidClass(quid) == QUID_CLASS_FRAME)
01425 {
01426 int keyfr = keyframes->GetFrameId(quid);
01427 if (keyfr != -1)
01428 shot = keyframes->GetShotId(keyfr);
01429 }
01430 if (QuidClass(quid) == QUID_CLASS_SHOT)
01431 {
01432 shot = segmentation->GetShotId(quid);
01433 }
01434 if (shot == -1)
01435 {
01436 ILOG_ERROR("Unable to find shot for " << QuidObj(quid));
01437 continue;
01438 }
01439 String name = segmentation->GetName(shot);
01440 if (std::find(res.begin(), res.end(), name) == res.end())
01441 res.push_back(name);
01442 if (res.size() >= 2000)
01443 break;
01444 }
01445 String topic;
01446 if (!conceptMapping.Get(concept, topic))
01447 {
01448 ILOG_ERROR("No topic for " << concept);
01449 continue;
01450 }
01451 if (!judge.HasTopic(topic))
01452 {
01453 ILOG_INFO("No judge for " << topic << " = " << concept);
01454 continue;
01455 }
01456 Real64 infAP = judge.Judge(topic, res);
01457 std::cout << concept << " " << infAP << std::endl;
01458 miap += infAP;
01459 nrMiap++;
01460 }
01461 std::cout << "miap = " << miap / nrMiap << std::endl << std::endl;
01462 delete simSet;
01463 delete vidSet;
01464 #endif
01465 }
01466
01467 void
01468 DoPing()
01469 {
01470 #ifndef REPOSITORY_TODO
01471 typedef Impala::Util::Channel Channel;
01472
01473 ILOG_VAR(Impala.Application.Util.DoPing);
01474 Channel* channel = Impala::Util::Database::GetInstance().GetDataChannel();
01475 if (! (channel && channel->Valid()))
01476 {
01477 ILOG_ERROR("No server, use --dataServer to specify server");
01478 return;
01479 }
01480 char* buf = channel->Buffer();
01481 sprintf(buf, "ping\0");
01482 channel->SendRequest(strlen(buf)+1);
01483 if (channel->LastSendHadError())
01484 {
01485 ILOG_ERROR("Failed to send message");
01486 return;
01487 }
01488 ILOG_INFO("Server OK");
01489 #endif
01490 }
01491
01492 int
01493 mainUtil(int argc, char* argv[])
01494 {
01495 CmdOptions& options = CmdOptions::GetInstance();
01496 options.Initialise(false, false, true);
01497 options.AddOption(0, "start", "nr", "0");
01498 options.AddOption(0, "end", "nr", "-1");
01499 String usageStr = "cmd = \n\n";
01500 usageStr += " trectopicdata ...\n";
01501 usageStr += " trecset year {devel|test} collection.xml\n";
01502 usageStr += " trecjudge qrel.txt shotresult.txt topiccode\n";
01503 usageStr += " dumpstills videoset.txt\n";
01504 usageStr += " dumpkeyframes videoset.txt\n";
01505 usageStr += " dumpsegmentation videoset.txt\n";
01506 usageStr += " exportshotmpeg7 videoset.txt {25|30000} generateShotNames withKeyframes\n";
01507 usageStr += " exportannompeg7 videoset.txt {25|30000} concepts.txt\n";
01508 usageStr += " importannotations videoset.txt {25|30000} concepts.txt\n";
01509 usageStr += " exportconceptmpeg7 videoset.txt {25|30000} names.txt\n";
01510 usageStr += " dumpparameter videoset.txt concepts.txt model featuredef\n";
01511 usageStr += " apconfusion videoset.txt annoset.txt names.txt [mapping]\n";
01512 usageStr += " scoreconcepts videoset.txt annoset.txt names.txt [mapping]\n";
01513 usageStr += " scoreconceptsvideo videoset.txt annoset.txt names.txt vidStart vidEnd\n";
01514 usageStr += " combineconcepts videoset.txt concepts.txt model resultName {avg|mil} {feature}+\n";
01515 usageStr += " mapconcepts2shots videoset.txt conceptSet model feature\n";
01516 usageStr += " concepts2trecresult videoset.txt names.txt\n";
01517 usageStr += " judgeconcepts videoset.txt names.txt conceptMapping.txt qrels\n";
01518 usageStr += " ping\n";
01519 usageStr += " checknativetypesize\n";
01520 if (! options.ParseArgs(argc, argv, usageStr, 1))
01521 return 1;
01522
01523 ILOG_VAR(Impala.Application.Util.mainUtil);
01524
01525 String cmd = options.GetArg(0);
01526 ILOG_DEBUG("cmd = " << cmd);
01527 if (cmd == "trectopicdata")
01528 DoTrecTopicData();
01529 else if (cmd == "trecset")
01530 DoTrecSet();
01531 else if (cmd == "trecjudge")
01532 DoTrecJudge();
01533 else if (cmd == "dumpstills")
01534 DoDumpStills();
01535 else if (cmd == "exportshotmpeg7")
01536 DoExportShotMpeg7();
01537 else if (cmd == "exportannompeg7")
01538 DoExportAnnoMpeg7();
01539 else if (cmd == "importannotations")
01540 DoImportAnnotations();
01541 else if (cmd == "importannotationsork")
01542 DoImportAnnotationsOrk();
01543 else if (cmd == "importannotationsactive")
01544 DoImportAnnotationsActive();
01545 else if (cmd == "exportconceptmpeg7")
01546 DoExportConceptMpeg7();
01547 else if (cmd == "dumpparameter")
01548 DoDumpParameter();
01549 else if (cmd == "apconfusion")
01550 DoAPConfusion();
01551 else if (cmd == "scoreconcepts")
01552 DoScoreConcepts();
01553 else if (cmd == "scoreconceptsvideo")
01554 DoScoreConceptsVideo();
01555 else if (cmd == "combineconcepts")
01556 DoCombineConcepts();
01557 else if (cmd == "mapconcepts2shots")
01558 DoMapConcepts2Shots();
01559 else if (cmd == "concepts2trecresult")
01560 DoConcepts2TrecResult();
01561 else if (cmd == "judgeconcepts")
01562 DoJudgeConcepts();
01563 else if (cmd == "ping")
01564 DoPing();
01565 else if (cmd == "checknativetypesize")
01566 Impala::Core::Array::Element::CheckNativeTypeSize();
01567 else ILOG_ERROR("Unknown cmd : " << cmd);
01568
01569 return 0;
01570 }
01571
01572 }
01573 }
01574 }
01575
01576 int
01577 main(int argc, char* argv[])
01578 {
01579 return Impala::Application::Util::mainUtil(argc, argv);
01580 }