00001 #ifndef Impala_Core_Trec_TrecTopic_h
00002 #define Impala_Core_Trec_TrecTopic_h
00003
00004 #include "Persistency/ImageSetRepository.h"
00005 #include "Persistency/VideoSetRepository.h"
00006 #include "Persistency/SegmentationRepository.h"
00007 #include "Persistency/KeyframesRepository.h"
00008 #include "Util/StringParser.h"
00009 #include "Core/ImageSet/MakeImageSet.h"
00010 #include "Core/VideoSet/MakeVideoSet.h"
00011 #include "Core/VideoSet/Segmentation.h"
00012 #include "Core/VideoSet/Keyframes.h"
00013 #include "Core/Trec/SearchTopic.h"
00014 #include "Util/XmlDoc.h"
00015 #include "Persistency/XmlFileReader.h"
00016
00017 namespace Impala
00018 {
00019 namespace Core
00020 {
00021 namespace Trec
00022 {
00023
00024 class TrecTopic : public SearchTopic, public Util::XmlDoc
00025 {
00026 public:
00027
00028 TrecTopic(String topicFile) : SearchTopic()
00029 {
00030 mYear = 0;
00031 ReadTopics(topicFile);
00032 }
00033
00034 TrecTopic(String topicFile, String year) : SearchTopic()
00035 {
00036 mYear = atol(year);
00037 ReadTopics(topicFile);
00038 LoadDataSets(year);
00039 }
00040
00041
00042
00043 String
00044 GetVideoExampleSrcFile(int topic, int example)
00045 {
00046 return mVideoSrc[topic][example];
00047 }
00048
00049
00050 String
00051 GetVideoExampleStart(int topic, int example)
00052 {
00053 return mVideoStart[topic][example];
00054 }
00055
00056 int
00057 GetVideoExampleStartFrame(int topic, int example)
00058 {
00059 String str = mVideoStart[topic][example];
00060 return ParseTimeFrame(str);
00061 }
00062
00063
00064 String
00065 GetVideoExampleEnd(int topic, int example)
00066 {
00067 return mVideoEnd[topic][example];
00068 }
00069
00070 int
00071 GetVideoExampleEndFrame(int topic, int example)
00072 {
00073 String str = mVideoEnd[topic][example];
00074 return ParseTimeFrame(str);
00075 }
00076
00077
00078 String
00079 GetVideoExampleDuration(int topic, int example)
00080 {
00081 double startT = ParseTime(mVideoStart[topic][example]);
00082 double endT = ParseTime(mVideoEnd[topic][example]);
00083 double duration = endT - startT;
00084 if (duration < 1.5)
00085 return "1.5";
00086 if (duration > 59.9)
00087 return "59.9";
00088 char buf[128];
00089 sprintf(buf, "%.3f", duration);
00090 return String(buf);
00091 }
00092
00093
00094
00095 String
00096 GenerateDataSets(String year)
00097 {
00098 mYear = atol(year);
00099 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00100 Util::Database* db = new Util::Database();
00101 String setBase = "trec" + year + "topic";
00102 ImageSet::ImageSet keySet(db, setBase + "_keyframes.txt", false);
00103 ImageSet::ImageSet thumbSet(db, setBase + "_thumbnails.txt", false);
00104 VideoSet::VideoSet vidSet(db, setBase + ".txt", false);
00105 #else // REPOSITORY_USED
00106 String setBase = "trec" + year + "topic";
00107 ImageSet::ImageSet keySet(setBase + "_keyframes.txt");
00108 ImageSet::ImageSet thumbSet(setBase + "_thumbnails.txt");
00109 VideoSet::VideoSet vidSet(setBase + ".txt");
00110 #endif // REPOSITORY_USED
00111 VideoSet::Segmentation segm(&vidSet, "");
00112 VideoSet::Keyframes keyframes(&vidSet, "");
00113 int videoId = 0;
00114 int shotId = 0;
00115 for (int i=0 ; i<NrTopic() ; i++)
00116 {
00117 ILOG_DEBUG("topic = " << mNum[i]);
00118 if (mVideoSrc[i].size() == 0)
00119 {
00120 ILOG_ERROR(mNum[i] << " has no videoExample, insert dummy!");
00121 exit(1);
00122 }
00123 for (int j=0 ; j<mVideoSrc[i].size() ; j++)
00124 {
00125 keySet.AddFile(GenExampleName(i, j) + ".jpg", "keyframes",
00126 mNum[i]);
00127 thumbSet.AddFile(GenExampleName(i, j) + ".jpg", "thumbnails",
00128 mNum[i]);
00129 vidSet.AddFile(GenExampleName(i, j) + ".mpg", ".", mNum[i]);
00130 int startFr = GetVideoExampleStartFrame(i, j);
00131 int endFr = GetVideoExampleEndFrame(i, j);
00132 String name = "shot" + MakeString(videoId+1) + "_"
00133 + MakeString(shotId+1);
00134 segm.Add(videoId, 0, endFr - startFr, name);
00135 keyframes.Add(videoId, shotId, (endFr - startFr) / 2,
00136 GenExampleName(i, j) + ".jpg");
00137 videoId++;
00138 shotId++;
00139 }
00140 }
00141 ILOG_DEBUG("Saving");
00142 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00143 keySet.SaveImageSet();
00144 thumbSet.SaveImageSet();
00145 vidSet.SaveVideoSet();
00146 bool binary = true;
00147 segm.Save("segmentation", binary);
00148 keyframes.Save("keyframes", binary);
00149 #else // REPOSITORY_USED
00150 Persistency::Locator baseLoc = vidSet.GetLocator();
00151 Persistency::ImageSetLocator keySetLoc(baseLoc, keySet.GetSetName());
00152 Persistency::ImageSetRepository().Add(keySetLoc, &keySet);
00153 Persistency::ImageSetLocator thumbSetLoc(baseLoc, thumbSet.GetSetName());
00154 Persistency::ImageSetRepository().Add(thumbSetLoc, &thumbSet);
00155 Persistency::VideoSetRepository().Add(baseLoc, &vidSet);
00156 bool binary = true;
00157 Persistency::SegmentationLocator sLoc(baseLoc, "segmentation");
00158 Persistency::SegmentationRepository().Add(sLoc, &segm);
00159 Persistency::KeyframesLocator kLoc(baseLoc, "keyframes");
00160 Persistency::KeyframesRepository().Add(kLoc, &keyframes);
00161 #endif // REPOSITORY_USED
00162 keyframes.WriteImageSets();
00163 ILOG_DEBUG("Loading");
00164 LoadDataSets(year);
00165 ILOG_DEBUG("Done");
00166 return setBase + ".txt";
00167 }
00168
00169 void
00170 LoadDataSets(String year)
00171 {
00172 String setBase = "trec" + year + "topic";
00173 mImages = ImageSet::MakeImageSet(setBase + "_keyframes.txt");
00174 mThumbnails = ImageSet::MakeImageSet(setBase + "_thumbnails.txt");
00175 mThumbnails->SetImageSrc(true, false, false);
00176
00177
00178 mVideos = VideoSet::MakeVideoSet(setBase + ".txt");
00179 mTopics = mImages;
00180 }
00181
00182 void
00183 Dump()
00184 {
00185 for (int t=0 ; t<mNum.size() ; t++)
00186 {
00187 std::cout << "num : " << mNum[t] << std::endl;
00188 std::cout << "text : " << mText[t] << std::endl;
00189 for (int v=0 ; v<mVideoSrc[t].size() ; v++)
00190 {
00191 std::cout << " video " << mVideoSrc[t][v] << " from "
00192 << mVideoStart[t][v] << " to "
00193 << mVideoEnd[t][v] << std::endl;
00194 }
00195 }
00196 }
00197
00198 private:
00199
00200 double
00201 ParseTime(String str)
00202 {
00203 Util::StringParser p(str);
00204 int minutes = p.GetInt('m');
00205 p.AdvanceP();
00206 double seconds = p.GetDouble('s');
00207 seconds += minutes * 60;
00208 return seconds;
00209 }
00210
00211 int
00212 ParseTimeFrame(String str)
00213 {
00214 double seconds = ParseTime(str);
00215 double fractions = 29.97;
00216 if (mYear == 0)
00217 ILOG_ERROR("Need year to compute frame");
00218 if (mYear >= 2007)
00219 fractions = 25;
00220 return seconds * fractions;
00221 }
00222
00223 void
00224 ReadTopics(String xmlFile)
00225 {
00226 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00227 Util::Database& db = Util::Database::GetInstance();
00228 Util::IOBuffer* ioBuf = db.GetIOBuffer(xmlFile, true, true, "");
00229 #else // REPOSITORY_USED
00230 Persistency::FileLocator loc(xmlFile);
00231 typedef Persistency::RepositoryInFileSystem FS;
00232 Persistency::File file = FS::GetInstance().GetFile(loc, false, false);
00233 Util::IOBuffer* ioBuf = file.GetReadBuffer(true, "");
00234 #endif // REPOSITORY_USED
00235 Persistency::XmlFileReader reader;
00236 DOMDocument* doc = reader.Read(xmlFile, ioBuf);
00237 delete ioBuf;
00238 DOMNode* root = GetChildNode(doc, "videoTopics", true);
00239 std::vector<DOMNode*> topics = GetChildNodes(root, "videoTopic");
00240 ILOG_INFO("topics.size = " << topics.size());
00241 int curFileIdx = 0;
00242 for (int t=0 ; t<topics.size() ; t++)
00243 {
00244 DOMNode* topic = topics[t];
00245 String num = GetAttributeValue(topic, "num");
00246 ILOG_DEBUG("num = " << num);
00247 mNum.push_back(num);
00248 DOMNode* textDesc = GetChildNode(topic, "textDescription", true);
00249 String text = GetAttributeValue(textDesc, "text");
00250 ILOG_DEBUG(" text = " << text);
00251 mText.push_back(text);
00252 std::vector<String> src;
00253 std::vector<String> start;
00254 std::vector<String> end;
00255 std::vector<int> fileIdx;
00256 std::vector<DOMNode*> vidEx = GetChildNodes(topic, "videoExample");
00257 ILOG_DEBUG("nr video examples = " << vidEx.size());
00258 for (int i=0 ; i<vidEx.size() ; i++)
00259 {
00260 DOMNode* example = vidEx[i];
00261 src.push_back(GetAttributeValue(example, "src"));
00262 start.push_back(GetAttributeValue(example, "start"));
00263 end.push_back(GetAttributeValue(example, "stop"));
00264 fileIdx.push_back(curFileIdx++);
00265 }
00266 mVideoSrc.push_back(src);
00267 mVideoStart.push_back(start);
00268 mVideoEnd.push_back(end);
00269 mFileIdx.push_back(fileIdx);
00270 }
00271 }
00272
00273 String
00274 GenExampleName(int topic, int example)
00275 {
00276 return FileNameBase(mVideoSrc[topic][example]) + "_" +
00277 mVideoStart[topic][example] + "_" + mVideoEnd[topic][example];
00278 }
00279
00280
00281
00282 int mYear;
00283
00284 std::vector< std::vector<String> > mVideoSrc;
00285 std::vector< std::vector<String> > mVideoStart;
00286 std::vector< std::vector<String> > mVideoEnd;
00287 std::vector< std::vector<int> > mFileIdx;
00288
00289 ILOG_VAR_DEC;
00290 };
00291
00292 ILOG_VAR_INIT(TrecTopic, Core.Trec);
00293
00294 }
00295 }
00296 }
00297
00298 #endif