C# は歩行者検出に Onnx を使用します

1. VS2019 は、OnnxDemo という名前の .Net5 コンソール プログラムを作成します。

2. NuGet は次のライブラリをインストールします。プレリリース バージョンを確認するように注意してください。そうでないと、ライブラリの 1 つが見つかりません。

3. OnnxRuntime には [FasterRCNN-10.onnx] という名前の onnx ファイルが github にあります。それをダウンロードして、onnxs フォルダーに置きます。

4. 検出された画像をいくつか準備し、入力フォルダーに置きます

5. テスト結果を保存する出力フォルダーを作成します。

6. デモコードは以下のとおりです [Program.cs ファイル]

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.ML.OnnxRuntime.Tensors;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.Fonts;
//using SixLabors.Fonts;
//using System.Drawing;

namespace Microsoft.ML.OnnxRuntime.FasterRcnnSample
{
    class Program
    {
        public static void Main(string[] args)
        {
            // OnnxRuntime官网提供的模型文件,已下载到项目运行文件夹下
            // Read paths
            string modelFilePath = @"onnxs/FasterRCNN-10.onnx";

            // 读取模型文件到会话对象中
            // Run inference
            using var session = new InferenceSession(modelFilePath);

            // 依次读入每一张待检测的图片,图片在inputs文件夹下
            for (int bl = 1; bl <= Directory.GetFiles("inputs").Length; bl++)
            {
                string imageFilePath = $"inputs/({bl}).jpeg";
                string outImageFilePath = $"outputs/{bl}.jpeg";

                // 读取图片
                // Read image
                using Image<Rgb24> image = Image.Load<Rgb24>(imageFilePath);

                // 改变图片大小至模型运算指定的大小
                // Resize image
                float ratio = 800f / Math.Min(image.Width, image.Height);
                image.Mutate(x => x.Resize((int)(ratio * image.Width), (int)(ratio * image.Height)));

                // Preprocess image
                var paddedHeight = (int)(Math.Ceiling(image.Height / 32f) * 32f);
                var paddedWidth = (int)(Math.Ceiling(image.Width / 32f) * 32f);
                Tensor<float> input = new DenseTensor<float>(new[] { 3, paddedHeight, paddedWidth });
                var mean = new[] { 102.9801f, 115.9465f, 122.7717f };
                for (int y = paddedHeight - image.Height; y < image.Height; y++)
                {
                    image.ProcessPixelRows(im =>
                    {
                        var pixelSpan = im.GetRowSpan(y);
                        for (int x = paddedWidth - image.Width; x < image.Width; x++)
                        {
                            input[0, y, x] = pixelSpan[x].B - mean[0];
                            input[1, y, x] = pixelSpan[x].G - mean[1];
                            input[2, y, x] = pixelSpan[x].R - mean[2];
                        }
                    });

                }

                // 将图片传至模型输入层
                // Setup inputs and outputs
                var inputs = new List<NamedOnnxValue>
                {
                    NamedOnnxValue.CreateFromTensor("image", input)
                };

                // 运行模型得到结果
                using IDisposableReadOnlyCollection<DisposableNamedOnnxValue> results = session.Run(inputs);

                // 对运行结果解析
                // Postprocess to get predictions
                var resultsArray = results.ToArray();
                float[] boxes = resultsArray[0].AsEnumerable<float>().ToArray();
                long[] labels = resultsArray[1].AsEnumerable<long>().ToArray();
                float[] confidences = resultsArray[2].AsEnumerable<float>().ToArray();

                var predictions = new List<Prediction>();
                // 置信度不小于0.7则视为检测出该特征
                var minConfidence = 0.7f;
                for (int i = 0; i < boxes.Length - 4; i += 4)
                {
                    var index = i / 4;
                    if (confidences[index] >= minConfidence)
                    {
                        predictions.Add(new Prediction
                        {
                            Box = new Box(boxes[i], boxes[i + 1], boxes[i + 2], boxes[i + 3]),
                            Label = LabelMap.Labels[labels[index]],
                            Confidence = confidences[index]
                        });
                    }
                }

                // 给检测的对象画框
                // Put boxes, labels and confidence on image and save for viewing
                using var outputImage = File.OpenWrite(outImageFilePath);
                Font font = SystemFonts.CreateFont("Arial", 28);
                foreach (var p in predictions)
                {
                    image.Mutate(x =>
                    {
                        x.DrawLines(Color.Red, 2f, new PointF[] {

                        new PointF(p.Box.Xmin, p.Box.Ymin),
                        new PointF(p.Box.Xmax, p.Box.Ymin),

                        new PointF(p.Box.Xmax, p.Box.Ymin),
                        new PointF(p.Box.Xmax, p.Box.Ymax),

                        new PointF(p.Box.Xmax, p.Box.Ymax),
                        new PointF(p.Box.Xmin, p.Box.Ymax),

                        new PointF(p.Box.Xmin, p.Box.Ymax),
                        new PointF(p.Box.Xmin, p.Box.Ymin)
                        });
                        x.DrawText($"{p.Label}, {p.Confidence:0.00}", font, Color.Blue, new PointF(p.Box.Xmin, p.Box.Ymin));
                    });
                }
                // 图片保存到outputs文件夹下
                image.SaveAsJpeg(outputImage);
            }
        }
    }
    public class Prediction
    {
        public Box Box { set; get; }
        public string Label { set; get; }
        public float Confidence { set; get; }
    }
    public class Box
    {
        public Box(float xMin, float yMin, float xMax, float yMax)
        {
            Xmin = xMin;
            Ymin = yMin;
            Xmax = xMax;
            Ymax = yMax;
        }
        public float Xmin { set; get; }
        public float Xmax { set; get; }
        public float Ymin { set; get; }
        public float Ymax { set; get; }
    }

    public static class LabelMap
    {
        static LabelMap()
        {
            Labels = new string[]
            {
                "",
                "person",
                "bicycle",
                "car",
                "motorcycle",
                "airplane",
                "bus",
                "train",
                "truck",
                "boat",
                "traffic light",
                "fire hydrant",
                "stop sign",
                "parking meter",
                "bench",
                "bird",
                "cat",
                "dog",
                "horse",
                "sheep",
                "cow",
                "elephant",
                "bear",
                "zebra",
                "giraffe",
                "backpack",
                "umbrella",
                "handbag",
                "tie",
                "suitcase",
                "frisbee",
                "skis",
                "snowboard",
                "sports ball",
                "kite",
                "baseball bat",
                "baseball glove",
                "skateboard",
                "surfboard",
                "tennis racket",
                "bottle",
                "wine glass",
                "cup",
                "fork",
                "knife",
                "spoon",
                "bowl",
                "banana",
                "apple",
                "sandwich",
                "orange",
                "broccoli",
                "carrot",
                "hot dog",
                "pizza",
                "donut",
                "cake",
                "chair",
                "couch",
                "potted plant",
                "bed",
                "dining table",
                "toilet",
                "tv",
                "laptop",
                "mouse",
                "remote",
                "keyboard",
                "cell phone",
                "microwave",
                "oven",
                "toaster",
                "sink",
                "refrigerator",
                "book",
                "clock",
                "vase",
                "scissors",
                "teddy bear",
                "hair drier",
                "toothbrush"
            };
        }

        public static string[] Labels { set; get; }
    }
}

7. プロジェクトの構成は次のとおりです

おすすめ

転載: blog.csdn.net/qq_36694133/article/details/128209770