Alturos.Yolo

repository·master·Indexed 19 days ago

https://github.com/alturosdestinations/alturos.yolo

A C# wrapper for the Darknet YOLO object detection system providing real-time detection with CPU and GPU (Nvidia CUDA) acceleration. It includes the YoloWrapper class for object detection, ConfigurationDetector for environment setup, and YoloPreTrainedDatasetRepository for downloading models. Supports .jpg files and requires specific native dependencies including OpenCV and CUDA Toolkit 10.2 for GPU support.

Tokens
3K
Snippets
11
Records
15
Agent score
65%

What's inside Alturos.Yolo

  1. Install Alturos.Yolo via NuGet

    master

    To start using object detection in your C# project, install the core wrapper and a pre-trained dataset via the NuGet Package Manager. For YOLOv2-tiny, you should install both the wrapper and the specific dataset package.

    PM> install-package Alturos.Yolo
    PM> install-package Alturos.YoloV2TinyVocData
  2. Extract darknet build artifacts from Docker

    master

    After building darknet inside a Docker container, you can copy the compiled release files to your local host machine using docker cp. Replace <container_id> with your actual running container ID.

    # Find your container ID
    docker ps
    
    # Copy the build_release folder to a local directory
    docker cp <container_id>:/darknet/build_release C:\temp
  3. Build darknet using Docker on Linux

    master

    To build the darknet dependency required by Alturos.Yolo, use a GCC Docker container to compile it from source. This involves setting up a build environment with necessary libraries (OpenCV, FFmpeg, GTK), cloning the repository, and configuring the Makefile to enable shared libraries (LIBSO=1) and OpenCV support (OPENCV=1).

    # 1. Start a GCC container
    docker pull gcc
    docker run -it gcc /bin/bash
    
    # 2. Install build dependencies
    apt-get update && apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libopencv-dev -y
    
    # 3. Clone and configure darknet
    git clone https://github.com/AlexeyAB/darknet.git
    cd darknet
    sed -i 's/LIBSO=0/LIBSO=1/g' Makefile
    sed -i 's/OPENCV=0/OPENCV=1/g' Makefile
    
    # 4. Build
    make
  4. Install Alturos.Yolo and pre-trained models

    master

    The Alturos.Yolo package provides the core logic for CPU-based object detection. For a quick start with pre-trained models, you should also install the Alturos.YoloV2TinyVocData NuGet package.

    To get started immediately with object detection, run:

    install-package Alturos.Yolo
    install-package Alturos.YoloV2TinyVocData
    install-package Alturos.Yolo
    install-package Alturos.YoloV2TinyVocData
  5. Configure GPU support for faster detection

    master

    GPU detection is typically 10 times faster than CPU. To enable it, you must satisfy these requirements:

    1. Install the latest Nvidia driver.
    2. Install Nvidia CUDA Toolkit 10.2.
    3. Download Nvidia cuDNN v7.6.5 for CUDA 10.2.
    4. Copy cudnn64_7.dll from the cuDNN output directory into your project folder.
  6. Configure GPU detection for Alturos.Yolo

    master

    By default, Alturos.Yolo uses CPU detection. It will automatically switch to GPU mode if the following Nvidia dependencies are present in your environment:

    1. Nvidia CUDA Toolkit 10.2: Must be installed along with a hardware driver that supports CUDA.
    2. Nvidia cuDNN v7.6.5 for CUDA 10.2: Specifically, the cudnn64_7.dll file is required.

    Setup Step: Copy cudnn64_7.dll (found in the %cudnn%\bin directory) into the x64 directory of your application's output folder.

  7. Set up Alturos.Yolo on Linux using Docker

    master

    To set up a development environment for Alturos.Yolo on Linux, use the Microsoft .NET Core SDK Docker image. You will also need to install libgomp1 via apt-get to satisfy dependencies for the object detection logic.

    Follow these steps:

    1. Pull and run the .NET Core SDK container.
    2. Update apt and install vim and libgomp1.
    3. Create a new .NET console project.
    4. Add the required NuGet packages: Alturos.Yolo and Alturos.YoloV2TinyVocData.
    # Pull and enter the SDK container
    docker pull mcr.microsoft.com/dotnet/core/sdk
    docker run -it mcr.microsoft.com/dotnet/core/sdk /bin/bash
    
    # Inside the container, install dependencies
    apt-get update
    apt-get install vim -y
    apt-get install libgomp1
    
    # Setup the project
    mkdir test
    cd test
    dotnet new console
    dotnet add package Alturos.Yolo --version 3.0.3-alpha
    dotnet add package Alturos.YoloV2TinyVocData --version 1.0.0
  8. Troubleshoot DllNotFoundException and NotSupportedException

    master

    If you encounter errors during runtime, check the following:

    • DllNotFoundException: Use the Dependencies tool to verify that all required DLLs (like yolo_cpp_dll_gpu.dll) have their references available.
    • NotSupportedException: Ensure you are using the latest Nvidia driver.
    • GPU Usage: Use the command %PROGRAMFILES%\NVIDIA Corporation\NVSMI\nvidia-smi.exe to check graphic device usage.
  9. Detect objects with automatic configuration

    master

    Use ConfigurationDetector to automatically determine the best configuration for your environment, then pass that configuration to a YoloWrapper. The Detect method accepts an image file path and returns a collection of detected items containing the object type, confidence score, and bounding box coordinates.

    var configurationDetector = new ConfigurationDetector();
    var config = configurationDetector.Detect();
    using (var yoloWrapper = new YoloWrapper(config))
    {
    	var items = yoloWrapper.Detect(@"image.jpg");
    	// items[0].Type -> "Person, Car, ..."
    	// items[0].Confidence -> 0.0 (low) -> 1.0 (high)
    	// items[0].X -> bounding box
    	// items[0].Y -> bounding box
    	// items[0].Width -> bounding box
    	// items[0].Height -> bounding box
    }
  10. Perform object detection with YoloWrapper

    master

    To detect objects in an image, use ConfigurationDetector to automatically determine the best configuration, then pass that configuration to a YoloWrapper instance. The Detect method returns a collection of items containing the object type, confidence score, and bounding box coordinates.

    Each detected item provides:

    • Type: The label of the detected object (e.g., "Person", "Car").
    • Confidence: A value from 0.0 (low) to 1.0 (high).
    • X, Y, Width, Height: The bounding box coordinates of the object.
    var configurationDetector = new ConfigurationDetector();
    var config = configurationDetector.Detect();
    using (var yoloWrapper = new YoloWrapper(config))
    {
    	var items = yoloWrapper.Detect(@"image.jpg");
    	// items[0].Type -> "Person, Car, ..."
    	// items[0].Confidence -> 0.0 (low) -> 1.0 (high)
    	// items[0].X -> bounding box
    	// items[0].Y -> bounding box
    	// items[0].Width -> bounding box
    	// items[0].Height -> bounding box
    }
  11. Perform object detection with YoloWrapper

    master

    Use the YoloWrapper class to detect objects in an image. You must provide the configuration file (.cfg), the weights file (.weights), and the names file (.names) during initialization.

    Important Note: Currently, the library only supports .jpg files; attempting to use .png files may result in failures.

    using System;
    using System.IO;
    using Alturos.Yolo;
    
    namespace test
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Yolo Test - Choose file:");
                var fileName = Console.ReadLine();
                
                // Initialize the wrapper with model files
                using (var yoloWrapper = new YoloWrapper("yolov2-tiny-voc.cfg", "yolov2-tiny-voc.weights", "voc.names"))
                {
                    var imageData = File.ReadAllBytes(fileName);
                    var items = yoloWrapper.Detect(imageData);
                    
                    foreach (var item in items)
                    {
                        Console.WriteLine(item.Type);
                    }
                }
            }
        }
    }