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; // 格式化为百分比
                }
            }
        }
    }
}
相关推荐
CodeCraft Studio1 小时前
Aspose.Words for .NET 25.7:支持自建大语言模型(LLM),实现更安全灵活的AI文档处理功能
人工智能·ai·语言模型·llm·.net·智能文档处理·aspose.word
要记得喝水1 小时前
C#某公司面试题(含题目和解析)--1
开发语言·windows·面试·c#·.net
SEO-狼术2 小时前
Aqua Data Studio 25.5
.net
MasterNeverDown21 小时前
.net 微服务jeager链路跟踪
微服务·架构·.net
喵叔哟1 天前
51.【.NET8 实战--孢子记账--从单体到微服务--转向微服务】--新增功能--登录注册扩展
数据库·微服务·.net
时光追逐者1 天前
.NET 使用 CsvHelper 快速读取和写入 CSV 文件
c#·.net·.net core·csv
界面开发小八哥2 天前
文档控件DevExpress Office File API v25.1新本亮点:重磅升级各类API
c#·.net·界面控件·devexpress·ui开发
追逐时光者2 天前
.NET 使用 CsvHelper 快速读取和写入 CSV 文件
后端·.net
Kookoos2 天前
差分隐私在运营指标:ABP 的 DP 计数器与噪声预算
.net·差分隐私·abp vnext·拉普拉斯机制·隐私预算
蒋星熠3 天前
.NET技术深度解析:现代企业级开发指南
人工智能·python·深度学习·微服务·ai·性能优化·.net