예제 #1
0
        public static Image <Bgr, byte> detectFace(Image <Bgr, byte> image, out int detections, out List <PointF> positions)
        {
            Image <Bgr, byte> copyImage = new Image <Bgr, byte>(image.Bitmap);           //Copy the image into a new one

            Person_Detector.facePositions.Clear();


            using (GpuImage <Bgr, Byte> gpuImage = new GpuImage <Bgr, byte>(image))
                using (GpuImage <Gray, Byte> gpuGray = gpuImage.Convert <Gray, Byte>())                                //The cascade classifier only takes gray for some reason
                {
                    faces = face.DetectMultiScale(gpuGray, 1.1, 10, Size.Empty);

                    //Draws rectanges to the face detection positions
                    foreach (Rectangle f in faces)
                    {
                        copyImage.Draw(f, new Bgr(Color.Red), 4);
                        Person_Detector.facePositions.Add(new PointF(f.Location.X + (f.Width / 2), f.Location.Y + (f.Height / 2)));
                    }
                    detections = faces.Length;
                    positions  = facePositions;
                }



            return(copyImage);
        }
예제 #2
0
 public static void UsingGPU(Image <Bgr, Byte> image, GpuCascadeClassifier face, GpuCascadeClassifier eye, List <Rectangle> faces, List <Rectangle> eyes, out long detectionTime)
 {
     watch = Stopwatch.StartNew();
     using (GpuImage <Bgr, Byte> gpuImage = new GpuImage <Bgr, byte>(image))
         using (GpuImage <Gray, Byte> gpuGray = gpuImage.Convert <Gray, Byte>())
         {
             Rectangle[] faceRegion = face.DetectMultiScale(gpuGray, 1.1, 10, Size.Empty);
             faces.AddRange(faceRegion);
             foreach (Rectangle f in faceRegion)
             {
                 using (GpuImage <Gray, Byte> faceImg = gpuGray.GetSubRect(f))
                 {
                     //For some reason a clone is required.
                     //Might be a bug of GpuCascadeClassifier in opencv
                     using (GpuImage <Gray, Byte> clone = faceImg.Clone())
                     {
                         Rectangle[] eyeRegion = eye.DetectMultiScale(clone, 1.1, 10, Size.Empty);
                         foreach (Rectangle e in eyeRegion)
                         {
                             Rectangle eyeRect = e;
                             eyeRect.Offset(f.X, f.Y);
                             eyes.Add(eyeRect);
                         }
                     }
                 }
             }
         }
     watch.Stop();
     detectionTime = watch.ElapsedMilliseconds;
 }
예제 #3
0
        public static Rectangle[] Detect(Image <Bgr, Byte> image, string cascadeFile,
                                         double scaleFactor = 1.3, int minNeighbors = 10,
                                         Emgu.CV.CvEnum.HAAR_DETECTION_TYPE detectionType = Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                                         int minSize = 20, int maxSize = 0)
        {
            string cascadeFilePath = CascadeManager.GetCascade(cascadeFile);

            Size minimumSize;

            if (minSize == 0)
            {
                minimumSize = Size.Empty;
            }
            else
            {
                minimumSize = new Size(minSize, minSize);
            }

            Size maximumSize;

            if (maxSize == 0)
            {
                maximumSize = Size.Empty;
            }
            else
            {
                maximumSize = new Size(maxSize, maxSize);
            }

            if (GpuInvoke.HasCuda)
            {
                using (GpuCascadeClassifier cascade = new GpuCascadeClassifier(cascadeFilePath))
                    using (GpuImage <Bgr, Byte> gpuImage = new GpuImage <Bgr, byte>(image))
                        using (GpuImage <Gray, Byte> gpuGray = gpuImage.Convert <Gray, Byte>())
                        {
                            return(cascade.DetectMultiScale(gpuGray, scaleFactor, minNeighbors, minimumSize));
                        }
            }
            else
            {
                using (HaarCascade cascade = new HaarCascade(cascadeFilePath))
                    using (Image <Gray, Byte> gray = image.Convert <Gray, Byte>())
                    {
                        gray._EqualizeHist();

                        MCvAvgComp[] detected = cascade.Detect(gray,
                                                               scaleFactor, minNeighbors,
                                                               detectionType,
                                                               minimumSize, maximumSize);

                        return((from x in detected
                                select x.rect).ToArray());
                    }
            }
        }
