C#版免费离线人脸识别之虹软ArcSoft V3.0(推荐)是一款完全开源免费的人脸识别程序,使用者可以在本地环境下运行,不需要联网即可进行人脸识别。下面是详细的使用攻略。
1. 安装ArcSoft SDK
首先,在官网下载并安装ArcSoft SDK,根据自己的操作系统版本选择相应的SDK安装包,可以在ArcSoft官网的开发者中心下载:
http://www.arcsoft.com.cn/developer/
下载完成后,双击安装包,按提示进行安装即可。
2. 设置环境变量
安装完成后,需要将SDK的bin目录添加到环境变量中。在Windows系统中,可以通过以下步骤进行设置:
- 右键点击“计算机”,选择“属性”;
- 点击左侧的“高级系统设置”;
- 在“高级”选项卡中,点击“环境变量”;
- 在“系统变量”中找到“Path”变量,点击“编辑”;
- 在弹出的“编辑环境变量”窗口中,点击“新建”按钮,输入SDK的bin目录路径,例如:D:\arcsoft-sdk\bin;
- 点击“确定”按钮,保存设置并关闭窗口。
3. 下载并运行示例代码
下载 C#版免费离线人脸识别之虹软ArcSoft V3.0(推荐)的示例代码:https://github.com/wgdxy/ArcFaceDemo_CSharp
解压缩后运行 AFDemo.sln
,打开 AFDemo 项目。将项目中 App.config 中的 APPID 、SDKKEY 和 LIBRARY_PATH 替换成你自己的。你可以在官网注册账号并创建应用获取 APPID 和 SDKKEY。LIBRARY_PATH 替换成SDK中的人脸库路径。
4. 运行示例程序测试人脸识别功能
在运行示例程序前,请确保已经安装摄像头或者USB摄像头,并连接正常。
在运行示例程序时,可以通过按下空格键来进行人脸识别。程序将会识别摄像头抓取到的人脸图像,并显示检测到的人脸数量、位置、角度、性别、年龄等信息。可以通过按下Esc键退出程序。
示例1:人脸检测示例
int errorCode = 0;
AFD_FSDK_Version version = new AFD_FSDK_Version();
IntPtr pVersion = Marshal.AllocHGlobal(Marshal.SizeOf(version));
Marshal.StructureToPtr(version, pVersion, false);
errorCode = AFD_FSDK_InitialFaceEngine(AppData._APPID, AppData._FD_SDKKEY, pFDEngine, AFD_FSDK_OPF_0_HIGHER_EXT, 16, 5);
CheckError(errorCode);
errorCode = AFD_FSDK_GetVersion(pFDEngine, pVersion);
CheckError(errorCode);
version = (AFD_FSDK_Version)Marshal.PtrToStructure(pVersion, typeof(AFD_FSDK_Version));
Console.WriteLine("##### AFD_FSDK_GetVersion:{0}.{1}.{2}.{3}", version.lCodebase, version.lMajor, version.lMinor, version.lBuild);
Console.WriteLine("##### AFD_FSDK_GetVersion: "+ Marshal.PtrToStringAnsi(version.Version));
示例2:人脸识别并裁剪处理示例
AFD_FSDK_Version version = new AFD_FSDK_Version();
IntPtr pVersion = Marshal.AllocHGlobal(Marshal.SizeOf(version));
Marshal.StructureToPtr(version, pVersion, false);
APL_FSDK_Version version1 = new APL_FSDK_Version();
IntPtr pVersion1 = Marshal.AllocHGlobal(Marshal.SizeOf(version1));
Marshal.StructureToPtr(version1, pVersion1, false);
errorCode = AFD_FSDK_InitialFaceEngine(AppData._APPID, AppData._FD_SDKKEY, pFDEngine, AFD_FSDK_OPF_0_HIGHER_EXT, 16, 1);
CheckError(errorCode);
errorCode = AFD_FSDK_GetVersion(pFDEngine, pVersion);
CheckError(errorCode);
version = (AFD_FSDK_Version)Marshal.PtrToStructure(pVersion, typeof(AFD_FSDK_Version));
Console.WriteLine("##### AFD_FSDK_GetVersion:{0}.{1}.{2}.{3}", version.lCodebase, version.lMajor, version.lMinor, version.lBuild);
Console.WriteLine("##### AFD_FSDK_GetVersion: "+ Marshal.PtrToStringAnsi(version.Version));
errorCode = AFR_FSDK_InitialEngine(AppData._APPID, AppData._FR_SDKKEY, pFREngine, AFR_FSDK_OPF_0_HIGHER_EXT, 16, 1);
CheckError(errorCode);
errorCode = AFR_FSDK_GetVersion(pFREngine, pVersion1);
CheckError(errorCode);
version1 = (APL_FSDK_Version)Marshal.PtrToStructure(pVersion1, typeof(APL_FSDK_Version));
Console.WriteLine("##### AFR_FSDK_GetVersion:{0}.{1}.{2}.{3}", version1.lCodebase, version1.lMajor, version1.lMinor, version1.lBuild);
Console.WriteLine("##### AFR_FSDK_GetVersion: " + Marshal.PtrToStringAnsi(version1.Version));
int cameraIndex = 0;
for (int i = 0; i < CameraNames.Length; i++)
{
if (CameraNames[i] == AppData._CameraName)
{
cameraIndex = i;
break;
}
}
//连接摄像头
cap = new Capture(cameraIndex);
//得到视频源
fps = (int)cap.GetCaptureProperty(CapProp.Fps);
frameWidth = (int)cap.GetCaptureProperty(CapProp.FrameWidth);
frameHeight = (int)cap.GetCaptureProperty(CapProp.FrameHeight);
while (InvokeFlag)
{
//Capture a Image
frame = cap.QueryFrame();
//镜像变换
Cv2.Flip(frame, frame, FlipMode.Y);
//处理图象
if (frame.Cols > 0 && frame.Rows > 0)
{
IntPtr ImageData = Marshal.AllocHGlobal(frame.Width * frame.Height * 3);
AFT_FSDK_FACERES faceResult = new AFT_FSDK_FACERES();
// 顔檢出
IntPtr pImage = Marshal.AllocHGlobal(frame.Width * frame.Height * 3);
Cv2.CvtColor(frame, frame, ColorConversionCodes.BGR2RGB);
DataConverter.DataConverter.BufferToImageData(frame.ToBytes(), frame.Width, frame.Height, ImageData);
errorCode = AFT_FSDK_FaceFeatureDetect(pFDEngine, frame.Width, frame.Height, AFT_FSDKEngine.CP_PAF_NV21, ImageData, ref faceResult);
Marshal.FreeHGlobal(pImage);
if (errorCode == 0)
{
FaceRect[] rects = new FaceRect[faceResult.nFace];
for (int i = 0; i < rects.Length; i++)
{
rects[i] = (FaceRect)Marshal.PtrToStructure(faceResult.rcFace + Marshal.SizeOf(typeof(FaceRect)) * i, typeof(FaceRect));
}
// 顔認識測
int[] deepFeatures = new int[AppData._MaxFaceCount * 2];
float[] scores = new float[AppData._MaxFaceCount];
IntPtr faceResultPtr = Marshal.AllocHGlobal(faceResult.nFace * Marshal.SizeOf(typeof(AFR_FSDK_FACEINPUT)));
AFR_FSDK_FACEINPUT[] faceInput = new AFR_FSDK_FACEINPUT[faceResult.nFace];
for (int i = 0; i < faceResult.nFace; i++)
{
byte[] data = new byte[rects[i].Width * rects[i].Height * 3];
int width = (int)(rects[i].Width + 3) & ~3;
int height = (int)(rects[i].Height + 1) & ~1;
byte[] cropData = new byte[width * height * 3 / 2];
DataConverter.DataConverter.ImageDataToBuffer(ImageData + rects[i].Y * frame.Width * 3 + rects[i].X * 3, frame.Width * 3, width, height,
cropData, width * 3, AFT_FSDKEngine.CP_PAF_RGB24_B8G8R8);
DataConverter.DataConverter.RGB24ImageToNV21(cropData, width, height, data);
faceInput[i].lOrient = faceResult.lfaceOrient[i];
faceInput[i].rcFace.left = 0;
faceInput[i].rcFace.top = 0;
faceInput[i].rcFace.right = width;
faceInput[i].rcFace.bottom = height;
faceInput[i].pbFeature = Marshal.AllocHGlobal(AFR_FSDK_FACEFEATURE.FeatureSize);
Marshal.Copy(Marshal.AllocHGlobal(AFR_FSDK_FACEFEATURE.FeatureSize).ToInt32() - AFR_FSDK_FACEFEATURE.FeatureSize, deepFeatures, i * 2, 2);
faceInput[i].lFeatureSize = deepFeatures[i * 2 + 1];
errorCode = AFR_FSDK_ExtractFRFeature(pFREngine, data, width, height, AFR_FSDKEngine.CP_PAF_NV21, ref faceInput[i], ref deepFeatures[i * 2]);
if (errorCode != 0)
{
Marshal.FreeHGlobal(faceInput[i].pbFeature);
faceInput[i].pbFeature = IntPtr.Zero;
}
Marshal.StructureToPtr(faceInput[i], faceResultPtr + Marshal.SizeOf(typeof(AFR_FSDK_FACEINPUT)) * i, false);
}
errorCode = AFR_FSDK_FacePairMatching(pFREngine, faceResultPtr, faceResult.nFace, deepFeatures, scores);
for (int i = 0; i < faceResult.nFace; i++)
{
if (scores[i] < AppData._Threshold)
{
Cv2.Rectangle(frame, new OpenCvSharp.Rect(rects[i].Left, rects[i].Top, rects[i].Width, rects[i].Height), new Scalar(0, 255, 0));
Cv2.PutText(frame, "Unknown", new Point(rects[i].Left, rects[i].Top), HersheyFonts.HersheyComplex, 1, new Scalar(0, 255, 0), 2, LineTypes.Link8, false);
}
else
{
string name = System.Web.HttpUtility.UrlDecode(AppData._FaceDbMatch.FindNameByFeature(deepFeatures, i, ref scores[i]), System.Text.Encoding.UTF8);
Cv2.Rectangle(frame, new OpenCvSharp.Rect(rects[i].Left, rects[i].Top, rects[i].Width, rects[i].Height), new Scalar(0, 0, 255));
Cv2.PutText(frame, name, new Point(rects[i].Left, rects[i].Top), HersheyFonts.HersheyComplex, 1, new Scalar(0, 0, 255), 2, LineTypes.Link8, false);
}
if (faceInput[i].pbFeature != IntPtr.Zero)
{
Marshal.FreeHGlobal(faceInput[i].pbFeature);
}
}
Marshal.FreeHGlobal(faceResultPtr);
}
else
{
Console.WriteLine("error code: " + errorCode);
}
Marshal.FreeHGlobal(ImageData);
}
//显示当前图象
try
{
videoImage.Dispatcher.Invoke(new ShowImageDelagate(ShowImage), frame.ToBitmapSource());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C#版免费离线人脸识别之虹软ArcSoft V3.0(推荐) - Python技术站