SeetaFace6Onnx.model.fas_first 1.0.1

dotnet add package SeetaFace6Onnx.model.fas_first --version 1.0.1
                    
NuGet\Install-Package SeetaFace6Onnx.model.fas_first -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SeetaFace6Onnx.model.fas_first" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SeetaFace6Onnx.model.fas_first" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="SeetaFace6Onnx.model.fas_first" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SeetaFace6Onnx.model.fas_first --version 1.0.1
                    
#r "nuget: SeetaFace6Onnx.model.fas_first, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SeetaFace6Onnx.model.fas_first@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SeetaFace6Onnx.model.fas_first&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=SeetaFace6Onnx.model.fas_first&version=1.0.1
                    
Install as a Cake Tool

<div align="center">

SeetaFace6Onnx

基于 ONNX Runtime 的 SeetaFace6 .NET 人脸识别库

项目简介 · 快速开始 · 模型包 · 执行设备 · 示例项目 · 构建与测试

</div>

项目简介

SeetaFace6Onnx 将 SeetaFace6 的模型转换为 ONNX,使用OnnxRuntime进行推理。提供人脸检测、识别、关键点、属性、质量、活体和跟踪等功能。业务代码均在托管层实现,运行时不再依赖 SeetaFace、TenniS 或 C++ Bridge。

  • 支持 .NET 6+
  • 使用 SkiaSharp 读取和处理图像
  • 支持 ONNX Runtime CPU、NVIDIA CUDA 和 Windows DirectML 执行后端
  • 高性能,当前项目推理性能至少是SeetaFace6Sharp 5倍以上
  • 跨平台,支持win(x64)、linux(x64/arm64/loongarch64)等

SeetaFace6Onnx 的业务层不依赖原生 SeetaFace 运行时,但 ONNX Runtime 和 SkiaSharp
仍包含与平台相关的原生组件。部署时必须为目标平台选择正确的 NuGet 运行时包。

功能

功能 主要类型 说明
人脸检测 FaceDetector 返回人脸位置和置信度
人脸关键点 FaceLandmarker 支持 Light 5 点、Normal 68 点和 Mask 5 点
人脸识别 FaceRecognizer 支持 Normal、Light、Mask 特征提取与相似度比较
人脸跟踪 FaceTracker 面向连续视频帧的有状态跟踪
活体检测 FaceAntiSpoofing 支持单帧和连续视频帧检测
属性预测 AgePredictorGenderPredictor 年龄和性别预测
状态检测 MaskDetectorEyeStateDetector 口罩和双眼状态检测
质量评估 FaceQualityQualityOfLBN 亮度、清晰度、完整度、姿态、分辨率、结构和 LBN

快速开始

1. 安装依赖

核心库不包含 ONNX 模型,也只引用 ONNX Runtime 的托管 API。一个可运行的应用需要同时安装:

  1. SeetaFace6Onnx 核心包;
  2. 一个 ONNX Runtime Provider 包;
  3. 所需的模型包。

过下列命令安装 CPU 运行时和全部模型:

dotnet add package SeetaFace6Onnx
dotnet add package SeetaFace6Onnx.model.all
dotnet add package Microsoft.ML.OnnxRuntime

Linux 应用还需要部署 SkiaSharp 的 Linux 原生资源:

dotnet add package SkiaSharp.NativeAssets.Linux

生产项目可以只安装实际使用的模型包,以减小发布体积。参见模型包

2. 检测并提取人脸特征

using System;
using System.IO;
using SeetaFace6Onnx.Models;
using SeetaFace6Onnx.Predictors;
using SkiaSharp;

using SKBitmap image = SKBitmap.Decode("face.jpg")
    ?? throw new IOException("无法读取 face.jpg");
using var detector = new FaceDetector();
using var landmarker = new FaceLandmarker();
using var recognizer = new FaceRecognizer();

FaceInfo[] faces = detector.Detect(image);
if (faces.Length == 0)
{
    Console.WriteLine("未检测到人脸");
    return;
}

FaceInfo face = faces[0];
FaceMarkPoint[] points = landmarker.Mark(image, face);
float[] feature = recognizer.Extract(image, points);

