示例#1
0
        //处理三个摄像头图像
        private void Bg1_DoWork(object sender, DoWorkEventArgs e)
        {
            string        ConnString = Tool.connstr;//连接字符串
            SqlConnection conn       = new SqlConnection(ConnString);

            int[]    countID1   = new int[CountingSize];
            byte[][] templates1 = new byte[CountingSize][];
            int[]    countID2   = new int[CountingSize];
            byte[][] templates2 = new byte[CountingSize][];

            //存储所有已经出现的id
            List <long> allIDs1 = new List <long>();

            //新建1个Tracker
            int tracker1 = 0;

            FSDK.CreateTracker(ref tracker1);
            int tracker2 = 0;

            FSDK.CreateTracker(ref tracker2);

            //设置Tracker参数
            int err1 = 0;

            FSDK.SetTrackerMultipleParameters(tracker1,
                                              "RecognizeFaces=" + RecognizeFaces.ToString().ToLower() +
                                              "; HandleArbitraryRotations=" + HandleArbitraryRotations.ToString().ToLower() +
                                              "; DetermineFaceRotationAngle=" + HandleArbitraryRotations.ToString().ToLower() +
                                              "; InternalResizeWidth=" + InternalResizeWidth.ToString().ToLower() +
                                              "; FaceDetectionThreshold=" + FaceDetectionThreshold.ToString().ToLower() + ";",
                                              ref err1);
            int err2 = 0;

            FSDK.SetTrackerMultipleParameters(tracker2,
                                              "RecognizeFaces=" + RecognizeFaces.ToString().ToLower() +
                                              "; HandleArbitraryRotations=" + HandleArbitraryRotations.ToString().ToLower() +
                                              "; DetermineFaceRotationAngle=" + HandleArbitraryRotations.ToString().ToLower() +
                                              "; InternalResizeWidth=" + InternalResizeWidth.ToString().ToLower() +
                                              "; FaceDetectionThreshold=" + FaceDetectionThreshold.ToString().ToLower() + ";",
                                              ref err2);

            DateTime t1 = DateTime.Now;
            DateTime t2 = DateTime.Now;

            //记录总帧数
            int loops = 0;

            while (!needClose)
            {
                //声明定义三个图片句柄,来源于三个摄像头句柄
                Int32 imageHandle1 = 0;
                Int32 imageHandle2 = 0;
                if (FSDK.FSDKE_OK != FSDKCam.GrabFrame(cameraHandle1, ref imageHandle1))
                {
                    continue;
                }
                if (FSDK.FSDKE_OK != FSDKCam.GrabFrame(cameraHandle2, ref imageHandle2))
                {
                    continue;
                }

                FSDK.CImage image1      = new FSDK.CImage(imageHandle1);
                Image       frameImage1 = image1.ToCLRImage();
                FSDK.CImage image2      = new FSDK.CImage(imageHandle2);
                Image       frameImage2 = image2.ToCLRImage();

                long[] IDs1;
                long[] IDs2;
                long   faceCount1 = 0;
                long   faceCount2 = 0;
                FSDK.FeedFrame(tracker1, 0, image1.ImageHandle, ref faceCount1, out IDs1, sizeof(long) * 256); // maximum 256 faces detected
                FSDK.FeedFrame(tracker2, 0, image2.ImageHandle, ref faceCount2, out IDs2, sizeof(long) * 256); // maximum 256 faces detected
                Array.Resize(ref IDs1, (int)faceCount1);
                Array.Resize(ref IDs2, (int)faceCount2);
                Graphics g1 = Graphics.FromImage(frameImage1);
                Graphics g2 = Graphics.FromImage(frameImage2);

                conn.Open();

                Application.DoEvents(); //使控件可用

                //处理1号相机当前帧
                for (int i = 0; i < IDs1.Length; ++i)
                {
                    try
                    {
                        //计算人脸特征模板
                        FSDK.TFacePosition facePosition1 = new FSDK.TFacePosition();
                        FSDK.GetTrackerFacePosition(tracker1, 0, IDs1[i], ref facePosition1);

                        //绘制人脸矩形框
                        int left = facePosition1.xc - (int)(facePosition1.w * 0.5);
                        int top  = facePosition1.yc - (int)(facePosition1.w * 0.5);
                        g1.DrawRectangle(Pens.LightGreen, left, top, (int)(facePosition1.w * 1.0), (int)(facePosition1.w * 1.0));

                        //计算当前ID对应人脸特征串
                        byte[] temp1 = image1.GetFaceTemplateInRegion(ref facePosition1);

                        //记录当前ID
                        if (allIDs1.Count == 0)//如果是第一个ID则直接加入
                        {
                            //记录当前ID出现次数
                            countID1[IDs1[i]] = countID1[IDs1[i]] >= 100 ? 100 : countID1[IDs1[i]] + 1;

                            //更新当前ID的特征串
                            templates1[IDs1[i]] = temp1;

                            //无重复添加新的ID
                            AddIDIntoAllIDList(allIDs1, IDs1[i]);
                        }
                        else if (allIDs1.Exists(x => x == IDs1[i]))//如果已存在当前ID,则只更新特征串数据
                        {
                            //更新当前ID的特征串
                            templates1[IDs1[i]] = temp1;

                            //记录当前ID出现次数
                            countID1[IDs1[i]] = countID1[IDs1[i]] >= 100 ? 100 : countID1[IDs1[i]] + 1;
                        }
                        else//如果是新的ID,则与原来的每个ID对应的模板进行比对,看相似度
                        {
                            float simlarity = 0;
                            //foreach (long id in allIDs1)
                            //{
                            //    int r = FSDK.MatchFaces(ref templates1[id], ref temp1, ref simlarity);
                            //    if (simlarity > 0.8f)//发现相似
                            //    {
                            //        //FSDK.PurgeID(tracker1, IDs[i]);
                            //        countID1[id] = countID1[id] >= 100 ? 100 : countID1[id] + 1;
                            //        break;
                            //    }
                            //    else//没有相似
                            //    {
                            //        //记录当前ID出现次数
                            //        countID1[IDs1[i]] = countID1[IDs1[i]] >= 100 ? 100 : countID1[IDs1[i]] + 1;

                            //        //更新当前ID的特征串
                            //        templates1[IDs1[i]] = temp1;

                            //        //无重复添加新的ID
                            //        AddIDIntoAllIDList(allIDs1, IDs1[i]);
                            //    }
                            //}

                            int sim1_count = 0;
                            foreach (long id in allIDs1)
                            {
                                int r = FSDK.MatchFaces(ref templates1[id], ref temp1, ref simlarity);
                                if (simlarity > 0.8f)//发现相似
                                {
                                    countID1[id] = countID1[id] >= 100 ? 100 : countID1[id] + 1;
                                    sim1_count   = sim1_count + 1;
                                    break;
                                }
                            }

                            if (sim1_count == 0)
                            {
                                //记录当前ID出现次数
                                countID1[IDs1[i]] = countID1[IDs1[i]] >= 100 ? 100 : countID1[IDs1[i]] + 1;

                                //更新当前ID的特征串
                                templates1[IDs1[i]] = temp1;

                                //无重复添加新的ID
                                AddIDIntoAllIDList(allIDs1, IDs1[i]);
                            }
                        }
                    }
                    catch { }
                }

                //处理2号相机当前帧
                for (int i = 0; i < IDs2.Length; ++i)
                {
                    try
                    {
                        //计算人脸特征模板
                        FSDK.TFacePosition facePosition2 = new FSDK.TFacePosition();
                        FSDK.GetTrackerFacePosition(tracker2, 0, IDs2[i], ref facePosition2);

                        //绘制人脸矩形框
                        int left = facePosition2.xc - (int)(facePosition2.w * 0.5);
                        int top  = facePosition2.yc - (int)(facePosition2.w * 0.5);
                        g2.DrawRectangle(Pens.LightGreen, left, top, (int)(facePosition2.w * 1.0), (int)(facePosition2.w * 1.0));

                        //计算当前ID对应人脸特征串
                        byte[] temp2 = image2.GetFaceTemplateInRegion(ref facePosition2);

                        //如果是新的ID,则与原来的每个ID对应的模板进行比对,看相似度
                        float simlarity = 0;
                        foreach (long id in allIDs1)
                        {
                            int r = FSDK.MatchFaces(ref templates1[id], ref temp2, ref simlarity);
                            if (simlarity > 0.8f)//发现相似
                            {
                                countID1[id] = 0;

                                int        a             = Convert.ToInt32(id);
                                DateTime   XiaCheShiJian = DateTime.Now.ToLocalTime();
                                SqlCommand sqlCmd        = new SqlCommand("UPDATE [SafeSystem].[dbo].[FaceRecognition] SET [XiaCheShiJian] = @XiaCheShiJian WHERE Template = " + a + "and XiaCheShiJian is null", conn);
                                sqlCmd.Parameters.Add("@XiaCheShiJian", System.Data.SqlDbType.DateTime);
                                sqlCmd.Parameters["@XiaCheShiJian"].Value = XiaCheShiJian;
                                sqlCmd.ExecuteNonQuery();
                                RemoveIDFromAllIDList(allIDs1, IDs1[i]);
                                break;
                            }
                        }
                    }
                    catch { }
                }

                //组织当前全部ID列表字符串
                string strAll1 = "";
                int    index1  = 0;
                foreach (int id in allIDs1)
                {
                    if (index1 == 0)
                    {
                        strAll1 += "(" + id + ":" + countID1[id] + ")";
                    }
                    else
                    {
                        strAll1 += ",(" + id + ":" + countID1[id] + ")";
                    }
                    index1++;
                }

                //显示当前实时数据到当前帧
                t2 = DateTime.Now;
                TimeSpan ts = t2 - t1;
                t1 = t2;
                g1.DrawString("Frames Count:" + loops, new Font("微软雅黑", 20f), Brushes.Red, 10, 0);
                g1.DrawString("All Person:" + allIDs1.Count + "[" + strAll1 + "]", new Font("微软雅黑", 20f), Brushes.Red, 10, 50);

                g1.DrawString("Frame Time:" + ts.TotalMilliseconds.ToString("####.#") + "ms", new Font("微软雅黑", 20f), Brushes.Red, 10, 100);
                g1.DrawString("Frame Rate:" + (1000.0 / ts.TotalMilliseconds).ToString("####.#") + "fps", new Font("微软雅黑", 20f), Brushes.Red, 10, 150);
                g1.Dispose();
                g1 = null;
                Helper.SetImage(box1, frameImage1);
                Thread.Sleep(SleepTime);
                g2.Dispose();
                g2 = null;
                Helper.SetImage(box2, frameImage2);
                Thread.Sleep(SleepTime);

                //每处理100帧图像就清理一次出现次数很少的ID,减少计数错误
                if (loops == 100)
                {
                    List <long> bak1 = new List <long>();
                    foreach (long id in allIDs1)
                    {
                        if (countID1[id] > MiniAppear)
                        {
                            bak1.Add(id);
                            int        a = Convert.ToInt32(id);
                            DateTime   ShangCheShiJian = DateTime.Now.ToLocalTime();
                            SqlCommand sqlCmd          = new SqlCommand("if not exists (SELECT [Template] FROM [SafeSystem].[dbo].[FaceRecognition] Where Template = '" + a + "' and XiaCheShiJian is null) INSERT INTO [SafeSystem].[dbo].[FaceRecognition] ([Template],[ShangCheShiJian]) " + " values(@Template, @ShangCheShiJian)", conn);
                            sqlCmd.Parameters.Add("@Template", System.Data.SqlDbType.Int);
                            sqlCmd.Parameters.Add("@ShangCheShiJian", System.Data.SqlDbType.DateTime);
                            sqlCmd.Parameters["@Template"].Value        = a;
                            sqlCmd.Parameters["@ShangCheShiJian"].Value = ShangCheShiJian;
                            sqlCmd.ExecuteNonQuery();
                        }
                        else
                        {
                            countID1[id] = 0;
                        }
                    }
                    allIDs1 = bak1;
                    loops   = 0;
                }
                loops++;

                GC.Collect(); // collect the garbage after the deletion
                conn.Close();
            }
            FSDK.SaveTrackerMemoryToFile(tracker1, TrackerMemoryFile);
            FSDK.FreeTracker(tracker1);
            FSDK.FreeTracker(tracker2);

            FSDKCam.CloseVideoCamera(cameraHandle1);
            FSDKCam.CloseVideoCamera(cameraHandle2);
            FSDKCam.FinalizeCapturing();
        }
示例#2
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();
        }