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
(Undo revision 997 by Inuxejiq (talk))
(Updated source code with GPU processing)
Line 11: Line 11:
 
== Source code ==
 
== Source code ==
 
<source lang="csharp">
 
<source lang="csharp">
using (HOGDescriptor hog = new HOGDescriptor())
+
      static void Run()
{
+
      {
  float[] desc = HOGDescriptor.GetDefaultPeopleDetector();
+
        Image<Bgr, Byte> image = new Image<Bgr, byte>("pedestrian.png");
  hog.SetSVMDetector(desc);
 
  Image<Bgr, Byte> image = new Image<Bgr, byte>("pedestrian.png");
 
  
  Rectangle[] rects = hog.DetectMultiScale(image);
+
        Stopwatch watch = Stopwatch.StartNew();
 +
        Rectangle[] regions;
  
  foreach (Rectangle rect in rects)
+
        //check if there is a compatible GPU to run pedestrian detection
  {
+
        if (GpuInvoke.HasCuda)
      image.Draw(rect, new Bgr(Color.Red), 1);
+
        {  //this is the GPU version
  }
+
            using (GpuHOGDescriptor des = new GpuHOGDescriptor())
  ImageViewer.Show(image);
+
            using (GpuImage<Bgr, Byte> gpuImg = new GpuImage<Bgr,byte>(image))
}
+
            using (GpuImage<Bgra, Byte> gpuBgra = gpuImg.Convert<Bgra, Byte>())
 +
            {
 +
              des.SetSVMDetector(GpuHOGDescriptor.GetDefaultPeopleDetector());
 +
              regions = des.DetectMultiScale(gpuBgra);
 +
            }
 +
        }
 +
        else
 +
        {  //this is the CPU version
 +
            using (HOGDescriptor des = new HOGDescriptor())
 +
            {
 +
              des.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector());
 +
              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));
 +
      }
 
</source>
 
</source>
  
 
== Result ==
 
== Result ==
 
[[image:PedestrianDetectionExample1.png |center|Pedestrian Detection]]
 
[[image:PedestrianDetectionExample1.png |center|Pedestrian Detection]]

Revision as of 21:22, 28 January 2011

System Requirement

Component Requirement Detail
Emgu CV Version 2.0.1.0 Available from SVN only
Operation System Cross Platform


Source code

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

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

Result

Pedestrian Detection