예제 #4
0
        public static Rectangle[] Detect(Image<Bgr, Byte> image, string cascadeFile,
            double scaleFactor = 1.3, int minNeighbors = 10,
            Emgu.CV.CvEnum.HAAR_DETECTION_TYPE detectionType = Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
            int minSize = 20, int maxSize = 0)
        {
            string cascadeFilePath = CascadeManager.GetCascade(cascadeFile);

            Size minimumSize;
            if (minSize == 0)
            {
                minimumSize = Size.Empty;
            }
            else
            {
                minimumSize = new Size(minSize, minSize);
            }

            Size maximumSize;
            if (maxSize == 0)
            {
                maximumSize = Size.Empty;
            }
            else
            {
                maximumSize = new Size(maxSize, maxSize);
            }

            if (GpuInvoke.HasCuda)
            {
                using (GpuCascadeClassifier cascade = new GpuCascadeClassifier(cascadeFilePath))
                using (GpuImage<Bgr, Byte> gpuImage = new GpuImage<Bgr, byte>(image))
                using (GpuImage<Gray, Byte> gpuGray = gpuImage.Convert<Gray, Byte>())
                {
                    return cascade.DetectMultiScale(gpuGray, scaleFactor, minNeighbors, minimumSize);
                }
            }
            else
            {
                using (HaarCascade cascade = new HaarCascade(cascadeFilePath))
                using (Image<Gray, Byte> gray = image.Convert<Gray, Byte>())
                {
                    gray._EqualizeHist();

                    MCvAvgComp[] detected = cascade.Detect(gray,
                        scaleFactor, minNeighbors,
                        detectionType,
                        minimumSize, maximumSize);

                    return (from x in detected
                            select x.rect).ToArray();
                }
            }
        }
예제 #5
0
        private Rectangle[] ProcessOnGpu(Image <Gray, byte> grayImage)
        {
            if (FGpuCascadeClassifier == null)
            {
                Status = "Can't load Haar file";
                return(new Rectangle[0]);
            }

            using (var gpuImage = new GpuImage <Gray, byte>(grayImage))
            {
                return(FGpuCascadeClassifier.DetectMultiScale(gpuImage, ScaleFactor, MinNeighbors, MinSize));
            }
        }
예제 #6
0
        public static void Detect(Image<Bgr, Byte> image, String faceFileName, List<Rectangle> faces, out long detectionTime)
        {
            Stopwatch watch;

            if (GpuInvoke.HasCuda)
            {
                using (GpuCascadeClassifier face = new GpuCascadeClassifier(faceFileName))
                {
                    watch = Stopwatch.StartNew();
                    using (GpuImage<Bgr, Byte> gpuImage = new GpuImage<Bgr, byte>(image))
                    using (GpuImage<Gray, Byte> gpuGray = gpuImage.Convert<Gray, Byte>())
                    {
                        Rectangle[] faceRegion = face.DetectMultiScale(gpuGray, 1.1, 10, Size.Empty);
                        faces.AddRange(faceRegion);
                    }
                    watch.Stop();
                }
            }
            else
            {
                //Read the HaarCascade objects
                using (CascadeClassifier face = new CascadeClassifier(faceFileName))
                {
                    watch = Stopwatch.StartNew();
                    using (Image<Gray, Byte> gray = image.Convert<Gray, Byte>()) //Convert it to Grayscale
                    {
                        //normalizes brightness and increases contrast of the image
                        gray._EqualizeHist();

                        //Detect the faces  from the gray scale image and store the locations as rectangle
                        //The first dimensional is the channel
                        //The second dimension is the index of the rectangle in the specific channel
                        Rectangle[] facesDetected = face.DetectMultiScale(
                           gray,
                           1.1,
                           10,
                           new Size(20, 20),
                           Size.Empty);
                        faces.AddRange(facesDetected);
                    }
                    watch.Stop();
                }
            }
            detectionTime = watch.ElapsedMilliseconds;
        }
예제 #7
0
        public static void Detect(Image <Bgr, Byte> image, String faceFileName, List <Rectangle> faces, out long detectionTime)
        {
            Stopwatch watch;

            if (GpuInvoke.HasCuda)
            {
                using (GpuCascadeClassifier face = new GpuCascadeClassifier(faceFileName))
                {
                    watch = Stopwatch.StartNew();
                    using (GpuImage <Bgr, Byte> gpuImage = new GpuImage <Bgr, byte>(image))
                        using (GpuImage <Gray, Byte> gpuGray = gpuImage.Convert <Gray, Byte>())
                        {
                            Rectangle[] faceRegion = face.DetectMultiScale(gpuGray, 1.1, 10, Size.Empty);
                            faces.AddRange(faceRegion);
                        }
                    watch.Stop();
                }
            }
            else
            {
                //Read the HaarCascade objects
                using (CascadeClassifier face = new CascadeClassifier(faceFileName))
                {
                    watch = Stopwatch.StartNew();
                    using (Image <Gray, Byte> gray = image.Convert <Gray, Byte>()) //Convert it to Grayscale
                    {
                        //normalizes brightness and increases contrast of the image
                        gray._EqualizeHist();

                        //Detect the faces  from the gray scale image and store the locations as rectangle
                        //The first dimensional is the channel
                        //The second dimension is the index of the rectangle in the specific channel
                        Rectangle[] facesDetected = face.DetectMultiScale(
                            gray,
                            1.1,
                            10,
                            new Size(20, 20),
                            Size.Empty);
                        faces.AddRange(facesDetected);
                    }
                    watch.Stop();
                }
            }
            detectionTime = watch.ElapsedMilliseconds;
        }
        private void DetectFace(Image <Bgr, Byte> image, List <Rectangle> faces)
        {
#if !IOS
            if (GpuInvoke.HasCuda)
            {
                using (GpuCascadeClassifier face = new GpuCascadeClassifier(_faceFileName))
                {
                    using (GpuImage <Bgr, Byte> gpuImage = new GpuImage <Bgr, byte>(image))
                        using (GpuImage <Gray, Byte> gpuGray = gpuImage.Convert <Gray, Byte>())
                        {
                            Rectangle[] faceRegion = face.DetectMultiScale(gpuGray, 1.1, 10, Size.Empty);
                            faces.AddRange(faceRegion);
                        }
                }
            }
            else
#endif
            {
                //Read the HaarCascade objects
                using (CascadeClassifier face = new CascadeClassifier(_faceFileName))
                {
                    using (Image <Gray, Byte> gray = image.Convert <Gray, Byte>()) //Convert it to Grayscale
                    {
                        //normalizes brightness and increases contrast of the image
                        gray._EqualizeHist();

                        //Detect the faces  from the gray scale image and store the locations as rectangle
                        //The first dimensional is the channel
                        //The second dimension is the index of the rectangle in the specific channel
                        Rectangle[] facesDetected = face.DetectMultiScale(
                            gray,
                            1.1,
                            10,
                            new Size(20, 20),
                            Size.Empty);
                        faces.AddRange(facesDetected);
                    }
                }
            }
        }
