@Namespace(value="cv::face") @Properties(inherit=opencv_face.class) public class FacemarkTrain extends Facemark
To utilize this API in your program, please take a look at the \ref tutorial_table_of_content_facemark ### Description
The AAM and LBF facemark models in OpenCV are derived from the abstract base class FacemarkTrain, which provides a unified access to those facemark algorithms in OpenCV.
Here is an example on how to declare facemark algorithm:
// Using Facemark in your code:
Ptr<Facemark> facemark = FacemarkLBF::create();
The typical pipeline for facemark detection is listed as follows: - (Non-mandatory) Set a user defined face detection using FacemarkTrain::setFaceDetector. The facemark algorithms are designed to fit the facial points into a face. Therefore, the face information should be provided to the facemark algorithm. Some algorithms might provides a default face recognition function. However, the users might prefer to use their own face detector to obtains the best possible detection result. - (Non-mandatory) Training the model for a specific algorithm using FacemarkTrain::training. In this case, the model should be automatically saved by the algorithm. If the user already have a trained model, then this part can be omitted. - Load the trained model using Facemark::loadModel. - Perform the fitting via the Facemark::fit.
Pointer.CustomDeallocator, Pointer.Deallocator, Pointer.NativeDeallocator, Pointer.ReferenceCounter| Constructor and Description |
|---|
FacemarkTrain(Pointer p)
Pointer cast constructor.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
addTrainingSample(Mat image,
Point2fVector landmarks)
\brief Add one training sample to the trainer.
|
boolean |
getData() |
boolean |
getData(Pointer items)
\brief Get data from an algorithm
|
boolean |
getFaces(Mat image,
RectVector faces)
\brief Detect faces from a given image using default or user defined face detector.
|
boolean |
setFaceDetector(Pointer detector) |
boolean |
setFaceDetector(Pointer detector,
Pointer userData)
\brief Set a user defined face detector for the Facemark algorithm.
|
void |
training() |
void |
training(Pointer parameters)
\brief Trains a Facemark algorithm using the given dataset.
|
clear, empty, getDefaultName, position, read, save, save, write, write, writeaddress, asBuffer, asByteBuffer, availablePhysicalBytes, calloc, capacity, capacity, close, deallocate, deallocate, deallocateReferences, deallocator, deallocator, equals, fill, formatBytes, free, hashCode, isNull, isNull, limit, limit, malloc, maxBytes, maxPhysicalBytes, memchr, memcmp, memcpy, memmove, memset, offsetof, parseBytes, physicalBytes, position, put, realloc, referenceCount, releaseReference, retainReference, setNull, sizeof, toString, totalBytes, totalPhysicalBytes, withDeallocator, zeropublic FacemarkTrain(Pointer p)
Pointer.Pointer(Pointer).@Cast(value="bool") public boolean addTrainingSample(@ByVal Mat image, @ByRef Point2fVector landmarks)
image - Input image.landmarks - The ground-truth of facial landmarks points corresponds to the image.
Example of usage
String imageFiles = "../data/images_train.txt";
String ptsFiles = "../data/points_train.txt";
std::vector<String> images_train;
std::vector<String> landmarks_train;
// load the list of dataset: image paths and landmark file paths
loadDatasetList(imageFiles,ptsFiles,images_train,landmarks_train);
Mat image;
std::vector<Point2f> facial_points;
for(size_t i=0;i<images_train.size();i++){
image = imread(images_train[i].c_str());
loadFacePoints(landmarks_train[i],facial_points);
facemark->addTrainingSample(image, facial_points);
}
The contents in the training files should follows the standard format. Here are examples for the contents in these files. example of content in the images_train.txt
/home/user/ibug/image_003_1.jpg
/home/user/ibug/image_004_1.jpg
/home/user/ibug/image_005_1.jpg
/home/user/ibug/image_006.jpg
example of content in the points_train.txt
/home/user/ibug/image_003_1.pts
/home/user/ibug/image_004_1.pts
/home/user/ibug/image_005_1.pts
/home/user/ibug/image_006.pts
public void training(Pointer parameters)
parameters - Optional extra parameters (algorithm dependent).
Example of usage
FacemarkLBF::Params params;
params.model_filename = "ibug68.model"; // filename to save the trained model
Ptr<Facemark> facemark = FacemarkLBF::create(params);
// add training samples (see Facemark::addTrainingSample)
facemark->training();
public void training()
@Cast(value="bool") public boolean setFaceDetector(@Cast(value="cv::face::FN_FaceDetector") Pointer detector, Pointer userData)
detector - The user defined face detector functionuserData - Detector parameters
Example of usage
MyDetectorParameters detectorParameters(...);
facemark->setFaceDetector(myDetector, &detectorParameters);
Example of a user defined face detector
bool myDetector( InputArray image, OutputArray faces, void* userData)
{
MyDetectorParameters* params = (MyDetectorParameters*)userData;
// -------- do something --------
}
TODO Lifetime of detector parameters is uncontrolled. Rework interface design to "Ptr
@Cast(value="bool") public boolean setFaceDetector(@Cast(value="cv::face::FN_FaceDetector") Pointer detector)
@Cast(value="bool") public boolean getFaces(@ByVal Mat image, @ByRef RectVector faces)
image - Input image.faces - Output of the function which represent region of interest of the detected faces. Each face is stored in cv::Rect container.
Example of usage
std::vector<cv::Rect> faces;
facemark->getFaces(img, faces);
for(int j=0;j<faces.size();j++){
cv::rectangle(img, faces[j], cv::Scalar(255,0,255));
}
@Cast(value="bool") public boolean getData(Pointer items)
items - The obtained data, algorithm dependent.
Example of usage
Ptr<FacemarkAAM> facemark = FacemarkAAM::create();
facemark->loadModel("AAM.yml");
FacemarkAAM::Data data;
facemark->getData(&data);
std::vector<Point2f> s0 = data.s0;
cout<<s0<<endl;
Copyright © 2020. All rights reserved.