Object oriented design
Like face detection and face recognition, face tracking consists of two parts: data and algorithm. The algorithm trains the model by storing (i.e. offline) data in advance, and then performs some operations on the new (i.e. online) data. Therefore, object-oriented design is a good choice.
In opencv 2.x version, it is convenient to introduce XML/YAML file storage type. For algorithm, it will greatly simplify the task of organizing offline data. The following shows this function through a fake class
-
Custom class foo
1 // foo.h 2 /* 3 In the following code, a serialization function is defined to serialize the read and write I/O functions. 4 FileStorage Class supports two types of data structures that can be serialized. 5 For the sake of simplicity, all classes in this chapter will adopt mapping, in which each variable used for storage will create a FileNode object of type FileNode::MAP. 6 This requires that each element in the variable be assigned a unique key. For consistency, use the variable name as a label 7 */ 8 9 #include <opencv2/opencv.hpp> 10 #include <iostream> 11 using namespace cv; 12 using namespace std; 13 14 class foo { 15 public: 16 int a, b; 17 void write(FileStorage &fs) const { // Serialize store custom data types 18 assert(fs.isOpened()); 19 fs << "{" << "a" << a << "b" << b << "}"; // Establish FileNode::MAP Object of type 20 } 21 void read(const FileNode& node) { // Read data 22 assert(node.type() == FileNode::MAP); 23 node["a"] >> a; node["b"] >> b; 24 } 25 };
-
In order for the serialization of FileStorage classes to work properly, you need to define write and read functions
1 template<class T> 2 void 3 write(FileStorage& fs, 4 const string&, 5 const T& x) 6 { 7 x.write(fs); 8 } 9 //============================================================================== 10 template<class T> 11 void 12 read(const FileNode& node, 13 T& x, 14 const T& d) 15 { 16 if(node.empty())x = d; else x.read(node); 17 }
-
In order to make it easy to save and load user-defined classes with serialization, the functions of load ﹣ ft and save ﹣ ft are defined with modularization function
1 template <class T> 2 T load_ft(const char* fname){ 3 T x; FileStorage f(fname,FileStorage::READ); 4 f["ft object"] >> x; f.release(); return x; // Define the label associated with the object as ft object 5 } 6 //============================================================================== 7 template<class T> 8 void save_ft(const char* fname,const T& x){ 9 FileStorage f(fname,FileStorage::WRITE); 10 f << "ft object" << x; f.release(); 11 }
-
Define the above in ft.hpp
1 /* 2 ft.hpp 3 Used to load and save object data 4 */ 5 6 #ifndef _FT_FT_HPP_ 7 #define _FT_FT_HPP_ 8 #include <opencv2/opencv.hpp> 9 //============================================================================== 10 // In order to make it easy to save and load user-defined classes with serialization, modular function definitions are adopted load_ft,save_ft function 11 template <class T> 12 T load_ft(const char* fname){ 13 T x; FileStorage f(fname,FileStorage::READ); 14 f["ft object"] >> x; f.release(); return x; // Define the label associated with the object as ft object 15 } 16 //============================================================================== 17 template<class T> 18 void save_ft(const char* fname,const T& x){ 19 FileStorage f(fname,FileStorage::WRITE); 20 f << "ft object" << x; f.release(); 21 } 22 //============================================================================== 23 // In order to FileStorage The serialization of class can work normally, and it needs to be defined write, read function 24 template<class T> 25 void 26 write(FileStorage& fs, 27 const string&, 28 const T& x) 29 { 30 x.write(fs); 31 } 32 //============================================================================== 33 template<class T> 34 void 35 read(const FileNode& node, 36 T& x, 37 const T& d) 38 { 39 if(node.empty())x = d; else x.read(node); 40 } 41 //============================================================================== 42 #endif