Difference between revisions of "Pedestrian Detection in CSharp"

From Emgu CV: OpenCV in .NET (C#, VB, C++ and more)
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
!Component || Requirement || Detail  
 
!Component || Requirement || Detail  
 
|-
 
|-
|Emgu CV || [[Version_History#Emgu.CV-2.2.1|Version 2.2.1]] || For GPU mode to be enabled please make sure the [http://developer.nvidia.com/object/cuda_3_2_downloads.html latest cuda graphic card driver] from NVIDIA is installed.
+
|Emgu CV || [[Version_History#Emgu.CV-2.4.0|Version 2.4.0]] || For GPU mode to be enabled please make sure the [http://developer.nvidia.com/cuda-downloads latest cuda graphic card driver] from NVIDIA is installed.
 
|-
 
|-
 
|Operation System || Cross Platform ||
 
|Operation System || Cross Platform ||
Line 9: Line 9:
  
 
== Source code ==
 
== Source code ==
 +
=== Emgu CV 4.x ===
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
Click to view source code
 +
<div class="mw-collapsible-content">
 +
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 [https://www.nuget.org/packages?q=emgu.cv.models Emgu.CV.Models ] nuget package. The <code>Init()</code> function will download the models using the internet connection.
 +
 +
<source lang="csharp">
 +
using Emgu.CV.Models;
 +
...
 +
            using (Emgu.CV.Models.PedestrianDetector detector = new PedestrianDetector())
 +
            {
 +
                await detector.Init();
 +
                String text = detector.ProcessAndRender(imageIn, imageOut);
 +
            }
 +
 +
</source>
 +
</div>
 +
</div>
 +
=== Emgu CV 2.x ===
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
Click to view source code
 +
<div class="mw-collapsible-content">
 
<source lang="csharp">
 
<source lang="csharp">
       static void Run()
+
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)
 
       {
 
       {
        Image<Bgr, Byte> image = new Image<Bgr, byte>("pedestrian.png");
+
         Stopwatch watch;
 
 
         Stopwatch watch;
 
 
         Rectangle[] regions;
 
         Rectangle[] regions;
  
 
         //check if there is a compatible GPU to run pedestrian detection
 
         //check if there is a compatible GPU to run pedestrian detection
         if (GpuInvoke.HasCuda)  
+
         if (GpuInvoke.HasCuda)
 
         {  //this is the GPU version
 
         {  //this is the GPU version
 
             using (GpuHOGDescriptor des = new GpuHOGDescriptor())
 
             using (GpuHOGDescriptor des = new GpuHOGDescriptor())
Line 43: Line 81:
 
         }
 
         }
 
         watch.Stop();
 
         watch.Stop();
 +
 +
        processingTime = watch.ElapsedMilliseconds;
  
 
         foreach (Rectangle pedestrain in regions)
 
         foreach (Rectangle pedestrain in regions)
Line 48: Line 88:
 
             image.Draw(pedestrain, new Bgr(Color.Red), 1);
 
             image.Draw(pedestrain, new Bgr(Color.Red), 1);
 
         }
 
         }
 +
        return image;
 +
      }
 +
  }
 +
}
  
        ImageViewer.Show(
 
            image,
 
            String.Format("Pedestrain detection using {0} in {1} milliseconds.",
 
              GpuInvoke.HasCuda ? "GPU" : "CPU",
 
              watch.ElapsedMilliseconds));
 
      }
 
 
</source>
 
</source>
 +
</div>
 +
</div>
  
 
== Performance Comparison ==
 
== Performance Comparison ==
Line 65: Line 105:
 
!CPU|| GPU || Emgu CV Package || Execution Time (millisecond)
 
!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''' || <del>NVidia Quadro FX 880M</del> || 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
+
| '''Core i7 Q720@1.6Ghz''' || <del>NVidia Quadro FX 880M</del> || libemgucv-windows-x64-tbb-ipp-icc--2.2.2.1195 || 307
 
|-
 
|-
| '''Pentium D 920@2.8Ghz''' || NVidia GeForce GTS 450 || libemgucv-windows-x86-2.2.1.1150 || 2037
+
| <del>Core i7 Q720@1.6Ghz</del> || '''NVidia Quadro FX 880M''' || libemgucv-windows-x64-gpu-2.2.1.1150 || 424
 
|-
 
|-
| '''Pentium D 920@2.8Ghz''' || NVidia GeForce GTS 450 || libemgucv-windows-x86-tbb-ipp-icc-2.2.2.1195 || 996
+
| <del>Core i7 Q720@1.6Ghz</del> || '''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-gpu-2.2.1.1150 || 126
+
| Google Nexus S (Android) || || libemgucv-android-2.4.0.1717_beta || 19169
 
|-
 
|-
| Pentium D 920@2.8Ghz || '''NVidia GeForce GTS 450''' || libemgucv-windows-x86-gpu-tbb-ipp-icc-2.2.1.1150 || 124
+
| LG G Flex 2 (Android) || || libemgucv-android-3.1.0.2298 || 272
 
|}
 
|}
  
 
== Result ==
 
== Result ==
 +
*Windows
 +
 
[[image:PedestrianDetectionExample1.png |center|Pedestrian Detection]]
 
[[image:PedestrianDetectionExample1.png |center|Pedestrian Detection]]
 +
 +
*Android (Nexus S)
 +
 +
[[File:MonoAndroidPedestrianDetectionResultNexusS.jpg | center | 500px]]

Latest revision as of 19:10, 25 August 2021

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