表情识别模块
- 1.环境部署
- 1.1同样采用fastDeploy库
- 1.2相关模型
- 2.封装成静态库
- 2.1参考[百度Paddle中PP-Mattingv2的部署并将之封装并调用一个C++静态库](https://blog.csdn.net/weixin_43564060/article/details/128882099)
- 2.2项目依赖添加
- 2.3生成成功
- 3.test
- 3.1创建emotion_test项目
- 3.2进行项目配置
- 3.3解决dll文件缺失的问题
- 3.4运行结果
1.环境部署
1.1同样采用fastDeploy库
可以参考百度Paddle中PP-Mattingv2的部署并将之封装并调用一个C++静态库,部署过程大致一样,只是核心的代码进行了改动。
1.2相关模型
模型使用的自训练resnet50模型,其中输出的标签为:
- 0.angry
- 1.disgust
- 2.fear
- 3.happy
- 4.neutral
- 5.sad
- 6.surprise
模型需要三个文件:model.pdmodel,model.pdiparams,model.yml
2.封装成静态库
2.1参考百度Paddle中PP-Mattingv2的部署并将之封装并调用一个C++静态库
framework.h代码:
#pragma once
#define WIN32_LEAN_AND_MEAN // 从 Windows 头文件中排除极少使用的内容
#include "fastdeploy/vision.h"
std::string emotion_CpuInfer(const std::string& model_dir, const cv::Mat& image_file);
std::string emotion_GpuInfer(const std::string& model_dir, const cv::Mat& image_file);
int emotion_infer_by_camera(const std::string& device, const std::string& model_dir, const std::string& window_name);
emotion_StaticLib.cpp代码为:
// emotion_StaticLib.cpp : 定义静态库的函数。
//
#include "pch.h"
#include "framework.h"
#ifdef WIN32
const char sep = '\\';
#else
const char sep = '/';
#endif
std::string emotion_CpuInfer(const std::string& model_dir, const cv::Mat& image_file) {
auto model_file = model_dir + sep + "model.pdmodel";
auto params_file = model_dir + sep + "model.pdiparams";
auto config_file = model_dir + sep + "inference.yml";
auto option = fastdeploy::RuntimeOption();
option.UseCpu();
auto model = fastdeploy::vision::classification::PaddleClasModel(
model_file, params_file, config_file, option);
std::string result;
if (!model.Initialized()) {
std::cerr << "Failed to initialize." << std::endl;
result = "Failed to initialize.";
return result;
}
auto im = image_file;
fastdeploy::vision::ClassifyResult res;
if (!model.Predict(im, &res)) {
std::cerr << "Failed to predict." << std::endl;
result = "Failed to initialize.";
return result;
}
// print res
std::cout << res.Str() << std::endl;
return res.Str();
}
std::string emotion_GpuInfer(const std::string& model_dir, const cv::Mat& image_file) {
auto model_file = model_dir + sep + "model.pdmodel";
auto params_file = model_dir + sep + "model.pdiparams";
auto config_file = model_dir + sep + "inference.yml";
auto option = fastdeploy::RuntimeOption();
option.UseGpu();
auto model = fastdeploy::vision::classification::PaddleClasModel(
model_file, params_file, config_file, option);
std::string result;
if (!model.Initialized()) {
std::cerr << "Failed to initialize." << std::endl;
result = "Failed to initialize.";
return result;
}
auto im = image_file;
fastdeploy::vision::ClassifyResult res;
if (!model.Predict(im, &res)) {
std::cerr << "Failed to predict." << std::endl;
result = "Failed to initialize.";
return result;
}
// print res
std::cout << res.Str() << std::endl;
return res.Str();
}
int emotion_infer_by_camera(const std::string& device, const std::string& model_dir, const std::string& window_name = "video") {
cv::VideoCapture cap;
cap.open(0);
std::string result;
if (!cap.isOpened()) {
std::cout << "open camera failed!" << std::endl;
return 0;
}
cv::namedWindow(window_name, 1);
while (1) {
time_t t_now = time(0);
cv::Mat frame;
cap >> frame;
if (frame.empty()) {
return 0;
}
cv::imshow(window_name, frame);
emotion_CpuInfer(model_dir, frame);
if (device == "gpu") {
cv::imshow(window_name, frame);
result = emotion_GpuInfer(model_dir, frame);
}
else {
cv::imshow(window_name, frame);
result = emotion_CpuInfer(model_dir, frame);
}
std::cout << "emotion此帧共消耗" << (time(0) - t_now) << "秒" << std::endl;
if (cv::waitKey(30) >= 0) break;
}
cap.release();
return 1;
}
所有的环境部署步骤与百度Paddle中PP-Mattingv2的部署并将之封装并调用一个C++静态库一致,在该部署过程中,只进行了cpu环境的部署
2.2项目依赖添加
注意:所有的环境必须是Release X64
2.3生成成功
到此为止封装已经超过了,在项目里面即可部署使用。
3.test
3.1创建emotion_test项目
emotion_test.cpp
#include <vector>
#include <iostream>
#include <string>
#include "C:/Users/44869/Desktop/emotion_StaticLib/emotion_StaticLib/pch.h"
int main() {
emotion_infer_by_camera("cpu", "A:/emotion/resnet50", "emotion");
return 0;
}
3.2进行项目配置
3.3解决dll文件缺失的问题
运行C:\Users\44869\Desktop\emotion_StaticLib\fastdeploy-win-x64-1.0.3下的fastdeploy_init.bat
生成的所有dll文件复制到C:\Users\44869\Desktop\emotion_StaticLib\emotion_test\x64\Release下即可