예제 #1
0
        public void TestHOG1()
        {
            if (CudaInvoke.HasCuda)
            {
                using (CudaHOG hog = new CudaHOG(new Size(64, 128), new Size(16, 16), new Size(8, 8), new Size(8, 8), 9))
                    using (Mat pedestrianDescriptor = hog.GetDefaultPeopleDetector())
                        using (Image <Bgr, Byte> image = new Image <Bgr, byte>("pedestrian.png"))
                        {
                            hog.SetSVMDetector(pedestrianDescriptor);
                            //hog.GroupThreshold = 0;
                            Stopwatch   watch = Stopwatch.StartNew();
                            Rectangle[] rects;
                            using (CudaImage <Bgr, Byte> CudaImage = new CudaImage <Bgr, byte>(image))
                                using (CudaImage <Bgra, Byte> gpuBgra = CudaImage.Convert <Bgra, Byte>())
                                    using (VectorOfRect vRect = new VectorOfRect())
                                    {
                                        hog.DetectMultiScale(gpuBgra, vRect);
                                        rects = vRect.ToArray();
                                    }
                            watch.Stop();

                            Assert.AreEqual(1, rects.Length);

                            foreach (Rectangle rect in rects)
                            {
                                image.Draw(rect, new Bgr(Color.Red), 1);
                            }
                            Trace.WriteLine(String.Format("HOG detection time: {0} ms", watch.ElapsedMilliseconds));

                            //ImageViewer.Show(image, String.Format("Detection Time: {0}ms", watch.ElapsedMilliseconds));
                        }
            }
        }
예제 #2
0
        /// <summary>
        /// Find the pedestrian in the image
        /// </summary>
        /// <param name="image">The image</param>
        /// <returns>The region where pedestrians are detected</returns>
        public static Rectangle[] Find(IInputArray image, HOGDescriptor hog, CudaHOG hogCuda = null)
        {
            Rectangle[] regions;

            using (InputArray iaImage = image.GetInputArray())
            {
                //if the input array is a GpuMat
                //check if there is a compatible Cuda device to run pedestrian detection
                if (iaImage.Kind == InputArray.Type.CudaGpuMat && hogCuda != null)
                {
                    //this is the Cuda version
                    using (GpuMat cudaBgra = new GpuMat())
                        using (VectorOfRect vr = new VectorOfRect())
                        {
                            CudaInvoke.CvtColor(image, cudaBgra, ColorConversion.Bgr2Bgra);
                            hogCuda.DetectMultiScale(cudaBgra, vr);
                            regions = vr.ToArray();
                        }
                }
                else
                {
                    //this is the CPU/OpenCL version
                    MCvObjectDetection[] results = hog.DetectMultiScale(image);
                    regions = new Rectangle[results.Length];
                    for (int i = 0; i < results.Length; i++)
                    {
                        regions[i] = results[i].Rect;
                    }
                }

                return(regions);
            }
        }
