예제 #1
0
파일: Program.cs 프로젝트: neutmute/emgucv
      static void Run()
      {
         IImage image;

         //Read the files as an 8-bit Bgr image  

         image = new UMat("lena.jpg", ImreadModes.Color); //UMat version
         //image = new Mat("lena.jpg", ImreadModes.Color); //CPU version

         long detectionTime;
         List<Rectangle> faces = new List<Rectangle>();
         List<Rectangle> eyes = new List<Rectangle>();

         DetectFace.Detect(
           image, "haarcascade_frontalface_default.xml", "haarcascade_eye.xml", 
           faces, eyes,
           out detectionTime);

         foreach (Rectangle face in faces)
            CvInvoke.Rectangle(image, face, new Bgr(Color.Red).MCvScalar, 2);
         foreach (Rectangle eye in eyes)
            CvInvoke.Rectangle(image, eye, new Bgr(Color.Blue).MCvScalar, 2);

         //display the image 
         using (InputArray iaImage = image.GetInputArray())
         ImageViewer.Show(image, String.Format(
            "Completed face and eye detection using {0} in {1} milliseconds", 
            (iaImage.Kind == InputArray.Type.CudaGpuMat && CudaInvoke.HasCuda) ? "CUDA" :
            (iaImage.IsUMat && CvInvoke.UseOpenCL) ? "OpenCL" 
            : "CPU",
            detectionTime));
      }
예제 #2
0
        static void Run()
        {
            IImage image;

            //Read the files as an 8-bit Bgr image
            image = new UMat("C:/Users/lucas/Desktop/samt/anno3/306/Progetto2_FaceDetectionPeopleCounting/img/facce.png", ImreadModes.Color); //UMat version
            //image = new Mat("lena.jpg", ImreadModes.Color); //CPU version

            long             detectionTime;
            List <Rectangle> faces = new List <Rectangle>();
            List <Rectangle> eyes  = new List <Rectangle>();

            DetectFace.Detect(
                image, "haarcascade_frontalface_default.xml", "haarcascade_eye.xml",
                faces, eyes,
                out detectionTime);

            foreach (Rectangle face in faces)
            {
                CvInvoke.Rectangle(image, face, new Bgr(Color.Red).MCvScalar, 2);
            }
            foreach (Rectangle eye in eyes)
            {
                CvInvoke.Rectangle(image, eye, new Bgr(Color.Blue).MCvScalar, 2);
            }

            //display the image
            using (InputArray iaImage = image.GetInputArray())
                ImageViewer.Show(image, String.Format(
                                     "Completed face and eye detection using {0} in {1} milliseconds",
                                     (iaImage.Kind == InputArray.Type.CudaGpuMat && CudaInvoke.HasCuda) ? "CUDA" :
                                     (iaImage.IsUMat && CvInvoke.UseOpenCL) ? "OpenCL"
            : "CPU",
                                     detectionTime));
        }
예제 #3
0
파일: Form1.cs 프로젝트: FinchYang/study
        string Run(string fname)
        {
            IImage image;

            //Read the files as an 8-bit Bgr image

            image = new UMat(fname, ImreadModes.Color); //UMat version
            //image = new Mat("lena.jpg", ImreadModes.Color); //CPU version
            //   listmMats.Add(image);
            //
            var aaa = new Image <Emgu.CV.Structure.Gray, byte>(fname);

            trainingImages.Add(aaa);

            Names_List_ID.Add(Names_List_ID.Count());

            long             detectionTime;
            List <Rectangle> faces = new List <Rectangle>();
            List <Rectangle> eyes  = new List <Rectangle>();

            //   richTextBox1.AppendText(Environment.NewLine + "aaa");
            DetectFace.Detect(
                image, "haarcascade_frontalface_default.xml", "haarcascade_eye.xml",
                faces, eyes,
                out detectionTime);

            foreach (Rectangle face in faces)
            {
                CvInvoke.Rectangle(image, face, new Bgr(Color.Red).MCvScalar, 2);
            }
            foreach (Rectangle eye in eyes)
            {
                CvInvoke.Rectangle(image, eye, new Bgr(Color.Blue).MCvScalar, 2);
            }
            //   richTextBox1.AppendText(Environment.NewLine + "bbb");
            //display the image
            using (InputArray iaImage = image.GetInputArray())
            {
                // recognizer.Train(image, );
                var tempfile = Path.GetTempFileName() + ".jpg";
                //  richTextBox1.AppendText(Environment.NewLine + "ccc");
                image.Save(tempfile);
                // aaa.Save(tempfile);
                // recognizer.Train(image, Names_List_ID.ToArray());
                // listmMats.Add(iaImage);
                //var a = new ImageViewer();
                //a.BackgroundImage = ;
                //a.ShowDialog();
                //ImageViewer.Show(image, String.Format(
                //  //    ImageViewer.Show(image, String.Format(
                //    "Completed face and eye detection using {0} in {1} milliseconds",
                //    (iaImage.Kind == InputArray.Type.CudaGpuMat && CudaInvoke.HasCuda) ? "CUDA" :
                //    (iaImage.IsUMat && CvInvoke.UseOpenCL) ? "OpenCL"
                //    : "CPU",
                //    detectionTime));
                richTextBox1.AppendText(Environment.NewLine + "eee");
                return(tempfile);
            }
        }
