#include "sfe_toolkit/sfe_core.h"
#include "sfe_toolkit/sfe_palm.h"
#include <iomanip>
#include <regex>
#include <sstream>
#include <vector>
#include "annotate.hpp"
#include "solvers.h"
#include "utils.hpp"
#include <unordered_map>
std::string
image_probe =
"assets/palm/palm_id1_r1.jpg";
#ifdef SOLVER_PALM_LIVENESS
std::string solver_palm_liveness = SOLVER_PALM_LIVENESS;
#endif
std::cout << "SFEDetection{ " << std::endl;
std::cout <<
" confidence: " << detected_palm.
confidence << std::endl;
std::cout << " SFEBoundingBox{ " << std::endl;
std::cout <<
" x: " << detected_palm.
bounding_box.
x << std::endl;
std::cout <<
" y: " << detected_palm.
bounding_box.
y << std::endl;
std::cout << " }" << std::endl;
std::cout << " }" << std::endl;
std::cout << std::endl;
}
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 << "-i: Path to liveness solver." << std::endl;
std::cout << "-t: Palm detection threshold. <0,1>" << std::endl;
}
int main(
int argc,
char *argv[]) {
{
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"))
if (args.count("-d"))
if (args.count("-i"))
solver_palm_liveness = args["-i"];
if (args.count("-t"))
utils::printFormatted("PARAMETERS");
std::cout <<
"Probe image: " <<
image_probe << std::endl;
std::cout << "Palm liveness solver: " << solver_palm_liveness << std::endl;
}
utils::printToolkitInfo();
{
utils::printFormatted("LOADING solvers");
utils::checkError(error);
utils::checkError(error);
}
{
utils::printFormatted("PALM DETECTION");
utils::checkError(error);
}
{
size_t detection_count = 1;
&detected_palm, &detection_count);
utils::checkError(error);
if (detection_count == 0) {
std::cout << "No palm detected in the probe image." << std::endl;
return 0;
} else {
std::cout << "Found " << detection_count
<< " palm(s) in the probe image. Using the palm with highest "
"confidence."
<< std::endl;
}
}
float liveness_score = 0.0f;
{
utils::printFormatted("LIVENESS CHECK");
&liveness_score);
utils::checkError(error);
}
{
std::cout << "Liveness score is " << liveness_score;
static const float LIVENESS_THRESHOLD = 0.8189f;
if (liveness_score < LIVENESS_THRESHOLD) {
std::cout << " which is below " << LIVENESS_THRESHOLD
<< " threshold. Palm is spoof." << std::endl;
} else {
std::cout << " which is above " << LIVENESS_THRESHOLD
<< " threshold. Palm is genuine." << std::endl;
}
}
{
std::stringstream label;
label << "Palm" << std::fixed << std::setprecision(2)
<<
" d:" << detected_palm.
confidence <<
" l:" << liveness_score;
annotate::labelBox(image, label.str(), detected_palm.
bounding_box,
annotate::WHITE, annotate::GREEN);
size_t size = image.width * image.height * 3;
auto png_file = std::vector<unsigned char>(size);
utils::checkError(error);
png_file.resize(size);
utils::saveFile("palm_liveness.png", png_file);
std::cout << std::endl;
std::cout << "Annotated image saved to palm_liveness.png" << std::endl;
}
utils::printFormatted("FINISHED");
}
float detection_threshold
const auto SOLVER_PARAMETERS
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.
void * SFEError
Error type used to hold optional error message.
void sfeImageFree(SFEImage image)
Free memory associated with SFEImage.
@ SFE_IMAGE_FORMAT_PNG
Portable Network Graphics format.
SFEError sfeImageEncode(SFEImageView image, SFEImageFormat image_format, unsigned char *out_data, size_t *in_out_data_len)
Encode SFEImage into a buffer with specified image_format.
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 sfePalmLivenessPassive(SFESolver solver, SFEImageView image, const SFEDetection *detection, float *out_liveness_score)
Passive liveness score calculation.
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>
Core detection - tagged union containing all detection types.
SFEBoundingBox bounding_box
Bounding box (common for all detection types)
float confidence
Detection confidence - range <0,1> (common for all detection types)
Raw owned raster image representation, HWC|BGR order.