예제 #3
0
        /// <summary>
        /// Find the pedestrian in the image
        /// </summary>
        /// <param name="image">The image</param>
        /// <param name="processingTime">The pedestrian detection time in milliseconds</param>
        /// <returns>The region where pedestrians are detected</returns>
        public static Rectangle[] Find(Mat image, bool tryUseCuda, bool tryUseOpenCL, out long processingTime)
        {
            Stopwatch watch;

            Rectangle[] regions;

#if !(IOS || NETFX_CORE)
            //check if there is a compatible Cuda device to run pedestrian detection
            if (tryUseCuda && CudaInvoke.HasCuda)
            { //this is the Cuda version
                using (CudaHOG des = new CudaHOG(new Size(64, 128), new Size(16, 16), new Size(8, 8), new Size(8, 8)))
                {
                    des.SetSVMDetector(des.GetDefaultPeopleDetector());

                    watch = Stopwatch.StartNew();
                    using (GpuMat cudaBgr = new GpuMat(image))
                        using (GpuMat cudaBgra = new GpuMat())
                            using (VectorOfRect vr = new VectorOfRect())
                            {
                                CudaInvoke.CvtColor(cudaBgr, cudaBgra, ColorConversion.Bgr2Bgra);
                                des.DetectMultiScale(cudaBgra, vr);
                                regions = vr.ToArray();
                            }
                }
            }
            else
#endif
            {
                //Many opencl functions require opencl compatible gpu devices.
                //As of opencv 3.0-alpha, opencv will crash if opencl is enable and only opencv compatible cpu device is presented
                //So we need to call CvInvoke.HaveOpenCLCompatibleGpuDevice instead of CvInvoke.HaveOpenCL (which also returns true on a system that only have cpu opencl devices).
                CvInvoke.UseOpenCL = tryUseOpenCL && CvInvoke.HaveOpenCLCompatibleGpuDevice;

                //this is the CPU/OpenCL version
                using (HOGDescriptor des = new HOGDescriptor())
                {
                    des.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector());

                    //load the image to umat so it will automatically use opencl is available
                    UMat umat = image.ToUMat(AccessType.Read);

                    watch = Stopwatch.StartNew();

                    MCvObjectDetection[] results = des.DetectMultiScale(umat);
                    regions = new Rectangle[results.Length];
                    for (int i = 0; i < results.Length; i++)
                    {
                        regions[i] = results[i].Rect;
                    }
                    watch.Stop();
                }
            }

            processingTime = watch.ElapsedMilliseconds;

            return(regions);
        }
        /// <summary>
        /// Find the pedestrian in the image
        /// </summary>
        /// <param name="image">The image</param>
        /// <param name="processingTime">The pedestrian detection time in milliseconds</param>
        /// <returns>The region where pedestrians are detected</returns>
        public static Rectangle[] Find(Mat image, bool tryUseCuda, out long processingTime)
        {
            Stopwatch watch;

            Rectangle[] regions;

#if !(__IOS__ || NETFX_CORE)
            //check if there is a compatible Cuda device to run pedestrian detection
            if (tryUseCuda && CudaInvoke.HasCuda)
            { //this is the Cuda version
                using (CudaHOG des = new CudaHOG(new Size(64, 128), new Size(16, 16), new Size(8, 8), new Size(8, 8)))
                {
                    des.SetSVMDetector(des.GetDefaultPeopleDetector());

                    watch = Stopwatch.StartNew();
                    using (GpuMat cudaBgr = new GpuMat(image))
                        using (GpuMat cudaBgra = new GpuMat())
                            using (VectorOfRect vr = new VectorOfRect())
                            {
                                CudaInvoke.CvtColor(cudaBgr, cudaBgra, ColorConversion.Bgr2Bgra);
                                des.DetectMultiScale(cudaBgra, vr);
                                regions = vr.ToArray();
                            }
                }
            }
            else
#endif
            {
                //this is the CPU/OpenCL version
                using (HOGDescriptor des = new HOGDescriptor())
                {
                    des.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector());

                    //load the image to umat so it will automatically use opencl is available
                    UMat umat = image.ToUMat(AccessType.Read);

                    watch = Stopwatch.StartNew();

                    MCvObjectDetection[] results = des.DetectMultiScale(umat);
                    regions = new Rectangle[results.Length];
                    for (int i = 0; i < results.Length; i++)
                    {
                        regions[i] = results[i].Rect;
                    }
                    watch.Stop();
                }
            }

            processingTime = watch.ElapsedMilliseconds;

            return(regions);
        }
