Exemplo n.º 1
0
        //主处理部分
        private void start()
        {
            loadUsers();
            NeedClose = false;
            int CameraHandle = CameraStart();

            //设置监测条件  其中

            /*
             * SetFaceDetectionParameters(false, false, 100);参数1将人脸的检测角度从正负15°拓展到30°
             *                                               参数2将决定是否检测人脸所处的角度
             *                                               参数3是缩放大小 值越大质量越高
             *现在有效距离1米  最大有效距离1.3米
             * /*
             * SetFaceDetectionThreshold(3);设置对于人脸的敏感性 数值越高就越对于人脸不敏感 只能在人脸十分清晰的时候才能将其检测
             */
            FSDK.SetFaceDetectionParameters(true, false, 100);
            FSDK.SetFaceDetectionThreshold(1);
            while (!NeedClose)
            {
                try
                {
                    switch (programState)
                    {
                    case ProgramState.psNormal:
                        Normalhandle();
                        break;

                    case ProgramState.psAddFace:
                        AddFacehandle();
                        lock (obj)
                        {
                            ok = true;
                            Monitor.PulseAll(obj);
                            programState = ProgramState.psNormal;
                        }
                        break;

                    case ProgramState.psRecognize:
                        Recongnizehandle();
                        lock (obj)
                        {
                            ok = true;
                            Monitor.PulseAll(obj);
                            programState = ProgramState.psNormal;
                        }
                        break;

                    case ProgramState.psNothing:
                        Nothinghandle();
                        break;
                    }
                    Application.DoEvents();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
Exemplo n.º 2
0
        private void loadSubject(string fileName)
        {
            FSDK.SetFaceDetectionParameters(false, true, 384);
            FSDK.SetFaceDetectionThreshold((int)FaceDetectionThreshold);

            TFaceRecord fr = new TFaceRecord();

            fr.ImageFileName = fileName;

            fr.FacePosition   = new FSDK.TFacePosition();
            fr.FacialFeatures = new FSDK.TPoint[2];
            fr.Template       = new byte[FSDK.TemplateSize];

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

            fr.FacePosition = fr.image.DetectFace();
            if (0 == fr.FacePosition.w)
            {
                if (fileName.Length <= 1)
                {
                    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)));

                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.");
                }

                FaceSearchList.Add(fr);
                FaceSearchImageList.Images.Add(fr.faceImage.ToCLRImage());
                listView1.Items.Add((FaceSearchImageList.Images.Count - 1).ToString(), fileName.Split('\\')[fileName.Split('\\').Length - 1], FaceSearchImageList.Images.Count - 1);

                using (Image img = fr.image.ToCLRImage())
                {
                    pictureBox1.Image = img;
                    pictureBox1.Refresh();
                }
            }
            pictureBox1.Image = null;
        }
