OpenCV for Unity 2.6.4
Enox Software / Please refer to OpenCV official document ( http://docs.opencv.org/4.10.0/index.html ) for the details of the argument of the method.
|
Abstract base class for all face recognition models. More...
Public Member Functions | |
string | getLabelInfo (int label) |
Gets string information by label. | |
MatOfInt | getLabelsByString (string str) |
Gets vector of labels by string. | |
void | predict (Mat src, int[] label, double[] confidence) |
Predicts a label and associated confidence (e.g. distance) for a given input image. | |
void | predict_collect (Mat src, PredictCollector collector) |
int | predict_label (Mat src) |
void | read (string filename) |
Loads a FaceRecognizer and its model state. | |
void | setLabelInfo (int label, string strInfo) |
Sets string info for the specified model's label. | |
void | train (List< Mat > src, Mat labels) |
Trains a FaceRecognizer with given data and associated labels. | |
void | update (List< Mat > src, Mat labels) |
Updates a FaceRecognizer with given data and associated labels. | |
void | write (string filename) |
Saves a FaceRecognizer and its model state. | |
Public Member Functions inherited from OpenCVForUnity.CoreModule.Algorithm | |
virtual void | clear () |
Clears the algorithm state. | |
virtual bool | empty () |
Returns true if the Algorithm is empty (e.g. in the very beginning or after unsuccessful read. | |
virtual string | getDefaultName () |
IntPtr | getNativeObjAddr () |
void | save (string filename) |
Public Member Functions inherited from OpenCVForUnity.DisposableObject | |
void | Dispose () |
void | ThrowIfDisposed () |
Static Public Member Functions | |
static new FaceRecognizer | __fromPtr__ (IntPtr addr) |
Static Public Member Functions inherited from OpenCVForUnity.CoreModule.Algorithm | |
static Algorithm | __fromPtr__ (IntPtr addr) |
Static Public Member Functions inherited from OpenCVForUnity.DisposableObject | |
static IntPtr | ThrowIfNullIntPtr (IntPtr ptr) |
Protected Member Functions | |
override void | Dispose (bool disposing) |
Protected Member Functions inherited from OpenCVForUnity.CoreModule.Algorithm | |
Protected Member Functions inherited from OpenCVForUnity.DisposableOpenCVObject | |
DisposableOpenCVObject () | |
DisposableOpenCVObject (bool isEnabledDispose) | |
DisposableOpenCVObject (IntPtr ptr) | |
DisposableOpenCVObject (IntPtr ptr, bool isEnabledDispose) | |
Protected Member Functions inherited from OpenCVForUnity.DisposableObject | |
DisposableObject () | |
DisposableObject (bool isEnabledDispose) | |
Additional Inherited Members | |
Package Functions inherited from OpenCVForUnity.CoreModule.Algorithm | |
Package Attributes inherited from OpenCVForUnity.DisposableOpenCVObject | |
Properties inherited from OpenCVForUnity.DisposableObject | |
bool | IsDisposed [get, protected set] |
bool | IsEnabledDispose [get, set] |
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.
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:
Moreover every FaceRecognizer supports the:
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:
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:
If you've set the threshold to 0.0 as we did above, then:
is going to yield -1 as predicted label, which states this face is unknown.
Since every FaceRecognizer is a Algorithm, you can use Algorithm::name to get the name of a FaceRecognizer:
|
static |
|
protectedvirtual |
Reimplemented from OpenCVForUnity.CoreModule.Algorithm.
Reimplemented in OpenCVForUnity.FaceModule.FisherFaceRecognizer, and OpenCVForUnity.FaceModule.LBPHFaceRecognizer.
string OpenCVForUnity.FaceModule.FaceRecognizer.getLabelInfo | ( | int | label | ) |
Gets string information by label.
If an unknown label id is provided or there is no label information associated with the specified label id the method returns an empty string.
MatOfInt OpenCVForUnity.FaceModule.FaceRecognizer.getLabelsByString | ( | string | str | ) |
Gets vector of labels by string.
The function searches for the labels containing the specified sub-string in the associated string info.
void OpenCVForUnity.FaceModule.FaceRecognizer.predict | ( | Mat | src, |
int[] | label, | ||
double[] | confidence ) |
Predicts a label and associated confidence (e.g. distance) for a given input image.
src | Sample image to get a prediction from. |
label | The predicted label for the given image. |
confidence | Associated confidence (e.g. distance) for the predicted label. |
The suffix const means that prediction does not affect the internal model state, so the method can be safely called from within different threads.
The following example shows how to get a prediction from a trained model:
Or to get a prediction and the associated confidence (e.g. distance):
void OpenCVForUnity.FaceModule.FaceRecognizer.predict_collect | ( | Mat | src, |
PredictCollector | collector ) |
src | Sample image to get a prediction from. |
collector | User-defined collector object that accepts all results |
To implement this method u just have to do same internal cycle as in predict(InputArray src, CV_OUT int &label, CV_OUT double &confidence) but not try to get "best@ result, just resend it to caller side with given collector
int OpenCVForUnity.FaceModule.FaceRecognizer.predict_label | ( | Mat | src | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
void OpenCVForUnity.FaceModule.FaceRecognizer.read | ( | string | filename | ) |
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.
void OpenCVForUnity.FaceModule.FaceRecognizer.setLabelInfo | ( | int | label, |
string | strInfo ) |
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.
Trains a FaceRecognizer with given data and associated labels.
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:
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:
And finally train it on the given dataset (the face images and labels):
Updates a FaceRecognizer with given data and associated labels.
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.
Calling update on an Eigenfaces model (see EigenFaceRecognizer.create), which doesn't support updating, will throw an error similar to:
void OpenCVForUnity.FaceModule.FaceRecognizer.write | ( | string | filename | ) |
Saves a FaceRecognizer and its model state.
Saves this model to a given filename, either as XML or YAML.
filename | The filename to store this FaceRecognizer to (either XML/YAML). |
Every FaceRecognizer overwrites FaceRecognizer.save(FileStorage& fs) to save the internal model state. FaceRecognizer.save(const String& filename) saves the state of a model to the given filename.
The suffix const means that prediction does not affect the internal model state, so the method can be safely called from within different threads.