Struct opencv::face::FaceRecognizer
source · pub struct FaceRecognizer { /* private fields */ }
Expand description
Abstract base class for all face recognition models
All face recognition models in OpenCV are derived from the abstract base class FaceRecognizer, which provides a unified access to all face recongition algorithms in OpenCV.
§Description
I’ll go a bit more into detail explaining FaceRecognizer, because it doesn’t look like a powerful interface at first sight. But: Every FaceRecognizer is an Algorithm, so you can easily get/set all model internals (if allowed by the implementation). Algorithm is a relatively new OpenCV concept, which is available since the 2.4 release. I suggest you take a look at its description.
Algorithm provides the following features for all derived classes:
- So called “virtual constructor”. That is, each Algorithm derivative is registered at program start and you can get the list of registered algorithms and create instance of a particular algorithm by its name (see Algorithm::create). If you plan to add your own algorithms, it is good practice to add a unique prefix to your algorithms to distinguish them from other algorithms.
- Setting/Retrieving algorithm parameters by name. If you used video capturing functionality from OpenCV highgui module, you are probably familar with cv::cvSetCaptureProperty, ocvcvGetCaptureProperty, VideoCapture::set and VideoCapture::get. Algorithm provides similar method where instead of integer id’s you specify the parameter names as text Strings. See Algorithm::set and Algorithm::get for details.
- Reading and writing parameters from/to XML or YAML files. Every Algorithm derivative can store all its parameters and then read them back. There is no need to re-implement it each time.
Moreover every FaceRecognizer supports the:
- Training of a FaceRecognizer with FaceRecognizer::train on a given set of images (your face database!).
- Prediction of a given sample image, that means a face. The image is given as a Mat.
- Loading/Saving the model state from/to a given XML or YAML.
- Setting/Getting labels info, that is stored as a string. String labels info is useful for keeping names of the recognized people.
Note: When using the FaceRecognizer interface in combination with Python, please stick to Python 2. Some underlying scripts like create_csv will not work in other versions, like Python 3. Setting the Thresholds +++++++++++++++++++++++
Sometimes you run into the situation, when you want to apply a threshold on the prediction. A common scenario in face recognition is to tell, whether a face belongs to the training dataset or if it is unknown. You might wonder, why there’s no public API in FaceRecognizer to set the threshold for the prediction, but rest assured: It’s supported. It just means there’s no generic way in an abstract class to provide an interface for setting/getting the thresholds of every possible FaceRecognizer algorithm. The appropriate place to set the thresholds is in the constructor of the specific FaceRecognizer and since every FaceRecognizer is a Algorithm (see above), you can get/set the thresholds at runtime!
Here is an example of setting a threshold for the Eigenfaces method, when creating the model:
// Let's say we want to keep 10 Eigenfaces and have a threshold value of 10.0
int num_components = 10;
double threshold = 10.0;
// Then if you want to have a cv::FaceRecognizer with a confidence threshold,
// create the concrete implementation with the appropriate parameters:
Ptr<FaceRecognizer> model = EigenFaceRecognizer::create(num_components, threshold);
Sometimes it’s impossible to train the model, just to experiment with threshold values. Thanks to Algorithm it’s possible to set internal model thresholds during runtime. Let’s see how we would set/get the prediction for the Eigenface model, we’ve created above:
// The following line reads the threshold from the Eigenfaces model:
double current_threshold = model->getDouble("threshold");
// And this line sets the threshold to 0.0:
model->set("threshold", 0.0);
If you’ve set the threshold to 0.0 as we did above, then:
//
Mat img = imread("person1/3.jpg", IMREAD_GRAYSCALE);
// Get a prediction from the model. Note: We've set a threshold of 0.0 above,
// since the distance is almost always larger than 0.0, you'll get -1 as
// label, which indicates, this face is unknown
int predicted_label = model->predict(img);
// ...
is going to yield -1 as predicted label, which states this face is unknown.
§Getting the name of a FaceRecognizer
Since every FaceRecognizer is a Algorithm, you can use Algorithm::name to get the name of a FaceRecognizer:
// Create a FaceRecognizer:
Ptr<FaceRecognizer> model = EigenFaceRecognizer::create();
// And here's how to get its name:
String name = model->name();
Trait Implementations§
source§impl AlgorithmTrait for FaceRecognizer
impl AlgorithmTrait for FaceRecognizer
source§impl AlgorithmTraitConst for FaceRecognizer
impl AlgorithmTraitConst for FaceRecognizer
fn as_raw_Algorithm(&self) -> *const c_void
source§fn write(&self, fs: &mut impl FileStorageTrait) -> Result<()>
fn write(&self, fs: &mut impl FileStorageTrait) -> Result<()>
source§fn write_1(&self, fs: &mut impl FileStorageTrait, name: &str) -> Result<()>
fn write_1(&self, fs: &mut impl FileStorageTrait, name: &str) -> Result<()>
source§fn write_with_name(&self, fs: &Ptr<FileStorage>, name: &str) -> Result<()>
fn write_with_name(&self, fs: &Ptr<FileStorage>, name: &str) -> Result<()>
source§fn write_with_name_def(&self, fs: &Ptr<FileStorage>) -> Result<()>
fn write_with_name_def(&self, fs: &Ptr<FileStorage>) -> Result<()>
§Note
source§fn empty(&self) -> Result<bool>
fn empty(&self) -> Result<bool>
source§fn save(&self, filename: &str) -> Result<()>
fn save(&self, filename: &str) -> Result<()>
source§fn get_default_name(&self) -> Result<String>
fn get_default_name(&self) -> Result<String>
source§impl Boxed for FaceRecognizer
impl Boxed for FaceRecognizer
source§unsafe fn from_raw(
ptr: <FaceRecognizer as OpenCVFromExtern>::ExternReceive
) -> Self
unsafe fn from_raw( ptr: <FaceRecognizer as OpenCVFromExtern>::ExternReceive ) -> Self
source§fn into_raw(
self
) -> <FaceRecognizer as OpenCVTypeExternContainer>::ExternSendMut
fn into_raw( self ) -> <FaceRecognizer as OpenCVTypeExternContainer>::ExternSendMut
source§fn as_raw(&self) -> <FaceRecognizer as OpenCVTypeExternContainer>::ExternSend
fn as_raw(&self) -> <FaceRecognizer as OpenCVTypeExternContainer>::ExternSend
source§fn as_raw_mut(
&mut self
) -> <FaceRecognizer as OpenCVTypeExternContainer>::ExternSendMut
fn as_raw_mut( &mut self ) -> <FaceRecognizer as OpenCVTypeExternContainer>::ExternSendMut
source§impl Debug for FaceRecognizer
impl Debug for FaceRecognizer
source§impl Drop for FaceRecognizer
impl Drop for FaceRecognizer
source§impl FaceRecognizerTrait for FaceRecognizer
impl FaceRecognizerTrait for FaceRecognizer
fn as_raw_mut_FaceRecognizer(&mut self) -> *mut c_void
source§fn train(
&mut self,
src: &impl ToInputArray,
labels: &impl ToInputArray
) -> Result<()>
fn train( &mut self, src: &impl ToInputArray, labels: &impl ToInputArray ) -> Result<()>
source§fn update(
&mut self,
src: &impl ToInputArray,
labels: &impl ToInputArray
) -> Result<()>
fn update( &mut self, src: &impl ToInputArray, labels: &impl ToInputArray ) -> Result<()>
source§fn read(&mut self, filename: &str) -> Result<()>
fn read(&mut self, filename: &str) -> Result<()>
source§fn read_1(&mut self, fn_: &impl FileNodeTraitConst) -> Result<()>
fn read_1(&mut self, fn_: &impl FileNodeTraitConst) -> Result<()>
source§impl FaceRecognizerTraitConst for FaceRecognizer
impl FaceRecognizerTraitConst for FaceRecognizer
fn as_raw_FaceRecognizer(&self) -> *const c_void
source§fn predict_label(&self, src: &impl ToInputArray) -> Result<i32>
fn predict_label(&self, src: &impl ToInputArray) -> Result<i32>
source§fn predict(
&self,
src: &impl ToInputArray,
label: &mut i32,
confidence: &mut f64
) -> Result<()>
fn predict( &self, src: &impl ToInputArray, label: &mut i32, confidence: &mut f64 ) -> Result<()>
source§fn predict_collect(
&self,
src: &impl ToInputArray,
collector: Ptr<PredictCollector>
) -> Result<()>
fn predict_collect( &self, src: &impl ToInputArray, collector: Ptr<PredictCollector> ) -> Result<()>
source§fn write(&self, filename: &str) -> Result<()>
fn write(&self, filename: &str) -> Result<()>
source§fn write_1(&self, fs: &mut impl FileStorageTrait) -> Result<()>
fn write_1(&self, fs: &mut impl FileStorageTrait) -> Result<()>
source§fn empty(&self) -> Result<bool>
fn empty(&self) -> Result<bool>
source§fn get_label_info(&self, label: i32) -> Result<String>
fn get_label_info(&self, label: i32) -> Result<String>
source§fn get_labels_by_string(&self, str: &str) -> Result<Vector<i32>>
fn get_labels_by_string(&self, str: &str) -> Result<Vector<i32>>
source§fn get_threshold(&self) -> Result<f64>
fn get_threshold(&self) -> Result<f64>
source§impl From<BasicFaceRecognizer> for FaceRecognizer
impl From<BasicFaceRecognizer> for FaceRecognizer
source§fn from(s: BasicFaceRecognizer) -> Self
fn from(s: BasicFaceRecognizer) -> Self
source§impl From<EigenFaceRecognizer> for FaceRecognizer
impl From<EigenFaceRecognizer> for FaceRecognizer
source§fn from(s: EigenFaceRecognizer) -> Self
fn from(s: EigenFaceRecognizer) -> Self
source§impl From<FaceRecognizer> for Algorithm
impl From<FaceRecognizer> for Algorithm
source§fn from(s: FaceRecognizer) -> Self
fn from(s: FaceRecognizer) -> Self
source§impl From<FisherFaceRecognizer> for FaceRecognizer
impl From<FisherFaceRecognizer> for FaceRecognizer
source§fn from(s: FisherFaceRecognizer) -> Self
fn from(s: FisherFaceRecognizer) -> Self
source§impl From<LBPHFaceRecognizer> for FaceRecognizer
impl From<LBPHFaceRecognizer> for FaceRecognizer
source§fn from(s: LBPHFaceRecognizer) -> Self
fn from(s: LBPHFaceRecognizer) -> Self
source§impl TryFrom<FaceRecognizer> for BasicFaceRecognizer
impl TryFrom<FaceRecognizer> for BasicFaceRecognizer
source§impl TryFrom<FaceRecognizer> for EigenFaceRecognizer
impl TryFrom<FaceRecognizer> for EigenFaceRecognizer
source§impl TryFrom<FaceRecognizer> for FisherFaceRecognizer
impl TryFrom<FaceRecognizer> for FisherFaceRecognizer
source§impl TryFrom<FaceRecognizer> for LBPHFaceRecognizer
impl TryFrom<FaceRecognizer> for LBPHFaceRecognizer
impl Send for FaceRecognizer
Auto Trait Implementations§
impl Freeze for FaceRecognizer
impl RefUnwindSafe for FaceRecognizer
impl !Sync for FaceRecognizer
impl Unpin for FaceRecognizer
impl UnwindSafe for FaceRecognizer
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<Mat> ModifyInplace for Matwhere
Mat: Boxed,
impl<Mat> ModifyInplace for Matwhere
Mat: Boxed,
source§unsafe fn modify_inplace<Res>(
&mut self,
f: impl FnOnce(&Mat, &mut Mat) -> Res
) -> Res
unsafe fn modify_inplace<Res>( &mut self, f: impl FnOnce(&Mat, &mut Mat) -> Res ) -> Res
Mat
or another similar object. By passing
a mutable reference to the Mat
to this function your closure will get called with the read reference and a write references
to the same Mat
. This is of course unsafe as it breaks the Rust aliasing rules, but it might be useful for some performance
sensitive operations. One example of an OpenCV function that allows such in-place modification is imgproc::threshold
. Read more