Example #1
0
        public InputName(TFaceRecord Photo)
        {
            InitializeComponent();

            RealPhoto = Photo;
            facePhoto = Photo.faceImage;
            Image frameImage = Photo.faceImage.ToCLRImage();

            picBox.Image = frameImage;
        }
Example #2
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            TFaceRecord fr = new TFaceRecord();

            fr = RealPhoto;
            fr.ImageFileName = "NA";

            fr.Fullname = txtName.Text.Trim();
            fr.Title    = comTitle.Text.Trim();
            fr.Gender   = comGender.Text.Trim();
            fr.ListType = "Open";

            if (Global.AutomaticTaining)
            {
                Global.Fullname = fr.Fullname;
                Global.Title    = fr.Title;
                Global.Gender   = fr.Gender;
                Global.ListType = "Open";
            }

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

            //fr.image = facePhoto;

            //fr.FacePosition = fr.image.DetectFace();
            if (0 == fr.FacePosition.w)
            {
                MessageBox.Show("No face found. Try to lower the Minimal Face Quality parameter in the Options dialog box.\r\n", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            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


                if (fx.AddPerson(fr))
                {
                    fx.GetPersons();
                    MessageBox.Show(fr.Fullname + " Has Been Added Successfully.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                }
            }



            this.Close();
        }
Example #3
0
        public void GetPerson(TFaceRecord faceIn, out string FaceFullName)
        {
            bool   ok    = false;
            string fName = "Unknown Face";

            if (Global.Persons == null)
            {
                FaceFullName = fName; return;
            }

            try
            {
                float Threshold = 0.034455f;
                //float Threshold = 0.001155f;

                SimilarFaces[]      Faces = null;
                SimilarFaces[]      faces = new SimilarFaces[Global.Persons.Length];
                List <SimilarFaces> Fa    = new List <SimilarFaces>();
                int i = -1;

                foreach (TFaceRecord fac in Global.Persons)
                {
                    TFaceRecord F = new TFaceRecord();
                    F = fac;

                    float Similarity = 0.0f;
                    FSDK.MatchFaces(ref faceIn.Template, ref F.Template, ref Similarity);
                    if (Similarity >= Threshold)
                    {
                        i++;
                        faces[i].FullName   = fac.Fullname;
                        faces[i].Similarity = Similarity;
                    }
                }


                if (faces != null)
                {
                    Faces = faces.Where(v => v.FullName != null).ToArray();

                    fName = GetMostAcurateFace(Faces);
                    //fName = SortSimilarFaces(Faces)[0].FullName;
                }
            }
            catch (Exception n)
            {
            }

            FaceFullName = fName;
            //return ok;
        }
Example #4
0
        public bool GetPerson(TFaceRecord faceIn, out TFaceRecord faceOut)
        {
            bool ok = false;

            TFaceRecord f = new TFaceRecord();

            if (Global.Persons == null)
            {
                faceOut = f; return(ok);
            }

            try
            {
                float Threshold = 0.001155f;
                //FSDK.GetMatchingThresholdAtFAR(0.0009f, ref Threshold);

                List <float> sFaces = new List <float>();

                SimilarFaces[] Faces = null;
                SimilarFaces[] faces = new SimilarFaces[Global.Persons.Length];
                int            i     = -1;

                foreach (TFaceRecord fac in Global.Persons.AsParallel())
                {
                    TFaceRecord F = new TFaceRecord();
                    F = fac;

                    float Similarity = 0.0f; //0.000026273356E == 0.00228634826
                    FSDK.MatchFaces(ref faceIn.Template, ref F.Template, ref Similarity);
                    if (Similarity >= Threshold)
                    {
                        i++;
                        faces[i].FullName   = fac.Fullname;
                        faces[i].Similarity = Similarity;

                        ok = true;
                        f  = fac;
                        break;
                    }
                }

                //Faces.OrderByDescending<SimilarFaces,>

                //Parallel.ForEach(Global.Persons.AsParallel(), fac =>
                // {

                //     float Similarity = 0.0f;
                //     FSDK.MatchFaces(ref faceIn.Template, ref fac.Template, ref Similarity);
                //     if (Similarity >= Threshold)
                //     {
                //         ok = true;
                //         f = fac;
                //     }

                // });
            }
            catch (Exception n)
            {
            }

            faceOut = f;
            return(ok);
        }
Example #5
0
        public TFaceRecord[] GetPersons()
        {
            TFaceRecord[] faces = null;


            using (OleDbConnection cn = new OleDbConnection())
            {
                try
                {
                    cn.ConnectionString = cnString;
                    cn.Open();

                    string           sql = "SELECT * FROM PersonTable";
                    OleDbCommand     cmd = new OleDbCommand(sql, cn);
                    OleDbDataAdapter da  = new OleDbDataAdapter(cmd);

                    DataTable table = new DataTable();
                    da.Fill(table);


                    if (table.Rows.Count > 0)
                    {
                        faces = new TFaceRecord[table.Rows.Count];
                        int i = -1;

                        foreach (DataRow row in table.Rows)
                        {
                            TFaceRecord f = new TFaceRecord();

                            f.Fullname = row.ItemArray[1].ToString();
                            f.Title    = row.ItemArray[2].ToString();
                            f.Gender   = row.ItemArray[3].ToString();
                            f.ListType = row.ItemArray[4].ToString();

                            f.ImageFileName = row.ItemArray[5].ToString();

                            f.FacePosition       = new FSDK.TFacePosition();
                            f.FacePosition.xc    = Convert.ToInt32(row.ItemArray[6].ToString());
                            f.FacePosition.yc    = Convert.ToInt32(row.ItemArray[7].ToString());
                            f.FacePosition.w     = Convert.ToInt32(row.ItemArray[8].ToString());
                            f.FacePosition.angle = Convert.ToDouble(row.ItemArray[9].ToString());

                            f.FacialFeatures = new FSDK.TPoint[2];

                            f.FacialFeatures[0]   = new FSDK.TPoint();
                            f.FacialFeatures[0].x = Convert.ToInt32(row.ItemArray[10].ToString());
                            f.FacialFeatures[0].y = Convert.ToInt32(row.ItemArray[11].ToString());

                            f.FacialFeatures[1]   = new FSDK.TPoint();
                            f.FacialFeatures[1].x = Convert.ToInt32(row.ItemArray[12].ToString());
                            f.FacialFeatures[1].y = Convert.ToInt32(row.ItemArray[13].ToString());

                            f.Template = Convert.FromBase64String(row.ItemArray[14].ToString());

                            Image img = Base64ToImage(row.ItemArray[15].ToString());
                            f.image = new FSDK.CImage(img);

                            Image fImg = Base64ToImage(row.ItemArray[16].ToString());
                            f.faceImage = new FSDK.CImage(fImg);

                            i++;
                            faces[i] = f;
                        }

                        Global.Persons = faces;
                    }
                }
                catch (Exception n)
                {
                }
            }

            return(faces);
        }
Example #6
0
        public bool AddPerson(TFaceRecord fr)
        {
            bool ok = true;


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


                OleDbConnection cn = new OleDbConnection();

                try
                {
                    cn.ConnectionString = cnString;
                    cn.Open();

                    //string sqlStr = "INSERT INTO PersonTable(Fname, Title, Gender, ListType, ImageFileName, FacePositionXc, FacePositionYc, FacePositionW, FacePositionAngle, Eye1X, Eye1Y, Eye2X, Eye2Y, Template, Image, FaceImage) Values(@Fname, @Title, @Gender, @ListType, @ImageFileName, @FacePositionXc, @FacePositionYc, @FacePositionW, @FacePositionAngle, @Eye1X, @Eye1Y, @Eye2X, @Eye2Y, @Template, @Image, @FaceImage)";
                    string       sqlStr = "INSERT INTO PersonTable(Fname, Title, Gender, ListType, ImageFileName, FacePositionXc, FacePositionYc, FacePositionW, FacePositionAngle, Eye1X, Eye1Y, Eye2X, Eye2Y, Template, Imaged, FaceImage) Values(@Fname, @Title, @Gender, @ListType, @ImageFileName, @FacePositionXc, @FacePositionYc, @FacePositionW, @FacePositionAngle, @Eye1X, @Eye1Y, @Eye2X, @Eye2Y, @Template, @Image, @FaceImage)";
                    OleDbCommand oCmd   = new OleDbCommand(sqlStr, cn);

                    oCmd.Parameters.Add("@Fname", OleDbType.VarChar);
                    oCmd.Parameters.Add("@Title", OleDbType.VarChar);
                    oCmd.Parameters.Add("@Gender", OleDbType.VarChar);
                    oCmd.Parameters.Add("@ListType", OleDbType.VarChar);

                    oCmd.Parameters.Add("@ImageFileName", OleDbType.VarChar);
                    oCmd.Parameters.Add("@FacePositionXc", OleDbType.Integer);
                    oCmd.Parameters.Add("@FacePositionYc", OleDbType.Integer);
                    oCmd.Parameters.Add("@FacePositionW", OleDbType.Integer);
                    oCmd.Parameters.Add("@FacePositionAngle", OleDbType.Single);

                    oCmd.Parameters.Add("@Eye1X", OleDbType.Integer);
                    oCmd.Parameters.Add("@Eye1Y", OleDbType.Integer);
                    oCmd.Parameters.Add("@Eye2X", OleDbType.Integer);
                    oCmd.Parameters.Add("@Eye2Y", OleDbType.Integer);

                    oCmd.Parameters.Add("@Template", OleDbType.VarChar);
                    oCmd.Parameters.Add("@Image", OleDbType.VarChar);
                    oCmd.Parameters.Add("@FaceImage", OleDbType.VarChar);


                    /*
                     *                                      oCmd.Parameters.Add("@Template", OleDbType.VarBinary);
                     *                  oCmd.Parameters.Add("@Image", OleDbType.VarBinary);
                     *                  oCmd.Parameters.Add("@FaceImage", OleDbType.VarBinary);
                     *
                     */
                    oCmd.Parameters["@Fname"].Value    = fr.Fullname;
                    oCmd.Parameters["@Title"].Value    = fr.Title;
                    oCmd.Parameters["@Gender"].Value   = fr.Gender;
                    oCmd.Parameters["@ListType"].Value = fr.ListType;

                    oCmd.Parameters["@ImageFileName"].Value     = fr.ImageFileName;
                    oCmd.Parameters["@FacePositionXc"].Value    = fr.FacePosition.xc;
                    oCmd.Parameters["@FacePositionYc"].Value    = fr.FacePosition.yc;
                    oCmd.Parameters["@FacePositionW"].Value     = fr.FacePosition.w;
                    oCmd.Parameters["@FacePositionAngle"].Value = (float)fr.FacePosition.angle;

                    oCmd.Parameters["@Eye1X"].Value = fr.FacialFeatures[0].x;
                    oCmd.Parameters["@Eye1Y"].Value = fr.FacialFeatures[0].y;
                    oCmd.Parameters["@Eye2X"].Value = fr.FacialFeatures[1].x;
                    oCmd.Parameters["@Eye2Y"].Value = fr.FacialFeatures[1].y;


                    string template = Convert.ToBase64String(fr.Template);
                    oCmd.Parameters["@Template"].Value = template;

                    string image = Convert.ToBase64String(img_array);
                    oCmd.Parameters["@Image"].Value = image;

                    string faceImage = Convert.ToBase64String(img_face_array);
                    oCmd.Parameters["@FaceImage"].Value = faceImage;

                    int iresult = oCmd.ExecuteNonQuery();
                    cn.Close();
                    cn.Open();
                    string       sqlString_student = "INSERT INTO students(regno,fullname,gender,level) VALUES('" + fr.Title + "','" + fr.Fullname + "','" + fr.Gender + "','" + fr.course + "')";
                    OleDbCommand cmds = new OleDbCommand(sqlString_student, cn);
                    cmds.ExecuteNonQuery();
                    cn.Close();

                    img.Dispose();
                    img_face.Dispose();
                }
                catch (Exception ex)
                {
                    ok = false;
                    MessageBox.Show(ex.Message, "Exception on saving to database");
                }
                finally
                {
                    cn.Close();
                }
            }catch (Exception n)
            {
                ok = false;
                MessageBox.Show(n.Message);
            }


            return(ok);
        }
Example #7
0
        private void StartStreamingAndDetecting()
        {
            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();
            }

            try
            {
                int tracker = 0;                                                                     // creating a Tracker
                if (FSDK.FSDKE_OK != FSDK.LoadTrackerMemoryFromFile(ref tracker, TrackerMemoryFile)) // try to load saved tracker state
                {
                    FSDK.CreateTracker(ref tracker);                                                 // if could not be loaded, create a new tracker
                }
                int err = 0;                                                                         // set realtime face detection parameters
                FSDK.SetTrackerMultipleParameters(tracker, "HandleArbitraryRotations=false; DetermineFaceRotationAngle=true; InternalResizeWidth=100; FaceDetectionThreshold=1;", ref err);
                FSDK.SetFaceDetectionParameters(false, true, 384);

                int FrameCounter = 0;
                Pen P            = new Pen(Color.AliceBlue);
                int L            = 0;
                int T            = 0;
                int W            = 0;

                string[] Names = new string[10];

                while (!needClose)
                {
                    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);

                    long[] IDs;
                    long   faceCount = 0;
                    FSDK.FeedFrame(tracker, cameraHandle, image.ImageHandle, ref faceCount, out IDs, sizeof(long) * 256); // maximum of 256 faces detected
                    Array.Resize(ref IDs, (int)faceCount);


                    // make UI controls accessible (to find if the user clicked on a face)
                    Application.DoEvents();

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


                    for (int i = 0; i < IDs.Length; ++i)
                    {
                        string FaceFullName = "Unknown Face";
//                        Name = "";
                        if (FaceFullName == "Unknown Face")
                        {
                            FSDK.TFacePosition facePosition = new FSDK.TFacePosition();
                            FSDK.GetTrackerFacePosition(tracker, cameraHandle, IDs[i], ref facePosition);

                            int left = facePosition.xc - (int)(facePosition.w * 0.6);
                            int top  = facePosition.yc - (int)(facePosition.w * 0.5);
                            int w    = (int)(facePosition.w * 1.2);

                            string name;

                            TFaceRecord fIn = new TFaceRecord();

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

                            FSDK.CImage img = new FSDK.CImage(image.ImageHandle);
                            img = image.CopyRect((int)(facePosition.xc - Math.Round(facePosition.w * 0.5)), (int)(facePosition.yc - Math.Round(facePosition.w * 0.5)), (int)(facePosition.xc + Math.Round(facePosition.w * 0.5)), (int)(facePosition.yc + Math.Round(facePosition.w * 0.5)));

                            fIn.image        = img;
                            fIn.FacePosition = fIn.image.DetectFace();

                            fIn.faceImage      = fIn.image.CopyRect((int)(fIn.FacePosition.xc - Math.Round(fIn.FacePosition.w * 0.5)), (int)(fIn.FacePosition.yc - Math.Round(fIn.FacePosition.w * 0.5)), (int)(fIn.FacePosition.xc + Math.Round(fIn.FacePosition.w * 0.5)), (int)(fIn.FacePosition.yc + Math.Round(fIn.FacePosition.w * 0.5)));
                            fIn.FacialFeatures = fIn.image.DetectEyesInRegion(ref fIn.FacePosition);
                            fIn.Template       = fIn.image.GetFaceTemplateInRegion(ref fIn.FacePosition); // get template with higher precision


                            //TFaceRecord fOut = new TFaceRecord();
                            //string FaceFullName = "Unknown Face";

                            //if (FrameCounter < 9)
                            //{
                            //    FrameCounter++;

                            //    StringFormat format = new StringFormat();
                            //    format.Alignment = StringAlignment.Center;

                            //    gr.DrawString("Searching ...", new Font("Arial", 16), new SolidBrush(Color.LightGreen), facePosition.xc, top + w + 5, format);

                            //    Fx.GetPerson(fIn, out FaceFullName);
                            //    Names[FrameCounter] = FaceFullName;
                            //    name = FaceFullName;

                            //}
                            //else
                            //{

                            //FrameCounter++;
                            Fx.GetPerson(fIn, out FaceFullName);
                            Names[FrameCounter] = FaceFullName;
                            name = FaceFullName;
                            if (FaceFullName != "")
                            {
                                IntruderFullName = FaceFullName;
                            }

                            FrameCounter = 0;


                            /*/ draw name
                             * name = (from n in Names
                             *      group n by n into g
                             *      select new
                             *      {
                             *          Key = g.Key,
                             *          Count = g.Count()
                             *      }).OrderByDescending(g => g.Count).ToArray()[0].ToString();
                             */

                            StringFormat format = new StringFormat();
                            format.Alignment = StringAlignment.Center;

                            gr.DrawString(name, new Font("Arial", 16), new SolidBrush(Color.LightGreen), facePosition.xc, top + w + 5, format);

                            Pen pen = Pens.LightGreen;
                            gr.DrawRectangle(pen, left, top, w, w);


                            if (mouseX >= left && mouseX <= left + w && mouseY >= top && mouseY <= top + w)
                            {
                                pen = Pens.Blue;
                            }

                            P = pen;
                            L = left;
                            T = top;
                            W = w;

                            gr.DrawRectangle(pen, left, top, w, w);
                            if (FaceFullName == "Unknown Face")
                            {
                                un++;
                                if (un >= 20)
                                {
                                    //MessageBox.Show("I don't kno him");
                                }
                            }
                            else
                            {
                                kno++;
                                if (kno >= 5)
                                {
                                    //MessageBox.Show("i know him");

                                    //Get detected student details
                                    var d_student = db.Students.Where(p => p.fullname == Name).FirstOrDefault();
                                    var att       = db.Attendances.Where(p => p.fullname == name && p.insertdate > DateTime.Now.AddDays(-1));
                                    if (att.Count() == 0)
                                    {
                                        //save to attedance
                                        db.Attendances.Add(
                                            new Attendance
                                        {
                                            // regno =
                                            regno      = d_student.regno,
                                            fullname   = d_student.fullname,
                                            gender     = d_student.gender,
                                            level      = d_student.level,
                                            insertdate = DateTime.Now
                                        });
                                        db.SaveChanges();
                                    }
                                }
                            }

                            if (MakingCall == false)
                            {
                                MakingCall = true;
                                // timer.Tick += timer_Tick;
                                //timer.Interval = 30000000;
                                // timer.Start();
                                //MessageBox.Show("UNKNOWN");
                                Thread.Sleep(5000);
                                TwilioClass call = new TwilioClass();
                                if (call.MakeCall(IntruderFullName))
                                {
                                    MakingCall = false;
                                }
                            }
                            //}
                            //gr.DrawRectangle(P, L, T, W, W);
                            name             = "";
                            IntruderFullName = "";
                        }
                    }

                    //programState = ProgramState.psRecognize;

                    // display current frame
                    imageBox.Image = frameImage;
                    GC.Collect(); // collect the garbage after the deletion
                }

                FSDKCam.CloseVideoCamera(cameraHandle);
                FSDKCam.FinalizeCapturing();
            }
            catch (Exception n)
            {
                StartStreamingAndDetecting();
            }
        }
