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; // 格式化为百分比
                }
            }
        }
    }
}
相关推荐
code_shenbing5 分钟前
.NET MVC实现电影票管理
mvc·.net
shepherd枸杞泡茶4 小时前
第3章 3.3日志 .NET Core日志 NLog使用教程
c#·asp.net·.net·.netcore
时光追逐者14 小时前
推荐几款开源免费的 .NET MAUI 组件库
microsoft·开源·c#·.net·.net core·maui
三天不学习16 小时前
.Net面试宝典【刷题系列】
面试·职场和发展·.net
JosieBook16 小时前
【.NET全栈】.NET包含的所有技术
.net
shepherd枸杞泡茶16 小时前
第3章 3.2 配置系统 .NET Core配置系统
后端·c#·asp.net·.net
追逐时光者1 天前
Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
后端·.net
CodeCraft Studio1 天前
文档处理控件TX Text Control系列教程:使用 .NET C# 从 PDF 文档中提取基于模板的文本
pdf·c#·.net
Crazy Struggle2 天前
.NET 使用 DeepSeek R1 开发智能 AI 客户端
人工智能·ai·.net·deepseek
界面开发小八哥2 天前
界面控件DevExpress WPF v24.2新版亮点:支持.NET 9
.net·wpf·界面控件·devexpress·ui开发·用户界面