private void button5_Click(object sender, EventArgs e) { if (textBox1.Text.Trim() != "") { for (int i = 0; i < listface.Count; i++) { if (listface[i].face_name == textBox1.Text.Trim()) { currentfacenum = i; DialogResult dr = MessageBox.Show("该用户名的人脸已经注册,确认将删除该人脸!", "提示", MessageBoxButtons.OKCancel); if (dr == DialogResult.Cancel) { MessageBox.Show("取消删除人脸操作!"); textBox1.Text = ""; return; } else { facelibrary = new FaceLibrary(); facelibrary.face_name = listface[currentfacenum].face_name; facelibrary.face_Feature = listface[currentfacenum].face_Feature; listface.Remove(facelibrary); string listfacelibrary = JsonListToString(listface); FileWrite(filepath, listfacelibrary); MessageBox.Show("删除人脸操作成功!"); textBox1.Text = ""; currentfacenum = -1; } } } if (currentfacenum < 0) { currentfacenum = -1; MessageBox.Show("该用户名未注册!"); } } else { MessageBox.Show("请输入注册人脸的用户名!"); } textBox1.Text = ""; }
private void ImageGrabbedProcess(object sender, EventArgs arg) { Mat mat = new Mat(); DateTime now = DateTime.Now; capture.Retrieve(mat); // 捕获摄像头图片 bitmap = mat.Bitmap; detectandextractfeature = detectAndExtractFeature(bitmap, pictureBox0, true, false, false); if (detectandextractfeature.faceflag && addflag) { pictureBox0.Image = null; facelibrary = new FaceLibrary(); facelibrary.face_name = textBox1.Text.Trim(); detectandextractfeature = detectAndExtractFeature(bitmap, pictureBox0, false, true, true); facelibrary.face_Feature = detectandextractfeature.face_Feature; if (currentfacenum > -1) //覆盖当前人脸信息 { listface[currentfacenum] = facelibrary; } else { listface.Add(facelibrary); } addflag = false; currentfacenum = -1; string listfacestr = JsonListToString(listface); FileWrite("facelibrary", listfacestr); MessageBox.Show("人脸注册成功!"); } else { if (addflag) { MessageBox.Show("未检测到人脸,人脸注册失败!"); addflag = false; } } if (detectandextractfeature.faceflag && checkflag) { detectandextractfeature = detectAndExtractFeature(bitmap, pictureBox0, false, false, true); float similar = 0.0f; int num = -1; for (int i = 0; i < listface.Count; i++) { float facesimilar = recogitionAndFacePairMatching(listface[i].face_Feature, detectandextractfeature.face_Feature); Console.WriteLine("编号: " + i + "-------" + " 人脸姓名: " + listface[i].face_name + "-------" + " 相似度: " + facesimilar); if (facesimilar > similar) { similar = facesimilar; num = i; } } if (similar > changecheckvalue) { setControlText(label1, "识别结果: " + listface[num].face_name); } else { setControlText(label1, "识别结果: " + "无此人"); } } else { if (checkflag) { setControlText(label1, "识别结果: " + "未检测到人脸"); } } // 设置显示图片 imageBox0.Image = mat; detectandextractfeature = new DetectAndExtractFeature(); }
private void ImageGrabbedProcess(object sender, EventArgs arg) { try { Mat mat = new Mat(); DateTime now = DateTime.Now; capture.Retrieve(mat); // 捕获摄像头图片 Bitmap bitmap = mat.Bitmap; detectfacefeature = GetDetectFaceFeature(bitmap, pictureBox0, false); if (detectfacefeature.faceflag) { //人脸识别 if (checkflag) { float similar = 0.0f; int num = 0; for (int i = 0; i < listface.Count; i++) { float facesimilar = recogitionAndFacePairMatching(listface[i].face_Feature, detectfacefeature.face_Feature); Console.WriteLine("编号: " + i + " -- " + "人脸姓名: " + listface[i].face_name + " -- " + "相似度: " + facesimilar); if (facesimilar > similar) { similar = facesimilar; num = i; } } //刷新进度条 refresh = new Refresh(refreshprogressbar); Invoke(refresh, (similar * 100), listface[num].face_name); if (similar > changecheckvalue) { setControlText(label1, "识别结果: " + listface[num].face_name); bool sendflag = false; if (radioButton1.Checked) { sendflag = Serial_Device_API.GateAPI.GateSendData(Serial_Device_API.DeviceSerialPort.GateSerialPort, Serial_Device_API.GateAPI.gateleft); } else if (radioButton2.Checked) { sendflag = Serial_Device_API.GateAPI.GateSendData(Serial_Device_API.DeviceSerialPort.GateSerialPort, Serial_Device_API.GateAPI.gateright); } if (sendflag) { Console.WriteLine("数据发送成功!"); } else { Console.WriteLine("数据发送失败!"); } } else { setControlText(label1, "识别结果: " + "无此人"); } } else { setControlText(label1, "识别结果: " + "未开启人脸识别"); //刷新进度条 refresh = new Refresh(refreshprogressbar); Invoke(refresh, 0.00f, ""); } //添加人脸 if (addflag) { pictureBox0.Image = null; facelibrary = new FaceLibrary(); facelibrary.face_name = textBox1.Text.Trim(); detectfacefeature = GetDetectFaceFeature(bitmap, pictureBox0, true); facelibrary.face_Feature = detectfacefeature.face_Feature; if (currentfacenum > -1) //覆盖当前人脸信息 { listface[currentfacenum] = facelibrary; } else { listface.Add(facelibrary); } addflag = false; currentfacenum = -1; string listfacelibrary = JsonListToString(listface); FileWrite(filepath, listfacelibrary); MessageBox.Show("人脸注册成功!"); //清除人脸用户名 refreshtext = new RefreshText(refreshTextbox); Invoke(refreshtext, textBox1, ""); } } else { if (checkflag) { setControlText(label1, "识别结果: " + "未检测到人脸"); } if (addflag) { MessageBox.Show("未检测到人脸,人脸注册失败!"); addflag = false; } } //设置显示图片 imageBox0.Image = mat; } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private void button4_Click(object sender, EventArgs e) { if (textBox1.Text.Trim() != "" && pictureBox0.Image != null) { for (int i = 0; i < listface.Count; i++) { if (listface[i].face_name == textBox1.Text.Trim()) { DialogResult dr = MessageBox.Show("该用户名的人脸已经注册,确认将覆盖该人脸!", "提示", MessageBoxButtons.OKCancel); if (dr == DialogResult.Cancel) { textBox1.Text = ""; return; } else { currentfacenum = i; } } } DetectFaceFeature picboxdetectandextractfeature = GetDetectFaceFeature((Bitmap)pictureBox0.Image, pictureBox0, true); if (picboxdetectandextractfeature.faceflag) { facelibrary = new FaceLibrary(); facelibrary.face_name = textBox1.Text.Trim(); facelibrary.face_Feature = picboxdetectandextractfeature.face_Feature; if (currentfacenum > -1) //覆盖当前人脸信息 { listface[currentfacenum] = facelibrary; } else { listface.Add(facelibrary); } string listfacelibrary = JsonListToString(listface); FileWrite(filepath, listfacelibrary); MessageBox.Show("人脸注册成功!"); textBox1.Text = ""; } else { MessageBox.Show("未检测到人脸,人脸注册失败!"); } currentfacenum = -1; } else { if (textBox1.Text.Trim() == "" && pictureBox0.Image == null) { MessageBox.Show("请输入注册人脸的用户名和图片!"); } else if (textBox1.Text.Trim() == "") { MessageBox.Show("请输入注册人脸的用户名!"); } else if (pictureBox0.Image == null) { MessageBox.Show("请输入人脸图片!"); } } }