예제 #9
0
        Image <Bgr, Byte> LBP_recognizer(Image <Bgr, Byte> image_BGR, Image <Gray, Byte> image_gray)
        {
            image_BGR  = image_BGR.Copy();
            image_gray = image_gray.Copy();
            image_gray._EqualizeHist();


            if (GPU_classifier_selector)
            {
                detected_faces = GPU_classifier.DetectMultiScale(new GpuImage <Gray, Byte>(image_gray), 1.1, 15, new Size(24, 24));
            }
            else
            {
                detected_faces = classifier.DetectMultiScale(image_gray, 1.1, 15, new Size(24, 24), Size.Empty);
            }

            foreach (var face in detected_faces)
            {
                image_gray.ROI = face;
                var result = recognizer.Predict(image_gray.Copy().Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC));
                if (result.Label == -1)
                {
                    image_BGR.Draw(new Rectangle(face.Location, face.Size), new Bgr(0, 0, 255), 3);

                    image_BGR.Draw("Unknown", ref font, new Point(face.X - 20, face.Y - 20), new Bgr(0, 0, 255));
                }
                else
                {
                    image_BGR.Draw(new Rectangle(face.Location, face.Size), new Bgr(0, 255, 0), 3);

                    image_BGR.Draw(imagesLabels[result.Label], ref font, new Point(face.X - 20, face.Y - 20), new Bgr(0, 0, 255));
                }
                //image_BGR.ROI = image_gray.ROI;
                image_gray.ROI = Rectangle.Empty;
            }
            return(image_BGR);
        }
예제 #10
0
        public void classify(BitmapSource frame)
        {
            Console.WriteLine(relativeURI);

            //byte[] classifiedImage = frame;
            //WriteableBitmap frameImage = new WriteableBitmap(frameWidth, frameHeight, 96, 96, PixelFormats.Bgr32, null);

            //BitmapSource frameImage = BitmapSource.Create(frameWidth, frameHeight, 96, 96, PixelFormats.Bgr32, null, frame, stride);

            /*
            resultsPtr = CvInvoke.cvHaarDetectObjects(
                Marshal.GetIUnknownForObject(frame),
                classifier,
                resultsPtr,
                1.1,
                3,
                Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                new System.Drawing.Size(0,0),
                new System.Drawing.Size(0,0)
            );

            Console.WriteLine("Classified?!? Pointer below: ");
            Console.WriteLine(resultsPtr.ToString());
            */
            //return classifiedImage;
            Console.WriteLine(" - - - Converting Bitmap...");
            System.Drawing.Bitmap bitmapFrame;
            using (MemoryStream outStream = new MemoryStream())
            {
                BitmapEncoder enc = new BmpBitmapEncoder();
                enc.Frames.Add(BitmapFrame.Create(frame));
                enc.Save(outStream);
                bitmapFrame = new System.Drawing.Bitmap(outStream);
            }
            Console.WriteLine(" - - - Bitmap converted!");

            Image<Bgr, Byte> image = new Image<Bgr, Byte>(bitmapFrame);

            Console.WriteLine(" - - - Image set");
            Console.WriteLine(" - - - Check CUDA...");

            if (GpuInvoke.HasCuda)
            {
                Console.WriteLine(" - - - Has CUDA!");
                using (GpuCascadeClassifier target = new GpuCascadeClassifier(classifierURI))
                {
                    using (GpuImage<Bgr, Byte> gpuImage = new GpuImage<Bgr, byte>(image))
                    using (GpuImage<Gray, Byte> gpuGray = gpuImage.Convert<Gray, Byte>())
                    {
                        Console.WriteLine(" - - - Detecting!");
                        Rectangle[] targetSet = target.DetectMultiScale(gpuGray, 1.1, 10, System.Drawing.Size.Empty);
                        Console.WriteLine(" - - - Detected :D :D :D Printing rectangle set: ");
                        foreach (Rectangle f in targetSet)
                        {
                            Console.WriteLine("Rectangle found at: " + f.ToString());
                            //draw the face detected in the 0th (gray) channel with blue color
                            image.Draw(f, new Bgr(System.Drawing.Color.Blue), 2);
                        }
                        Console.WriteLine(" - - - DONE");
                    }
                }

            }
            else
            {

                using (HOGDescriptor des = new HOGDescriptor())
                {
                    //des.SetSVMDetector
                }

                Console.WriteLine(" - - - No CUDA  :( ");
                Console.WriteLine(" - - - Devices available: " + GpuInvoke.GetCudaEnabledDeviceCount());
            }
        }
