这是由ml.net自动生成的代码,我只实现了按钮1上的打开文件对话框代码。以下是Windows窗体应用程序中图像检测的代码。这里有两个按钮和一个图片框。按钮1用于浏览图像,按钮2用于检测图像。
namespace WindowsFormsApp3{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { var ofd = new OpenFileDialog(); ofd.Filter = "Image Files|*.jpg;*.png"; if (ofd.ShowDialog() == DialogResult.OK) { pictureBox1.Image = Image.FromFile(ofd.FileName); } } private void button2_Click(object sender, EventArgs e) { // Add input data var input = new ModelInput(); // Load model and predict output of sample data ModelOutput result = ConsumeModel.Predict(input); MessageBox.Show(result.Prediction); } }}
当我点击按钮2来检测图像时,会出现以下异常 System.TypeInitializationException: 'The type initializer for 'WindowsFormsApp3ML.Model.ConsumeModel' threw an exception. FileNotFoundException: Could not load file or assembly 'Microsoft.ML.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.' 我已经尝试了两次训练我的模型,但都出现了同样的异常。
回答:
尝试从NuGet安装Microsoft.Ml.Data
并针对x64进行设置;如果还不行,你可以考虑转向.NET Core —— Bill和他的团队正在强制大家使用.NET Core。