SmartFace Embedded Toolkit  4.2.1
Loading...
Searching...
No Matches
example_palm_attributes.cpp File Reference
#include "sfe_toolkit/sfe_core.h"
#include "sfe_toolkit/sfe_palm.h"
#include <iomanip>
#include <optional>
#include <regex>
#include <sstream>
#include <vector>
#include "annotate.hpp"
#include "solvers.h"
#include "utils.hpp"
#include <unordered_map>

Go to the source code of this file.

Functions

void printPalmDetectionInfo (SFEDetection &detected_palm, SFEPalmLandmarks &attributes)
 
void printHelp ()
 
int main (int argc, char *argv[])
 Main fuction is used to parse command line arguments.
 

Variables

std::string image_probe = "assets/palm/palm_id1_r1.jpg"
 
std::string solver_palm_detect = SOLVER_PALM_DETECT
 Solvers to use in example, the defaults are filled in by CMake.
 
std::optional< std::string > solver_palm_attributes = std::nullopt
 
const auto SOLVER_PARAMETERS = std::vector<SFESolverParameter>{}
 
float detection_threshold = 0.1f
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Main fuction is used to parse command line arguments.

[Image Load]

[Image Load]

[Palm Detect]

[Palm Detect]

[Palm Landmarks]

[Palm Landmarks]

[Palm Attributes]

[Palm Attributes]

Definition at line 67 of file example_palm_attributes.cpp.

67 {
68
69 { // STAGE: 0 Parse command line arguments
70 // Map to store argument values
71 std::unordered_map<std::string, std::string> args;
72
73 // Parse command line arguments
74 for (int i = 1; i < argc; ++i) {
75 std::string arg = argv[i];
76 if (arg[0] == '-') {
77 if (arg == "-h") {
78 printHelp();
79 return 0;
80 }
81 // Check if there's a next argument and it isn't another option
82 if (i + 1 < argc && argv[i + 1][0] != '-') {
83 args[arg] = argv[++i];
84 } else {
85 std::cerr << "Option " << arg << " requires a value." << std::endl;
86 return 1;
87 }
88 } else {
89 std::cerr << "Unknown option: " << arg << std::endl;
90 return 1;
91 }
92 }
93
94 // Assign values to variables based on parsed arguments
95 if (args.count("-p"))
96 image_probe = args["-p"];
97 if (args.count("-d"))
98 solver_palm_detect = args["-d"];
99 if (args.count("-i"))
100 solver_palm_attributes = args["-i"];
101 if (args.count("-t"))
102 detection_threshold = std::stof(args["-t"]);
103
104 utils::printFormatted("PARAMETERS");
105 // Print resource names
106 std::cout << "Probe image: " << image_probe << std::endl;
107
108 // Print solver names
109 std::cout << "Palm detection solver: " << solver_palm_detect << std::endl;
110 std::cout << "Palm attributes solver: "
111 << (solver_palm_attributes.has_value() ? solver_palm_attributes.value() : "not specified")
112 << std::endl;
113
114 // Print other options
115 std::cout << "Detection threshold: " << detection_threshold << std::endl;
116 }
117
118 utils::printToolkitInfo();
119
120 SFEError error{};
121
122 SFESolver detector_solver{};
123 DEFER(sfeSolverFree(detector_solver));
124 { // STAGE 1: loading solvers
125 utils::printFormatted("LOADING solvers");
126 // Initialize palm detection solver
127 error = sfeSolverCreate(
129 SOLVER_PARAMETERS.size(), &detector_solver);
130 utils::checkError(error);
131 }
132
133 SFEImage image{};
134 DEFER(sfeImageFree(image));
135 { // STAGE 2: Load image
136
137 utils::printFormatted("PALM DETECTION");
139 // Load image data from file
140 auto image_data = utils::readFile(image_probe);
141
142 // Decode image from data
143 error = sfeImageDecode(image_data.data(), image_data.size(), &image);
144 utils::checkError(error);
146 }
148 SFEDetection detected_palm = {};
149 { // STAGE 3: Detect palm in the image
150 size_t detection_count = 1;
151 // Detect palm in the image
152 error = sfeDetect(detector_solver, image, detection_threshold,
153 &detected_palm, &detection_count);
154 utils::checkError(error);
155
156 if (detection_count == 0) {
157 std::cout << "No palm detected in the probe image." << std::endl;
158 return 0;
159 } else {
160 std::cout << "Found " << detection_count
161 << " palm(s) in the probe image. Using the palm with highest "
162 "confidence."
163 << std::endl;
164 }
165 }
167
169 SFEPalmLandmarks landmarks = {};
170 { // STAGE 4: Extract palm landmarks
171 utils::printFormatted("LANDMARKS");
172 // Extract palm landmarks (hand position, orientation, keypoints)
173 error = sfePalmLandmarks(detector_solver, image, &detected_palm, &landmarks);
174 utils::checkError(error);
175 }
177
178 if (solver_palm_attributes.has_value()) { // STAGE 5: Attributes (if solver available)
179 SFESolver attributes_solver{};
180 DEFER(sfeSolverFree(attributes_solver));
181 // Initialize palm attributes solver
182 error = sfeSolverCreate(
183 solver_palm_attributes.value().c_str(), SOLVER_PARAMETERS.data(),
184 SOLVER_PARAMETERS.size(), &attributes_solver);
185 utils::checkError(error);
186
188 utils::printFormatted("ATTRIBUTES");
189 SFEPalmAttributes attributes = {};
190 error = sfePalmAttributes(attributes_solver, image, &landmarks,
191 &attributes);
192 utils::checkError(error);
194
195 printPalmDetectionInfo(detected_palm, landmarks);
196 std::cout << "Hand orientation " << attributes.hand_orientation << std::endl;
197 std::cout << "Hand position " << attributes.hand_position << std::endl;
198 std::cout << "Quality " << attributes.quality << std::endl;
199 } else {
200 printPalmDetectionInfo(detected_palm, landmarks);
201 std::cout << "Palm attributes solver not specified, skipping attributes analysis." << std::endl;
202 }
203
204 utils::printFormatted("FINISHED");
205}
void printHelp()
std::string image_probe
float detection_threshold
const auto SOLVER_PARAMETERS
std::optional< std::string > solver_palm_attributes
std::string solver_palm_detect
Solvers to use in example, the defaults are filled in by CMake.
void printPalmDetectionInfo(SFEDetection &detected_palm)
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 sfePalmAttributes(SFESolver solver, SFEImageView image, const SFEPalmLandmarks *landmarks, SFEPalmAttributes *out_attributes)
Calculate palm image quality attributes from given source image and palm detection.
Core detection - tagged union containing all detection types.
Raw owned raster image representation, HWC|BGR order.
Definition sfe_core.h:70
float quality
Range <0, 1>. The recommended threshold for further palm processing is 0.6.
Definition sfe_palm.h:58
float hand_orientation
Definition sfe_palm.h:56
float hand_position
Definition sfe_palm.h:52
Palm landmarks (keypoints, hand position, orientation, confidence)