예제 #11
0
        private void DetectFaceAndEyes(Image <Bgr, byte> image, List <Rectangle> faces, List <Rectangle> eyes)
        {
#if !IOS
            if (GpuInvoke.HasCuda)
            {
                using (var face = new GpuCascadeClassifier(_faceFileName))
                    using (var eye = new GpuCascadeClassifier(_eyeFileName))
                    {
                        using (var gpuImage = new GpuImage <Bgr, byte>(image))
                            using (var gpuGray = gpuImage.Convert <Gray, byte>())
                            {
                                var faceRegion = face.DetectMultiScale(gpuGray, 1.1, 10, Size.Empty);
                                faces.AddRange(faceRegion);
                                foreach (var f in faceRegion)
                                {
                                    using (var faceImg = gpuGray.GetSubRect(f))
                                    {
                                        //For some reason a clone is required.
                                        //Might be a bug of GpuCascadeClassifier in opencv
                                        using (var clone = faceImg.Clone())
                                        {
                                            var eyeRegion = eye.DetectMultiScale(clone, 1.1, 10, Size.Empty);

                                            foreach (var e in eyeRegion)
                                            {
                                                var eyeRect = e;
                                                eyeRect.Offset(f.X, f.Y);
                                                eyes.Add(eyeRect);
                                            }
                                        }
                                    }
                                }
                            }
                    }
            }
            else
#endif
            using (var face = new CascadeClassifier(_faceFileName))
                using (var eye = new CascadeClassifier(_eyeFileName))
                {
                    using (var gray = image.Convert <Gray, byte>()) //Convert it to Grayscale
                    {
                        //normalizes brightness and increases contrast of the image
                        gray._EqualizeHist();

                        //Detect the faces  from the gray scale image and store the locations as rectangle
                        //The first dimensional is the channel
                        //The second dimension is the index of the rectangle in the specific channel
                        var facesDetected = face.DetectMultiScale(
                            gray,
                            1.1,
                            10,
                            new Size(20, 20),
                            Size.Empty);
                        faces.AddRange(facesDetected);

                        foreach (var f in facesDetected)
                        {
                            //Set the region of interest on the faces
                            gray.ROI = f;
                            var eyesDetected = eye.DetectMultiScale(
                                gray,
                                1.1,
                                10,
                                new Size(20, 20),
                                Size.Empty);
                            gray.ROI = Rectangle.Empty;

                            foreach (var e in eyesDetected)
                            {
                                var eyeRect = e;
                                eyeRect.Offset(f.X, f.Y);
                                eyes.Add(eyeRect);
                            }
                        }
                    }
                }
        }
