InspireFace-商用級的跨平臺開源人臉分析SDK
InspireFaceSDK是由insightface開發的?款?臉識別軟件開發?具包(SDK)。它提供了?系列功能,可以滿?各種應?場景下的?臉識別需求,包括但不限于閘機、?臉?禁、?臉驗證等。
該SDK包含了多種常?的?臉識別算法,涵蓋了?臉檢測、?臉屬性分析、?臉特征提取和?臉?對等功能。這些算法經過精?設計,具備業界領先的?臉識別能?和算法精度?平。為了滿?不同的部署需求,InspireFaceSDK提供了四種不同平臺的部署選項。
這些選項涵蓋了各種推理場景,包括閘機芯?、端側設備(如智能?機、平板電腦)、CPU(中央處理器)和服務器(CUDA)、NPU(神經處理單元)等。開發?員可以根據實際情況選擇適合??應?的部署?案。
如何獲取
- 你可以直接從insightface.ai進入到倉庫中獲取到InspireFaceSDK的穩定版本的源代碼;
- 如果你持續關注代碼更新動態,你可以從InspireFace的開發倉庫進入獲取到開發版本的資源;
功能介紹
InspireFaceSDK目前支持以下的功能,并且有更多的功能正在開發中:
Index | Function | Adaptation | Note |
---|---|---|---|
1 | 人臉檢測 | 支持 | 支持多種尺度圖像的輸入 |
2 | 稠密關鍵點檢測 | 支持 | |
3 | 人臉識別 | 支持 | 基于arcface的高精度模型 |
4 | 人臉跟蹤 | 支持 | 擁有多種跟蹤模式 |
5 | 口罩檢測 | 支持 | |
6 | 靜默人臉活體檢測 | 支持 | MiniVision |
7 | 質量分析 | 支持 | |
8 | 頭部姿態估計 | 支持 | |
9 | 屬性分析 | 支持 | 年齡、種族、性別等等 |
10 | 表情與動作分析 | 開發中 | 眨眼、搖頭、點頭 |
11 | 人臉紅外活體檢測 | 開發中 |
平臺與架構的支持
我們已經完成了跨各種操作系統和CPU架構的軟件適配和測試。這包括對Linux、macOS、iOS和Android等平臺的兼容性驗證,以及對特定硬件如Rockchip部分設備支持的測試,以確保在不同環境下穩定運行。
No. | Operating System | CPU Architecture | Special Device Support | Adapted | Passed Tests |
---|---|---|---|---|---|
1 | Linux | ARMv7 | - | 已適配 | 通過離線測試 |
2 | ARMv8 | - | 已適配 | 通過離線測試 | |
3 | x86/x86_64 | - | 已適配 | 通過在線測試 | |
4 | ARMv7 | RV1109RV1126 | 已適配 | 通過離線測試 | |
5 | x86/x86_64 | CUDA | 已適配 | 通過離線測試 | |
6 | macOS | Intel x86 | - | 已適配 | 通過離線測試 |
7 | Apple Silicon | - | 已適配 | 通過離線測試 | |
8 | iOS | ARM | - | 已適配 | 通過離線測試 |
9 | Android | ARMv7 | - | 已適配 | |
10 | ARMv8 | - | 已適配 |
[編譯]準備工作
安裝OpenCV
如果您打算在本地或服務器上使用SDK,請確保已經在主機設備上安裝了OpenCV,以便在編譯過程中成功鏈接。對于交叉編譯目標,如Android或ARM嵌入式板,您可以使用3rdparty/ inspirreface -precompile/ OpenCV /提供的預編譯OpenCV庫,所以可以跳過此步驟。
安裝MNN
3rdparty目錄已經包含了MNN庫,并指定了一個特定的版本作為穩定版本。如果您需要在編譯期間啟用或禁用其他配置選項,您可以參考MNN提供的CMake選項。如果您需要使用自己的預編譯版本,請隨意替換它,如果不需要請跳過此步驟。
獲取工程
您可以拉取穩定版本或開發版的工程代碼到本地:
# Pull the stable version
git clone https://github.com/deepinsight/insightface
cd insightface/cpp-package/inspireface/# or, Pull the develop version
git clone https://github.com/HyperInspire/InspireFace
拉取第三方依賴
在編譯前必須要拉取第三方依賴庫到inspireface
的根目錄下:
# Must enter this directory
cd InspireFace
# Clone the repository and pull submodules
git clone --recurse-submodules https://github.com/HyperInspire/3rdparty.git
如果你需要更新3rdpart
倉庫以確保它是最新的,或者如果你在初始拉取時沒有使用——recursive
參數,你可以運行git submodule update——init——recursive
:
# Must enter this directory
cd InspireFace
# If you're not using recursive pull
git clone https://github.com/HyperInspire/3rdparty.gitcd 3rdparty
git pull
# Update submodules
git submodule update --init --recursive
環境要求
目前InspireFace支持的宿主機設備僅有Linux和MacOS兩個系統,請確保您的計算機符合以下配置需求:
- CMake (version 3.10 or higher)
- OpenCV (version 4.20 or higher)
- Use the specific OpenCV-SDK supported by each target platform such as Android, iOS, and Linux.
- NDK (version 16 or higher, only required for Android)
- MNN (version 1.4.0 or higher)
- C++ Compiler
- Either GCC or Clang can be used (macOS does not require additional installation as Xcode is included)
- Recommended GCC version is 4.9 or higher
- Note that in some distributions, GCC (GNU C Compiler) and G++ (GNU C++ Compiler) are installed separately.
- For instance, on Ubuntu, you need to install both gcc and g++
- Recommended Clang version is 3.9 or higher
- Recommended GCC version is 4.9 or higher
- arm-linux-gnueabihf (for RV1109/RV1126)
- Prepare the cross-compilation toolchain in advance, such as gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf
- Either GCC or Clang can be used (macOS does not require additional installation as Xcode is included)
- CUDA (version 10.1 or higher)
- GPU-based inference requires installing NVIDIA’s CUDA dependencies on the device.
- Eigen3
- If you need to use the tracking-by-detection feature, you must have Eigen3 installed in advance.
- RKNN
- Adjust and select versions currently supported for specific requirements.
[編譯]開始編譯
工程采用CMake作為構建工具,CMake的選項用于控制編譯階段的各種細節。請根據您的實際需求進行選擇。您可以查看參數表CMake Option。
Linux/MacOS編譯
確保安裝了OpenCV,就可以開始編譯過程了。如果您使用的是macOS或Linux,您可以使用項目根目錄下command
文件夾中提供的shell腳本快速編譯:
cd InspireFace/
# Execute the local compilation script
bash command/build.sh
編譯之后,您可以在構建目錄中找到包含編譯結果的本地文件。安裝目錄結構如下:
inspireface-linux├── include│ ├── herror.h│ └── inspireface.h└── lib└── libInspireFace.so
- libInspireFace.so:編譯的動態鏈接庫.
- inspireface.h:頭文件定義
- herror.h:引用錯誤號定義
Note: 更多平臺的相關的詳細編譯過程您可以直接進入源代碼倉庫中。
[示例]如何使用
C/C++ API
要將InspireFace集成到C/ c++項目中,您只需要鏈接InspireFace庫并包含適當的頭文件。下面是一個展示人臉檢測的基本例子:
HResult ret;
// The resource file must be loaded before it can be used
ret = HFLaunchInspireFace(packPath);
if (ret != HSUCCEED) {std::cout << "Load Resource error: " << ret << std::endl;return ret;
}// Enable the functions in the pipeline: mask detection, live detection, and face quality detection
HOption option = HF_ENABLE_QUALITY | HF_ENABLE_MASK_DETECT | HF_ENABLE_LIVENESS;
// Non-video or frame sequence mode uses IMAGE-MODE, which is always face detection without tracking
HFDetectMode detMode = HF_DETECT_MODE_IMAGE;
// Maximum number of faces detected
HInt32 maxDetectNum = 5;
// Handle of the current face SDK algorithm context
HFSession session = {0};
ret = HFCreateInspireFaceSessionOptional(option, detMode, maxDetectNum, -1, -1, &session);
if (ret != HSUCCEED) {std::cout << "Create FaceContext error: " << ret << std::endl;return ret;
}// Load a image
cv::Mat image = cv::imread(sourcePath);
if (image.empty()) {std::cout << "The source entered is not a picture or read error." << std::endl;return 1;
}
// Prepare an image parameter structure for configuration
HFImageData imageParam = {0};
imageParam.data = image.data; // Data buffer
imageParam.width = image.cols; // Target view width
imageParam.height = image.rows; // Target view width
imageParam.rotation = HF_CAMERA_ROTATION_0; // Data source rotate
imageParam.format = HF_STREAM_BGR; // Data source format// Create an image data stream
HFImageStream imageHandle = {0};
ret = HFCreateImageStream(&imageParam, &imageHandle);
if (ret != HSUCCEED) {std::cout << "Create ImageStream error: " << ret << std::endl;return ret;
}// Execute HF_FaceContextRunFaceTrack captures face information in an image
HFMultipleFaceData multipleFaceData = {0};
ret = HFExecuteFaceTrack(session, imageHandle, &multipleFaceData);
if (ret != HSUCCEED) {std::cout << "Execute HFExecuteFaceTrack error: " << ret << std::endl;return ret;
}
// Print the number of faces detected
auto faceNum = multipleFaceData.detectedNum;
std::cout << "Num of face: " << faceNum << std::endl;ret = HFReleaseImageStream(imageHandle);
if (ret != HSUCCEED) {printf("Release image stream error: %lu\n", ret);
}
// The memory must be freed at the end of the program
ret = HFReleaseInspireFaceSession(session);
if (ret != HSUCCEED) {printf("Release session error: %lu\n", ret);return ret;
}
Note: 更多使用相關的詳細方法您可以直接進入源代碼倉庫中查看。
Python Native API
InspireFace提供了一個Python API,允許更有效地使用InspireFace庫。編譯完動態鏈接庫后,你需要將其符號鏈接或復制到根目錄下的python/inspireface/modules/core
目錄下。然后你可以通過導航到**python/**目錄開始測試。你的Python環境需要安裝一些依賴項:
import cv2
import inspireface as ifac
from inspireface.param import *# Step 1: Initialize the SDK and load the algorithm resource files.
resource_path = "pack/Pikachu"
ret = ifac.launch(resource_path)
assert ret, "Launch failure. Please ensure the resource path is correct."# Optional features, loaded during session creation based on the modules specified.
opt = HF_ENABLE_NONE
session = ifac.InspireFaceSession(opt, HF_DETECT_MODE_IMAGE)# Load the image using OpenCV.
image = cv2.imread(image_path)
assert image is not None, "Please check that the image path is correct."# Perform face detection on the image.
faces = session.face_detection(image)
print(f"face detection: {len(faces)} found")# Copy the image for drawing the bounding boxes.
draw = image.copy()
for idx, face in enumerate(faces):print(f"{'==' * 20}")print(f"idx: {idx}")# Print Euler angles of the face.print(f"roll: {face.roll}, yaw: {face.yaw}, pitch: {face.pitch}")# Draw bounding box around the detected face.x1, y1, x2, y2 = face.locationcv2.rectangle(draw, (x1, y1), (x2, y2), (0, 0, 255), 2)
Note: 倉庫中的會提供更多的其他平臺下的使用方法,我們推薦您使用最新的代碼進行集成
[快捷]直接下載預編譯的庫
如果你想快速體驗來節約編譯的時間你可以跳過編譯步驟,直接通過開發倉庫的Release頁面下載我們提供的最新的發行版SDK,其中涵蓋了大部分的平臺支持,所有的預編譯庫均是通過Github Actions進行持續集成發布,已經通過了測試。
模型列表
InspireFace目前提供了多款模型應對不同的使用場景,目前涵蓋了移動端、服務端和部分嵌入式設備端,您可以進入到我們的Github頁面進行更詳細的了解和下載體驗:
Name | Supported Devices | Note |
---|---|---|
Pikachu | CPU | 適合移動端設備部署 |
Megatron | CPU, GPU | 適合移動端、服務端和攜GPU設備部署 |
Gundam-RV1109 | RKNPU | 支持RK1109/1126設備部署 |
測試實例