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

FeatureTableRepositoryInMonet.h

Go to the documentation of this file.
00001 #ifndef Impala_Persistency_FeatureTableRepositoryInMonet_h
00002 #define Impala_Persistency_FeatureTableRepositoryInMonet_h
00003 
00004 #include "Persistency/VideoSetRepository.h"
00005 #include "Persistency/RepositoryInMonetDB.h"
00006 #include "Core/Feature/FeatureTable.h"
00007 #include "Persistency/FeatureLocator.h"
00008 #include "Util/BinHex.h"
00009 
00010 namespace Impala
00011 {
00012 namespace Persistency
00013 {
00014 
00015 
00016 class FeatureTableRepositoryInMonet
00017 {
00018 public:
00019 
00020     typedef Link::Monet::Connection Connection;
00021     typedef Core::Feature::FeatureTable FeatureTable;
00022 
00023     FeatureTableRepositoryInMonet()
00024     {
00025     }
00026 
00027     FeatureTable*
00028     Get(const FeatureLocator& loc)
00029     {
00030         if (loc.GetIsIndex())
00031         {
00032             ILOG_ERROR("Index not implemented (yet)");
00033             return 0;
00034         }
00035         if (loc.GetWalkType() != "Frames")
00036         {
00037             ILOG_ERROR("Only implemented for Frames");
00038             return 0;
00039         }
00040 
00041         Connection* conn = RepMonet().GetConnection(loc);
00042 
00043         String vidSetBase = FileNameBase(loc.GetDataSet());
00044         Core::Feature::FeatureDefinition def = loc.GetFeatureDef();
00045         Core::VideoSet::VideoSet* vidSet = VideoSetRepository().Get(loc);
00046         int fileIdx = vidSet->GetFileId(loc.GetContainer());
00047         Quid vidQuid = vidSet->GetQuidVideo(fileIdx, true);
00048 
00049         String query =
00050             "select fr.fragment_start, fv.vector \
00051              from feature_vector fv, fragment fr, file f, feature fe \
00052              where  f.quid = " + MakeString(vidQuid) + " and \
00053                     fr.media_id = f.media_id and \
00054                     fr.fragment_length = 1 and \
00055                     fr.keyframe = false and \
00056                     fe.feature_name = '" + loc.GetFeatureString() + "' and \
00057                     fe.feature_id = fv.feature_id and \
00058                     fv.fragment_id = fr.fragment_id and \
00059                     fv.file_id = f.file_id \
00060              order by fr.fragment_start;";
00061 
00062         MapiHdl hdl = conn->QueryPartStart(query);
00063         if (hdl == 0)
00064             return 0;
00065 
00066         int* start = 0;
00067         int startSize = 0;
00068         if (!conn->QueryPartFetchInt(hdl, 0, start, startSize))
00069             return 0;
00070 
00071         std::vector<String> strs;
00072         if (!conn->QueryPartFetchString(hdl, 1, strs))
00073             return 0;
00074 
00075         conn->QueryPartEnd(hdl);
00076 
00077         FeatureTable* res = 0;
00078         for (int i=0 ; i<startSize ; i++)
00079         {
00080             Quid q = vidSet->GetQuidFrame(fileIdx, start[i], true);
00081 
00082             size_t dataSize;
00083             UInt8* data = Util::Hex2Bin(strs[i], dataSize);        
00084             int n = dataSize / sizeof(Real64);
00085             Real64* realData = (Real64*) data;
00086             FeatureTable::VectorReal64 v(n, realData, true);
00087 
00088             if (res == 0)
00089                 res = new FeatureTable(loc.GetFeatureDef(), strs.size(), n);
00090             res->Add(q, v);
00091             delete data;
00092         }
00093 
00094         if (res == 0)
00095             ILOG_ERROR("No features found");
00096         delete start;
00097         return res;
00098     }
00099 
00100     void
00101     Add(const FeatureLocator& loc, FeatureTable* tab)
00102     {
00103         if (loc.GetIsIndex())
00104         {
00105             ILOG_ERROR("Index not implemented (yet)");
00106             return;
00107         }
00108         if (loc.GetWalkType() != "Frames")
00109         {
00110             ILOG_ERROR("Only implemented for Frames");
00111             return;
00112         }
00113 
00114         Connection* conn = RepMonet().GetConnection(loc);
00115 
00116         String vidSetBase = FileNameBase(loc.GetDataSet());
00117         Core::VideoSet::VideoSet* vidSet = VideoSetRepository().Get(loc);
00118         int fileIdx = vidSet->GetFileId(loc.GetContainer());
00119         Quid vidQuid = vidSet->GetQuidVideo(fileIdx, true);
00120 
00121         String q = "select i_add_feature('" + loc.GetFeatureString() + "');";
00122         conn->Query(q, false, false);
00123 
00124         conn->Query("delete from i_bulk_feature_vector;", false, false);
00125         int totalSize = tab->Size();
00126         int bulkSize = 2000;
00127         Timer timer;
00128         for (int i=0 ; i<totalSize ; i+=bulkSize)
00129         {
00130             int left = totalSize - i;
00131             int nr = (left > bulkSize) ? bulkSize : left;
00132             String q = "copy " + MakeString(nr) +
00133                 " records into i_bulk_feature_vector from stdin" +
00134                 " using delimiters ' ';\n";
00135             for (int j=i ; j<i+nr ; j++)
00136             {
00137                 Quid frameQuid = tab->Get1(j);
00138                 if (QuidClass(frameQuid) != QUID_CLASS_FRAME)
00139                     ILOG_ERROR("Quid is not a frame");
00140                 int frameNr = QuidId(frameQuid);
00141                 FeatureTable::VectorReal64 v = tab->Get2(j);
00142                 size_t dataSize = v.Size() * sizeof(Real64);
00143                 UInt8* data = (UInt8*) v.GetData();
00144                 String hexData = Util::Bin2Hex(data, dataSize);
00145 
00146                 q += MakeString(vidQuid) + " " + MakeString(frameNr) + " "
00147                     + hexData + "\n";
00148             }
00149             conn->Query(q, false, false);
00150             ILOG_INFO("Did bulk " << i << " at " << timer.SplitTime());
00151 
00152             conn->Query("insert into feature_vector (fragment_id, file_id, \
00153                                                      feature_id, vector) \
00154                          select fr.fragment_id, f.file_id, fe.feature_id, \
00155                                 bfv.vector \
00156                          from i_bulk_feature_vector bfv, file f, \
00157                               fragment fr, feature fe \
00158                          where bfv.quid = f.quid and \
00159                                f.media_id = fr.media_id and \
00160                                fr.fragment_start = bfv.fragment_start and \
00161                                fr.fragment_length = 1 and \
00162                                fr.keyframe = false and \
00163                                fe.feature_name = '" + loc.GetFeatureString() + "' \
00164                          order by bfv.fragment_start;", false, false);
00165             ILOG_INFO("Did insert select at " << timer.SplitTime());
00166             conn->Query("delete from i_bulk_feature_vector;", false, false);
00167         }
00168     }
00169 
00170 private:
00171 
00172     RepositoryInMonetDB&
00173     RepMonet()
00174     {
00175         return RepositoryInMonetDB::GetInstance();
00176     }
00177 
00178     ILOG_VAR_DEC;
00179 };
00180 
00181 ILOG_VAR_INIT(FeatureTableRepositoryInMonet, Impala.Persistency);
00182 
00183 } // namespace Persistency
00184 } // namespace Impala
00185 
00186 #endif

Generated on Fri Mar 19 09:31:42 2010 for ImpalaSrc by  doxygen 1.5.1