SVMObjectDB.C

00001 /*!@file Learn/SVMObjectDB.H SVM Object Database module */
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: Laurent Itti <itti@usc.edu>
00034 // $HeadURL: svn://dparks@isvn.usc.edu/software/invt/trunk/saliency/src/Learn/SVMObjectDB.C $
00035 // $Id: SVMObjectDB.C 11135 2009-04-21 20:14:32Z mviswana $
00036 //
00037 
00038 #include "Learn/SVMObjectDB.H"
00039 
00040 #ifdef HAVE_LIBXML
00041 
00042 void SVMObjectDB::getNodeContent(xmlDocPtr doc, xmlNodePtr nodePtr,
00043                                   const char* nodeName, std::string &result)
00044 {
00045   xmlChar *tmp = NULL;
00046   if (!xmlStrcmp(nodePtr->name, (const xmlChar *)nodeName))
00047     tmp = xmlNodeGetContent(nodePtr);
00048   //xmlNodeListGetString(doc, nodePtr->xmlChildrenNode, 1);
00049 
00050   if (tmp != NULL) {
00051     result = std::string((const char*)tmp);
00052     xmlFree(tmp);
00053   }
00054 }
00055 
00056 
00057 
00058 void SVMObjectDB::loadObjDB(const std::string& filename)
00059 {
00060   xmlDocPtr doc;
00061 
00062   // Get the xml file for the given scene:
00063   LINFO("Reading xml formatted object db file: %s", filename.c_str());
00064 
00065   // check if the file exists:
00066   doc = xmlReadFile(filename.c_str(), NULL, 0);
00067   if (doc == NULL) {
00068     LINFO("Failed to parse %s", filename.c_str());
00069     return;
00070   }
00071 
00072   /* Get the root element node */
00073   xmlNode *root_element = xmlDocGetRootElement(doc);
00074 
00075   // look for object annotations
00076   xmlNodePtr cur = root_element->xmlChildrenNode; //dont care about top level
00077   while (cur != NULL) {
00078 
00079     // get the objects from the list:
00080     if ((!xmlStrcmp(cur->name, (const xmlChar *)"object"))) {
00081       // get the  object data:
00082       std::string objId, objName, objDescription;
00083 
00084       xmlNodePtr objectPtr = cur->xmlChildrenNode;
00085 
00086       while(objectPtr != NULL) { // read the attributes
00087         getNodeContent(doc, objectPtr, "id", objId);
00088         getNodeContent(doc, objectPtr, "name", objName);
00089         getNodeContent(doc, objectPtr, "description", objDescription);
00090         // next object data:
00091         objectPtr = objectPtr->next;
00092       }
00093       SVMObject obj;
00094       obj.id = atoi(objId.c_str());
00095       obj.name = objName;
00096       obj.description = objDescription;
00097       LINFO("Reading in object %s[%s]",objName.c_str(),objId.c_str());
00098       objdb.insert(obj);
00099      }
00100     cur = cur->next;
00101   }
00102 
00103   xmlFreeDoc(doc);
00104   xmlCleanupParser();
00105 }
00106 
00107 void SVMObjectDB::writeObjDB(const std::string& filename)
00108 {
00109   xmlDocPtr doc = xmlNewDoc((xmlChar *)"1.0");
00110 
00111   // Write the xml file for the given scene:
00112   LINFO("Writing out xml formatted object db file: %s", filename.c_str());
00113 
00114   // check if the file exists:
00115 
00116   /* Get the root element node */
00117   xmlNode *root_element = xmlNewNode(0, (xmlChar *)"objectDatabase");
00118   xmlDocSetRootElement(doc,root_element);
00119   char idbuf[50];
00120   std::set<SVMObject>::iterator it;
00121   for(it=objdb.begin();it!=objdb.end();it++) {
00122     xmlNode *objXML = xmlNewNode(0, (xmlChar *)"object");
00123     xmlAddChild(root_element,objXML);
00124     SVMObject objRef = (SVMObject) *it;
00125     LINFO("Writing out object %d[%s]\n",objRef.id,objRef.name.c_str());
00126     sprintf(idbuf,"%d",objRef.id);
00127     xmlNewTextChild(objXML,NULL,(xmlChar *)"id",(xmlChar *) idbuf);
00128     xmlNewTextChild(objXML,NULL,(xmlChar *)"name",(xmlChar *) objRef.name.c_str());
00129     xmlNewTextChild(objXML,NULL,(xmlChar *)"description",(xmlChar *) objRef.description.c_str());
00130     //xmlAttr *attr = xmlNewProp(objXML,"id",idbuf);
00131     //attr = xmlNewProp(objXML,"name",objRef.name.c_str());
00132     //attr = xmlNewProp(objXML,"description",objRef.description.c_str());
00133   }
00134   // Write out the document and clean up
00135   xmlSaveFileEnc(filename.c_str(),doc,"UTF-8");
00136   xmlFreeDoc(doc);
00137   xmlCleanupParser();
00138 
00139 }
00140 
00141 SVMObject SVMObjectDB::updateObject(const int id, const std::string& name)
00142 {
00143   SVMObject h;
00144   // Id unknown
00145   if(id == -1) {
00146     if(name.compare("")==0)
00147       LFATAL("When updating an object, the name must be defined");
00148     h=getObject(name);
00149   }
00150   else {
00151     h=getObject(id);
00152     if(!h.initialized() && name.compare("")==0)
00153       LFATAL("New id is given, with no name");
00154   }
00155   if(h.initialized())
00156     return h;
00157   else
00158     return newObject(id,name);
00159 }
00160 
00161 SVMObject SVMObjectDB::newObject(const int id, const std::string& name)
00162 {
00163   SVMObject h;
00164   int maxId=0;
00165   h.name = name;
00166   std::set<SVMObject>::iterator it;
00167   if(id == -1){
00168     SVMObject tmp;
00169     for(it=objdb.begin();it!=objdb.end();it++){
00170       tmp = (SVMObject) *it;
00171       if(tmp.id > maxId)
00172         maxId = tmp.id;
00173     }
00174     h.id = maxId+1;
00175   }
00176   else {
00177     h.id = id;
00178   }
00179   objdb.insert(h);
00180   return h;
00181 }
00182 
00183 SVMObject SVMObjectDB::getObject(const std::string& name)
00184 {
00185   std::set<SVMObject>::iterator it;
00186   SVMObject h;
00187   SVMObject tmp;
00188   for(it=objdb.begin();it!=objdb.end();it++){
00189     tmp = (SVMObject) *it;
00190     if(tmp.name.compare(name)==0)
00191       return tmp;
00192   }
00193   return h;
00194 }
00195 
00196 SVMObject SVMObjectDB::getObject(const int id)
00197 {
00198   std::set<SVMObject>::iterator it;
00199   SVMObject h;
00200   SVMObject tmp;
00201   for(it=objdb.begin();it!=objdb.end();it++){
00202     tmp = (SVMObject) *it;
00203     if(tmp.id==id)
00204       return tmp;
00205   }
00206   return h;
00207 }
00208 
00209 #else
00210 
00211 
00212 void SVMObjectDB::getNodeContent(xmlDocPtr doc, xmlNodePtr nodePtr,
00213                                   const char* nodeName, std::string &result){
00214   LFATAL("SVM Object DB Error: Binary was not compiled with XML Support");
00215 }
00216 
00217 void SVMObjectDB::loadObjDB(const std::string& filename){
00218   LFATAL("SVM Object DB Error: Binary was not compiled with XML Support");
00219 }
00220 
00221 void SVMObjectDB::writeObjDB(const std::string& filename) {
00222   LFATAL("SVM Object DB Error: Binary was not compiled with XML Support");
00223 }
00224 
00225 
00226 SVMObject SVMObjectDB::updateObject(const int id, const std::string& name) {
00227   LFATAL("SVM Object DB Error: Binary was not compiled with XML Support");
00228   return SVMObject() ; // just to keep the compiler happy
00229 }
00230 
00231 SVMObject SVMObjectDB::getObject(const std::string& name) {
00232   LFATAL("SVM Object DB Error: Binary was not compiled with XML Support");
00233   return SVMObject() ; // just to keep the compiler happy
00234 }
00235 
00236 SVMObject SVMObjectDB::getObject(const int id) {
00237   LFATAL("SVM Object DB Error: Binary was not compiled with XML Support");
00238   return SVMObject() ; // just to keep the compiler happy
00239 }
00240 
00241 
00242 #endif
Generated on Sun May 8 08:05:19 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3