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.4.0 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

Emgu CV 4.x

Click to view source code

In the 4.5.3 release, you can use PedestrianDetector class under the Emgu.CV.Models namespace. If you are using nuget, please install the Emgu.CV.Models nuget package. The Init() function will download the models using the internet connection.

using Emgu.CV.Models;
...
            using (Emgu.CV.Models.PedestrianDetector detector = new PedestrianDetector())
            {
                await detector.Init();
                String text = detector.ProcessAndRender(imageIn, imageOut);
            }

Emgu CV 2.x

Click to view source code

using System;
using System.Collections.Generic;
using Emgu.CV;
using Emgu.CV.Structure;
using System.Drawing;
using System.Diagnostics;
using Emgu.CV.GPU;

namespace PedestrianDetection
{
   public static class FindPedestrian
   {
      /// <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 image with pedestrian highlighted.</returns>
      public static Image<Bgr, Byte> Find(Image<Bgr, Byte> image, out long processingTime)
      {
         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();

         processingTime = watch.ElapsedMilliseconds;

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

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
Google Nexus S (Android) libemgucv-android-2.4.0.1717_beta 19169
LG G Flex 2 (Android) libemgucv-android-3.1.0.2298 272

Result

  • Windows
Pedestrian Detection
  • Android (Nexus S)
MonoAndroidPedestrianDetectionResultNexusS.jpg