예제 #4
0
        public static void GetFaceRectangle(String filePath, String baseFolder)
        {
            IImage image;

            //Read the files as an 8-bit Bgr image

            image = new UMat(filePath, ImreadModes.Color); //UMat version
                                                           //image = new Mat("lena.jpg", ImreadModes.Color); //CPU version

            long             detectionTime = 0;
            List <Rectangle> faces         = new List <Rectangle>();
            List <Rectangle> eyes          = new List <Rectangle>();

            foreach (var face_file in new DirectoryInfo(baseFolder + "haarcascades").GetFiles("*face*"))
            {
                foreach (var eye_file in new DirectoryInfo(baseFolder + "haarcascades").GetFiles("*eye*"))
                {
                    long currentDetectionTime = 0;
                    DetectFace.Detect(
                        image, face_file.FullName, eye_file.FullName,
                        faces, eyes,
                        out currentDetectionTime);
                    if (faces.Count != 0)
                    {
                        break;
                    }
                    eyes           = new List <Rectangle>();
                    detectionTime += currentDetectionTime;
                }
                if (faces.Count != 0)
                {
                    break;
                }
            }

            foreach (Rectangle face in faces)
            {
                CvInvoke.Rectangle(image, face, new Bgr(Color.Red).MCvScalar, 2);
            }
            foreach (Rectangle eye in eyes)
            {
                CvInvoke.Rectangle(image, eye, new Bgr(Color.Blue).MCvScalar, 2);
            }

            //display the image
            using (InputArray iaImage = image.GetInputArray())
                ImageViewer.Show(image, String.Format(
                                     "Completed face and eye detection using {0} in {1} milliseconds",
                                     (iaImage.Kind == InputArray.Type.CudaGpuMat && CudaInvoke.HasCuda) ? "CUDA" :
                                     (iaImage.IsUMat && CvInvoke.UseOpenCL) ? "OpenCL"
                   : "CPU",
                                     detectionTime));
        }
