基于Deepface的情緒識別c++
文章目錄
- 基于Deepface的情緒識別c++
- 簡介
- 下載模型并轉為onnx(facial_expression_model_weights.h5)
- 測試
- 取出照片的人臉部分并處理成模型輸入格式
- 用模型推理一下看看結果
- 用onnxruntime的c++庫推理
簡介
DeepFace是一個基于深度學習的開源人臉識別與屬性分析框架,其情緒識別模塊通過卷積神經網絡(CNN)架構實現了對7種基礎情緒(生氣、厭惡、恐懼、開心、悲傷、驚訝、中性)的高精度分類。該技術結合了OpenCV的圖像處理能力,支持從人臉檢測、對齊到情緒預測的全流程自動化處理,準確率高達97.53%,超越人類平均水平。其核心模型如VGG-Face、ArcFace等通過大規模數據集(如FER2013、AffectNet)訓練,能夠捕捉面部微表情的細微差異。(以上內容來著deepseek)
下載模型并轉為onnx(facial_expression_model_weights.h5)
這是情緒識別部分的源碼
# stdlib dependencies
from typing import List, Union# 3rd party dependencies
import numpy as np
import cv2# project dependencies
from deepface.commons import package_utils, weight_utils
from deepface.models.Demography import Demography
from deepface.commons.logger import Logger# dependency configuration
tf_version = package_utils.get_tf_major_version()if tf_version == 1:from keras.models import Sequentialfrom keras.layers import Conv2D, MaxPooling2D, AveragePooling2D, Flatten, Dense, Dropout
else:from tensorflow.keras.models import Sequentialfrom tensorflow.keras.layers import (Conv2D,MaxPooling2D,AveragePooling2D,Flatten,Dense,Dropout,)# Labels for the emotions that can be detected by the model.
labels = ["angry", "disgust", "fear", "happy", "sad", "surprise", "neutral"]logger = Logger()# pylint: disable=line-too-long, disable=too-few-public-methodsWEIGHTS_URL = "https://github.com/serengil/deepface_models/releases/download/v1.0/facial_expression_model_weights.h5"class EmotionClient(Demography):"""Emotion model class"""def __init__(self):self.model = load_model()self.model_name = "Emotion"def _preprocess_image(self, img: np.ndarray) -> np.ndarray:"""Preprocess single image for emotion detectionArgs:img: Input image (224, 224, 3)Returns:Preprocessed grayscale image (48, 48)"""img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)img_gray = cv2.resize(img_gray, (48, 48))return img_graydef predict(self, img: Union[np.ndarray, List[np.ndarray]]) -> np.ndarray:"""Predict emotion probabilities for single or multiple facesArgs:img: Single image as np.ndarray (224, 224, 3) orList of images as List[np.ndarray] orBatch of images as np.ndarray (n, 224, 224, 3)Returns:np.ndarray (n, n_emotions)where n_emotions is the number of emotion categories"""# Preprocessing input image or image list.imgs = self._preprocess_batch_or_single_input(img)processed_imgs = np.expand_dims(np.array([self._preprocess_image(img) for img in imgs]), axis=-1)# Predictionpredictions = self._predict_internal(processed_imgs)return predictionsdef load_model(url=WEIGHTS_URL,
) -> Sequential:"""Consruct emotion model, download and load weights"""num_classes = 7model = Sequential()# 1st convolution layermodel.add(Conv2D(64, (5, 5), activation="relu", input_shape=(48, 48, 1)))model.add(MaxPooling2D(pool_size=(5, 5), strides=(2, 2)))# 2nd convolution layermodel.add(Conv2D(64, (3, 3), activation="relu"))model.add(Conv2D(64, (3, 3), activation="relu"))model.add(AveragePooling2D(pool_size=(3, 3), strides=(2, 2)))# 3rd convolution layermodel.add(Conv2D(128, (3, 3), activation="relu"))model.add(Conv2D(128, (3, 3), activation="relu"))model.add(AveragePooling2D(pool_size=(3, 3), strides=(2, 2)))model.add(Flatten())# fully connected neural networksmodel.add(Dense(1024, activation="relu"))model.add(Dropout(0.2))model.add(Dense(1024, activation="relu"))model.add(Dropout(0.2))model.add(Dense(num_classes, activation="softmax"))# ----------------------------weight_file = weight_utils.download_weights_if_necessary(file_name="facial_expression_model_weights.h5", source_url=url)model = weight_utils.load_model_weights(model=model, weight_file=weight_file)return model
下載模型并轉為onnx
import tensorflow as tf
import tf2onnx
import onnx# 1. 加載原始模型
model = load_model()# 2. 定義輸入簽名
input_signature = [tf.TensorSpec(shape=(None, 48, 48, 1), dtype=tf.float32, name='input')]# 3. 轉換為ONNX
onnx_model, _ = tf2onnx.convert.from_keras(model,input_signature=input_signature,opset=13,output_path="deepface_emotion.onnx"
)
測試
取出照片的人臉部分并處理成模型輸入格式
測試圖片:
取出人臉圖片:
import cv2
import matplotlib.pyplot as plt#處理成輸入格式
img = cv2.imread("../img/test2_face.jpg")
img = img.astype(np.float32)/255.0
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_gray = cv2.resize(img_gray, (48, 48))
gray_img = np.expand_dims(img_gray, axis=-1)
test_input = np.expand_dims(gray_img, axis=0)
用模型推理一下看看結果
import onnxruntime as ort
import numpy as np# 加載ONNX模型
ort_session = ort.InferenceSession("deepface_emotion.onnx", providers=['CPUExecutionProvider'])# 運行推理
outputs = ort_session.run(None, {'input': test_input})
print("預測概率:", outputs[0])# 獲取預測結果
predicted_class = np.argmax(outputs[0])
print("預測類別:", predicted_class)labels = ["angry", "disgust", "fear", "happy", "sad", "surprise", "neutral"]
print(f"預測類型:{labels[predicted_class]},概率:{outputs[0][0][predicted_class]:.2f}")
結果:
預測概率: [[2.0709881e-10 1.9225350e-18 4.2878087e-06 9.9998260e-01 1.3058154e-051.4756028e-11 1.7358280e-08]]
預測類別: 3
預測類型:happy,概率:1.00
用onnxruntime的c++庫推理
#pragma once
#include <iostream>
#include <numeric> //數值計算
#include <tuple> //C++17 元組
#include <opencv2/opencv.hpp>
#include <onnxruntime_cxx_api.h>namespace LIANGBAIKAI_BASE_MODEL_NAME
{#define ORT_OLD_VISON 12 // ort1.12.0 之前的版本為舊版本APIclass Deepface_Emotion_Onnxruntime{public:enum Severity_log{E_INTERNAL_ERROR = 0, // 內部錯誤E_ERROR = 1, // 一般錯誤E_WARNING = 2, // 警告E_INFO = 3, // 信息};Deepface_Emotion_Onnxruntime() : _OrtMemoryInfo(Ort::MemoryInfo::CreateCpu(OrtAllocatorType::OrtDeviceAllocator, OrtMemType::OrtMemTypeCPUOutput)) {};virtual ~Deepface_Emotion_Onnxruntime() {};/*** @description: 設置日志等級* @param {Severity_log} severity* @return {*}*/virtual void setReportableSeverity(Severity_log severity){_reportableSeverity = severity;log(E_INFO, "reportableSeverity set to " + std::to_string(severity));}/*** @description:* @param {string} &modelPath 模型文件* @param {int} netWidth 模型輸入尺寸寬* @param {int} netHeight 模型輸入尺寸高* @param {bool} isCuda 是否使用cuda* @param {int} cudaID cuda的id* @param {bool} warmUp warm up gpu-model* @return {*} 返回是否初始化成功*/bool init(const std::string &modelPath, int netWidth = 48, int netHeight = 48, bool isCuda = true, int cudaID = 0, bool warmUp = true){_netWidth = netWidth;_netHeight = netHeight;bool rec = ReadModel(modelPath, isCuda, cudaID, warmUp);return rec;}/*** @description: 推理* @param {cv::Mat} &img : 輸入人臉圖片* @return {std::tuple<int, float>} 返回情緒,置信度*/std::tuple<int, float> infer(const cv::Mat &img){log(E_INFO, "infer");cv::Mat img_tmp = img.clone();cv::resize(img_tmp, img_tmp, cv::Size(224, 224));cv::Mat normalizedImage;// cv::normalize(img_tmp, normalizedImage, 0, 1, cv::NORM_MINMAX, CV_32F); // 歸一化 結果與python的源碼結果有一點偏差,用下面的方法img_tmp.convertTo(normalizedImage, CV_32F, 1.0 / 255.0);cv::Mat resizedImage;cv::resize(normalizedImage, resizedImage, cv::Size(_netWidth, _netHeight));cv::Mat grayImage;cv::cvtColor(resizedImage, grayImage, cv::COLOR_BGR2GRAY);cv::Mat blob;cv::dnn::blobFromImage(grayImage, blob, 1.0, cv::Size(0, 0), cv::Scalar(0), false, false);int64_t input_tensor_length = VectorProduct(_inputTensorShape);std::vector<Ort::Value> input_tensors;std::vector<Ort::Value> output_tensors;input_tensors.push_back(Ort::Value::CreateTensor<float>(_OrtMemoryInfo, (float *)blob.data, input_tensor_length, _inputTensorShape.data(), _inputTensorShape.size()));log(E_INFO, "infer run");output_tensors = _OrtSession->Run(Ort::RunOptions{nullptr},_inputNodeNames.data(),input_tensors.data(),_inputNodeNames.size(),_outputNodeNames.data(),_outputNodeNames.size());float *all_data = output_tensors[0].GetTensorMutableData<float>();int max_index = 0;float max_value = 0.0f;for (int i = 0; i < _outputTensorShape[1]; i++){log(E_INFO, "result" + std::to_string(i) + ": " + std::to_string(all_data[i]));if (all_data[i] > max_value){max_value = all_data[i];max_index = i;}}return std::make_tuple(max_index, max_value * 99.99);}private:/*** @description: 讀取onnx模型* @param {string} &modelPath : onnx模型路徑* @param {bool} isCuda: 如果為true,使用Ort-GPU,否則在cpu上運行。* @param {int} cudaID: 如果isCuda==true,在cudaID上運行Ort-GPU。* @param {bool} warmUp: 如果isCuda==true,預熱GPU-model。* @return {*}*/bool ReadModel(const std::string &modelPath, bool isCuda = true, int cudaID = 0, bool warmUp = true){if (_batchSize < 1){_batchSize = 1;}try{// 列出可用的推理提供者 cpu cuda dml tensorrt openvinostd::vector<std::string> available_providers = Ort::GetAvailableProviders();auto cuda_available = std::find(available_providers.begin(), available_providers.end(), "CUDAExecutionProvider");if (isCuda && (cuda_available == available_providers.end())){log(E_ERROR, "Your ORT build without GPU. Change to CPU.");log(E_INFO, "************* Infer model on CPU! *************");}else if (isCuda && (cuda_available != available_providers.end())){log(E_INFO, "************* Infer model on GPU! *************");#if ORT_API_VERSION < ORT_OLD_VISONOrtCUDAProviderOptions cudaOption;cudaOption.device_id = cudaID;_OrtSessionOptions.AppendExecutionProvider_CUDA(cudaOption);
#else// 添加CUDA執行提供者OrtStatus *status = OrtSessionOptionsAppendExecutionProvider_CUDA(_OrtSessionOptions, cudaID);if (status != NULL){log(E_ERROR, "OrtSessionOptionsAppendExecutionProvider_CUDA ERROR");}
#endif}else{log(E_INFO, "************* Infer model on CPU! *************");}// 設置圖優化級別_OrtSessionOptions.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_EXTENDED);#ifdef _WIN32std::wstring model_path(modelPath.begin(), modelPath.end());_OrtSession = std::make_shared<Ort::Session>(_OrtEnv, modelPath.c_str(), _OrtSessionOptions);
#else// 創建會話_OrtSession = std::make_shared<Ort::Session>(_OrtEnv, modelPath.c_str(), _OrtSessionOptions);
#endif// 創建默認的內存分配器Ort::AllocatorWithDefaultOptions allocator;// 初始化輸入_inputNodesNum = _OrtSession->GetInputCount();
#if ORT_API_VERSION < ORT_OLD_VISON_inputName = _OrtSession->GetInputName(0, allocator);_inputNodeNames.push_back(_inputName);
#else// 獲取輸入名稱_inputName = std::move(_OrtSession->GetInputNameAllocated(0, allocator));_inputNodeNames.push_back(_inputName.get());
#endif// 獲取輸入類型信息Ort::TypeInfo inputTypeInfo = _OrtSession->GetInputTypeInfo(0);// 獲取輸入張量類型和形狀信息auto input_tensor_info = inputTypeInfo.GetTensorTypeAndShapeInfo();// 獲取輸入數據類型_inputNodeDataType = input_tensor_info.GetElementType();// 獲取輸入張量形狀_inputTensorShape = input_tensor_info.GetShape();if (_inputTensorShape[0] == -1){_isDynamicShape = true;_inputTensorShape[0] = _batchSize;}if (_inputTensorShape[2] == -1 || _inputTensorShape[3] == -1){_isDynamicShape = true;_inputTensorShape[2] = _netHeight;_inputTensorShape[3] = _netWidth;}// 獲取輸出節點數_outputNodesNum = _OrtSession->GetOutputCount();// 獲取輸出名稱
#if ORT_API_VERSION < ORT_OLD_VISON_output_name0 = _OrtSession->GetOutputName(0, allocator);_outputNodeNames.push_back(_output_name0);
#else_output_name0 = std::move(_OrtSession->GetOutputNameAllocated(0, allocator));_outputNodeNames.push_back(_output_name0.get());
#endifOrt::TypeInfo type_info_output0(nullptr);type_info_output0 = _OrtSession->GetOutputTypeInfo(0); // output0// 獲取輸出張量類型和形狀信息auto tensor_info_output0 = type_info_output0.GetTensorTypeAndShapeInfo();// 獲取輸出數據類型_outputNodeDataType = tensor_info_output0.GetElementType();// 獲取輸出張量形狀_outputTensorShape = tensor_info_output0.GetShape();log(E_INFO, "inputNodesNum:" + std::to_string(_inputNodesNum));log(E_INFO, "inputNodeNames:" + std::string(_inputNodeNames[0]));std::string inputTensorShapeStr = " ";for (unsigned int i = 0; i < _inputTensorShape.size(); i++){inputTensorShapeStr += std::to_string(_inputTensorShape[i]) + " ";}log(E_INFO, "inputTensorShape:" + inputTensorShapeStr);log(E_INFO, "outputNodesNum:" + std::to_string(_outputNodesNum));log(E_INFO, "outputNodeNames:" + std::string(_outputNodeNames[0]));std::string outputTensorShapeStr = " ";for (unsigned int i = 0; i < _outputTensorShape.size(); i++){outputTensorShapeStr += std::to_string(_outputTensorShape[i]) + " ";}log(E_INFO, "outputTensorShape:" + outputTensorShapeStr);log(E_INFO, "outputNodeDataType:" + std::to_string(_outputNodeDataType));// warm upif (isCuda && warmUp){// draw runlog(E_INFO, "Start warming up");// 計算輸入張量長度size_t input_tensor_length = VectorProduct(_inputTensorShape);float *temp = new float[input_tensor_length];// 創建輸入張量std::vector<Ort::Value> input_tensors;// 創建輸出張量std::vector<Ort::Value> output_tensors;input_tensors.push_back(Ort::Value::CreateTensor<float>(_OrtMemoryInfo, temp, input_tensor_length, _inputTensorShape.data(),_inputTensorShape.size()));for (int i = 0; i < 3; ++i){output_tensors = _OrtSession->Run(Ort::RunOptions{nullptr},_inputNodeNames.data(),input_tensors.data(),_inputNodeNames.size(),_outputNodeNames.data(),_outputNodeNames.size());}delete[] temp;}}catch (const std::exception &){log(E_ERROR, "read model error !");return false;}log(E_INFO, "read model success !");return true;}void log(Severity_log severity, const std::string msg) noexcept{// 根據嚴重性級別決定是否打印日志if (severity <= _reportableSeverity){switch (severity){case Severity_log::E_INTERNAL_ERROR:std::cerr << "[INTERNAL ERROR] " << msg << std::endl;break;case Severity_log::E_ERROR:std::cerr << "[ERROR] " << msg << std::endl;break;case Severity_log::E_WARNING:std::cerr << "[WARNING] " << msg << std::endl;break;case Severity_log::E_INFO:std::cout << "[INFO] " << msg << std::endl;break;default:break;}}}// 計算向量中所有元素的乘積template <typename T>T VectorProduct(const std::vector<T> &v){return std::accumulate(v.begin(), v.end(), 1, std::multiplies<T>());};int _netWidth = 48; // ONNX-net-input-widthint _netHeight = 48; // ONNX-net-input-heightint _batchSize = 1; // if multi-batch,set thisbool _isDynamicShape = false; // onnx support dynamic shape// ONNXRUNTIMEOrt::Env _OrtEnv = Ort::Env(OrtLoggingLevel::ORT_LOGGING_LEVEL_ERROR, "Resnet");Ort::SessionOptions _OrtSessionOptions = Ort::SessionOptions();std::shared_ptr<Ort::Session> _OrtSession;Ort::MemoryInfo _OrtMemoryInfo;
#if ORT_API_VERSION < ORT_OLD_VISONchar *_inputName, *_output_name0;
#elsestd::shared_ptr<char> _inputName, _output_name0;
#endifstd::vector<char *> _inputNodeNames; // 輸入節點名std::vector<char *> _outputNodeNames; // 輸出節點名size_t _inputNodesNum = 0; // 輸入節點數size_t _outputNodesNum = 0; // 輸出節點數ONNXTensorElementDataType _inputNodeDataType; // 數據類型ONNXTensorElementDataType _outputNodeDataType;std::vector<int64_t> _inputTensorShape; // 輸入張量shapestd::vector<int64_t> _outputTensorShape;Severity_log _reportableSeverity = Severity_log::E_ERROR;};
}
完整項目代碼