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

Example of use of sfe_palm library

#include "sfe_toolkit/sfe_core.h"
#include "sfe_toolkit/sfe_palm.h"
#include <regex>
#include <vector>
#include <iomanip>
#include <sstream>
#include "annotate.hpp"
#include "solvers.h"
#include "utils.hpp"
#include <unordered_map>
std::string image_match = "assets/palm/palm_id1_r1.jpg";
std::string image_probe = "assets/palm/palm_id1_r2.jpg";
std::string image_non_match = "assets/palm/palm_id2_l1.jpg";
std::string solver_palm_detect = SOLVER_PALM_DETECT;
std::string solver_palm_extraction = SOLVER_PALM_EXTRACTION;
// NOTE: Using the same solver for landmarks as for detection, this is intentional.
std::string solver_palm_landmarks = SOLVER_PALM_DETECT;
const auto SOLVER_PARAMETERS = std::vector<SFESolverParameter>{};
SFESolver &detector_solver, SFESolver &landmarks_solver, SFESolver &extraction_solver) {
SFEImage image{};
DEFER(sfeImageFree(image));
SFEError error{};
{ // STAGE 1: Load image
// Load image data from file
auto image_data = utils::readFile(image_path);
// Decode image from data
error = sfeImageDecode(image_data.data(), image_data.size(), &image);
utils::checkError(error);
}
SFEDetection detected_palm = {};
{ // STAGE 2: Detect palm in the image
// Detect palm in the image
float detection_threshold = 0.1f;
size_t detected_count = 1;
error = sfeDetect(detector_solver, image, detection_threshold,
&detected_palm, &detected_count);
utils::checkError(error);
if (detected_count > 0 && detected_palm.confidence < 0.5) {
throw std::runtime_error("No palm detected in the probe image.");
}
std::cout << "Found palm in the image. Extracting template..." << std::endl;
}
SFEPalmLandmarks landmarks = {};
{ // STAGE 3: Extract palm landmarks
// Extract palm landmarks (hand position, orientation, keypoints)
// These are required for template extraction
error = sfePalmLandmarks(landmarks_solver, image, &detected_palm, &landmarks);
utils::checkError(error);
}
SFEPalmTemplate palm_template;
{ // STAGE 4: Extract probe palm template
// Use template extraction solver to create new palm template
// Requires palm landmarks from previous step
error = sfePalmTemplateExtract(extraction_solver, image, &landmarks,
&palm_template);
utils::checkError(error);
}
return palm_template;
}
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 << "-g: Matching image file. Our \"palm database\"." << std::endl;
std::cout << "-d: Path to detector solver." << std::endl;
std::cout << "-l: Path to landmarks solver." << std::endl;
std::cout << "-e: Path to template extraction solver." << std::endl;
std::cout << "-t: Detection threshold. <0,1>" << std::endl;
std::cout << "-i: Identification threshold. <0,1>" << std::endl;
}
int main(int argc, char *argv[]) {
{ // STAGE 0: Parse command line arguments
// Map to store argument values
std::unordered_map<std::string, std::string> args;
// Parse command line arguments
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg[0] == '-') {
if (arg == "-h") {
return 0;
}
// Check if there's a next argument and it isn't another option
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;
}
}
// Assign values to variables based on parsed arguments
if (args.count("-p"))
image_probe = args["-p"];
if (args.count("-g"))
image_match = args["-g"];
if (args.count("-d"))
solver_palm_detect = args["-d"];
if (args.count("-l"))
solver_palm_landmarks = args["-l"];
if (args.count("-e"))
solver_palm_extraction = args["-e"];
if (args.count("-i"))
identification_threshold = std::stof(args["-i"]);
utils::printFormatted("EXAMPLE PARAMETERS");
// Print resource names
std::cout << "Probe image: " << image_probe << std::endl;
std::cout << "Matching image: " << image_match << std::endl;
// Print solver names
std::cout << "Palm detection solver: " << solver_palm_detect << std::endl;
std::cout << "Palm landmarks solver: " << solver_palm_landmarks << std::endl;
std::cout << "Palm extraction solver: " << solver_palm_extraction << std::endl;
// Print other options
std::cout << "Identification threshold: " << identification_threshold
<< std::endl;
}
utils::printToolkitInfo();
SFEError error{};
SFESolver detector_solver{};
DEFER(sfeSolverFree(detector_solver));
SFESolver landmarks_solver{};
DEFER(sfeSolverFree(landmarks_solver));
SFESolver extraction_solver{};
DEFER(sfeSolverFree(extraction_solver));
{ // STAGE 1.1: loading solvers
utils::printFormatted("LOADING SOLVERS");
// Initialize Palm detection solver
error = sfeSolverCreate(
SOLVER_PARAMETERS.size(), &detector_solver);
utils::checkError(error);
// Initialize Palm landmarks solver (using same solver as detection)
error = sfeSolverCreate(
SOLVER_PARAMETERS.size(), &landmarks_solver);
utils::checkError(error);
// Initialize Palm extraction solver
error = sfeSolverCreate(
SOLVER_PARAMETERS.size(), &extraction_solver);
utils::checkError(error);
}
SFEPalmTemplate probe_palm_template;
{ // STAGE 1: Extract probe palm template
utils::printFormatted("PROBE TEMPLATE EXTRACTION");
probe_palm_template = extract_template(image_probe, detector_solver, landmarks_solver, extraction_solver);
}
{ // STAGE 1.1: (optional) Print template attributes
utils::printFormatted("PROBE TEMPLATE ATTRIBUTES");
SFEPalmTemplateVersion version = {};
error = sfePalmTemplateVersion(&probe_palm_template, &version);
utils::checkError(error);
std::cout << "Version of the probe template: " << version.major << '.'
<< version.minor << '.' << version.patch << std::endl;
}
size_t gallery_size = 10;
std::vector<SFEPalmTemplate> palm_template_gallery(gallery_size);
{ // STAGE 2: Create palm templates gallery
utils::printFormatted("PALM GALLERY EXTRACTION");
auto matching_palm_template =
extract_template(image_match, detector_solver, landmarks_solver, extraction_solver);
auto non_matching_palm_template =
extract_template(image_non_match, detector_solver, landmarks_solver, extraction_solver);
// Fill the gallery with non-matching templates for demonstration purposes
for (size_t i = 0; i < gallery_size; i++) {
palm_template_gallery[i] = non_matching_palm_template;
}
// Add matching template to the gallery at index 5
palm_template_gallery[5] = matching_palm_template;
}
size_t candidate_count = 1;
std::vector<SFETemplateIdentificationCandidate> identification_results(
candidate_count);
{ // STAGE 3: Identify the probe template
utils::printFormatted("1:N IDENTIFICATION");
&probe_palm_template, palm_template_gallery.data(),
palm_template_gallery.size(), identification_threshold,
identification_results.data(), &candidate_count, 4);
utils::checkError(error);
if (candidate_count == 0) {
std::cout << "No candidates found above identification threshold "
<< identification_threshold << std::endl;
return 0;
}
std::cout << "Found " << identification_results.size()
<< " candidates above identification threshold "
<< identification_threshold << std::endl;
for (auto &result : identification_results)
std::cout << "Template index: #" << result.index
<< ", score: " << result.score << std::endl;
}
{ // STAGE 4: 1:1 matching with top candidate to showcase 1:1 matching
utils::printFormatted("1:1 MATCHING");
if (identification_results.size() == 0) {
std::cout << "No candidates found above identification threshold "
<< identification_threshold << std::endl;
return 0;
}
// Since it's sorted vector by score, the best candidate is the first one
auto match_palm_template =
palm_template_gallery[identification_results[0].index];
float match_confidence;
error = sfePalmTemplateMatch(&probe_palm_template, &match_palm_template,
&match_confidence);
utils::checkError(error);
std::cout
<< "Matching score of probe template with the best template, score: "
<< match_confidence << std::endl;
}
utils::printFormatted("FINISHED");
}
void printHelp()
float identification_threshold
std::string image_probe
float detection_threshold
const auto SOLVER_PARAMETERS
SFEIrisTemplate extract_template(std::string image_path, SFESolver &detector_solver, SFESolver &template_solver)
std::string image_match
std::string image_non_match
std::string solver_palm_landmarks
std::string solver_palm_detect
Solvers to use in example, the defaults are filled in by CMake.
std::string solver_palm_extraction
std::string image_path
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 sfePalmLandmarks(SFESolver solver, SFEImageView image, const SFEDetection *detection, SFEPalmLandmarks *out_landmarks)
Calculate palm landmarks from given source image and palm detection.
SFEError sfePalmTemplateMatch(const SFEPalmTemplate *template1, const SFEPalmTemplate *template2, float *out_matching_score)
Match two palm templates.
SFEError sfePalmTemplateExtract(SFESolver solver, SFEImageView image, const SFEPalmLandmarks *landmarks, SFEPalmTemplate *out_palm_template)
Extract template from source image and given palm attributes.
SFEError sfePalmTemplateIdentify(const SFEPalmTemplate *probe_palm_template, const SFEPalmTemplate *templates_gallery, size_t gallery_size, float matching_score_threshold, SFETemplateIdentificationCandidate *out_candidates, size_t *in_out_candidates_count, size_t thread_count)
Get an ordered array of SFETemplateIdentificationCandidate from probe's template best matches with te...
SFEError sfePalmTemplateVersion(const SFEPalmTemplate *palm_template, SFEPalmTemplateVersion *out_palm_template_version)
Get palm template version.
Core detection - tagged union containing all detection types.
float confidence
Detection confidence - range <0,1> (common for all detection types)
Raw owned raster image representation, HWC|BGR order.
Definition sfe_core.h:70
Palm landmarks (keypoints, hand position, orientation, confidence)
Palm template struct.
Definition sfe_palm.h:22
Palm template version struct.
Definition sfe_palm.h:27
uint8_t patch
Patch template version.
Definition sfe_palm.h:33
uint8_t minor
Minor template version.
Definition sfe_palm.h:31
uint8_t major
Major template version.
Definition sfe_palm.h:29