现在的位置: 首页 > 综合 > 正文

Neurotec Biometrics 人脸、检测、采集、识别、匹配、对比

2012年05月02日 ⁄ 综合 ⁄ 共 5805字 ⁄ 字号 评论关闭

   private void MatchFaces()
        {
            try
            {
                using (NImage frame = _camera.GetFrame())
                {
                    if (frame == null)
                    {
                        return;
                    }
                    using (NGrayscaleImage grayscaleImage = frame.ToGrayscale())
                    {
                        NleFace[] faces = _extractor.DetectFaces(grayscaleImage);
                        if (faces.Length <= 0)
                            return;
                        NleFace face = faces[0];
                        NleDetectionDetails detectionDetails = _extractor.DetectFacialFeatures(grayscaleImage, face);
                        NleExtractionStatus extractionStatus;
                        NLTemplate template = _extractor.ExtractUsingDetails(grayscaleImage, detectionDetails, out extractionStatus);
                        if (extractionStatus == NleExtractionStatus.TemplateCreated)
                        {
                            _template = template.Save();
                        }
                    }
                }

                if (_template != null && _templates.Length > 0)
                {
                    try
                    {
                        DataTable dt = new DataTable("tblScore");
                        dt.Columns.Add("colTemplate", System.Type.GetType("System.String"));
                        dt.Columns.Add("colScore", System.Type.GetType("System.Int32"));
                        _matcher.IdentifyStart(_template);
                        for (int i = 0; i < _templates.Length; ++i)
                        {
                            int score = _matcher.IdentifyNext(_templates[i]);
                            DataRow dr = dt.NewRow();
                            dr["colTemplate"] = _templatesNames[i];
                            dr["colScore"] = score;
                            dt.Rows.Add(dr);
                        }

                        dt.DefaultView.Sort = "colScore DESC";
                        DataTable dtTemp = dt.DefaultView.ToTable();
                        listViewEx1.Items.Clear();
                        listViewEx1.Sorting = SortOrder.None;
                        for (int row = 0; row < 10; row++)
                        {
                            listViewEx1.Items.Add(new ListViewItem(new string[] { dtTemp.Rows[row][0].ToString().Replace(".dat", ""), dtTemp.Rows[row][1].ToString() }));
                        }
                        PictureBox pic = new PictureBox();
                        pic.Image = NImage.FromFile(Application.StartupPath + "\\Pictures\\" + dtTemp.Rows[0][0].ToString().Replace(".dat", "") + ".jpg").ToBitmap();
                        pic.Width = pic.Image.Width;
                        pic.Height = pic.Image.Height;
                        faceHistory.Add(pic);
                        UpdatePanel(null, null, NleExtractionStatus.None);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                    finally
                    {
                        _matcher.IdentifyEnd();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void BuildTemplates()
        {
            try
            {
                using (NImage frame = _camera.GetFrame())
                {
                    if (frame == null)
                    {
                        return;
                    }
                    using (NGrayscaleImage grayscale = frame.ToGrayscale())
                    {
                        strDate = DateTime.Now.Date.ToShortDateString().Replace("/", "");
                        NleFace[] faces = _extractor.DetectFaces(grayscale);
                        Rectangle imgRect = new Rectangle();
                        for (int ii = 0; ii < faces.Length; ii++)
                        {
                            NleFace face = faces[ii];
                            NleDetectionDetails detectionDetails = _extractor.DetectFacialFeatures(grayscale, face);
                            NleExtractionStatus extractionStatus;
                            NLTemplate template = _extractor.ExtractUsingDetails(grayscale, detectionDetails, out extractionStatus);

                            if (extractionStatus == NleExtractionStatus.TemplateCreated)
                            {
                                i = i + 1;
                                string FileName = Application.StartupPath + "\\Pictures\\" + strDate + i.ToString() + ".jpg";
                                imgRect.X = detectionDetails.Face.Rectangle.X; //detectionDetails.Face.Confidence
                                imgRect.Y = detectionDetails.Face.Rectangle.Y;
                                imgRect.Width = detectionDetails.Face.Rectangle.Width;
                                imgRect.Height = detectionDetails.Face.Rectangle.Height;
                                Bitmap img = frame.ToBitmap().Clone(imgRect, System.Drawing.Imaging.PixelFormat.DontCare); //截取人脸图像
                                img.Save(FileName);

                                string strFileName = Path.GetFileNameWithoutExtension(FileName);
                                string strTemplate = Application.StartupPath + "\\Templates\\" + strFileName + ".dat";
                                listViewEx1.Items.Add(new ListViewItem(new string[] { strFileName, "0" }));
                                File.WriteAllBytes(strTemplate, template.Save().ToByteArray());

                                PictureBox pic = new PictureBox();
                                pic.Image = img;
                                pic.Width = pic.Image.Width;
                                pic.Height = pic.Image.Height;
                                faceHistory.Add(pic);
                            }
                        }

                        UpdatePanel();
                    }
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

抱歉!评论已关闭.