예제 #5
0
        static void Run()
        {
            IImage image;

            //Read the files as an 8-bit Bgr image

            image = new UMat("lena.jpg", ImreadModes.Color); //UMat version
            //image = new Mat("lena.jpg", ImreadModes.Color); //CPU version

            long detectionTime;

            /*
             * List<Rectangle> faces = new List<Rectangle>();
             * List<Rectangle> eyes = new List<Rectangle>();
             *
             * DetectFace.Detect(
             * image, "haarcascade_frontalface_default.xml", "haarcascade_eye.xml",
             * faces, eyes,
             * out detectionTime);
             *
             * foreach (Rectangle face in faces)
             * CvInvoke.Rectangle(image, face, new Bgr(Color.Red).MCvScalar, 2);
             * foreach (Rectangle eye in eyes)
             * CvInvoke.Rectangle(image, eye, new Bgr(Color.Blue).MCvScalar, 2);
             * */
            ProcessImage(image, out detectionTime);

            //display the image
            using (InputArray iaImage = image.GetInputArray())
            {
                var viewer = new ImageViewer(image, String.Format(
                                                 "Completed face and eye detection using {0} in {1} milliseconds",
                                                 (iaImage.Kind == InputArray.Type.CudaGpuMat && CudaInvoke.HasCuda)
                      ? "CUDA" : (iaImage.IsUMat && CvInvoke.UseOpenCL) ? "OpenCL" : "CPU",
                                                 detectionTime));
                viewer.ImageBox.ImageReload += (sender, e) =>
                {
                    ProcessImage(e, out detectionTime);
                    //(sender as ImageBox)
                };
                Application.Run(viewer);

                /*
                 * ImageViewer.Show(image, String.Format(
                 *  "Completed face and eye detection using {0} in {1} milliseconds",
                 *  (iaImage.Kind == InputArray.Type.CudaGpuMat && CudaInvoke.HasCuda)
                 *      ? "CUDA": (iaImage.IsUMat && CvInvoke.UseOpenCL)? "OpenCL": "CPU",
                 *  detectionTime));
                 * */
            }
        }
        //private void Ruc()
        //{
        //    try
        //    {
        //        _capture = new VideoCapture();
        //    }
        //    catch (Exception ex)
        //    {
        //        MetroMessageBox.Show(this, "Mensaje :" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, 120);
        //        return;
        //    }
        //    Application.Idle += ProcessFrame;
        //}

        void Run()
        {
            IImage image;

            //Read the files as an 8-bit Bgr image

            image = new UMat("img1.jpg", ImreadModes.Color); //UMat version
                                                             //image = new Mat("lena.jpg", ImreadModes.Color); //CPU version
            FileInfo file = new FileInfo("haarcascade_frontalface_default.xml");

            long             detectionTime;
            List <Rectangle> faces = new List <Rectangle>();
            List <Rectangle> eyes  = new List <Rectangle>();

            DetectFace.Detect(
                image, "haarcascade_frontalface_default.xml", "haarcascade_eye.xml",
                faces, eyes,
                out detectionTime);

            foreach (Rectangle face in faces)
            {
                CvInvoke.Rectangle(image, face, new Bgr(Color.Red).MCvScalar, 2);
            }
            foreach (Rectangle eye in eyes)
            {
                CvInvoke.Rectangle(image, eye, new Bgr(Color.Blue).MCvScalar, 2);
            }

            //display the image
            InputArray iaImage = image.GetInputArray();

            //ME CODE
            pbCapture.Image     = image;
            txtDescription.Text = String.Format(
                "Completed face and eye detection using {0} in {1} milliseconds",
                (iaImage.Kind == InputArray.Type.CudaGpuMat && CudaInvoke.HasCuda) ? "CUDA" :
                (iaImage.IsUMat && CvInvoke.UseOpenCL) ? "OpenCL"
               : "CPU",
                detectionTime);
            //ImageViewer.Show(image, String.Format(
            //   "Completed face and eye detection using {0} in {1} milliseconds",
            //   (iaImage.Kind == InputArray.Type.CudaGpuMat && CudaInvoke.HasCuda) ? "CUDA" :
            //   (iaImage.IsUMat && CvInvoke.UseOpenCL) ? "OpenCL"
            //   : "CPU",
            //   detectionTime));
        }
예제 #7
0
        static void Run()
        {
            //读取指定路径的图片
            IImage image = new UMat("images/IMG_1288.jpg", ImreadModes.Color); //UMat version

            //定义人脸识别的时间
            long detectionTime;

            //人脸列表
            List <Rectangle> faces = new List <Rectangle>();

            //眼睛列表
            List <Rectangle> eyes = new List <Rectangle>();

            //开始识别
            DetectFace.Detect(image, "haarcascade_frontalface_default.xml", "haarcascade_eye.xml", faces, eyes, out detectionTime);

            //将识别后的所有面部,用矩形区域标注出来
            foreach (Rectangle face in faces)
            {
                CvInvoke.Rectangle(image, face, new Bgr(Color.Red).MCvScalar, 2);
            }

            /*
             * 2017-10-19 不需要标注眼睛,比较混乱
             *
             * //将识别后的所有眼睛,用矩形区域标注出来
             * foreach (Rectangle eye in eyes)
             * {
             *  CvInvoke.Rectangle(image, eye, new Bgr(Color.Blue).MCvScalar, 2);
             * }
             */

            using (InputArray iaImage = image.GetInputArray())
            {
                //获取人脸检测方式
                String method = (iaImage.Kind == InputArray.Type.CudaGpuMat && CudaInvoke.HasCuda) ? "CUDA" : (iaImage.IsUMat && CvInvoke.UseOpenCL) ? "OpenCL" : "CPU";

                //显示图片
                ImageViewer.Show(image, String.Format("检测完毕,使用 {0} 模式, 耗时 {1} 毫秒", method, detectionTime));
            }
        }
예제 #8
0
        private static void Run()
        {
            IImage image;

            //Read the files as an 8-bit Bgr image

            image = new UMat("lena.jpg", ImreadModes.Color); //UMat version
            //image = new Mat("lena.jpg", ImreadModes.Color); //CPU version

            long detectionTime;
            var  faces = new List <Rectangle>();
            var  eyes  = new List <Rectangle>();

            DetectFace.Detect(
                image, "haarcascade_frontalface_default.xml", "haarcascade_eye.xml",
                faces, eyes,
                out detectionTime);

            foreach (var face in faces)
            {
                CvInvoke.Rectangle(image, face, new Bgr(Color.Red).MCvScalar, 2);
            }
            foreach (var eye in eyes)
            {
                CvInvoke.Rectangle(image, eye, new Bgr(Color.Blue).MCvScalar, 2);
            }

            //display the image
            using (var iaImage = image.GetInputArray())
            {
                ImageViewer.Show(image, string.Format(
                                     "Completed face and eye detection using {0} in {1} milliseconds",
                                     iaImage.Kind == InputArray.Type.CudaGpuMat && CudaInvoke.HasCuda ? "CUDA"
                    : iaImage.IsUMat && CvInvoke.UseOpenCL ? "OpenCL"
                    : "CPU",
                                     detectionTime));
            }
        }