Console.WriteLine($"置信度:{face.Score:F4}");
Console.WriteLine($"位置:{face.Location}");
Console.WriteLine($"特征长度:{feature.Length}");

上述代码使用默认的 Light 5 点关键点模型、Normal 识别模型和 CPU Provider。比较两张人脸时, 分别提取特征后调用 recognizer.Compare(feature1, feature2)recognizer.IsSelf(feature1, feature2)

模型包

目前包含以下模型包(均可以通过nuget直接安装):

模型包 用途
SeetaFace6Onnx.model.all 全部 15 个模型
SeetaFace6Onnx.model.face_detector 人脸检测和人脸跟踪
SeetaFace6Onnx.model.face_landmarker_pts5 Light 5 点关键点
SeetaFace6Onnx.model.face_landmarker_pts68 Normal 68 点关键点
SeetaFace6Onnx.model.face_landmarker_mask_pts5 Mask 5 点关键点
SeetaFace6Onnx.model.face_recognizer Normal 人脸识别,1024 维特征
SeetaFace6Onnx.model.face_recognizer_light Light 人脸识别,512 维特征
SeetaFace6Onnx.model.face_recognizer_mask Mask 人脸识别,512 维特征
SeetaFace6Onnx.model.fas_first 局部活体检测
SeetaFace6Onnx.model.fas_second 全局活体检测,默认启用
SeetaFace6Onnx.model.age_predictor 年龄预测
SeetaFace6Onnx.model.gender_predictor 性别预测
SeetaFace6Onnx.model.mask_detector 口罩检测
SeetaFace6Onnx.model.eye_state 眼睛状态检测
SeetaFace6Onnx.model.pose_estimation 扩展姿态质量评估
SeetaFace6Onnx.model.quality_lbn LBN 质量评估

安装对应的模型包后,会在构建和发布时将对应模型文件复制到:

runtimes/models/seetaface6

该路径也是 BaseConfig.ModelDirectory 的默认值。

如需从自定义位置加载模型,可为对应模块指定绝对目录:

var config = new FaceDetectConfig
{
    ModelDirectory = @"D:\models\seetaface6",
};
using var detector = new FaceDetector(config);

目录内的文件名必须与模型包中的名称一致,例如 face_detector.onnx

推理后端

Provider NuGet 包 适用环境
CPU Microsoft.ML.OnnxRuntime 默认选项,具体系统和架构以 ORT 包支持范围为准
CUDA Microsoft.ML.OnnxRuntime.Gpu 配有兼容驱动、CUDA 和 cuDNN 的 NVIDIA GPU
DirectML Microsoft.ML.OnnxRuntime.DirectML Windows 10/11,支持 DirectX 12 的设备

三个包都包含名为 onnxruntime 的原生库,同一个输出目录中只应选择一个。特别是 CUDA 和 DirectML 包不能混合部署。

默认执行设备为 CPU。使用加速设备时,既要安装对应 Provider 包,也要在每个模块的配置中明确选择:

var config = new FaceDetectConfig
{
    ExecutionProvider = ExecutionProvider.Cuda,
    GpuDeviceId = 0,
};
using var detector = new FaceDetector(config);

Cuda 替换为 DirectML 即可选择 DirectML。库不会在 Provider 不可用时静默回退,配置和部署
不匹配会抛出 NotSupportedException。CUDA 的驱动及依赖版本要求请参考 ONNX Runtime CUDA Execution Provider 文档

源码中的示例项目通过 OnnxRuntimeFlavor 选择原生包,默认值为 Cpu

dotnet build src\examples\SeetaFace6Onnx.Example.ConsoleApp -c Release -p:OnnxRuntimeFlavor=Cpu
dotnet build src\examples\SeetaFace6Onnx.Example.ConsoleApp -c Release -p:OnnxRuntimeFlavor=Cuda
dotnet build src\examples\SeetaFace6Onnx.Example.ConsoleApp -c Release -p:OnnxRuntimeFlavor=DirectML

OnnxRuntimeFlavor 只决定部署哪个原生 Provider;应用仍需通过各模块的 ExecutionProvider 配置 决定实际使用的设备。

