Trait opencv::face::FaceRecognizer
source · pub trait FaceRecognizer: AlgorithmTrait + FaceRecognizerConst {
fn as_raw_mut_FaceRecognizer(&mut self) -> *mut c_void;
fn train(
&mut self,
src: &dyn ToInputArray,
labels: &dyn ToInputArray
) -> Result<()> { ... }
fn update(
&mut self,
src: &dyn ToInputArray,
labels: &dyn ToInputArray
) -> Result<()> { ... }
fn read(&mut self, filename: &str) -> Result<()> { ... }
fn read_1(&mut self, fn_: &FileNode) -> Result<()> { ... }
fn set_label_info(&mut self, label: i32, str_info: &str) -> Result<()> { ... }
fn set_threshold(&mut self, val: f64) -> Result<()> { ... }
}
Required Methods§
fn as_raw_mut_FaceRecognizer(&mut self) -> *mut c_void
Provided Methods§
sourcefn train(
&mut self,
src: &dyn ToInputArray,
labels: &dyn ToInputArray
) -> Result<()>
fn train(
&mut self,
src: &dyn ToInputArray,
labels: &dyn ToInputArray
) -> Result<()>
Trains a FaceRecognizer with given data and associated labels.
Parameters
- src: The training images, that means the faces you want to learn. The data has to be given as a vector<Mat>.
- labels: The labels corresponding to the images have to be given either as a vector<int> or a Mat of type CV_32SC1.
The following source code snippet shows you how to learn a Fisherfaces model on a given set of images. The images are read with imread and pushed into a std::vector<Mat>. The labels of each image are stored within a std::vector<int> (you could also use a Mat of type CV_32SC1). Think of the label as the subject (the person) this image belongs to, so same subjects (persons) should have the same label. For the available FaceRecognizer you don’t have to pay any attention to the order of the labels, just make sure same persons have the same label:
// holds images and labels
vector<Mat> images;
vector<int> labels;
// using Mat of type CV_32SC1
// Mat labels(number_of_samples, 1, CV_32SC1);
// images for first person
images.push_back(imread("person0/0.jpg", IMREAD_GRAYSCALE)); labels.push_back(0);
images.push_back(imread("person0/1.jpg", IMREAD_GRAYSCALE)); labels.push_back(0);
images.push_back(imread("person0/2.jpg", IMREAD_GRAYSCALE)); labels.push_back(0);
// images for second person
images.push_back(imread("person1/0.jpg", IMREAD_GRAYSCALE)); labels.push_back(1);
images.push_back(imread("person1/1.jpg", IMREAD_GRAYSCALE)); labels.push_back(1);
images.push_back(imread("person1/2.jpg", IMREAD_GRAYSCALE)); labels.push_back(1);
Now that you have read some images, we can create a new FaceRecognizer. In this example I’ll create a Fisherfaces model and decide to keep all of the possible Fisherfaces:
// Create a new Fisherfaces model and retain all available Fisherfaces,
// this is the most common usage of this specific FaceRecognizer:
//
Ptr<FaceRecognizer> model = FisherFaceRecognizer::create();
And finally train it on the given dataset (the face images and labels):
// This is the common interface to train all of the available cv::FaceRecognizer
// implementations:
//
model->train(images, labels);
sourcefn update(
&mut self,
src: &dyn ToInputArray,
labels: &dyn ToInputArray
) -> Result<()>
fn update(
&mut self,
src: &dyn ToInputArray,
labels: &dyn ToInputArray
) -> Result<()>
Updates a FaceRecognizer with given data and associated labels.
Parameters
- src: The training images, that means the faces you want to learn. The data has to be given as a vector<Mat>.
- labels: The labels corresponding to the images have to be given either as a vector<int> or a Mat of type CV_32SC1.
This method updates a (probably trained) FaceRecognizer, but only if the algorithm supports it. The Local Binary Patterns Histograms (LBPH) recognizer (see createLBPHFaceRecognizer) can be updated. For the Eigenfaces and Fisherfaces method, this is algorithmically not possible and you have to re-estimate the model with FaceRecognizer::train. In any case, a call to train empties the existing model and learns a new model, while update does not delete any model data.
// Create a new LBPH model (it can be updated) and use the default parameters,
// this is the most common usage of this specific FaceRecognizer:
//
Ptr<FaceRecognizer> model = LBPHFaceRecognizer::create();
// This is the common interface to train all of the available cv::FaceRecognizer
// implementations:
//
model->train(images, labels);
// Some containers to hold new image:
vector<Mat> newImages;
vector<int> newLabels;
// You should add some images to the containers:
//
// ...
//
// Now updating the model is as easy as calling:
model->update(newImages,newLabels);
// This will preserve the old model data and extend the existing model
// with the new features extracted from newImages!
Calling update on an Eigenfaces model (see EigenFaceRecognizer::create), which doesn’t support updating, will throw an error similar to:
OpenCV Error: The function/feature is not implemented (This FaceRecognizer (FaceRecognizer.Eigenfaces) does not support updating, you have to use FaceRecognizer::train to update it.) in update, file /home/philipp/git/opencv/modules/contrib/src/facerec.cpp, line 305
terminate called after throwing an instance of "cv::Exception"
Note: The FaceRecognizer does not store your training images, because this would be very memory intense and it’s not the responsibility of te FaceRecognizer to do so. The caller is responsible for maintaining the dataset, he want to work with.
sourcefn read(&mut self, filename: &str) -> Result<()>
fn read(&mut self, filename: &str) -> Result<()>
Loads a FaceRecognizer and its model state.
Loads a persisted model and state from a given XML or YAML file . Every FaceRecognizer has to overwrite FaceRecognizer::load(FileStorage& fs) to enable loading the model state. FaceRecognizer::load(FileStorage& fs) in turn gets called by FaceRecognizer::load(const String& filename), to ease saving a model.
sourcefn read_1(&mut self, fn_: &FileNode) -> Result<()>
fn read_1(&mut self, fn_: &FileNode) -> Result<()>
Loads a FaceRecognizer and its model state.
Loads a persisted model and state from a given XML or YAML file . Every FaceRecognizer has to overwrite FaceRecognizer::load(FileStorage& fs) to enable loading the model state. FaceRecognizer::load(FileStorage& fs) in turn gets called by FaceRecognizer::load(const String& filename), to ease saving a model.
Overloaded parameters
sourcefn set_label_info(&mut self, label: i32, str_info: &str) -> Result<()>
fn set_label_info(&mut self, label: i32, str_info: &str) -> Result<()>
Sets string info for the specified model’s label.
The string info is replaced by the provided value if it was set before for the specified label.
sourcefn set_threshold(&mut self, val: f64) -> Result<()>
fn set_threshold(&mut self, val: f64) -> Result<()>
Sets threshold of model