facelandmarks.cpp
3.17 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "facelandmarks.h"
// FaceLandmarks::~FaceLandmarks(){
// pfld_interpreter->releaseModel();
// pfld_interpreter->releaseSession(session);
// }
bool FaceLandmarks::init_model(string model_path){
pfld_interpreter = unique_ptr<MNN::Interpreter>(MNN::Interpreter::createFromFile(model_path.c_str()));
if(nullptr==pfld_interpreter){
return false;
}
//创建session
MNN::ScheduleConfig schedule_config;
schedule_config.type = forward_type;
schedule_config.numThread = num_thread;
MNN::BackendConfig backend_config;
backend_config.memory = MNN::BackendConfig::Memory_Normal;
backend_config.power = MNN::BackendConfig::Power_Normal;
backend_config.precision = MNN::BackendConfig::Precision_Normal;
schedule_config.backendConfig = &backend_config;
session = pfld_interpreter->createSession(schedule_config);
input_tensor = pfld_interpreter->getSessionInput(session,NULL);
pfld_interpreter->resizeTensor(input_tensor,{1,3,112,112});
pfld_interpreter->resizeSession(session);
//数据预处理
MNN::CV::ImageProcess::Config image_config;
::memcpy(image_config.normal,normal,sizeof(normal));
image_config.sourceFormat = MNN::CV::BGR;
image_config.destFormat = MNN::CV::BGR;
pretreat = shared_ptr<MNN::CV::ImageProcess>(MNN::CV::ImageProcess::create(image_config));
// pretreat->setMatrix(transforms);
return true;
}
vector<vector<float>> FaceLandmarks::inference(string image_path){
Mat image = cv::imread(image_path);
vector<vector<float>> landmarks;
int width = image.cols;
int height = image.rows;
Mat resize_image;
cv::resize(image,resize_image,Size(112,112));
float ws = float(width)/float(112.0);
float hs = float(height)/float(112.0);
pretreat->convert(resize_image.data,112,112,0,input_tensor);
pfld_interpreter->runSession(session);
auto output_landmark = pfld_interpreter->getSessionOutput(session, NULL);
MNN::Tensor landmark_tensor(output_landmark, output_landmark->getDimensionType());
output_landmark->copyToHostTensor(&landmark_tensor);
float* result = landmark_tensor.host<float>();
for (int i = 0; i < 106; ++i) {
vector<float> curr_pt={result[2 * i + 0] * ws,result[2 * i + 1] * hs};
landmarks.push_back(curr_pt);
}
return landmarks;
}
vector<vector<float>> FaceLandmarks::inference(Mat image){
vector<vector<float>> landmarks;
int width = image.cols;
int height = image.rows;
Mat resize_image;
cv::resize(image,resize_image,Size(112,112));
float ws = float(width)/float(112.0);
float hs = float(height)/float(112.0);
pretreat->convert(resize_image.data,112,112,0,input_tensor);
pfld_interpreter->runSession(session);
auto output_landmark = pfld_interpreter->getSessionOutput(session, NULL);
MNN::Tensor landmark_tensor(output_landmark, output_landmark->getDimensionType());
output_landmark->copyToHostTensor(&landmark_tensor);
float* result = landmark_tensor.host<float>();
for (int i = 0; i < 106; ++i) {
vector<float> curr_pt={result[2 * i + 0] * ws,result[2 * i + 1] * hs};
landmarks.push_back(curr_pt);
}
return landmarks;
}