XMLInput.C

Go to the documentation of this file.
00001 /*!@file Transport/XMLInput.C Use xml file as input */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00005 // University of Southern California (USC) and the iLab at USC.         //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Lior Elazary <elazary@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Transport/XMLInput.C $
00035 // $Id: XMLInput.C 12962 2010-03-06 02:13:53Z irock $
00036 //
00037 
00038 #include "Transport/XMLInput.H"
00039 #include "Transport/TransportOpts.H"
00040 #include "Component/OptionManager.H"
00041 #include "Component/ModelOptionDef.H"
00042 #include "Image/ShapeOps.H"
00043 #include "Image/CutPaste.H"
00044 #include "Raster/GenericFrame.H"
00045 #include "Image/DrawOps.H"
00046 
00047 const ModelOptionDef OPT_XMLDrawPolygons =
00048   { MODOPT_ARG(bool), "XMLDrawPolygons", &MOC_INPUT, OPTEXP_CORE,
00049     "Draw the polygons on the frame with the name of the object ",
00050     "draw-polygons", '\0', "<true/false>", "false" };
00051 
00052 const ModelOptionDef OPT_XMLGetObjects =
00053   { MODOPT_ARG(bool), "XMLGetObjects", &MOC_INPUT, OPTEXP_CORE,
00054     "Return frames with only the objects in them. Can be used for training or testing only objects.",
00055     "get-objects", '\0', "<true/false>", "false" };
00056 
00057 const ModelOptionDef OPT_XMLFilterObjectName =
00058   { MODOPT_ARG(std::string), "XMLFilterObjectName", &MOC_INPUT, OPTEXP_CORE,
00059     "Only return objects matching the given name.",
00060     "filter-object-name", '\0', "string", "" };
00061 
00062 const ModelOptionDef OPT_XMLRootPath =
00063   { MODOPT_ARG(std::string), "XMLRootPath", &MOC_INPUT, OPTEXP_CORE,
00064     "The root path from which all xml files will be read from.",
00065     "xml-root-path", '\0', "string", "" };
00066 
00067 // ######################################################################
00068 XMLInput::XMLInput(OptionManager& mgr) :
00069   FrameIstream(mgr, "XMLInput Input", "XMLInputInput"),
00070   itsDrawPolygons(&OPT_XMLDrawPolygons, this),
00071   itsGetObjects(&OPT_XMLGetObjects, this),
00072   itsFilterObjectName(&OPT_XMLFilterObjectName, this),
00073   itsRootPath(&OPT_XMLRootPath, this),
00074   itsFrameNum(0),
00075   itsCurrentSceneNum(0),
00076   itsObjectNum(0)
00077 {
00078 }
00079 
00080 XMLInput::~XMLInput()
00081 {
00082 }
00083 
00084 // ######################################################################
00085 void XMLInput::setConfigInfo(const std::string& filename)
00086 {
00087   // NOTE: if you modify any behavior here, then please update the
00088   // corresponding documentation for the global "--in" option inside
00089   // the OPT_InputFrameSource definition in Media/MediaOpts.C
00090 
00091   if (filename.size() == 0)
00092     return;
00093 
00094   LINFO("Reading xml file %s", filename.c_str());
00095   if (itsTestImages.get() == 0)
00096     itsTestImages.reset(new TestImages(filename.c_str(), TestImages::XMLFILE));
00097 
00098 }
00099 
00100 bool XMLInput::setFrameNumber(int n)
00101 {
00102   ASSERT(n >= 0);
00103   itsFrameNum = n;
00104 
00105   return true;
00106 }
00107 
00108 
00109 // ######################################################################
00110 GenericFrameSpec XMLInput::peekFrameSpec()
00111 {
00112   GenericFrameSpec result;
00113 
00114   result.nativeType = GenericFrame::RGB_U8;
00115   result.videoFormat = VIDFMT_AUTO;
00116   result.videoByteSwap = false;
00117   result.dims = Dims(0,0);
00118   result.floatFlags = 0;
00119 
00120   return result;
00121 }
00122 
00123 // ######################################################################
00124 GenericFrame XMLInput::readFrame()
00125 {
00126   if (itsTestImages.get() == 0)
00127     LFATAL("No scene data. Need xml file");
00128 
00129   if (!itsGetObjects.getVal())
00130     itsCurrentSceneNum = itsFrameNum;
00131 
00132   //If we dont have the frame number, then return an empty image
00133   if (itsCurrentSceneNum >= itsTestImages->getNumScenes())
00134   {
00135     LINFO("No more scenes");
00136     return GenericFrame();
00137   }
00138 
00139 
00140   //Get the scene
00141 
00142 
00143   TestImages::SceneData sceneData = itsTestImages->getSceneData(itsCurrentSceneNum);
00144   rutz::shared_ptr<TestImages::SceneData> scene(new TestImages::SceneData);
00145   scene->description = sceneData.description;
00146   scene->filename = sceneData.filename;
00147   scene->type = sceneData.type;
00148   scene->useType = sceneData.useType;
00149 
00150 //  LINFO("Scene %s", sceneData.filename.c_str());
00151   Image<PixRGB<byte> > sceneImg;
00152   if (itsGetObjects.getVal())
00153   {
00154 
00155     if (itsObjectNum < sceneData.objects.size())
00156     {
00157       TestImages::ObjData objData = sceneData.objects[itsObjectNum];
00158       std::vector<Point2D<int> > objPoly = objData.polygon;
00159 
00160       Image<PixRGB<byte> > img = itsTestImages->getScene(itsCurrentSceneNum);
00161 
00162       //Get the bounding box
00163       Rectangle rect = findBoundingRect(objPoly, img.getDims());
00164       sceneImg = crop(img, rect);
00165 
00166       scene->objects.push_back(objData);
00167       itsObjectNum++;
00168       if (itsObjectNum >= sceneData.objects.size())
00169       {
00170         itsCurrentSceneNum++;
00171         itsObjectNum = 0;
00172       }
00173     }
00174 
00175 
00176 
00177   } else {
00178     scene->objects = sceneData.objects;
00179     sceneImg = itsTestImages->getScene(itsCurrentSceneNum);
00180 
00181     if (itsDrawPolygons.getVal())
00182     {
00183       for(uint i=0; i<sceneData.objects.size(); i++)
00184       {
00185         TestImages::ObjData objData = sceneData.objects[i];
00186 
00187         if (itsFilterObjectName.getVal() == objData.name || itsFilterObjectName.getVal().empty())
00188         {
00189           std::vector<Point2D<int> > objPoly = objData.polygon;
00190           Point2D<int> p1 = objPoly[0];
00191           for(uint i=1; i<objPoly.size(); i++)
00192           {
00193             drawLine(sceneImg, p1, objPoly[i], PixRGB<byte>(0, 255, 0), 0);
00194             p1 = objPoly[i];
00195           }
00196           drawLine(sceneImg, p1, objPoly[0], PixRGB<byte>(0, 255, 0)); //close the polygon
00197 
00198           writeText(sceneImg, objPoly[0]+10, objData.name.c_str(), PixRGB<byte>(255,255,255), PixRGB<byte>(0,0,0));
00199         }
00200 
00201       }
00202     }
00203 
00204   }
00205 
00206   if (!sceneData.dims.isEmpty())
00207     sceneImg = rescale(sceneImg, sceneData.dims);
00208   scene->dims = sceneImg.getDims();
00209 
00210 
00211   GenericFrame frame(sceneImg);
00212   frame.addMetaData(std::string("SceneData"), scene);
00213 
00214   return frame;
00215 }
00216 
00217 
00218 // ######################################################################
00219 /* So things look consistent in everyone's emacs... */
00220 /* Local Variables: */
00221 /* indent-tabs-mode: nil */
00222 /* End: */
Generated on Sun May 8 08:06:57 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3