X-Boost  2.3.8
Utils.h
Go to the documentation of this file.
1 /* XBoost: Ada-Boost and Friends on Haar/ICF/HOG Features, Library and ToolBox
2  *
3  * Copyright (c) 2008-2014 Paolo Medici <medici@ce.unipr.it>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 
21 #ifndef _CLASSIFIER_UTILS_H
22 #define _CLASSIFIER_UTILS_H
23 
24 #include <fstream>
25 #include <stdexcept>
26 #include <iostream>
27 
39 template<class Classifier>
40 void load_classifier(const char *network_file, Classifier & dst, unsigned int & width, unsigned int & height, char* strconf);
41 
42 
49 template<class Classifier>
50 void load_classifier(const char *network_file, Classifier & dst, unsigned int & width, unsigned int & height);
51 
57 template<class Classifier>
58 void save_classifier(const char *network_file, const Classifier & src, unsigned int width, unsigned int height, const char* config);
59 
61 
62 
63 template<class Classifier>
64 void load_classifier(const char *network_file, Classifier & dst, unsigned int & width, unsigned int & height)
65 {
66 std::ifstream in(network_file);
67 if(in)
68  {
69  std::string signature;
70  in >> signature >> width >> height;
71  if(signature != dst.signature())
72  {
73  std::cerr << "expected " << dst.signature() << ". Readed " << signature << std::endl;
74  throw std::runtime_error("wrong signature");
75  }
76  in >> dst;
77  }
78  else
79  {
80  std::cerr << network_file << " not valid" << std::endl;
81  throw std::runtime_error("file not valid");
82  }
83 }
84 
85 template<class Classifier>
86 void load_classifier(const char *network_file, Classifier & dst, unsigned int & width, unsigned int & height, char* strconf)
87 {
88 std::ifstream in(network_file);
89 if(in)
90  {
91  std::string signature;
92  in >> signature >> width >> height;
93  if(signature != dst.signature())
94  {
95  std::cerr << "expected " << dst.signature() << ". Readed " << signature << std::endl;
96  throw std::runtime_error("wrong signature");
97  }
98  in >> dst;
99  }
100  else
101  {
102  std::cerr << network_file << " not valid" << std::endl;
103  throw std::runtime_error("file not valid");
104  }
105 }
106 
107 
108 template<class Classifier>
109 void save_classifier(const char *network_file, const Classifier & src, unsigned int width, unsigned int height, const char* config)
110 {
111 std::ofstream out(network_file);
112 if(config)
113  out << src.signature() << ' ' << width << ' ' << height << ' ' << config << '\n' << src;
114 else
115  out << src.signature() << ' ' << width << ' ' << height << '\n' << src; // TODO: insert a "fake" config as, for example, '-' ?
116 }
117 
118 #endif
void save_classifier(const char *network_file, const Classifier &src, unsigned int width, unsigned int height, const char *config)
Definition: Utils.h:109
void load_classifier(const char *network_file, Classifier &dst, unsigned int &width, unsigned int &height, char *strconf)
Definition: Utils.h:86