/// <summary> /// Xử Lý /// </summary> /// <param name="SearchFace"></param> public void xuLy(TFaceRecord SearchFace, int index) { float Threshold = 0.0f; FSDK.GetMatchingThresholdAtFAR(Form1.FARValue / 100, ref Threshold); int MatchedCount = 0; int FaceCount = Form2.FaceList.Count; float[] Similarities = new float[FaceCount]; int[] Numbers = new int[FaceCount]; for (int j = 0; j < SearchFace.Template.Count; j++) { for (int i = 0; i < Form2.FaceList.Count; i++) { float Similarity = 0.0f; TFaceRecord CurrentFace = Form2.FaceList[i]; byte[] anh1 = new byte[FSDK.TemplateSize]; byte[] anh2 = new byte[FSDK.TemplateSize]; anh1 = SearchFace.Template[j]; anh2 = Form2.FaceList[i].Template[0]; FSDK.MatchFaces(ref anh1, ref anh2, ref Similarity); if (Similarity >= Threshold) { SearchFace.Template[0] = SearchFace.Template[j]; Form2.FaceList.Add(SearchFace); imageList2.Images.Add(SearchFace.image.ToCLRImage()); items = new ListViewItem(); items.ImageIndex = demlv2++; items.Text = SearchFace.ImageFileName; items.ToolTipText = SearchFace.ImageFileName; items.Tag = SearchFace; listView2.Items.Add(items); // MessageBox.Show(index.ToString()+" "+AnhList.Count+" "+FaceListID.Count); AnhList[index].TrangThai = true;///sfsdfds return; ////Similarities[MatchedCount] = Similarity; ////Numbers[MatchedCount] = i; ////++MatchedCount; } } } }
private void button1_Click(object sender, EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "JPEG (*.jpg)|*.jpg|Windows bitmap (*.bmp)|*.bmp|All files|*.*"; dlg.Multiselect = true; if (dlg.ShowDialog() == DialogResult.OK) { listView1.Visible = true; if (thunho == false) { this.Width = this.Width + listView1.Width; thunho = true; } try { //Set các thông số độ nghiêng FSDK.SetFaceDetectionParameters(false, true, 384); ///Set thông số phát hiện khung mặt FSDK.SetFaceDetectionThreshold((int)Form1.FaceDetectionThreshold); foreach (string fn in dlg.FileNames) { TFaceRecord fr = new TFaceRecord(); fr.ImageFileName = fn; //fr.FacePosition = new FSDK.TFacePosition(); fr.FacialFeatures = new FSDK.TPoint[2]; //fr.Template = new byte[FSDK.TemplateSize]; fr.image = new FSDK.CImage(fn); //textBox1.Text += "Enrolling '" + fn + "'\r\n"; //textBox1.Refresh(); ///Lấy vị trí mặt trong ảnh //fr.FacePosition = fr.image.DetectFace(); FSDK.DetectMultipleFaces(fr.image.ImageHandle, ref fr.CountFace, out fr.FacePosition, sizeof(long) * 256); Array.Resize(ref fr.FacePosition, fr.CountFace); if (fr.FacePosition.Length == 0) { if (dlg.FileNames.Length <= 1) { MessageBox.Show("Không có khuôn mặt nào !", "Lỗi "); } else { MessageBox.Show(fn + ": Không tìm thấy khuôn mặt nào !"); } } else { //Sao chép mặt fr.faceImage = fr.image.CopyRect((int)(fr.FacePosition[0].xc - Math.Round(fr.FacePosition[0].w * 0.5)), (int)(fr.FacePosition[0].yc - Math.Round(fr.FacePosition[0].w * 0.5)), (int)(fr.FacePosition[0].xc + Math.Round(fr.FacePosition[0].w * 0.5)), (int)(fr.FacePosition[0].yc + Math.Round(fr.FacePosition[0].w * 0.5))); try { ///Lấy đặc điểm của mắt fr.FacialFeatures = fr.image.DetectEyesInRegion(ref fr.FacePosition[0]); } catch (Exception ex2) { MessageBox.Show(ex2.Message, "Lỗi không nhận diện được mắt !"); byte[] by = new byte[FSDK.TemplateSize]; by = null; fr.Template.Add(by); } try { ///Lấy giá trị nhận diện khung mặt byte[] by = new byte[FSDK.TemplateSize]; by = fr.image.GetFaceTemplateInRegion(ref fr.FacePosition[0]); // get template with higher precision fr.Template.Add(by); } catch (Exception ex2) { MessageBox.Show(ex2.Message, "Không phát hiện khuôn mặt"); } //Them vào danh sách FaceList.Add(fr); imageList1.Images.Add(fr.faceImage.ToCLRImage()); listView1.Items.Add((imageList1.Images.Count - 1).ToString(), fn.Split('\\')[fn.Split('\\').Length - 1], imageList1.Images.Count - 1); //textBox1.Text += "File '" + fn + "' enrolled\r\n"; //textBox1.Refresh(); listView1.SelectedIndices.Clear(); listView1.SelectedIndices.Add(listView1.Items.Count - 1); } } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString(), "Exception"); } } }
private void nhậnDiệnToolStripMenuItem_Click(object sender, EventArgs e) { if (AnhList.Count == 0 || Form2.FaceList == null?true:Form2.FaceList.Count == 0) { MessageBox.Show("Chưa có dữ liệu ảnh đào tạo hoặc chưa có ảnh nhận diện ", "Lỗi "); } else { demlv2 = 0; listView2.Items.Clear(); imageList2.Images.Clear(); FaceListID.Clear(); listView2.LargeImageList = imageList2; for (int i3 = 0; i3 < AnhList.Count; i3++) { try { string fn = AnhList[i3].URL; TFaceRecord fr = new TFaceRecord(); fr.ImageFileName = fn; //fr.FacePosition = new FSDK.TFacePosition(); fr.FacialFeatures = new FSDK.TPoint[FSDK.FSDK_FACIAL_FEATURE_COUNT]; //fr.Template = new byte[FSDK.TemplateSize]; try { fr.image = new FSDK.CImage(fn); } catch (Exception ex) { MessageBox.Show(ex.Message, "Lỗi tải file "); } FSDK.DetectMultipleFaces(fr.image.ImageHandle, ref fr.CountFace, out fr.FacePosition, sizeof(long) * 256); Array.Resize(ref fr.FacePosition, fr.CountFace); if (fr.CountFace != 0) { for (int i = 0; i < fr.FacePosition.Length; i++) { //fr.faceImage = fr.image.CopyRect((int)(fr.FacePosition.xc - Math.Round(fr.FacePosition.w * 0.5)), (int)(fr.FacePosition.yc - Math.Round(fr.FacePosition.w * 0.5)), (int)(fr.FacePosition.xc + Math.Round(fr.FacePosition.w * 0.5)), (int)(fr.FacePosition.yc + Math.Round(fr.FacePosition.w * 0.5))); bool eyesDetected = false; try { fr.FacialFeatures = fr.image.DetectEyesInRegion(ref fr.FacePosition[i]); eyesDetected = true; } catch (Exception ex) { MessageBox.Show(ex.Message, "Lỗi nhận diện mắt"); byte[] by = new byte[FSDK.TemplateSize]; by = null; fr.Template.Add(by); } if (eyesDetected) { byte[] by = new byte[FSDK.TemplateSize]; by = fr.image.GetFaceTemplateInRegion(ref fr.FacePosition[i]); // get template with higher precision fr.Template.Add(by); } } } FaceListID.Add(fr); } catch (Exception ex) { MessageBox.Show("không mở được ảnh : " + ex.Message.ToString(), "Lỗi "); } } ShowXuLi showXuLi = new ShowXuLi(); showXuLi.max = 100; showXuLi.min = 1; showXuLi.step = 100 / FaceListID.Count; showXuLi.Show(); for (int i1 = 0; i1 < FaceListID.Count; i1++) { xuLy(FaceListID[i1], i1); if (demlv2 > 0) { listView2.SelectedIndices.Clear(); listView2.SelectedIndices.Add(0); listView2.CheckBoxes = true; } showXuLi.getIndex(((i1 + 1) * showXuLi.step).ToString(), 0); } showXuLi.getIndex("100", 100); MessageBox.Show("Nhận diện được " + listView2.Items.Count + " ảnh có mặt bạn !", "Thông báo "); showXuLi.Close(); if (groupBox3.Visible == false) { this.Height = this.Height + groupBox3.Height; groupBox3.Visible = true; } } }