예제 #12
0
        static void Run()
        {
            Image <Bgr, Byte> image = new Image <Bgr, byte>("lena.jpg"); //Read the files as an 8-bit Bgr image

            Stopwatch watch;
            String    faceFileName = "haarcascade_frontalface_default.xml";
            String    eyeFileName  = "haarcascade_eye.xml";

            if (GpuInvoke.HasCuda)
            {
                using (GpuCascadeClassifier face = new GpuCascadeClassifier(faceFileName))
                    using (GpuCascadeClassifier eye = new GpuCascadeClassifier(eyeFileName))
                    {
                        watch = Stopwatch.StartNew();
                        using (GpuImage <Bgr, Byte> gpuImage = new GpuImage <Bgr, byte>(image))
                            using (GpuImage <Gray, Byte> gpuGray = gpuImage.Convert <Gray, Byte>())
                            {
                                Rectangle[] faceRegion = face.DetectMultiScale(gpuGray, 1.1, 10, Size.Empty);
                                foreach (Rectangle f in faceRegion)
                                {
                                    //draw the face detected in the 0th (gray) channel with blue color
                                    image.Draw(f, new Bgr(Color.Blue), 2);
                                    using (GpuImage <Gray, Byte> faceImg = gpuGray.GetSubRect(f))
                                    {
                                        //For some reason a clone is required.
                                        //Might be a bug of GpuCascadeClassifier in opencv
                                        using (GpuImage <Gray, Byte> clone = faceImg.Clone())
                                        {
                                            Rectangle[] eyeRegion = eye.DetectMultiScale(clone, 1.1, 10, Size.Empty);

                                            foreach (Rectangle e in eyeRegion)
                                            {
                                                Rectangle eyeRect = e;
                                                eyeRect.Offset(f.X, f.Y);
                                                image.Draw(eyeRect, new Bgr(Color.Red), 2);
                                            }
                                        }
                                    }
                                }
                            }
                        watch.Stop();
                    }
            }
            else
            {
                //Read the HaarCascade objects
                using (HaarCascade face = new HaarCascade(faceFileName))
                    using (HaarCascade eye = new HaarCascade(eyeFileName))
                    {
                        watch = Stopwatch.StartNew();
                        using (Image <Gray, Byte> gray = image.Convert <Gray, Byte>()) //Convert it to Grayscale
                        {
                            //normalizes brightness and increases contrast of the image
                            gray._EqualizeHist();

                            //Detect the faces  from the gray scale image and store the locations as rectangle
                            //The first dimensional is the channel
                            //The second dimension is the index of the rectangle in the specific channel
                            MCvAvgComp[] facesDetected = face.Detect(
                                gray,
                                1.1,
                                10,
                                Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                                new Size(20, 20));

                            foreach (MCvAvgComp f in facesDetected)
                            {
                                //draw the face detected in the 0th (gray) channel with blue color
                                image.Draw(f.rect, new Bgr(Color.Blue), 2);

                                //Set the region of interest on the faces
                                gray.ROI = f.rect;
                                MCvAvgComp[] eyesDetected = eye.Detect(
                                    gray,
                                    1.1,
                                    10,
                                    Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                                    new Size(20, 20));
                                gray.ROI = Rectangle.Empty;

                                foreach (MCvAvgComp e in eyesDetected)
                                {
                                    Rectangle eyeRect = e.rect;
                                    eyeRect.Offset(f.rect.X, f.rect.Y);
                                    image.Draw(eyeRect, new Bgr(Color.Red), 2);
                                }
                            }
                        }
                        watch.Stop();
                    }
            }

            //display the image
            ImageViewer.Show(image, String.Format(
                                 "Completed face and eye detection using {0} in {1} milliseconds",
                                 GpuInvoke.HasCuda ? "GPU": "CPU",
                                 watch.ElapsedMilliseconds));
        }
