(三)C#使用yolo

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 object
            using var yolo = new Yolo(new YoloOptions
            {
                OnnxModel = onnxFile,          // Your Yolo model in onnx format
                ModelType = ModelType.ObjectDetection,      // Set your model type
                Cuda = false,                               // Use CPU or CUDA for GPU accelerated inference. Default = true
                GpuId = 0,                                  // Select Gpu by id. Default = 0
                PrimeGpu = 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 image
            using var image = SKImage.FromEncodedData(sourceImg);

            // Run inference and get the results
            var 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 results
            using var resultImage = image.Draw(results);

            // Save to file
            resultImage.Save(@"D:\new_image.jpg", SKEncodedImageFormat.Jpeg, 80);

 D盘生成的图片

这个库非常简单明了 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值