人脸识别库
ViewFaceCore 是一个使用 C# 开发的超简单的离线人脸识别库。
( 基于 SeetaFace6 ),多平台支持, 开源、免费、跨平台 (win/linux)。
支持的.NET 版本, .NET Framework,.NET Standard, .NET/.NET Core 。
简单的人脸信息检测
以 windows x64平台 为例,一个简单的人脸检测Demo,需要先使用 nuget 安装依赖。
ViewFaceCoreViewFaceCore .NET 核心库。
ViewFaceCore.all_models人脸检测的模型支持(图省事可以直接安装这个)。
ViewFaceCore.runtime.win.x64Windows-x64 的本机运行时,其它平台自行选择安装,可安装多个。
ViewFaceCore.Extension.SkiaSharpSkiaSharp图像处理扩展,ImageSharp、SkiaSharp、System.Drawing三选一。
开始获取人脸信息,如下
using SkiaSharp;
using System;
using ViewFaceCore.Core;
using ViewFaceCore.Model;
namespace ViewFaceCore.Demo.ConsoleApp
{
internal class Program
{
private readonly static string imagePath = @"images/Jay_3.jpg";
static void MAIn(string[] args)
{
using var bitmap = SKBitmap.Decode(imagePath);
using FaceDetector faceDetector = new FaceDetector();
FaceInfo[] infos = faceDetector.Detect(bitmap);
Console.WriteLine($"识别到的人脸数量:{infos.Length} 个人脸信息:n");
Console.WriteLine($"No.t人脸置信度t位置信息");
for (int i = 0; i < infos.Length; i++)
{
Console.WriteLine($"{i}t{infos[i].Score:f8}t{infos[i].Location}");
}
Console.ReadKey();
}
}
}
static void AntiSpoofingDemo()
{
using var bitmap = SKBitmap.Decode(imagePath0);
using FaceDetector faceDetector = new FaceDetector();
using FaceLandmarker faceMark = new FaceLandmarker();
using FaceAntiSpoofing faceAntiSpoofing = new FaceAntiSpoofing();
var info = faceDetector.Detect(bitmap).First();
var markPoints = faceMark.Mark(bitmap, info);
Stopwatch sw = Stopwatch.StartNew();
sw.Start();
var result = faceAntiSpoofing.AntiSpoofing(bitmap, info, markPoints);
Console.WriteLine($"活体检测,结果:{result.Status},清晰度:{result.Clarity},真实度:{result.Reality},耗时:{sw.ElapsedMilliseconds}ms");
sw.Stop();
Console.WriteLine();
}
人脸检测,输入待检测的图片,输出检测到的每个人脸位置,用矩形表示。
using System;
using System.Drawing;
using System.Linq;
using ViewFaceCore;
using ViewFaceCore.Core;
using ViewFaceCore.Model;
namespace Demo
{
internal class Program
{
private readonly static string imagePath = @"images/Jay_3.jpg";
private readonly static string outputPath = @"images/Jay_out.jpg";
static void Main(string[] args)
{
using var bitmap = (Bitmap)Image.FromFile(imagePath);
using FaceDetector faceDetector = new FaceDetector();
FaceInfo[] infos = faceDetector.Detect(bitmap);
//输出人脸信息
Console.WriteLine($"识别到的人脸数量:{infos.Length} 个人脸信息:n");
Console.WriteLine($"No.t人脸置信度t位置信息");
for (int i = 0; i < infos.Length; i++)
{
Console.WriteLine($"{i}t{infos[i].Score:f8}t{infos[i].Location}");
}
//画方框,标记人脸
using (Graphics g = Graphics.FromImage(bitmap))
{
g.DrawRectangles(new Pen(Color.Red, 4), infos.Select(p => new RectangleF(p.Location.X, p.Location.Y, p.Location.Width, p.Location.Height)).ToArray());
}
bitmap.Save(outputPath);
Console.WriteLine($"输出图片已保存至:{outputPath}");
Console.WriteLine();
}
}
}
人脸识别的一个基本概念,就是将待识别的人脸经过处理变成二进制数据的特征,然后基于特征表示的人脸进行相似度计算,最终与相似度阈值对比,一般超过阈值就认为特征表示的人脸是同一个人。
static void FaceRecognizerDemo()
{
Stopwatch sw = Stopwatch.StartNew();
sw.Start();
using var faceImage0 = SKBitmap.Decode(imagePath0).ToFaceImage();
using var faceImage1 = SKBitmap.Decode(imagePath1).ToFaceImage();
//检测人脸信息
using FaceDetector faceDetector = new FaceDetector();
FaceInfo[] infos0 = faceDetector.Detect(faceImage0);
FaceInfo[] infos1 = faceDetector.Detect(faceImage1);
//标记人脸位置
using FaceLandmarker faceMark = new FaceLandmarker();
FaceMarkPoint[] points0 = faceMark.Mark(faceImage0, infos0[0]);
FaceMarkPoint[] points1 = faceMark.Mark(faceImage1, infos1[0]);
//提取特征值
using FaceRecognizer faceRecognizer = new FaceRecognizer();
float[] data0 = faceRecognizer.Extract(faceImage0, points0);
float[] data1 = faceRecognizer.Extract(faceImage1, points1);
//对比特征值
bool isSelf = faceRecognizer.IsSelf(data0, data1);
Console.WriteLine($"识别到的人脸是否为同一人:{isSelf},对比耗时:{sw.ElapsedMilliseconds}ms");
Console.WriteLine();
sw.Stop();
}