Exemplo n.º 3
0
        private void camera_select(object sender, EventArgs e)
        {
            progressBar1.Value = 0;
            int s = camera.SelectedIndex;

            if (s != Fitems.cameraN)
            {
                Fitems.init = false;
            }
            if ((Fitems.cameraN == s) && Fitems.init == true)
            {
                doneLoadingCamera(); return;
            }

            //   System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(fsdk_async_funcs));
            //  t.IsBackground = true;
            //  t.SetApartmentState(System.Threading.ApartmentState.STA);
            // t.Priority = System.Threading.ThreadPriority.BelowNormal;
            //  t.Start();
            backgroundWorker1.RunWorkerAsync();

            string cameraName = (string)camera.SelectedItem;


            int r = FSDKCam.OpenVideoCamera(ref cameraName, ref s);

            if (r != FSDK.FSDKE_OK)
            {
                MessageBox.Show("Error opening camera\nCamera Busy", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }
            int count;

            String[] cameraList;
            FSDKCam.GetCameraList(out cameraList, out count);

            FSDK.SetFaceDetectionParameters(Fitems.facerotationB, Fitems.faceAngle, Fitems.faceWidth);
            FSDK.SetFaceDetectionThreshold(Fitems.facethreshold);

            FSDKCam.VideoFormatInfo[] formatList;

            FSDKCam.GetVideoFormatList(ref cameraName, out formatList, out count);
            Fitems.CamX    = formatList[0].Width;
            Fitems.CamY    = formatList[0].Height;
            Fitems.cameraN = s;
            Application.DoEvents();
            Fitems.init = true;
        }
Exemplo n.º 4
0
 void _is_face_in_image(string filename)
 {
     try
     {
         //assuming that faces are vertical (handlearbitraryrotations=false) to speed up face detection
         FSDK.SetFaceDetectionParameters(false, true, 384);
         FSDK.SetFaceDetectionThreshold((int)(FaceDetectionThreshold));
         fr = new TFaceRecord();
         fr.ImageFileName  = filename;
         fr.FacePosition   = new FSDK.TFacePosition();
         fr.FacialFeatures = new FSDK.TPoint[1];
         fr.Template       = new Byte[FSDK.TemplateSize - 1];
         fr.image          = new FSDK.CImage(filename);
         fr.FacePosition   = fr.image.DetectFace();
         if (fr.FacePosition.w == 0)
         {
             MessageBox.Show("no face found, make sure you face the camera then try capturing image again...");
         }
         else
         {
             fr.faceImage = fr.image.CopyRect((int)(fr.FacePosition.xc - System.Math.Round(fr.FacePosition.w * 0.5)), (int)(fr.FacePosition.yc - System.Math.Round(fr.FacePosition.w * 0.5)), (int)(fr.FacePosition.xc + System.Math.Round(fr.FacePosition.w * 0.5)), (int)(fr.FacePosition.yc + System.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("Error detecting eyes during registration!!!");
             }
             if (eyesDetected)
             {
                 fr.Template = fr.image.GetFaceTemplateInRegion(ref fr.FacePosition);
             }
             //call savemethod here ->SAVE(fr);
             save_records(fr);
             FaceList = new List <TFaceRecord>();
             FaceList.Add(fr);
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("Can't open image(s) with error: " + ex.Message.ToString());
     }
 }
Exemplo n.º 5
0
        private bool EnrollFace(Face a_face)
        {
            //ASSUMING THAT FACES ARE VERTICAL (HANDLEARBITRARYROTATIONS IS FALSE) TO SPEED UP FACE DETECTION
            FSDK.SetFaceDetectionParameters(false, true, 384);
            FSDK.SetFaceDetectionThreshold((int)FaceDetectionThreshold);



            //GET POSITION OF FACE IN IMAGE
            a_face.face_position = FacePosition.FromFSDK(a_face.Clone().image.DetectFace());
            a_face.face_image    = a_face.Clone().image.CopyRect((int)(a_face.face_position.xc - Math.Round(a_face.face_position.w * 0.5)), (int)(a_face.face_position.yc - Math.Round(a_face.face_position.w * 0.5)), (int)(a_face.face_position.xc + Math.Round(a_face.face_position.w * 0.5)), (int)(a_face.face_position.yc + Math.Round(a_face.face_position.w * 0.5)));

            //GET THE FACIAL FEATURES OF THE FACE
            FSDK.TFacePosition face_pos = a_face.face_position;
            a_face.facial_features = a_face.Clone().image.DetectEyesInRegion(ref face_pos);

            //GET A TEMPLATE OF THE FACE TO BE USED FOR LATER COMPARISON
            a_face.face_template = a_face.Clone().image.GetFaceTemplateInRegion(ref face_pos);
            known_faces_list.Add(a_face);

            return(true);
        }
Exemplo n.º 6
0
        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");
                }
            }
        }
