private void matchFaceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (FaceList.Count == 0)
            {
                MessageBox.Show("Please enroll faces first", "Error");
            }
            else
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "JPEG (*.jpg)|*.jpg|Windows bitmap (*.bmp)|*.bmp|All files|*.*";

                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        string      fn = dlg.FileNames[0];
                        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];

                        fr.image = new FSDK.CImage(fn);

                        fr.FacePosition = fr.image.DetectFace();
                        if (0 == fr.FacePosition.w)
                        {
                            MessageBox.Show("No faces found. Try to lower the Minimal Face Quality parameter in the Options dialog box.", "Enrollment error");
                        }
                        else
                        {
                            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)));
                            fr.FacialFeatures = fr.image.DetectEyesInRegion(ref fr.FacePosition);
                            fr.Template       = fr.image.GetFaceTemplateInRegion(ref fr.FacePosition); // get template with higher precision
                            Results frmResults = new Results();
                            frmResults.Go(fr);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Can't open image(s) with error: " + ex.Message.ToString(), "Error");
                    }
                }
            }
        }
        private void SaveFaceInDB(TFaceRecord fr)
        {
            System.Data.SqlClient.SqlConnection sqlConnect = null;
            try
            {
                //preparing FaceRecord to save
                Image        img       = null;
                Image        img_face  = null;
                MemoryStream strm      = new MemoryStream();
                MemoryStream strm_face = new MemoryStream();
                img      = fr.image.ToCLRImage();
                img_face = fr.faceImage.ToCLRImage();
                img.Save(strm, System.Drawing.Imaging.ImageFormat.Jpeg);
                img_face.Save(strm_face, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte [] img_array      = new byte[strm.Length];
                byte [] img_face_array = new byte[strm_face.Length];
                strm.Position = 0;
                strm.Read(img_array, 0, img_array.Length);
                strm_face.Position = 0;
                strm_face.Read(img_face_array, 0, img_face_array.Length);

                //connect to Microsoft SQL Server and save FaceRecord
                sqlConnect = new System.Data.SqlClient.SqlConnection("server=" + SQLServerAddress + "," + SQLServerPort + "; initial catalog=" + DatabaseName + "; Integrated Security=SSPI");
                sqlConnect.Open();
                System.Data.SqlClient.SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand("INSERT INTO FaceList(ImageFileName, FacePositionXc, FacePositionYc, FacePositionW, FacePositionAngle, Eye1X, Eye1Y, Eye2X, Eye2Y, Template, Image, FaceImage) " +
                                                                                               " values(@ImageFileName, @FacePositionXc, @FacePositionYc, @FacePositionW, @FacePositionAngle, @Eye1X, @Eye1Y, @Eye2X, @Eye2Y, @Template, @Image, @FaceImage)",
                                                                                               sqlConnect);

                sqlCmd.Parameters.Add("@ImageFileName", System.Data.SqlDbType.VarChar, 260);
                sqlCmd.Parameters.Add("@FacePositionXc", System.Data.SqlDbType.Int);
                sqlCmd.Parameters.Add("@FacePositionYc", System.Data.SqlDbType.Int);
                sqlCmd.Parameters.Add("@FacePositionW", System.Data.SqlDbType.Int);
                sqlCmd.Parameters.Add("@FacePositionAngle", System.Data.SqlDbType.Real);
                sqlCmd.Parameters.Add("@Eye1X", System.Data.SqlDbType.Int);
                sqlCmd.Parameters.Add("@Eye1Y", System.Data.SqlDbType.Int);
                sqlCmd.Parameters.Add("@Eye2X", System.Data.SqlDbType.Int);
                sqlCmd.Parameters.Add("@Eye2Y", System.Data.SqlDbType.Int);
                sqlCmd.Parameters.Add("@Template", System.Data.SqlDbType.VarBinary);
                sqlCmd.Parameters.Add("@Image", System.Data.SqlDbType.VarBinary);
                sqlCmd.Parameters.Add("@FaceImage", System.Data.SqlDbType.VarBinary);
                sqlCmd.Parameters["@ImageFileName"].Value     = fr.ImageFileName;
                sqlCmd.Parameters["@FacePositionXc"].Value    = fr.FacePosition.xc;
                sqlCmd.Parameters["@FacePositionYc"].Value    = fr.FacePosition.yc;
                sqlCmd.Parameters["@FacePositionW"].Value     = fr.FacePosition.w;
                sqlCmd.Parameters["@FacePositionAngle"].Value = (float)fr.FacePosition.angle;
                sqlCmd.Parameters["@Eye1X"].Value             = fr.FacialFeatures[0].x;
                sqlCmd.Parameters["@Eye1Y"].Value             = fr.FacialFeatures[0].y;
                sqlCmd.Parameters["@Eye2X"].Value             = fr.FacialFeatures[1].x;
                sqlCmd.Parameters["@Eye2Y"].Value             = fr.FacialFeatures[1].y;
                sqlCmd.Parameters["@Template"].Value          = fr.Template;
                sqlCmd.Parameters["@Image"].Value             = img_array;
                sqlCmd.Parameters["@FaceImage"].Value         = img_face_array;

                int iresult = sqlCmd.ExecuteNonQuery();
                textBox1.Text += iresult + " rows modified in database.\r\n";

                img.Dispose();
                img_face.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception on saving to database");
            }
            finally
            {
                if (sqlConnect != null)
                {
                    sqlConnect.Close();
                }
            }
        }
        private void LoadDB()
        {
            System.Data.SqlClient.SqlConnection sqlConnect = null;
            try
            {
                sqlConnect = new System.Data.SqlClient.SqlConnection("server=" + SQLServerAddress + "," + SQLServerPort + "; initial catalog=" + DatabaseName + "; Integrated Security=SSPI");
                sqlConnect.Open();

                System.Data.SqlClient.SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand("SELECT ImageFileName, FacePositionXc, FacePositionYc, FacePositionW, FacePositionAngle, Eye1X, Eye1Y, Eye2X, Eye2Y, Template, Image, FaceImage FROM FaceList", sqlConnect);

                System.Data.SqlClient.SqlDataReader reader = sqlCmd.ExecuteReader();
                while (reader.Read())
                {
                    TFaceRecord fr = new TFaceRecord();
                    fr.ImageFileName = reader.GetString(0);

                    fr.FacePosition       = new FSDK.TFacePosition();
                    fr.FacePosition.xc    = reader.GetInt32(1);
                    fr.FacePosition.yc    = reader.GetInt32(2);
                    fr.FacePosition.w     = reader.GetInt32(3);
                    fr.FacePosition.angle = reader.GetFloat(4);

                    fr.FacialFeatures      = new FSDK.TPoint[2];
                    fr.FacialFeatures[0]   = new FSDK.TPoint();
                    fr.FacialFeatures[0].x = reader.GetInt32(5);
                    fr.FacialFeatures[0].y = reader.GetInt32(6);
                    fr.FacialFeatures[1]   = new FSDK.TPoint();
                    fr.FacialFeatures[1].x = reader.GetInt32(7);
                    fr.FacialFeatures[1].y = reader.GetInt32(8);

                    fr.Template = new byte[FSDK.TemplateSize];
                    reader.GetBytes(9, 0, fr.Template, 0, FSDK.TemplateSize);

                    Image img      = Image.FromStream(new System.IO.MemoryStream(reader.GetSqlBinary(10).Value));
                    Image img_face = Image.FromStream(new System.IO.MemoryStream(reader.GetSqlBinary(11).Value));
                    fr.image     = new FSDK.CImage(img);
                    fr.faceImage = new FSDK.CImage(img_face);


                    FaceList.Add(fr);

                    imageList1.Images.Add(fr.faceImage.ToCLRImage());
                    string fn = fr.ImageFileName;
                    listView1.Items.Add((imageList1.Images.Count - 1).ToString(), fn.Split('\\')[fn.Split('\\').Length - 1], imageList1.Images.Count - 1);

                    textBox1.Text += "File '" + fn + "' read from database\r\n";

                    img.Dispose();
                    img_face.Dispose();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception on loading database");
            }
            finally
            {
                if (sqlConnect != null)
                {
                    sqlConnect.Close();
                }
            }
        }
        private void enrollFacesToolStripMenuItem_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)
            {
                try
                {
                    //Assuming that faces are vertical (HandleArbitraryRotations = false) to speed up face detection
                    FSDK.SetFaceDetectionParameters(false, true, 384);
                    FSDK.SetFaceDetectionThreshold((int)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();
                        fr.FacePosition = fr.image.DetectFace();
                        if (0 == fr.FacePosition.w)
                        {
                            if (dlg.FileNames.Length <= 1)
                            {
                                MessageBox.Show("No faces found. Try to lower the Minimal Face Quality parameter in the Options dialog box.", "Enrollment error");
                            }
                            else
                            {
                                textBox1.Text += (fn + ": No faces found. Try to lower the Minimal Face Quality parameter in the Options dialog box.\r\n");
                            }
                        }
                        else
                        {
                            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)));
                            fr.FacialFeatures = fr.image.DetectEyesInRegion(ref fr.FacePosition);
                            fr.Template       = fr.image.GetFaceTemplateInRegion(ref fr.FacePosition); // get template with higher precision

                            SaveFaceInDB(fr);
                            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("Can't open image(s) with error: " + ex.Message.ToString(), "Error");
                }
            }
        }
        private void enrollFacesToolStripMenuItem_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)
            {
                try
                {
                    //Assuming that faces are vertical (HandleArbitraryRotations = false) to speed up face detection
                    FSDK.SetFaceDetectionParameters(false, true, 384);
                    FSDK.SetFaceDetectionThreshold((int)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();
                        fr.FacePosition = fr.image.DetectFace();
                        if (0 == fr.FacePosition.w)
                            if (dlg.FileNames.Length <= 1)
                                MessageBox.Show("No faces found. Try to lower the Minimal Face Quality parameter in the Options dialog box.", "Enrollment error");
                            else
                                textBox1.Text += (fn + ": No faces found. Try to lower the Minimal Face Quality parameter in the Options dialog box.\r\n");
                        else
                        {
                            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)));

                            try
                            {
                                fr.FacialFeatures = fr.image.DetectEyesInRegion(ref fr.FacePosition);
                            }
                            catch (Exception ex2)
                            {
                                MessageBox.Show(ex2.Message, "Error detecting eyes.");
                            }

                            try
                            {
                                fr.Template = fr.image.GetFaceTemplateInRegion(ref fr.FacePosition); // get template with higher precision
                            }
                            catch (Exception ex2)
                            {
                                MessageBox.Show(ex2.Message, "Error retrieving face template.");
                            }

                            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 matchFaceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (FaceList.Count == 0)
                MessageBox.Show("Please enroll faces first", "Error");
            else {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "JPEG (*.jpg)|*.jpg|Windows bitmap (*.bmp)|*.bmp|All files|*.*";

                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        string fn = dlg.FileNames[0];
                        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, "Error loading file");
                        }

                        fr.FacePosition = fr.image.DetectFace();
                        if (0 == fr.FacePosition.w)
                            MessageBox.Show("No faces found. Try to lower the Minimal Face Quality parameter in the Options dialog box.", "Enrollment error");
                        else
                        {
                            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);
                                eyesDetected = true;
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message, "Error detecting eyes.");
                            }

                            if (eyesDetected)
                            {
                                fr.Template = fr.image.GetFaceTemplateInRegion(ref fr.FacePosition); // get template with higher precision
                            }
                        }

                        Results frmResults = new Results();
                        frmResults.Go(fr);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Can't open image(s) with error: " + ex.Message.ToString(), "Error");
                    }

                }
            }
        }
        public void Go(TFaceRecord SearchFace)
        {
            Image img = SearchFace.image.ToCLRImage();

            pictureBox1.Image  = img;
            pictureBox1.Height = img.Height;
            pictureBox1.Width  = img.Width;

            imageList1 = new ImageList();
            Size size100x100 = new Size();

            size100x100.Height    = 100;
            size100x100.Width     = 100;
            imageList1.ImageSize  = size100x100;
            imageList1.ColorDepth = ColorDepth.Depth24Bit;

            listView1.OwnerDraw      = false;
            listView1.View           = View.LargeIcon;
            listView1.Dock           = DockStyle.Bottom;
            listView1.LargeImageList = imageList1;

            label1.Dock = DockStyle.Bottom;

            float Threshold = 0.0f;

            FSDK.GetMatchingThresholdAtFAR(Form1.FARValue / 100, ref Threshold);

            int MatchedCount = 0;
            int FaceCount    = Form1.FaceList.Count;

            float[] Similarities = new float[FaceCount];
            int[]   Numbers      = new int[FaceCount];

            for (int i = 0; i < Form1.FaceList.Count; i++)
            {
                float       Similarity  = 0.0f;
                TFaceRecord CurrentFace = Form1.FaceList[i];
                FSDK.MatchFaces(ref SearchFace.Template, ref CurrentFace.Template, ref Similarity);
                if (Similarity >= Threshold)
                {
                    Similarities[MatchedCount] = Similarity;
                    Numbers[MatchedCount]      = i;
                    ++MatchedCount;
                }
            }

            if (MatchedCount == 0)
            {
                MessageBox.Show("No matches found. You can try to increase the FAR parameter in the Options dialog box.", "No matches");
            }
            else
            {
                floatReverseComparer cmp = new floatReverseComparer();
                Array.Sort(Similarities, Numbers, 0, MatchedCount, (IComparer <float>)cmp);

                label1.Text = "Faces Matched: " + MatchedCount.ToString();
                for (int i = 0; i < MatchedCount; i++)
                {
                    imageList1.Images.Add(Form1.FaceList[Numbers[i]].faceImage.ToCLRImage());
                    listView1.Items.Add((Similarities[i] * 100.0f).ToString(System.Globalization.CultureInfo.InvariantCulture.NumberFormat),
                                        Form1.FaceList[Numbers[i]].ImageFileName.Split('\\')[Form1.FaceList[Numbers[i]].ImageFileName.Split('\\').Length - 1] +
                                        "\r\nSimilarity = " + (Similarities[i] * 100).ToString(),
                                        imageList1.Images.Count - 1);
                }
            }


            this.Show();
        }
