/// <summary> /// 人脸特征比对 /// </summary> /// <param name="faceFeature1">特征1</param> /// <param name="faceFeature2">特征2</param> /// <param name="similarity">相似度</param> /// <param name="compareModel">ASF_LIFE_PHOTO:用于生活照之间的特征比对;ASF_ID_PHOTO:用于证件照或证件照和生活照之间的特征比对</param> /// <returns>返回0表示正常;返回负数请根据ErrorCodeUtil类注释查看;其他值请在官网-帮助中心查询</returns> public int ASFFaceFeatureCompare(FaceFeature faceFeature1, FaceFeature faceFeature2, out float similarity, ASF_CompareModel compareModel = ASF_CompareModel.ASF_LIFE_PHOTO) { similarity = 0f; if (faceFeature1 == null || faceFeature2 == null) { return(ErrorCodeUtil.FEATURE_IS_NULL); } #region 将特征对象转化为特征结构体,再转化为非托管内存 ASF_FaceFeature asfFeatureStruct1 = new ASF_FaceFeature(); asfFeatureStruct1.featureSize = faceFeature1.featureSize; asfFeatureStruct1.feature = MemoryUtil.Malloc(asfFeatureStruct1.featureSize); MemoryUtil.Copy(faceFeature1.feature, 0, asfFeatureStruct1.feature, asfFeatureStruct1.featureSize); IntPtr pFeature1 = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_FaceFeature>()); MemoryUtil.StructureToPtr(asfFeatureStruct1, pFeature1); ASF_FaceFeature asfFeatureStruct2 = new ASF_FaceFeature(); asfFeatureStruct2.featureSize = faceFeature2.featureSize; asfFeatureStruct2.feature = MemoryUtil.Malloc(asfFeatureStruct2.featureSize); MemoryUtil.Copy(faceFeature2.feature, 0, asfFeatureStruct2.feature, asfFeatureStruct2.featureSize); IntPtr pFeature2 = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_FaceFeature>()); MemoryUtil.StructureToPtr(asfFeatureStruct2, pFeature2); #endregion //调用SDK接口 int retCode = ASFFunctions.ASFFaceFeatureCompare(pEngine, pFeature1, pFeature2, ref similarity, compareModel); MemoryUtil.FreeArray(pFeature1, pFeature2, asfFeatureStruct1.feature, asfFeatureStruct2.feature); return(retCode); }
/// <summary> /// 比较两个特征值的相似度,返回相似度 /// </summary> /// <param name="feature1"></param> /// <param name="feature2"></param> /// <returns></returns> private float CompareTwoFeatures(IntPtr feature1, IntPtr feature2) { float similarity = 0.0f; //调用人脸匹配方法,进行匹配 ASFFunctions.ASFFaceFeatureCompare(pVideoImageEngine, feature1, feature2, ref similarity); return(similarity); }
/// <summary> /// 匹配事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void matchBtn_Click(object sender, EventArgs e) { if (imagesFeatureList.Count == 0) { MessageBox.Show("请注册人脸!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (image1Feature == IntPtr.Zero) { if (picImageCompare.Image == null) { MessageBox.Show("请选择识别图!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show("比对失败,识别图未提取到特征值!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } return; } float compareSimilarity = 0f; int compareNum = 0; AppendText(string.Format("------------------------------开始比对,时间:{0}------------------------------\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ms"))); for (int i = 0; i < peoples.Count; i++) { foreach (KeyValuePair <string, IntPtr> ptr in peoples[i].images) { IntPtr feature = ptr.Value; //imagesFeatureList[i]; float similarity = 0f; long t1 = DateTime.Now.ToFileTime(); int ret = ASFFunctions.ASFFaceFeatureCompare(pEngine, image1Feature, feature, ref similarity); long t2 = DateTime.Now.ToFileTime(); AppendText(string.Format("与{0}比对结果:{1}\r\n", peoples[i].name, similarity)); imageList.Items[i].Text = string.Format("{0}号({1})", i, similarity); if (similarity > compareSimilarity) { compareSimilarity = similarity; compareNum = i; MessageBox.Show("欢迎你。{0}", peoples[i].name); } } } if (compareSimilarity > 0) { lblCompareInfo.Text = " " + compareNum + "号," + compareSimilarity; } AppendText(string.Format("------------------------------比对结束,时间:{0}------------------------------\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ms"))); }
/// <summary> /// 匹配事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void matchBtn_Click(object sender, EventArgs e) { if (imagesFeatureList.Count == 0) { MessageBox.Show("请注册人脸!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (image1Feature == IntPtr.Zero) { if (picImageCompare.Image == null) { MessageBox.Show("请选择识别图!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show("比对失败,识别图未提取到特征值!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } return; } //标记已经做了匹配比对,在开启视频的时候要清除比对结果 isCompare = true; float compareSimilarity = 0f; int compareNum = 0; AppendText(string.Format("------------------------------开始比对,时间:{0}------------------------------\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ms"))); for (int i = 0; i < imagesFeatureList.Count; i++) { IntPtr feature = imagesFeatureList[i]; float similarity = 0f; int ret = ASFFunctions.ASFFaceFeatureCompare(pImageEngine, image1Feature, feature, ref similarity); //增加异常值处理 if (similarity.ToString().IndexOf("E") > -1) { similarity = 0f; } AppendText(string.Format("与{0}号比对结果:{1}\r\n", i, similarity)); imageList.Items[i].Text = string.Format("{0}号({1})", i, similarity); if (similarity > compareSimilarity) { compareSimilarity = similarity; compareNum = i; } } if (compareSimilarity > 0) { lblCompareInfo.Text = " " + compareNum + "号," + compareSimilarity; } AppendText(string.Format("------------------------------比对结束,时间:{0}------------------------------\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ms"))); }
/// <summary> /// 得到feature比较结果 /// </summary> /// <param name="feature"></param> /// <returns></returns> private int compareFeature(IntPtr feature, out float similarity) { int result = -1; similarity = 0f; //如果人脸库不为空,则进行人脸匹配 if (imagesFeatureList != null && imagesFeatureList.Count > 0) { for (int i = 0; i < imagesFeatureList.Count; i++) { //调用人脸匹配方法,进行匹配 ASFFunctions.ASFFaceFeatureCompare(pVideoRGBImageEngine, feature, imagesFeatureList[i], ref similarity); if (similarity >= threshold) { result = i; break; } } } return(result); }
/// <summary> /// 得到feature比较结果 /// </summary> /// <param name="pVideoFaceFeature"></param> /// <returns></returns> private int compareFeatureFromDB(IntPtr pVideoFaceFeature, out float similarity) { int result = -1; similarity = 0f; //如果人脸库不为空,则进行人脸匹配 if (imagesFeatureList != null && imagesFeatureList.Count > 0) { for (int i = 0; i < imagesFeatureList.Count; i++) { //调用人脸匹配方法,进行人脸特征匹配 ASFFunctions.ASFFaceFeatureCompare(pVideoImageEngine, pVideoFaceFeature, imagesFeatureList[i], ref similarity); if (similarity >= threshold)// 相似度大于阈值,输出人脸序号,暂时默认阈值为0.8 { result = i; break; } } } return(result); }
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("请注册人脸!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (image1Feature == IntPtr.Zero) { if (pictureBoxToRecognize.Image == null) { MessageBox.Show("请选择识别图!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show("比对失败,识别图未提取到特征值!", "提示", 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("对比结果:" + name + " 可信度:" + similarity + "\r\n"); return; } } AppendText("无结果\r\n"); } }
/// <summary> /// 匹配 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void MatchImage() { if (ImagesFeatureList.Count == 0) { MessageBox.Show("请注册人脸!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (ImageFeature == IntPtr.Zero) { if (FRC.FIView._faceIdentificationImageBox.Source == null) { MessageBox.Show("请选择识别图!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show("比对失败,识别图未提取到特征值!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } return; } //标记已经做了匹配比对,在开启视频的时候要清除比对结果 isCompare = true; float compareSimilarity = 0f; int compareNum = -1; AppendText(string.Format("------------------------------开始比对,时间:{0}------------------------------\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ms"))); ImagesFeatureList.Clear(); foreach (var item in FRC.FRVM.PIVMList) { ImagesFeatureList.Add(item.FaceCharacteristics); } for (int i = 0; i < ImagesFeatureList.Count; i++) { IntPtr feature = ImagesFeatureList[i]; float similarity = 0f; int ret = ASFFunctions.ASFFaceFeatureCompare(pImageEngine, ImageFeature, feature, ref similarity); //增加异常值处理 if (similarity.ToString().IndexOf("E") > -1) { similarity = 0f; } AppendText(string.Format("与{0}号比对结果:{1}\r\n", i, similarity)); //选择最合适的图片 if (similarity > compareSimilarity && similarity >= threshold) { compareSimilarity = similarity; compareNum = i; } //_imageList.Items[i].Text = string.Format("{0}号({1})", i, similarity); } FRC.FIVM.PersonInfoUC.DataContext = FRC.FRVM.PIVMList.SingleOrDefault(x => x.FaceCharacteristics == ImagesFeatureList[compareNum]); if (compareSimilarity > 0) { //_lblCompareInfo.Text = " " + compareNum + "号," + compareSimilarity; } AppendText(string.Format("------------------------------比对结束,时间:{0}------------------------------\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ms"))); }
public void CompareFeature(IntPtr sourceFeature, IntPtr libFeature, ref float similarity) { ASFFunctions.ASFFaceFeatureCompare(pVideoRGBImageEngine, sourceFeature, libFeature, ref similarity); }