最近在研究人脸相关算法的C++实现时,发现了lite.ai.toolkit这个库;官方描述为:A lite C++ toolkit of awesome AI models, such as Object Detection, Face Detection, Face Recognition, Segmentation, Matting, etc. See Model Zoo and ONNX Hub, MNN Hub, TNN Hub, NCNN Hub.
简单试用发现很不错,其提供了最小依赖的库以及丰富的示例、海量模型(不仅仅是人脸相关的),非常适合开箱即用。所以,这里就写一份使用教程,供大家参考,同时也给自己备忘。
1. lite.ai.toolkit包编译安装
从官网clone下来源码,并使用命令编译安装:
./sh
之后,在build目录下,就会生成“install”文件夹,这是最终的安装结果,留着备用;建议将其另外存放一个安全的位置,防止在build文件夹被删除时一并丢失。
2. 设置环境变量
上一步编译后的包,包含了所需要的所有基本库,需要设置一下环境变量:
export LD_LIBRARY_PATH=your_path/install/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=your_path/install/third_party/opencv/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=your_path/install/third_party/onnxruntime/lib:$LD_LIBRARY_PATH
注意:上述命令中的your_path需要改为第1步得到的“install”所在的父目录!
3. 编写Demo示例
*** 新建一个demo文件夹,在里面准备如下内容:***
先准备一个Demo代码,命名为test_lite_face_parsing_bisenet.cpp
:
//
// Created by DefTruth on 2022/7/1.
//
#include "lite/lite.h"
static void test_default(std::string test_img_path)
{
std::string onnx_path = "../face_parsing_512x512.onnx";
// std::string test_img_path = "../test_lite_face_parsing.png";
std::string save_lable_path = "../label.jpg";
std::string save_img_path = "../merge.jpg";
lite::cv::segmentation::FaceParsingBiSeNet *face_parsing_bisenet =
new lite::cv::segmentation::FaceParsingBiSeNet(onnx_path, 4); // 4 threads
lite::types::FaceParsingContent content;
cv::Mat img_bgr = cv::imread(test_img_path);
face_parsing_bisenet->detect(img_bgr, content);
if (content.flag)
{
if (!content.merge.empty())
{
cv::imwrite(save_lable_path, content.label);
cv::imwrite(save_img_path, content.merge);
}
std::cout << "Default Version FaceParsingBiSeNet Done!" << std::endl;
}
delete face_parsing_bisenet;
}
static void test_lite(std::string test_img_path)
{
test_default(test_img_path);
/*
test_onnxruntime();
test_mnn();
test_ncnn();
test_tnn();
*/
}
int main(__unused int argc, __unused char *argv[])
{
std::string test_img_path = argv[1];
test_lite(test_img_path);
return 0;
}
这是一个人脸解析的Demo,可以将人脸的各个部位,如头发/脸部/左右眉毛/左右眼睛/左右耳朵/鼻子/上下嘴唇/脖子等部分分割出来,从而可以用于下游的一些任务,比如针对某个部位做针对性的特效等。使用的模型是BiSENet。
下面,还需要再编写一个CMakeists.txt
,用于编译我们上面的例子:
cmake_minimum_required(VERSION 3.10)
project(face_parsing)
set(CMAKE_CXX_STANDARD 17)
set(lite.ai.toolkit_DIR your_path/install)
find_package(lite.ai.toolkit REQUIRED PATHS ${lite.ai.toolkit_DIR})
if (lite.ai.toolkit_Found)
message(STATUS "lite.ai.toolkit_INCLUDE_DIRS: ${lite.ai.toolkit_INCLUDE_DIRS}")
message(STATUS " lite.ai.toolkit_LIBS: ${lite.ai.toolkit_LIBS}")
message(STATUS " lite.ai.toolkit_LIBS_DIRS: ${lite.ai.toolkit_LIBS_DIRS}")
endif()
add_executable(lite_face_parsing test_lite_face_parsing_bisenet.cpp)
target_link_libraries(lite_face_parsing ${lite.ai.toolkit_LIBS})
注意,这里的your_path同上,也需要改为你的目录。
此外,还需要准备模型文件和测试图片,其中模型文件从官网提供的下载链接获取(Baidu Drive code: 8gin,从中找到face_parsing_512x512.onnx并下载),测试图片使用一张人脸正面照即可。
4. 编译运行
# 编译
mkdir build && cd build && cmake .. && make -j4
# 运行:制定图片路径
./lite_face_parsing path_to_image
即可在目录中看到保存的结果。下面分别是可视化效果与保存的label:
可视化效果:
label(里面不同的像素值代表不同的部位):
reference
- https://github.com/DefTruth/lite.ai.toolkit/tree/main
- https://github.com/zllrunning/face-parsing.PyTorch
- https://github.com/CoinCheung/BiSeNet
- https://arxiv.org/abs/2004.02147
- @misc{lite.ai.toolkit@2021,
title={lite.ai.toolkit: A lite C++ toolkit of awesome AI models.},
url={https://github.com/DefTruth/lite.ai.toolkit},
note={Open-source software available at https://github.com/DefTruth/lite.ai.toolkit},
author={Yanjun Qiu},
year={2021}
}