Exemplo n.º 7
0
        private void button1_Click(object sender, EventArgs e)
        {
            TFaceRecord fr = new TFaceRecord();

            FSDK.SetFaceDetectionParameters(false, true, 384);
            FSDK.SetFaceDetectionThreshold((int)FaceDetectionThreshold);

            fr.ImageFileName  = imgLoc;
            fr.FacePosition   = new FSDK.TFacePosition();
            fr.FacialFeatures = new FSDK.TPoint[2];
            fr.Template       = new byte[FSDK.TemplateSize];

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

            fr.FacePosition = fr.image.DetectFace();
            if (0 == fr.FacePosition.w)
            {
                if (imgLoc.Length <= 1)
                {
                    MessageBox.Show("No faces found. Try to lower the Minimal Face Quality parameter in the Options dialog box.", "Enrollment error");
                }
                else
                {
                }
            }
            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

                FaceList.Add(fr);
            }


            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);

            conn = new SqlCeConnection(stringCon);
            conn.Open();

            var cmd = new SqlCeCommand("insert into FaceList (ImageFileName,SubjectName,FacePositionXc,FacePositionYc,FacePositionW,FacePositionAngle,Eye1X,Eye1Y,Eye2X,Eye2Y,Template,Image,FaceImage) values (@IFName,@SName,@FPXc,@FPYc,@FPW,@FPA,@Eye1X,@Eye1Y,@Eye2X,@Eye2Y,@Template,@Image,@FaceImage)", conn);



            cmd.Parameters.Add(@"IFName", fr.ImageFileName);
            cmd.Parameters.Add(@"SName", textBox1.Text.Trim());
            cmd.Parameters.Add(@"FPXc", fr.FacePosition.xc);
            cmd.Parameters.Add(@"FPYc", fr.FacePosition.yc);
            cmd.Parameters.Add(@"FPW", fr.FacePosition.w);
            cmd.Parameters.Add(@"FPA", fr.FacePosition.angle);
            cmd.Parameters.Add(@"Eye1X", fr.FacialFeatures[0].x);
            cmd.Parameters.Add(@"Eye1Y", fr.FacialFeatures[0].y);
            cmd.Parameters.Add(@"Eye2X", fr.FacialFeatures[1].x);
            cmd.Parameters.Add(@"Eye2Y", fr.FacialFeatures[1].y);
            cmd.Parameters.Add(@"Template", fr.Template);
            cmd.Parameters.Add(@"Image", img_array);
            cmd.Parameters.Add(@"FaceImage", img_face_array);


            int x = cmd.ExecuteNonQuery();

            conn.Close();
            conn.Dispose();
            cmd.Dispose();
            MessageBox.Show(x.ToString() + "Image successfully added !!");
        }
Exemplo n.º 8
0
 private void trackBar1_Scroll(object sender, EventArgs e)
 {
     Fitems.facethreshold = trackBar1.Value;
     label5.Text          = "Face Detection Threshold =>" + Fitems.facethreshold + "\n Internal Resize Width =>" + Fitems.faceWidth;
     FSDK.SetFaceDetectionThreshold(Fitems.facethreshold);
 }
Exemplo n.º 9
0
        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length == 0)
            {
                MessageBox.Show("Please Enter the Subject Name");
                textBox1.Focus();
            }
            else
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter      = "JPEG (*.jpg)|*.jpg|Windows bitmap (*.bmp)|*.bmp|All files|*.*";
                dlg.Multiselect = true;

                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        FSDK.SetFaceDetectionParameters(false, true, 384);
                        FSDK.SetFaceDetectionThreshold((int)FaceDetectionThreshold);


                        foreach (string fn in dlg.FileNames)
                        {
                            FaceList.Clear();
                            TFaceRecord fr = new TFaceRecord();
                            fr.ImageFileName  = fn;
                            fr.suspectName    = textBox1.Text.Trim();
                            fr.FacePosition   = new FSDK.TFacePosition();
                            fr.FacialFeatures = new FSDK.TPoint[2];
                            fr.Template       = new byte[FSDK.TemplateSize];

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


                            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
                                {
                                }
                            }
                            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



                                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);
                            }

                            listView1.SelectedIndices.Clear();
                            // listView1.SelectedIndices.Add(listView1.Items.Count - 1);

                            db.SaveSubject(fr, Constants.conString);
                        }


                        listView1.Refresh();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Can't open image(s) with error: " + ex.Message.ToString(), "Error");
                    }
                }
            }
        }
