SmartFace Embedded Toolkit  4.2.1
Loading...
Searching...
No Matches
example_face_demographic_attributes.cpp

Example of use of sfe_face library - demographic attributes (age, gender)

#include "sfe_toolkit/sfe_core.h"
#include "sfe_toolkit/sfe_face.h"
#include "annotate.hpp"
#include "solvers.h"
#include "utils.hpp"
#include <iomanip>
#include <iostream>
#include <sstream>
#include <unordered_map>
#include <vector>
std::string image_probe = "assets/face/images/obiwan0.png";
std::string solver_face_detect = SOLVER_FACE_DETECT;
std::string solver_face_landmarks = SOLVER_FACE_LANDMARKS;
std::string solver_face_demographic = SOLVER_FACE_DEMOGRAPHIC_ATTRIBUTES;
const auto SOLVER_PARAMETERS = std::vector<SFESolverParameter>{};
size_t face_size_min = 28;
size_t face_size_max = 170;
float detection_threshold = 0.1f;
void printHelp() {
std::cout << "Help: Usage of the program." << std::endl;
std::cout << "Options:" << std::endl;
std::cout << "-h: Display help." << std::endl;
std::cout << "-p: Probe image file." << std::endl;
std::cout << "-d: Path to detector solver." << std::endl;
std::cout << "-l: Path to landmarks solver." << std::endl;
std::cout << "-g: Path to demographic attributes solver." << std::endl;
std::cout << "-t: Face detection threshold. <0,1>" << std::endl;
std::cout << "-m: Minimal face size in pixels to detect." << std::endl;
std::cout << "-x: Max face size in pixels to detect." << std::endl;
}
int main(int argc, char *argv[]) {
{ // STAGE 0: Parse command line arguments
std::unordered_map<std::string, std::string> args;
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg[0] == '-') {
if (arg == "-h") {
return 0;
}
if (i + 1 < argc && argv[i + 1][0] != '-') {
args[arg] = argv[++i];
} else {
std::cerr << "Option " << arg << " requires a value." << std::endl;
return 1;
}
} else {
std::cerr << "Unknown option: " << arg << std::endl;
return 1;
}
}
if (args.count("-p"))
image_probe = args["-p"];
if (args.count("-d"))
solver_face_detect = args["-d"];
if (args.count("-l"))
solver_face_landmarks = args["-l"];
if (args.count("-g"))
if (args.count("-t"))
detection_threshold = std::stof(args["-t"]);
if (args.count("-m"))
face_size_min = std::stoi(args["-m"]);
if (args.count("-x"))
face_size_max = std::stoi(args["-x"]);
utils::printFormatted("PARAMETERS");
std::cout << "Probe image: " << image_probe << std::endl;
std::cout << "Face detection solver: " << solver_face_detect << std::endl;
std::cout << "Face landmarks solver: " << solver_face_landmarks << std::endl;
std::cout << "Face demographic attributes solver: " << solver_face_demographic
<< std::endl;
std::cout << "Min face size [px]: " << face_size_min << std::endl;
std::cout << "Max face size [px]: " << face_size_max << std::endl;
std::cout << "Detection threshold: " << detection_threshold << std::endl;
}
utils::printToolkitInfo();
SFEError error{};
SFESolver detector_solver{};
DEFER(sfeSolverFree(detector_solver));
SFESolver landmarks_solver{};
DEFER(sfeSolverFree(landmarks_solver));
SFESolver demographic_solver{};
DEFER(sfeSolverFree(demographic_solver));
{ // STAGE 1: loading solvers
utils::printFormatted("LOADING solvers");
error = sfeSolverCreate(
SOLVER_PARAMETERS.size(), &detector_solver);
utils::checkError(error);
error = sfeSolverCreate(
SOLVER_PARAMETERS.size(), &landmarks_solver);
utils::checkError(error);
error = sfeSolverCreate(
SOLVER_PARAMETERS.size(), &demographic_solver);
utils::checkError(error);
}
SFEImage image{};
DEFER(sfeImageFree(image));
{ // STAGE 2: Load image
utils::printFormatted("FACE DETECTION");
// Load image data from file
auto image_data = utils::readFile(image_probe);
// Decode image from data
error = sfeImageDecode(image_data.data(), image_data.size(), &image);
utils::checkError(error);
}
SFEDetection detected_face = {};
{ // STAGE 3: Detect face in the image
size_t detection_count = 1;
error = sfeDetect(detector_solver, image, detection_threshold,
&detected_face, &detection_count);
utils::checkError(error);
if (detection_count == 0) {
std::cout << "No face detected in the probe image." << std::endl;
return 0;
} else {
std::cout << "Found " << detection_count
<< " face(s) in the probe image. Using the face with highest "
"confidence."
<< std::endl;
}
}
std::vector<SFEFaceLandmarks> landmarks(SFE_FACE_LANDMARK_COUNT);
{ // STAGE 4: Get face landmarks
error = sfeFaceLandmarks(landmarks_solver, image, &detected_face,
landmarks.data());
utils::checkError(error);
}
{ // STAGE 5: Demographic attributes
utils::printFormatted("FACE DEMOGRAPHIC ATTRIBUTES");
error = sfeFaceDemographicAttributes(demographic_solver, image,
landmarks.data(), &attributes);
utils::checkError(error);
}
{ // STAGE 6: Print demographic attributes
std::cout << "Demographic attributes:" << std::endl;
std::cout << " Age: " << std::fixed << std::setprecision(1) << attributes.age
<< " years" << std::endl;
std::cout << " Gender score: " << std::setprecision(2) << attributes.gender
<< " (" << (attributes.gender > 0.0f ? "female" : "male") << ")"
<< std::endl;
}
utils::printFormatted("FINISHED");
}
std::string solver_face_demographic
void printHelp()
std::string solver_face_detect
Solvers to use in example, the defaults are filled in by CMake.
std::string image_probe
float detection_threshold
const auto SOLVER_PARAMETERS
size_t face_size_max
size_t face_size_min
Required size of the face to be detected.
std::string solver_face_landmarks
void sfeSolverFree(SFESolver solver)
Free memory associated with SFESolver.
void * SFESolver
Solver provides an abstract interface over inference models and engines.
Definition sfe_core.h:102
void * SFEError
Error type used to hold optional error message.
Definition sfe_core.h:97
void sfeImageFree(SFEImage image)
Free memory associated with SFEImage.
SFEError sfeSolverCreate(const char *solver_file, const SFESolverParameter *solver_parameters, size_t solver_parameters_count, SFESolver *out_solver)
Create new solver from solver file.
SFEError sfeImageDecode(const unsigned char *data, size_t data_len, SFEImage *out_image)
Decode SFEImage from raw image data of various formats. Eg. PNG, JPEG ..
SFEError sfeDetect(SFESolver solver, SFEImageView image, float threshold, SFEDetection *out_detections, size_t *in_out_detection_count)
Detect objects in the source image using unified detection API.
SFEError sfeFaceDemographicAttributes(SFESolver solver, SFEImageView image, SFEFaceLandmarks face_landmarks[SFE_FACE_LANDMARK_COUNT], SFEFaceDemographicAttributes *out_demographic_attributes)
Estimate demographic attributes (age, gender) from a face image.
#define SFE_FACE_LANDMARK_COUNT
Definition sfe_face.h:17
SFEError sfeFaceLandmarks(SFESolver solver, SFEImageView image, const SFEDetection *detection, SFEFaceLandmarks out_face_landmarks[SFE_FACE_LANDMARK_COUNT])
Detect 23 landmarks of the face detected in the area of source image marked with detection.
Core detection - tagged union containing all detection types.
Demographic attributes of the detected face.
Definition sfe_face.h:309
Raw owned raster image representation, HWC|BGR order.
Definition sfe_core.h:70