Search code examples
swiftxcodecoreml

How can I fix this error? (No exact matches in call to instance method 'prediction')


I am working on integrating a model that takes image input into an Xcode project, but I am running into an error that says: No exact matches in call to instance method 'prediction'

import SwiftUI
import CoreML

let testImage:Image = Image("testHappyImage")

func testing(image: Image) ->
    DDAMFNOutput?  {
    
        do {
            
            let config = MLModelConfiguration()
            
            let model = try DDAMFN(configuration: config)
            
            let prediction = try model.prediction(image)
            // ^ This line is giving me the error that says: No exact matches in call to                instance method 'prediction'
            
            return prediction
            
        } catch {
            
            
        }
        
    return nil
}

Here is proof that the model takes image input. testHappyImage is a 112 by 112 color image.this is a screenshot of the mlmodel's metadata -->

I don't know what this error means or where I should start to fix it.

I cannot find any official apple documentation on this error, nor have I found any answers on Stack Overflow that worked for me. Any help would be appreciated!


Solution

  • You need a CGImage, not a SwiftUI Image. You can convert one to the other using a ImageRenderer.

    guard let image = ImageRenderer(content: Image("1")).cgImage else { 
        // cannot convert - handle this appropriately
        return 
    }
    

    Xcode should have generated a class named DDAMFNInput for you - pass an instance of this to predictions. Pass the CGImage you previously created to its initialiser.

    let input = DDAMFNInput(imageWith: image)
    // you can also use DDAMFNInput(imageAt: url) if you have a URL
    
    let predictions = try model.prediction(input: input)
    

    Then you can access predictions.linear_0, predictions.x_201 etc.