private int recongnize(FaceTemplate template)
        {
            bool match = false;

            int i;

            for (i = 0; i < UserTemplates.Count; i++)
            {
                foreach (FaceTemplate t in UserTemplates[i])
                {
                    float        Siminarity = 0.0f;
                    FaceTemplate t1         = t;
                    FSDK.MatchFaces(ref template.templateData, ref t1.templateData, ref Siminarity);
                    float threashold = 0.0f;
                    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    FSDK.GetMatchingThresholdAtFAR(0.25f, ref threashold);
                    //0.1表示程序误认为不同的人为检测对象的几率是0.1   该取值范围0~1
                    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    if (Siminarity > threashold)
                    {
                        match = true;
                        break;
                    }
                }
                if (match)
                {
                    return(i);
                }
            }
            return(-1);
        }
Exemple #2
0
 public void IndsertFaceTemplate(FaceTemplate faceTemplate)
 {
     if (this._faceCompreaCore != null)
     {
         this._faceCompreaCore.AddFaceTemplate(faceTemplate);
     }
 }
        private int recongnize(FaceTemplate template)
        {
            bool match = false;

            int i;
            for (i = 0; i < UserTemplates.Count; i++)
            {
                foreach (FaceTemplate t in UserTemplates[i])
                {
                    float Siminarity = 0.0f;
                    FaceTemplate t1 = t;
                    FSDK.MatchFaces(ref template.templateData, ref t1.templateData, ref Siminarity);
                    float threashold = 0.0f;
                    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    FSDK.GetMatchingThresholdAtFAR(0.25f, ref threashold);
                    //0.1表示程序误认为不同的人为检测对象的几率是0.1   该取值范围0~1
                    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    if (Siminarity > threashold)
                    {
                        match = true;
                        break;
                    }
                }
                if (match)
                {
                    return i;
                }

            }
            return -1;
        }
        int Recongnizehandle()
        {

#if TIME_DEBUG
            label2.Text = "Stopwatch REC start";
            stopwatch.Reset();
            stopwatch.Start();
#endif
            faceTemplates.Clear();
            //label1.Text = "Recongnize start";
            int maxbear = 0;
            int minbear = 0;
            reset();
            recperson = -1;
            for (int count = 0; count < REPEAT_REC; count++)
            {
                Int32 ImageHandle = 0;
                if (FSDK.FSDKE_OK != FSDKCam.GrabFrame(CameraHandle, ref ImageHandle))
                {
                    return -1;
                }
                FSDK.CImage Image = new FSDK.CImage(ImageHandle);
                Image FrameImage = Image.ToCLRImage();
                gr = Graphics.FromImage(FrameImage);

                FSDK.TFacePosition[] FacePosition = Image.DetectMultipleFaces();
                if (FacePosition.Length == 0)
                {
                    minbear++;
                    //无人脸状态超出忍受值
                    if (minbear > MAX_MINBEAR)
                    {
                        recperson = -2;
#if TIME_DEBUG
                        stopwatch.Stop();
                        TimeSpan timespan2 = stopwatch.Elapsed;
                        double milliseconds2 = timespan2.TotalMilliseconds;  //  总毫秒数
                        label2.Text = milliseconds2.ToString() + " .REM";
#endif
                        return 1;
                    }
                    count--;
                    continue;
                }
                if (FacePosition.Length != 1)
                {
                    maxbear++;
                    //多人脸状态超出忍受值
                    if (maxbear > MAX_MAXBEAR)
                    {
                        recperson = -3;
                        return 1;
                    }
                    count--;
                    continue;
                }

                //draw(FacePosition[0]);

                FaceTemplate Template = new FaceTemplate();
                FSDK.TPoint[] features = Image.DetectEyesInRegion(ref FacePosition[0]);

                Template.templateData = Image.GetFaceTemplateUsingEyes(ref features);
                
                int recnum = recongnize(Template);
                faceTemplates.Add(Template);

                who[count] = recnum;

                if (recnum != -1)
                {
                    StringFormat format = new StringFormat();
                    format.Alignment = StringAlignment.Center;
                    gr.DrawString(userName[recnum], new System.Drawing.Font("Arial", 16),
                                                        new System.Drawing.SolidBrush(System.Drawing.Color.LightGreen),
                                                        FacePosition[0].xc, FacePosition[0].yc + FacePosition[0].w * 0.55f, format);
                }
                Application.DoEvents();
            }

            recperson = vote();
            //label1.Text = "Recongnize the man";

            if (recperson > -1)
            {
                List<FaceTemplate> temp = new List<FaceTemplate>(faceTemplates.ToArray());
                int i;
                for (i = 0; i < temp.Count; i++)
                {
                    UserTemplates[recperson].Add(temp[i]);
                }
            }

#if TIME_DEBUG
            stopwatch.Stop();
            TimeSpan timespan = stopwatch.Elapsed;
            double milliseconds = timespan.TotalMilliseconds;  //  总毫秒数
            label2.Text = milliseconds.ToString() + " .REC";
#endif
            return 0;
        }
        int AddFacehandle()
        {

#if TIME_DEBUG
            label2.Text = "Stopwatch REC start";
            stopwatch.Reset();
            stopwatch.Start();
#endif
            faceTemplates.Clear();
            int maxbear = 0;
            int minbear = 0;
            //label1.Text = "Adding new one";
            for (int count = 0; count < REPEAT_REM; count++)
            {
                Int32 ImageHandle = 0;
                if (FSDK.FSDKE_OK != FSDKCam.GrabFrame(CameraHandle, ref ImageHandle))
                {
                    return -1;
                }
                FSDK.CImage Image = new FSDK.CImage(ImageHandle);
                Image FrameImage = Image.ToCLRImage();
                gr = Graphics.FromImage(FrameImage);

                FSDK.TFacePosition[] FacePosition = Image.DetectMultipleFaces();
                //只允许有一个人在检测范围内
                if (FacePosition.Length == 0) 
                {
                    minbear++;
                    //无人脸状态超出忍受值
                    if (minbear > MAX_MINBEAR)
                    {
#if TIME_DEBUG
                        stopwatch.Stop();
                        TimeSpan timespan2 = stopwatch.Elapsed;
                        double milliseconds2 = timespan2.TotalMilliseconds;  //  总毫秒数
                        label2.Text = milliseconds2.ToString() + " .REM";
#endif
                        faceTemplates.Clear();
                        username = "";
                        //label1.Text = "no people";
                        recperson = -2;
                        return -1;
                    }
                    count--;
                    continue;
                }
                if (FacePosition.Length != 1) 
                {
                    maxbear++;
                    //多人脸状态超出忍受值
                    if (maxbear > MAX_MAXBEAR)
                    {
                        faceTemplates.Clear();
                        username = "";
                        //label1.Text = "Too many people";
                        recperson = -3;
                        return -1;
                    }
                    count--;
                    continue;
                }


                //draw(FacePosition[0]);
                FaceTemplate Template = new FaceTemplate();
                FSDK.TPoint[] features = Image.DetectEyesInRegion(ref FacePosition[0]);
                Template.templateData = Image.GetFaceTemplateUsingEyes(ref features);

                faceTemplates.Add(Template);
                Application.DoEvents();
            }
            if (AddNewOne)
            {
                if (btn1Click)
                {
                    btn1Click = false;
                    int i = 0;
                    for (i = 0; i < REPEAT_REM; i++)
                    {
                        int fnum = btn1Count * REPEAT_REM + i;
                        string tpath = ".\\Users\\" + btn1UserName + fnum.ToString() + ".dat";
                        MemoryStream m = new MemoryStream(faceTemplates[i].templateData);
                        FileStream fs = new FileStream(tpath, FileMode.OpenOrCreate);
                        m.WriteTo(fs);
                        m.Close();
                        fs.Close();
                    }
                }
                if (btn2Click)
                {
                    btn2Click = false;
                    int i = 0;
                    for (i = 0; i < REPEAT_REM; i++)
                    {
                        int fnum = btn2Count * REPEAT_REM + i;
                        string tpath = ".\\Users\\" + btn2UserName + fnum.ToString() + ".dat";
                        MemoryStream m = new MemoryStream(faceTemplates[i].templateData);
                        FileStream fs = new FileStream(tpath, FileMode.OpenOrCreate);
                        m.WriteTo(fs);
                        m.Close();
                        fs.Close();
                    }
                }
                if(true)
                {
                    AddNewOne = false;
                    string name = username;
                    username = "";
                    if (namexist(name) == -1)
                    {
                        userName.Add(name);
                        List<FaceTemplate> temp = new List<FaceTemplate>(faceTemplates.ToArray());
                        UserTemplates.Add(temp);
                    }
                    else
                    {
                        List<FaceTemplate> temp = new List<FaceTemplate>(faceTemplates.ToArray());
                        int existnum = namexist(name);
                        int i;
                        for (i = 0; i < temp.Count; i++)
                        {
                            UserTemplates[existnum].Add(temp[i]);
                        }
                    }
                }
            }
            faceTemplates.Clear();
            username = "";
            //label1.Text = "Saved the man";

#if TIME_DEBUG
            stopwatch.Stop();
            TimeSpan timespan = stopwatch.Elapsed; 
            double milliseconds = timespan.TotalMilliseconds;  //  总毫秒数
            label2.Text = milliseconds.ToString()+" .REM";
#endif
            recperson = -1;
            return 0;
        }
        int Recongnizehandle()
        {
#if TIME_DEBUG
            label2.Text = "Stopwatch REC start";
            stopwatch.Reset();
            stopwatch.Start();
#endif
            faceTemplates.Clear();
            //label1.Text = "Recongnize start";
            int maxbear = 0;
            int minbear = 0;
            reset();
            recperson = -1;
            for (int count = 0; count < REPEAT_REC; count++)
            {
                Int32 ImageHandle = 0;
                if (FSDK.FSDKE_OK != FSDKCam.GrabFrame(CameraHandle, ref ImageHandle))
                {
                    return(-1);
                }
                FSDK.CImage Image      = new FSDK.CImage(ImageHandle);
                Image       FrameImage = Image.ToCLRImage();
                gr = Graphics.FromImage(FrameImage);

                FSDK.TFacePosition[] FacePosition = Image.DetectMultipleFaces();
                if (FacePosition.Length == 0)
                {
                    minbear++;
                    //无人脸状态超出忍受值
                    if (minbear > MAX_MINBEAR)
                    {
                        recperson = -2;
#if TIME_DEBUG
                        stopwatch.Stop();
                        TimeSpan timespan2     = stopwatch.Elapsed;
                        double   milliseconds2 = timespan2.TotalMilliseconds; //  总毫秒数
                        label2.Text = milliseconds2.ToString() + " .REM";
#endif
                        return(1);
                    }
                    count--;
                    continue;
                }
                if (FacePosition.Length != 1)
                {
                    maxbear++;
                    //多人脸状态超出忍受值
                    if (maxbear > MAX_MAXBEAR)
                    {
                        recperson = -3;
                        return(1);
                    }
                    count--;
                    continue;
                }

                //draw(FacePosition[0]);

                FaceTemplate  Template = new FaceTemplate();
                FSDK.TPoint[] features = Image.DetectEyesInRegion(ref FacePosition[0]);

                Template.templateData = Image.GetFaceTemplateUsingEyes(ref features);

                int recnum = recongnize(Template);
                faceTemplates.Add(Template);

                who[count] = recnum;

                if (recnum != -1)
                {
                    StringFormat format = new StringFormat();
                    format.Alignment = StringAlignment.Center;
                    gr.DrawString(userName[recnum], new System.Drawing.Font("Arial", 16),
                                  new System.Drawing.SolidBrush(System.Drawing.Color.LightGreen),
                                  FacePosition[0].xc, FacePosition[0].yc + FacePosition[0].w * 0.55f, format);
                }
                Application.DoEvents();
            }

            recperson = vote();
            //label1.Text = "Recongnize the man";

            if (recperson > -1)
            {
                List <FaceTemplate> temp = new List <FaceTemplate>(faceTemplates.ToArray());
                int i;
                for (i = 0; i < temp.Count; i++)
                {
                    UserTemplates[recperson].Add(temp[i]);
                }
            }

#if TIME_DEBUG
            stopwatch.Stop();
            TimeSpan timespan     = stopwatch.Elapsed;
            double   milliseconds = timespan.TotalMilliseconds; //  总毫秒数
            label2.Text = milliseconds.ToString() + " .REC";
#endif
            return(0);
        }
        int AddFacehandle()
        {
#if TIME_DEBUG
            label2.Text = "Stopwatch REC start";
            stopwatch.Reset();
            stopwatch.Start();
#endif
            faceTemplates.Clear();
            int maxbear = 0;
            int minbear = 0;
            //label1.Text = "Adding new one";
            for (int count = 0; count < REPEAT_REM; count++)
            {
                Int32 ImageHandle = 0;
                if (FSDK.FSDKE_OK != FSDKCam.GrabFrame(CameraHandle, ref ImageHandle))
                {
                    return(-1);
                }
                FSDK.CImage Image      = new FSDK.CImage(ImageHandle);
                Image       FrameImage = Image.ToCLRImage();
                gr = Graphics.FromImage(FrameImage);

                FSDK.TFacePosition[] FacePosition = Image.DetectMultipleFaces();
                //只允许有一个人在检测范围内
                if (FacePosition.Length == 0)
                {
                    minbear++;
                    //无人脸状态超出忍受值
                    if (minbear > MAX_MINBEAR)
                    {
#if TIME_DEBUG
                        stopwatch.Stop();
                        TimeSpan timespan2     = stopwatch.Elapsed;
                        double   milliseconds2 = timespan2.TotalMilliseconds; //  总毫秒数
                        label2.Text = milliseconds2.ToString() + " .REM";
#endif
                        faceTemplates.Clear();
                        username = "";
                        //label1.Text = "no people";
                        recperson = -2;
                        return(-1);
                    }
                    count--;
                    continue;
                }
                if (FacePosition.Length != 1)
                {
                    maxbear++;
                    //多人脸状态超出忍受值
                    if (maxbear > MAX_MAXBEAR)
                    {
                        faceTemplates.Clear();
                        username = "";
                        //label1.Text = "Too many people";
                        recperson = -3;
                        return(-1);
                    }
                    count--;
                    continue;
                }


                //draw(FacePosition[0]);
                FaceTemplate  Template = new FaceTemplate();
                FSDK.TPoint[] features = Image.DetectEyesInRegion(ref FacePosition[0]);
                Template.templateData = Image.GetFaceTemplateUsingEyes(ref features);

                faceTemplates.Add(Template);
                Application.DoEvents();
            }
            if (AddNewOne)
            {
                if (btn1Click)
                {
                    btn1Click = false;
                    int i = 0;
                    for (i = 0; i < REPEAT_REM; i++)
                    {
                        int          fnum  = btn1Count * REPEAT_REM + i;
                        string       tpath = ".\\Users\\" + btn1UserName + fnum.ToString() + ".dat";
                        MemoryStream m     = new MemoryStream(faceTemplates[i].templateData);
                        FileStream   fs    = new FileStream(tpath, FileMode.OpenOrCreate);
                        m.WriteTo(fs);
                        m.Close();
                        fs.Close();
                    }
                }
                if (btn2Click)
                {
                    btn2Click = false;
                    int i = 0;
                    for (i = 0; i < REPEAT_REM; i++)
                    {
                        int          fnum  = btn2Count * REPEAT_REM + i;
                        string       tpath = ".\\Users\\" + btn2UserName + fnum.ToString() + ".dat";
                        MemoryStream m     = new MemoryStream(faceTemplates[i].templateData);
                        FileStream   fs    = new FileStream(tpath, FileMode.OpenOrCreate);
                        m.WriteTo(fs);
                        m.Close();
                        fs.Close();
                    }
                }
                if (true)
                {
                    AddNewOne = false;
                    string name = username;
                    username = "";
                    if (namexist(name) == -1)
                    {
                        userName.Add(name);
                        List <FaceTemplate> temp = new List <FaceTemplate>(faceTemplates.ToArray());
                        UserTemplates.Add(temp);
                    }
                    else
                    {
                        List <FaceTemplate> temp = new List <FaceTemplate>(faceTemplates.ToArray());
                        int existnum             = namexist(name);
                        int i;
                        for (i = 0; i < temp.Count; i++)
                        {
                            UserTemplates[existnum].Add(temp[i]);
                        }
                    }
                }
            }
            faceTemplates.Clear();
            username = "";
            //label1.Text = "Saved the man";

#if TIME_DEBUG
            stopwatch.Stop();
            TimeSpan timespan     = stopwatch.Elapsed;
            double   milliseconds = timespan.TotalMilliseconds; //  总毫秒数
            label2.Text = milliseconds.ToString() + " .REM";
#endif
            recperson = -1;
            return(0);
        }
    public void Read(TProtocol iprot)
    {
        iprot.IncrementRecursionDepth();
        try
        {
            TField field;
            iprot.ReadStructBegin();
            while (true)
            {
                field = iprot.ReadFieldBegin();
                if (field.Type == TType.Stop)
                {
                    break;
                }
                switch (field.ID)
                {
                case 1:
                    if (field.Type == TType.String)
                    {
                        FaceObjID = iprot.ReadString();
                    }
                    else
                    {
                        TProtocolUtil.Skip(iprot, field.Type);
                    }
                    break;

                case 2:
                    if (field.Type == TType.List)
                    {
                        {
                            Ft = new List <FaceTemplate>();
                            TList _list4 = iprot.ReadListBegin();
                            for (int _i5 = 0; _i5 < _list4.Count; ++_i5)
                            {
                                FaceTemplate _elem6;
                                _elem6 = new FaceTemplate();
                                _elem6.Read(iprot);
                                Ft.Add(_elem6);
                            }
                            iprot.ReadListEnd();
                        }
                    }
                    else
                    {
                        TProtocolUtil.Skip(iprot, field.Type);
                    }
                    break;

                default:
                    TProtocolUtil.Skip(iprot, field.Type);
                    break;
                }
                iprot.ReadFieldEnd();
            }
            iprot.ReadStructEnd();
        }
        finally
        {
            iprot.DecrementRecursionDepth();
        }
    }
Exemple #9
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();
        }
Exemple #10
0
        public bool SavePerson()
        {
            bool            ok = true;
            OleDbConnection cn = new OleDbConnection();

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

                sqlString = "INSERT INTO PersonTable(Title,FName,Gender,template) VALUES('" + Title + "','" + FullName + "','" + Gender + "','" + FaceTemplate.ToString() + "')";
                OleDbCommand cmd = new OleDbCommand(sqlString, cn);
                cmd.ExecuteNonQuery();
                cn.Close();
                // addd new Student
                db.Students.Add(
                    new Student
                {
                    regno      = Title,
                    fullname   = FullName,
                    gender     = Gender,
                    level      = level,
                    insertdate = DateTime.Now
                });
                db.SaveChanges();
            }
            catch (Exception n)
            {
                MessageBox.Show("An error occured: " + n);
                ok = false;
            }
            finally
            {
                cn.Close();
            }
            return(ok);
        }