00001 #ifndef Impala_Persistency_AnnotationTableRepositoryInMonet_h
00002 #define Impala_Persistency_AnnotationTableRepositoryInMonet_h
00003
00004 #include "Persistency/VideoSetRepository.h"
00005 #include "Persistency/RepositoryInMonetDB.h"
00006 #include "Core/Table/AnnotationTable.h"
00007 #include "Persistency/AnnotationTableLocator.h"
00008
00009 namespace Impala
00010 {
00011 namespace Persistency
00012 {
00013
00014
00015 class AnnotationTableRepositoryInMonet
00016 {
00017 public:
00018
00019 typedef Link::Monet::Connection Connection;
00020 typedef Core::Table::AnnotationTable AnnotationTable;
00021
00022 AnnotationTableRepositoryInMonet()
00023 {
00024 mAnnotator = "fabchannel";
00025 }
00026
00027 AnnotationTable*
00028 Get(const AnnotationTableLocator& loc)
00029 {
00030 if (loc.GetQuidClass() != QUID_CLASS_FRAME)
00031 {
00032 ILOG_ERROR("Only implemented for Frames");
00033 return 0;
00034 }
00035
00036 Connection* conn = RepMonet().GetConnection(loc);
00037
00038 String vidSetBase = FileNameBase(loc.GetDataSet());
00039 String query =
00040 "select f.quid, fr.fragment_start, a.relevance \
00041 from fragment fr, file f, video_set vs, video_sets vss, \
00042 keyword_sets kss, keyword_set ks, keyword k, annotator a, \
00043 annotation a \
00044 where vss.set_name = '" + vidSetBase + "' and \
00045 vs.video_sets_id = vss.video_sets_id and \
00046 f.file_id = vs.file_id and \
00047 fr.media_id = f.media_id and \
00048 fr.fragment_length = 1 and \
00049 fr.keyframe = false and \
00050 kss.set_name = '" + FileNameBase(loc.GetConceptSet()) + "' and \
00051 kss.video_sets_id = vss.video_sets_id and \
00052 ks.keyword_sets_id = kss.keyword_sets_id and \
00053 ks.keyword_id = k.keyword_id and \
00054 k.keyword_name = '" + loc.GetKeyword() + "' and \
00055 a.annotator_name = '" + mAnnotator + "' and \
00056 a.fragment_id = fr.fragment_id and \
00057 a.keyword_id = k.keyword_id and \
00058 a.approved = true \
00059 order by f.quid, fr.fragment_start;";
00060
00061 MapiHdl hdl = conn->QueryPartStart(query);
00062 if (hdl == 0)
00063 return 0;
00064
00065 Quid* quid = 0;
00066 int quidSize = 0;
00067 if (!conn->QueryPartFetchULL(hdl, 0, quid, quidSize))
00068 return 0;
00069
00070 int* start = 0;
00071 int startSize = 0;
00072 if (!conn->QueryPartFetchInt(hdl, 1, start, startSize))
00073 return 0;
00074
00075 double* rel = 0;
00076 int relSize;
00077 if (!conn->QueryPartFetchDouble(hdl, 2, rel, relSize))
00078 return 0;
00079
00080 conn->QueryPartEnd(hdl);
00081
00082 Core::VideoSet::VideoSet* vidSet = VideoSetRepository().Get(loc);
00083 AnnotationTable* anno = new AnnotationTable(loc.GetKeyword(), quidSize);
00084 for (int i=0 ; i<quidSize ; i++)
00085 {
00086 int vidId = QuidId(quid[i]);
00087 Quid q = vidSet->GetQuidFrame(vidId, start[i], false);
00088 if (rel[i] == 1.0)
00089 anno->AddPositive(q);
00090 else if (rel[i] == 0.0)
00091 anno->AddNegative(q);
00092 else
00093 anno->AddSkip(q);
00094 }
00095
00096 delete quid;
00097 delete start;
00098 delete rel;
00099 return anno;
00100 }
00101
00102 void
00103 Add(const AnnotationTableLocator& loc, AnnotationTable* tab)
00104 {
00105 if (loc.GetQuidClass() != QUID_CLASS_FRAME)
00106 {
00107 ILOG_ERROR("Only implemented for Frames");
00108 return;
00109 }
00110
00111 Connection* conn = RepMonet().GetConnection(loc);
00112
00113 String vidSetBase = FileNameBase(loc.GetDataSet());
00114 Core::VideoSet::VideoSet* vidSet = VideoSetRepository().Get(loc);
00115
00116 conn->Query("select i_add_annotator('+" + mAnnotator + "', 'the boss', true);",
00117 false, false);
00118
00119 conn->Query("delete from i_bulk_annotation;", false, false);
00120 Timer timer;
00121 String keyword = loc.GetKeyword();
00122 ILOG_INFO("keyword " << keyword << " has " << tab->Size() << " annos");
00123 String date("2007/08/22-11:00:00");
00124 int nr = tab->Size();
00125 String q = "copy " + MakeString(nr) +
00126 " records into i_bulk_annotation from stdin using delimiters ' ';\n";
00127 for (int i=0 ; i<nr ; i++)
00128 {
00129 Quid frameQuid = tab->Get1(i);
00130 int vidId = QuidObject(frameQuid);
00131 Quid vidQuid = vidSet->GetQuidVideo(vidId, false);
00132 int frameNr = QuidId(frameQuid);
00133 Real64 relevance = 0.5;
00134 if (tab->IsPositive(i))
00135 relevance = 1.0;
00136 else if (tab->IsNegative(i))
00137 relevance = 0.0;
00138 Real64 confidence = 1.0;
00139 q += keyword + " " + MakeString(vidQuid) + " " + MakeString(frameNr)
00140 + " " + MakeString(relevance) + " " + MakeString(confidence)
00141 + " " + date + "\n";
00142 }
00143 conn->Query(q, false, false);
00144 ILOG_INFO("Did bulk in " << timer.SplitTime());
00145 conn->Query("insert into annotation (fragment_id, keyword_id, \
00146 annotator_id, \
00147 relevance, confidence, \
00148 annotation_date, approved) \
00149 select fr.fragment_id, k.keyword_id, a.annotator_id, \
00150 ba.relevance, ba.confidence, ba.annotation_date, \
00151 a.approved \
00152 from i_bulk_annotation ba, media m, fragment fr, \
00153 video_sets vss, keyword_sets kss, keyword_set ks, \
00154 keyword k, annotator a \
00155 where ba.quid = m.quid and \
00156 m.media_id = fr.media_id and \
00157 fr.fragment_start = ba.fragment_start and \
00158 fr.fragment_length = 1 and \
00159 fr.keyframe = false and \
00160 vss.set_name = '" + vidSetBase + "' and \
00161 kss.set_name = '" + FileNameBase(loc.GetConceptSet()) + "' and \
00162 kss.video_sets_id = vss.video_sets_id and \
00163 ks.keyword_sets_id = kss.keyword_sets_id and \
00164 ks.keyword_id = k.keyword_id and \
00165 k.keyword_name = ba.keyword_name and \
00166 a.annotator_name = '" + mAnnotator + "' \
00167 order by ba.quid, ba.fragment_start;", false, false);
00168 conn->Query("delete from i_bulk_annotation;", false, false);
00169 }
00170
00171 private:
00172
00173 RepositoryInMonetDB&
00174 RepMonet()
00175 {
00176 return RepositoryInMonetDB::GetInstance();
00177 }
00178
00179 String mAnnotator;
00180
00181 ILOG_VAR_DEC;
00182 };
00183
00184 ILOG_VAR_INIT(AnnotationTableRepositoryInMonet, Impala.Persistency);
00185
00186 }
00187 }
00188
00189 #endif