Ejemplo n.º 1
0
        private bool createTemplate(Bitmap enrollmentBmp, bool largeTemplate, out NleDetectionDetails detDetails)
        {
            NLExtractor templateExtractor = new NLExtractor();

            if (largeTemplate)
            {
                templateExtractor.TemplateSize = NleTemplateSize.Large;
            }
            else
            {
                templateExtractor.TemplateSize = NleTemplateSize.Medium;
            }
            NImage              enrollmentImage     = NImage.FromBitmap(enrollmentBmp);
            NGrayscaleImage     enrollmentGrayscale = enrollmentImage.ToGrayscale();
            NleDetectionDetails _detDetails         = null;

            try {
                verifyLicense();
                NleExtractionStatus extractionStatus;
                facialTemplate = templateExtractor.Extract(enrollmentGrayscale, out _detDetails, out extractionStatus);

                if (extractionStatus != NleExtractionStatus.TemplateCreated)
                {
                    MessageBox.Show("Face Template Extraction Failed!\nPlease try again.\n" + extractionStatus.ToString());
                    detDetails = _detDetails;
                    return(false);
                }
                else
                {
                    detDetails = _detDetails;
                    return(true);
                }
            } catch (Exception ex) {
                MessageBox.Show("" + ex);
                detDetails = null;
                return(false);
            } finally {
                NLicense.ReleaseComponents(Components);
                if (templateExtractor != null)
                {
                    templateExtractor.Dispose();
                }
            }
        }
