Exemple #1
0
        /// <summary>
        /// 该线程用于识别人脸并显示人脸对应的名字
        /// </summary>
        private void videoShow()
        {
            while (true)
            {
                //Mat img = new Mat();
                cap.Read(img);
                faces = faceDetect.GetFaceRects(img);
                List <Mat> dstFaces = faceDetect.GetFaces(img, faces);
                if (dstFaces.Count > 0)
                {
                    //bool flag1 = true;
                    //grabBox.BeginInvoke(new Action<Mat>(t =>
                    //{
                    //    grabBox.Image = t.ToBitmap();
                    //    flag1 = false;
                    //}), dstFaces[0]);
                    //while (flag1)
                    //{
                    //    Thread.Sleep(10);
                    //}
                    List <string> names = faceDetect.PredictFace(dstFaces);
                    if (names.Count > 0)
                    {
                        grabBox.BeginInvoke(new Action <string>(t =>
                        {
                            label2.Text = t;
                        }), names[0]);
                    }
                    Bitmap bitmap = img.ToBitmap();
                    faceDetect.ShowFaceRects(faces, bitmap, names);

                    bool flag2 = true;
                    pictureBox.BeginInvoke(new Action <Bitmap>(t =>
                    {
                        pictureBox.Image = t;
                        flag2            = false;
                    }), bitmap);
                    while (flag2)
                    {
                        Thread.Sleep(10);
                    }
                }
                //Cv2.WaitKey(100);
            }
        }
Exemple #2
0
        /// <summary>
        /// 该线程用于识别人脸并显示人脸对应的名字
        /// </summary>
        private void videoShow()
        {
            while (true)
            {
                if (end)
                {
                    end = false;
                    break;
                }
                //Mat img = new Mat();
                try
                {
                    var ret = cap.Read(img);
                    faces         = faceDetect.GetFaceRects(img);
                    bitmapForShow = img.ToBitmap();
                    List <Mat> dstFaces = faceDetect.GetFaces(img, faces);
                    if (dstFaces.Count > 0)
                    {
                        List <string> names = faceDetect.PredictFace(dstFaces);
                        if (names.Count > 0)
                        {
                            grabBox.BeginInvoke(new Action <string>(t =>
                            {
                                label2.Text = t;
                            }), names[0]);
                        }

                        faceDetect.ShowFaceRects(faces, bitmapForShow, names);

                        bool flag2 = true;
                        pictureBox.BeginInvoke(new Action <Bitmap>(t =>
                        {
                            pictureBox.Image = new Bitmap(t);
                            flag2            = false;
                        }), bitmapForShow);

                        while (flag2)
                        {
                            Thread.Sleep(10);
                        }
                        //pictureBox.Image.Dispose();
                        //bitmap.Dispose();
                    }
                    else
                    {
                        bool flag2 = true;
                        pictureBox.BeginInvoke(new Action <Bitmap>(t =>
                        {
                            pictureBox.Image = new Bitmap(t);
                            flag2            = false;
                        }), bitmapForShow);

                        while (flag2)
                        {
                            Thread.Sleep(10);
                        }
                    }

                    //清理
                    foreach (var x in dstFaces)
                    {
                        x.Dispose();
                    }
                    GC.Collect();
                }catch (Exception e)
                {
                }

                //Cv2.WaitKey(100);
                //GC.Collect();
            }
        }