Created
May 26, 2021 05:50
-
-
Save iosdevie/25fe2019766d7e2ee23a07a97d11c1e3 to your computer and use it in GitHub Desktop.
Predict an image using MLModel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func predict(image: UIImage) -> Animal? { | |
let imageConstraint = model.modelDescription.inputDescriptionsByName["image"]!.imageConstraint! | |
do{ | |
let imageOptions: [MLFeatureValue.ImageOption: Any] = [ | |
.cropAndScale: VNImageCropAndScaleOption.scaleFill.rawValue | |
] | |
let featureValue = try MLFeatureValue(cgImage: image.cgImage!, constraint: imageConstraint, options: imageOptions) | |
let featureProviderDict = try MLDictionaryFeatureProvider(dictionary: ["image" : featureValue]) | |
let prediction = try updatableModel?.prediction(from: featureProviderDict) | |
let value = prediction?.featureValue(for: "classLabel")?.stringValue | |
if value == "Dog"{ | |
return .dog | |
} | |
else{ | |
return .cat | |
} | |
}catch(let error){ | |
print("error is \(error.localizedDescription)") | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment