Search code examples
mauiml.net

Cant access ML model in my MAUI project ML.net


Hello i am trying to make the "is this a hot dog" app from silicon valley using ML.net image classification.

I am working in a Maui app and already got a trained model which i put the zipfile in the app and the model.consumption file.

I already got code for taking a picture.

private async void OnTakePhotoClicked(object sender, EventArgs e)
{
    var options = new StoreCameraMediaOptions();
    var result = await CrossMedia.Current.TakePhotoAsync(options);
    if (result is null) return;

    UploadedOrSelectedImage.Source = ImageSource.FromStream(() => result.GetStream());

    using var stream = result.GetStream();
    using var memoryStream = new MemoryStream();
    await stream.CopyToAsync(memoryStream);
    var imageBytes = memoryStream.ToArray();


    var modelInput = new MLModel2.ModelInput()
    {
        Label = "Image",
        ImageSource = imageBytes
    };

    var prediction = MLModel2.Predict(modelInput);

    ResultLabel.Text = prediction.PredictedLabel;
}

But it seems to always break when trying to find the zip file and trying to load the model.

    private static async Task<PredictionEngine<ModelInput, ModelOutput>> CreatePredictEngine()
    {
        const string filePath = "MLModel2.zip";
        using var stream = await FileSystem.OpenAppPackageFileAsync(filePath);
        var mlContext = new MLContext();
        ITransformer mlModel = mlContext.Model.Load(stream, out var _);
        return mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);

    }

i already tried different file paths, but none seems to work.


Solution

  • Cant access ML model in my MAUI project ML.net

    About it, you can see this case: Microsoft.ML and Xamarin. The answer said:

    Unfortunately, ML.NET doesn't work directly in Xamarin just yet. I believe you would have to wait until MAUI comes out to be able to do that.

    But MAUI does not seem to support ML.NET very well so far. I searched something about MAUI and ML.Net and found this on GitHub: ML.Net in MAUI. You can follow up.

    In Addition, I also found this session: ML.NET: Machine learning from data to production in less than 30 minutes. It involves MAUI and ML.NET. Wish it can help you.