Ejemplo n.º 2
0
    protected void Button1_Click(object sender, EventArgs e)
    {
        //  System.Windows.Forms.OpenFileDialog openFaceImageDlg = new OpenFileDialog();

        //   openFaceImageDlg.ShowDialog();

        string savePath = Server.MapPath("~/image/");//指定上传文件在服务器上的保存路径;

        if (FileUpload1.PostedFile.FileName != null)
        {
            if (FileUpload1.HasFile)
            {
                //检查服务器上是否存在这个物理路径,如果不存在则创建
                if (!System.IO.Directory.Exists(savePath))
                {
                    System.IO.Directory.CreateDirectory(savePath);
                }
                savePath = savePath + "\\" + FileUpload1.FileName;
                FileUpload1.SaveAs(savePath);
            }
        }

        // openFaceImageDlg.Filter = NImages.GetOpenFileFilterString(true, true);
        // if (openFaceImageDlg.ShowDialog() == DialogResult.OK)
        // {
        //    if (_image != null) _image.Dispose();
        //    _image = null;

        //      try
        //    {
        //   TextBox1.Text = FileUpload1.PostedFile.FileName;
        // Read image
        _image = NImage.FromFile(savePath);
        //        DetectFace(_image);
        //    }
        //    catch (Exception ex)
        //   {
        //        Utils.ShowException(ex);
        //   }
        NleFace nlef;

        nle.DetectFace(_image.ToGrayscale(), out nlef);
        nle.DetectAllFeaturePoints = true;
        nle.DetectBlink            = true;
        nle.DetectEmotion          = true;
        nle.DetectExpression       = true;
        nle.DetectGender           = true;
        nle.DetectGlasses          = true;
        nle.DetectDarkGlasses      = true;
        nle.DetectMouthOpen        = true;
        nle.MaxRollAngleDeviation  = short.Parse(cbRollAngle.SelectedValue);
        nle.MaxYawAngleDeviation   = short.Parse(cbYawAngle.SelectedValue);
        string Blink      = "";
        string Emotion    = "";
        string Expression = "";
        string Gender     = "";
        string Glasses    = "";
        string Mouth      = "";

        NleDetectionDetails detail = nle.DetectFacialFeatures(_image.ToGrayscale(), nlef);
        NleDetectionDetails detail2;
        NleExtractionStatus Status;

        nle.Extract(_image.ToGrayscale(), out detail2, out Status);
        List <NLFeaturePoint> points = new List <NLFeaturePoint>();

        points.Add(detail.LeftEyeCenter);
        points.Add(detail.MouthCenter);
        points.Add(detail.RightEyeCenter);
        for (int i = 0; i < detail.Points.Length; i++)
        {
            points.Add(detail.Points[i]);
        }
        Bitmap bit = new Bitmap(savePath);

        Graphics g = Graphics.FromImage(bit);
        Brush    b = new SolidBrush(Color.Green);
        Pen      p = new Pen(b);

        for (int i = 0; i < points.Count; i++)
        {
            g.DrawRectangle(p, points.ElementAt(i).X - 2.5f, points.ElementAt(i).Y - 2.5f, 5, 5);
        }

        string dirpath = "D:/img2.JPG";

        if (System.IO.File.Exists(dirpath) == true)
        {
            System.IO.File.Delete(dirpath);
        }

        bit.Save(dirpath, System.Drawing.Imaging.ImageFormat.Jpeg);
        if (detail2.EmotionAngerConfidence > 50 && detail2.EmotionAngerConfidence != 254 && detail2.EmotionAngerConfidence != 255)
        {
            Emotion += " Anger ";
        }
        if (detail2.EmotionDisgustConfidence > 50 && detail2.EmotionDisgustConfidence != 254 && detail2.EmotionDisgustConfidence != 255)
        {
            Emotion += " Disgust ";
        }
        if (detail2.EmotionFearConfidence > 50 && detail2.EmotionFearConfidence != 254 && detail2.EmotionFearConfidence != 255)
        {
            Emotion += " Fear ";
        }
        if (detail2.EmotionHappinessConfidence > 50 && detail2.EmotionHappinessConfidence != 254 && detail2.EmotionHappinessConfidence != 255)
        {
            Emotion += " Happyness ";
        }
        if (detail2.EmotionNeutralConfidence > 50 && detail2.EmotionNeutralConfidence != 254 && detail2.EmotionNeutralConfidence != 255)
        {
            Emotion += " Netral ";
        }
        if (detail2.EmotionSadnessConfidence > 50 && detail2.EmotionSadnessConfidence != 254 && detail2.EmotionSadnessConfidence != 255)
        {
            Emotion += " Sadness ";
        }
        if (detail2.EmotionSurpriseConfidence > 50 && detail2.EmotionSurpriseConfidence != 254 && detail2.EmotionSurpriseConfidence != 255)
        {
            Emotion += " Surprise ";
        }
        Expression += detail2.Expression.ToString();

        if (detail2.DarkGlassesConfidence != 254 && detail2.DarkGlassesConfidence != 255)
        {
            if (detail2.DarkGlassesConfidence > 50)
            {
                Glasses += " wearing glasses ";
            }
            else
            {
                Glasses += " not wearing glasses ";
            }
        }
        if (detail2.BlinkConfidence != 254 && detail2.BlinkConfidence != 255)
        {
            if (detail2.BlinkConfidence > 50)
            {
                Blink += " Eye open ";
            }
            else
            {
                Blink += " Eye close ";
            }
        }
        Gender += detail2.Gender.ToString();
        if (detail2.MouthOpenConfidence != 254 && detail2.MouthOpenConfidence != 255)
        {
            if (detail2.MouthOpenConfidence < 51)
            {
                Mouth += " Mouth close ";
            }
            else
            {
                Mouth += " Mouth open ";
            }
        }
        //Response.Write("<script>alert('"+Blink+Emotion+Expression+Glasses+Gender+Mouth+"')</script>");
        //  Response.Write("<script>alert('" + detail2.BlinkConfidence + Emotion + Expression + Gender + detail2.MouthOpenConfidence + "')</script>");
        DataSet dst = new DataSet();

        dst.DataSetName = "result";
        DataTable dt = new DataTable();

        dst.Tables.Add(dt);
        dt.Columns.Add("blink");
        dt.Columns.Add("emotion");
        dt.Columns.Add("expression");
        dt.Columns.Add("glasses");
        dt.Columns.Add("gender");
        dt.Columns.Add("mouth");

        DataRow row1 = dt.NewRow();

        row1["blink"]      = Blink;
        row1["emotion"]    = Emotion;
        row1["expression"] = Expression;
        row1["glasses"]    = detail2.DarkGlassesConfidence;
        row1["gender"]     = Gender;
        row1["mouth"]      = Mouth;
        dt.Rows.Add(row1);
        // dst.Tables.Add(dt);
        Session["result"]    = dst;
        ListView1.DataSource = dst;
        ListView1.DataBind();
        _image.Dispose();
        bit.Dispose();
        g.Dispose();
    }
