Search code examples
c#object-detectionpython.netyolov5

Object detection using YoloV5 in C#


I am new to ML and am trying to make inferences using the YoloV5 model from a C# program. I am aware of the github port, but would like to use torch instead of ML.NET. I found PythonNET nuget package, and this code is working:

using System;
using System.Threading.Tasks;
using Python.Runtime;

namespace Utilities.ML
{
    public class ObjectDetection
    {
        public static void Detect(string url)
        {
            Runtime.PythonDLL = @"C:\Python38\python38.dll";
            using (Py.GIL())
            {
                dynamic torch = Py.Import("torch");
                dynamic model = torch.hub.load("ultralytics/yolov5", "yolov5s");
                dynamic img = url;
                dynamic results = model(img).pandas().xyxy[0].to_json();
                var str = results.ToString(); //Contains bounding box coords and class names in json format.
            }
        }
    }
}

The problem is that each time I call this function, the model is loaded. This is an expensive operation, and I am wondering if it would be a bad idea to persist the model object after it is loaded the first time Detect() is called.

Also, is this a bad solution for what I am trying to accomplish? I don't have a ton of Python experience, but I'd imagine another way to tackle this would be to host a localhost REST API that my C# application could send requests to?


Solution

  • Posting this as an answer for anyone else that is seeking a good way to use YOLOv5 in C#, that doesn't require dealing with python, since it is using ML.Net:

    github.com/mentalstack/yolov5-net