C#版免費(fèi)離線人臉識(shí)別之虹軟ArcSoft?V3.0(推薦)
【溫馨提示】本文共678字(不含代碼),8張圖。預(yù)計(jì)閱讀時(shí)間需要6分鐘。
1. 前言
人臉識(shí)別&比對(duì)發(fā)展到今天,已經(jīng)是一個(gè)非常成熟的技術(shù)了,而且應(yīng)用在生活的方方面面,比如手機(jī)、車(chē)站、天網(wǎng)等。
我從2016年就開(kāi)始做人臉識(shí)別相關(guān)的App,到現(xiàn)在差不多4個(gè)年頭了,用過(guò)的SDK有微軟認(rèn)知服務(wù)、曠視科技的Face++、開(kāi)源的OpenCV。
這里就之前我用過(guò)的做一下對(duì)比。
| web api | Windows SDK | Android SDK | iOS SDK | 離線使用 | 價(jià)格 | 速度 | |
| 微軟認(rèn)知服務(wù) | ?? | ? | ? | ? | ? | 收費(fèi) | 取決于網(wǎng)速 |
| 曠視Face++ | ?? | ? | ?? | ?? | ?? | 收費(fèi) |
web版取決于網(wǎng)速 本地SDK離線版識(shí)別速度沒(méi)測(cè)試過(guò),但應(yīng)該很快 |
| OpenCV | ? | ?? | ?? | ?? | ?? | 免費(fèi) | 有點(diǎn)慢 |
而今天介紹的這個(gè)虹軟人臉識(shí)別服務(wù),是免費(fèi)的、免費(fèi)的、免費(fèi)的。
最重要的是它還支持離線識(shí)別,并且提供Android、iOS、C++、C#版SDK,現(xiàn)在已經(jīng)升級(jí)到全新的3.0版本,支持活體識(shí)別。
| web api? | Windows SDK? | Android SDK? | iOS SDK? | 離線使用? | 價(jià)格? | 速度? | |
| 虹軟人臉識(shí)別 | ??? | ??? | ??? | ?? | ?? |
免費(fèi)版 - 需要在線激活 收費(fèi)版 - 離線激活,提供更多高級(jí)服務(wù)? |
web版取決于網(wǎng)速 本地SDK離線版識(shí)別速度極快 |
圖片來(lái)自官網(wǎng)



2. 下載虹軟SDK開(kāi)發(fā)包
你可以去https://ai.arcsoft.com.cn/ucenter/resource/build/index.html#/index注冊(cè)一個(gè)賬號(hào),然后就可以申請(qǐng)使用虹軟離線SDK。
這里主要講一下Windows下的SDK使用。
注意Win下面分為x86和x64兩個(gè)版本,所以在編譯App的時(shí)候不要選擇Any CPU,而是選擇和你下載的一樣的架構(gòu)。

新建一個(gè)Winform解決方案,選擇編譯架構(gòu),把你下載的SDK/lib里面的文件放進(jìn)對(duì)應(yīng)的Debug目錄。


3. 初始化識(shí)別引擎
SDK需要一個(gè)ID和KEY,這些你都可以在虹軟開(kāi)發(fā)者中心申請(qǐng)到。
private void InitEngines()
{//在線激活引擎 如出現(xiàn)錯(cuò)誤,1.請(qǐng)先確認(rèn)從官網(wǎng)下載的sdk庫(kù)已放到對(duì)應(yīng)的bin中,2.當(dāng)前選擇的CPU為x86或者x64
int retCode = 0;
try
{
retCode = ASFFunctions.ASFActivation(appId, sdkKey);
}
catch (Exception ex)
{
//禁用相關(guān)功能按鈕
//ControlsEnable(false, chooseMultiImgBtn, matchBtn, btnClearFaceList, chooseImgBtn);
if (ex.Message.Contains("無(wú)法加載 DLL"))
{
MessageBox.Show("請(qǐng)將sdk相關(guān)DLL放入bin對(duì)應(yīng)的x86或x64下的文件夾中!");
}
else
{
MessageBox.Show("激活引擎失敗!");
}
return;
}
Console.WriteLine("Activate Result:" + retCode);
//初始化引擎
uint detectMode = DetectionMode.ASF_DETECT_MODE_IMAGE;//Image模式下檢測(cè)臉部的角度優(yōu)先值
int imageDetectFaceOrientPriority = ASF_OrientPriority.ASF_OP_0_ONLY;
//人臉在圖片中所占比例,如果需要調(diào)整檢測(cè)人臉尺寸請(qǐng)修改此值,有效數(shù)值為2-32
int detectFaceScaleVal = 16;
//最大需要檢測(cè)的人臉個(gè)數(shù)
int detectFaceMaxNum = 5;
//引擎初始化時(shí)需要初始化的檢測(cè)功能組合
int combinedMask = FaceEngineMask.ASF_FACE_DETECT | FaceEngineMask.ASF_FACERECOGNITION | FaceEngineMask.ASF_AGE | FaceEngineMask.ASF_GENDER | FaceEngineMask.ASF_FACE3DANGLE;
//初始化引擎,正常值為0,其他返回值請(qǐng)參考http://ai.arcsoft.com.cn/bbs/forum.php?mod=viewthread&tid=19&_dsign=dbad527e
retCode = ASFFunctions.ASFInitEngine(detectMode, imageDetectFaceOrientPriority, detectFaceScaleVal, detectFaceMaxNum, combinedMask, ref pImageEngine);
Console.WriteLine("InitEngine Result:" + retCode);
AppendText((retCode == 0) ? "引擎初始化成功!\r\n" : string.Format("引擎初始化失敗!錯(cuò)誤碼為:{0}\r\n", retCode));
}
4. 注冊(cè)人臉
要想識(shí)別人臉,首相要像指紋識(shí)別那樣,把一個(gè)人的人臉事先錄入進(jìn)去,才可以實(shí)現(xiàn)識(shí)別。
我這里做一個(gè)簡(jiǎn)單的demo,輸入一個(gè)名字,選擇照片即可注冊(cè)。

