Giter Club home page Giter Club logo

csharp-yolo-video's Introduction

CSharp-Yolo-Video

Although the C# wrapper for Darknet exists, I went through a hard time figuring out how to apply the wrapper for videos. For later use for myself and saving others' time, I summarize how to apply the Yolo wrapper on videos.

Getting Started

The following instructions will lead you to setting the environment for using the Yolo wrapper in your project.

System requriements

Install Alturos.Yolo using NuGet as follows or you can use the NuGet GUI instead. If you're using NuGet GUI, search for "Alturos.Yolo".

PM> install-package Alturos.Yolo (C# wrapper and C++ dlls 28MB)
PM> install-package Alturos.YoloV2TinyVocData (YOLOv2-tiny Pre-Trained Dataset 56MB)

(Optional) For GPU support, install and download the followings.

  1. Install the latest Nvidia driver for your graphic device.
  2. Install Nvidia CUDA Toolkit 10.1 (must be installed add a hardware driver for cuda support)
  3. Download Nvidia cuDNN v7.6.3 for CUDA 10.1
  4. Copy the cudnn64_7.dll from the output directory of cdDNN v7.6.3. into the x64 folder of your project.

Install OpenCvSharp3-AnyCPU over NuGet as follows or search for "OpenCvSharp3-AnyCPU". Although the package name contains CvSharp3, it is actually an OpenCv 4.x wrapper.

PM> install-package OpenCvSharp3-AnyCPU

Download pretrained weights and place it in your project directory. For more information, visit Alturos.Yolo

Model Processing Resolution Cfg Weights Names
YOLOv3 608x608 yolov3.cfg yolov3.weights coco.names
YOLOv3-tiny 416x416 yolov3-tiny.cfg yolov3-tiny.weights coco.names
YOLOv2 608x608 yolov2.cfg yolov2.weights coco.names
YOLOv2-tiny 416x416 yolov2-tiny.cfg yolov2-tiny.weights voc.names
yolo9000 448x448 yolo9000.cfg yolo9000.weights 9k.names

Write Codes for Video Object Recognition

The following is the minimum code for running the Yolo wrapper on a video file. For running the code, set the solution platform as "x64"!

using OpenCvSharp;
using OpenCvSharp.Extensions;

using Alturos.Yolo;

private void VideoObjectDetection()
{
  // YOLO setting
  int yoloWidth = 608, yoloHeight = 608;
  var configurationDetector = new ConfigurationDetector();
  var config = configurationDetector.Detect();
  YoloWrapper yoloWrapper = new YoloWrapper(config);
  
  // OpenCV & WPF setting
  VideoCapture videocapture;
  Mat image = new Mat();
  WriteableBitmap wb = new WriteableBitmap(yoloWidth, yoloHeight, 96, 96, PixelFormats.Bgr24, null);
  
  byte[] imageInBytes = new byte[(int)(yoloWidth * yoloHeight * image.Channels())];
  
  // Read a video file and run object detection over it!
  using (videocapture = new VideoCapture(address))
  {
    using(Mat imageOriginal = new Mat())
    {
      // read a single frame and convert the frame into a byte array
      videocapture.Read(imageOriginal);
      image = imageOriginal.Resize(new OpenCvSharp.Size(yoloWidth, yoloHeight));
      imageInBytes = image.ToBytes();
      
      // conduct object detection and display the result
      var items = yolowrapper.Detect(imageInBytes);
      foreach(var item in items)
      {
        var x = item.X;
        var y = item.Y;
        var width = item.Width;
        var height = item.Height;
        var type = item.Type;  // class name of the object
        
        // draw a bounding box for the detected object
        // you can set different colors for different classes
        Cv2.Rectangle(image, new OpenCvSharp.Rect(x, y, width, height), Scalar.Green, 3);
      }
      
      // display the detection result
      WriteableBitmapConverter.ToWriteableBitmap(image, wb);
      /* WPF component: videoViewer
      <Canvas Name="canvasYoloVideo" Height="608" Width="608">
        <Image Name="videoViewer" Height="608" Width="608" Stretch="Fill" />
      </Canvas>
      */
      videoViewer.Source = wb;
    }
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.