Exemplo n.º 10
0
        public bool Recognize(ref string path, bool needCrop)
        {
            var AppDataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Abalone", "TestRotator");

            FaceRectRelative = RectangleF.Empty;
            LeftEyeCenter    = RightEyeCenter = LeftMouth = LeftNose = RightNose = RightMouth = Vector2.Zero;

            var executablePath = Path.GetDirectoryName(Application.ExecutablePath);

            FSDK.TPoint[] pointFeature;
            var           image = new FSDK.CImage(path);

            var faceRectangle  = Rectangle.Empty;
            var mouthRectangle = Rectangle.Empty;

            FSDK.SetFaceDetectionThreshold(5);
            FSDK.SetFaceDetectionParameters(true, true, 512);
            var facePosition = image.DetectFace();

            if (0 == facePosition.w)
            {
                MessageBox.Show("No faces detected", "Face Detection");
                return(false);
            }

            if (needCrop)
            {
                RotatedAngle = facePosition.angle;      // угол, на который повернута голова.
            }
            pointFeature = image.DetectFacialFeaturesInRegion(ref facePosition);

            var left = facePosition.xc - (int)(facePosition.w * 0.6f);

            left = left < 0 ? 0 : left;
            //   int top = facePosition.yc - (int)(facePosition.w * 0.5f);             // верхушку определяет неправильлно. поэтому просто не будем обрезать :)
            BottomFace = new Vector2(pointFeature[11].x, pointFeature[11].y);

            var distance = pointFeature[2].y - pointFeature[11].y;
            var top      = pointFeature[16].y + distance - 15;     // определение высоты по алгоритму старикана

            top = top < 0 ? 0 : top;

            var newWidth = (int)(facePosition.w * 1.2);

            newWidth = newWidth > image.Width || newWidth == 0 ? image.Width : newWidth;

            faceRectangle = new Rectangle(left, top, newWidth, BottomFace.Y + 15 < image.Height ? (int)(BottomFace.Y + 15) - top : image.Height - top - 1);
            if (needCrop)       // если это создание проекта - то нужно обрезать фотку и оставить только голову
            {
                var bmpImage = new Bitmap(path);

                using (var croppedImage = ImageEx.Crop(bmpImage, faceRectangle))
                {
                    path = AppDataDir;
                    FolderEx.CreateDirectory(path);
                    path = Path.Combine(path, "tempHaarImage.jpg");
                    croppedImage.Save(path, ImageFormat.Jpeg);

                    return(Recognize(ref path, false));
                }
            }

            LeftEyeCenter  = new Vector2(pointFeature[0].x, pointFeature[0].y);
            RightEyeCenter = new Vector2(pointFeature[1].x, pointFeature[1].y);

            LeftMouth  = new Vector2(pointFeature[3].x, pointFeature[3].y);
            RightMouth = new Vector2(pointFeature[4].x, pointFeature[4].y);

            LeftNose  = new Vector2(pointFeature[45].x, pointFeature[45].y);
            RightNose = new Vector2(pointFeature[46].x, pointFeature[46].y);

            TopFace     = new Vector2(pointFeature[66].x, pointFeature[66].y);
            MiddleFace1 = new Vector2(pointFeature[66].x, pointFeature[66].y);
            MiddleFace2 = new Vector2(pointFeature[5].x, pointFeature[5].y);


            RightMiddleFace1 = new Vector2(pointFeature[67].x, pointFeature[67].y);
            RightMiddleFace2 = new Vector2(pointFeature[6].x, pointFeature[6].y);

            #region Поворот фотки по глазам!

            var v = new Vector2(LeftEyeCenter.X - RightEyeCenter.X, LeftEyeCenter.Y - RightEyeCenter.Y);
            v.Normalize();      // ПД !
            var xVector = new Vector2(1, 0);

            var xDiff = xVector.X - v.X;
            var yDiff = xVector.Y - v.Y;
            var angle = Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI;

            if (Math.Abs(angle) > 1 && angleCount <= 5)                // поворачиваем наклоненные головы
            {
                ++angleCount;

                using (var ms = new MemoryStream(File.ReadAllBytes(path))) // Don't use using!!
                {
                    var originalImg = (Bitmap)Image.FromStream(ms);

                    path = AppDataDir;
                    FolderEx.CreateDirectory(path);
                    path = Path.Combine(path, "tempHaarImage.jpg");

                    using (var ii = ImageEx.RotateImage(new Bitmap(originalImg), (float)-angle))
                        ii.Save(path, ImageFormat.Jpeg);
                }

                return(Recognize(ref path, false));
            }

            #endregion

            var upperUpperLip = pointFeature[54];       // вехняя точка верхней губы
            var lowerUpperLip = pointFeature[61];       // нижняя точка верхней губы
            var lowerLip      = pointFeature[64];       // верхняя точка нижней губы

            var diff2 = Math.Abs(lowerUpperLip.y - upperUpperLip.y);
            var diffX = Math.Abs(lowerLip.y - lowerUpperLip.y);

            IsOpenSmile = diffX > diff2;            // экспериментальным путем выяснено, что улыбка на 90% открытая, если расстояние между верхней и нижней губой больше, чем толщина верхней губы

            #region Переводим в относительные координаты

            LeftMouth  = new Vector2(LeftMouth.X / (image.Width * 1f), LeftMouth.Y / (image.Height * 1f));
            RightMouth = new Vector2(RightMouth.X / (image.Width * 1f), RightMouth.Y / (image.Height * 1f));

            LeftEyeCenter  = new Vector2(LeftEyeCenter.X / (image.Width * 1f), LeftEyeCenter.Y / (image.Height * 1f));
            RightEyeCenter = new Vector2(RightEyeCenter.X / (image.Width * 1f), RightEyeCenter.Y / (image.Height * 1f));

            LeftNose  = new Vector2(LeftNose.X / (image.Width * 1f), LeftNose.Y / (image.Height * 1f));
            RightNose = new Vector2(RightNose.X / (image.Width * 1f), RightNose.Y / (image.Height * 1f));


            TopFace     = new Vector2(TopFace.X / (image.Width * 1f), TopFace.Y / (image.Height * 1f));
            MiddleFace1 = new Vector2(MiddleFace1.X / (image.Width * 1f), MiddleFace1.Y / (image.Height * 1f));
            MiddleFace2 = new Vector2(MiddleFace2.X / (image.Width * 1f), MiddleFace2.Y / (image.Height * 1f));
            BottomFace  = new Vector2(BottomFace.X / (image.Width * 1f), BottomFace.Y / (image.Height * 1f));

            RightMiddleFace1 = new Vector2(RightMiddleFace1.X / (image.Width * 1f), RightMiddleFace1.Y / (image.Height * 1f));
            RightMiddleFace2 = new Vector2(RightMiddleFace2.X / (image.Width * 1f), RightMiddleFace2.Y / (image.Height * 1f));

            FacialFeatures = new List <Vector3>();
            RealPoints     = new List <Vector2>();
            int index       = 0;
            var pointDepths = GetPointDepths();
            foreach (var point in pointFeature)
            {
                FacialFeatures.Add(new Vector3(point.x / (image.Width * 1f), point.y / (image.Height * 1f), pointDepths[index++]));
                RealPoints.Add(new Vector2(point.x, point.y));
            }

            ImageWidth  = image.Width;
            ImageHeight = image.Height;

            #endregion

            return(true);
        }