예제 #5
0
        /// <summary>
        /// Find the pedestrian in the image
        /// </summary>
        /// <param name="image">The image</param>
        /// <param name="processingTime">The processing time in milliseconds</param>
        /// <returns>The region where pedestrians are detected</returns>
        public static Rectangle[] Find(IInputArray image, out long processingTime)
        {
            Stopwatch watch;

            Rectangle[] regions;

            using (InputArray iaImage = image.GetInputArray())
            {
#if !(__IOS__ || NETFX_CORE)
                //if the input array is a GpuMat
                //check if there is a compatible Cuda device to run pedestrian detection
                if (iaImage.Kind == InputArray.Type.CudaGpuMat)
                {
                    //this is the Cuda version
                    using (CudaHOG des = new CudaHOG(new Size(64, 128), new Size(16, 16), new Size(8, 8), new Size(8, 8)))
                    {
                        des.SetSVMDetector(des.GetDefaultPeopleDetector());

                        watch = Stopwatch.StartNew();
                        using (GpuMat cudaBgra = new GpuMat())
                            using (VectorOfRect vr = new VectorOfRect())
                            {
                                CudaInvoke.CvtColor(image, cudaBgra, ColorConversion.Bgr2Bgra);
                                des.DetectMultiScale(cudaBgra, vr);
                                regions = vr.ToArray();
                            }
                    }
                }
                else
#endif
                {
                    //this is the CPU/OpenCL version
                    using (HOGDescriptor des = new HOGDescriptor())
                    {
                        des.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector());
                        watch = Stopwatch.StartNew();

                        MCvObjectDetection[] results = des.DetectMultiScale(image);
                        regions = new Rectangle[results.Length];
                        for (int i = 0; i < results.Length; i++)
                        {
                            regions[i] = results[i].Rect;
                        }
                        watch.Stop();
                    }
                }

                processingTime = watch.ElapsedMilliseconds;

                return(regions);
            }
        }
예제 #6
0
        public List <IImage> ProcessFrame(IImage original)
        {
            Rectangle[] peopleRegion;

            using (InputArray iaImage = original.GetInputArray())
            {
#if !(__IOS__ || NETFX_CORE)
                //if the input array is a GpuMat
                //check if there is a compatible Cuda device to run pedestrian detection
                if (iaImage.Kind == InputArray.Type.CudaGpuMat)
                {
                    //this is the Cuda version
                    using (CudaHOG des = new CudaHOG(new Size(64, 128), new Size(16, 16), new Size(8, 8), new Size(8, 8)))
                    {
                        des.SetSVMDetector(des.GetDefaultPeopleDetector());

                        using (GpuMat cudaBgra = new GpuMat())
                            using (VectorOfRect vr = new VectorOfRect())
                            {
                                CudaInvoke.CvtColor(original, cudaBgra, ColorConversion.Bgr2Bgra);
                                des.DetectMultiScale(cudaBgra, vr);
                                peopleRegion = vr.ToArray();
                            }
                    }
                }
                else
#endif
                {
                    //this is the CPU/OpenCL version
                    using (HOGDescriptor peopleDescriptor = new HOGDescriptor())
                    {
                        peopleDescriptor.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector());

                        MCvObjectDetection[] peopleFound = peopleDescriptor
                                                           .DetectMultiScale(original, 0, default(Size), default(Size), AdjustableParameters["Scale"].CurrentValue, AdjustableParameters["SimilarityThreshold"].CurrentValue, AdjustableParameters["MeanShiftGrouping"].CurrentValue == 1);
                        peopleRegion = new Rectangle[peopleFound.Length];
                        for (int i = 0; i < peopleFound.Length; i++)
                        {
                            peopleRegion[i] = peopleFound[i].Rect;
                        }
                    }
                }

                IImage copy = CopyAndDraw(original, peopleRegion);
                return(new List <IImage> {
                    copy
                });
            }
        }
예제 #7
0
        /// <summary>
        /// Initialize the pedestrian detection model
        /// </summary>
        /// <param name="onDownloadProgressChanged">Call back method during download</param>
        /// <param name="initOptions">Initialization options. None supported at the moment, any value passed will be ignored.</param>
        /// <returns>Asyn task</returns>
        public async Task Init(DownloadProgressChangedEventHandler onDownloadProgressChanged = null, Object initOptions = null)
        {
            _hog = new HOGDescriptor();
            _hog.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector());

            if (CudaInvoke.HasCuda)
            {
                _hogCuda = new CudaHOG(
                    new Size(64, 128),
                    new Size(16, 16),
                    new Size(8, 8),
                    new Size(8, 8));
                _hogCuda.SetSVMDetector(_hogCuda.GetDefaultPeopleDetector());
            }
        }
