ML.NET 图像分类

参考文档:https://www.cnblogs.com/mq0036/p/18302572

复制代码
using MLNET_Image;
using static MLNET_Image.MLModel1;

namespace MLNET.Image
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void btnSelectImage_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Title = "Select Image";
                openFileDialog.Filter = "Image Files (*.jpg, *.png, *.bmp)|*.jpg;*.png;*.bmp|All Files (*.*)|*.*";

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    // 获取用户选择的文件路径
                    string selectedImagePath = openFileDialog.FileName;

                    // 从文件加载图片
                    System.Drawing.Image img = System.Drawing.Image.FromFile(openFileDialog.FileName);
                    this.pictureBox1.Image = img;

                    var imageBytes = File.ReadAllBytes(selectedImagePath);
                    ModelInput sampleData = new MLModel1.ModelInput()
                    {
                        ImageSource = imageBytes,
                    };

                    //Load model and predict output
                    var result = MLModel1.Predict(sampleData);

                    var label = result.PredictedLabel;
                    var score = MLModel1.GetSortedScoresWithLabels(result).Where(x => x.Key == label).First().Value; 

                    this.lblResult.Text = result.PredictedLabel + " " + score; // 格式化为百分比
                }
            }
        }
    }
}
相关推荐
公子小六1 小时前
基于.NET的Windows窗体编程之WinForms数据持久化(1)
windows·microsoft·c#·.net·winforms
Jeff-Tsai3 小时前
Day 16:JSON-RPC 2.0:协议选型与请求管道
.net·架构设计·erp·definition-driven
IT规划师4 小时前
Redis 数据类型全解 - Stream 深度:Pending 回收、重试死信与消息队列选型(十一)
redis·.net
学以智用21 小时前
ASP.NET MVC5(.NET Framework)底层完整运行逻辑
.net
学以智用1 天前
FilterAttribute(ASP.NET Core MVC 过滤器特性)
.net
Jeff-Tsai1 天前
Day 15:API 契约分层与类型注册表
.net·架构设计·erp·definition-driven
1-1=01 天前
C# 数据结构和特殊类型
数据结构·c#·.net·.netcore
IT规划师1 天前
Redis 数据类型全解 - Stream 深度:消费者组、Pending 队列与死信处理(十)
redis·.net
Jeff-Tsai2 天前
Day 14:业务对象的扩展点与重写时机
.net·架构设计·erp·definition-driven
IT规划师2 天前
Redis 数据类型全解 - GEO 地理空间:附近的人、外卖、打车——背后原理与精度陷阱(九)
redis·.net