Exemplo n.º 11
0
        private void button1_Click(object sender, EventArgs e)
        {
            this.button1.Enabled = false;
            int cameraHandle = 0;

            int r = FSDKCam.OpenVideoCamera(ref cameraName, ref cameraHandle);

            if (r != FSDK.FSDKE_OK)
            {
                MessageBox.Show("Error opening the first camera", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }


            // set realtime face detection parameters
            FSDK.SetFaceDetectionParameters(false, false, 100);
            FSDK.SetFaceDetectionThreshold(3);

            // list where we store face templates
            // faceTemplates = new List();
            faceTemplates = new List <FaceTemplate>();


            while (!needClose)
            {
                // faceTemplates.Clear();

                Int32 imageHandle = 0;
                if (FSDK.FSDKE_OK != FSDKCam.GrabFrame(cameraHandle, ref imageHandle)) // grab the current frame from the camera
                {
                    Application.DoEvents();
                    continue;
                }

                FSDK.CImage image = new FSDK.CImage(imageHandle);

                Image    frameImage = image.ToCLRImage();
                Graphics gr         = Graphics.FromImage(frameImage);

                FSDK.TFacePosition facePosition = image.DetectFace();
                // if a face is detected, we can recognize it
                if (facePosition.w != 0)
                {
                    gr.DrawRectangle(Pens.LightGreen, facePosition.xc - facePosition.w / 2, facePosition.yc - facePosition.w / 2,
                                     facePosition.w, facePosition.w);

                    // create a new face template
                    FaceTemplate template = new FaceTemplate();

                    template.templateData = new byte[FSDK.TemplateSize];
                    FaceTemplate template1 = new FaceTemplate();
                    if (programState == ProgramState.psRemember || programState == ProgramState.psRecognize)
                    {
                        template.templateData = image.GetFaceTemplateInRegion(ref facePosition);
                    }


                    switch (programState)
                    {
                    case ProgramState.psNormal:     // normal state - do nothing
                        break;

                    case ProgramState.psRemember:     // Remember Me state - store facial templates

                        label1.Text = "Templates stored: " + faceTemplates.Count.ToString();
                        faceTemplates.Add(template);
                        if (faceTemplates.Count > 9)
                        {
                            // get the user name
                            InputName inputName = new InputName();
                            inputName.ShowDialog();
                            userName = inputName.userName;



                            cmd = new SqlCommand("insert into facetb values(@Name,@face)", con);
                            cmd.Parameters.AddWithValue("@Name", userName);

                            cmd.Parameters.AddWithValue("@face", template.templateData);

                            con.Open();
                            cmd.ExecuteNonQuery();
                            con.Close();
                            MessageBox.Show("Record Save!");

                            programState = ProgramState.psRecognize;
                        }
                        break;

                    case ProgramState.psRecognize:     // recognize the user
                        bool match = false;


                        con.Open();
                        cmd = new SqlCommand("select * from facetb ORDER BY id ASC ", con);
                        SqlDataReader dr = cmd.ExecuteReader();
                        while (dr.Read())
                        {
                            template1.templateData = (byte[])dr["face"];
                            faceTemplates.Add(template1);


                            strList.Add(dr["Name"].ToString());
                        }
                        con.Close();



                        int ii = 0;

                        foreach (FaceTemplate t in faceTemplates)
                        {
                            float        similarity = 0.0f;
                            FaceTemplate t1         = t;
                            FSDK.MatchFaces(ref template.templateData, ref t1.templateData, ref similarity);
                            float threshold = 0.0f;
                            FSDK.GetMatchingThresholdAtFAR(0.01f, ref threshold);     // set FAR to 1%
                            if (similarity > threshold)
                            {
                                userName = strList[ii].ToString();

                                label3.Text = strList[ii].ToString();
                                match       = true;
                                break;
                            }

                            ii++;
                        }

                        con.Close();



                        if (match)
                        {
                            StringFormat format = new StringFormat();
                            format.Alignment = StringAlignment.Center;

                            gr.DrawString(userName, new System.Drawing.Font("Arial", 16),
                                          new System.Drawing.SolidBrush(System.Drawing.Color.LightGreen),
                                          facePosition.xc, facePosition.yc + facePosition.w * 0.55f, format);
                            // abc = 0;
                            send();
                        }

                        else
                        {
                            abc         = 0;
                            label3.Text = "UnKnow FACE";
                        }
                        break;
                    }
                }

                // display current frame
                pictureBox1.Image = frameImage;

                GC.Collect(); // collect the garbage after the deletion

                // make UI controls accessible
                Application.DoEvents();
            }

            FSDKCam.CloseVideoCamera(cameraHandle);
            FSDKCam.FinalizeCapturing();
        }
Exemplo n.º 12
0
        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");
                }
            }
        }