Ejemplo n.º 3
0
        private void enrollFromImage(bool toView2)
        {
            NImage nImage = null;

            if (System.Configuration.ConfigurationManager.AppSettings["Enroll"] != "file")
            {
                byte[]          buffer = null;
                DBHelper.DBUtil db     = new DBHelper.DBUtil();
                if (System.Configuration.ConfigurationManager.AppSettings["Enroll"] == "service")
                {
                    buffer = db.GetImageFromWebService(IMAGE_TYPE.picture, this.userId);
                }
                else
                {
                    buffer = db.GetImage(IMAGE_TYPE.picture, this.userId);
                }

                MemoryStream ms = null;
                try
                {
                    if (buffer != null)
                    {
                        ms = new MemoryStream(buffer);
                    }

                    if (ms != null)
                    {
                        nImage = NImage.FromStream(ms);
                    }
                }
                catch (Exception ex)
                {
                    ShowError(string.Format("Error creating image retrieved from database {0}", ex.Message));
                    return;
                }
                finally
                {
                    if (ms != null)
                    {
                        ms.Dispose();
                    }
                }
            }
            else
            {
                if (!toView2)
                {
                    nImage = NImage.FromFile(Path.GetDirectoryName(Application.ExecutablePath) + "\\Roman.jpg");
                }
                else
                {
                    nImage = NImage.FromFile(Path.GetDirectoryName(Application.ExecutablePath) + "\\Roman2.jpg");
                }
            }

            try
            {
                if (!toView2)
                {
                    nlView1.Image = nImage.ToBitmap();
                }
                else
                {
                    nlView2.Image = nImage.ToBitmap();
                }

                using (NGrayscaleImage gray = nImage.ToGrayscale())
                {
                    NleDetectionDetails details;
                    NleExtractionStatus status;
                    try { _extractor.DetectAllFeaturePoints = Settings.Default.ExtractorDetectAllFeaturesNonLive; }
                    catch { }
                    NLTemplate template = _extractor.Extract(gray, out details, out status);
                    if (status != NleExtractionStatus.TemplateCreated)
                    {
                        _newRecord = null;
//                        ShowError(string.Format("Template extraction failed: {0}", status));
                        String descr = getStatusDescription(status);
                        if (descr != String.Empty)
                        {
                            ShowError(string.Format("Template extraction failed: {0}", descr));
                        }
                        else
                        {
                            ShowError(string.Format("Template extraction failed: {0}", status));
                        }
                        return;
                    }
                    else
                    {
                        _newRecord = new FaceRecord(template, nImage, details);
                        if (!toView2)
                        {
                            _enrolledTemplateList = new List <byte[]>();
                            _enrolledTemplateList.Add(template.Save().ToByteArray());
                        }
                        else
                        {
                            _capturedTemplateList = new List <byte[]>();
                            _capturedTemplateList.Add(template.Save().ToByteArray());
                        }
                        template.Dispose();
                    }

                    if (!toView2)
                    {
                        SetImageToView(nlView1, nImage.ToBitmap(), new NleDetectionDetails[] { details }, status, 100, TimeSpan.Zero);
                    }
                    else
                    {
                        SetImageToView(nlView2, nImage.ToBitmap(), new NleDetectionDetails[] { details }, status, 100, TimeSpan.Zero);
                    }
                }
            }
            catch (Exception ex)
            {
                ShowError(ex.Message);
            }
            return;
        }