C# USB视频人脸检测

2021-06-03 04:03

阅读:755

标签:包导入   focus   gray   src   partial   rgs   oat   ssi   type   

此程序基于 虹软人脸识别进行的开发
SDK下载地址:https://ai.arcsoft.com.cn/ucenter/user/reg?utm_source=csdn1&utm_medium=referral

**前提条件**
从虹软官网下载获取ArcFace引擎应用开发包,及其对应的激活码(App_id, SDK_key)

将获取到的开发包导入到您的应用中

App_id与SDK_key是在初始化的时候需要使用
**基本类型**

所有基本类型在平台库中有定义。 定义规则是在ANSIC 中的基本类型

前加上字母“M”同时将类型的第一个字母改成大写。例如“long” 被定义成“MLong
**”数据结构与枚举**

AFR_FSDK_FACEINPUT
描述: 脸部信息
定义

typedef struct{
MRECT	rcFace;
AFR_FSDK_OrientCode lOrient;
} AFR_FSDK_FACEINPUT, *LPAFR_FSDK_FACEINPUT;

  


成员描述


rcFace脸部矩形框信息
lOrient脸部旋转角度

AFR_FSDK_FACEMODEL
描述: 脸部特征信息

定义
typedef struct{
MByte	*pbFeature;
MInt32	lFeatureSize;
} AFR_FSDK_FACEMODEL, *LPAFR_FSDK_FACEMODEL;

  


成员描述

pbFeature提取到的脸部特征

lFeatureSize特征信息长度

AFR_FSDK_VERSION

描述: 引擎版本信息

定义
typedef struct{
MInt32	lCodebase;
MInt32	lMajor;
MInt32	lMinor;
MInt32	lBuild;
MInt32 lFeatureLevel;
MPChar	Version;
MPChar	BuildDate;
MPChar CopyRight;
} AFR_FSDK_VERSION, *LPAFR_FSDK_VERSION;

  

成员描述
lCodebase代码库版本号
lMajor主版本号
lMinor次版本号
lBuild编译版本号,递增
lFeatureLevel特征库版本号
Version字符串形式的版本号
BuildDate编译时间
CopyRight版权
```
枚举
AFR_FSDK_ORIENTCODE
描述: 基于逆时针的脸部方向枚举值

  

定义

};

成员描述
AFR_FSDK_FOC_00 度
AFR_FSDK_FOC_9090度
AFR_FSDK_FOC_270270度
AFR_FSDK_FOC_180180度
AFR_FSDK_FOC_3030度
AFR_FSDK_FOC_6060度
AFR_FSDK_FOC_120120度
AFR_FSDK_FOC_150150度
AFR_FSDK_FOC_210210度
AFR_FSDK_FOC_240240度
AFR_FSDK_FOC_300300度
AFR_FSDK_FOC_330330度

  

`
支持的颜色格式
描述: 颜色格式及其对齐规则
定义
ASVL_PAF_I420 8-bit Y层,之后是8-bit的2x2 采样的U层和V层
ASVL_PAF_YUYV Y0, U0, Y1, V0
ASVL_PAF_RGB24_B8G8R8 BGR24, B8G8R8

API ReferenceAFR_FSDK_InitialEngine
描述: 初始化引擎参数

原型
MRESULT AFR_FSDK_InitialEngine(
MPChar	AppId,
MPChar	SDKKey,
Mbyte	*pMem,
MInt32	lMemSize,
MHandle	*phEngine
);

  


参数

AppId[in] 用户申请SDK时获取的App Id
SDKKey[in] 用户申请SDK时获取的SDK Key
pMem[in] 分配给引擎使用的内存地址
lMemSize[in] 分配给引擎使用的内存大小
phEngine[out] 引擎handle

  


返回值: 成功返回MOK,否则返回失败code。失败codes如下所列:
MERR_INVALID_PARAM 参数输入非法
MERR_NO_MEMORY 内存不足
AFR_FSDK_ExtractFRFeature
描述: 获取脸部特征参数

