-
Notifications
You must be signed in to change notification settings - Fork 705
/
ncnn_glint_arcface.cpp
42 lines (37 loc) · 1.26 KB
/
ncnn_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/11/13.
//
#include "ncnn_glint_arcface.h"
using ncnncv::NCNNGlintArcFace;
void NCNNGlintArcFace::transform(const cv::Mat &mat, ncnn::Mat &in)
{
// BGR NHWC -> RGB NCHW
int h = mat.rows;
int w = mat.cols;
in = ncnn::Mat::from_pixels_resize(
mat.data, ncnn::Mat::PIXEL_BGR2RGB,
w, h, input_width, input_height
);
in.substract_mean_normalize(mean_vals, norm_vals);
}
void NCNNGlintArcFace::detect(const cv::Mat &mat, types::FaceContent &face_content)
{
if (mat.empty()) return;
// 1. make input tensor
ncnn::Mat input;
this->transform(mat, input);
// 2. inference & extract
auto extractor = net->create_extractor();
extractor.set_light_mode(false); // default
extractor.set_num_threads(num_threads);
extractor.input("input", input);
ncnn::Mat embedding;
extractor.extract("embedding", embedding);
const unsigned int hidden_dim = embedding.w; // 512
const float *embedding_values = (float *) embedding.data;
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;
}