00001 #ifndef Impala_Core_ImageSet_InterestPointProc_h
00002 #define Impala_Core_ImageSet_InterestPointProc_h
00003
00004 #include "Core/ImageSet/Reporter.h"
00005 #include "Core/ImageSet/MakeImageSet.h"
00006 #include "Core/Array/MakeFromValue.h"
00007 #include "Core/Array/ArrayListDelete.h"
00008 #include "Core/Array/WriteRaw.h"
00009 #include "Core/Array/Mirror.h"
00010 #include "Persistency/FeatureTableRepository.h"
00011 #include "Core/Feature/InterestPointFeature.h"
00012
00013 namespace Impala
00014 {
00015 namespace Core
00016 {
00017 namespace ImageSet
00018 {
00019
00020 class InterestPointProc : public Listener
00021 {
00022 public:
00023
00024 typedef Persistency::FeatureLocator FeatureLocator;
00025 typedef Persistency::FeatureTableRepository FeatureTableRepository;
00026 typedef Persistency::FileLocator FileLocator;
00027 typedef Persistency::File File;
00028
00029 InterestPointProc(Reporter* reporter, CmdOptions& options)
00030 : mInterestPointFeature(options), mOptions(options), mNoZIP(false),
00031 mSkipThisFolder(false)
00032 {
00033 mNoZIP = options.GetBool("noZIP");
00034 mCodebookSet = "";
00035 mFeatureTableResult.SetInfo("codebook=" + options.GetString("codebook"));
00036 mInterestPointFeature.SetResultOutput(&mFeatureTableResult);
00037 if (options.GetNrArg() > 2)
00038 mCodebookSet = options.GetArg(2);
00039 if (mNoZIP)
00040 {
00041 ILOG_INFO("NOT creating ZIP output files");
00042 }
00043 else
00044 {
00045 ILOG_INFO("Creating ZIP output files");
00046 }
00047 }
00048
00049 static void
00050 AddCmdOptions()
00051 {
00052 Feature::InterestPointFeature::AddCmdOptions();
00053
00054 CmdOptions& options = CmdOptions::GetInstance();
00055 options.AddOption(0, "noZIP", "", "0");
00056 options.AddOption(0, "interestPointPostProcess", "command", "");
00057 options.AddOption(0, "useDetectorCache", "name", "");
00058 options.AddOption(0, "mirror", "mode", "0");
00059 }
00060
00061 virtual void
00062 HandleNewWalk(ImageSet* is)
00063 {
00064 if (!mOptions.GetString("codebook").empty())
00065 {
00066 Database::RawDataSet* bookSet = is;
00067 if (!mCodebookSet.empty())
00068 {
00069 ILOG_INFO("Using separate codebook set: " << mCodebookSet);
00070 bookSet = Database::MakeRawDataSet(mCodebookSet);
00071 }
00072
00073 mInterestPointFeature.SetCodebook(mOptions.GetString("codebook"),
00074 bookSet->GetLocator());
00075 mFeatureTableResult.SetFeatureDefinition(
00076 mInterestPointFeature.GetFeatureDefinition());
00077 }
00078 }
00079
00080 virtual void
00081 HandleNewWalk(ImageSet* is, String walkType)
00082 {
00083 mWalkType = walkType;
00084 }
00085
00086 virtual void
00087 HandleNewDir(ImageSet* is, int dirId)
00088 {
00089 ILOG_INFO("HandleNewDir: " << dirId);
00090 int fileId = is->GetFirstFileId(dirId);
00091 mTempFilenames.clear();
00092 mSkipThisFolder = false;
00093 if (CmdOptions::GetInstance().GetBool("override"))
00094 return;
00095
00096 if (!mNoZIP)
00097 {
00098
00099 FeatureLocator loc(is->GetLocator(), false, false, "",
00100 mInterestPointFeature.GetFeatureName(),
00101 is->GetContainer(dirId));
00102 loc.ExtendSuffix(".zip");
00103 if (FeatureTableRepository().Exists(loc))
00104 {
00105 mSkipThisFolder = true;
00106 ILOG_WARNING("Skipping folder " << dirId <<
00107 "; ZIP file already exists");
00108 }
00109 return;
00110 }
00111
00112 if (mInterestPointFeature.mClusterInput)
00113 {
00114
00115 Feature::FeatureDefinition def = mInterestPointFeature.GetFeatureName();
00116 def.AddParameter("clusterinput", "2");
00117 FeatureLocator loc(is->GetLocator(), false, false, "",
00118 def.AsString(), is->GetContainer(dirId));
00119 if (FeatureTableRepository().Exists(loc))
00120 {
00121 mSkipThisFolder = true;
00122 ILOG_WARNING("Skipping folder " << dirId <<
00123 "; FeatureData:ClusterInput file already exists");
00124 }
00125 }
00126 else
00127 {
00128
00129 String f = mInterestPointFeature.GetFeatureDefinition().AsString();
00130 FeatureLocator loc(is->GetLocator(), false, false, "", f,
00131 is->GetContainer(dirId));
00132 if (FeatureTableRepository().Exists(loc))
00133 {
00134 mSkipThisFolder = true;
00135 ILOG_WARNING("Skipping folder " << dirId <<
00136 "; FeatureData file already exists");
00137 }
00138 }
00139 }
00140
00141 virtual void
00142 HandleNewFile(ImageSet* is, int fileId, Array::Array2dVec3UInt8* im)
00143 {
00144 if (mSkipThisFolder)
00145 return;
00146 int dirId = is->GetDirIdOfFile(fileId);
00147
00148 String fileName = is->GetAsPath(fileId);
00149
00150 String outputFilename =
00151 PathJoin(GetTmpPath(), is->GetFileBase(fileId) + "." +
00152 mInterestPointFeature.GetFeatureName());
00153 ILOG_DEBUG(" outputFilename=" << outputFilename << "; fileId=" << fileId
00154 << "; dirId=" << dirId);
00155 if (im == 0)
00156 {
00157 ILOG_ERROR("No image " << fileName);
00158 return;
00159 }
00160
00161 Util::IOBuffer* detectorCache = 0;
00162 if (mOptions.GetString("useDetectorCache") != "")
00163 {
00164 String udc = mOptions.GetString("useDetectorCache");
00165 String detectorCacheFilename = PathJoin
00166 ("FeatureData", udc, is->GetContainerDirOfFile(fileId),
00167 is->GetFileBase(fileId) + "." + udc);
00168 FileLocator fLoc(is->GetLocator(), detectorCacheFilename);
00169 File file = RepositoryGetFile(fLoc, false, false);
00170 detectorCache = file.GetReadBuffer();
00171 ILOG_INFO("detectorCacheFilename = " << detectorCacheFilename);
00172 }
00173 if (mNoZIP)
00174 {
00175 outputFilename = "";
00176 }
00177 mInterestPointFeature.mClusterInputImage =
00178 QuidObj(is->GetQuidImage(fileId)).ToString();
00179
00180 if (mOptions.GetInt("mirror"))
00181 {
00182 if (mOptions.GetInt("mirror") == 1)
00183 {
00184 Array::MirrorY(im, im);
00185 }
00186 }
00187
00188 if (!mInterestPointFeature.FindInterestPoints(mOptions, im,
00189 is->GetQuidImage(fileId), outputFilename, true, detectorCache))
00190 {
00191 ILOG_ERROR("Extracting interest points/descriptors failed: " <<
00192 fileName);
00193 }
00194 if (detectorCache)
00195 delete detectorCache;
00196
00197
00198 if (mOptions.GetString("interestPointPostProcess") != "")
00199 {
00200 String cmd = mOptions.GetString("interestPointPostProcess")
00201 + " " + outputFilename;
00202 ILOG_INFO("Post-processing = " << cmd);
00203 system(cmd.c_str());
00204 }
00205 if (!mNoZIP)
00206 {
00207 mTempFilenames.push_back(outputFilename);
00208 }
00209 }
00210
00211 virtual void
00212 HandleDoneDir(ImageSet* is, int dirId)
00213 {
00214 if (mSkipThisFolder)
00215 return;
00216
00217 FeatureLocator loc(is->GetLocator(), false, false, "", "empty",
00218 is->GetContainer(dirId));
00219
00220 if (mFeatureTableResult.GetTableSize() != 0)
00221 {
00222 for(int i = 0; i < mFeatureTableResult.Size(); i++)
00223 {
00224 loc.SetFeatureDef(mFeatureTableResult.GetFeatureDefinition(i));
00225 FeatureTableRepository().Add(loc, mFeatureTableResult.GetTable(i));
00226 }
00227 mFeatureTableResult.Delete();
00228 }
00229
00230
00231 if (!mNoZIP)
00232 {
00233 String featureName = mInterestPointFeature.GetFeatureName();
00234 String fName = PathJoin(GetTmpPath(), "tmp-" + featureName +
00235 "-" + MakeString(dirId) + ".zip");
00236 String txtName = StringReplace(fName, ".zip", ".txt");
00237 loc.SetFeatureString(featureName + ".zip");
00238 String serverName = FeatureTableRepository().ExposeFilePath(loc,
00239 true);
00240 serverName = StringReplace(serverName, ".zip.tab", ".tab.zip");
00241
00242
00243 std::ofstream fs(txtName.c_str());
00244 for (std::vector<String>::iterator iter = mTempFilenames.begin();
00245 iter != mTempFilenames.end(); iter++)
00246 {
00247 fs << (*iter) << std::endl;
00248 }
00249 fs.close();
00250
00251 String cmd = "zip ";
00252 cmd += fName;
00253 cmd += " -@ <";
00254 cmd += txtName;
00255
00256 if (!serverName.empty())
00257 {
00258
00259
00260 system(cmd.c_str());
00261 }
00262
00263
00264 if (std::remove(txtName.c_str()) != 0)
00265 {
00266 ILOG_WARN("Could not cleanup intermediate file: " << txtName);
00267 }
00268
00269
00270 File srcFile = Persistency::RepositoryGetFile(fName, false, false);
00271 FileLocator dstLoc(is->GetLocator(), serverName);
00272 File dstFile = Persistency::RepositoryGetFile(dstLoc, true, false);
00273 dstFile.CopyFrom(srcFile);
00274
00275
00276 if (std::remove(fName.c_str()) != 0)
00277 {
00278 ILOG_WARN("Could not cleanup temporary ZIP file: " << fName);
00279 }
00280 }
00281
00282
00283 if (mInterestPointFeature.mClusterInput &&
00284 mInterestPointFeature.mClusterInputData)
00285 {
00286 loc.SetFeatureDef(mInterestPointFeature.mClusterInputData->GetFeatureDefinition());
00287 FeatureTableRepository().Add(loc, mInterestPointFeature.mClusterInputData);
00288 delete mInterestPointFeature.mClusterInputData;
00289 mInterestPointFeature.mClusterInputData = 0;
00290 }
00291
00292
00293 for (std::vector<String>::iterator iter = mTempFilenames.begin();
00294 iter != mTempFilenames.end(); iter++)
00295 {
00296 if (std::remove((*iter).c_str()) != 0)
00297 {
00298 ILOG_WARN("Could not cleanup intermediate file: " << (*iter));
00299 }
00300 }
00301 mTempFilenames.clear();
00302 ILOG_INFO("HandleDoneDir: " << dirId);
00303 }
00304
00305 private:
00306
00307 Feature::FeatureTableResult mFeatureTableResult;
00308 Feature::InterestPointFeature mInterestPointFeature;
00309 CmdOptions& mOptions;
00310 String mCodebookSet;
00311 std::vector<String> mTempFilenames;
00312 bool mNoZIP;
00313 bool mSkipThisFolder;
00314 String mWalkType;
00315
00316 ILOG_VAR_DEC;
00317 };
00318
00319 ILOG_VAR_INIT(InterestPointProc, Impala.Core.ImageSet);
00320
00321 }
00322 }
00323 }
00324
00325 #endif