Home || Visual Search || Applications || Architecture || Important Messages || OGL || Src

mainUtil.cpp

Go to the documentation of this file.
00001 #include "Persistency/SimilarityTableSetRepository.h"
00002 #include "Persistency/BestFileRepository.h"
00003 #include "Persistency/SegmentationRepository.h"
00004 #include "Persistency/FrameRepository.h"
00005 #include "Persistency/KeywordListRepository.h"
00006 #include "Persistency/AnnotationTableSetRepository.h"
00007 #include "Persistency/QuidTableRepository.h"
00008 #include "Util/SimpleMap.h"
00009 #include "Util/StringParser.h"
00010 #include "Core/Table/AveragePrecision.h"
00011 #include "Core/Table/AveragePrecisionConfusion.h"
00012 #include "Core/Table/SelectQuids.h"
00013 #include "Core/Trec/TrecTopic.h"
00014 #include "Core/Trec/Collection.h"
00015 #include "Core/Trec/SearchJudge.h"
00016 #include "Core/Array/Element/CheckNativeTypeSize.h"
00017 #include "Core/Array/LimitCompressedImageSize.h"
00018 #include "Core/Array/WritePng.h"
00019 #include "Core/Array/ImageArchiveMemory.h"
00020 #include "Core/Column/AddAssign.h"
00021 #include "Core/Column/MulAssign.h"
00022 #include "Core/Column/DivAssign.h"
00023 #include "Core/Database/MakeRawDataSet.h"
00024 #include "Core/VideoSet/Stills.h"
00025 #include "Core/VideoSet/Keyframes.h"
00026 #include "Core/VideoSet/Mpeg7DocWrite.h"
00027 
00028 
00029 // since we are not using libraries:
00030 #include "Link/ImpalaLib.cpp"
00031 
00032 namespace Impala
00033 {
00034 namespace Application
00035 {
00036 namespace Util
00037 {
00038 
00039 
00040 using namespace Persistency;
00041 
00042 void
00043 DoCreateImageSet()
00044 {
00045     typedef Core::ImageSet::ImageSet ImageSet;
00046     typedef Core::Array::Array2dScalarUInt8 Array2dScalarUInt8;
00047     typedef Core::Array::Array2dVec3UInt8 Array2dVec3UInt8;
00048 
00049     ILOG_VAR(Impala.Application.Util.DoCreateImageSet);
00050     CmdOptions& options = CmdOptions::GetInstance();
00051     if (options.GetNrArg() < 3)
00052     {
00053         ILOG_ERROR("Need more parameters");
00054         return;
00055     }
00056 
00057     String setName = options.GetArg(1);
00058     ImageSet imSet(setName);
00059     std::vector<Array2dScalarUInt8*> imList;
00060 
00061     String container("."); // Kill directory structure for now
00062     int a = 2;
00063     RepositoryInFileSystem& repFS = RepositoryInFileSystem::GetInstance();
00064     std::vector<String> input;
00065     String fName = options.GetArg(a);
00066     if (FileNameExt(fName) == "txt")
00067     {
00068         String fName = options.GetArg(a);
00069         FileLocator loc(fName);
00070         Persistency::File file = repFS.GetFile(loc, false, false);
00071         file.ReadStrings(input);
00072     }
00073     else
00074     {
00075         while (a < options.GetNrArg())
00076         {
00077             String fName = options.GetArg(a++);
00078             input.push_back(fName);
00079         }
00080     }
00081     ILOG_INFO("Input contains " << input.size() << " files");
00082     for (int i=0 ; i<input.size() ; i++)
00083     {
00084         String fName = input[i];
00085         String tail = FileNameTail(fName);
00086         //String container = FileNamePath2(fName);
00087         imSet.AddFile(tail, container);
00088         FileLocator loc(fName);
00089         Persistency::File file = repFS.GetFile(loc, false, false);
00090         if (!file.Valid())
00091             return;
00092         Impala::Util::IOBuffer* ioBuf = file.GetReadBuffer();
00093         Int64 bufSize = ioBuf->Size();
00094         Array2dScalarUInt8* bufIm =
00095             Core::Array::ArrayCreate<Array2dScalarUInt8>(bufSize, 1);
00096         Int64 nrRead = ioBuf->Read(bufIm->CPB(), ioBuf->Size());
00097 
00098         Core::Array::LimitCompressedImageSize(bufIm, 720);
00099         imList.push_back(bufIm);
00100     }
00101     ImageSetLocator setLoc(setName, options);
00102     ImageSetRepository().Add(setLoc, &imSet);
00103 
00104     ImageArchiveLocator arLoc(setLoc, false, container, "images.raw", 0);
00105     Core::Array::ImageArchiveMemory wrapper(&imList);
00106     ImageArchiveRepository().Add(arLoc, &wrapper);
00107     Core::Array::ArrayListDelete(&imList);
00108 }
00109 
00110 void
00111 DoTrecTopicData()
00112 {
00113     typedef Core::VideoSet::VideoSet VideoSet;
00114     typedef Core::VideoSet::Keyframes Keyframes;
00115     typedef Core::Array::Array2dVec3UInt8 Array2dVec3UInt8;
00116 
00117     ILOG_VAR(Impala.Application.Util.DoTrecTopicData);
00118     CmdOptions& options = CmdOptions::GetInstance();
00119     if (options.GetNrArg() < 4)
00120     {
00121         ILOG_ERROR("Need more parameters");
00122         return;
00123     }
00124     VideoSet* sVidSet = Core::VideoSet::MakeVideoSet(options.GetArg(1));
00125     Core::Trec::TrecTopic topic(options.GetArg(2));
00126     ILOG_DEBUG("Generating data sets");
00127     String dSetName = topic.GenerateDataSets(options.GetArg(3));
00128     bool doVideo = true;
00129     if (options.GetNrArg() > 4)
00130         if (options.GetArg(4) == "novideo")
00131             doVideo = false;
00132     VideoSet* dVidSet = Core::VideoSet::MakeVideoSet(dSetName);
00133     KeyframesLocator kLoc(dSetName, "keyframes", options);
00134     Keyframes* keyframes = KeyframesRepository().Get(kLoc, dVidSet);
00135     int curKey = -1;
00136     for (int i=0 ; i<topic.NrTopic() ; i++)
00137     //for (int i=0 ; i<1 ; i++)
00138     {
00139         for (int j=0 ; j<topic.GetNrExamples(i) ; j++)
00140         {
00141             curKey++;
00142             ILOG_INFO("nr " << curKey << " of " << keyframes->GetNrKeyframes());
00143             ILOG_DEBUG("topic = " << i << ", example = " << j);
00144             ILOG_DEBUG("topic = " << topic.GetTopicNum(i));
00145             String vidName = topic.GetVideoExampleSrcFile(i, j);
00146             ILOG_DEBUG("vidName = " << vidName);
00147             int vidId = sVidSet->GetFileId(vidName);
00148             if (vidId == -1)
00149             {
00150                 ILOG_ERROR("Unable to locate video " << vidName);
00151                 continue;
00152             }
00153             Core::Stream::RgbDataSrc* src = sVidSet->GetVideo(vidId);
00154             if (src == 0)
00155             {
00156                 ILOG_ERROR("Unable to locate video " << vidName);
00157                 continue;
00158             }
00159             int startFr = topic.GetVideoExampleStartFrame(i, j);
00160             int endFr = topic.GetVideoExampleEndFrame(i, j);
00161             src->GotoFrame((startFr + endFr) / 2);
00162             Array2dVec3UInt8* wrapper = Core::Array::ArrayCreate<Array2dVec3UInt8>
00163                 (src->FrameWidth(), src->FrameHeight(), 0, 0, src->DataPtr(),
00164                  true);
00165             int frameNr = keyframes->GetFrameNr(curKey);
00166             String container = dVidSet->GetContainerFile(curKey);
00167             Persistency::FrameLocator loc(dVidSet->GetLocator(), container,
00168                                           frameNr, ".png");
00169             Persistency::FrameRepository().Add(loc, wrapper);
00170             delete wrapper;
00171             delete src;
00172 
00173             if (!doVideo)
00174                 continue;
00175 
00176             // get video clip
00177             String exeName("mencoder -oac copy -ovc copy -of mpeg");
00178             Impala::Util::StringParser pStart(topic.GetVideoExampleStart(i, j));
00179             int startM = pStart.GetInt('m');
00180             pStart.AdvanceP();
00181             int startS = pStart.GetInt('.');
00182             pStart.AdvanceP();
00183             int startF = pStart.GetInt('s');
00184             String duration = topic.GetVideoExampleDuration(i, j);
00185             String vidFileName = sVidSet->GetResolvedVideoPath(vidId);
00186             char sysBuf[1024];
00187             sprintf(sysBuf, "%s %s -o %s -ss %d:%d.%d -endpos %s",
00188                     exeName.c_str(), vidFileName.c_str(),
00189                     topic.GetVideoExampleName(i, j, true).c_str(),
00190                     startM, startS, startF, duration.c_str());
00191 
00192             for (int c=0 ; c<1024 ; c++)
00193                 if (sysBuf[c] == '/')
00194                     sysBuf[c] = '\\';
00195                 
00196             ILOG_DEBUG("sysbuf = [" << sysBuf << "]");
00197             int sysRes = system(sysBuf);
00198             if (sysRes != 0)
00199             {
00200                 ILOG_ERROR("system ["  << sysBuf << "]");
00201                 return;
00202             }
00203         }
00204     }
00205     delete sVidSet;
00206     delete dVidSet;
00207 }
00208 
00209 void
00210 DoTrecSet()
00211 {
00212     ILOG_VAR(Impala.Application.Util.DoTrecSet);
00213     CmdOptions& options = CmdOptions::GetInstance();
00214     if (options.GetNrArg() != 6)
00215     {
00216         ILOG_ERROR("Need 6 arguments");
00217         return;
00218     }
00219     String setName = options.GetArg(1);
00220     String use = options.GetArg(2);
00221     String collName = options.GetArg(3);
00222     String clipsKeys = options.GetArg(4);
00223     if (clipsKeys == "nil")
00224         clipsKeys = "";
00225     bool vidSetOnly = StringToBool(options.GetArg(5));
00226     Persistency::FileLocator loc(collName);
00227     Core::Trec::Collection coll(loc);
00228     //coll.WriteTo(std::cout);
00229     Locator setLoc(setName, options);
00230     coll.WriteVideoSet(setLoc, use, clipsKeys, vidSetOnly);
00231 }
00232 
00233 void
00234 DoTrecJudge()
00235 {
00236     ILOG_VAR(Impala.Application.Util.DoTrecJudge);
00237     CmdOptions& options = CmdOptions::GetInstance();
00238     if (options.GetNrArg() < 4)
00239     {
00240         ILOG_ERROR("Need at least 4 arguments");
00241         return;
00242     }
00243     String judgeFile = options.GetArg(1);
00244     String shotFile = options.GetArg(2);
00245     String topic = options.GetArg(3);
00246     Core::Trec::SearchJudge judge(judgeFile);
00247     judge.Stats();
00248     std::vector<String> shots;
00249     Persistency::File file = RepositoryGetFile(shotFile, false, false);
00250     file.ReadStrings(shots);
00251 
00252     Real64 infAP = judge.Judge(topic, shots);
00253     std::cout << "infAP = " << infAP << std::endl;
00254 }
00255 
00256 void
00257 DoTrecDropped()
00258 {
00259     typedef Core::VideoSet::VideoSet VideoSet;
00260     typedef Core::VideoSet::Segmentation Segmentation;
00261 
00262     ILOG_VAR(Impala.Application.Util.DoTrecDropped);
00263     CmdOptions& options = CmdOptions::GetInstance();
00264     if (options.GetNrArg() < 2)
00265     {
00266         ILOG_ERROR("Need at least 2 arguments");
00267         return;
00268     }
00269     String setName = options.GetArg(1);
00270     String dropName = options.GetArg(2);
00271 
00272     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(setName);
00273     SegmentationLocator sLoc(setName, "segmentation", options);
00274     Segmentation* seg = SegmentationRepository().Get(sLoc, vidSet);
00275     ILOG_INFO("nrVideos = " << seg->GetNrVideos());
00276 
00277     Persistency::FileLocator fLoc(vidSet->GetLocator(), dropName);
00278     typedef Persistency::RepositoryInFileSystem FS;
00279     Persistency::File file = FS::GetInstance().GetFile(fLoc, false, false);
00280     std::vector<String> dropped;
00281     file.ReadStrings(dropped);
00282     std::cout << "dropped size = " << dropped.size() << std::endl;
00283 
00284     int nrFiles = 0;
00285     int nrShots = 0;
00286     std::vector<int> ids;
00287     for (int i=0 ; i<dropped.size() ; i++)
00288     {
00289         int fileId = vidSet->GetFileId(dropped[i]);
00290         if (fileId == -1)
00291             continue;
00292         nrFiles++;
00293         nrShots += seg->GetNrShotsVideo(fileId);
00294         ids.push_back(fileId);
00295     }
00296     std::cout << "dropped " << nrFiles << " files and " << nrShots << " shots"
00297               << std::endl;
00298     std::sort(ids.begin(), ids.end());
00299     for (int i=0 ; i<ids.size() ; i++)
00300         std::cout << ids[i] << std::endl;
00301     delete seg;
00302     delete vidSet;
00303 }
00304 
00305 void
00306 DoTrecMissing()
00307 {
00308     typedef Core::VideoSet::VideoSet VideoSet;
00309     typedef Core::VideoSet::Keyframes Keyframes;
00310     typedef Core::Table::QuidTable QuidTable;
00311 
00312     ILOG_VAR(Impala.Application.Util.DoTrecMissing);
00313     CmdOptions& options = CmdOptions::GetInstance();
00314     if (options.GetNrArg() < 2)
00315     {
00316         ILOG_ERROR("Need at least 2 arguments");
00317         return;
00318     }
00319     String setName = options.GetArg(1);
00320     String missingName = options.GetArg(2);
00321 
00322     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(setName);
00323     KeyframesLocator kLoc(setName, "keyframes", options);
00324     Keyframes* keyframes = KeyframesRepository().Get(kLoc, vidSet);
00325 
00326     Persistency::FileLocator fLoc(vidSet->GetLocator(), missingName);
00327     typedef Persistency::RepositoryInFileSystem FS;
00328     Persistency::File file = FS::GetInstance().GetFile(fLoc, false, false);
00329     std::vector<String> missing;
00330     file.ReadStrings(missing);
00331     std::cout << "missing size = " << missing.size() << std::endl;
00332 
00333     QuidTable* quids = new QuidTable();
00334     int nrFiles = 0;
00335     int nrShots = 0;
00336     for (int i=0 ; i<missing.size() ; i++)
00337     {
00338         int keyId = keyframes->GetFrameId(missing[i]);
00339         if (keyId == -1)
00340         {
00341             ILOG_ERROR("Could find keyframe " << missing[i]);
00342             continue;
00343         }
00344         Quid q = keyframes->GetQuidFrame(keyId);
00345         quids->Add(q);
00346     }
00347     ILOG_INFO("quid size = " << quids->Size());
00348 
00349     QuidTableLocator qLoc(vidSet->GetLocator(), "VideoIndex",
00350                           FileNameBase(missingName) + ".tab");
00351     QuidTableRepository().Add(qLoc, quids);
00352     delete quids;
00353     delete keyframes;
00354     delete vidSet;
00355 }
00356 
00357 void
00358 DoExportShotMpeg7()
00359 {
00360     typedef Core::VideoSet::VideoSet VideoSet;
00361     typedef Core::VideoSet::Segmentation Segmentation;
00362     typedef Core::VideoSet::Keyframes Keyframes;
00363     typedef Core::VideoSet::Mpeg7Doc Mpeg7Doc;
00364 
00365     ILOG_VAR(Impala.Application.Util.DoExportShotMpeg7);
00366     CmdOptions& options = CmdOptions::GetInstance();
00367     if (options.GetNrArg() < 5)
00368     {
00369         ILOG_ERROR("Need more parameters");
00370         return;
00371     }
00372     String videoSetName = options.GetArg(1);
00373     int fractions = Impala::atol(options.GetArg(2));
00374     if ((fractions != 25) && (fractions != 30000))
00375         ILOG_WARN("Never tried fractions = " << fractions);
00376     bool generateShotNames = Impala::atol(options.GetArg(3));
00377     bool withKeyframes = Impala::atol(options.GetArg(4));
00378 
00379     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(videoSetName);
00380     SegmentationLocator sLoc(videoSetName, "segmentation", options);
00381     Segmentation* segmentation = SegmentationRepository().Get(sLoc, vidSet);
00382     KeyframesLocator kLoc(videoSetName, "keyframes", options);
00383     Keyframes* keyframes = KeyframesRepository().Get(kLoc, vidSet);
00384     for (int v=0 ; v<vidSet->NrFiles() ; v++)
00385     {
00386         String fileName = vidSet->GetFile(v);
00387         ILOG_INFO("doing file " << fileName);
00388         String id = fileName;
00389         int nrFrames = segmentation->GetNrFramesVideo(v);
00390         int frameRateNum = 0;
00391         int frameRateDen = 0;
00392         if (fractions == 0)
00393         {
00394             Core::Stream::RgbDataSrc* rgbSrc = vidSet->GetVideo(v);
00395             if ((!rgbSrc) || (rgbSrc->FrameRateNum() < 0)
00396                 || (rgbSrc->FrameRateDen() < 0))
00397             {
00398                 ILOG_ERROR("Need src with known frame rate");
00399                 continue;
00400             }
00401             frameRateNum = rgbSrc->FrameRateNum();
00402             frameRateDen = rgbSrc->FrameRateDen();
00403             if (rgbSrc)
00404                 delete rgbSrc;
00405         }
00406         Mpeg7Doc mp7(id, fileName, nrFrames, fractions, withKeyframes, "", "",
00407                      frameRateNum, frameRateDen);
00408         for (int s=0 ; s<segmentation->GetNrShotsVideo(v) ; s++)
00409         {
00410             int shot = segmentation->GetFirstShotVideo(v) + s;
00411             String shotName = (generateShotNames)
00412                 ? "shot" + MakeString(v+1) + "_" + MakeString(s+1)
00413                 : segmentation->GetName(shot);
00414             if (withKeyframes)
00415             {
00416                 std::vector<String> names;
00417                 std::vector<int> frames;
00418                 for (int k=0 ; k<keyframes->GetNrKeyframesShot(shot) ; k++)
00419                 {
00420                     int key = keyframes->GetFirstKeyframeShot(shot) + k;
00421                     String keyName = (generateShotNames)
00422                         ? "shot"+MakeString(v+1)+"_"+MakeString(s+1)+"_RKF"
00423                         : Impala::FileNameBase(keyframes->GetName(key));
00424                     names.push_back(keyName);
00425                     frames.push_back(keyframes->GetFrameNr(key));
00426                 }
00427                 mp7.AddShot(shotName, segmentation->GetStart(shot),
00428                             segmentation->GetEnd(shot), names, frames);
00429             }
00430             else
00431             {
00432                 mp7.AddShot(shotName, segmentation->GetStart(shot),
00433                             segmentation->GetEnd(shot));
00434             }
00435         }
00436         Core::VideoSet::Mpeg7DocWrite(&mp7, vidSet, v);
00437     }
00438 }
00439 
00440 /* Create CA annotations from two datasets, one with positives (idash_annotator)
00441    and one with only negative examples.
00442    The latter is based on the middle keyframe of videos from the Ewijk set.
00443  */
00444 void
00445 DoCreateCaAnno()
00446 {
00447     typedef Core::VideoSet::VideoSet VideoSet;
00448     typedef Core::VideoSet::Keyframes Keyframes;
00449     typedef Core::Table::AnnotationTable AnnotationTable;
00450 
00451     ILOG_VAR(Impala.Application.Util.DoCreateCaAnno);
00452 
00453     Core::Table::AnnotationTableSet tSet;
00454     AnnotationTable* tab = new AnnotationTable("CaMaterial", 0);
00455     tSet.Add(tab);
00456 
00457     VideoSet* vidSetPosi = Core::VideoSet::MakeVideoSet("idash_annotator.txt");
00458     KeyframesLocator kLocPosi(vidSetPosi->GetLocator(), "keyframes");
00459     Keyframes* keyframesPosi = KeyframesRepository().Get(kLocPosi, vidSetPosi);
00460     for (int v=0 ; v<vidSetPosi->NrFiles() ; v++)
00461     //for (int v=0 ; v<2 ; v++)
00462     {
00463         int nrKeys = keyframesPosi->GetNrKeyframesVideo(v);
00464         int keyId = keyframesPosi->GetFirstKeyframeVideo(v) + nrKeys - 1;
00465         Quid q = keyframesPosi->GetQuidFrame(keyId);
00466         tab->AddPositive(q);
00467     }
00468 
00469     //VideoSet* vidSetNega = Core::VideoSet::MakeVideoSet("idashtest4.txt");
00470     VideoSet* vidSetNega = Core::VideoSet::MakeVideoSet("EwijkMID.txt");
00471     KeyframesLocator kLocNega(vidSetNega->GetLocator(), "keyframes");
00472     Keyframes* keyframesNega = KeyframesRepository().Get(kLocNega, vidSetNega);
00473     for (int v=0 ; v<vidSetNega->NrFiles() ; v++)
00474     //for (int v=0 ; v<2 ; v++)
00475     {
00476         int nrKeys = keyframesNega->GetNrKeyframesVideo(v);
00477         int keyId = keyframesNega->GetFirstKeyframeVideo(v) + nrKeys - 1;
00478         Quid q = keyframesNega->GetQuidFrame(keyId);
00479         tab->AddNegative(q);
00480     }
00481 
00482     AnnotationTableSetLocator aLoc(vidSetPosi->GetLocator(), QUID_CLASS_FRAME,
00483                                    "conceptsAnnotator.txt");
00484     AnnotationTableSetRepository().Add(aLoc, &tSet);
00485     delete keyframesPosi;
00486     delete vidSetPosi;
00487     delete keyframesNega;
00488     delete vidSetNega;
00489 }
00490 
00491 /* Export .truth files as Mpeg7 files. */
00492 void
00493 DoExportAnnoMpeg7()
00494 {
00495     typedef Core::VideoSet::VideoSet VideoSet;
00496     typedef Core::VideoSet::Segmentation Segmentation;
00497     typedef Core::VideoSet::Keyframes Keyframes;
00498     typedef Core::VideoSet::Mpeg7Doc Mpeg7Doc;
00499 
00500     ILOG_VAR(Impala.Application.Util.DoExportAnnoMpeg7);
00501     CmdOptions& options = CmdOptions::GetInstance();
00502     if (options.GetNrArg() < 5)
00503     {
00504         ILOG_ERROR("Need more parameters");
00505         return;
00506     }
00507     String videoSetName = options.GetArg(1);
00508     int fractions = Impala::atol(options.GetArg(2));
00509     if ((fractions != 25) && (fractions != 30000))
00510         ILOG_WARN("Never tried fractions = " << fractions);
00511     String conceptSet = options.GetArg(3);
00512     String anno = options.GetArg(4);
00513     bool hasKeyframes = false;
00514     if (anno == "hasKeyframes")
00515     {
00516         hasKeyframes = true;
00517     }
00518     else if (anno != "hasShots")
00519     {
00520         ILOG_ERROR("Argument should be hasShots or hasKeyframes");
00521         return;
00522     }
00523 
00524     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(videoSetName);
00525     SegmentationLocator sLoc(videoSetName, "segmentation", options);
00526     Segmentation* segmentation = SegmentationRepository().Get(sLoc, vidSet);
00527     KeyframesLocator kLoc(videoSetName, "keyframes", options);
00528     Keyframes* keyframes = KeyframesRepository().Get(kLoc, vidSet);
00529     Persistency::KeywordListLocator loc(vidSet->GetLocator(), conceptSet);
00530     Core::Table::KeywordList concepts =
00531         *(Persistency::KeywordListRepository().Get(loc));
00532     for (int c=0 ; c<concepts.size() ; c++)
00533     {
00534         ILOG_INFO("doing concept " << concepts[c]);
00535         ILOG_NDC_PUSH(concepts[c]);
00536         String fName = "Annotations/groundtruth/" + concepts[c] + ".truth";
00537         Persistency::FileLocator fLoc(vidSet->GetLocator(), fName);
00538         Persistency::File file = RepositoryGetFile(fLoc, false, false);
00539         std::vector<String> annos;
00540         file.ReadStrings(annos);
00541         std::sort(annos.begin(), annos.end());
00542         for (int v=0 ; v<vidSet->NrFiles() ; v++)
00543         {
00544             String vidFile = vidSet->GetFile(v);
00545             int nrFrames = segmentation->GetNrFramesVideo(v);
00546             int frameRateNum = 0;
00547             int frameRateDen = 0;
00548             if (fractions == 0)
00549             {
00550                 Core::Stream::RgbDataSrc* rgbSrc = vidSet->GetVideo(v);
00551                 if ((!rgbSrc) || (rgbSrc->FrameRateNum() < 0)
00552                     || (rgbSrc->FrameRateDen() < 0))
00553                 {
00554                     ILOG_ERROR("Need src with known frame rate");
00555                     continue;
00556                 }
00557                 frameRateNum = rgbSrc->FrameRateNum();
00558                 frameRateDen = rgbSrc->FrameRateDen();
00559                 if (rgbSrc)
00560                     delete rgbSrc;
00561             }
00562             Mpeg7Doc mp7(vidFile, vidFile, nrFrames, fractions, false,
00563                          conceptSet, concepts[c], frameRateNum, frameRateDen);
00564             if (hasKeyframes)
00565             {
00566                 for (int k=0 ; k<keyframes->GetNrKeyframesVideo(v) ; k++)
00567                 {
00568                     int keyId = keyframes->GetFirstKeyframeVideo(v) + k;
00569                     String keyName = FileNameBase(keyframes->GetName(keyId));
00570                     int frameNr = keyframes->GetFrameNr(keyId);
00571                     double relevance = 0;
00572                     if (find(annos.begin(),annos.end(),keyName) != annos.end())
00573                         relevance = 1;
00574                     mp7.AddAnnotation(concepts[c], keyName, frameNr, frameNr,
00575                                       relevance, 1);
00576                 }
00577             }
00578             else
00579             {
00580                 for (int s=0 ; s<segmentation->GetNrShotsVideo(v) ; s++)
00581                 {
00582                     int shotId = segmentation->GetFirstShotVideo(v) + s;
00583                     String shotName = segmentation->GetName(shotId);
00584                     double relevance = 0;
00585                     if (find(annos.begin(),annos.end(),shotName) != annos.end())
00586                         relevance = 1;
00587                     int firstKey = keyframes->GetFirstKeyframeShot(shotId);
00588                     int nrKey = keyframes->GetNrKeyframesShot(shotId);
00589                     for (int k=firstKey ; k<firstKey+nrKey ; k++)
00590                     {
00591                         if (!keyframes->IsRKF(k))
00592                             continue;
00593                         String keyName = FileNameBase(keyframes->GetName(k));
00594                         int frameNr = keyframes->GetFrameNr(k);
00595                         mp7.AddAnnotation(concepts[c], keyName, frameNr,
00596                                           frameNr, relevance, 1);
00597                     }
00598                 }
00599             }
00600             Core::VideoSet::Mpeg7DocWrite(&mp7, vidSet, v);
00601         }
00602         ILOG_NDC_POP;
00603     }
00604 }
00605 
00606 /* Import .truth files with [N]RKF as AnnotationTableSet.
00607  * Note: does positives only!
00608  */
00609 void
00610 DoImportAnnoTruth()
00611 {
00612     typedef Core::VideoSet::VideoSet VideoSet;
00613     typedef Core::VideoSet::Keyframes Keyframes;
00614     typedef Core::Table::AnnotationTableSet AnnotationTableSet;
00615     typedef Core::Table::AnnotationTable AnnotationTable;
00616 
00617     ILOG_VAR(Impala.Application.Util.DoImportAnnoTruth);
00618     CmdOptions& options = CmdOptions::GetInstance();
00619     if (options.GetNrArg() < 4)
00620     {
00621         ILOG_ERROR("Need more parameters");
00622         return;
00623     }
00624     String videoSetName = options.GetArg(1);
00625     String conceptSet = options.GetArg(2);
00626     String maskSet = options.GetArg(3);
00627 
00628     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(videoSetName);
00629     KeyframesLocator kLoc(vidSet->GetLocator(), "keyframes");
00630     Keyframes* keyframes = KeyframesRepository().Get(kLoc, vidSet);
00631     Persistency::KeywordListLocator loc(vidSet->GetLocator(), conceptSet);
00632     Core::Table::KeywordList concepts =
00633         *(Persistency::KeywordListRepository().Get(loc));
00634 
00635     typedef Persistency::RepositoryInFileSystem FS;
00636     Persistency::FileLocator mLoc(vidSet->GetLocator(), maskSet);
00637     Persistency::File mFile = FS::GetInstance().GetFile(mLoc, false, false);
00638     std::vector<String> mask;
00639     mFile.ReadStrings(mask);
00640     if (mask.size() == 0)
00641         ILOG_WARN("mask is empty");
00642 
00643     AnnotationTableSet tSet;
00644     for (int c=0 ; c<concepts.size() ; c++)
00645     {
00646         ILOG_INFO("doing concept " << concepts[c]);
00647         ILOG_NDC_PUSH(concepts[c]);
00648         AnnotationTable* tab = new AnnotationTable(concepts[c], 0);
00649         tSet.Add(tab);
00650 
00651         String fName = "Annotations/groundtruth/" + conceptSet + "/" +
00652             concepts[c] + ".truth";
00653         Persistency::FileLocator fLoc(vidSet->GetLocator(), fName);
00654         Persistency::File file = FS::GetInstance().GetFile(fLoc, false, true);
00655         if (!file.Valid())
00656         {
00657             ILOG_INFO("No truth file for " << concepts[c]);
00658             ILOG_NDC_POP;
00659             continue;
00660         }
00661             
00662         std::vector<String> annos;
00663         file.ReadStrings(annos);
00664         for (int i=0 ; i<annos.size() ; i++)
00665         {
00666             if (find(mask.begin(), mask.end(), annos[i]) == mask.end())
00667             {
00668                 ILOG_DEBUG("not in mask, skipping");
00669                 continue;
00670             }
00671 
00672             int keyId = keyframes->GetFrameId(annos[i] + ".jpg");
00673             if (keyId == -1)
00674             {
00675                 // In trec2005 some NRKF seem to refer to the same frame as RKF
00676                 String h1 = StringHead(annos[i], '_', true);
00677                 String t = StringTail(annos[i], '_', false);
00678                 String h2 = StringHead(t, '_', true);
00679                 String rkf = h1 + h2 + "RKF";
00680                 keyId = keyframes->GetFrameId(rkf + ".jpg");
00681             }
00682             if (keyId == -1)
00683             {
00684                 ILOG_ERROR("Don't know " << annos[i]);
00685             }
00686             else
00687             {
00688                 Quid q = keyframes->GetQuidFrame(keyId);
00689                 tab->AddPositive(q);
00690             }
00691         }
00692         ILOG_NDC_POP;
00693     }
00694 
00695     AnnotationTableSetLocator aLoc(vidSet->GetLocator(), QUID_CLASS_FRAME,
00696                                    conceptSet);
00697     AnnotationTableSetRepository().Add(aLoc, &tSet);
00698     delete keyframes;
00699     delete vidSet;
00700 }
00701 
00702 /* Export frames from AnnotationTableSet as .truth files with [N]RKF. */
00703 void
00704 DoExportAnnoTruth()
00705 {
00706     typedef Core::VideoSet::VideoSet VideoSet;
00707     typedef Core::VideoSet::Keyframes Keyframes;
00708     typedef Core::Table::AnnotationTableSet AnnotationTableSet;
00709     typedef Core::Table::AnnotationTable AnnotationTable;
00710     typedef Core::Table::QuidTable QuidTable;
00711 
00712     ILOG_VAR(Impala.Application.Util.DoExportAnnoTruth);
00713     CmdOptions& options = CmdOptions::GetInstance();
00714     if (options.GetNrArg() < 3)
00715     {
00716         ILOG_ERROR("Need more parameters");
00717         return;
00718     }
00719     String videoSetName = options.GetArg(1);
00720     String conceptSet = options.GetArg(2);
00721 
00722     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(videoSetName);
00723     KeyframesLocator kLoc(vidSet->GetLocator(), "keyframes");
00724     Keyframes* keyframes = KeyframesRepository().Get(kLoc, vidSet);
00725     AnnotationTableSetLocator aLoc(vidSet->GetLocator(), QUID_CLASS_FRAME,
00726                                    conceptSet);
00727     AnnotationTableSet* tSet = AnnotationTableSetRepository().Get(aLoc);
00728     if (tSet == 0)
00729         return;
00730 
00731     for (int t=0 ; t<tSet->Size() ; t++)
00732     {
00733         AnnotationTable* tab = tSet->GetTable(t);
00734         tab->DumpSummary();
00735         QuidTable* quids = tab->GetPositive();
00736         std::vector<String> annos;
00737         for (int i=0 ; i<quids->Size() ; i++)
00738         {
00739             Quid q = quids->Get1(i);
00740             int id = keyframes->GetFrameId(q);
00741             if (id != -1)
00742             {
00743                 String name = keyframes->GetName(id);
00744                 annos.push_back(FileNameBase(name));
00745             }
00746         }
00747         delete quids;
00748 
00749         String fName = "Annotations/groundtruth/" + conceptSet + "/" +
00750             tab->GetLabel() + ".truth";
00751         Persistency::FileLocator fLoc(vidSet->GetLocator(), fName);
00752         typedef Persistency::RepositoryInFileSystem FS;
00753         Persistency::File file = FS::GetInstance().GetFile(fLoc, true, false);
00754         ILOG_INFO("Writing to " << file.GetPath());
00755         file.WriteStrings(annos);
00756     }
00757     delete tSet;
00758     delete keyframes;
00759     delete vidSet;
00760 }
00761 
00762 /* import .ann files (from IMAG).
00763 One line looks like this:
00764 
00765 RYY MCG-ICT-CAS Airplane BG_12460 TRECVID2007_1/shot1_100_RKF N [S]
00766 ==
00767 Annotation_Person Organizer Concept Videoname Keyframename Judgment(Positive/Negative) Sign(S)
00768 
00769 The optional S indicates that the annotator was not sure whether it is Pos/Neg
00770 */
00771 void
00772 DoImportAnnotations()
00773 {
00774     typedef Core::VideoSet::VideoSet VideoSet;
00775     typedef Core::VideoSet::Segmentation Segmentation;
00776     typedef Core::VideoSet::Keyframes Keyframes;
00777     typedef Core::VideoSet::Mpeg7Doc Mpeg7Doc;
00778 
00779     ILOG_VAR(Impala.Application.Util.DoImportAnnotations);
00780     CmdOptions& options = CmdOptions::GetInstance();
00781     if (options.GetNrArg() < 4)
00782     {
00783         ILOG_ERROR("Need more parameters");
00784         return;
00785     }
00786     String videoSetName = options.GetArg(1);
00787     int fractions = Impala::atol(options.GetArg(2));
00788     if ((fractions != 25) && (fractions != 30000))
00789         ILOG_WARN("Never tried fractions = " << fractions);
00790     String conceptSet = options.GetArg(3);
00791 
00792     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(videoSetName);
00793     SegmentationLocator sLoc(videoSetName, "segmentation", options);
00794     Segmentation* segmentation = SegmentationRepository().Get(sLoc, vidSet);
00795     KeyframesLocator kLoc(videoSetName, "keyframes", options);
00796     Keyframes* keyframes = KeyframesRepository().Get(kLoc, vidSet);
00797     FileLocator fLoc(conceptSet);
00798     typedef Persistency::RepositoryInFileSystem FS;
00799     Persistency::File file = FS::GetInstance().GetFile(fLoc, false, false);
00800     std::vector<String> concepts;
00801     file.ReadStrings(concepts);
00802     String path = FileNamePath(conceptSet) + "/";
00803     conceptSet = FileNameTail(conceptSet);
00804     for (int c=0 ; c<concepts.size() ; c++)
00805     {
00806         ILOG_INFO("doing concept " << concepts[c]);
00807         ILOG_NDC_PUSH(concepts[c]);
00808         String fName = path + concepts[c] + ".ann";
00809         std::vector<String> lines;
00810         fLoc = FileLocator(fName);
00811         file = FS::GetInstance().GetFile(fLoc, false, false);
00812         file.ReadStrings(lines);
00813         int curVidId = -1;
00814         Mpeg7Doc* curDoc = 0;
00815         for (int l=0 ; l<lines.size() ; l++)
00816         {
00817             Impala::Util::StringParser p(lines[l]);
00818             String s = p.GetString(' '); // Person
00819             s = p.GetString(' '); // Organizer
00820             s = p.GetString(' '); // Concept
00821             s = p.GetString(' '); // Videoname
00822             s = Impala::FileNameTail(p.GetString(' ')); // Keyframename
00823             int key = keyframes->GetFrameId(s + ".jpg");
00824             if (key == -1)
00825             {
00826                 ILOG_ERROR("Unable to find keyframe " << s);
00827                 continue;
00828             }
00829             double relevance = (p.GetString(' ', false) == "P") ? 1.0 : 0.0;
00830             double confidence = 1.0;
00831             if (!p.TheEnd() && (p.GetString(' ', false) == "S"))
00832                 relevance = 0.5; // open for debate :-)
00833             
00834             if (keyframes->GetVideoId(key) != curVidId)
00835             {   // new video
00836                 if (curDoc)
00837                 {
00838                     Core::VideoSet::Mpeg7DocWrite(curDoc, vidSet, curVidId);
00839                     delete curDoc;
00840                     curDoc = 0;
00841                 }
00842                 curVidId = keyframes->GetVideoId(key);
00843                 String vidFile = vidSet->GetFile(curVidId);
00844                 int nrFrames = segmentation->GetNrFramesVideo(curVidId);
00845                 int frameRateNum = 0;
00846                 int frameRateDen = 0;
00847                 if (fractions == 0)
00848                 {
00849                     Core::Stream::RgbDataSrc* rgbSrc = vidSet->GetVideo(curVidId);
00850                     if ((!rgbSrc) || (rgbSrc->FrameRateNum() < 0)
00851                         || (rgbSrc->FrameRateDen() < 0))
00852                     {
00853                         ILOG_ERROR("Need src with known frame rate");
00854                         continue;
00855                     }
00856                     frameRateNum = rgbSrc->FrameRateNum();
00857                     frameRateDen = rgbSrc->FrameRateDen();
00858                     if (rgbSrc)
00859                         delete rgbSrc;
00860                 }
00861                 curDoc = new Mpeg7Doc(vidFile, vidFile, nrFrames, fractions,
00862                                       false, conceptSet, concepts[c],
00863                                       frameRateNum, frameRateDen);
00864             }
00865             String name = FileNameBase(keyframes->GetName(key));
00866             int frameNr = keyframes->GetFrameNr(key);
00867             curDoc->AddAnnotation(concepts[c], name, frameNr, frameNr,
00868                                   relevance, confidence);
00869         }
00870         if (curDoc)
00871         {
00872             Core::VideoSet::Mpeg7DocWrite(curDoc, vidSet, curVidId);
00873             delete curDoc;
00874         }
00875         ILOG_NDC_POP;
00876     }
00877 }
00878 
00879 /* import .1 (positive shot1_1) and .0 (negative shot1_1) files from Ork
00880    Files are named after rels.
00881 */
00882 void
00883 DoImportAnnotationsOrk()
00884 {
00885     typedef Core::VideoSet::VideoSet VideoSet;
00886     typedef Core::VideoSet::Segmentation Segmentation;
00887     typedef Core::VideoSet::Keyframes Keyframes;
00888     typedef Core::VideoSet::Mpeg7Doc Mpeg7Doc;
00889 
00890     ILOG_VAR(Impala.Application.Util.DoImportAnnotations);
00891     CmdOptions& options = CmdOptions::GetInstance();
00892     if (options.GetNrArg() < 6)
00893     {
00894         ILOG_ERROR("Need more parameters");
00895         return;
00896     }
00897     String videoSetName = options.GetArg(1);
00898     int fractions = Impala::atol(options.GetArg(2));
00899     if ((fractions != 25) && (fractions != 30000))
00900         ILOG_WARN("Never tried fractions = " << fractions);
00901     String conceptSet = options.GetArg(3);
00902     String mappingName = options.GetArg(4);
00903     String inputPath = options.GetArg(5);
00904 
00905     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(videoSetName);
00906     SegmentationLocator sLoc(videoSetName, "segmentation", options);
00907     Segmentation* segmentation = SegmentationRepository().Get(sLoc, vidSet);
00908     KeyframesLocator kLoc(videoSetName, "keyframes", options);
00909     Keyframes* keyframes = KeyframesRepository().Get(kLoc, vidSet);
00910 
00911     Persistency::KeywordListLocator loc(vidSet->GetLocator(), conceptSet);
00912     Core::Table::KeywordList concepts =
00913         *(Persistency::KeywordListRepository().Get(loc));
00914 
00915     std::vector<String> lines;
00916     Impala::Util::SimpleMap<String, String> conceptMapping;
00917     FileLocator fLoc(mappingName);
00918     typedef Persistency::RepositoryInFileSystem FS;
00919     Persistency::File file = FS::GetInstance().GetFile(fLoc, false, false);
00920     file.ReadStrings(lines);
00921 
00922     for (int i=0 ; i<lines.size() ; i++)
00923     {
00924         Impala::Util::StringParser p(lines[i]);
00925         String topic = p.GetString(' ');
00926         String concept = p.GetString(' ', false);
00927         std::cout << topic << " = " << concept << std::endl;
00928         conceptMapping.Add(concept, topic);
00929     }
00930     for (int c=0 ; c<concepts.size() ; c++)
00931     {
00932         ILOG_INFO("doing concept " << concepts[c]);
00933         ILOG_NDC_PUSH(concepts[c]);
00934 
00935         String topic;
00936         if (!conceptMapping.Get(concepts[c], topic))
00937         {
00938             ILOG_ERROR("no topic for " << concepts[c]);
00939             ILOG_NDC_POP;
00940             continue;
00941         }
00942 
00943         std::vector<String> posAnnos;
00944         String fName = inputPath + "." + topic + ".1";
00945         FileLocator posLoc(vidSet->GetLocator(), fName);
00946         file = FS::GetInstance().GetFile(posLoc, false, false);
00947         file.ReadStrings(posAnnos);
00948 
00949         std::vector<String> negAnnos;
00950         fName = inputPath + "." + topic + ".0";
00951         FileLocator negLoc(vidSet->GetLocator(), fName);
00952         file = FS::GetInstance().GetFile(negLoc, false, false);
00953         file.ReadStrings(negAnnos);
00954         if ((posAnnos.size() == 0) || (negAnnos.size() == 0))
00955         {
00956             ILOG_ERROR("missing input for " << concepts[c]);
00957             ILOG_NDC_POP;
00958             continue;
00959         }
00960 
00961         std::sort(posAnnos.begin(), posAnnos.end());
00962         std::sort(negAnnos.begin(), negAnnos.end());
00963         for (int v=0 ; v<vidSet->NrFiles() ; v++)
00964         {
00965             String vidFile = vidSet->GetFile(v);
00966             int nrFrames = segmentation->GetNrFramesVideo(v);
00967             int frameRateNum = 0;
00968             int frameRateDen = 0;
00969             if (fractions == 0)
00970             {
00971                 Core::Stream::RgbDataSrc* rgbSrc = vidSet->GetVideo(v);
00972                 if ((!rgbSrc) || (rgbSrc->FrameRateNum() < 0)
00973                     || (rgbSrc->FrameRateDen() < 0))
00974                 {
00975                     ILOG_ERROR("Need src with known frame rate");
00976                     continue;
00977                 }
00978                 frameRateNum = rgbSrc->FrameRateNum();
00979                 frameRateDen = rgbSrc->FrameRateDen();
00980                 if (rgbSrc)
00981                     delete rgbSrc;
00982             }
00983             Mpeg7Doc mp7(vidFile, vidFile, nrFrames, fractions, false,
00984                          conceptSet, concepts[c], frameRateNum, frameRateDen);
00985 
00986             for (int s=0 ; s<segmentation->GetNrShotsVideo(v) ; s++)
00987             {
00988                 int shotId = segmentation->GetFirstShotVideo(v) + s;
00989                 String shotName = segmentation->GetName(shotId);
00990                 double relevance = -1;
00991                 if (find(posAnnos.begin(), posAnnos.end(), shotName) !=
00992                     posAnnos.end())
00993                 {
00994                     relevance = 1;
00995                 }
00996                 else if (find(negAnnos.begin(), negAnnos.end(), shotName) !=
00997                          negAnnos.end())
00998                 {
00999                     relevance = 0;
01000                 }
01001                 if (relevance < 0)
01002                     continue;
01003                 int key = keyframes->GetShotRKF(shotId);
01004 
01005                 String keyName = FileNameBase(keyframes->GetName(key));
01006                 int frameNr = keyframes->GetFrameNr(key);
01007                 mp7.AddAnnotation(concepts[c], keyName, frameNr, frameNr,
01008                                   relevance, 1);
01009             }
01010             if (mp7.HasAnnotations())
01011                 Core::VideoSet::Mpeg7DocWrite(&mp7, vidSet, v);
01012         }
01013         ILOG_NDC_POP;
01014     }
01015 }
01016 
01017 /* Import annotations from active learning (in Cees' format?).
01018  * Annotations are on RKF's and stored in Annotations/from_active.
01019  * Positives are stored as shot1_1 in file concept/shots_bookmarked.txt and
01020  * negatives are stored in concept/shots_bookmarked_negative.txt.
01021  */
01022 void
01023 DoImportAnnotationsActive()
01024 {
01025     typedef Core::VideoSet::VideoSet VideoSet;
01026     typedef Core::VideoSet::Segmentation Segmentation;
01027     typedef Core::VideoSet::Keyframes Keyframes;
01028     typedef Core::VideoSet::Mpeg7Doc Mpeg7Doc;
01029 
01030     ILOG_VAR(Impala.Application.Util.DoImportAnnotationsActive);
01031     CmdOptions& options = CmdOptions::GetInstance();
01032     if (options.GetNrArg() < 4)
01033     {
01034         ILOG_ERROR("Need more parameters");
01035         return;
01036     }
01037     String videoSetName = options.GetArg(1);
01038     int fractions = Impala::atol(options.GetArg(2));
01039     if ((fractions != 25) && (fractions != 30000))
01040         ILOG_WARN("Never tried fractions = " << fractions);
01041     String conceptSet = options.GetArg(3);
01042 
01043     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(videoSetName);
01044     SegmentationLocator sLoc(videoSetName, "segmentation", options);
01045     Segmentation* segmentation = SegmentationRepository().Get(sLoc, vidSet);
01046     KeyframesLocator kLoc(videoSetName, "keyframes", options);
01047     Keyframes* keyframes = KeyframesRepository().Get(kLoc, vidSet);
01048 
01049     KeywordListLocator loc(vidSet->GetLocator(), conceptSet);
01050     Core::Table::KeywordList concepts =
01051         *(Persistency::KeywordListRepository().Get(loc));
01052     for (int c=0 ; c<concepts.size() ; c++)
01053     {
01054         ILOG_INFO("doing concept " << concepts[c]);
01055         ILOG_NDC_PUSH(concepts[c]);
01056 
01057         std::vector<String> posAnnos;
01058         String fName = "from_active/" + concepts[c] + "/shots_bookmarked.txt";
01059         KeywordListLocator posLoc(vidSet->GetLocator(), fName);
01060         if (KeywordListRepository().Exists(posLoc))
01061             posAnnos = *(KeywordListRepository().Get(posLoc));
01062 
01063         std::vector<String> negAnnos;
01064         fName = "from_active/" + concepts[c] + "/shots_bookmarked_negative.txt";
01065         KeywordListLocator negLoc(vidSet->GetLocator(), fName);
01066         if (KeywordListRepository().Exists(negLoc))
01067             posAnnos = *(KeywordListRepository().Get(negLoc));
01068 
01069         if ((posAnnos.size() == 0) || (negAnnos.size() == 0))
01070         {
01071             ILOG_ERROR("missing input for " << concepts[c]);
01072             ILOG_NDC_POP;
01073             continue;
01074         }
01075 
01076         std::sort(posAnnos.begin(), posAnnos.end());
01077         std::sort(negAnnos.begin(), negAnnos.end());
01078         for (int v=0 ; v<vidSet->NrFiles() ; v++)
01079         {
01080             String vidFile = vidSet->GetFile(v);
01081             int nrFrames = segmentation->GetNrFramesVideo(v);
01082             int frameRateNum = 0;
01083             int frameRateDen = 0;
01084             if (fractions == 0)
01085             {
01086                 Core::Stream::RgbDataSrc* rgbSrc = vidSet->GetVideo(v);
01087                 if ((!rgbSrc) || (rgbSrc->FrameRateNum() < 0)
01088                     || (rgbSrc->FrameRateDen() < 0))
01089                 {
01090                     ILOG_ERROR("Need src with known frame rate");
01091                     continue;
01092                 }
01093                 frameRateNum = rgbSrc->FrameRateNum();
01094                 frameRateDen = rgbSrc->FrameRateDen();
01095                 if (rgbSrc)
01096                     delete rgbSrc;
01097             }
01098             Mpeg7Doc mp7(vidFile, vidFile, nrFrames, fractions, false,
01099                          conceptSet, concepts[c], frameRateNum, frameRateDen);
01100 
01101             for (int s=0 ; s<segmentation->GetNrShotsVideo(v) ; s++)
01102             {
01103                 int shotId = segmentation->GetFirstShotVideo(v) + s;
01104                 String shotName = segmentation->GetName(shotId);
01105                 double relevance = -1;
01106                 if (find(posAnnos.begin(), posAnnos.end(), shotName) !=
01107                     posAnnos.end())
01108                 {
01109                     relevance = 1;
01110                 }
01111                 else if (find(negAnnos.begin(), negAnnos.end(), shotName) !=
01112                          negAnnos.end())
01113                 {
01114                     relevance = 0;
01115                 }
01116                 if (relevance < 0)
01117                     continue;
01118                 int key = keyframes->GetShotRKF(shotId);
01119 
01120                 String keyName = FileNameBase(keyframes->GetName(key));
01121                 int frameNr = keyframes->GetFrameNr(key);
01122                 mp7.AddAnnotation(concepts[c], keyName, frameNr, frameNr,
01123                                   relevance, 1);
01124             }
01125             if (mp7.HasAnnotations())
01126                 Core::VideoSet::Mpeg7DocWrite(&mp7, vidSet, v);
01127         }
01128         ILOG_NDC_POP;
01129     }
01130 }
01131 
01132 void
01133 DoDumpParameter()
01134 {
01135     typedef Core::Database::RawDataSet RawDataSet;
01136     typedef Core::Feature::FeatureDefinition FeatureDef;
01137 
01138     ILOG_VAR(Impala.Application.Util.DoDumpParameter);
01139     CmdOptions& options = CmdOptions::GetInstance();
01140     if (options.GetNrArg() < 5)
01141     {
01142         ILOG_ERROR("Need more parameters");
01143         return;
01144     }
01145     String dataSetName = options.GetArg(1);
01146     String conceptSet = options.GetArg(2);
01147     String model = options.GetArg(3);
01148     String feature = options.GetArg(4);
01149 
01150     RawDataSet* dataSet = Core::Database::MakeRawDataSet(dataSetName);
01151     KeywordListLocator keyLoc(dataSet->GetLocator(), conceptSet);
01152     Core::Table::KeywordList concepts = *(KeywordListRepository().Get(keyLoc));
01153 
01154     ModelLocator modelLoc(dataSet->GetLocator(), conceptSet, model, feature, "");
01155     Real64 map = 0;
01156     int nrMap = 0;
01157     std::cout << "parameter best for " << conceptSet << "  " << model << "  "
01158               << feature << std::endl;
01159     for (int c=0 ; c<concepts.size() ; c++)
01160     {
01161         String con = concepts[c];
01162         modelLoc.SetConcept(con);
01163         Impala::Util::PropertySet* props = BestFileRepository().Get(modelLoc);
01164         Real64 ap = props->GetDouble("value", 0.0);
01165         std::cout << con << " " << ap << std::endl;
01166         map += ap;
01167         nrMap++;
01168     }
01169     std::cout << "map = " << map / nrMap << std::endl << std::endl;
01170 }
01171 
01172 void
01173 DoAPConfusion()
01174 {
01175     typedef Core::Database::RawDataSet RawDataSet;
01176     typedef Core::Table::SimilarityTableSet SimilarityTableSet;
01177     typedef Core::Table::AnnotationTable AnnotationTable;
01178     using Impala::Core::Table::AveragePrecision;
01179 
01180     ILOG_VAR(Impala.Application.Util.DoAPConfusion);
01181     CmdOptions& options = CmdOptions::GetInstance();
01182     if (options.GetNrArg() < 5)
01183     {
01184         ILOG_ERROR("Need more parameters");
01185         return;
01186     }
01187     String dataSetName = options.GetArg(1);
01188     String conceptSet = options.GetArg(2);
01189     String model = options.GetArg(3);
01190     String feature = options.GetArg(4);
01191     String mappingName;
01192     if (options.GetNrArg() > 5)
01193         mappingName = options.GetArg(5);
01194 
01195     RawDataSet* dataSet = Core::Database::MakeRawDataSet(dataSetName);
01196     SimilarityTableSetLocator simLoc(dataSet->GetLocator(), true, "",
01197                                      conceptSet, model, feature, "");
01198     SimilarityTableSet* simSet = SimilarityTableSetRepository().Get(simLoc);
01199     Impala::Util::SimpleMap<String, String> conceptMapping;
01200     if (!mappingName.empty())
01201     {
01202         std::vector<String> lines;
01203         Persistency::File file = RepositoryGetFile(mappingName, false, false);
01204         file.ReadStrings(lines);
01205         for (int i=0 ; i<lines.size() ; i++)
01206         {
01207             Impala::Util::StringParser p(lines[i]);
01208             String src = p.GetString(' ');
01209             String dst = p.GetString(' ', false);
01210             std::cout << src << " => " << dst << std::endl;
01211             conceptMapping.Add(src, dst);
01212         }
01213     }
01214 
01215     Core::Table::QuidTable* qTable = simSet->GetQuidTable();
01216     int qClass = QuidClass(qTable->Get1(0));
01217     Real64 map = 0;
01218     Real64 mapJ = 0;
01219     Real64 map2000 = 0;
01220     Real64 map2000J = 0;
01221     bool do2000 = (simSet->TableSize() > 2000);
01222     int nrMap = 0;
01223     std::cout << simLoc << ", size = " << simSet->TableSize() << std::endl;
01224 
01225     AnnotationTable** allAnnotations = new AnnotationTable*[simSet->NrTables()];
01226 
01227     for (int c=0 ; c<simSet->NrTables() ; c++)
01228     {
01229         String concept = simSet->GetName(c);
01230         String mappedConcept;
01231         if (!mappingName.empty())
01232         {
01233             if (conceptMapping.Get(concept, mappedConcept))
01234             {
01235                 concept = mappedConcept;
01236             }
01237             else
01238             {
01239                 continue;
01240             }
01241         }
01242         AnnotationTableLocator annoLoc(dataSet->GetLocator(), qClass,
01243                                        conceptSet, concept);
01244         if (!AnnotationTableRepository().Exists(annoLoc))
01245         {
01246             ILOG_ERROR("No groundtruth for " << concept);
01247             continue;
01248         }
01249         allAnnotations[c] = AnnotationTableRepository().Get(annoLoc);
01250         allAnnotations[c]->UpdateQuidMap();
01251     }
01252 
01253     std::cout << "\nConfusion Matrix Average Precision" << std::endl;
01254     for (int c=0 ; c<simSet->NrTables() ; c++)
01255     {
01256         Real64* confEntry = AveragePrecisionConfusion(simSet->GetRankTable(c),
01257                                                       allAnnotations,
01258                                                       c, simSet->NrTables(), -1);
01259 
01260         String concept = simSet->GetName(c);
01261         std::cout << MakeString(concept, 25) << "\t";
01262 
01263         Real64 total = 0;
01264         for (int i=0; i<simSet->NrTables() ; i++)
01265         {
01266             total += confEntry[i];
01267             std::cout << confEntry[i] << "\t";
01268         }
01269 
01270         std::cout << total << std::endl;
01271     }
01272  
01273     std::cout << "\nConfusion Matrix Average Precision Judged" << std::endl;
01274     for (int c=0 ; c<simSet->NrTables() ; c++)
01275     {
01276         Real64* confEntry =
01277             AveragePrecisionJudgedConfusion(simSet->GetRankTable(c),
01278                                             allAnnotations,
01279                                             c, simSet->NrTables(), -1);
01280 
01281         String concept = simSet->GetName(c);
01282         std::cout << MakeString(concept, 25) << "\t ";
01283 
01284         Real64 total = 0;
01285         for (int i=0; i<simSet->NrTables() ; i++)
01286         {
01287             total += confEntry[i];
01288             std::cout << confEntry[i] << "\t ";
01289         }
01290 
01291         std::cout << total << std::endl;
01292     }
01293 
01294     for (int i=0; i < simSet->NrTables() ; i++)
01295         delete allAnnotations[i];
01296     delete allAnnotations;
01297     delete simSet;
01298     delete dataSet;
01299 }
01300 
01301 void
01302 DoScoreConcepts()
01303 {
01304     typedef Core::Database::RawDataSet RawDataSet;
01305     typedef Core::Table::SimilarityTableSet SimilarityTableSet;
01306     typedef Core::Table::AnnotationTable AnnotationTable;
01307     using Impala::Core::Table::AveragePrecision;
01308 
01309     ILOG_VAR(Impala.Application.Util.DoScoreConcept);
01310     CmdOptions& options = CmdOptions::GetInstance();
01311     if (options.GetNrArg() < 5)
01312     {
01313         ILOG_ERROR("Need more parameters");
01314         return;
01315     }
01316     String dataSetName = options.GetArg(1);
01317     String conceptSet = options.GetArg(2);
01318     String model = options.GetArg(3);
01319     String feature = options.GetArg(4);
01320     String annotations = conceptSet;
01321     if (options.GetNrArg() > 5)
01322         annotations = options.GetArg(5);
01323     String mappingName;
01324     if (options.GetNrArg() > 6)
01325         mappingName = options.GetArg(6);
01326 
01327     RawDataSet* dataSet = Core::Database::MakeRawDataSet(dataSetName);
01328     SimilarityTableSetLocator simLoc(dataSet->GetLocator(), true, "",
01329                                      conceptSet, model, feature, "");
01330     SimilarityTableSet* simSet = SimilarityTableSetRepository().Get(simLoc);
01331     Impala::Util::SimpleMap<String, String> conceptMapping;
01332     if (!mappingName.empty())
01333     {
01334         std::vector<String> lines;
01335         Persistency::File file = RepositoryGetFile(mappingName, false, false);
01336         file.ReadStrings(lines);
01337         for (int i=0 ; i<lines.size() ; i++)
01338         {
01339             Impala::Util::StringParser p(lines[i]);
01340             String src = p.GetString(' ');
01341             String dst = p.GetString(' ', false);
01342             std::cout << src << " => " << dst << std::endl;
01343             conceptMapping.Add(src, dst);
01344         }
01345     }
01346 
01347     Core::Table::QuidTable* qTable = simSet->GetQuidTable();
01348     int qClass = QuidClass(qTable->Get1(0));
01349     Real64 map = 0;
01350     Real64 mapJ = 0;
01351     Real64 map2000 = 0;
01352     Real64 map2000J = 0;
01353     bool do2000 = (simSet->TableSize() > 2000);
01354     int nrMap = 0;
01355     //std::cout << namesPath << ", size = " << simSet->TableSize() << std::endl;
01356     std::cout << simLoc << ", size = " << simSet->TableSize() << std::endl;
01357     for (int c=0 ; c<simSet->NrTables() ; c++)
01358     {
01359         String concept = simSet->GetName(c);
01360         String mappedConcept;
01361         if (!mappingName.empty())
01362         {
01363             if (conceptMapping.Get(concept, mappedConcept))
01364             {
01365                 concept = mappedConcept;
01366             }
01367             else
01368             {
01369                 continue;
01370             }
01371         }
01372         AnnotationTableLocator annoLoc(dataSet->GetLocator(), qClass,
01373                                        annotations, concept);
01374         if (!AnnotationTableRepository().Exists(annoLoc))
01375         {
01376             ILOG_INFO("No groundtruth for " << concept);
01377             continue;
01378         }
01379         AnnotationTable* truth = AnnotationTableRepository().Get(annoLoc);
01380         truth->UpdateQuidMap();
01381         Real64 ap = AveragePrecision(simSet->GetRankTable(c), truth, -1);
01382         Real64 apJ = AveragePrecisionJudged(simSet->GetRankTable(c), truth,
01383                                             false, -1);
01384         map += ap;
01385         mapJ += apJ;
01386         std::cout << MakeString(concept, 30)
01387                   << " " << ap << "\t j: " << apJ;
01388 
01389         if (do2000)
01390         {
01391             Real64 ap2000 = AveragePrecision(simSet->GetRankTable(c), truth,
01392                                              2000);
01393             Real64 ap2000J = AveragePrecisionJudged(simSet->GetRankTable(c),
01394                                                     truth, false, 2000);
01395             map2000 += ap2000;
01396             map2000J += ap2000J;
01397             std::cout << "\t " << ap2000 << "\t j: " << ap2000J;
01398         }
01399 
01400         std::cout << std::endl;
01401         nrMap++;
01402         delete truth;
01403     }
01404     std::cout << "map=" << map/nrMap << ", mapJudged=" << mapJ/nrMap;
01405     if (do2000)
01406         std::cout << " map2000=" << map2000/nrMap << ", map2000Judged="
01407                   << map2000J/nrMap;
01408     std::cout << std::endl << std::endl;
01409     delete simSet;
01410     delete dataSet;
01411 }
01412 
01413 void
01414 DoScoreConceptsVideo()
01415 {
01416     typedef Core::Database::RawDataSet RawDataSet;
01417     typedef Core::Table::SimilarityTableSet SimilarityTableSet;
01418     typedef Core::Table::SimilarityTableSet::RankTableType RankTableType;
01419     typedef Core::Table::AnnotationTable AnnotationTable;
01420     using Impala::Core::Table::AveragePrecision;
01421 
01422     ILOG_VAR(Impala.Application.Util.DoScoreConceptVideo);
01423     CmdOptions& options = CmdOptions::GetInstance();
01424     if (options.GetNrArg() < 5)
01425     {
01426         ILOG_ERROR("Need more parameters");
01427         return;
01428     }
01429     String dataSetName = options.GetArg(1);
01430     String conceptSet = options.GetArg(2);
01431     String model = options.GetArg(3);
01432     String feature = options.GetArg(4);
01433     int vidStart = atol(options.GetArg(5));
01434     int vidEnd = atol(options.GetArg(6));
01435 
01436     RawDataSet* dataSet = Core::Database::MakeRawDataSet(dataSetName);
01437     SimilarityTableSetLocator simLoc(dataSet->GetLocator(), true, "",
01438                                      conceptSet, model, feature, "");
01439     SimilarityTableSet* simSet = SimilarityTableSetRepository().Get(simLoc);
01440 
01441     Core::Table::QuidTable* qTable = simSet->GetQuidTable();
01442     int qClass = QuidClass(qTable->Get1(0));
01443     Real64 mapJ = 0;
01444     int nrMap = 0;
01445     std::cout << simLoc << ", size = " << simSet->TableSize() << std::endl;
01446     std::vector<int> videos;
01447     std::cout << "videos: ";
01448     for (int i=vidStart ; i<vidEnd ; i++)
01449     {
01450         videos.push_back(i);
01451         std::cout << i << " ";
01452     }
01453     std::cout << std::endl;
01454     for (int c=0 ; c<simSet->NrTables() ; c++)
01455     {
01456         String concept = simSet->GetName(c);
01457         AnnotationTableLocator annoLoc(dataSet->GetLocator(), qClass,
01458                                        conceptSet, concept);
01459         if (!AnnotationTableRepository().Exists(annoLoc))
01460         {
01461             ILOG_ERROR("No groundtruth for " << concept);
01462             continue;
01463         }
01464         AnnotationTable* truth = AnnotationTableRepository().Get(annoLoc);
01465         truth->UpdateQuidMap();
01466         RankTableType* rank = simSet->GetRankTable(c);
01467         if (videos.size() > 0)
01468         {
01469             Core::Table::CriterionQuidObjectInSet<RankTableType> crit(videos);
01470             Core::Table::Select(rank, rank, crit, true);
01471         }
01472         Real64 apJ = AveragePrecisionJudged(rank, truth, false, -1, true);
01473         mapJ += apJ;
01474         std::cout << MakeString(concept, 25) << " " << apJ << std::endl;
01475         nrMap++;
01476         delete truth;
01477     }
01478     std::cout << "mapJudged=" << mapJ/nrMap << std::endl;
01479     std::cout << std::endl;
01480     delete simSet;
01481     delete dataSet;
01482 }
01483 
01484 void
01485 DoCombineConcepts()
01486 {
01487     typedef Core::VideoSet::VideoSet VideoSet;
01488     typedef Core::Table::SimilarityTableSet SimilarityTableSet;
01489     typedef Core::Table::SimilarityTableSet::SimTableType SimTableType;
01490 
01491     ILOG_VAR(Impala.Application.Util.DoCombineConcepts);
01492     CmdOptions& options = CmdOptions::GetInstance();
01493     if (options.GetNrArg() < 7)
01494     {
01495         ILOG_ERROR("Need more parameters");
01496         return;
01497     }
01498     String vidSetName = options.GetArg(1);
01499     String conceptSet = options.GetArg(2);
01500     String model = options.GetArg(3);
01501     String resultName = options.GetArg(4);
01502     String combi = options.GetArg(5);
01503     if ((combi != "mul") && (combi != "avg"))
01504     {
01505         ILOG_ERROR("Unknown combination method : " << combi);
01506         return;
01507     }
01508     std::vector<String> featureDefs;
01509     for (int i=6 ; i<options.GetNrArg() ; i++)
01510         featureDefs.push_back(options.GetArg(i));
01511 
01512     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(vidSetName);
01513 
01514     SimilarityTableSetLocator loc(vidSet->GetLocator(), true, "", conceptSet,
01515                                   model, featureDefs[0], "");
01516     SimilarityTableSet* combinedSet = SimilarityTableSetRepository().Get(loc);
01517     for (int i=1 ; i<featureDefs.size() ; i++)
01518     {
01519         loc.SetFeature(featureDefs[i]);
01520         SimilarityTableSet* simSet = SimilarityTableSetRepository().Get(loc);
01521         for (int t=0 ; t<simSet->NrTables() ; t++)
01522         {
01523             if (combi == "mul")
01524                 MulAssign(combinedSet->GetSimTable(t)->GetColumn1(),
01525                           simSet->GetSimTable(t)->GetColumn1(), 0, -1);
01526             else
01527                 AddAssign(combinedSet->GetSimTable(t)->GetColumn1(),
01528                           simSet->GetSimTable(t)->GetColumn1(), 0, -1);
01529         }
01530         delete simSet;
01531     }
01532     if (combi == "avg")
01533         for (int t=0 ; t<combinedSet->NrTables() ; t++)
01534             DivAssign(combinedSet->GetSimTable(t)->GetColumn1(),
01535                       featureDefs.size(), 0, -1);
01536     combinedSet->ComputeRanks(true);
01537     loc.SetFeature(resultName);
01538     SimilarityTableSetRepository().Add(loc, combinedSet);
01539     delete combinedSet;
01540     delete vidSet;
01541 }
01542 
01543 void
01544 DoMapConcepts2Shots()
01545 {
01546     typedef Core::VideoSet::VideoSet VideoSet;
01547     typedef Core::VideoSet::Segmentation Segmentation;
01548     typedef Core::Table::SimilarityTableSet SimilarityTableSet;
01549     typedef Core::Table::SimilarityTableSet::SimTableType SimTableType;
01550 
01551     ILOG_VAR(Impala.Application.Util.DoMapConcepts2Shots);
01552     CmdOptions& options = CmdOptions::GetInstance();
01553     if (options.GetNrArg() < 5)
01554     {
01555         ILOG_ERROR("Need more parameters");
01556         return;
01557     }
01558     String vidSetName = options.GetArg(1);
01559     String conceptSet = options.GetArg(2);
01560     String model = options.GetArg(3);
01561     String feature = options.GetArg(4);
01562     String aggregationMode = "max";
01563     if (options.GetNrArg() >= 6)
01564     {
01565         aggregationMode = options.GetArg(5);
01566     }
01567 
01568     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(vidSetName);
01569     SegmentationLocator sLoc(vidSetName, "segmentation", options);
01570     Segmentation* segmentation = SegmentationRepository().Get(sLoc, vidSet);
01571     SimilarityTableSetLocator simLoc(vidSet->GetLocator(), true, "",
01572                                      conceptSet, model, feature, "");
01573     SimilarityTableSet* simSet = SimilarityTableSetRepository().Get(simLoc);
01574  
01575     Core::Table::QuidTable* frameQuids = simSet->GetQuidTable();
01576     int nrShots = segmentation->GetNrShots();
01577     SimilarityTableSet* res = new SimilarityTableSet(simSet->GetNames(), nrShots);
01578     Core::Table::QuidTable* shotQuids = res->GetQuidTable();
01579     for (int t=0 ; t<simSet->NrTables() ; t++)
01580     {
01581         SimTableType* frameSimTable = simSet->GetSimTable(t);
01582         SimTableType* shotSimTable = res->GetSimTable(t);
01583         int lastShot = -1;
01584         Real64 lastSim = 0;
01585         int lastCount = 0;
01586         for (int i=0 ; i<frameSimTable->Size() ; i++)
01587         {
01588             Quid frameQ = frameQuids->Get1(i);
01589             Real64 frameSim = frameSimTable->Get1(i);
01590             int shot = segmentation->GetShotId(frameQ);
01591             if (shot != lastShot)
01592             {
01593                 if (lastShot != -1)
01594                 {
01595                     Quid check = segmentation->GetQuidShot(lastShot);
01596                     int rank = shotSimTable->Size();
01597                     if (shotQuids->Size() <= rank)
01598                         shotQuids->Add(check);
01599                     if (check != shotQuids->Get1(rank))
01600                     {
01601                         ILOG_ERROR("Quids do not match");
01602                     }
01603                     if (aggregationMode == "avg")
01604                     {
01605                         lastSim = lastSim / lastCount;
01606                     }
01607                     shotSimTable->Add(lastSim);
01608                 }
01609                 lastSim = frameSim;
01610                 lastShot = shot;
01611                 lastCount = 1;
01612             }
01613             else
01614             {
01615                 lastCount++;
01616                 if (aggregationMode == "max")
01617                 {
01618                     if (frameSim > lastSim)
01619                         lastSim = frameSim;
01620                 }
01621                 else if (aggregationMode == "avg")
01622                 {
01623                     lastSim += frameSim;
01624                 }
01625             }
01626         }
01627         if (lastShot != -1)
01628         {
01629             Quid check = segmentation->GetQuidShot(lastShot);
01630             int rank = shotSimTable->Size();
01631             if (shotQuids->Size() <= rank)
01632                 shotQuids->Add(check);
01633             if (check != shotQuids->Get1(rank))
01634             {
01635                 ILOG_ERROR("Quids do not match");
01636             }
01637             if (aggregationMode == "avg")
01638             {
01639                 lastSim = lastSim / lastCount;
01640             }
01641             shotSimTable->Add(lastSim);
01642         }
01643     }
01644 
01645     res->ComputeRanks(true);
01646     simLoc.SetFeature("shot" + feature);
01647     SimilarityTableSetRepository().Add(simLoc, res);
01648     delete res;
01649     delete simSet;
01650     delete vidSet;
01651 }
01652 
01653 // Note : this "hack" requires *not* doing LoadRanks and SaveRanks in
01654 // SimilarityTableSetRepositoryInFile
01655 void
01656 DoConcepts2AllFrames()
01657 {
01658     typedef Core::VideoSet::VideoSet VideoSet;
01659     typedef Core::VideoSet::Segmentation Segmentation;
01660     typedef Core::Table::SimilarityTableSet SimilarityTableSet;
01661     typedef Core::Table::SimilarityTableSet::SimTableType SimTableType;
01662 
01663     ILOG_VAR(Impala.Application.Util.DoConceptsExtra2AllFrames);
01664     CmdOptions& options = CmdOptions::GetInstance();
01665     if (options.GetNrArg() < 5)
01666     {
01667         ILOG_ERROR("Need more parameters");
01668         return;
01669     }
01670     String vidSetName = options.GetArg(1);
01671     String conceptSet = options.GetArg(2);
01672     String model = options.GetArg(3);
01673     String feature = options.GetArg(4);
01674 
01675     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(vidSetName);
01676     SegmentationLocator sLoc(vidSetName, "segmentation", options);
01677     Segmentation* segmentation = SegmentationRepository().Get(sLoc, vidSet);
01678     SimilarityTableSetLocator srcLoc(vidSet->GetLocator(), true, "",
01679                                      conceptSet, model, feature, "");
01680     SimilarityTableSet* srcSet = SimilarityTableSetRepository().Get(srcLoc);
01681  
01682     Core::Table::QuidTable* srcQuids = srcSet->GetQuidTable();
01683     srcQuids->UpdateGroups();
01684     for (int v=0 ; v<vidSet->NrFiles() ; v++)
01685     {
01686         int nrFrames = segmentation->GetNrFramesVideo(v);
01687         ILOG_INFO("Video " << v << " has " << nrFrames << " frames and " <<
01688                   segmentation->GetNrShotsVideo(v) << " shots");
01689         SimilarityTableSet* dstSet = new SimilarityTableSet(srcSet->GetNames(),
01690                                                             nrFrames);
01691         Core::Table::QuidTable* dstQuids = dstSet->GetQuidTable();
01692         for (int t=0 ; t<srcSet->NrTables() ; t++)
01693         {
01694             SimTableType* srcSimTable = srcSet->GetSimTable(t);
01695             SimTableType* dstSimTable = dstSet->GetSimTable(t);
01696             for (int f=0 ; f<nrFrames ; f++)
01697             {
01698                 Quid quid = vidSet->GetQuidFrame(v, f);
01699                 if (t == 0)
01700                     dstQuids->Add(quid);
01701                 Real64 sim = 0;
01702                 int obj = QuidObject(quid);
01703                 int start = srcQuids->GetFirstQuidObject(obj);
01704                 int nr = srcQuids->GetNrQuidsObject(obj);
01705                 int idx = srcQuids->Size();
01706                 for (int i=start ; i<start+nr ; i++)
01707                 {
01708                     if (srcQuids->Get1(i) == quid)
01709                     {
01710                         idx = i;
01711                         break;
01712                     }
01713                 }
01714                 if (idx != srcQuids->Size())
01715                 {
01716                     sim = srcSimTable->Get1(idx);
01717                 }
01718                 dstSimTable->Add(sim);
01719             }
01720         }
01721 
01722         // Not used right now, so skipping...
01723         //dstSet->ComputeRanks(true);
01724         SimilarityTableSetLocator dstLoc(vidSet->GetLocator(), false, "Frames",
01725                                          conceptSet, model, feature,
01726                                          vidSet->GetContainer(v));
01727         SimilarityTableSetRepository().Add(dstLoc, dstSet);
01728         delete dstSet;
01729     }
01730     delete srcSet;
01731     delete vidSet;
01732 }
01733 
01734 void
01735 DoConcepts2TrecResult()
01736 {
01737     typedef Core::VideoSet::VideoSet VideoSet;
01738     typedef Core::VideoSet::Segmentation Segmentation;
01739     typedef Core::VideoSet::Keyframes Keyframes;
01740     typedef Core::Table::SimilarityTableSet SimilarityTableSet;
01741     typedef Core::Table::SimilarityTableSet::RankTableType RankTableType;
01742 
01743     ILOG_VAR(Impala.Application.Util.DoConcepts2TrecResult);
01744     CmdOptions& options = CmdOptions::GetInstance();
01745     if (options.GetNrArg() < 5)
01746     {
01747         ILOG_ERROR("Need more parameters");
01748         return;
01749     }
01750     String vidSetName = options.GetArg(1);
01751     String conceptSet = options.GetArg(2);
01752     String model = options.GetArg(3);
01753     String feature = options.GetArg(4);
01754 
01755     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(vidSetName);
01756     SegmentationLocator sLoc(vidSetName, "segmentation", options);
01757     Segmentation* segmentation = SegmentationRepository().Get(sLoc, vidSet);
01758     KeyframesLocator kLoc(vidSetName, "keyframes", options);
01759     Keyframes* keyframes = KeyframesRepository().Get(kLoc, vidSet);
01760     SimilarityTableSetLocator simLoc(vidSet->GetLocator(), true, "",
01761                                      conceptSet, model, feature, "");
01762     SimilarityTableSet* simSet = SimilarityTableSetRepository().Get(simLoc);
01763 
01764     for (int c=0 ; c<simSet->NrTables() ; c++)
01765     {
01766         String concept = simSet->GetName(c);
01767         ILOG_INFO("Doing concept " << concept);
01768         std::vector<String> res;
01769         RankTableType* rTable = simSet->GetRankTable(c);
01770         for (int i=0 ; i<rTable->Size() ; i++)
01771         {
01772             Quid quid = rTable->Get1(i);
01773             int shot = -1;
01774             if (QuidClass(quid) == QUID_CLASS_FRAME) // assume it is a keyframe
01775             {
01776                 int keyfr = keyframes->GetFrameId(quid);
01777                 if (keyfr != -1)
01778                     shot = keyframes->GetShotId(keyfr);
01779             }
01780             if (QuidClass(quid) == QUID_CLASS_SHOT)
01781             {
01782                 shot = segmentation->GetShotId(quid);
01783             }
01784             if (shot == -1)
01785             {
01786                 ILOG_ERROR("Unable to find shot for " << QuidObj(quid));
01787                 continue;
01788             }
01789             String name = segmentation->GetName(shot);
01790             if (std::find(res.begin(), res.end(), name) == res.end())
01791                 res.push_back(name);
01792         }
01793         String fName = concept + ".txt";
01794         Persistency::File file = RepositoryGetFile(fName, true, false);
01795         file.WriteStrings(res.begin(), res.end());
01796     }
01797     delete simSet;
01798     delete vidSet;
01799 }
01800 
01801 void
01802 DoJudgeConcepts()
01803 {
01804     typedef Core::VideoSet::VideoSet VideoSet;
01805     typedef Core::VideoSet::Segmentation Segmentation;
01806     typedef Core::VideoSet::Keyframes Keyframes;
01807     typedef Core::Table::SimilarityTableSet SimilarityTableSet;
01808     typedef Core::Table::SimilarityTableSet::RankTableType RankTableType;
01809 
01810     ILOG_VAR(Impala.Application.Util.DoJudgeConcepts);
01811     CmdOptions& options = CmdOptions::GetInstance();
01812     if (options.GetNrArg() < 7)
01813     {
01814         ILOG_ERROR("Need at least 7 arguments");
01815         return;
01816     }
01817     String vidSetName = options.GetArg(1);
01818     String conceptSet = options.GetArg(2);
01819     String model = options.GetArg(3);
01820     String feature = options.GetArg(4);
01821     String mappingName = options.GetArg(5);
01822     String judgeFile = options.GetArg(6);
01823 
01824     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(vidSetName);
01825     SegmentationLocator sLoc(vidSetName, "segmentation", options);
01826     Segmentation* segmentation = SegmentationRepository().Get(sLoc, vidSet);
01827     KeyframesLocator kLoc(vidSetName, "keyframes", options);
01828     Keyframes* keyframes = KeyframesRepository().Get(kLoc, vidSet);
01829     SimilarityTableSetLocator simLoc(vidSet->GetLocator(), true, "",
01830                                      conceptSet, model, feature, "");
01831     SimilarityTableSet* simSet = SimilarityTableSetRepository().Get(simLoc);
01832 
01833     std::vector<String> lines;
01834     Impala::Util::SimpleMap<String, String> conceptMapping;
01835     Persistency::File file = RepositoryGetFile(mappingName, false, false);
01836     file.ReadStrings(lines);
01837     for (int i=0 ; i<lines.size() ; i++)
01838     {
01839         Impala::Util::StringParser p(lines[i]);
01840         String topic = p.GetString(' ');
01841         String concept = p.GetString(' ', false);
01842         std::cout << topic << " = " << concept << std::endl;
01843         conceptMapping.Add(concept, topic);
01844     }
01845     Core::Trec::SearchJudge judge(judgeFile);
01846     judge.Stats();
01847 
01848     Real64 miap = 0;
01849     int nrMiap = 0;
01850     std::cout << simLoc << ", size = " << simSet->TableSize() << std::endl;
01851     for (int c=0 ; c<simSet->NrTables() ; c++)
01852     {
01853         String concept = simSet->GetName(c);
01854         ILOG_DEBUG("Doing concept " << concept);
01855         std::vector<String> res;
01856         RankTableType* rTable = simSet->GetRankTable(c);
01857         for (int i=0 ; i<rTable->Size() ; i++)
01858         {
01859             Quid quid = rTable->Get1(i);
01860             int shot = -1;
01861             if (QuidClass(quid) == QUID_CLASS_FRAME) // assume it is a keyframe
01862             {
01863                 int keyfr = keyframes->GetFrameId(quid);
01864                 if (keyfr != -1)
01865                     shot = keyframes->GetShotId(keyfr);
01866             }
01867             if (QuidClass(quid) == QUID_CLASS_SHOT)
01868             {
01869                 shot = segmentation->GetShotId(quid);
01870             }
01871             if (shot == -1)
01872             {
01873                 ILOG_ERROR("Unable to find shot for " << QuidObj(quid));
01874                 continue;
01875             }
01876             String name = segmentation->GetName(shot);
01877             if (std::find(res.begin(), res.end(), name) == res.end())
01878                 res.push_back(name);
01879             if (res.size() >= 2000)
01880                 break;
01881         }
01882         String topic;
01883         if (!conceptMapping.Get(concept, topic))
01884         {
01885             ILOG_ERROR("No topic for " << concept);
01886             continue;
01887         }
01888         if (!judge.HasTopic(topic))
01889         {
01890             ILOG_INFO("No judge for " << topic << " = " << concept);
01891             continue;
01892         }
01893         Real64 infAP = judge.Judge(topic, res);
01894         std::cout << concept << " " << infAP << std::endl;
01895         miap += infAP;
01896         nrMiap++;
01897     }
01898     std::cout << "miap = " << miap / nrMiap << std::endl << std::endl;
01899     delete simSet;
01900     delete vidSet;
01901 }
01902 
01903 void
01904 DoPing()
01905 {
01906     typedef Impala::Util::Channel Channel;
01907     typedef Impala::Util::ChannelPool ChannelPool;
01908 
01909     ILOG_VAR(Impala.Application.Util.DoPing);
01910     CmdOptions& options = CmdOptions::GetInstance();
01911     if (options.GetNrArg() < 2)
01912     {
01913         ILOG_ERROR("Need at least 2 arguments");
01914         return;
01915     }
01916     String hostName = options.GetArg(1);
01917     String passFile = options.GetString("passwordFile");
01918     Channel* channel = ChannelPool::Instance().Get(hostName, passFile);;
01919     if (! (channel && channel->Valid()))
01920     {
01921         ILOG_ERROR("Could not connect to server");
01922         return;
01923     }
01924     char* buf = channel->Buffer();
01925     sprintf(buf, "ping\0");
01926     channel->SendRequest(strlen(buf)+1);
01927     if (channel->LastSendHadError())
01928     {
01929         ILOG_ERROR("Failed to send message");
01930         return;
01931     }
01932     ILOG_INFO("Server OK");
01933 }
01934 
01935 int
01936 mainUtil(int argc, char* argv[])
01937 {
01938     CmdOptions& options = CmdOptions::GetInstance();
01939     options.Initialise(true, false, true);
01940     options.AddOption(0, "start", "nr", "0");
01941     options.AddOption(0, "end", "nr", "-1");
01942     String usageStr = "cmd = \n\n";
01943     usageStr += "  createimageset imageset.txt [imagefile]+\n";
01944     usageStr += "  trectopicdata ...\n";
01945     usageStr += "  trecset year {devel|test} collection.xml\n";
01946     usageStr += "  trecjudge qrel.txt shotresult.txt topiccode\n";
01947     usageStr += "  dumpkeyframes videoset.txt\n";
01948     usageStr += "  dumpsegmentation videoset.txt\n";
01949     usageStr += "  exportshotmpeg7 videoset.txt {25|30000} generateShotNames withKeyframes\n";
01950     usageStr += "  exportannompeg7 videoset.txt {25|30000} concepts.txt\n";
01951     usageStr += "  importannotruth videoset.txt concepts.txt maskfile\n";
01952     usageStr += "  exportannotruth videoset.txt concepts.txt\n";
01953     usageStr += "  importannotations videoset.txt {25|30000} concepts.txt\n";
01954     usageStr += "  exportconceptmpeg7 videoset.txt {25|30000} names.txt\n";
01955     usageStr += "  dumpparameter videoset.txt concepts.txt model featuredef\n";
01956     usageStr += "  apconfusion videoset.txt concepts.txt model feature [mapping]\n";
01957     usageStr += "  scoreconcepts videoset.txt concepts.txt model feature [annotations=concepts.txt] [mapping]\n";
01958     usageStr += "  scoreconceptsvideo videoset.txt annoset.txt names.txt vidStart vidEnd\n";
01959     usageStr += "  combineconcepts videoset.txt concepts.txt model resultName {avg|mil} {feature}+\n";
01960     usageStr += "  mapconcepts2shots videoset.txt conceptSet model feature [mode=max]\n";
01961     usageStr += "  conceptsextra2allframes videoset.txt conceptSet model feature\n";
01962     usageStr += "  concepts2trecresult videoset.txt concepts.txt model feature\n";
01963     usageStr += "  judgeconcepts videoset.txt concepts.txt model feature conceptMapping.txt qrels\n";
01964     usageStr += "  ping host:port\n";
01965     usageStr += "  checknativetypesize\n";
01966     if (! options.ParseArgs(argc, argv, usageStr, 1))
01967         return 1;
01968 
01969     ILOG_VAR(Impala.Application.Util.mainUtil);
01970 
01971     String cmd = options.GetArg(0);
01972     ILOG_DEBUG("cmd = " << cmd);
01973     if (cmd == "createimageset")
01974         DoCreateImageSet();
01975     else if (cmd == "trectopicdata")
01976         DoTrecTopicData();
01977     else if (cmd == "trecset")
01978         DoTrecSet();
01979     else if (cmd == "trecjudge")
01980         DoTrecJudge();
01981     else if (cmd == "trecdropped")
01982         DoTrecDropped();
01983     else if (cmd == "trecmissing")
01984         DoTrecMissing();
01985     else if (cmd == "createcaanno")
01986         DoCreateCaAnno();
01987     else if (cmd == "exportshotmpeg7")
01988         DoExportShotMpeg7();
01989     else if (cmd == "exportannompeg7")
01990         DoExportAnnoMpeg7();
01991     else if (cmd == "importannotruth")
01992         DoImportAnnoTruth();
01993     else if (cmd == "exportannotruth")
01994         DoExportAnnoTruth();
01995     else if (cmd == "importannotations")
01996         DoImportAnnotations();
01997     else if (cmd == "importannotationsork")
01998         DoImportAnnotationsOrk();
01999     else if (cmd == "importannotationsactive")
02000         DoImportAnnotationsActive();
02001     else if (cmd == "dumpparameter")
02002         DoDumpParameter();
02003     else if (cmd == "apconfusion")
02004         DoAPConfusion();
02005     else if (cmd == "scoreconcepts")
02006         DoScoreConcepts();
02007     else if (cmd == "scoreconceptsvideo")
02008         DoScoreConceptsVideo();
02009     else if (cmd == "combineconcepts")
02010         DoCombineConcepts();
02011     else if (cmd == "mapconcepts2shots")
02012         DoMapConcepts2Shots();
02013     else if (cmd == "concepts2allframes")
02014         DoConcepts2AllFrames();
02015     else if (cmd == "concepts2trecresult")
02016         DoConcepts2TrecResult();
02017     else if (cmd == "judgeconcepts")
02018         DoJudgeConcepts();
02019     else if (cmd == "ping")
02020         DoPing();
02021     else if (cmd == "checknativetypesize")
02022         Impala::Core::Array::Element::CheckNativeTypeSize();
02023     else ILOG_ERROR("Unknown cmd : " << cmd);
02024 
02025     return ILOG_ERROR_COUNT;
02026 }
02027 
02028 } // namespace Util
02029 } // namespace Application
02030 } // namespace Impala
02031 
02032 int
02033 main(int argc, char* argv[])
02034 {
02035     return Impala::Application::Util::mainUtil(argc, argv);
02036 }

Generated on Thu Jan 13 09:03:45 2011 for ImpalaSrc by  doxygen 1.5.1