(三)C#使用yolo
github地址
https://github.com/NickSwardh/YoloDotNet
1.安装yoloDotNet
下面是根据官网例子,写的测试代码
private async void btnVisionTest_Click(object sender, RoutedEventArgs e){string sourceImg = await VisionHelper.GetInstance().GetLocalImage();string onnxFile = @"D:\easyboot\Onnx\best.onnx";// Instantiate a new Yolo objectusing var yolo = new Yolo(new YoloOptions{OnnxModel = onnxFile, // Your Yolo model in onnx formatModelType = ModelType.ObjectDetection, // Set your model typeCuda = false, // Use CPU or CUDA for GPU accelerated inference. Default = trueGpuId = 0, // Select Gpu by id. Default = 0PrimeGpu = false, // Pre-allocate GPU before first inference. Default = false// ImageResize = ImageResize.Proportional // Proportional = Default, Stretched = Squares the image// SamplingOptions = new SKSamplingOptions(SKFilterMode.Linear, SKMipmapMode.None) // View benchmark-test examples: https://github.com/NickSwardh/YoloDotNet/blob/development/test/YoloDotNet.Benchmarks/ImageExtensionTests/ResizeImageTests.cs});// Load imageusing var image = SKImage.FromEncodedData(sourceImg);// Run inference and get the resultsvar results = yolo.RunObjectDetection(image, confidence: 0.25, iou: 0.7);//results.ForEach(a => a.BoundingBox){// Console.WriteLine(ar//}// Tip:// Use the extension method FilterLabels([]) on any result if you only want specific labels.// Example: Select only the labels you're interested in and exclude the rest.// var results = yolo.RunObjectDetection(image).FilterLabels(["person", "car", "cat"]);// Draw resultsusing var resultImage = image.Draw(results);// Save to fileresultImage.Save(@"D:\new_image.jpg", SKEncodedImageFormat.Jpeg, 80);
D盘生成的图片
这个库非常简单明了