//private void button1_Click(object sender, EventArgs e)
        //{
        //    this.Dispose();
        //}

        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;


            float Threshold = 0.0f;

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

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

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

            for (int i = 0; i < Server.FaceList.Count; i++)
            {
                float Similarity = 0.0f;

                TFaceRecord CurrentFace = Server.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(Server.FaceList[Numbers[i]].faceImage.ToCLRImage());

                    listView1.Items.Add((Similarities[i] * 100.0f).ToString(System.Globalization.CultureInfo.InvariantCulture.NumberFormat),
                                        Server.FaceList[Numbers[i]].ImageFileName.Split('\\')[Server.FaceList[Numbers[i]].ImageFileName.Split('\\').Length - 1] +

                                        "\r\nSimilarity = " + (Similarities[i] * 100).ToString(), imageList1.Images.Count - 1);
                }
            }
            this.Show();
        }
Example #2
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");
                }
            }
        }
Example #3
0
        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");
                    }
                }
            }
        }
Example #4
0
        private void handleClients(object threadId)
        {
            int       clientId  = (int)threadId;
            TcpClient tcpClient = tcpClients[clientId];

            string remoteEndPoint = tcpClient.Client.RemoteEndPoint.ToString();
            string clientIp       = remoteEndPoint.Substring(0, remoteEndPoint.IndexOf(":"));
            string clientPort     = remoteEndPoint.Substring(remoteEndPoint.IndexOf(":") + 1);

            add("Client " + remoteEndPoint + " connected");

            //Client client = new Client("Client " + clientIp + " @ " + clientPort);
            //client = new Client("Client " + clientIp + " @ " + clientPort);
            // this.BeginInvoke((Action)(() => client.Show()));

            NetworkStream stream     = tcpClient.GetStream();
            string        clientData = string.Empty;

            Byte[] bytes = new Byte[256];
            int    i;
            bool   init = false;

            try
            {
                while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                {
                    if (!init)
                    {
                        clientData += Encoding.ASCII.GetString(bytes, 0, i);
                    }

                    if (clientData.Contains("<EOF>") && !init)
                    {
                        init = true;
                        string result = clientData.Substring(0, clientData.IndexOf("<EOF>"));
                        clientData = string.Empty;
                        byte[]       imageBytes = Convert.FromBase64String(result);
                        MemoryStream ms         = new MemoryStream(imageBytes, 0, imageBytes.Length);
                        ms.Position = 0;
                        Image img = Image.FromStream(ms);

                        //if (client == null || client.IsDisposed)
                        //{
                        //    client = new Client("Client " + clientIp + " @ " + clientPort);
                        //    this.BeginInvoke((Action)(() => client.Show()));
                        //}
                        //this.BeginInvoke((Action)(() => client.label1.Visible = false));
                        //this.BeginInvoke((Action)(() => client.pictureBox1.Image = img));

                        if (FaceList.Count == 0)
                        {
                            add("Please enroll faces first");
                            init = false;
                        }
                        else
                        {
                            TFaceRecord fr = new TFaceRecord();
                            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(img);
                            //img.Dispose();

                            fr.FacePosition = fr.image.DetectFace();

                            if (0 == fr.FacePosition.w)
                            {
                                add("No faces found. Try to lower the Minimal Face Quality parameter in the Options dialog box.");
                                init = false;
                            }
                            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
                                {
                                    add("Detecting eyes failed.");
                                    init = false;
                                }

                                if (eyesDetected)
                                {
                                    fr.Template = fr.image.GetFaceTemplateInRegion(ref fr.FacePosition); // get template with higher precision
                                    float Threshold = 0.0f;
                                    FSDK.GetMatchingThresholdAtFAR(FARValue / 100, ref Threshold);

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

                                    float[] Similarities = new float[FaceCount];

                                    for (int x = 0; x < FaceList.Count; x++)
                                    {
                                        float       Similarity  = 0.0f;
                                        TFaceRecord CurrentFace = FaceList[x];

                                        FSDK.MatchFaces(ref fr.Template, ref CurrentFace.Template, ref Similarity);

                                        if (Similarity >= Threshold)
                                        {
                                            Similarities[MatchedCount] = Similarity;
                                            ++MatchedCount;
                                        }
                                    }

                                    if (MatchedCount != 0)
                                    {
                                        float finalResult = Similarities.Max();

                                        if (finalResult * 100 < 95.0)
                                        {
                                            SendBackToClient(tcpClient, "R=NONE;");
                                            add("No morethan 95.0% matches found in database faces");
                                            //init = false;
                                        }
                                        else
                                        {
                                            int index = 0;
                                            for (int x = 0; x < MatchedCount; x++)
                                            {
                                                if (Similarities[x] == finalResult)
                                                {
                                                    index = x;
                                                }
                                            }
                                            SendBackToClient(tcpClient, "R=" + FaceName[index]);
                                            //init = false;
                                        }
                                    }
                                    else
                                    {
                                        SendBackToClient(tcpClient, "R=NONE;");
                                        add("No matches found. You can try to increase the FAR parameter in the Options dialog box.");
                                        //init = false;
                                    }
                                }
                                else
                                {
                                    add("No eyes detected in photos.");
                                    init = false;
                                }
                            }
                        }
                    }
                    else
                    {
                        if (clientData.Contains(";"))
                        {
                            string result = clientData.Substring(0, clientData.IndexOf(";"));
                            if (result == "OK")
                            {
                                init = false;
                            }
                        }
                    }
                }

                tcpClients.Remove(clientId);
                tcpClient.Client.Shutdown(SocketShutdown.Both);
                tcpClient.Client.Close();
                tcpClient.Client.Dispose();

                int idx = 0;
                for (int x = 0; x < threadClients.Count(); x++)
                {
                    if (threadClients[x].ManagedThreadId == clientId)
                    {
                        idx = x;
                    }
                }
                threadClients.RemoveAt(idx);

                add("Client " + remoteEndPoint + " disconnected");

                //if (client.Visible)
                //{
                //    this.BeginInvoke((Action)(() => client.Close()));
                //}
            }
            catch
            {
                add("Client " + remoteEndPoint + " error");
            }
        }