예제 #8
0
        public void InitalizeBodyTracker()
        {
            if (!Global.canRunCuda)
            {
                return;
            }

            // Used when body identification with GPU
            Size winSize     = new Size(64, 128);
            Size blockSize   = new Size(16, 16);
            Size blockStride = new Size(8, 8);
            Size cellSize    = new Size(8, 8);
            int  nBins       = 9;

            des = new CudaHOG(winSize, blockSize, blockStride, cellSize, nBins);
            des.HitThreshold = 0;

            des.GroupThreshold = 0;
        }
예제 #9
0
        public async Task Init(DownloadProgressChangedEventHandler onDownloadProgressChanged = null, Object initOptions = null)
#endif
        {
            _hog = new HOGDescriptor();
            _hog.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector());

#if (UNITY_EDITOR || UNITY_IOS || UNITY_ANDROID || UNITY_STANDALONE)
            yield return(null);
#else
            if (CudaInvoke.HasCuda)
            {
                _hogCuda = new CudaHOG(
                    new Size(64, 128),
                    new Size(16, 16),
                    new Size(8, 8),
                    new Size(8, 8));
                _hogCuda.SetSVMDetector(_hogCuda.GetDefaultPeopleDetector());
            }
#endif
        }
예제 #10
0
        public Rectangle[] Detect(Image <Gray, byte> grayframe)
        {
            using (CudaHOG des = new CudaHOG(new Size(64, 128), new Size(16, 16), new Size(8, 8), new Size(8, 8)))
            {
                des.SetSVMDetector(des.GetDefaultPeopleDetector());

                using (GpuMat cudaBgra = new GpuMat())
                {
                    using (VectorOfRect vr = new VectorOfRect())
                    {
                        //CudaInvoke.CvtColor(grayframe, cudaBgra, ColorConversion.Bgr2Bgra);
                        cudaBgra.Upload(grayframe);
                        //CudaInvoke.CvtColor(grayframe, cudaBgra, ColorConversion.Gray2Bgra);
                        des.DetectMultiScale(cudaBgra, vr);
                        var regions = vr.ToArray();

                        return(regions);
                    }
                }
            }
        }
예제 #11
0
        public void TestHOG2()
        {
            if (CudaInvoke.HasCuda)
            {
                using (CudaHOG hog = new CudaHOG(
                           new Size(48, 96), //winSize
                           new Size(16, 16), //blockSize
                           new Size(8, 8),   //blockStride
                           new Size(8, 8)    //cellSize
                           ))
                    using (Mat pedestrianDescriptor = hog.GetDefaultPeopleDetector())
                        using (Image <Bgr, Byte> image = new Image <Bgr, byte>("pedestrian.png"))
                        {
                            //float[] pedestrianDescriptor = CudaHOGDescriptor.GetPeopleDetector48x96();
                            hog.SetSVMDetector(pedestrianDescriptor);

                            Stopwatch   watch = Stopwatch.StartNew();
                            Rectangle[] rects;
                            using (GpuMat cudaImage = new GpuMat(image))
                                using (GpuMat gpuBgra = new GpuMat())
                                    using (VectorOfRect vRect = new VectorOfRect())
                                    {
                                        CudaInvoke.CvtColor(cudaImage, gpuBgra, ColorConversion.Bgr2Bgra);
                                        hog.DetectMultiScale(gpuBgra, vRect);
                                        rects = vRect.ToArray();
                                    }
                            watch.Stop();

                            //Assert.AreEqual(1, rects.Length);

                            foreach (Rectangle rect in rects)
                            {
                                image.Draw(rect, new Bgr(Color.Red), 1);
                            }
                            Trace.WriteLine(String.Format("HOG detection time: {0} ms", watch.ElapsedMilliseconds));

                            //ImageViewer.Show(image, String.Format("Detection Time: {0}ms", watch.ElapsedMilliseconds));
                        }
            }
        }