Pedestrian Detection in CSharp

From Emgu CV: OpenCV in .NET (C#, VB, C++ and more)
Jump to navigation Jump to search

System Requirement

Component Requirement Detail
Emgu CV Version 2.2.1 For GPU mode to be enabled please make sure the latest cuda graphic card driver from NVIDIA is installed.
Operation System Cross Platform

Source code

      static void Run()
      {
         Image<Bgr, Byte> image = new Image<Bgr, byte>("pedestrian.png");

         Stopwatch watch;  
         Rectangle[] regions;

         //check if there is a compatible GPU to run pedestrian detection
         if (GpuInvoke.HasCuda) 
         {  //this is the GPU version
            using (GpuHOGDescriptor des = new GpuHOGDescriptor())
            {
               des.SetSVMDetector(GpuHOGDescriptor.GetDefaultPeopleDetector());

               watch = Stopwatch.StartNew();
               using (GpuImage<Bgr, Byte> gpuImg = new GpuImage<Bgr, byte>(image))
               using (GpuImage<Bgra, Byte> gpuBgra = gpuImg.Convert<Bgra, Byte>())
               {
                  regions = des.DetectMultiScale(gpuBgra);
               }
            }
         }
         else
         {  //this is the CPU version
            using (HOGDescriptor des = new HOGDescriptor())
            {
               des.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector());

               watch = Stopwatch.StartNew();
               regions = des.DetectMultiScale(image);
            }
         }
         watch.Stop();

         foreach (Rectangle pedestrain in regions)
         {
            image.Draw(pedestrain, new Bgr(Color.Red), 1);
         }

         ImageViewer.Show(
            image,
            String.Format("Pedestrain detection using {0} in {1} milliseconds.", 
               GpuInvoke.HasCuda ? "GPU" : "CPU", 
               watch.ElapsedMilliseconds));
      }

Performance Comparison

We have setup two workstations to test the performance of the pedestrian detection code. The first workstation is a Lenovo W510 laptop, equipped with a fast Core i7 Q720 CPU and a relatively slow Quadro FX 880M GPU. The second workstation is a white box desktop with a slower Pentium D and a EVGA GeForce GTS 450 FPB GPU. This graphic card was purchased recently for ~$100 USD and harness 192 CUDA cores running at 882MHz speed. This mid-range graphic card (using NVidia's Fermi architecture) is a good candidate for GPU processing consider its price point. As a comparison, the Quadro FX 880M has 48 CUDA cores at 550MHz and is quite under power for GPU processing. For optimal performance, the GeForce GTX 580 is available with more than twice the performance compares with GTS 450 according to NVidia's web site, Or the Telsa product line is available for mission critical application.

We run the example several times and the execution time for the 5th run is measured (such that the image and the binary should be cached in the memory).

CPU GPU Emgu CV Package Execution Time (millisecond)
Core i7 Q720@1.6Ghz NVidia Quadro FX 880M libemgucv-windows-x64-2.2.1.1150 760
Core i7 Q720@1.6Ghz NVidia Quadro FX 880M libemgucv-windows-x64-tbb-ipp-icc--2.2.2.1195 307
Core i7 Q720@1.6Ghz NVidia Quadro FX 880M libemgucv-windows-x64-gpu-2.2.1.1150 424
Core i7 Q720@1.6Ghz NVidia Quadro FX 880M libemgucv-windows-x64-gpu-tbb-ipp-icc-2.2.1.1150 423
Pentium D 920@2.8Ghz NVidia GeForce GTS 450 libemgucv-windows-x86-2.2.1.1150 2037
Pentium D 920@2.8Ghz NVidia GeForce GTS 450 libemgucv-windows-x86-tbb-ipp-icc-2.2.1.1150 1402
Pentium D 920@2.8Ghz NVidia GeForce GTS 450 libemgucv-windows-x86-gpu-2.2.1.1150 126
Pentium D 920@2.8Ghz NVidia GeForce GTS 450 libemgucv-windows-x86-gpu-tbb-ipp-icc-2.2.1.1150 124

Result

Pedestrian Detection