Beispiel #8
0
        private void SaveFaceInDB(TFaceRecord fr)
        {
            System.Data.SqlClient.SqlConnection sqlConnect = null;
            try
            {
                //preparing FaceRecord to save
                Image img = null;
                Image img_face = null;
                MemoryStream strm = new MemoryStream();
                MemoryStream strm_face = new MemoryStream();
                img = fr.image.ToCLRImage();
                img_face = fr.faceImage.ToCLRImage();
                img.Save(strm, System.Drawing.Imaging.ImageFormat.Jpeg);
                img_face.Save(strm_face, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte [] img_array = new byte[strm.Length];
                byte [] img_face_array = new byte[strm_face.Length];
                strm.Position = 0;
                strm.Read(img_array, 0, img_array.Length);
                strm_face.Position = 0;
                strm_face.Read(img_face_array, 0, img_face_array.Length);

                //connect to Microsoft SQL Server and save FaceRecord
                sqlConnect = new System.Data.SqlClient.SqlConnection("server=" + SQLServerAddress + "," + SQLServerPort + "; initial catalog=" + DatabaseName + "; Integrated Security=SSPI");
                sqlConnect.Open();
                System.Data.SqlClient.SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand("INSERT INTO FaceList(ImageFileName, FacePositionXc, FacePositionYc, FacePositionW, FacePositionAngle, Eye1X, Eye1Y, Eye2X, Eye2Y, Template, Image, FaceImage) "+
                     " values(@ImageFileName, @FacePositionXc, @FacePositionYc, @FacePositionW, @FacePositionAngle, @Eye1X, @Eye1Y, @Eye2X, @Eye2Y, @Template, @Image, @FaceImage)",
                     sqlConnect);

                sqlCmd.Parameters.Add("@ImageFileName", System.Data.SqlDbType.VarChar, 260);
                sqlCmd.Parameters.Add("@FacePositionXc", System.Data.SqlDbType.Int);
                sqlCmd.Parameters.Add("@FacePositionYc", System.Data.SqlDbType.Int);
                sqlCmd.Parameters.Add("@FacePositionW", System.Data.SqlDbType.Int);
                sqlCmd.Parameters.Add("@FacePositionAngle", System.Data.SqlDbType.Real);
                sqlCmd.Parameters.Add("@Eye1X", System.Data.SqlDbType.Int);
                sqlCmd.Parameters.Add("@Eye1Y", System.Data.SqlDbType.Int);
                sqlCmd.Parameters.Add("@Eye2X", System.Data.SqlDbType.Int);
                sqlCmd.Parameters.Add("@Eye2Y", System.Data.SqlDbType.Int);
                sqlCmd.Parameters.Add("@Template", System.Data.SqlDbType.VarBinary);
                sqlCmd.Parameters.Add("@Image", System.Data.SqlDbType.VarBinary);
                sqlCmd.Parameters.Add("@FaceImage", System.Data.SqlDbType.VarBinary);
                sqlCmd.Parameters["@ImageFileName"].Value = fr.ImageFileName;
                sqlCmd.Parameters["@FacePositionXc"].Value = fr.FacePosition.xc;
                sqlCmd.Parameters["@FacePositionYc"].Value = fr.FacePosition.yc;
                sqlCmd.Parameters["@FacePositionW"].Value = fr.FacePosition.w;
                sqlCmd.Parameters["@FacePositionAngle"].Value = (float)fr.FacePosition.angle;
                sqlCmd.Parameters["@Eye1X"].Value = fr.FacialFeatures[0].x;
                sqlCmd.Parameters["@Eye1Y"].Value = fr.FacialFeatures[0].y;
                sqlCmd.Parameters["@Eye2X"].Value = fr.FacialFeatures[1].x;
                sqlCmd.Parameters["@Eye2Y"].Value = fr.FacialFeatures[1].y;
                sqlCmd.Parameters["@Template"].Value = fr.Template;
                sqlCmd.Parameters["@Image"].Value = img_array;
                sqlCmd.Parameters["@FaceImage"].Value = img_face_array;

                int iresult = sqlCmd.ExecuteNonQuery();
                textBox1.Text += iresult + " rows modified in database.\r\n";

                img.Dispose();
                img_face.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception on saving to database");
            }
            finally
            {
                if (sqlConnect != null)
                    sqlConnect.Close();
            }
        }
Beispiel #9
0
        private void LoadDB()
        {
            System.Data.SqlClient.SqlConnection sqlConnect = null;
            try
            {
                sqlConnect = new System.Data.SqlClient.SqlConnection("server=" + SQLServerAddress + "," + SQLServerPort + "; initial catalog=" + DatabaseName + "; Integrated Security=SSPI");
                sqlConnect.Open();

                System.Data.SqlClient.SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand("SELECT ImageFileName, FacePositionXc, FacePositionYc, FacePositionW, FacePositionAngle, Eye1X, Eye1Y, Eye2X, Eye2Y, Template, Image, FaceImage FROM FaceList", sqlConnect);

                System.Data.SqlClient.SqlDataReader reader = sqlCmd.ExecuteReader();
                while (reader.Read())
                {
                    TFaceRecord fr = new TFaceRecord();
                    fr.ImageFileName = reader.GetString(0);

                    fr.FacePosition = new FSDK.TFacePosition();
                    fr.FacePosition.xc = reader.GetInt32(1);
                    fr.FacePosition.yc = reader.GetInt32(2);
                    fr.FacePosition.w = reader.GetInt32(3);
                    fr.FacePosition.angle = reader.GetFloat(4);

                    fr.FacialFeatures = new FSDK.TPoint[2];
                    fr.FacialFeatures[0] = new FSDK.TPoint();
                    fr.FacialFeatures[0].x = reader.GetInt32(5);
                    fr.FacialFeatures[0].y = reader.GetInt32(6);
                    fr.FacialFeatures[1] = new FSDK.TPoint();
                    fr.FacialFeatures[1].x = reader.GetInt32(7);
                    fr.FacialFeatures[1].y = reader.GetInt32(8);

                    fr.Template = new byte[FSDK.TemplateSize];
                    reader.GetBytes(9, 0, fr.Template, 0, FSDK.TemplateSize);

                    Image img = Image.FromStream(new System.IO.MemoryStream(reader.GetSqlBinary(10).Value));
                    Image img_face = Image.FromStream(new System.IO.MemoryStream(reader.GetSqlBinary(11).Value));
                    fr.image = new FSDK.CImage(img);
                    fr.faceImage = new FSDK.CImage(img_face);

                    FaceList.Add(fr);

                    imageList1.Images.Add(fr.faceImage.ToCLRImage());
                    string fn = fr.ImageFileName;
                    listView1.Items.Add((imageList1.Images.Count - 1).ToString(), fn.Split('\\')[fn.Split('\\').Length - 1], imageList1.Images.Count - 1);

                    textBox1.Text += "File '" + fn + "' read from database\r\n";

                    img.Dispose();
                    img_face.Dispose();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception on loading database");
            }
            finally
            {
                if (sqlConnect != null)
                    sqlConnect.Close();
            }
        }