参考文档: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; // 格式化为百分比
}
}
}
}
}