예제 #13
0
        private static Rectangle[] DetectFace(Image<Bgr, Byte> image, string faceFileName)
        {
            try
            {
                if (GpuInvoke.HasCuda)
                {
                    using (GpuCascadeClassifier face = new GpuCascadeClassifier(faceFileName))
                    {
                        using (GpuImage<Bgr, Byte> gpuImage = new GpuImage<Bgr, byte>(image))
                        using (GpuImage<Gray, Byte> gpuGray = gpuImage.Convert<Gray, Byte>())
                        {
                            Rectangle[] faceRegion = face.DetectMultiScale(gpuGray, 1.1, 10, Size.Empty);

                            return faceRegion;
                        }
                    }
                }
                else
                {
                    //Read the HaarCascade objects
                    using (CascadeClassifier face = new CascadeClassifier(faceFileName))
                    {

                        using (Image<Gray, Byte> gray = image.Convert<Gray, Byte>()) //Convert it to Grayscale
                        {
                            //normalizes brightness and increases contrast of the image
                            gray._EqualizeHist();

                            //Detect the faces  from the gray scale image and store the locations as rectangle
                            //The first dimensional is the channel
                            //The second dimension is the index of the rectangle in the specific channel
                            Rectangle[] facesDetected = face.DetectMultiScale(
                               gray,
                               1.1,
                               10,
                               new Size(filterWidth, filterHeight),
                               Size.Empty);

                            return facesDetected;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #14
0
파일: DetectFace.cs 프로젝트: Raptek/STEM
 //        public static void detectFaceGPU(Image<Bgr, Byte> image, String faceFileName, String eyesFileName, List<Rectangle> facesList, List<Rectangle> eyesList, out long detectionTime)
 public static void detectFaceGPU(Image<Bgr, Byte> image, String faceFileName, String eyesFileName, List<Rectangle> facesList, out Rectangle eyeL, out Rectangle eyeR, out long detectionTime)
 {
     Stopwatch watch;
     Rectangle eyeLtmp = new Rectangle(), eyeRtmp = new Rectangle();
     using (GpuCascadeClassifier faceCascade = new GpuCascadeClassifier(faceFileName))
     using (GpuCascadeClassifier eyesCascade = new GpuCascadeClassifier(eyesFileName))
     {
         watch = Stopwatch.StartNew();
         using (GpuImage<Bgr, Byte> gpuImage = new GpuImage<Bgr,byte>(image))
         using (GpuImage<Gray, Byte> grayImage = gpuImage.Convert<Gray, Byte>())
         {
             Rectangle[] facesRegion = faceCascade.DetectMultiScale(grayImage, 1.1, 10, new Size(image.Width / 8, image.Height / 8));
             facesList.AddRange(facesRegion);
             foreach (Rectangle f in facesRegion)
             {
                 #region ROI dla oczu
                 Point eyesBoxTopLeft = new Point(f.X, f.Top + f.Height / 4);
                 Size eyesBoxArea = new Size(f.Width, f.Height / 4);
                 Rectangle eyesBox = new Rectangle(eyesBoxTopLeft, eyesBoxArea);
                 Size rightEyeBoxArea = new Size(eyesBox.Width / 2, eyesBox.Height);
                 Rectangle rightEyeBox = new Rectangle(eyesBoxTopLeft, rightEyeBoxArea);
                 Size leftEyeBoxArea = new Size(eyesBox.Width / 2, eyesBox.Height);
                 Rectangle leftEyeBox = new Rectangle(new Point(eyesBoxTopLeft.X + leftEyeBoxArea.Width, eyesBoxTopLeft.Y), leftEyeBoxArea);
                 #endregion
                 #region Prawe oko
                 using (GpuImage<Gray, Byte> faceImg = grayImage.GetSubRect(rightEyeBox))
                 {
                     using (GpuImage<Gray, Byte> clone = faceImg.Clone())
                     {
                         Rectangle[] eyeRegion = eyesCascade.DetectMultiScale(clone, 1.1, 10, new Size(30, 30));
                         foreach (Rectangle e in eyeRegion)
                         {
                             Rectangle eyeRect = e;
                             eyeRect.Offset(rightEyeBox.X, rightEyeBox.Y);
                             eyeRtmp = eyeRect;
                         }
                     }
                 }
                 #endregion
                 #region Lewe oko
                 using (GpuImage<Gray, Byte> faceImg = grayImage.GetSubRect(leftEyeBox))
                 {
                     using (GpuImage<Gray, Byte> clone = faceImg.Clone())
                     {
                         Rectangle[] eyeRegion = eyesCascade.DetectMultiScale(clone, 1.1, 10, new Size(30, 30));
                         foreach (Rectangle e in eyeRegion)
                         {
                             Rectangle eyeRect = e;
                             eyeRect.Offset(leftEyeBox.X, leftEyeBox.Y);
                             eyeLtmp = eyeRect;
                         }
                     }
                 }
                 #endregion
             }
         }
         watch.Stop();
     }
     detectionTime = watch.ElapsedMilliseconds;
     eyeL = eyeLtmp;
     eyeR = eyeRtmp;
 }
예제 #15
0
        public void classify(BitmapSource frame)
        {
            Console.WriteLine(relativeURI);

            //byte[] classifiedImage = frame;
            //WriteableBitmap frameImage = new WriteableBitmap(frameWidth, frameHeight, 96, 96, PixelFormats.Bgr32, null);

            //BitmapSource frameImage = BitmapSource.Create(frameWidth, frameHeight, 96, 96, PixelFormats.Bgr32, null, frame, stride);

            /*
             * resultsPtr = CvInvoke.cvHaarDetectObjects(
             *  Marshal.GetIUnknownForObject(frame),
             *  classifier,
             *  resultsPtr,
             *  1.1,
             *  3,
             *  Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
             *  new System.Drawing.Size(0,0),
             *  new System.Drawing.Size(0,0)
             * );
             *
             * Console.WriteLine("Classified?!? Pointer below: ");
             * Console.WriteLine(resultsPtr.ToString());
             */
            //return classifiedImage;
            Console.WriteLine(" - - - Converting Bitmap...");
            System.Drawing.Bitmap bitmapFrame;
            using (MemoryStream outStream = new MemoryStream())
            {
                BitmapEncoder enc = new BmpBitmapEncoder();
                enc.Frames.Add(BitmapFrame.Create(frame));
                enc.Save(outStream);
                bitmapFrame = new System.Drawing.Bitmap(outStream);
            }
            Console.WriteLine(" - - - Bitmap converted!");

            Image <Bgr, Byte> image = new Image <Bgr, Byte>(bitmapFrame);

            Console.WriteLine(" - - - Image set");
            Console.WriteLine(" - - - Check CUDA...");

            if (GpuInvoke.HasCuda)
            {
                Console.WriteLine(" - - - Has CUDA!");
                using (GpuCascadeClassifier target = new GpuCascadeClassifier(classifierURI))
                {
                    using (GpuImage <Bgr, Byte> gpuImage = new GpuImage <Bgr, byte>(image))
                        using (GpuImage <Gray, Byte> gpuGray = gpuImage.Convert <Gray, Byte>())
                        {
                            Console.WriteLine(" - - - Detecting!");
                            Rectangle[] targetSet = target.DetectMultiScale(gpuGray, 1.1, 10, System.Drawing.Size.Empty);
                            Console.WriteLine(" - - - Detected :D :D :D Printing rectangle set: ");
                            foreach (Rectangle f in targetSet)
                            {
                                Console.WriteLine("Rectangle found at: " + f.ToString());
                                //draw the face detected in the 0th (gray) channel with blue color
                                image.Draw(f, new Bgr(System.Drawing.Color.Blue), 2);
                            }
                            Console.WriteLine(" - - - DONE");
                        }
                }
            }
            else
            {
                using (HOGDescriptor des = new HOGDescriptor())
                {
                    //des.SetSVMDetector
                }

                Console.WriteLine(" - - - No CUDA  :( ");
                Console.WriteLine(" - - - Devices available: " + GpuInvoke.GetCudaEnabledDeviceCount());
            }
        }
예제 #16
0
        public static void Detect(Image <Bgr, Byte> image, String faceFileName, String eyeFileName, List <Rectangle> faces, List <Rectangle> eyes, out long detectionTime)
        {
            Stopwatch watch;

            if (GpuInvoke.HasCuda)
            {
                using (GpuCascadeClassifier face = new GpuCascadeClassifier(faceFileName))
                    using (GpuCascadeClassifier eye = new GpuCascadeClassifier(eyeFileName))
                    {
                        watch = Stopwatch.StartNew();
                        using (GpuImage <Bgr, Byte> gpuImage = new GpuImage <Bgr, byte>(image))
                            using (GpuImage <Gray, Byte> gpuGray = gpuImage.Convert <Gray, Byte>())
                            {
                                Rectangle[] faceRegion = face.DetectMultiScale(gpuGray, 1.4, 4, Size.Empty);
                                faces.AddRange(faceRegion);
                                foreach (Rectangle f in faceRegion)
                                {
                                    using (GpuImage <Gray, Byte> faceImg = gpuGray.GetSubRect(f))
                                    {
                                        //For some reason a clone is required.
                                        //Might be a bug of GpuCascadeClassifier in opencv
                                        using (GpuImage <Gray, Byte> clone = faceImg.Clone(null))
                                        {
                                            Rectangle[] eyeRegion = eye.DetectMultiScale(clone, 1.4, 4, Size.Empty);

                                            foreach (Rectangle e in eyeRegion)
                                            {
                                                //Rectangle eyeRect = e;
                                                //eyeRect.Offset(f.X, f.Y);
                                                //eyes.Add(eyeRect);
                                            }
                                        }
                                    }
                                }
                            }
                        watch.Stop();
                    }
            }
            else
            {
                //Read the HaarCascade objects
                using (CascadeClassifier face = new CascadeClassifier(faceFileName))
                    using (CascadeClassifier eye = new CascadeClassifier(eyeFileName))
                    {
                        watch = Stopwatch.StartNew();
                        using (Image <Gray, Byte> gray = image.Convert <Gray, Byte>()) //Convert it to Grayscale
                        {
                            //normalizes brightness and increases contrast of the image
                            gray._EqualizeHist();

                            //Detect the faces  from the gray scale image and store the locations as rectangle
                            //The first dimensional is the channel
                            //The second dimension is the index of the rectangle in the specific channel
                            Rectangle[] facesDetected = face.DetectMultiScale(
                                gray,
                                1.3,
                                4,
                                new Size(20, 20),
                                Size.Empty);
                            faces.AddRange(facesDetected);

                            foreach (Rectangle f in facesDetected)
                            {
                                //Set the region of interest on the faces
                                gray.ROI = f;
                                Rectangle[] eyesDetected = eye.DetectMultiScale(
                                    gray,
                                    1.4,
                                    10,
                                    new Size(20, 20),
                                    Size.Empty);
                                gray.ROI = Rectangle.Empty;

                                foreach (Rectangle e in eyesDetected)
                                {
                                    //Rectangle eyeRect = e;
                                    //eyeRect.Offset(f.X, f.Y);
                                    //eyes.Add(eyeRect);
                                }
                            }
                        }
                        watch.Stop();
                    }
            }
            detectionTime = watch.ElapsedMilliseconds;
        }
예제 #17
0
        public static void Detect(Image<Bgr, Byte> image, String faceFileName, String eyeFileName, List<Rectangle> faces, List<Rectangle> eyes, out long detectionTime)
        {
            Stopwatch watch;

             if (GpuInvoke.HasCuda)
             {
            using (GpuCascadeClassifier face = new GpuCascadeClassifier(faceFileName))
            using (GpuCascadeClassifier eye = new GpuCascadeClassifier(eyeFileName))
            {
               watch = Stopwatch.StartNew();
               using (GpuImage<Bgr, Byte> gpuImage = new GpuImage<Bgr, byte>(image))
               using (GpuImage<Gray, Byte> gpuGray = gpuImage.Convert<Gray, Byte>())
               {
                  Rectangle[] faceRegion = face.DetectMultiScale(gpuGray, 1.1, 10, Size.Empty);
                  faces.AddRange(faceRegion);
                  foreach (Rectangle f in faceRegion)
                  {
                     using (GpuImage<Gray, Byte> faceImg = gpuGray.GetSubRect(f))
                     {
                        //For some reason a clone is required.
                        //Might be a bug of GpuCascadeClassifier in opencv
                        using (GpuImage<Gray, Byte> clone = faceImg.Clone())
                        {
                           Rectangle[] eyeRegion = eye.DetectMultiScale(clone, 1.1, 10, Size.Empty);

                           foreach (Rectangle e in eyeRegion)
                           {
                              Rectangle eyeRect = e;
                              eyeRect.Offset(f.X, f.Y);
                              eyes.Add(eyeRect);
                           }
                        }
                     }
                  }
               }
               watch.Stop();
            }
             }
             else
             {
            //Read the HaarCascade objects
            using (CascadeClassifier face = new CascadeClassifier(faceFileName))
            using (CascadeClassifier eye = new CascadeClassifier(eyeFileName))
            {
               watch = Stopwatch.StartNew();
               using (Image<Gray, Byte> gray = image.Convert<Gray, Byte>()) //Convert it to Grayscale
               {
                  //normalizes brightness and increases contrast of the image
                  gray._EqualizeHist();

                  //Detect the faces  from the gray scale image and store the locations as rectangle
                  //The first dimensional is the channel
                  //The second dimension is the index of the rectangle in the specific channel
                  Rectangle[] facesDetected = face.DetectMultiScale(
                     gray,
                     1.1,
                     10,
                     new Size(20, 20),
                     Size.Empty);
                  faces.AddRange(facesDetected);

                  foreach (Rectangle f in facesDetected)
                  {
                     //Set the region of interest on the faces
                     gray.ROI = f;
                     Rectangle[] eyesDetected = eye.DetectMultiScale(
                        gray,
                        1.1,
                        10,
                        new Size(20, 20),
                        Size.Empty);
                     gray.ROI = Rectangle.Empty;

                     foreach (Rectangle e in eyesDetected)
                     {
                        Rectangle eyeRect = e;
                        eyeRect.Offset(f.X, f.Y);
                        eyes.Add(eyeRect);
                     }
                  }
               }
               watch.Stop();
            }
             }
             detectionTime = watch.ElapsedMilliseconds;
        }
예제 #18
0
        static void Run()
        {
            Image<Bgr, Byte> image = new Image<Bgr, byte>("lena.jpg"); //Read the files as an 8-bit Bgr image

             Stopwatch watch;
             String faceFileName = "haarcascade_frontalface_default.xml";
             String eyeFileName = "haarcascade_eye.xml";

             if (GpuInvoke.HasCuda)
             {
            using (GpuCascadeClassifier face = new GpuCascadeClassifier(faceFileName))
            using (GpuCascadeClassifier eye = new GpuCascadeClassifier(eyeFileName))
            {
               watch = Stopwatch.StartNew();
               using (GpuImage<Bgr, Byte> gpuImage = new GpuImage<Bgr, byte>(image))
               using (GpuImage<Gray, Byte> gpuGray = gpuImage.Convert<Gray, Byte>())
               {
                  Rectangle[] faceRegion = face.DetectMultiScale(gpuGray, 1.1, 10, Size.Empty);
                  foreach (Rectangle f in faceRegion)
                  {
                     //draw the face detected in the 0th (gray) channel with blue color
                     image.Draw(f, new Bgr(Color.Blue), 2);
                     using (GpuImage<Gray, Byte> faceImg = gpuGray.GetSubRect(f))
                     {
                        //For some reason a clone is required.
                        //Might be a bug of GpuCascadeClassifier in opencv
                        using (GpuImage<Gray, Byte> clone = faceImg.Clone())
                        {
                           Rectangle[] eyeRegion = eye.DetectMultiScale(clone, 1.1, 10, Size.Empty);

                           foreach (Rectangle e in eyeRegion)
                           {
                              Rectangle eyeRect = e;
                              eyeRect.Offset(f.X, f.Y);
                              image.Draw(eyeRect, new Bgr(Color.Red), 2);
                           }
                        }
                     }
                  }
               }
               watch.Stop();
            }
             }
             else
             {
            //Read the HaarCascade objects
            using(HaarCascade face = new HaarCascade(faceFileName))
            using(HaarCascade eye = new HaarCascade(eyeFileName))
            {
               watch = Stopwatch.StartNew();
               using (Image<Gray, Byte> gray = image.Convert<Gray, Byte>()) //Convert it to Grayscale
               {
                  //normalizes brightness and increases contrast of the image
                  gray._EqualizeHist();

                  //Detect the faces  from the gray scale image and store the locations as rectangle
                  //The first dimensional is the channel
                  //The second dimension is the index of the rectangle in the specific channel
                  MCvAvgComp[] facesDetected = face.Detect(
                     gray,
                     1.1,
                     10,
                     Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                     new Size(20, 20));

                  foreach (MCvAvgComp f in facesDetected)
                  {
                     //draw the face detected in the 0th (gray) channel with blue color
                     image.Draw(f.rect, new Bgr(Color.Blue), 2);

                     //Set the region of interest on the faces
                     gray.ROI = f.rect;
                     MCvAvgComp[] eyesDetected = eye.Detect(
                        gray,
                        1.1,
                        10,
                        Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                        new Size(20, 20));
                     gray.ROI = Rectangle.Empty;

                     foreach (MCvAvgComp e in eyesDetected)
                     {
                        Rectangle eyeRect = e.rect;
                        eyeRect.Offset(f.rect.X, f.rect.Y);
                        image.Draw(eyeRect, new Bgr(Color.Red), 2);
                     }
                  }
               }
               watch.Stop();
            }
             }

             //display the image
             ImageViewer.Show(image, String.Format(
            "Completed face and eye detection using {0} in {1} milliseconds",
            GpuInvoke.HasCuda ? "GPU": "CPU",
            watch.ElapsedMilliseconds));
        }