Exemplo n.º 13
0
        public void recFace(object pppp)
        {
            Bitmap ppp = (Bitmap)pppp;

            if (label1.InvokeRequired)
            {
                label1.Invoke(new System.Threading.ParameterizedThreadStart(recFace), new object[] { pppp });
                return;
            }
            bool error = false;

            try
            {
                IntPtr ff = ppp.GetHbitmap();
                FSDK.LoadImageFromHBitmap(ref cimg, ff);
                FSDK.SetFaceDetectionParameters(false, false, 500);
                FSDK.SetFaceDetectionThreshold(1);

                FSDK.TFacePosition facePosition = new FSDK.TFacePosition();

                IntPtr hbitmapHandle = IntPtr.Zero;

                FSDK.SaveImageToHBitmap(cimg, ref hbitmapHandle);
                Image    ccimg = Image.FromHbitmap(hbitmapHandle);
                Graphics gr    = Graphics.FromImage(ccimg);

                if (FSDK.FSDKE_OK == FSDK.DetectFace(cimg, ref facePosition))
                {
                    gr.DrawRectangle(Pens.LightBlue, facePosition.xc - facePosition.w / 2, facePosition.yc - facePosition.w / 2,
                                     facePosition.w, facePosition.w);
                    byte[] tempd = new byte[FSDK.TemplateSize];
                    FSDK.GetFaceTemplateInRegion(cimg, ref facePosition, out tempd);
                    temp = System.Text.Encoding.ASCII.GetString(tempd);
                    if (check_reg(tempd))
                    {
                        timer2.Enabled = false;
                        StringFormat format = new StringFormat();
                        format.Alignment = StringAlignment.Center;
                        gr.DrawString(_name + " | " + _matric + " | " + _room, new System.Drawing.Font("Candara", 18),
                                      new System.Drawing.SolidBrush(System.Drawing.Color.LightBlue),
                                      facePosition.xc, facePosition.yc + facePosition.w * 0.55f, format);
                    }
                    else
                    {
                        timer2.Enabled = true;
                    }
                }
                else
                {
                    timer2.Enabled = false;
                    label1.Text    = "Please Focus on the camera";
                }
                pictureBox1.Height = ccimg.Height;
                pictureBox1.Width  = ccimg.Width;
                pictureBox1.Image  = ccimg;
                FSDK.FreeImage(cimg);
                DeleteObject(hbitmapHandle);
                GC.Collect();
                Application.DoEvents();
            }
            catch (NullReferenceException nul)
            {
                timer1.Enabled = false;
                timer1.Dispose();
                MessageBox.Show(nul.ToString());
                error = true;
            }
            catch (ExternalException exer)
            {
            }
            catch (AccessViolationException acs)
            {
            }
            finally
            {
                if (error)
                {
                    canClose = true;
                    Application.Exit();
                    Application.Restart();
                }
            }
        }
Exemplo n.º 14
0
        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)
                    {
                        string name = Path.GetFileNameWithoutExtension(fn);

                        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);

                        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
                            {
                                add(name + " face enroll failed.\nTry to lower the Minimal Face Quality parameter in the Options dialog box.");
                            }
                        }
                        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);
                            FaceName.Add(name);

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

                            add(name + " enrolled");
                            listView1.SelectedIndices.Clear();
                            listView1.SelectedIndices.Add(listView1.Items.Count - 1);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString(), "Exception");
                }
            }
        }