性能与并发

  • 相同模型、Provider、设备编号和线程配置会共享 ONNX Runtime Session。
  • ThreadNumber 控制 ORT 的 intra-op 线程数;默认值已按模块分档,但仍应在目标硬件上实测。
  • FaceTrackerFaceAntiSpoofing.PredictVideo 保存视频状态,每路视频应使用独立实例。
  • 高频调用可使用接收 Span<T> 的重载复用结果缓冲,减少托管分配。
var faces = new FaceInfo[30];
var points = new FaceMarkPoint[landmarker.PointCount];
var feature = new float[recognizer.FeatureSize];

int faceCount = detector.Detect(image, faces);
if (faceCount > 0)
{
    landmarker.Mark(image, faces[0], points);
    recognizer.Extract(image, points, feature);
}

CPU 线程测试方法和 Ryzen 9 9950X 基线见 docs/cpu-thread-benchmark.md。运行端到端基准:

dotnet run --project src\benchmarks\SeetaFace6Onnx.Benchmarks -c Release -- `
  scripts\onnx\fp32 `
  src\benchmarks\SeetaFace6Onnx.Benchmarks\images\Jay_3.jpg `
  50

示例项目

仓库中的示例面向 .NET 10 SDK:

项目 说明
SeetaFace6Onnx.Example.ConsoleApp 检测、关键点、属性、质量、活体、识别和跟踪
SeetaFace6Onnx.Example.WebApp ASP.NET Core 图片上传与人脸分析
SeetaFace6Onnx.Example.Camera Avalonia + FlashCap 跨平台摄像头示例
SeetaFace6Onnx.Example.VideoForm Windows Forms 视频和人脸库示例
dotnet run --project src\examples\SeetaFace6Onnx.Example.ConsoleApp -c Release
dotnet run --project src\examples\SeetaFace6Onnx.Example.WebApp -c Release
dotnet run --project src\examples\SeetaFace6Onnx.Example.Camera -c Release
dotnet run --project src\examples\SeetaFace6Onnx.Example.VideoForm -c Release

Camera 和 VideoForm 使用 FlashCap 获取摄像头帧。VideoForm 仅支持 Windows。

构建与测试

构建核心库:

dotnet restore src\SeetaFace6Onnx\SeetaFace6Onnx.csproj
dotnet build src\SeetaFace6Onnx\SeetaFace6Onnx.csproj -c Release --no-restore

运行兼容性和推理测试:

dotnet test src\tests\SeetaFace6Onnx.Tests\SeetaFace6Onnx.Tests.csproj -c Release

测试项目会同时引用 SeetaFace6Sharp 1.0.10 作为差分基准,因此完整兼容性测试当前面向 Windows x64。

模型导出

原始 CSTA 模型位于 scripts/weights,导出的 FP32、FP16 和 INT8 文件分别位于 scripts/onnx/fp32scripts/onnx/fp16scripts/onnx/int8。安装 Python 依赖后可重新导出:

python -m pip install -r scripts\requirements.txt
python scripts\export_all.py --dtype float32
python scripts\verify_precision.py --dtype float32

--dtype 还接受 float16int8。当前 NuGet 模型项目只打包 FP32 文件;FP16 和 INT8 属于实验产物,部署前应在目标 Provider、真实数据和业务阈值上重新验证精度与性能。

项目结构

src/SeetaFace6Onnx/   核心库
src/models/           ONNX 模型 NuGet 打包项目
src/examples/         Console、Web、Avalonia 和 WinForms 示例
src/tests/            API、推理和兼容性测试
src/benchmarks/       .NET 端到端基准
scripts/              CSTA 解析、ONNX 导出、验证和 Python 基准
docs/                 设计与性能记录

参考与许可

本项目使用的 SeetaFace 模型来源于 SeetaFace6Open。使用、修改或分发代码及模型时,请同时遵守 SeetaFace6Open、ONNX Runtime、SkiaSharp 和其他第三方依赖的许可条款。

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on SeetaFace6Onnx.model.fas_first:

Package Downloads
SeetaFace6Onnx.model.all

All SeetaFace6 ONNX models for SeetaFace6Onnx.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 184 8/3/2026