예제 #9
0
        private void SingDetectHaar(string fileName, string fileNameToSave)
        {
            try
            {
                IImage image = new UMat(fileName, ImreadModes.Color);

                List <Rectangle> sings = new List <Rectangle>();
                _singDetectorHaar = new SingDetectorMethodHaara();

                long detectTime;
                bool success = false;

                _singDetectorHaar.Detect(image, _singFileCascad, sings, out detectTime);

                foreach (Rectangle sing in sings)
                {
                    CvInvoke.Rectangle(image, sing, new Bgr(Color.Red).MCvScalar, 2);
                    success = true;
                }

                using (InputArray iaImage = image.GetInputArray())
                {
                    if (success)
                    {
                        timeChart.Invoke(new Action <int, double>(AddHaar), _haarActuation, detectTime);
                        SaveFileBmp(image.Bitmap, _dirHaar, _haarSingOnImageTag, fileNameToSave);
                        _haarActuation++;
                    }
                }
                _haarDetectTime += detectTime;
                Console.WriteLine(String.Format("_haarActuation : {0}", _haarActuation));
                haarLabel.Invoke(new Action <int, double>(AddHaarLabel), _haarActuation, _haarDetectTime);
            }
            catch
            {
            }
        }
예제 #10
0
        }                            // /////////////////////////////////////////////////////////////////////////////////////////////////////////

        private void Run(UMat image) //!!!UMat
        //IImage image;
        //Read the files as an 8-bit Bgr image

        //image = new UMat("lena.jpg", ImreadModes.Color); //UMat version
        //image = new Mat("lena.jpg", ImreadModes.Color); //CPU version

        {
            long             detectionTime;
            List <Rectangle> faces = new List <Rectangle>();
            List <Rectangle> eyes  = new List <Rectangle>();

            //			DetectFace.Detect(
            //			  image, "haarcascade_frontalface_default.xml", "haarcascade_eye.xml",
            //			  faces, eyes, out detectionTime);
            DetectFace.DetectOnlyFace(
                image, "haarcascade_frontalface_default.xml",
                faces, out detectionTime,
                FaceParam.ScaleFactor(txScaleFactor), FaceParam.MinNeighbors(txMinNeighbors), FaceParam.MinObjectSize(txMinObjectSize));

            foreach (Rectangle face in faces)
            {
                CvInvoke.Rectangle(image, face, new Bgr(Color.Red).MCvScalar, 2);
            }
            foreach (Rectangle eye in eyes)
            {
                CvInvoke.Rectangle(image, eye, new Bgr(Color.Blue).MCvScalar, 2);
            }
            double r = gemor.put(faces.Count > 0);

            if (r < 0.4 && (signal == Signal.yes || signal == Signal.nodef))
            {
                signal = Signal.no;
            }
            else if (r > 0.6 && (signal == Signal.no || signal == Signal.nodef))
            {
                signal = Signal.yes;
            }
            string sret = phases.Run(signal, r);
            //lbDbgTimers.Text = sret;
            MCvScalar color = new MCvScalar(255, 0, 255, 0);

            CvInvoke.PutText(image, sret, new Point(2, 25), FontFace.HersheyComplexSmall, 1, color);             //_grayFrame
            if (signal == Signal.nodef && false)
            {
                if (faces.Count > 0)
                {
                    using (InputArray iaImage = image.GetInputArray()) {
                        string sreport = String.Format(
                            "Completed face detection using {0} in {1} milliseconds. Rct={2} x {3} r={4}",
                            (iaImage.Kind == InputArray.Type.CudaGpuMat && CudaInvoke.HasCuda) ? "CUDA" :
                            (iaImage.IsUMat && CvInvoke.UseOpenCL) ? "OpenCL" : "CPU", detectionTime,
                            faces[0].Width, faces[0].Height, r);
                        Console.WriteLine(sreport);
                    }
                }
                else
                {
                    Console.WriteLine(r.ToString());
                }
            }
        }         // ////////////////////////////////////////////////////////////////////////////////////////////////////////////