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

mainMonetTest.cpp

Go to the documentation of this file.
00001 #include "Util/BinHex.h"
00002 #include "Core/Array/ImageArchiveMapi.h"
00003 #include "Core/Array/ImageArchiveDiff.h"
00004 #include "Core/Table/KeywordListDiff.h"
00005 #include "Core/VideoSet/MakeVideoSet.h"
00006 #include "Core/VideoSet/Segmentation.h"
00007 #include "Core/VideoSet/Keyframes.h"
00008 #include "Core/Table/SimilarityTableSet.h"
00009 #include "Core/Table/AnnotationTableSet.h"
00010 #include "Core/Table/SimTableTypeDiff.h"
00011 #include "Core/Feature/FeatureTable.h"
00012 #include "Core/Table/Read.h"
00013 
00014 
00015 // since we are not using libraries:
00016 #include "Link/ImpalaLib.cpp"
00017 
00018 namespace Impala
00019 {
00020 namespace Samples
00021 {
00022 namespace MonetTest
00023 {
00024 
00025 using Link::Monet::Connection;
00026 using namespace Core::Array;
00027 using namespace Core::Table;
00028 using namespace Core::Feature;
00029 using namespace Core::VideoSet;
00030 using namespace Persistency;
00031 
00032 
00035 
00036 VideoSet*
00037 MakeVideoSetMapi(String vidSetName, Connection* conn)
00038 {
00039     ILOG_VAR(Impala.Samples.MonetTest.MakeVideoSetMapi);
00040 
00041     String vidSetBase = FileNameBase(vidSetName);
00042     String query =
00043         "select f.filename, f.quid \
00044          from   file f, video_set vs, video_sets vss \
00045          where  vss.set_name = '" + vidSetBase + "' and \
00046                 vs.video_sets_id = vss.video_sets_id and \
00047                 f.file_id = vs.file_id \
00048          order by f.quid;";
00049 
00050     MapiHdl hdl = conn->QueryPartStart(query);
00051     if (hdl == 0)
00052         return 0;
00053 
00054     std::vector<String> fName;
00055     if (!conn->QueryPartFetchString(hdl, 0, fName))
00056         return 0;
00057 
00058     Quid* quid = 0;
00059     int quidSize = 0;
00060     if (!conn->QueryPartFetchULL(hdl, 1, quid, quidSize))
00061         return 0;
00062 
00063     conn->QueryPartEnd(hdl);
00064 
00065     VideoSet* vidSet = new VideoSet(0, vidSetName, false);
00066     for (int i=0 ; i<fName.size() ; i++)
00067     {
00068         int fileId = QuidId(quid[i]);
00069         vidSet->AddFile(fName[i], fileId);
00070         if (vidSet->GetQuidVideo(i, true) != quid[i])
00071             ILOG_ERROR("Quid " << i << " doesn't match");
00072     }
00073     delete quid;
00074     return vidSet;
00075 }
00076 
00077 Segmentation*
00078 MakeSegmentationMapi(VideoSet* vidSet, Connection* conn)
00079 {
00080     ILOG_VAR(Impala.Samples.MonetTest.MakeSegmentationMapi);
00081 
00082     String vidSetBase = vidSet->GetSetNameBase();
00083     String query =
00084         "select f.quid, fr.fragment_start, fr.fragment_length, fr.fragment_name \
00085          from   fragment fr, file f, video_set vs, video_sets vss \
00086          where  vss.set_name = '" + vidSetBase + "' and \
00087                 vs.video_sets_id = vss.video_sets_id and \
00088                 f.file_id = vs.file_id and \
00089                 fr.media_id = f.media_id and \
00090                 fr.fragment_length > 1 and \
00091                 fr.keyframe = false \
00092          order by f.quid, fr.fragment_start;";
00093 
00094     MapiHdl hdl = conn->QueryPartStart(query);
00095     if (hdl == 0)
00096         return 0;
00097 
00098     Quid* quid = 0;
00099     int quidSize = 0;
00100     if (!conn->QueryPartFetchULL(hdl, 0, quid, quidSize))
00101         return 0;
00102 
00103     int* start = 0;
00104     int startSize = 0;
00105     if (!conn->QueryPartFetchInt(hdl, 1, start, startSize))
00106         return 0;
00107 
00108     int* length = 0;
00109     int lengthSize = 0;
00110     if (!conn->QueryPartFetchInt(hdl, 2, length, lengthSize))
00111         return 0;
00112 
00113     std::vector<String> name;
00114     if (!conn->QueryPartFetchString(hdl, 3, name))
00115         return 0;
00116 
00117     conn->QueryPartEnd(hdl);
00118 
00119     Segmentation* seg = new Segmentation(vidSet, "");
00120     for (int i=0 ; i<quidSize ; i++)
00121     {
00122         int id = QuidId(quid[i]);
00123         int end = start[i] + length[i] - 1;
00124         seg->Add(id, start[i], end, name[i]);
00125     }
00126     seg->UpdateGroups();
00127 
00128     delete quid;
00129     delete start;
00130     delete length;
00131     return seg;
00132 }
00133 
00134 Keyframes*
00135 MakeKeyframesMapi(VideoSet* vidSet, Connection* conn)
00136 {
00137     ILOG_VAR(Impala.Samples.MonetTest.MakeKeyframesMapi);
00138 
00139     String vidSetBase = vidSet->GetSetNameBase();
00140     String query =
00141         "select f.quid, fr.fragment_start, fr.fragment_name \
00142          from   fragment fr, file f, video_set vs, video_sets vss \
00143          where  vss.set_name = '" + vidSetBase + "' and \
00144                 vs.video_sets_id = vss.video_sets_id and \
00145                 f.file_id = vs.file_id and \
00146                 fr.media_id = f.media_id and \
00147                 fr.keyframe = true \
00148          order by f.quid, fr.fragment_start;";
00149 
00150     MapiHdl hdl = conn->QueryPartStart(query);
00151     if (hdl == 0)
00152         return 0;
00153 
00154     Quid* quid = 0;
00155     int quidSize = 0;
00156     if (!conn->QueryPartFetchULL(hdl, 0, quid, quidSize))
00157         return 0;
00158 
00159     int* start = 0;
00160     int startSize = 0;
00161     if (!conn->QueryPartFetchInt(hdl, 1, start, startSize))
00162         return 0;
00163 
00164     std::vector<String> name;
00165     if (!conn->QueryPartFetchString(hdl, 2, name))
00166         return 0;
00167 
00168     conn->QueryPartEnd(hdl);
00169 
00170     // Need segmentation for shotId
00171     Segmentation* seg = MakeSegmentationMapi(vidSet, conn);
00172     Keyframes* key = new Keyframes(vidSet, "");
00173     for (int i=0 ; i<quidSize ; i++)
00174     {
00175         int vidId = QuidId(quid[i]);
00176         int shotId = seg->GetShotId(vidId, start[i]);
00177         key->Add(vidId, shotId, start[i], name[i]);
00178     }
00179     key->UpdateGroups();
00180     delete seg;
00181 
00182     delete quid;
00183     delete start;
00184     return key;
00185 }
00186 
00187 KeywordList
00188 MakeKeywordListMapi(String vidSetName, String conceptSet, Connection* conn)
00189 {
00190     ILOG_VAR(Impala.Samples.MonetTest.MakeKeywordMapi);
00191 
00192     String vidSetBase = FileNameBase(vidSetName);
00193     String conceptSetBase = FileNameBase(conceptSet);
00194     String query =
00195         "select k.keyword_name \
00196          from   keyword k, keyword_set ks, keyword_sets kss, video_sets vss \
00197          where  vss.set_name = '" + vidSetBase + "' and \
00198                 kss.video_sets_id = vss.video_sets_id and \
00199                 kss.set_name = '" + conceptSetBase + "' and \
00200                 ks.keyword_sets_id = kss.keyword_sets_id and \
00201                 k.keyword_id = ks.keyword_id \
00202          order by k.keyword_id;";
00203 
00204     KeywordList concepts;
00205     MapiHdl hdl = conn->QueryPartStart(query);
00206     if (hdl == 0)
00207         return concepts;
00208 
00209     if (!conn->QueryPartFetchString(hdl, 0, concepts))
00210         return concepts;
00211 
00212     conn->QueryPartEnd(hdl);
00213 
00214     return concepts;
00215 }
00216 
00217 AnnotationTable*
00218 MakeAnnotationTableMapi(VideoSet* vidSet, String conceptSet, String keyword, 
00219                         int quidClass, Connection* conn)
00220 {
00221     ILOG_VAR(Impala.Samples.MonetTest.MakeAnnotationTableMapi);
00222 
00223     if (quidClass != QUID_CLASS_FRAME)
00224     {
00225         ILOG_ERROR("Can do Frame only");
00226         return 0;
00227     }
00228 
00229     String vidSetBase = vidSet->GetSetNameBase();
00230     String query =
00231         "select f.quid, fr.fragment_start, a.relevance \
00232          from   fragment fr, file f, video_set vs, video_sets vss, \
00233                 keyword_sets kss, keyword_set ks, keyword k, annotator a, \
00234                 annotation a \
00235          where  vss.set_name = '" + vidSetBase + "' and \
00236                 vs.video_sets_id = vss.video_sets_id and \
00237                 f.file_id = vs.file_id and \
00238                 fr.media_id = f.media_id and \
00239                 fr.fragment_length = 1 and \
00240                 fr.keyframe = false and \
00241                 kss.set_name = '" + FileNameBase(conceptSet) + "' and \
00242                 kss.video_sets_id = vss.video_sets_id and \
00243                 ks.keyword_sets_id = kss.keyword_sets_id and \
00244                 ks.keyword_id = k.keyword_id and \
00245                 k.keyword_name = '" + keyword + "' and \
00246                 a.annotator_name = 'fabchannel' and \
00247                 a.fragment_id = fr.fragment_id and \
00248                 a.keyword_id = k.keyword_id and \
00249                 a.approved = true \
00250          order by f.quid, fr.fragment_start;";
00251 
00252     MapiHdl hdl = conn->QueryPartStart(query);
00253     if (hdl == 0)
00254         return 0;
00255 
00256     Quid* quid = 0;
00257     int quidSize = 0;
00258     if (!conn->QueryPartFetchULL(hdl, 0, quid, quidSize))
00259         return 0;
00260 
00261     int* start = 0;
00262     int startSize = 0;
00263     if (!conn->QueryPartFetchInt(hdl, 1, start, startSize))
00264         return 0;
00265 
00266     double* rel = 0;
00267     int relSize;
00268     if (!conn->QueryPartFetchDouble(hdl, 2, rel, relSize))
00269         return 0;
00270 
00271     conn->QueryPartEnd(hdl);
00272 
00273     AnnotationTable* anno = new AnnotationTable(keyword, quidSize);
00274     for (int i=0 ; i<quidSize ; i++)
00275     {
00276         int vidId = QuidId(quid[i]);
00277         Quid q = vidSet->GetQuidFrame(vidId, start[i], false);
00278         if (rel[i] == 1.0)
00279             anno->AddPositive(q);
00280         else if (rel[i] == 0.0)
00281             anno->AddNegative(q);
00282         else
00283             anno->AddSkip(q);
00284     }
00285 
00286     delete quid;
00287     delete start;
00288     delete rel;
00289     return anno;
00290 }
00291 
00292 AnnotationTableSet*
00293 MakeAnnotationTableSetMapi(VideoSet* vidSet, String conceptSet, bool readTables,
00294                            int quidClass, Connection* conn)
00295 {
00296     ILOG_VAR(Impala.Samples.MonetTest.MakeAnnotationTableSetMapi);
00297 
00298     KeywordList keywords = MakeKeywordListMapi(vidSet->GetSetName(), conceptSet,
00299                                                conn);
00300     ILOG_INFO("Nr keywords = " << keywords.size());
00301 
00302     AnnotationTableSet* res = new AnnotationTableSet();
00303     int nrRead = 0;
00304     for (int i=0 ; i<keywords.size() ; i++)
00305     {
00306         ILOG_INFO("Doing table " << keywords[i]);
00307         AnnotationTable* table = 0;
00308         if (readTables)
00309         {
00310             table = MakeAnnotationTableMapi(vidSet, conceptSet, keywords[i],
00311                                             quidClass, conn);
00312             if (table != 0)
00313                 nrRead++;
00314         }
00315         if (table == 0)
00316             table = new AnnotationTable(keywords[i], 0);
00317         res->Add(table);
00318     }
00319     if (readTables && (nrRead == 0))
00320         ILOG_ERROR("No tables found");
00321     return res;
00322 }
00323 
00324 FeatureTable*
00325 MakeFeatureTableMapi(VideoSet* vidSet, int fileIdx, FeatureDefinition def,
00326                      Connection* conn)
00327 {
00328     ILOG_VAR(Impala.Samples.MonetTest.MakeFeatureTableMapi);
00329 
00330     Quid vidQuid = vidSet->GetQuidVideo(fileIdx, true);
00331     String query =
00332         "select fr.fragment_start, fv.vector \
00333          from feature_vector fv, fragment fr, file f, feature fe \
00334          where  f.quid = " + MakeString(vidQuid) + " and \
00335                 fr.media_id = f.media_id and \
00336                 fr.fragment_length = 1 and \
00337                 fr.keyframe = false and \
00338                 fe.feature_name = '" + def.AsString() + "' and \
00339                 fe.feature_id = fv.feature_id and \
00340                 fv.fragment_id = fr.fragment_id and \
00341                 fv.file_id = f.file_id \
00342          order by fr.fragment_start;";
00343 
00344     MapiHdl hdl = conn->QueryPartStart(query);
00345     if (hdl == 0)
00346         return 0;
00347 
00348     int* start = 0;
00349     int startSize = 0;
00350     if (!conn->QueryPartFetchInt(hdl, 0, start, startSize))
00351         return 0;
00352 
00353     std::vector<String> res;
00354     if (!conn->QueryPartFetchString(hdl, 1, res))
00355         return 0;
00356 
00357     conn->QueryPartEnd(hdl);
00358 
00359     FeatureTable* feat = 0;
00360     for (int i=0 ; i<startSize ; i++)
00361     {
00362         Quid q = vidSet->GetQuidFrame(fileIdx, start[i], true);
00363 
00364         size_t dataSize;
00365         UInt8* data = Util::Hex2Bin(res[i], dataSize);        
00366         int n = dataSize / sizeof(Real64);
00367         Real64* realData = (Real64*) data;
00368         FeatureTable::VectorReal64 v(n, realData, true);
00369 
00370         if (feat == 0)
00371             feat = new FeatureTable(def, res.size(), n);
00372         feat->Add(q, v);
00373         delete data;
00374     }
00375 
00376     if (feat == 0)
00377         ILOG_ERROR("No features found");
00378     delete start;
00379     return feat;
00380 }
00381 
00382 void
00383 MakeSimTableMapi(VideoSet* vidSet, int fileIdx, String conceptSet, String model,
00384                  String feature, String concept, Connection* conn,
00385                  SimilarityTableSet::SimTableType*& simTab,
00386                  QuidTable*& quidTab)
00387 {
00388     typedef SimilarityTableSet::SimTableType SimTableType;
00389 
00390     ILOG_VAR(Impala.Samples.MonetTest.MakeSimTableMapi);
00391 
00392     String vidSetBase = vidSet->GetSetNameBase();
00393     String conceptSetBase = FileNameBase(conceptSet);
00394     Quid vidQuid = vidSet->GetQuidVideo(fileIdx, true);
00395     String query =
00396         "select fr.fragment_start, s.confidence \
00397          from video_sets vss, video_set vs, keyword_sets kss, keyword_set ks, \
00398               keyword k, model_sets_name msn, model_sets mss, model_ref mr, \
00399               file f, fragment fr, score s \
00400          where vss.set_name = '" + vidSetBase + "' and \
00401                kss.set_name = '" + conceptSetBase + "' and \
00402                kss.video_sets_id = vss.video_sets_id and \
00403                msn.set_name = '" + feature + "' and \
00404                mss.model_sets_name_id = msn.model_sets_name_id and \
00405                k.keyword_name = '" + concept + "' and \
00406                ks.keyword_sets_id = kss.keyword_sets_id and \
00407                ks.keyword_id = k.keyword_id and \
00408                mr.model_name = '" + model + "' and \
00409                mr.keyword_set_id = ks.keyword_set_id and \
00410                mr.model_ref_id = mss.model_ref_id and \
00411                vs.video_sets_id = vss.video_sets_id and \
00412                f.file_id = vs.file_id and \
00413                f.quid = " + MakeString(vidQuid) + " and \
00414                fr.media_id = f.media_id and \
00415                fr.fragment_length = 1 and \
00416                fr.keyframe = false and \
00417                s.fragment_id = fr.fragment_id and \
00418                s.model_sets_id = mss.model_sets_id and \
00419                s.file_id = f.file_id \
00420          order by fr.fragment_start;";
00421 
00422     MapiHdl hdl = conn->QueryPartStart(query);
00423     if (hdl == 0)
00424         return;
00425 
00426     int* start = 0;
00427     int startSize = 0;
00428     if (!conn->QueryPartFetchInt(hdl, 0, start, startSize))
00429         return;
00430 
00431     double* sims = 0;
00432     int simSize = 0;
00433     if (!conn->QueryPartFetchDouble(hdl, 1, sims, simSize))
00434         return;
00435 
00436     conn->QueryPartEnd(hdl);
00437 
00438     simTab = new SimTableType(startSize);
00439     quidTab = new QuidTable(startSize);
00440     for (int i=0 ; i<startSize ; i++)
00441     {
00442         simTab->Add(sims[i]);
00443         Quid q = vidSet->GetQuidFrame(fileIdx, start[i], true);
00444         quidTab->Add(q);
00445     }
00446 
00447     delete start;
00448     delete sims;
00449 }
00450 
00453 
00454 // Assumes we are handling the "default" start + number specification
00455 int
00456 GetIntInRange(String optName, int minVal, int maxVal, bool isInterval)
00457 {
00458     CmdOptions& options = CmdOptions::GetInstance();
00459     int v = options.GetInt(optName);
00460     if (isInterval)
00461     {
00462         if (v == -1)
00463             return maxVal - minVal;
00464         if (minVal + v > maxVal)
00465             return maxVal - minVal;
00466     }
00467     else
00468     {
00469         if (v < minVal)
00470             return minVal;
00471         if (v > maxVal)
00472             return maxVal;
00473     }
00474     return v;
00475 }
00476 
00479 
00480 void
00481 DoSample(Connection* conn)
00482 {
00483     ILOG_VAR(Impala.Samples.MonetTest.DoSample);
00484 
00485     //conn.Query("select * from emp", true, true);
00486     //conn.Query("select * from video", true, true);
00487     //conn.Query("select * from model", true, true);
00488     //conn->Query("insert into model values (6, 'model nr 6');", true, true);
00489     conn->Query("copy 2 records into i_bulk_frame_image from stdin using delimiters ' ';\n488640559569698818 0 frame0 FFD8FFE000104A46494600010100000100010000 96 72 image/jpg\n488640559569698818 1 frame1 FFD8FFE000104A46494600010100000100010000 96 72 image/jpg\n", true, true);
00490 
00491 }
00492 
00493 void
00494 DoInsertVideoSets(Connection* conn)
00495 {
00496     ILOG_VAR(Impala.Samples.MonetTest.DoInsertVideoSets);
00497 
00498     VideoSetsRepository& rep = VideoSetsRepository::GetInstance();
00499     std::vector<int> ids = rep.GetAllId();
00500     for (int i=0 ; i<ids.size() ; i++)
00501     {
00502         String name = rep.GetSetName(ids[i]);
00503         String q = "select i_add_video_set(" + MakeString(ids[i])
00504             + ", '" + name + "');";
00505         ILOG_INFO("q = [" + q + "]");
00506         conn->Query(q, false, false);
00507     }
00508 }
00509 
00510 
00511 void
00512 DoInsertVideoFiles(Connection* conn)
00513 {
00514     ILOG_VAR(Impala.Samples.MonetTest.DoInsertVideoSet);
00515     CmdOptions& options = CmdOptions::GetInstance();
00516     if (options.GetNrArg() < 5)
00517     {
00518         ILOG_ERROR("Need more parameters");
00519         return;
00520     }
00521 
00522     String vidSetName = options.GetArg(4);
00523     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(vidSetName);
00524     String vidSetBase = vidSet->GetSetNameBase();
00525 
00526     int startFile = options.GetInt("startFile");
00527     for (int i=startFile ; i<vidSet->NrFiles() ; i++)
00528     {
00529         String file = vidSet->GetAsPath(i);
00530         Quid quid = vidSet->GetQuidVideo(i, true);
00531         // todo : get info from video
00532         //String q = "select i_add_video_file('" + vidSetBase + "', "
00533         //    + MakeString(quid) + ", '" + file
00534         //    + "', 352, 288, 25, -1, 'mpeg1', 'bitrate');";
00535         bool uniqueEvent = false;
00536         String evName = (uniqueEvent) ? vidSetBase + "_" + MakeString(quid)
00537                                       : FileNameBase(file);
00538         String q = "select i_add_video_file_event('" + vidSetBase + "', '"
00539             + evName + "', 'no description', " + MakeString(quid) + ", '"
00540             + file + "', 352, 288, true, 25, -1, 'mpeg1', 'bitrate');";
00541         ILOG_INFO("q = [" + q + "]");
00542         conn->Query(q, false, false);
00543     }
00544     delete vidSet;
00545 }
00546 
00547 
00548 void
00549 DoCheckVideoFiles(Connection* conn)
00550 {
00551     ILOG_VAR(Impala.Samples.MonetTest.DoCheckVideoSet);
00552     CmdOptions& options = CmdOptions::GetInstance();
00553     if (options.GetNrArg() < 5)
00554     {
00555         ILOG_ERROR("Need more parameters");
00556         return;
00557     }
00558 
00559     String vidSetName = options.GetArg(4);
00560     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(vidSetName);
00561     VideoSet* vidSet2 = MakeVideoSetMapi(vidSetName, conn);
00562     vidSet->Diff(vidSet2);
00563     delete vidSet;
00564     delete vidSet2;
00565 }
00566 
00567 
00568 void
00569 DoInsertSegmentation(Connection* conn)
00570 {
00571     ILOG_VAR(Impala.Samples.MonetTest.DoInsertSegmentation);
00572     CmdOptions& options = CmdOptions::GetInstance();
00573     if (options.GetNrArg() < 5)
00574     {
00575         ILOG_ERROR("Need more parameters");
00576         return;
00577     }
00578 
00579     String vidSetName = options.GetArg(4);
00580     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(vidSetName);
00581 
00582     Segmentation segmentation(vidSet, "segmentation");
00583     ILOG_INFO("nrVideos = " << segmentation.GetNrVideos());
00584     ILOG_INFO("nrShots = " << segmentation.GetNrShots());
00585     ILOG_INFO("nrFrames = " << segmentation.GetTotalNrFrames());
00586     conn->Query("delete from i_bulk_fragment;", false, false);
00587     int startFile = options.GetInt("startFile");
00588     Timer timer;
00589     for (int v=startFile ; v<segmentation.GetNrVideos() ; v++)
00590     {
00591         ILOG_INFO("v = " << v);
00592         Quid vidQuid = vidSet->GetQuidVideo(v, true);
00593         int first = segmentation.GetFirstShotVideo(v);
00594         int nr = segmentation.GetNrShotsVideo(v);
00595         String q = "copy " + MakeString(nr) +
00596             " records into i_bulk_fragment from stdin using delimiters ' ';\n";
00597         for (int s=first ; s<first+nr ; s++)
00598         {
00599             int start = segmentation.GetStart(s);
00600             int len = segmentation.GetEnd(s) - start + 1;
00601             q += MakeString(vidQuid) + " " + MakeString(start) + " "
00602                 + MakeString(len) + " " + segmentation.GetName(s)
00603                 + " false false\n";
00604         }
00605         conn->Query(q, false, false);
00606 
00607         nr = segmentation.GetNrFramesVideo(v);
00608         q = "copy " + MakeString(nr) +
00609             " records into i_bulk_fragment from stdin using delimiters ' ';\n";
00610         for (int f=0 ; f<nr ; f++)
00611         {
00612             q += MakeString(vidQuid) + " " + MakeString(f) + " 1 frame"
00613                 + MakeString(f) + " false false\n";
00614         }
00615         conn->Query(q, false, false);
00616     }
00617     ILOG_INFO("Did bulk in " << timer.SplitTime());
00618     conn->Query("insert into fragment (media_id, fragment_start, \
00619                                        fragment_length, fragment_name, \
00620                                        keyframe, representative) \
00621                  select m.media_id, bf.fragment_start, bf.fragment_length, \
00622                         bf.fragment_name, bf.keyframe, bf.representative \
00623                  from i_bulk_fragment bf, media m \
00624                  where bf.quid = m.quid \
00625                  order by bf.quid, bf.fragment_start;", false, false);
00626     ILOG_INFO("Did insert select at " << timer.SplitTime());
00627     conn->Query("delete from i_bulk_fragment;", false, false);
00628 }
00629 
00630 
00631 void
00632 DoCheckSegmentation(Connection* conn)
00633 {
00634     ILOG_VAR(Impala.Samples.MonetTest.DoCheckSegmentation);
00635     CmdOptions& options = CmdOptions::GetInstance();
00636     if (options.GetNrArg() < 5)
00637     {
00638         ILOG_ERROR("Need more parameters");
00639         return;
00640     }
00641 
00642     String vidSetName = options.GetArg(4);
00643     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(vidSetName);
00644     Segmentation segmentation(vidSet, "segmentation");
00645     Segmentation* segmentation2 = MakeSegmentationMapi(vidSet, conn);
00646     segmentation.Diff(segmentation2);
00647     delete segmentation2;
00648     delete vidSet;
00649 }
00650 
00651 
00652 void
00653 DoInsertKeyframes(Connection* conn)
00654 {
00655     ILOG_VAR(Impala.Samples.MonetTest.DoInsertKeyframes);
00656     CmdOptions& options = CmdOptions::GetInstance();
00657     if (options.GetNrArg() < 5)
00658     {
00659         ILOG_ERROR("Need more parameters");
00660         return;
00661     }
00662 
00663     String vidSetName = options.GetArg(4);
00664     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(vidSetName);
00665 
00666     Keyframes keyframes(vidSet, "keyframes");
00667     ILOG_INFO("nrVideos = " << keyframes.GetNrVideos());
00668     ILOG_INFO("nrKeyframes = " << keyframes.GetNrKeyframes());
00669     conn->Query("delete from i_bulk_fragment;", false, false);
00670     int startFile = options.GetInt("startFile");
00671     Timer timer;
00672     for (int v=startFile ; v<keyframes.GetNrVideos() ; v++)
00673     {
00674         ILOG_INFO("v = " << v);
00675         Quid vidQuid = vidSet->GetQuidVideo(v, true);
00676         int first = keyframes.GetFirstKeyframeVideo(v);
00677         int nr = keyframes.GetNrKeyframesVideo(v);
00678         String q = "copy " + MakeString(nr) +
00679             " records into i_bulk_fragment from stdin using delimiters ' ';\n";
00680         for (int k=first ; k<first+nr ; k++)
00681         {
00682             //int shot = keyframes.GetShotId(k); // cannot store in schema
00683             int frame = keyframes.GetFrameNr(k);
00684             String rkf = (keyframes.IsRKF(k)) ? "true" : "false";
00685             q += MakeString(vidQuid) + " " + MakeString(frame) + " 1 "
00686                 + keyframes.GetName(k) + " true " + rkf + "\n";
00687         }
00688         conn->Query(q, false, false);
00689     }
00690     ILOG_INFO("Did bulk in " << timer.SplitTime());
00691     conn->Query("insert into fragment (media_id, fragment_start, \
00692                                        fragment_length, fragment_name, \
00693                                        keyframe, representative) \
00694                  select m.media_id, bf.fragment_start, bf.fragment_length, \
00695                         bf.fragment_name, bf.keyframe, bf.representative \
00696                  from i_bulk_fragment bf, media m \
00697                  where bf.quid = m.quid \
00698                  order by bf.quid, bf.fragment_start;", false, false);
00699     ILOG_INFO("Did insert select at " << timer.SplitTime());
00700     conn->Query("delete from i_bulk_fragment;", false, false);
00701 }
00702 
00703 
00704 void
00705 DoCheckKeyframes(Connection* conn)
00706 {
00707     ILOG_VAR(Impala.Samples.MonetTest.DoCheckKeyframes);
00708     CmdOptions& options = CmdOptions::GetInstance();
00709     if (options.GetNrArg() < 5)
00710     {
00711         ILOG_ERROR("Need more parameters");
00712         return;
00713     }
00714 
00715     String vidSetName = options.GetArg(4);
00716     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(vidSetName);
00717     Keyframes keyframes(vidSet, "keyframes");
00718     Keyframes* keyframes2 = MakeKeyframesMapi(vidSet, conn);
00719     keyframes.Diff(keyframes2);
00720     delete keyframes2;
00721     delete vidSet;
00722 }
00723 
00724 
00725 void
00726 DoInsertFrameArchive(Connection* conn)
00727 {
00728     ILOG_VAR(Impala.Samples.MonetTest.DoInsertFrameArchive);
00729     CmdOptions& options = CmdOptions::GetInstance();
00730     if (options.GetNrArg() < 5)
00731     {
00732         ILOG_ERROR("Need more parameters");
00733         return;
00734     }
00735 
00736     String vidSetName = options.GetArg(4);
00737     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(vidSetName);
00738 
00739     conn->Query("delete from i_bulk_frame_image;", false, false);
00740     int startFile = options.GetInt("startFile");
00741     Timer timer;
00742     for (int v=startFile ; v<vidSet->NrFiles() ; v++)
00743     {
00744         String path = vidSet->GetFilePathFrames(v, "images_jpg.raw", false,
00745                                                 false);
00746         if (path.empty())
00747             continue;
00748         ImageArchiveFile ar(path, false, vidSet->GetDatabase());
00749         ILOG_INFO("vid " << v << " has " << ar.NrImages() << " images");
00750         ILOG_NDC_PUSH("v=" << v);
00751         Quid vidQuid = vidSet->GetQuidVideo(v, true);
00752 
00753         conn->Query("delete from i_bulk_frame_image;", false, false);
00754         int totalSize = ar.NrImages();
00755         int bulkSize = 4000;
00756         Timer timer;
00757 
00758         for (int i=0 ; i<totalSize ; i+=bulkSize)
00759         {
00760             int left = totalSize - i;
00761             int nr = (left > bulkSize) ? bulkSize : left;
00762             String q = "copy " + MakeString(nr) +
00763                 " records into i_bulk_frame_image from stdin" +
00764                 " using delimiters ' ';\n";
00765             for (int j=i ; j<i+nr ; j++)
00766             {
00767                 Array2dVec3UInt8* im = ar.ReadImage(j);
00768                 size_t dataSize = 0;
00769                 UInt8* data = ar.GetImageData(j, dataSize);
00770                 String hexData = Util::Bin2Hex(data, dataSize);
00771                 delete data;
00772                 String name = "frame" + MakeString(j);
00773                 String width = MakeString(im->CW());
00774                 String height = MakeString(im->CH());
00775                 q += MakeString(vidQuid) + " " + MakeString(j) + " " + name + " "
00776                     + hexData + " " + width + " " + height + " image/jpg \n";
00777                 delete im;
00778             }
00779             conn->Query(q, false, false);
00780             ILOG_INFO("Did bulk " << i << " at " << timer.SplitTime());
00781 
00782             conn->Query("insert into frame_image (fragment_id, image, width, \
00783                                                   height, mime_type) \
00784                          select fr.fragment_id, bf.image, bf.width, bf.height, \
00785                                 bf.mime_type \
00786                          from i_bulk_frame_image bf, file f, fragment fr \
00787                          where bf.quid = f.quid and \
00788                                f.media_id = fr.media_id and \
00789                                fr.fragment_start = bf.fragment_start and \
00790                                fr.fragment_length = 1 and \
00791                                fr.keyframe = false \
00792                          order by bf.quid, bf.fragment_start;", false, false);
00793             ILOG_INFO("Did insert select at " << timer.SplitTime());
00794             conn->Query("delete from i_bulk_frame_image;", false, false);
00795         }
00796         ILOG_NDC_POP;
00797     }
00798     delete vidSet;
00799 }
00800 
00801 
00802 void
00803 DoCheckFrameArchive(Connection* conn)
00804 {
00805     ILOG_VAR(Impala.Samples.MonetTest.DoCheckFrameArchive);
00806     CmdOptions& options = CmdOptions::GetInstance();
00807     if (options.GetNrArg() < 5)
00808     {
00809         ILOG_ERROR("Need more parameters");
00810         return;
00811     }
00812 
00813     String vidSetName = options.GetArg(4);
00814     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(vidSetName);
00815 
00816     int startFile = options.GetInt("startFile");
00817     Timer timer;
00818     for (int v=startFile ; v<vidSet->NrFiles() ; v++)
00819     {
00820         String path = vidSet->GetFilePathFrames(v, "images_jpg.raw", false,
00821                                                 false);
00822         if (path.empty())
00823             continue;
00824         ImageArchiveFile ar(path, false, vidSet->GetDatabase());
00825         ILOG_INFO("video " << v << " has " << ar.NrImages() << " images");
00826 
00827         Quid vidQuid = vidSet->GetQuidVideo(v, true);
00828 
00829         ImageArchiveMapi ar2(vidQuid, conn);
00830         Core::Array::ImageArchiveDiff(&ar, &ar2);
00831         ILOG_INFO("Did diff at " << timer.SplitTime());
00832     }
00833     delete vidSet;
00834 }
00835 
00836 
00837 void
00838 DoInsertKeywords(Connection* conn)
00839 {
00840     ILOG_VAR(Impala.Samples.MonetTest.DoInsertKeywords);
00841     CmdOptions& options = CmdOptions::GetInstance();
00842     if (options.GetNrArg() < 6)
00843     {
00844         ILOG_ERROR("Need more parameters");
00845         return;
00846     }
00847 
00848     String vidSetName = options.GetArg(4);
00849     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(vidSetName);
00850     String conceptSet = options.GetArg(5);
00851     String path = vidSet->GetFilePathAnnotation(conceptSet, false, false);
00852     if (path.empty())
00853         return;
00854     KeywordList concepts;
00855     Util::Database* db = vidSet->GetDatabase();
00856     Util::DatabaseReadString(std::back_inserter(concepts), path, db, true);
00857 
00858     String vidSetBase = vidSet->GetSetNameBase();
00859     String conceptSetBase = FileNameBase(conceptSet);
00860     for (int i=0 ; i<concepts.size() ; i++)
00861     {
00862         String q = "select i_add_keyword('" + vidSetBase + "', '" +
00863             conceptSetBase + "', '" + concepts[i] + "');";
00864         ILOG_INFO("q = [" + q + "]");
00865         conn->Query(q, false, false);
00866     }
00867 }
00868 
00869 
00870 void
00871 DoCheckKeywords(Connection* conn)
00872 {
00873     ILOG_VAR(Impala.Samples.MonetTest.DoCheckKeywords);
00874     CmdOptions& options = CmdOptions::GetInstance();
00875     if (options.GetNrArg() < 6)
00876     {
00877         ILOG_ERROR("Need more parameters");
00878         return;
00879     }
00880 
00881     String vidSetName = options.GetArg(4);
00882     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(vidSetName);
00883     String conceptSet = options.GetArg(5);
00884     String path = vidSet->GetFilePathAnnotation(conceptSet, false, false);
00885     if (path.empty())
00886         return;
00887     KeywordList concepts;
00888     Util::Database* db = vidSet->GetDatabase();
00889     Util::DatabaseReadString(std::back_inserter(concepts), path, db, true);
00890 
00891 
00892     KeywordList concepts2 = MakeKeywordListMapi(vidSetName, conceptSet, conn);
00893     Core::Table::KeywordListDiff(concepts, concepts2);
00894     delete vidSet;
00895 }
00896 
00897 
00898 void
00899 DoInsertAnnotations(Connection* conn)
00900 {
00901     ILOG_VAR(Impala.Samples.MonetTest.DoInsertAnnotations);
00902     CmdOptions& options = CmdOptions::GetInstance();
00903     if (options.GetNrArg() < 7)
00904     {
00905         ILOG_ERROR("Need more parameters");
00906         return;
00907     }
00908 
00909     String vidSetName = options.GetArg(4);
00910     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(vidSetName);
00911     String conceptSet = options.GetArg(5);
00912     int quidClass = StringToQuidClass(options.GetArg(6));
00913     if (quidClass != QUID_CLASS_FRAME)
00914     {
00915         ILOG_ERROR("Can do frame annotations only.");
00916         return;
00917     }
00918 
00919     AnnotationTableSet* tSet =
00920         Core::Table::AnnotationTableSet::MakeFromFile(vidSet, conceptSet, true,
00921                                                       quidClass);
00922     if (tSet == 0)
00923         return;
00924 
00925     conn->Query("select i_add_annotator('fabchannel', 'the boss', true);",
00926                 false, false);
00927 
00928     conn->Query("delete from i_bulk_annotation;", false, false);
00929     Timer timer;
00930     for (int t=0 ; t<tSet->Size() ; t++)
00931     {
00932         AnnotationTable* tab = tSet->GetTable(t);
00933         String keyword = tSet->GetLabel(t);
00934         ILOG_INFO("keyword " << keyword << " has " << tab->Size() << " annos");
00935         String date("2007/08/22-11:00:00");
00936         int nr = tab->Size();
00937         String q = "copy " + MakeString(nr) +
00938             " records into i_bulk_annotation from stdin using delimiters ' ';\n";
00939         for (int i=0 ; i<nr ; i++)
00940         {
00941             Quid frameQuid = tab->Get1(i);
00942             int vidId = QuidObject(frameQuid);
00943             Quid vidQuid = vidSet->GetQuidVideo(vidId, false);
00944             int frameNr = QuidId(frameQuid);
00945             Real64 relevance = 0.5; // skip, using Mpeg7 convention
00946             if (tab->IsPositive(i))
00947                 relevance = 1.0;
00948             else if (tab->IsNegative(i))
00949                 relevance = 0.0;
00950             Real64 confidence = 1.0;
00951             q += keyword + " " + MakeString(vidQuid) + " " + MakeString(frameNr)
00952                 + " " + MakeString(relevance) + " " + MakeString(confidence)
00953                 + " " + date + "\n";
00954         }
00955         conn->Query(q, false, false);
00956     }
00957     ILOG_INFO("Did bulk in " << timer.SplitTime());
00958     String vidSetBase = vidSet->GetSetNameBase();
00959     conn->Query("insert into annotation (fragment_id, keyword_id, annotator_id, \
00960                                          relevance, confidence, \
00961                                          annotation_date, approved) \
00962                  select fr.fragment_id, k.keyword_id, a.annotator_id, \
00963                         ba.relevance, ba.confidence, ba.annotation_date, \
00964                         a.approved \
00965                  from i_bulk_annotation ba, media m, fragment fr, \
00966                       video_sets vss, \
00967                       keyword_sets kss, keyword_set ks, keyword k, annotator a \
00968                  where ba.quid = m.quid and \
00969                        m.media_id = fr.media_id and \
00970                        fr.fragment_start = ba.fragment_start and \
00971                        fr.fragment_length = 1 and \
00972                        fr.keyframe = false and \
00973                        vss.set_name = '" + vidSetBase + "' and \
00974                        kss.set_name = '" + FileNameBase(conceptSet) + "' and \
00975                        kss.video_sets_id = vss.video_sets_id and \
00976                        ks.keyword_sets_id = kss.keyword_sets_id and \
00977                        ks.keyword_id = k.keyword_id and \
00978                        k.keyword_name = ba.keyword_name and \
00979                        a.annotator_name = 'fabchannel' \
00980                  order by ba.quid, ba.fragment_start;", false, false);
00981     conn->Query("delete from i_bulk_annotation;", false, false);
00982     delete tSet;
00983     delete vidSet;
00984 }
00985 
00986 
00987 void
00988 DoCheckAnnotations(Connection* conn)
00989 {
00990     ILOG_VAR(Impala.Samples.MonetTest.DoCheckAnnotations);
00991     CmdOptions& options = CmdOptions::GetInstance();
00992     if (options.GetNrArg() < 7)
00993     {
00994         ILOG_ERROR("Need more parameters");
00995         return;
00996     }
00997 
00998     String vidSetName = options.GetArg(4);
00999     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(vidSetName);
01000     String conceptSet = options.GetArg(5);
01001     int quidClass = StringToQuidClass(options.GetArg(6));
01002     if (quidClass != QUID_CLASS_FRAME)
01003     {
01004         ILOG_ERROR("Can do frame annotations only.");
01005         return;
01006     }
01007 
01008     AnnotationTableSet* tSet =
01009         Core::Table::AnnotationTableSet::MakeFromFile(vidSet, conceptSet, true,
01010                                                       quidClass);
01011     if (tSet == 0)
01012         return;
01013 
01014     AnnotationTableSet* tSet2 = MakeAnnotationTableSetMapi(vidSet, conceptSet,
01015                                                            true, quidClass,
01016                                                            conn);
01017     tSet->Diff(tSet2);
01018     delete tSet;
01019     delete tSet2;
01020     delete vidSet;
01021 }
01022 
01023 
01024 void
01025 DoInsertFeatures(Connection* conn)
01026 {
01027     ILOG_VAR(Impala.Samples.MonetTest.DoInsertFeatures);
01028     CmdOptions& options = CmdOptions::GetInstance();
01029     if (options.GetNrArg() < 6)
01030     {
01031         ILOG_ERROR("Need more parameters");
01032         return;
01033     }
01034 
01035     String vidSetName = options.GetArg(4);
01036     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(vidSetName);
01037     FeatureDefinition def(options.GetArg(5));
01038 
01039     String q = "select i_add_feature('" + def.AsString() + "');";
01040     conn->Query(q, false, false);
01041 
01042     int startFile = options.GetInt("startFile");
01043     for (int v=startFile ; v<vidSet->NrFiles() ; v++)
01044     {
01045         String fName = vidSet->GetFilePathFeatureData("Frames", def, v, false,
01046                                                       -1, false, false);
01047         if (fName.empty())
01048         {
01049             ILOG_ERROR("Unable to find " << def.AsString());
01050             continue;
01051         }
01052         FeatureTable* tab =
01053             Core::Feature::FeatureTable::MakeFromFile(def, fName,
01054                                                       vidSet->GetDatabase());
01055         ILOG_INFO("vid " << v << " has " << tab->Size() << " vectors");
01056         ILOG_NDC_PUSH("v=" << v);
01057         Quid vidQuid = vidSet->GetQuidVideo(v, true);
01058 
01059         conn->Query("delete from i_bulk_feature_vector;", false, false);
01060         int totalSize = tab->Size();
01061         int bulkSize = 2000;
01062         Timer timer;
01063         for (int i=0 ; i<totalSize ; i+=bulkSize)
01064         {
01065             int left = totalSize - i;
01066             int nr = (left > bulkSize) ? bulkSize : left;
01067             String q = "copy " + MakeString(nr) +
01068                 " records into i_bulk_feature_vector from stdin" +
01069                 " using delimiters ' ';\n";
01070             for (int j=i ; j<i+nr ; j++)
01071             {
01072                 Quid frameQuid = tab->Get1(j);
01073                 if (QuidClass(frameQuid) != QUID_CLASS_FRAME)
01074                     ILOG_ERROR("Quid is not a frame");
01075                 int frameNr = QuidId(frameQuid);
01076                 FeatureTable::VectorReal64 v = tab->Get2(j);
01077                 size_t dataSize = v.Size() * sizeof(Real64);
01078                 UInt8* data = (UInt8*) v.GetData();
01079                 String hexData = Util::Bin2Hex(data, dataSize);
01080 
01081                 q += MakeString(vidQuid) + " " + MakeString(frameNr) + " "
01082                     + hexData + "\n";
01083             }
01084             conn->Query(q, false, false);
01085             ILOG_INFO("Did bulk " << i << " at " << timer.SplitTime());
01086 
01087             String vidSetBase = vidSet->GetSetNameBase();
01088             conn->Query("insert into feature_vector (fragment_id, file_id, \
01089                                                      feature_id, vector) \
01090                          select fr.fragment_id, f.file_id, fe.feature_id, \
01091                                 bfv.vector \
01092                          from i_bulk_feature_vector bfv, file f, \
01093                               fragment fr, feature fe \
01094                          where bfv.quid = f.quid and \
01095                                f.media_id = fr.media_id and \
01096                                fr.fragment_start = bfv.fragment_start and \
01097                                fr.fragment_length = 1 and \
01098                                fr.keyframe = false and \
01099                                fe.feature_name = '" + def.AsString() + "' \
01100                          order by bfv.fragment_start;", false, false);
01101             ILOG_INFO("Did insert select at " << timer.SplitTime());
01102             conn->Query("delete from i_bulk_feature_vector;", false, false);
01103         }
01104         delete tab;
01105         ILOG_NDC_POP;
01106     }
01107     delete vidSet;
01108 }
01109 
01110 
01111 void
01112 DoCheckFeatures(Connection* conn)
01113 {
01114     ILOG_VAR(Impala.Samples.MonetTest.DoCheckFeatures);
01115     CmdOptions& options = CmdOptions::GetInstance();
01116     if (options.GetNrArg() < 6)
01117     {
01118         ILOG_ERROR("Need more parameters");
01119         return;
01120     }
01121 
01122     String vidSetName = options.GetArg(4);
01123     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(vidSetName);
01124     FeatureDefinition def(options.GetArg(5));
01125 
01126     for (int v=0 ; v<vidSet->NrFiles() ; v++)
01127     {
01128         ILOG_NDC_PUSH("v=" << v);
01129         Timer timer;
01130         // reversed order to reduce memory usage
01131         FeatureTable* tab2 = MakeFeatureTableMapi(vidSet, v, def, conn);
01132         ILOG_INFO("Did mapi at " << timer.SplitTime());
01133 
01134         String fName = vidSet->GetFilePathFeatureData("Frames", def, v, false,
01135                                                       -1, false, false);
01136         if (fName.empty())
01137         {
01138             ILOG_ERROR("Unable to find " << def.AsString());
01139             delete tab2;
01140             ILOG_NDC_POP;
01141             continue;
01142         }
01143         FeatureTable* tab =
01144             Core::Feature::FeatureTable::MakeFromFile(def, fName,
01145                                                       vidSet->GetDatabase());
01146         ILOG_INFO("Did file at " << timer.SplitTime());
01147 
01148         if (tab->Diff(tab2) == 0)
01149             ILOG_INFO("No differences");
01150         delete tab;
01151         delete tab2;
01152         ILOG_NDC_POP;
01153     }
01154     delete vidSet;
01155 }
01156 
01157 
01158 void
01159 DoInsertModels(Connection* conn)
01160 {
01161     ILOG_VAR(Impala.Samples.MonetTest.DoInsertModels);
01162     CmdOptions& options = CmdOptions::GetInstance();
01163     if (options.GetNrArg() < 8)
01164     {
01165         ILOG_ERROR("Need more parameters");
01166         return;
01167     }
01168 
01169     String vidSetName = options.GetArg(4);
01170     String conceptSet = options.GetArg(5);
01171     String modelName = options.GetArg(6);
01172     String featureDef(options.GetArg(7));
01173 
01174     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(vidSetName);
01175     String path = vidSet->GetFilePathAnnotation(conceptSet, false, false);
01176     if (path.empty())
01177         return;
01178     KeywordList concepts;
01179     Util::Database* db = vidSet->GetDatabase();
01180     Util::DatabaseReadString(std::back_inserter(concepts), path, db, true);
01181 
01182     String q = "select i_add_feature('" + featureDef + "');";
01183     ILOG_INFO("q = [" + q + "]");
01184     conn->Query(q, false, false);
01185 
01186     String vidSetBase = vidSet->GetSetNameBase();
01187     String conceptSetBase = FileNameBase(conceptSet);
01188     for (int i=0 ; i<concepts.size() ; i++)
01189     {
01190         String q = "select i_add_model('" + vidSetBase + "', '" +
01191             conceptSetBase + "', '" + concepts[i] + "', '" + modelName +
01192             "', '" + featureDef + "');";
01193         ILOG_INFO("q = [" + q + "]");
01194         conn->Query(q, false, false);
01195 
01196         // a model is also a set with itself as element
01197         q = "select i_add_model_sets('" + vidSetBase + "', '" +
01198             conceptSetBase + "', '" + concepts[i] + "', '" + modelName +
01199             "', '" + featureDef + "');";
01200         ILOG_INFO("q = [" + q + "]");
01201         conn->Query(q, false, false);
01202         q = "select i_add_model_to_set('" + vidSetBase + "', '" +
01203             conceptSetBase + "', '" + concepts[i] + "', '" + modelName +
01204             "', '" + featureDef + "', '" + featureDef + "');";
01205         ILOG_INFO("q = [" + q + "]");
01206         conn->Query(q, false, false);
01207     }
01208 }
01209 
01210 
01211 void
01212 DoInsertModelSets(Connection* conn)
01213 {
01214     ILOG_VAR(Impala.Samples.MonetTest.DoInsertModelSets);
01215     CmdOptions& options = CmdOptions::GetInstance();
01216     if (options.GetNrArg() < 9)
01217     {
01218         ILOG_ERROR("Need more parameters");
01219         return;
01220     }
01221 
01222     String vidSetName = options.GetArg(4);
01223     String conceptSet = options.GetArg(5);
01224     String modelName = options.GetArg(6);
01225     String modelSetName = options.GetArg(7);
01226     std::vector<String> featureDefs;
01227     for (int i=8 ; i<options.GetNrArg() ; i++)
01228         featureDefs.push_back(options.GetArg(i));
01229 
01230     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(vidSetName);
01231     String path = vidSet->GetFilePathAnnotation(conceptSet, false, false);
01232     if (path.empty())
01233         return;
01234     KeywordList concepts;
01235     Util::Database* db = vidSet->GetDatabase();
01236     Util::DatabaseReadString(std::back_inserter(concepts), path, db, true);
01237 
01238     String vidSetBase = vidSet->GetSetNameBase();
01239     String conceptSetBase = FileNameBase(conceptSet);
01240     for (int i=0 ; i<concepts.size() ; i++)
01241     {
01242         String q = "select i_add_model_sets('" + vidSetBase + "', '" +
01243             conceptSetBase + "', '" + concepts[i] + "', '" + modelName +
01244             "', '" + modelSetName + "');";
01245         ILOG_INFO("q = [" + q + "]");
01246         conn->Query(q, false, false);
01247 
01248         String baseQ = "select i_add_model_to_set('" + vidSetBase + "', '" +
01249             conceptSetBase + "', '" + concepts[i] + "', '" + modelName +
01250             "', '" + modelSetName + "', '";
01251         for (int f=0 ; f<featureDefs.size() ; f++)
01252         {
01253             String q = baseQ + featureDefs[f] + "');";
01254             ILOG_INFO("q = [" + q + "]");
01255             conn->Query(q, false, false);
01256         }
01257     }
01258 }
01259 
01260 
01261 void
01262 InsertOneSimTable(Connection* conn, VideoSet* vidSet, String conceptSet,
01263                   String model, String feature, int fileIdx, String concept,
01264                   QuidTable* quidTab)
01265 {
01266     typedef SimilarityTableSet::SimTableType SimTableType;
01267 
01268     ILOG_VAR(Impala.Samples.MonetTest.InsertOneSimTable);
01269     CmdOptions& options = CmdOptions::GetInstance();
01270 
01271     SimTableType simTable(0);
01272     String path =
01273         vidSet->GetFilePathSimilarityData("Frames", conceptSet, model,
01274                                           feature, fileIdx,
01275                                           concept + "_sim.tab", false, false);
01276     if (path.empty())
01277         return;
01278     Core::Table::Read(&simTable, path, vidSet->GetDatabase());
01279     ILOG_INFO("simTable size = " << simTable.Size());
01280     Quid vidQuid = vidSet->GetQuidVideo(fileIdx, true);
01281 
01282     conn->Query("delete from i_bulk_score;", false, false);
01283     int startScore = GetIntInRange("startScore", 0, simTable.Size(), false);
01284     int numberScores = GetIntInRange("numberScores", startScore,
01285                                      simTable.Size(), true);
01286     int bulkSize = 100000;
01287     Timer timer;
01288     for (int i=startScore ; i<startScore+numberScores ; i+=bulkSize)
01289     {
01290         int left = numberScores - i;
01291         int nr = (left > bulkSize) ? bulkSize : left;
01292         String q = "copy " + MakeString(nr) +
01293                  " records into i_bulk_score from stdin using delimiters ' ';\n";
01294         for (int j=i ; j<i+nr ; j++)
01295         {
01296             Quid frameQuid = quidTab->Get1(j);
01297             if (QuidClass(frameQuid) != QUID_CLASS_FRAME)
01298                 ILOG_ERROR("Quid is not a frame");
01299             int frameNr = QuidId(frameQuid);
01300 
01301             q += MakeString(vidQuid) + " " + MakeString(frameNr) + " "
01302                 + MakeString(simTable.Get1(j)) + "\n";
01303         }
01304         conn->Query(q, false, false);
01305         ILOG_INFO("Did bulk at " << timer.SplitTime());
01306     }
01307 
01308     String vidSetBase = vidSet->GetSetNameBase();
01309     String conceptSetBase = FileNameBase(conceptSet);
01310     conn->Query("insert into score (fragment_id, model_sets_id, file_id, \
01311                                     confidence) \
01312                  select fr.fragment_id, mss.model_sets_id, f.file_id, \
01313                         bs.confidence \
01314                  from video_sets vss, video_set vs, keyword_sets kss, \
01315                       keyword_set ks, keyword k, model_sets_name msn, \
01316                       model_sets mss, model_ref mr, \
01317                       file f, fragment fr, i_bulk_score bs \
01318                  where vss.set_name = '" + vidSetBase + "' and \
01319                        kss.set_name = '" + conceptSetBase + "' and \
01320                        kss.video_sets_id = vss.video_sets_id and \
01321                        msn.set_name = '" + feature + "' and \
01322                        mss.model_sets_name_id = msn.model_sets_name_id and \
01323                        k.keyword_name = '" + concept + "' and \
01324                        ks.keyword_sets_id = kss.keyword_sets_id and \
01325                        ks.keyword_id = k.keyword_id and \
01326                        mr.model_name = '" + model + "' and \
01327                        mr.keyword_set_id = ks.keyword_set_id and \
01328                        mr.model_ref_id = mss.model_ref_id and \
01329                        vs.video_sets_id = vss.video_sets_id and \
01330                        f.file_id = vs.file_id and \
01331                        bs.quid = f.quid and \
01332                        fr.media_id = f.media_id and \
01333                        fr.fragment_start = bs.fragment_start and \
01334                        fr.fragment_length = 1 and \
01335                        fr.keyframe = false \
01336                  order by bs.fragment_start;", false, false);
01337     ILOG_INFO("Did insert select at " << timer.SplitTime());
01338     conn->Query("delete from i_bulk_score;", false, false);
01339     ILOG_INFO("Done in " << timer.SplitTime());
01340 }
01341 
01342 void
01343 CheckOneSimTable(Connection* conn, VideoSet* vidSet, String conceptSet,
01344                  String model, String feature, int fileId, String concept,
01345                  QuidTable* quidTab)
01346 {
01347     typedef SimilarityTableSet::SimTableType SimTableType;
01348     CmdOptions& options = CmdOptions::GetInstance();
01349 
01350     ILOG_VAR(Impala.Samples.MonetTest.CheckOneSimTable);
01351     Timer timer;
01352 
01353     SimTableType table(0);
01354     String path =
01355         vidSet->GetFilePathSimilarityData("Frames", conceptSet, model,
01356                                           feature, fileId,
01357                                           concept + "_sim.tab", false, false);
01358     if (path.empty())
01359         return;
01360     Core::Table::Read(&table, path, vidSet->GetDatabase());
01361     ILOG_INFO("Did file in " << timer.SplitTime());
01362     ILOG_INFO("table size = " << table.Size());
01363 
01364     // ignore startScore for now
01365     //int startScore = options.GetInt("startScore");
01366     int numberScores = GetIntInRange("numberScores", 0, table.Size(), true);
01367     table.SetSize(numberScores);
01368     quidTab->SetSize(numberScores);
01369 
01370     SimTableType* simTab2 = 0;
01371     QuidTable* quidTab2 = 0;
01372     MakeSimTableMapi(vidSet, fileId, conceptSet, model, feature, concept, conn,
01373                      simTab2, quidTab2);
01374     ILOG_INFO("Did mapi at " << timer.SplitTime());
01375 if (quidTab->Size() == quidTab2->Size() + 1)
01376 {
01377 ILOG_INFO("Adjusting size to " << quidTab2->Size());
01378 quidTab->SetSize(quidTab2->Size());
01379 }
01380     if (quidTab->Diff(quidTab2) == 0)
01381     {
01382         ILOG_INFO("No differences in quids");
01383     }
01384     else
01385     {
01386         //quidTab->Dump(vidSet, 0, -1);
01387         //quidTab2->Dump(vidSet, 0, -1);
01388     }
01389 if (table.Size() == simTab2->Size() + 1)
01390 {
01391 ILOG_INFO("Adjusting size to " << simTab2->Size());
01392 table.SetSize(simTab2->Size());
01393 }
01394     if (SimTableTypeDiff(&table, simTab2) == 0)
01395         ILOG_INFO("No differences in sims");
01396     delete simTab2;
01397     delete quidTab2;
01398 }
01399 
01400 void
01401 DoSimilarities(Connection* conn, bool doInsert)
01402 {
01403     ILOG_VAR(Impala.Samples.MonetTest.DoSimilarities);
01404     CmdOptions& options = CmdOptions::GetInstance();
01405     if (options.GetNrArg() < 8)
01406     {
01407         ILOG_ERROR("Need more parameters");
01408         return;
01409     }
01410 
01411     String vidSetName = options.GetArg(4);
01412     String conceptSet = options.GetArg(5);
01413     String modelName = options.GetArg(6);
01414     String feature = options.GetArg(7);
01415 
01416     VideoSet* vidSet = Core::VideoSet::MakeVideoSet(vidSetName);
01417     String path = vidSet->GetFilePathAnnotation(conceptSet, false, false);
01418     if (path.empty())
01419         return;
01420     KeywordList concepts;
01421     Util::Database* db = vidSet->GetDatabase();
01422     Util::DatabaseReadString(std::back_inserter(concepts), path, db, true);
01423 
01424     int startF = GetIntInRange("startFile", 0, vidSet->NrFiles(), false);
01425     int numberF = GetIntInRange("numberFiles", startF, vidSet->NrFiles(), true);
01426     for (int v=startF ; v<startF+numberF ; v++)
01427     {
01428         QuidTable quidTab(0);
01429         String path =
01430             vidSet->GetFilePathSimilarityData("Frames", conceptSet, modelName,
01431                                               feature, v, "all_quids.tab",
01432                                               false, false);
01433         if (path.empty())
01434             continue;
01435         Core::Table::Read(&quidTab, path, vidSet->GetDatabase());
01436         ILOG_INFO("quidTab size = " << quidTab.Size());
01437         int startConcept = GetIntInRange("startConcept", 0, concepts.size(),
01438                                          false);
01439         int nrConcepts = GetIntInRange("numberConcepts", startConcept,
01440                                        concepts.size(), true);
01441         for (int c=startConcept ; c<startConcept+nrConcepts ; c++)
01442         {
01443             ILOG_NDC_PUSH("video " << v << " - " << concepts[c]);
01444             if (doInsert)
01445                 InsertOneSimTable(conn, vidSet, conceptSet, modelName,
01446                                   feature, v, concepts[c], &quidTab);
01447             else
01448                 CheckOneSimTable(conn, vidSet, conceptSet, modelName,
01449                                  feature, v, concepts[c], &quidTab);
01450             ILOG_NDC_POP;
01451         }
01452     }
01453 
01454     delete vidSet;
01455 }
01456 
01457 int
01458 mainMonetTest(int argc, char* argv[])
01459 {
01460     CmdOptions& options = CmdOptions::GetInstance();
01461     options.Initialise(false, false, true);
01462     options.AddOption(0, "saveQueries", "log_dir", "");
01463     options.AddOption(0, "startFile", "idx", "0");
01464     options.AddOption(0, "numberFiles", "nr", "-1");
01465     options.AddOption(0, "startConcept", "idx", "0");
01466     options.AddOption(0, "numberConcepts", "nr", "-1");
01467     options.AddOption(0, "startScore", "idx", "0");
01468     options.AddOption(0, "numberScores", "nr", "-1");
01469     options.AddOption(0, "", "nr", "-1");
01470     String usageStr = "cmd = \n\n";
01471     usageStr += "  sample host port db\n";
01472     usageStr += "  insertvideosets host port db\n";
01473     usageStr += "  insertvideofiles host port db videoSet.txt\n";
01474     usageStr += "  checkvideofiles host port db videoSet.txt\n";
01475     usageStr += "  insertsegmentation host port db videoSet.txt\n";
01476     usageStr += "  checksegmentation host port db videoSet.txt\n";
01477     usageStr += "  insertkeyframes host port db videoSet.txt\n";
01478     usageStr += "  checkkeyframes host port db videoSet.txt\n";
01479     usageStr += "  insertframearchive host port db videoSet.txt\n";
01480     usageStr += "  checkframearchive host port db videoSet.txt\n";
01481     usageStr += "  insertkeywords host port db videoSet.txt concepts.txt\n";
01482     usageStr += "  checkkeywords host port db videoSet.txt concepts.txt\n";
01483     usageStr += "  insertannotations host port db videoSet.txt concepts.txt quidClass\n";
01484     usageStr += "  checkannotations host port db videoSet.txt concepts.txt quidClass\n";
01485     usageStr += "  insertfeatures host port db videoSet.txt featureDef\n";
01486     usageStr += "  checkfeatures host port db videoSet.txt featureDef\n";
01487     usageStr += "  insertmodels host port db videoSet.txt concepts.txt model featureDef\n";
01488     usageStr += "  insertmodelsets host port db videoSet.txt concepts.txt model modelSet featureDef[s]\n";
01489     usageStr += "  insertsimilarities host port db videoSet.txt concepts.txt model feature\n";
01490     usageStr += "  checksimilarities host port db videoSet.txt concepts.txt model feature\n";
01491     if (! options.ParseArgs(argc, argv, usageStr, 4))
01492         return 1;
01493 
01494     ILOG_VAR(Impala.Samples.mainMonetTest);
01495 
01496     String cmd = options.GetArg(0);
01497     String host = options.GetArg(1);
01498     int port = atol(options.GetArg(2));
01499     String dbName = options.GetArg(3);
01500     ILOG_INFO("Connecting to " << dbName << " on " << host << ":" << port);
01501     Link::Monet::Connection conn(host, port, dbName);
01502     if (!conn.Valid())
01503         return 1;
01504     conn.SetSaveQueries(options.GetString("saveQueries"));
01505 
01506     Timer timer;
01507     if (cmd == "sample")
01508         DoSample(&conn);
01509     else if (cmd == "insertvideosets")
01510         DoInsertVideoSets(&conn);
01511     else if (cmd == "insertvideofiles")
01512         DoInsertVideoFiles(&conn);
01513     else if (cmd == "checkvideofiles")
01514         DoCheckVideoFiles(&conn);
01515     else if (cmd == "insertsegmentation")
01516         DoInsertSegmentation(&conn);
01517     else if (cmd == "checksegmentation")
01518         DoCheckSegmentation(&conn);
01519     else if (cmd == "insertkeyframes")
01520         DoInsertKeyframes(&conn);
01521     else if (cmd == "checkkeyframes")
01522         DoCheckKeyframes(&conn);
01523     else if (cmd == "insertframearchive")
01524         DoInsertFrameArchive(&conn);
01525     else if (cmd == "checkframearchive")
01526         DoCheckFrameArchive(&conn);
01527     else if (cmd == "insertkeywords")
01528         DoInsertKeywords(&conn);
01529     else if (cmd == "checkkeywords")
01530         DoCheckKeywords(&conn);
01531     else if (cmd == "insertannotations")
01532         DoInsertAnnotations(&conn);
01533     else if (cmd == "checkannotations")
01534         DoCheckAnnotations(&conn);
01535     else if (cmd == "insertfeatures")
01536         DoInsertFeatures(&conn);
01537     else if (cmd == "checkfeatures")
01538         DoCheckFeatures(&conn);
01539     else if (cmd == "insertmodels")
01540         DoInsertModels(&conn);
01541     else if (cmd == "insertmodelsets")
01542         DoInsertModelSets(&conn);
01543     else if (cmd == "insertsimilarities")
01544         DoSimilarities(&conn, true);
01545     else if (cmd == "checksimilarities")
01546         DoSimilarities(&conn, false);
01547     else ILOG_ERROR("Unknown cmd : " << cmd);
01548 
01549     ILOG_INFO("Did command in " << timer.SplitTimeStr());
01550     int nrError = ILogErrors::GetInstance().GetTotalNrErrors();
01551     ILOG_INFO("NrError = " << nrError);
01552     return nrError;
01553 }
01554 
01555 } // namespace MonetTest
01556 } // namespace Samples
01557 } // namespace Impala
01558 
01559 int
01560 main(int argc, char* argv[])
01561 {
01562     return Impala::Samples::MonetTest::mainMonetTest(argc, argv);
01563 }

Generated on Fri Mar 19 09:30:40 2010 for ImpalaSrc by  doxygen 1.5.1