整个项目使用虹软技术完成开发
一,准备工作
1.Afoge视频参数类
using AForge.Video.DirectShow;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FaceRecognization.Common
{
public class CameraPara
{
/// <summary>
/// 是否有摄像头
/// </summary>
public bool HasVideoDevice { get; set; }
/// <summary>
/// 视频源
/// </summary>
public VideoCaptureDevice VideoSource { get; set; }
/// <summary>
/// 视频图片的宽度
/// </summary>
public int FrameWidth { get; set; }
/// <summary>
/// 视频图片的高度
/// </summary>
public int FrameHeight { get; set; }
/// <summary>
/// 视频图片的字节数
/// </summary>
public int ByteCount { get; set; }
public CameraPara()
{
var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count == 0)//没有检测到摄像头
{
HasVideoDevice = false;
return;
}
VideoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);//连接第一个摄像头
var videoResolution = VideoSource.VideoCapabilities.First(ii => ii.FrameSize.Width == VideoSource.VideoCapabilities.Max(jj => jj.FrameSize.Width)); //获取摄像头最高的分辨率
FrameWidth = videoResolution.FrameSize.Width;
FrameHeight = videoResolution.FrameSize.Height;
ByteCount = videoResolution.BitCount / 8;
VideoSource.VideoResolution = videoResolution;
HasVideoDevice = true;
}
}
}
2.人脸识别相关的结构、类 和枚举
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace FaceRecognization.Face
{
/// <summary>
/// 人脸库
/// </summary>
public class FaceLib
{
public List<Item> Items { get; set; } = new List<Item>();
public class Item
{
/// <summary>
/// 用于排序
/// </summary>
public long OrderId { get; set; }
/// <summary>
/// 文件名作为ID
/// </summary>
public string ID { get; set; }
/// <summary>
/// 人脸模型
/// </summary>
public FaceModel FaceModel { get; set; }
}
}
/// <summary>
/// 人脸识别结果
/// </summary>
public class FaceResult
{
//public int NotMatchedCount { get; set; }
public string ID { get; set; }
public float Score { get; set; }
public System.Drawing.Rectangle Rectangle { get; set; }
public int Age { get; set; }
/// <summary>
/// 0:男,1:女,其他:未知
/// </summary>
public int Gender { get; set; }
public override string ToString()
{
string ret = "";
if (!string.IsNullOrEmpty(ID))
ret = ID + ",";
ret += Age + "岁";
if (Gender == 0)
ret += ",男";
else if (Gender == 1)
ret += ",女";
return ret + "," + Score;
}
}
/// <summary>
/// 人脸跟踪、检测、性别年龄评估和获取人脸信息的输入参数
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct ImageData
{
public uint u32PixelArrayFormat;
public int i32Width;
public int i32Height;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public IntPtr[] ppu8Plane;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4, ArraySubType = UnmanagedType.I4)]
public int[] pi32Pitch;
}
/// <summary>
/// 人脸跟踪的结果
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal struct TraceResult
{
[MarshalAs(UnmanagedType.I4)]
public int nFace;
[MarshalAs(UnmanagedType.I4)]
public int lfaceOrient;
public IntPtr rcFace;
}
/// <summary>
/// 人脸检测的结果
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal struct DetectResult
{
[MarshalAs(UnmanagedType.I4)]
public int nFace;
public IntPtr rcFace;
public IntPtr lfaceOrient;
}
/// <summary>
/// 人脸在图片中的位置
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct FaceRect
{
public int left;
public int top;
public int right;
public int bottom;
}
/// <summary>
/// 获取人脸特征的输入参数
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal struct FaceFeatureInput
{
public FaceRect rcFace;
public int lOrient;
}
/// <summary>
/// 人脸特征
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct FaceModel
{
public IntPtr pbFeature;
[MarshalAs(UnmanagedType.I4)]
public int lFeatureSize;
}
/// <summary>
/// 性别和年龄评估的输入参数
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal struct EstimationInput
{
public IntPtr pFaceRectArray;
public IntPtr pFaceOrientArray;
public int lFaceNumber;
}
/// <summary>
/// 性别和年龄评估的结果
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal struct EstimationResult
{
public IntPtr pResult;
public int lFaceNumber;
}
/// <summary>
/// 错误代码
/// </summary>
public enum ErrorCode
{
/// <summary>
/// 正确
/// </summary>
Ok = 0,
/// <summary>
/// 通用错误类型
/// </summary>
BasicBase = 0x0001,
/// <summary>
/// 错误原因不明
/// </summary>
Unknown = BasicBase,
/// <summary>
/// 无效的参数
/// </summary>
InvalidParam = BasicBase + 1,
/// <summary>
/// 引擎不支持
/// </summary>
Unsupported = BasicBase + 2,
/// <summary>
/// 内存不足
/// </summary>
NoMemory = BasicBase + 3,
/// <summary>
/// 状态错误
/// </summary>