opencv::hub_prelude

Trait FacemarkTrainTrait

source
pub trait FacemarkTrainTrait: FacemarkTrainTraitConst + FacemarkTrait {
    // Required method
    fn as_raw_mut_FacemarkTrain(&mut self) -> *mut c_void;

    // Provided methods
    fn add_training_sample(
        &mut self,
        image: &impl ToInputArray,
        landmarks: &impl ToInputArray,
    ) -> Result<bool> { ... }
    unsafe fn training(&mut self, parameters: *mut c_void) -> Result<()> { ... }
    fn training_def(&mut self) -> Result<()> { ... }
    fn set_face_detector(&mut self, detector: FN_FaceDetector) -> Result<bool> { ... }
    fn get_faces(
        &mut self,
        image: &impl ToInputArray,
        faces: &mut impl ToOutputArray,
    ) -> Result<bool> { ... }
    unsafe fn get_data(&mut self, items: *mut c_void) -> Result<bool> { ... }
    fn get_data_def(&mut self) -> Result<bool> { ... }
}
Expand description

Mutable methods for crate::face::FacemarkTrain

Required Methods§

Provided Methods§

source

fn add_training_sample( &mut self, image: &impl ToInputArray, landmarks: &impl ToInputArray, ) -> Result<bool>

Add one training sample to the trainer.

§Parameters
  • 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
source

unsafe fn training(&mut self, parameters: *mut c_void) -> Result<()>

Trains a Facemark algorithm using the given dataset. Before the training process, training samples should be added to the trainer using face::addTrainingSample function.

§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();
§C++ default parameters
  • parameters: 0
source

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

Trains a Facemark algorithm using the given dataset. Before the training process, training samples should be added to the trainer using face::addTrainingSample function.

§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();
§Note

This alternative version of FacemarkTrainTrait::training function uses the following default values for its arguments:

  • parameters: 0
source

fn set_face_detector(&mut self, detector: FN_FaceDetector) -> Result<bool>

Set a user defined face detector for the Facemark algorithm.

§Parameters
  • detector: The user defined face detector function
  • userData: 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”.

§C++ default parameters
  • user_data: 0
source

fn get_faces( &mut self, image: &impl ToInputArray, faces: &mut impl ToOutputArray, ) -> Result<bool>

Detect faces from a given image using default or user defined face detector. Some Algorithm might not provide a default face detector.

§Parameters
  • 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));
}
source

unsafe fn get_data(&mut self, items: *mut c_void) -> Result<bool>

Get data from an algorithm

§Parameters
  • 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;
§C++ default parameters
  • items: 0
source

fn get_data_def(&mut self) -> Result<bool>

Get data from an algorithm

§Parameters
  • 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;
§Note

This alternative version of FacemarkTrainTrait::get_data function uses the following default values for its arguments:

  • items: 0

Object Safety§

This trait is not object safe.

Implementors§