◆ printHelp()

void printHelp ( )

Definition at line 56 of file example_palm_attributes.cpp.

56 {
57 std::cout << "Help: Usage of the program." << std::endl;
58 std::cout << "Options:" << std::endl;
59 std::cout << "-h: Display help." << std::endl;
60 std::cout << "-p: Probe image file." << std::endl;
61 std::cout << "-d: Path to detector solver." << std::endl;
62 std::cout << "-i: Path to attributes solver." << std::endl;
63 std::cout << "-t: Palm detection threshold. <0,1>" << std::endl;
64}

◆ printPalmDetectionInfo()

void printPalmDetectionInfo ( SFEDetection &  detected_palm,
SFEPalmLandmarks &  attributes 
)
inline

Definition at line 33 of file example_palm_attributes.cpp.

33 {
34 std::cout << "SFEDetection{ " << std::endl;
35 std::cout << " confidence: " << detected_palm.confidence << std::endl;
36 std::cout << " hand_position: " << attributes.hand_position << std::endl;
37 std::cout << " hand_orientation: " << attributes.hand_orientation << std::endl;
38 std::cout << " SFEBoundingBox{ " << std::endl;
39 std::cout << " x: " << detected_palm.bounding_box.x << std::endl;
40 std::cout << " y: " << detected_palm.bounding_box.y << std::endl;
41 std::cout << " width: " << detected_palm.bounding_box.width << std::endl;
42 std::cout << " height: " << detected_palm.bounding_box.height << std::endl;
43 std::cout << " }" << std::endl;
44 std::cout << " Keypoints{ " << std::endl;
45 for (auto &keypoint : attributes.keypoints) {
46 std::cout << " SFEPalmKeypoint{ " << std::endl;
47 std::cout << " x: " << keypoint.x << std::endl;
48 std::cout << " y: " << keypoint.y << std::endl;
49 std::cout << " }" << std::endl;
50 }
51 std::cout << " }" << std::endl;
52 std::cout << " }" << std::endl;
53 std::cout << std::endl;
54}
float y
Y coordinate of the top left corner of the bounding box relative to source image size - range <0,...
float width
Width of the bounding box relative to source image size - range <0,1>
float x
X coordinate of the top left corner of the bounding box relative to source image size - range <0,...
float height
Height of the bounding box relative to source image size - range <0,1>
SFEBoundingBox bounding_box
Bounding box (common for all detection types)
float confidence
Detection confidence - range <0,1> (common for all detection types)
SFEHandPosition hand_position
Detected hand position.
SFEHandOrientation hand_orientation
Detected palm orientation.

Variable Documentation

◆ detection_threshold

float detection_threshold = 0.1f

Definition at line 31 of file example_palm_attributes.cpp.

◆ image_probe

std::string image_probe = "assets/palm/palm_id1_r1.jpg"

Definition at line 18 of file example_palm_attributes.cpp.

◆ solver_palm_attributes

std::optional<std::string> solver_palm_attributes = std::nullopt
Examples
example_palm_attributes.cpp.

Definition at line 26 of file example_palm_attributes.cpp.

◆ solver_palm_detect

std::string solver_palm_detect = SOLVER_PALM_DETECT

Solvers to use in example, the defaults are filled in by CMake.

Definition at line 21 of file example_palm_attributes.cpp.

◆ SOLVER_PARAMETERS

const auto SOLVER_PARAMETERS = std::vector<SFESolverParameter>{}

Definition at line 29 of file example_palm_attributes.cpp.