test-ObjRec.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
00039 #include "Component/ModelManager.H"
00040 #include "Image/Image.H"
00041 #include "Image/Transforms.H"
00042 #include "Media/FrameSeries.H"
00043 #include "Transport/FrameInfo.H"
00044 #include "Raster/GenericFrame.H"
00045 #include "ObjRec/ObjRecSPM.H"
00046
00047 #include "Media/TestImages.H"
00048 #include "GUI/DebugWin.H"
00049
00050 int getObjName(std::vector<std::string> &objNames, const std::string &objName)
00051 {
00052
00053
00054 uint i=0;
00055 for(i=0; i<objNames.size(); i++)
00056 if (objNames[i] == objName)
00057 return i;
00058
00059 objNames.push_back(objName);
00060 return i;
00061 }
00062
00063
00064 int main(const int argc, const char **argv)
00065 {
00066
00067 MYLOGVERB = LOG_INFO;
00068 ModelManager *mgr = new ModelManager("Test ObjRec");
00069
00070 nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(*mgr));
00071 mgr->addSubComponent(ofs);
00072
00073 nub::ref<ObjRecSPM> objRec(new ObjRecSPM(*mgr));
00074
00075 mgr->addSubComponent(objRec);
00076
00077 mgr->exportOptions(MC_RECURSE);
00078
00079 if (mgr->parseCommandLine(
00080 (const int)argc, (const char**)argv, "PathToImageDB", 1, 1) == false)
00081 return 1;
00082
00083 mgr->start();
00084
00085 TestImages testImages(mgr->getExtraArg(0).c_str(), TestImages::CALTECH256,
00086 1, 1, 1);
00087
00088
00089 std::vector<std::string> objNames;
00090
00091
00092 for(uint scene=0; scene<testImages.getNumScenes(TestImages::TRAIN); scene++)
00093 {
00094 Image<PixRGB<byte> > sceneImg = testImages.getScene(scene, TestImages::TRAIN);
00095
00096
00097 TestImages::SceneData sceneData = testImages.getSceneData(scene, TestImages::TRAIN);
00098 LINFO("Train %s with Scene %s",
00099 sceneData.description.c_str(),
00100 sceneData.filename.c_str());
00101 objRec->train(sceneImg, sceneData.description);
00102
00103
00104 getObjName(objNames, sceneData.description);
00105 }
00106 objRec->finalizeTraining();
00107
00108 int totalImages = 0;
00109 int correctImages = 0;
00110
00111
00112 Image<float> confMatrix(objNames.size(), objNames.size(), ZEROS);
00113
00114
00115 for(uint scene=0; scene<testImages.getNumScenes(TestImages::TEST); scene++)
00116 {
00117 Image<PixRGB<byte> > sceneImg = testImages.getScene(scene, TestImages::TEST);
00118
00119
00120 TestImages::SceneData sceneData = testImages.getSceneData(scene, TestImages::TEST);
00121 std::string classDesc = objRec->predict(sceneImg);
00122
00123 printf("Test:r:%s p:%s\n",
00124 sceneData.description.c_str(),
00125 classDesc.c_str());
00126 if(classDesc == sceneData.description)
00127 correctImages++;
00128 totalImages++;
00129
00130
00131 int x = getObjName(objNames, classDesc);
00132 int y = getObjName(objNames, sceneData.description);
00133
00134 if (confMatrix.coordsOk(x,y))
00135 {
00136 float pVal = confMatrix.getVal(x,y);
00137 confMatrix.setVal(x,y, pVal+1);
00138 } else {
00139 printf("Invalid corrd %ix%i", x, y);
00140 }
00141
00142 }
00143
00144 for(int y=0; y<confMatrix.getHeight(); y++)
00145 {
00146 for(int x=0; x<confMatrix.getWidth(); x++)
00147 printf("%f ", confMatrix.getVal(x,y));
00148 printf("\n");
00149 }
00150
00151
00152
00153
00154
00155 printf("Recognition Rate %i/%i=%f (train size=%i)\n",
00156 correctImages,
00157 totalImages,
00158 (float)correctImages/(float)totalImages,
00159 testImages.getNumScenes(TestImages::TRAIN)
00160 );
00161
00162
00163 mgr->stop();
00164
00165 return 0;
00166
00167 }
00168