Files
FastDeploy/fastdeploy/vision/faceid/contrib/insightface/preprocessor.cc
Zheng_Bicheng ec67f8ee6d [Model] Refactor insightface models (#919)
* 重构insightface代码

* 重写insightface example代码

* 重写insightface example代码

* 删除多余代码

* 修改预处理代码

* 修改文档

* 修改文档

* 恢复误删除的文件

* 修改cpp example

* 修改cpp example

* 测试python代码

* 测试python代码

* 测试python代码

* 测试python代码

* 测试python代码

* 测试python代码

* 测试python代码

* 跑通python代码

* 修复重复初始化的bug

* 更新adaface的python代码

* 修复c++重复初始化的问题

* 修复c++重复初始化的问题

* 修复Python重复初始化的问题

* 新增preprocess的几个参数的获取方式

* 修复注释的错误

* 按照要求修改

* 修改文档中的图片为图片压缩包

* 修改编译完成后程序的提示

* 更新错误include

* 删除无用文件

* 更新文档
2022-12-26 21:01:58 +08:00

77 lines
2.4 KiB
C++
Executable File

// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastdeploy/vision/faceid/contrib/insightface/preprocessor.h"
namespace fastdeploy {
namespace vision {
namespace faceid {
InsightFaceRecognitionPreprocessor::InsightFaceRecognitionPreprocessor() {
// parameters for preprocess
size_ = {112, 112};
alpha_ = {1.f / 127.5f, 1.f / 127.5f, 1.f / 127.5f};
beta_ = {-1.f, -1.f, -1.f}; // RGB
permute_ = true;
}
bool InsightFaceRecognitionPreprocessor::Preprocess(FDMat * mat, FDTensor* output) {
// face recognition model's preprocess steps in insightface
// reference: insightface/recognition/arcface_torch/inference.py
// 1. Resize
// 2. BGR2RGB
// 3. Convert(opencv style) or Normalize
// 4. HWC2CHW
int resize_w = size_[0];
int resize_h = size_[1];
if (resize_h != mat->Height() || resize_w != mat->Width()) {
Resize::Run(mat, resize_w, resize_h);
}
if (permute_) {
BGR2RGB::Run(mat);
}
Convert::Run(mat, alpha_, beta_);
HWC2CHW::Run(mat);
Cast::Run(mat, "float");
mat->ShareWithTensor(output);
output->ExpandDim(0); // reshape to n, h, w, c
return true;
}
bool InsightFaceRecognitionPreprocessor::Run(std::vector<FDMat>* images,
std::vector<FDTensor>* outputs) {
if (images->empty()) {
FDERROR << "The size of input images should be greater than 0." << std::endl;
return false;
}
FDASSERT(images->size() == 1, "Only support batch = 1 now.");
outputs->resize(1);
// Concat all the preprocessed data to a batch tensor
std::vector<FDTensor> tensors(images->size());
for (size_t i = 0; i < images->size(); ++i) {
if (!Preprocess(&(*images)[i], &tensors[i])) {
FDERROR << "Failed to preprocess input image." << std::endl;
return false;
}
}
(*outputs)[0] = std::move(tensors[0]);
return true;
}
} // namespace faceid
} // namespace vision
} // namespace fastdeploy