-
Notifications
You must be signed in to change notification settings - Fork 697
/
glint_arcface.cpp
42 lines (38 loc) · 1.51 KB
/
glint_arcface.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//
// Created by DefTruth on 2021/4/4.
//
#include "glint_arcface.h"
#include "lite/ort/core/ort_utils.h"
using ortcv::GlintArcFace;
Ort::Value GlintArcFace::transform(const cv::Mat &mat)
{
cv::Mat canvas;
cv::resize(mat, canvas, cv::Size(input_node_dims.at(3),
input_node_dims.at(2)));
cv::cvtColor(canvas, canvas, cv::COLOR_BGR2RGB);
// (1,3,112,112)
ortcv::utils::transform::normalize_inplace(canvas, mean_val, scale_val);
return ortcv::utils::transform::create_tensor(
canvas, input_node_dims, memory_info_handler,
input_values_handler, ortcv::utils::transform::CHW);
}
void GlintArcFace::detect(const cv::Mat &mat, types::FaceContent &face_content)
{
if (mat.empty()) return;
// 1. make input tensor
Ort::Value input_tensor = this->transform(mat);
// 2. inference
auto output_tensors = ort_session->Run(
Ort::RunOptions{nullptr}, input_node_names.data(),
&input_tensor, 1, output_node_names.data(), num_outputs
);
Ort::Value &embedding = output_tensors.at(0);
auto embedding_dims = output_node_dims.at(0); // (1,512)
const unsigned int hidden_dim = embedding_dims.at(1); // 512
const float *embedding_values = embedding.GetTensorMutableData<float>();
std::vector<float> embedding_norm(embedding_values, embedding_values + hidden_dim);
cv::normalize(embedding_norm, embedding_norm); // l2 normalize
face_content.embedding.assign(embedding_norm.begin(), embedding_norm.end());
face_content.dim = hidden_dim;
face_content.flag = true;
}