Skip to main content
Version: 3.25.1 (latest)

DynamicTemplateIndex

DynamicTemplateIndex is used to create a biometric template database.

Supported operations:

  • Adding and deleting templates by UUID
  • 1:N database search

The MATCHER_MODULE is used for 1:N searches.

Example of using DynamicTemplateIndex for database search:

Configuration Context Specification

  • async - Creates an asynchronous version of DynamicTemplateIndex
  • max_license_count - Maximum number of licenses
  • capacity - Reserved number of templates
  • model_version - Method name and version in the format name_version
{
"async": "<bool>",
"max_license_count": "<long>",
"capacity": "<long>",
"model_version": "<string>"
}
note

The async mode in DynamicTemplateIndex is currently in beta.

Using DynamicTemplateIndex

  1. Create FacerecService
  2. Create DynamicTemplateIndex (using loaded/generated templates or specifying the initial number of templates and the modification_version of FACE_TEMPLATE_EXTRACTOR)
  3. Add templates (load or generate using FACE_TEMPLATE_EXTRACTOR)
  4. Perform a search in DynamicTemplateIndex using MATCHER_MODULE
// 1
pbio::FacerecService::Ptr service = pbio::FacerecService::createService("<dll_path>", "<conf_path>", "<license_path>");

// 2
pbio::Context indexConfig = service->createContext();

indexConfig["capacity"] = 1000;
indexConfig["model_version"] = "1000_1";
indexConfig["async"] = false;
indexConfig["max_license_count"] = 1000;

pbio::DynamicTemplateIndex::Ptr index = service->createDynamicTemplateIndex(indexConfig);

// 3
std::vector<std::string> imagePaths =
{
"<path_to_image>"
};
std::vector<pbio::ProcessingBlock> pipeline;
pbio::Context detectorConfig = service->createContext();
pbio::Context fitterConfig = service->createContext();
pbio::Context faceTemplateExtractorConfig = service->createContext();
pbio::Context matcherModuleConfig = service->createContext();

pipeline.reserve(3);

detectorConfig["unit_type"] = "FACE_DETECTOR";
fitterConfig["unit_type"] = "FACE_FITTER";

faceTemplateExtractorConfig["unit_type"] = "FACE_TEMPLATE_EXTRACTOR";
faceTemplateExtractorConfig["modification"] = "1000";

matcherModuleConfig["unit_type"] = "MATCHER_MODULE";
matcherModuleConfig["modification"] = "1000";

pipeline.push_back(service->createProcessingBlock(detectorConfig));
pipeline.push_back(service->createProcessingBlock(fitterConfig));
pipeline.push_back(service->createProcessingBlock(faceTemplateExtractorConfig));

pbio::ProcessingBlock matcherModule = service->createProcessingBlock(matcherModuleConfig);
std::unordered_map<std::string, pbio::ContextTemplate::Ptr> uuidToTemplate;

for (const std::string& imagePath : imagePaths)
{
std::string imageData;

{
std::ifstream image(imagePath, std::ios::binary);
std::ostringstream stream;

if (!image.is_open())
{
throw std::runtime_error("Can't open file: " + imagePath);
}

stream << image.rdbuf();

imageData = stream.str();
}

pbio::Context data = service->createContextFromEncodedImage(imageData);

for (pbio::ProcessingBlock& block : pipeline)
{
block(data);
}

for (const pbio::Context& object : data["objects"])
{
pbio::ContextTemplate::Ptr contextTemplate = object["face_template"]["template"].getContextTemplate();
std::string uuid = object["face_template"]["uuid"].getString();

index->add(contextTemplate, uuid);

uuidToTemplate.emplace(std::move(uuid), contextTemplate);
}
}

// 4
pbio::Context matcherData = service->createContext();

matcherData["template_index"] = index;

for (const auto& value : uuidToTemplate)
{
matcherData["queries"] = value.second;

matcherModule(matcherData);

std::cout << (matcherData["results"][0]["uuid"].getString() == value.first) << std::endl;
}

DynamicTemplateIndex Methods

// Retrieving the UUID of a template by index in the database
std::string getUUID(size_t index) const;

// Retrieving a template by UUID
pbio::ContextTemplate::Ptr at(const std::string& uuid) const;

// Retrieving a template by index in the database
pbio::ContextTemplate::Ptr at(int64_t index) const;

// Adding a template to the database
void add(pbio::Template::Ptr templ, const std::string& uuid);

void add(const std::vector<pbio::Template::Ptr>& templates, const std::vector<std::string>& uuids);

void add(pbio::ContextTemplate::Ptr templ, const std::string& uuid);

void add(const std::vector<pbio::ContextTemplate::Ptr>& templates, const std::vector<std::string>& uuids);

// Deleting a template from the database by UUID
void remove(const std::string& uuid);

void remove(const std::vector<std::string>& uuids);

// Number of templates in the database
size_t size() const;

// Current database capacity
size_t capacity() const;

// Merging template databases
// otherIndex becomes Invalid
void concat(DynamicTemplateIndex::Ptr otherIndex);

// Retrieving the method name of templates in the database
std::string getMethodName() const;