原型
MRESULT AFR_FSDK_ExtractFRFeature (
MHandle	hEngine,
LPASVLOFFSCREEN	pInputImage,
LPAFR_FSDK_FACEINPUT	pFaceRes,
LPAFR_FSDK_FACEMODEL	pFaceModels
);

  


参数

hEngine[in] 引擎handle
pInputImage[in] 输入的图像数据
pFaceRes[in] 已检测到的脸部信息
pFaceModels[out] 提取的脸部特征信息

  


返回值: 成功返回MOK,否则返回失败code。失败codes如下所列:
MERR_INVALID_PARAM 参数输入非法
MERR_NO_MEMORY 内存不足
AFR_FSDK_FacePairMatching
描述: 脸部特征比较

原型

MRESULT AFR_FSDK_FacePairMatching(
MHandle	hEngine,
AFR_FSDK_FACEMODEL	*reffeature,
AFR_FSDK_FACEMODEL	*probefeature,
MFloat *pfSimilScore
);

  


参数

hEngine[in] 引擎handle
reffeature[in] 已有脸部特征信息
probefeature[in] 被比较的脸部特征信息
pfSimilScore[out] 脸部特征相似程度数值

  


返回值: 成功返回MOK,否则返回失败code。失败codes如下所列:
MERR_INVALID_PARAM 参数输入非法
MERR_NO_MEMORY 内存不足
AFR_FSDK_UninitialEngine
描述: 销毁引擎,释放相应资源

原型
MRESULT AFR_FSDK_UninitialEngine(
MHandle hEngine
);

  


参数
hEngine[in] 引擎handle
返回值: 成功返回MOK,否则返回失败code。失败codes如下所列:
MERR_INVALID_PARAM 参数输入非法
AFR_FSDK_GetVersion

原型
const AFR_FSDK_VERSION * AFR_FSDK_GetVersion(
MHandle hEngine
);

  


