Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of gr-peach-opencv-project-sd-card by
face_basic.hpp
00001 #ifndef __OPENCV_FACE_BASIC_HPP 00002 #define __OPENCV_FACE_BASIC_HPP 00003 00004 #include "opencv2/face.hpp" 00005 #include "precomp.hpp" 00006 00007 #include <set> 00008 #include <limits> 00009 #include <iostream> 00010 00011 using namespace cv; 00012 00013 // Reads a sequence from a FileNode::SEQ with type _Tp into a result vector. 00014 template<typename _Tp> 00015 inline void readFileNodeList(const FileNode& fn, std::vector<_Tp>& result) { 00016 if (fn.type() == FileNode::SEQ) { 00017 for (FileNodeIterator it = fn.begin(); it != fn.end();) { 00018 _Tp item; 00019 it >> item; 00020 result.push_back(item); 00021 } 00022 } 00023 } 00024 00025 // Writes the a list of given items to a cv::FileStorage. 00026 template<typename _Tp> 00027 inline void writeFileNodeList(FileStorage& fs, const String& name, 00028 const std::vector<_Tp>& items) { 00029 /*the debug 00030 // typedefs 00031 typedef typename std::vector<_Tp>::const_iterator constVecIterator; 00032 // write the elements in item to fs 00033 fs << name << "["; 00034 for (constVecIterator it = items.begin(); it != items.end(); ++it) { 00035 fs << *it; 00036 } 00037 fs << "]";*/ 00038 } 00039 00040 inline Mat asRowMatrix(InputArrayOfArrays src, int rtype, double alpha=1, double beta=0) { 00041 // make sure the input data is a vector of matrices or vector of vector 00042 if(src.kind() != _InputArray::STD_VECTOR_MAT && src.kind() != _InputArray::STD_VECTOR_VECTOR) { 00043 String error_message = "The data is expected as InputArray::STD_VECTOR_MAT (a std::vector<Mat>) or _InputArray::STD_VECTOR_VECTOR (a std::vector< std::vector<...> >)."; 00044 CV_Error(Error::StsBadArg, error_message); 00045 } 00046 // number of samples 00047 size_t n = src.total(); 00048 // return empty matrix if no matrices given 00049 if(n == 0) 00050 return Mat(); 00051 // dimensionality of (reshaped) samples 00052 size_t d = src.getMat(0).total(); 00053 // create data matrix 00054 Mat data((int)n, (int)d, rtype); 00055 // now copy data 00056 for(unsigned int i = 0; i < n; i++) { 00057 // make sure data can be reshaped, throw exception if not! 00058 if(src.getMat(i).total() != d) { 00059 String error_message = format("Wrong number of elements in matrix #%d! Expected %d was %d.", i, d, src.getMat(i).total()); 00060 CV_Error(Error::StsBadArg, error_message); 00061 } 00062 // get a hold of the current row 00063 Mat xi = data.row(i); 00064 // make reshape happy by cloning for non-continuous matrices 00065 if(src.getMat(i).isContinuous()) { 00066 src.getMat(i).reshape(1, 1).convertTo(xi, rtype, alpha, beta); 00067 } else { 00068 src.getMat(i).clone().reshape(1, 1).convertTo(xi, rtype, alpha, beta); 00069 } 00070 } 00071 return data; 00072 } 00073 00074 // Utility structure to load/save face label info (a pair of int and string) via FileStorage 00075 struct LabelInfo 00076 { 00077 LabelInfo():label(-1), value("") {} 00078 LabelInfo(int _label, const String &_value): label(_label), value(_value) {} 00079 int label; 00080 String value; 00081 void write(cv::FileStorage& fs) const 00082 { 00083 fs << "{" << "label" << label << "value" << value << "}"; 00084 } 00085 void read(const cv::FileNode& node) 00086 { 00087 label = (int)node["label"]; 00088 value = (String)node["value"]; 00089 } 00090 std::ostream& operator<<(std::ostream& out) 00091 { 00092 out << "{ label = " << label << ", " << "value = " << value.c_str() << "}"; 00093 return out; 00094 } 00095 }; 00096 00097 inline void write(cv::FileStorage& fs, const String&, const LabelInfo& x) 00098 { 00099 x.write(fs); 00100 } 00101 00102 inline void read(const cv::FileNode& node, LabelInfo& x, const LabelInfo& default_value = LabelInfo()) 00103 { 00104 if(node.empty()) 00105 x = default_value; 00106 else 00107 x.read(node); 00108 } 00109 00110 00111 class BasicFaceRecognizerImpl : public cv::face::BasicFaceRecognizer 00112 { 00113 public: 00114 BasicFaceRecognizerImpl(int num_components = 0, double threshold = DBL_MAX) 00115 : _num_components(num_components), _threshold(threshold) 00116 {} 00117 00118 void load(const FileStorage& fs) 00119 { 00120 //read matrices 00121 fs["num_components"] >> _num_components; 00122 fs["mean"] >> _mean; 00123 fs["eigenvalues"] >> _eigenvalues; 00124 fs["eigenvectors"] >> _eigenvectors; 00125 // read sequences 00126 readFileNodeList(fs["projections"], _projections); 00127 fs["labels"] >> _labels; 00128 const FileNode& fn = fs["labelsInfo"]; 00129 if (fn.type() == FileNode::SEQ) 00130 { 00131 _labelsInfo.clear(); 00132 for (FileNodeIterator it = fn.begin(); it != fn.end();) 00133 { 00134 LabelInfo item; 00135 it >> item; 00136 _labelsInfo.insert(std::make_pair(item.label, item.value)); 00137 } 00138 } 00139 } 00140 00141 void save(FileStorage& fs) const 00142 { 00143 //the debug 00144 /* 00145 // write matrices 00146 fs << "num_components" << _num_components; 00147 fs << "mean" << _mean; 00148 fs << "eigenvalues" << _eigenvalues; 00149 fs << "eigenvectors" << _eigenvectors; 00150 // write sequences 00151 writeFileNodeList(fs, "projections", _projections); 00152 fs << "labels" << _labels; 00153 fs << "labelsInfo" << "["; 00154 for (std::map<int, String>::const_iterator it = _labelsInfo.begin(); it != _labelsInfo.end(); it++) 00155 fs << LabelInfo(it->first, it->second); 00156 fs << "]";*/ 00157 } 00158 00159 CV_IMPL_PROPERTY(int, NumComponents, _num_components) 00160 CV_IMPL_PROPERTY(double, Threshold, _threshold) 00161 CV_IMPL_PROPERTY_RO(std::vector<cv::Mat>, Projections, _projections) 00162 CV_IMPL_PROPERTY_RO(cv::Mat, Labels, _labels) 00163 CV_IMPL_PROPERTY_RO(cv::Mat, EigenValues, _eigenvalues) 00164 CV_IMPL_PROPERTY_RO(cv::Mat, EigenVectors, _eigenvectors) 00165 CV_IMPL_PROPERTY_RO(cv::Mat, Mean, _mean) 00166 00167 protected: 00168 int _num_components; 00169 double _threshold; 00170 std::vector<Mat> _projections; 00171 Mat _labels; 00172 Mat _eigenvectors; 00173 Mat _eigenvalues; 00174 Mat _mean; 00175 }; 00176 00177 #endif // __OPENCV_FACE_BASIC_HPP 00178 00179
Generated on Tue Jul 12 2022 14:46:35 by
1.7.2
