libeblearntools
/home/rex/ebltrunk/tools/libeblearntools/include/pascalfull_dataset.hpp
00001 /***************************************************************************
00002  *   Copyright (C) 2009 by Pierre Sermanet   *
00003  *   pierre.sermanet@gmail.com   *
00004  *   All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Redistribution under a license not approved by the Open Source 
00014  *       Initiative (http://www.opensource.org) must display the 
00015  *       following acknowledgement in all advertising material:
00016  *        This product includes software developed at the Courant
00017  *        Institute of Mathematical Sciences (http://cims.nyu.edu).
00018  *     * The names of the authors may not be used to endorse or promote products
00019  *       derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00023  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00024  * DISCLAIMED. IN NO EVENT SHALL ThE AUTHORS BE LIABLE FOR ANY
00025  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00026  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00028  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  ***************************************************************************/
00032 
00033 #ifndef PASCALFULL_DATASET_HPP_
00034 #define PASCALFULL_DATASET_HPP_
00035 
00036 #include <algorithm>
00037 #include <stdlib.h>
00038 #include <sstream>
00039 #include <stdio.h>
00040 
00041 #include "xml_utils.h"
00042 
00043 using namespace std;
00044 
00045 namespace ebl {
00046 
00048   // constructors & initializations
00049 
00050   template <class Tdata>
00051   pascalfull_dataset<Tdata>::pascalfull_dataset(const char *name_,
00052                                                 const char *inroot_,
00053                                                 const char *outdir_,
00054                                                 const char *annotations)
00055     : pascal_dataset<Tdata>(name_, inroot_, false, false, false, annotations) {
00056     outdir = outdir_;
00057     data_cnt = 0;
00058     this->allocated = true; // fool extract method
00059  }
00060 
00061   template <class Tdata>
00062   pascalfull_dataset<Tdata>::~pascalfull_dataset() {
00063   }
00064 
00065 #ifdef __XML__ // disable some derived methods if XML not available
00066 
00068   // process xml
00069 
00070   template <class Tdata>
00071   bool pascalfull_dataset<Tdata>::process_xml(const string &xmlfile) {
00072     string image_filename;
00073     string image_fullname;
00074     string obj_classname;
00075 
00076     // parse xml file
00077     try {
00078       DomParser parser;
00079       //    parser.set_validate();
00080       parser.parse_file(xmlfile);
00081       if (parser) {
00082         // initialize root node and list
00083         const Node* pNode = parser.get_document()->get_root_node();
00084         Node::NodeList list = pNode->get_children();
00085         // get image filename
00086         for(Node::NodeList::iterator iter = list.begin();
00087             iter != list.end(); ++iter) {
00088           if (!strcmp((*iter)->get_name().c_str(), "filename")) {
00089             xml_get_string(*iter, image_filename);
00090             iter = list.end(); iter--; // stop loop
00091           }
00092         }
00093         image_fullname = imgroot;
00094         image_fullname += image_filename;
00095         // parse all objects in image
00096         for(Node::NodeList::iterator iter = list.begin();
00097             iter != list.end(); ++iter) {
00098           if (!strcmp((*iter)->get_name().c_str(), "object")) {
00099             Node::NodeList olist = (*iter)->get_children();
00100             for(Node::NodeList::iterator oiter = olist.begin();
00101                 oiter != olist.end(); ++oiter) {
00102               if (!strcmp((*oiter)->get_name().c_str(), "name")) {
00103                 xml_get_string(*oiter, obj_classname);
00104                 // if object's name matches an excluded class, stop this xml
00105                 if (find(exclude.begin(), exclude.end(),
00106                          obj_classname) != exclude.end())
00107                   return false;
00108               }
00109             }
00110           }
00111         }
00112       }
00113     } catch (const std::exception& ex) {
00114       cerr << "error: Xml exception caught: " << ex.what() << endl;
00115       return false;
00116     } catch (const char *err) {
00117       cerr << "error: " << err << endl;
00118       return false;
00119     }
00120     // copy image into output directory
00121     ostringstream cmd;
00122     ostringstream tgt;
00123     tgt << outdir << "/" << image_filename;
00124     cmd << "cp " << image_fullname << " " << tgt.str();
00125     if (std::system(cmd.str().c_str()))
00126       cerr << "warning: failed to execute: " << cmd.str() << endl;
00127     else {
00128       cout << data_cnt << ": copied " << tgt.str() << endl;
00129       data_cnt++;
00130     }
00131     return true;
00132   }
00133   
00134 #endif /* __XML__ */
00135 
00136 } // end namespace ebl
00137 
00138 #endif /* PASCALFULL_DATASET_HPP_ */