相关事例代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ArcsoftFace
{


public struct AFD_FSDK_FACERES
{
public int nFace; // number of faces detected


public IntPtr rcFace; // The bounding box of face


public IntPtr lfaceOrient; // the angle of each face
}


}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ArcsoftFace
{
public struct AFR_FSDK_FACEINPUT
{
public MRECT rcFace;	// The bounding box of face


public int lfaceOrient; // The orientation of face
}
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ArcsoftFace
{
public struct AFR_FSDK_FACEMODEL
{
public IntPtr pbFeature;	// The extracted features


public int lFeatureSize;	// The size of pbFeature
}
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ArcsoftFace
{
public struct AFR_FSDK_Version
{
public int lCodebase;
public int lMajor;
public int lMinor;
public int lBuild;
public int lFeatureLevel;
public IntPtr Version;
public IntPtr BuildDate;
public IntPtr CopyRight;
}
}

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;


namespace ArcsoftFace
{


public class AmFaceVerify
{
/**
* 初始化人脸检测引擎
* @return 初始化人脸检测引擎
*/
[DllImport("libarcsoft_fsdk_face_detection.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int AFD_FSDK_InitialFaceEngine(string appId, string sdkKey, IntPtr pMem, int lMemSize, ref IntPtr pEngine, int iOrientPriority, int nScale, int nMaxFaceNum);


/**
* 获取人脸检测 SDK 版本信息
* @return 获取人脸检测SDK 版本信息
*/
[DllImport("libarcsoft_fsdk_face_detection.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr AFD_FSDK_GetVersion(IntPtr pEngine);


/**
* 根据输入的图像检测出人脸位置,一般用于静态图像检测
* @return 人脸位置
*/
[DllImport("libarcsoft_fsdk_face_detection.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int AFD_FSDK_StillImageFaceDetection(IntPtr pEngine, IntPtr offline, ref IntPtr faceRes);

 


/**
* 初始化人脸识别引擎
* @return 初始化人脸识别引擎
*/
[DllImport("libarcsoft_fsdk_face_recognition.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int AFR_FSDK_InitialEngine(string appId, string sdkKey, IntPtr pMem, int lMemSize, ref IntPtr pEngine);


/**
* 获取人脸识别SDK 版本信息
* @return 获取人脸识别SDK 版本信息
*/
[DllImport("libarcsoft_fsdk_face_recognition.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr AFR_FSDK_GetVersion(IntPtr pEngine);


/**
* 提取人脸特征
* @return 提取人脸特征
*/
[DllImport("libarcsoft_fsdk_face_recognition.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int AFR_FSDK_ExtractFRFeature(IntPtr pEngine, IntPtr offline, IntPtr faceResult, IntPtr localFaceModels);


/**
* 获取相似度
* @return 获取相似度
*/
[DllImport("libarcsoft_fsdk_face_recognition.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int AFR_FSDK_FacePairMatching(IntPtr pEngine, IntPtr faceModels1, IntPtr faceModels2, ref float fSimilScore);


#region delete
///**
// * 创建人脸检测引擎
// * @param [in] model_path 模型文件夹路径
// * @param [out] engine 创建的人脸检测引擎
// * @return =0 表示成功,=0 表示实际检测到的人脸数量, gray = null;
Image smallframe = null;
Mat frame = new Mat();
private int sb = 0;
Rectangle f = new Rectangle();
public Form2()
{
InitializeComponent();


capture_tick = new System.Timers.Timer();
capture_tick.Interval = 50;
capture_tick.Enabled = Enabled;
capture_tick.Stop();
capture_tick.Elapsed += new ElapsedEventHandler(processfram);
}


private byte[] getBGR(Bitmap image, ref int width, ref int height, ref int pitch)
{
//Bitmap image = new Bitmap(imgPath);


const PixelFormat PixelFormat = PixelFormat.Format24bppRgb;


BitmapData data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat);


IntPtr ptr = data.Scan0;


int ptr_len = data.Height * Math.Abs(data.Stride);


byte[] ptr_bgr = new byte[ptr_len];


Marshal.Copy(ptr, ptr_bgr, 0, ptr_len);


width = data.Width;


height = data.Height;


pitch = Math.Abs(data.Stride);


int line = width * 3;


int bgr_len = line * height;


byte[] bgr = new byte[bgr_len];


for (int i = 0; i  image = frame.ToImage();
Image currentFrame = image;

 

 


if (sb == 0)
{
sb += 1;
MRECT rect = detectAndExtractFeature(image.ToBitmap(), 1);


if (Math.Abs(rect.left - f.Left) > 30 || Math.Abs(rect.top - f.Top) > 30)
{
f = new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
}

 


}
else if (sb >= 4)
{
sb = 0;
}
else
{
sb += 1;
}


// currentFrame.Draw(rect, new Bgr(Color.Red));
// image.Draw(detectAndExtractFeature(image.ToBitmap(),1),new Bgr(Color.Red),3);


currentFrame.Draw(f, new Bgr(Color.Red), 3);

 

 


imageBox1.Image = currentFrame.ToBitmap();
PointF pf = new PointF(50, 50);
using (Graphics g = imageBox1.CreateGraphics())
{
Font font = new Font("Arial", 12);
g.DrawString("left:" + f.Left + " top:" + f.Top, font, Brushes.Green, pf);


}
currentFrame.Dispose();
image.Dispose(); ;
}
capture_tick.Enabled = true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
capture_tick.Enabled = true;
}
}


public static void BoundingBox(Image src, Image draw)
{
using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint())
{
CvInvoke.FindContours(src, contours, null, RetrType.External,
ChainApproxMethod.ChainApproxSimple);


int count = contours.Size;
for (int i = 0; i (); //Convert it to Grayscale 
gray._EqualizeHist();//均衡化


CascadeClassifier ccr = new CascadeClassifier(haarXmlPath);
Rectangle[] rects = ccr.DetectMultiScale(gray, 1.3, 3, new Size(20, 20), Size.Empty);
foreach (Rectangle r in rects)
{
//This will focus in on the face from the haar results its not perfect but it will remove a majoriy 
//of the background noise 
Rectangle facesDetected = r;
facesDetected.X += (int)(facesDetected.Height * 0.6);
facesDetected.Y += (int)(facesDetected.Width * 0.8);
facesDetected.Height += (int)(facesDetected.Height * 0.1);
facesDetected.Width += (int)(facesDetected.Width * 0.2);


// frame.Draw(facesDetected, new Bgr(Color.Red), 3);//绘制检测框 
}

 


// imageBox_capture.Image = frame;
}
}

 


private MRECT detectAndExtractFeature(Image imageParam, int firstSecondFlg)
{


byte[] feature = null;
MRECT rect = new MRECT();
Bitmap bitmap = new Bitmap(imageParam);
byte[] imageData = null;
IntPtr imageDataPtr = IntPtr.Zero;
ASVLOFFSCREEN offInput = new ASVLOFFSCREEN();
AFD_FSDK_FACERES faceRes = new AFD_FSDK_FACERES();


IntPtr faceResPtr = IntPtr.Zero;
try
{


int width = 0;


int height = 0;


int pitch = 0;

 


imageData = getBGR(bitmap, ref width, ref height, ref pitch);


//GCHandle hObject = GCHandle.Alloc(imageData, GCHandleType.Pinned);


//IntPtr imageDataPtr = hObject.AddrOfPinnedObject();


imageDataPtr = Marshal.AllocHGlobal(imageData.Length);


Marshal.Copy(imageData, 0, imageDataPtr, imageData.Length);

 

 


offInput.u32PixelArrayFormat = 513;


offInput.ppu8Plane = new IntPtr[4];


offInput.ppu8Plane[0] = imageDataPtr;


offInput.i32Width = width;


offInput.i32Height = height;


offInput.pi32Pitch = new int[4];


offInput.pi32Pitch[0] = pitch;

 


IntPtr offInputPtr = Marshal.AllocHGlobal(Marshal.SizeOf(offInput));


Marshal.StructureToPtr(offInput, offInputPtr, false);


faceResPtr = Marshal.AllocHGlobal(Marshal.SizeOf(faceRes));


//Marshal.StructureToPtr(faceRes, faceResPtr, false);

 

 


//人脸检测
int detectResult = AmFaceVerify.AFD_FSDK_StillImageFaceDetection(detectEngine, offInputPtr, ref faceResPtr);

 

 


object obj = Marshal.PtrToStructure(faceResPtr, typeof(AFD_FSDK_FACERES));


faceRes = (AFD_FSDK_FACERES)obj;

 


for (int i = 0; i = w || StartY >= h)
{
return null;
}
if (StartX + iWidth > w)
{
iWidth = w - StartX;
}
if (StartY + iHeight > h)
{
iHeight = h - StartY;
}
try
{
Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);


Graphics g = Graphics.FromImage(bmpOut);


g.DrawImage(srcImage, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);


g.Dispose();


return bmpOut;
}
catch
{
return null;
}
}


private void Form2_Load(object sender, System.EventArgs e)
{
#region 初始化人脸检测引擎


int detectSize = 40 * 1024 * 1024;


IntPtr pMem = Marshal.AllocHGlobal(detectSize);


//1-1
//string appId = "4tnYSJ68e8wztSo4Cf7WvbyMZduHwpqtThAEM3obMWbE";


//1-1
//string sdkKey = "Cgbaq34izc8PA2Px26x8qqWTQn2P5vxijaWKdUrdCwYT";


//1-n
string appId = "8b4R2gvcoFQXKbC4wGtnYcqsa9Bd3FLiN3VWDFtJqcnB";


//1-n
string sdkKey = "A5Km3QjZKGuakWRmC2pSWTuNzbNbaSCnj5fFtjBBcdxm";


//人脸检测引擎初始化


// IntPtr aaa= AFD_FSDKLibrary.AFD_FSDK_InitialFaceEngine(appId, sdkKey, pMem, detectSize, ref detectEngine, 5, 50, 1);
int retCode = AmFaceVerify.AFD_FSDK_InitialFaceEngine(appId, sdkKey, pMem, detectSize, ref detectEngine, 5, 50, 1);
//获取人脸检测引擎版本
IntPtr versionPtr = AmFaceVerify.AFD_FSDK_GetVersion(detectEngine);


AFR_FSDK_Version version = (AFR_FSDK_Version)Marshal.PtrToStructure(versionPtr, typeof(AFR_FSDK_Version));


Console.WriteLine("lCodebase:{0} lMajor:{1} lMinor:{2} lBuild:{3} Version:{4} BuildDate:{5} CopyRight:{6}", version.lCodebase, version.lMajor, version.lMinor, version.lBuild, Marshal.PtrToStringAnsi(version.Version), Marshal.PtrToStringAnsi(version.BuildDate), Marshal.PtrToStringAnsi(version.CopyRight));


//Marshal.FreeHGlobal(versionPtr);


#endregion


#region 初始化人脸识别引擎


int recognizeSize = 40 * 1024 * 1024;


IntPtr pMemDetect = Marshal.AllocHGlobal(recognizeSize);


//1-1
//string appIdDetect = "4tnYSJ68e8wztSo4Cf7WvbyMZduHwpqtThAEM3obMWbE";


//1-1
//string sdkKeyDetect = "Cgbaq34izc8PA2Px26x8qqWaaBHbPD7wWMcTU6xe8VRo";


//1-n
string appIdDetect = "8b4R2gvcoFQXKbC4wGtnYcqsa9Bd3FLiN3VWDFtJqcnB";


//1-n
string sdkKeyDetect = "A5Km3QjZKGuakWRmC2pSWTuW9zdndn5EkVDo4LceRxLU";


//人脸识别引擎初始化
retCode = AmFaceVerify.AFR_FSDK_InitialEngine(appIdDetect, sdkKeyDetect, pMemDetect, recognizeSize, ref regcognizeEngine);


//获取人脸识别引擎版本
IntPtr versionPtrDetect = AmFaceVerify.AFR_FSDK_GetVersion(regcognizeEngine);


AFR_FSDK_Version versionDetect = (AFR_FSDK_Version)Marshal.PtrToStructure(versionPtrDetect, typeof(AFR_FSDK_Version));


Console.WriteLine("lCodebase:{0} lMajor:{1} lMinor:{2} lBuild:{3} lFeatureLevel:{4} Version:{5} BuildDate:{6} CopyRight:{7}", versionDetect.lCodebase, versionDetect.lMajor, versionDetect.lMinor, versionDetect.lBuild, versionDetect.lFeatureLevel, Marshal.PtrToStringAnsi(versionDetect.Version), Marshal.PtrToStringAnsi(versionDetect.BuildDate), Marshal.PtrToStringAnsi(versionDetect.CopyRight));


#endregion
}



}
}

  




USB视频 动态画框 源码下载地址
https://download.csdn.net/download/zhang1244/10368237
运行效果地址
https://download.csdn.net/download/zhang1244/10368222
普通人脸照片进行关键点提取以及相关对比相似度

https://download.csdn.net/download/zhang1244/10368197

运行效果地址

https://download.csdn.net/download/zhang1244/10368181

相关技术交流,后期可能开发相关与身份证照片进行实名制对比。请继续关注
```

C# USB视频人脸检测

标签:包导入   focus   gray   src   partial   rgs   oat   ssi   type   

原文地址:https://www.cnblogs.com/Zzz-/p/10912536.html


评论


亲,登录后才可以留言!