最近有個小區用到了虹軟的人臉識別,效果還不錯。又有一個項目要用人證訪客對比,分享一下項目,希望可以幫到有需要的。
碼字前先上項目地址:https://gitee.com/panmingzhi/IdCardFaceIdentifier
首先是讀證的問題,我們使用的是華視CVR100U,公司已經用這個型號6年了,以前一卡通的資料都用它錄,除了不好看,質量杠杠的。大部人的身份證都是很多年前辦理的,所有比對的相似度不要太高。
視頻采集還是使用的Aforge,使用 NewFrame 一方面要顯示到實時畫面,另一方面要異步的與當前讀到的證件進行比對。這里請不嘗試在NewFrame回調事件中直接顯示到pictureBox,請使用如下方式,百試不爽:
private void VideoCaptureDevice_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
{
Bitmap newFrame = (Bitmap)eventArgs.Frame.Clone();
//如果使用視頻文件請注釋下面的這行代碼,表示不對圖像進行水平翻轉
newFrame.RotateFlip(RotateFlipType.Rotate180FlipY);
lock (sync)
{
if (currentFrame != null)
{
currentFrame.Dispose();
currentFrame = null;
}
currentFrame = newFrame;
skinPictureBox2.Invalidate();
}
}
/**
* 繪制當前幀到控制,必須與獲取當前幀互斥
*/
private void skinPictureBox2_Paint(object sender, PaintEventArgs e)
{
lock (synCurrentMat)
{
Bitmap bitmap = GetCurrentFrame();
if (bitmap != null)
{
e.Graphics.DrawImage(bitmap, new Rectangle(0, 0, skinPictureBox2.Width, skinPictureBox2.Height), new Rectangle(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
}
}
}
//異步的人證對比時,使用此方法再獲取實時截圖
public Bitmap GetCurrentFrame()
{
if (captureToken.Token.IsCancellationRequested) return null;
lock (sync)
{
return (currentFrame == null || currentFrame.Width == 0 || currentFrame.Height == 0) ? null : AForge.Imaging.Image.Clone(currentFrame);
}
}
二代證身份證的讀取頻率是每1秒讀一次,讀到證件后,10秒內為人證比對時間,這10秒將不再讀證。10秒內比對成功,顯示成功提示2-3秒后,重新再開始讀證。
在讀證時,如果證件被正確讀取到后,要重新拿開再放置證件,這個在華視的產品說明書與SDK都有說明,其它廠家如鼎識也是一樣的。
華視閱讀器讀到的身份證圖片與證件信息默認保存在SDK目錄下的wz.txt與wz.bmp,使用wz.bmp做比對時,經常報內存出錯,后面我將bmp先轉成jpg保存一次后再做人證比對,似乎就沒問題。
證件比對時還是延續了之前方式,先將證件圖片解析成FaceModel,然后將當前視頻截圖連續與些FaceModel比對,每次讀到證件時都更新一次FaceModel。
/**
* 繪制當前幀到控制,必須與獲取當前幀互斥
*/
private void skinPictureBox2_Paint(object sender, PaintEventArgs e)
{
lock (synCurrentMat)
{
Bitmap bitmap = GetCurrentFrame();
if (bitmap != null)
{
e.Graphics.DrawImage(bitmap, new Rectangle(0, 0, skinPictureBox2.Width, skinPictureBox2.Height), new Rectangle(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
}
}
}
//異步的人證對比時,使用此方法再獲取實時截圖
public Bitmap GetCurrentFrame()
{
if (captureToken.Token.IsCancellationRequested) return null;
lock (sync)
{
return (currentFrame == null || currentFrame.Width == 0 || currentFrame.Height == 0) ? null : AForge.Imaging.Image.Clone(currentFrame);
}
}
這時截圖:(本人不上像,鬼畫桃胡將就一下)
1、提示放證

2、讀到證件后立即比對

3、比對顯示結果后將重新回到第一步

如果有問題的話歡迎多多交流
