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

source§

fn as_raw_mut_Algorithm(&mut self) -> *mut c_void

source§

fn clear(&mut self) -> Result<()>

Clears the algorithm state
source§

fn read(&mut self, fn_: &impl FileNodeTraitConst) -> Result<()>

Reads algorithm parameters from a file storage
source§

impl AlgorithmTraitConst for FaceRecognizer

source§

fn as_raw_Algorithm(&self) -> *const c_void

source§

fn write(&self, fs: &mut impl FileStorageTrait) -> Result<()>

Stores algorithm parameters in a file storage
source§

fn write_1(&self, fs: &mut impl FileStorageTrait, name: &str) -> Result<()>

Stores algorithm parameters in a file storage Read more
source§

fn write_with_name(&self, fs: &Ptr<FileStorage>, name: &str) -> Result<()>

@deprecated Read more
source§

fn write_with_name_def(&self, fs: &Ptr<FileStorage>) -> Result<()>

👎Deprecated:

§Note

Deprecated: ## Note This alternative version of AlgorithmTraitConst::write_with_name function uses the following default values for its arguments: Read more
source§

fn empty(&self) -> Result<bool>

Returns true if the Algorithm is empty (e.g. in the very beginning or after unsuccessful read
source§

fn save(&self, filename: &str) -> Result<()>

Saves the algorithm to a file. In order to make this method work, the derived class must implement Algorithm::write(FileStorage& fs).
source§

fn get_default_name(&self) -> Result<String>

Returns the algorithm string identifier. This string is used as top level xml/yml node tag when the object is saved to a file or string.
source§

impl Boxed for FaceRecognizer

source§

unsafe fn from_raw( ptr: <FaceRecognizer as OpenCVFromExtern>::ExternReceive ) -> Self

Wrap the specified raw pointer Read more
source§

fn into_raw( self ) -> <FaceRecognizer as OpenCVTypeExternContainer>::ExternSendMut

Return the underlying raw pointer while consuming this wrapper. Read more
source§

fn as_raw(&self) -> <FaceRecognizer as OpenCVTypeExternContainer>::ExternSend

Return the underlying raw pointer. Read more
source§

fn as_raw_mut( &mut self ) -> <FaceRecognizer as OpenCVTypeExternContainer>::ExternSendMut

Return the underlying mutable raw pointer Read more
source§

impl Debug for FaceRecognizer

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Drop for FaceRecognizer

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl FaceRecognizerTrait for FaceRecognizer

source§

fn as_raw_mut_FaceRecognizer(&mut self) -> *mut c_void

source§

fn train( &mut self, src: &impl ToInputArray, labels: &impl ToInputArray ) -> Result<()>

Trains a FaceRecognizer with given data and associated labels. Read more
source§

fn update( &mut self, src: &impl ToInputArray, labels: &impl ToInputArray ) -> Result<()>

Updates a FaceRecognizer with given data and associated labels. Read more
source§

fn read(&mut self, filename: &str) -> Result<()>

Loads a FaceRecognizer and its model state. Read more
source§

fn read_1(&mut self, fn_: &impl FileNodeTraitConst) -> Result<()>

Loads a FaceRecognizer and its model state. Read more
source§

fn set_label_info(&mut self, label: i32, str_info: &str) -> Result<()>

Sets string info for the specified model’s label. Read more
source§

fn set_threshold(&mut self, val: f64) -> Result<()>

Sets threshold of model
source§

impl FaceRecognizerTraitConst for FaceRecognizer

source§

fn as_raw_FaceRecognizer(&self) -> *const c_void

source§

fn predict_label(&self, src: &impl ToInputArray) -> Result<i32>

Predicts a label and associated confidence (e.g. distance) for a given input image. Read more
source§

fn predict( &self, src: &impl ToInputArray, label: &mut i32, confidence: &mut f64 ) -> Result<()>

Predicts a label and associated confidence (e.g. distance) for a given input image. Read more
source§

fn predict_collect( &self, src: &impl ToInputArray, collector: Ptr<PredictCollector> ) -> Result<()>

if implemented - send all result of prediction to collector that can be used for somehow custom result handling Read more
source§

fn write(&self, filename: &str) -> Result<()>

Saves a FaceRecognizer and its model state. Read more
source§

fn write_1(&self, fs: &mut impl FileStorageTrait) -> Result<()>

Saves a FaceRecognizer and its model state. Read more
source§

fn empty(&self) -> Result<bool>

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
source§

fn get_label_info(&self, label: i32) -> Result<String>

Gets string information by label. Read more
source§

fn get_labels_by_string(&self, str: &str) -> Result<Vector<i32>>

Gets vector of labels by string. Read more
source§

fn get_threshold(&self) -> Result<f64>

threshold parameter accessor - required for default BestMinDist collector
source§

impl From<BasicFaceRecognizer> for FaceRecognizer

source§

fn from(s: BasicFaceRecognizer) -> Self

Converts to this type from the input type.
source§

impl From<EigenFaceRecognizer> for FaceRecognizer

source§

fn from(s: EigenFaceRecognizer) -> Self

Converts to this type from the input type.
source§

impl From<FaceRecognizer> for Algorithm

source§

fn from(s: FaceRecognizer) -> Self

Converts to this type from the input type.
source§

impl From<FisherFaceRecognizer> for FaceRecognizer

source§

fn from(s: FisherFaceRecognizer) -> Self

Converts to this type from the input type.
source§

impl From<LBPHFaceRecognizer> for FaceRecognizer

source§

fn from(s: LBPHFaceRecognizer) -> Self

Converts to this type from the input type.
source§

impl TryFrom<FaceRecognizer> for BasicFaceRecognizer

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(s: FaceRecognizer) -> Result<Self>

Performs the conversion.
source§

impl TryFrom<FaceRecognizer> for EigenFaceRecognizer

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(s: FaceRecognizer) -> Result<Self>

Performs the conversion.
source§

impl TryFrom<FaceRecognizer> for FisherFaceRecognizer

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(s: FaceRecognizer) -> Result<Self>

Performs the conversion.
source§

impl TryFrom<FaceRecognizer> for LBPHFaceRecognizer

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(s: FaceRecognizer) -> Result<Self>

Performs the conversion.
source§

impl Send for FaceRecognizer

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<Mat> ModifyInplace for Mat
where Mat: Boxed,

source§

unsafe fn modify_inplace<Res>( &mut self, f: impl FnOnce(&Mat, &mut Mat) -> Res ) -> Res

Helper function to call OpenCV functions that allow in-place modification of a 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
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.