test-JunctionHOG.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "Component/ModelManager.H"
00039 #include "Image/DrawOps.H"
00040 #include "Image/Kernels.H"
00041 #include "Image/CutPaste.H"
00042 #include "Image/ColorOps.H"
00043 #include "Image/FilterOps.H"
00044 #include "Raster/Raster.H"
00045 #include "Media/FrameSeries.H"
00046 #include "Util/Timer.H"
00047 #include "Util/CpuTimer.H"
00048 #include "Util/StringUtil.H"
00049 #include "Features/JunctionHOG.H"
00050 #include "Learn/SVMClassifier.H"
00051 #include "rutz/rand.h"
00052 #include "rutz/trace.h"
00053
00054 #include <math.h>
00055 #include <fcntl.h>
00056 #include <limits>
00057 #include <string>
00058 #include <cstdio>
00059 #include <cstdlib>
00060 #include <dirent.h>
00061
00062 #define TRAIN_WIDTH 160
00063 #define TRAIN_HEIGHT 160
00064 #define SAMPLE_WIDTH 160
00065 #define SAMPLE_HEIGHT 160
00066
00067 #define TEST_SIZE 20
00068
00069 std::vector<std::string> readDir(std::string inName)
00070 {
00071 DIR *dp = opendir(inName.c_str());
00072 if(dp == NULL)
00073 {
00074 LFATAL("Directory does not exist %s",inName.c_str());
00075 }
00076 dirent *dirp;
00077 std::vector<std::string> fList;
00078 while ((dirp = readdir(dp)) != NULL ) {
00079 if (dirp->d_name[0] != '.')
00080 fList.push_back(inName + '/' + std::string(dirp->d_name));
00081 }
00082 LINFO("%"ZU" files in the directory\n", fList.size());
00083 LINFO("file list : \n");
00084 for (unsigned int i=0; i<fList.size(); i++)
00085 LINFO("\t%s", fList[i].c_str());
00086 std::sort(fList.begin(),fList.end());
00087 return fList;
00088 }
00089
00090
00091
00092 int main(const int argc, const char **argv)
00093 {
00094
00095 MYLOGVERB = LOG_INFO;
00096 ModelManager manager("Test JunctionHOG");
00097
00098
00099 rutz::urand rgen(time((time_t*)0)+getpid());
00100
00101
00102 SVMClassifier svm;
00103
00104 if (manager.parseCommandLine(
00105 (const int)argc, (const char**)argv, "<usejunctions> <outputdir> <obj1dir> ... <objNdir>", 4, 20) == false)
00106 return 0;
00107
00108 manager.start();
00109
00110
00111
00112 uint numCategories = manager.numExtraArgs()-2;
00113 int useJunc = atoi(manager.getExtraArg(0).c_str());
00114 std::string outputDir = manager.getExtraArg(1);
00115 HistogramOfGradients *hog;
00116 bool normalizeHistogram = true;
00117 bool fixedHistogram = true;
00118 Dims cellSize = Dims(8,8);
00119
00120 if(useJunc == 1)
00121 {
00122 LINFO("Creating JunctionHOG class, useJunc %d, orig str %s",useJunc,manager.getExtraArg(0).c_str());
00123 hog = new JunctionHOG(normalizeHistogram,cellSize,fixedHistogram);
00124 }
00125 else
00126 {
00127 LINFO("Creating HistogramOfGradients class, useJunc %d, orig str %s",useJunc,manager.getExtraArg(0).c_str());
00128 hog = new HistogramOfGradients(normalizeHistogram,cellSize,fixedHistogram);
00129 }
00130
00131 for(uint i=0;i<numCategories;i++)
00132 {
00133
00134 uint argIdx = i+2;
00135 uint idx = i+1;
00136 std::string fName = outputDir;
00137 fName.append(sformat("/Obj%u.out",idx));
00138 std::string inputDir = manager.getExtraArg(argIdx);
00139 LINFO("Will append files from dir %s to output file %s",inputDir.c_str(),fName.c_str());
00140 std::vector<std::string> fileList = readDir(inputDir);
00141
00142 for(uint f=0;f<fileList.size();f++)
00143 {
00144 Image<PixRGB<byte> > img = Raster::ReadRGB(fileList[f]);
00145 Image<float> lum,rg,by;
00146 getLAB(img, lum, rg, by);
00147 std::vector<float> hist=hog->createHistogram(lum,rg,by);
00148 svm.train(fName,idx,hist);
00149 }
00150 }
00151 LINFO("Use the files to test the classification performance of this feature vector");
00152 manager.stop();
00153
00154 }
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165