private void btnSelectImageToRegister_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "Select";
openFileDialog.Filter = "Image File|*.bmp;*.jpg;*.jpeg;*.png";
//openFileDialog.Multiselect = true;
openFileDialog.FileName = string.Empty;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
var numStart = imagePathList.Count;
string fileName = openFileDialog.FileName;
if (!checkImage(fileName))
return;
pictureBoxSelected.ImageLocation = fileName;
currentLeftFeature = IntPtr.Zero;
//人臉檢測(cè)以及提取人臉特征
ThreadPool.QueueUserWorkItem(new WaitCallback(delegate
{
Image image = ImageUtil.readFromFile(fileName);
if (image == null)
{
return;
}
if (image.Width > 1536 || image.Height > 1536)
{
image = ImageUtil.ScaleImage(image, 1536, 1536);
}
if (image == null)
{
return;
}
if (image.Width % 4 != 0)
{
image = ImageUtil.ScaleImage(image, image.Width - (image.Width % 4), image.Height);
}
//人臉檢測(cè)
ASF_MultiFaceInfo multiFaceInfo = FaceUtil.DetectFace(pImageEngine, image);
//判斷檢測(cè)結(jié)果
if (multiFaceInfo.faceNum > 0)
{
MRECT rect = MemoryUtil.PtrToStructure<MRECT>(multiFaceInfo.faceRects);
image = ImageUtil.CutImage(image, rect.left, rect.top, rect.right, rect.bottom);
}
else
{
if (image != null)
{
image.Dispose();
}
return;
}
//提取人臉特征
ASF_SingleFaceInfo singleFaceInfo = new ASF_SingleFaceInfo();
Image image1 = ImageUtil.readFromFile(fileName);
if (image1 == null)
{
return;
}
currentLeftFeature = FaceUtil.ExtractFeature(pImageEngine, image1, out singleFaceInfo);
this.Invoke(new Action(delegate
{
if (singleFaceInfo.faceRect.left == 0 && singleFaceInfo.faceRect.right == 0)
{
AppendText(string.Format("No face detected\r\r\n"));
}
else
{
AppendText(string.Format("Face landmark detected,[left:{0},right:{1},top:{2},bottom:{3},orient:{4}]\r\r\n", singleFaceInfo.faceRect.left, singleFaceInfo.faceRect.right, singleFaceInfo.faceRect.top, singleFaceInfo.faceRect.bottom, singleFaceInfo.faceOrient));
imagesFeatureList.Add(currentLeftFeature);
}
}));
if (image1 != null)
{
image1.Dispose();
}
}));
}
}
private void btnRegisterFace_Click(object sender, EventArgs e)
{
if(string.IsNullOrEmpty(textBoxName.Text))
{
MessageBox.Show("Set a name for current person");
return;
}
imagesFeatureDictionary.Add(currentLeftFeature, textBoxName.Text);
AppendText(string.Format(textBoxName.Text + " register success!\r\r\n"));
}
5. 人臉識(shí)別
當(dāng)把許多人臉?shù)浫氲较到y(tǒng)中后,我們就可以選擇一個(gè)需要比對(duì)的圖片,進(jìn)行識(shí)別了。
private void btnSelectImageToRecognize_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "Select";
openFileDialog.Filter = "Image File|*.bmp;*.jpg;*.jpeg;*.png";
//openFileDialog.Multiselect = true;
openFileDialog.FileName = string.Empty;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
var numStart = imagePathList.Count;
string fileName = openFileDialog.FileName;
if (!checkImage(fileName))
return;
image1Feature = IntPtr.Zero;
pictureBoxToRecognize.ImageLocation = fileName;
Image srcImage = ImageUtil.readFromFile(fileName);
ASF_SingleFaceInfo singleFaceInfo = new ASF_SingleFaceInfo();
//提取人臉特征
image1Feature = FaceUtil.ExtractFeature(pImageEngine, srcImage, out singleFaceInfo);
if (imagesFeatureList.Count == 0)
{
MessageBox.Show("請(qǐng)注冊(cè)人臉!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (image1Feature == IntPtr.Zero)
{
if (pictureBoxToRecognize.Image == null)
{
MessageBox.Show("請(qǐng)選擇識(shí)別圖!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show("比對(duì)失敗,識(shí)別圖未提取到特征值!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return;
}
for (int i = 0; i < imagesFeatureDictionary.Count; i++)
{
IntPtr feature = imagesFeatureDictionary.ElementAt(i).Key;
float similarity = 0f;
int ret = ASFFunctions.ASFFaceFeatureCompare(pImageEngine, image1Feature, feature, ref similarity);
//增加異常值處理
if (similarity.ToString().IndexOf("E") > -1)
similarity = 0f;
if(similarity > threshold)
{
string name = imagesFeatureDictionary.ElementAt(i).Value;
AppendText("對(duì)比結(jié)果:" + name + " 可信度:" + similarity + "\r\n");
return;
}
}
AppendText("無(wú)結(jié)果\r\n");
}
}
6. 運(yùn)行效果
本地離線識(shí)別最大的好處就是沒(méi)有延遲,識(shí)別結(jié)果立馬呈現(xiàn)。

7. 總結(jié)
本文只是簡(jiǎn)單介紹了如何使用虹軟的離線SDK,進(jìn)行人臉識(shí)別的方法,并且是圖片的方式。
源碼下載地址:https://github.com/hupo376787/ArcFaceDemo.git
如果需要攝像頭,那么需要?jiǎng)e的攝像頭SDK來(lái)輔助實(shí)現(xiàn)。
如果以后有時(shí)間我會(huì)加上。
到此這篇關(guān)于C#版免費(fèi)離線人臉識(shí)別——虹軟ArcSoft V3.0的文章就介紹到這了,更多相關(guān)C#離線人臉識(shí)別虹軟內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# 9.0 新特性之模式匹配簡(jiǎn)化的實(shí)現(xiàn)
這篇文章主要介紹了C# 9.0 新特性之模式匹配簡(jiǎn)化的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
基于WPF實(shí)現(xiàn)路徑圖標(biāo)控件
這篇文章主要介紹了如何利用WPF實(shí)現(xiàn)路徑圖標(biāo)控件,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,需要的小伙伴可以參考一下2023-07-07
C#傳值方式實(shí)現(xiàn)不同程序窗體間通信實(shí)例
Form2構(gòu)造函數(shù)中接收一個(gè)string類(lèi)型參數(shù),即Form1中選中行的文本,將Form2的TextBox控件的Text設(shè)置為該string,即完成了Form1向Form2的傳值2013-12-12
C#控制Excel Sheet使其自適應(yīng)頁(yè)寬與列寬的方法
這篇文章主要介紹了C#控制Excel Sheet使其自適應(yīng)頁(yè)寬與列寬的方法,涉及C#操作Excel的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
基于C# wpf 實(shí)現(xiàn)Grid內(nèi)控件拖動(dòng)詳情
這篇文章主要介紹了基于C# wpf 實(shí)現(xiàn)Grid內(nèi)控件拖動(dòng),有一些業(yè)務(wù)場(chǎng)景中我們需要拖動(dòng)控件,在Grid中就可以實(shí)現(xiàn)控件拖動(dòng),通過(guò)設(shè)置Margin屬性即可,下面文章我們來(lái)看看具體的實(shí)現(xiàn)內(nèi)容2021-11-11
WPF+ASP.NET SignalR實(shí)現(xiàn)動(dòng)態(tài)折線圖的繪制
這篇文章將以一個(gè)簡(jiǎn)單的動(dòng)態(tài)折線圖示例,簡(jiǎn)述如何通過(guò)ASP.NET SignalR實(shí)現(xiàn)后臺(tái)通知功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-01-01

