Run a Core ML image model with Vision
Wiring Core ML into Vision for image inference.
Add the .mlmodel so Xcode generates a class, wrap it in a VNCoreMLModel, run a VNCoreMLRequest via a VNImageRequestHandler, read results off the main queue.
WHAT THIS TESTS This checks that you know how Core ML and Vision compose for image tasks, and that Vision handles the preprocessing that developers otherwise get wrong.
A GOOD ANSWER COVERS You add the .mlmodel file to the Xcode project; Xcode compiles it and generates a strongly typed Swift class for the model. For image inputs the idiomatic path is through Vision: create a VNCoreMLModel by wrapping the generated model, then a VNCoreMLRequest with that model and a completion handler. To run it on an image, create a VNImageRequestHandler from the input's CGImage, CIImage, or CVPixelBuffer along with orientation, and call perform with the request. Vision automatically scales and crops the image to the model's expected input size according to request.imageCropAndScaleOption and handles color formats, so you do not resize pixels manually. In the completion handler you read the results, typically an array of VNClassificationObservation sorted by confidence, and update UI on the main queue.
COMMON WRONG ANSWERS Manually resizing or reformatting the pixel buffer instead of letting Vision do it. Running the request on the main thread, blocking the UI. Forgetting to pass the correct CGImagePropertyOrientation, producing wrong predictions on rotated photos. Calling the Core ML prediction directly with a mismatched pixel buffer.
LIKELY FOLLOW-UPS How to batch or run on a background queue and dispatch results back. The difference between VNClassificationObservation and VNRecognizedObjectObservation for detectors. How MLModelConfiguration can pin compute to CPU or Neural Engine. Updating models over the air with on-device model deployment.
ONE CONCRETE EXAMPLE You wrap the generated class: let model = try VNCoreMLModel(for: MyClassifier(configuration: .init()).model). You build let request = VNCoreMLRequest(model: model) { req, _ in let top = (req.results as? [VNClassificationObservation])?.first; DispatchQueue.main.async { self.label.text = top?.identifier } }. On a background queue you create let handler = VNImageRequestHandler(cgImage: image.cgImage!, orientation: .up) and call try handler.perform([request]). Vision resizes the photo to the model's input, runs inference, and your handler shows the top label.
Read the original → developer.apple.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.