Example #8
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            this.btnStart.Enabled = false;

            //int FSDK.SetHTTPProxy("192.168.43.1:8080", 8080, "", "");
            //int r = FSDKCam.OpenIPVideoCamera(FSDKCam.FSDK_VIDEOCOMPRESSIONTYPE.FSDK_MJPEG, "192.168.43.1", "", "", 60, ref cameraHandle);
            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();
            }

            try
            {
                int tracker = 0;                                                                     // creating a Tracker
                if (FSDK.FSDKE_OK != FSDK.LoadTrackerMemoryFromFile(ref tracker, TrackerMemoryFile)) // try to load saved tracker state
                {
                    FSDK.CreateTracker(ref tracker);                                                 // if could not be loaded, create a new tracker
                }
                int err = 0;                                                                         // set realtime face detection parameters
                FSDK.SetTrackerMultipleParameters(tracker, "HandleArbitraryRotations=false; DetermineFaceRotationAngle=true; InternalResizeWidth=100; FaceDetectionThreshold=1;", ref err);
                FSDK.SetFaceDetectionParameters(false, true, 384);

                while (!needClose)
                {
                    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);

                    long[] IDs;
                    long   faceCount = 0;
                    FSDK.FeedFrame(tracker, cameraHandle, image.ImageHandle, ref faceCount, out IDs, sizeof(long) * 256); // maximum of 256 faces detected
                    Array.Resize(ref IDs, (int)faceCount);


                    // make UI controls accessible (to find if the user clicked on a face)
                    Application.DoEvents();

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

                    for (int i = 0; i < IDs.Length; ++i)
                    {
                        FSDK.TFacePosition facePosition = new FSDK.TFacePosition();
                        FSDK.GetTrackerFacePosition(tracker, cameraHandle, IDs[i], ref facePosition);

                        int left = facePosition.xc - (int)(facePosition.w * 0.6);
                        int top  = facePosition.yc - (int)(facePosition.w * 0.5);
                        int w    = (int)(facePosition.w * 1.2);

                        string name;

                        TFaceRecord fIn = new TFaceRecord();

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

                        FSDK.CImage img = new FSDK.CImage(image.ImageHandle);
                        img = image.CopyRect((int)(facePosition.xc - Math.Round(facePosition.w * 0.5)), (int)(facePosition.yc - Math.Round(facePosition.w * 0.5)), (int)(facePosition.xc + Math.Round(facePosition.w * 0.5)), (int)(facePosition.yc + Math.Round(facePosition.w * 0.5)));

                        fIn.image        = img;
                        fIn.FacePosition = fIn.image.DetectFace();

                        fIn.faceImage      = fIn.image.CopyRect((int)(fIn.FacePosition.xc - Math.Round(fIn.FacePosition.w * 0.5)), (int)(fIn.FacePosition.yc - Math.Round(fIn.FacePosition.w * 0.5)), (int)(fIn.FacePosition.xc + Math.Round(fIn.FacePosition.w * 0.5)), (int)(fIn.FacePosition.yc + Math.Round(fIn.FacePosition.w * 0.5)));
                        fIn.FacialFeatures = fIn.image.DetectEyesInRegion(ref fIn.FacePosition);
                        fIn.Template       = fIn.image.GetFaceTemplateInRegion(ref fIn.FacePosition); // get template with higher precision


                        TFaceRecord fOut = new TFaceRecord();
                        //if (Fx.GetPerson(fIn, out fOut))
                        //{
                        //name = fOut.Fullname;

                        // draw name
                        //StringFormat format = new StringFormat();
                        //format.Alignment = StringAlignment.Center;

                        //lblPersons.Text = name;
                        // gr.DrawString(name, new Font("Arial", 16), new SolidBrush(Color.LightGreen), facePosition.xc, top + w + 5, format);

                        //}
                        //else
                        //{
                        //lblPersons.Text = "";

                        if (0 != fIn.FacePosition.w)
                        {
                            //img = new FSDK.CImage(image.ImageHandle);
                            //img = fIn.image.CopyRect((int)(facePosition.xc - Math.Round(facePosition.w * 0.5)), (int)(facePosition.yc - Math.Round(facePosition.w * 0.5)), (int)(facePosition.xc + Math.Round(facePosition.w * 0.5)), (int)(facePosition.yc + Math.Round(facePosition.w * 0.5)));
                            if (Global.AutomaticTaining)
                            {
                                InputName inputName = new InputName(fIn);
                                inputName.AutoSave();
                                userName = inputName.userName;
                            }
                            else
                            {
                                InputName inputName = new InputName(fIn);
                                if (DialogResult.OK == inputName.ShowDialog(this))
                                {
                                    userName = inputName.userName;
                                    if (userName == null || userName.Length <= 0)
                                    {
                                        String s = "";
                                    }
                                    else
                                    {
                                    }
                                }
                            }
                            //}
                        }

                        //int res = FSDK.GetAllNames(tracker, IDs[i], out name, 65536); // maximum of 65536 characters

                        /*/if (FSDK.FSDKE_OK == res && name.Length > 0)
                         * if (name.Length > 0)
                         * {
                         *  // draw name
                         *  //name = "Prince Daniel";
                         *  StringFormat format = new StringFormat();
                         *  format.Alignment = StringAlignment.Center;
                         *
                         *  gr.DrawString(name, new Font("Arial", 16), new SolidBrush(Color.LightGreen), facePosition.xc, top + w + 5, format);
                         * }
                         *
                         */

                        Pen pen = Pens.LightGreen;
                        if (mouseX >= left && mouseX <= left + w && mouseY >= top && mouseY <= top + w)
                        {
                            pen = Pens.Blue;
                            //programState = ProgramState.psRemember;
                            if (ProgramState.psRemember == programState)
                            {
                                //FSDK.CImage img = new FSDK.CImage(image.ImageHandle);
                                img = new FSDK.CImage(image.ImageHandle);
                                img = image.CopyRect((int)(facePosition.xc - Math.Round(facePosition.w * 0.5)), (int)(facePosition.yc - Math.Round(facePosition.w * 0.5)), (int)(facePosition.xc + Math.Round(facePosition.w * 0.5)), (int)(facePosition.yc + Math.Round(facePosition.w * 0.5)));

                                if (Global.AutomaticTaining)
                                {
                                    InputName inputName = new InputName(img);
                                    inputName.AutoSave();
                                    userName = inputName.userName;
                                }
                                else
                                {
                                    InputName inputName = new InputName(img);
                                    if (DialogResult.OK == inputName.ShowDialog(this))
                                    {
                                        userName = inputName.userName;
                                        if (userName == null || userName.Length <= 0)
                                        {
                                            String s = "";
                                        }
                                        else
                                        {
                                        }
                                    }
                                }
                            }
                        }


                        gr.DrawRectangle(pen, left, top, w, w);
                    }

                    programState = ProgramState.psRecognize;

                    // display current frame
                    imageBox.Image = frameImage;
                    GC.Collect(); // collect the garbage after the deletion
                }

                FSDKCam.CloseVideoCamera(cameraHandle);
                FSDKCam.FinalizeCapturing();
            }
            catch (Exception n)
            {
                Application.DoEvents();
            }
        }