Classifying Images With Vision And Core ML

Overview

Classifying Images with Vision and Core ML

Preprocess photos using the Vision framework and classify them with a Core ML model.

Overview

With the Core ML framework, you can use a trained machine learning model to classify input data. The Vision framework works with Core ML to apply classification models to images, and to preprocess those images to make machine learning tasks easier and more reliable.

This sample app uses the open source MobileNet model, one of several available classification models, to identify an image using 1000 classification categories as seen in the example screenshots below.

example screenshots of app identifying a potted plant, a fountain, and a bunch of bananas

Getting Started

This sample code project runs on iOS 11. However, you can also use Vision and Core ML in your own apps on macOS 10.13, iOS 11, or tvOS 11.

Preview the Sample App

To see this sample app in action, build and run the project, then use the buttons in the sample app's toolbar to take a picture or choose an image from your photo library. The sample app then uses Vision to apply the Core ML model to the chosen image, and shows the resulting classification labels along with numbers indicating the confidence level of each classification. It displays the top two classifications in order of the confidence score the model assigns to each.

Set Up Vision with a Core ML Model

Core ML automatically generates a Swift class that provides easy access to your ML model; in this sample, Core ML automatically generates the MobileNet class from the MobileNet model. To set up a Vision request using the model, create an instance of that class and use its model property to create a VNCoreMLRequest object. Use the request object's completion handler to specify a method to receive results from the model after you run the request.

let model = try VNCoreMLModel(for: MobileNet().model)

let request = VNCoreMLRequest(model: model, completionHandler: { [weak self] request, error in
    self?.processClassifications(for: request, error: error)
})
request.imageCropAndScaleOption = .centerCrop
return request

View in Source

An ML model processes input images in a fixed aspect ratio, but input images may have arbitrary aspect ratios, so Vision must scale or crop the image to fit. For best results, set the request's imageCropAndScaleOption property to match the image layout the model was trained with. For the available classification models, the centerCrop option is appropriate unless noted otherwise.

Run the Vision Request

Create a VNImageRequestHandler object with the image to be processed, and pass the requests to that object's perform(_:) method. This method runs synchronously—use a background queue so that the main queue isn't blocked while your requests execute.

DispatchQueue.global(qos: .userInitiated).async {
    let handler = VNImageRequestHandler(ciImage: ciImage, orientation: orientation)
    do {
        try handler.perform([self.classificationRequest])
    } catch {
        /*
         This handler catches general image processing errors. The `classificationRequest`'s
         completion handler `processClassifications(_:error:)` catches errors specific
         to processing that request.
         */
        print("Failed to perform classification.\n\(error.localizedDescription)")
    }
}

View in Source

Most models are trained on images that are already oriented correctly for display. To ensure proper handling of input images with arbitrary orientations, pass the image's orientation to the image request handler. (This sample app adds an initializer, init(_:), to the CGImagePropertyOrientation type for converting from UIImageOrientation orientation values.)

Handle Image Classification Results

The Vision request's completion handler indicates whether the request succeeded or resulted in an error. If it succeeded, its results property contains VNClassificationObservation objects describing possible classifications identified by the ML model.

func processClassifications(for request: VNRequest, error: Error?) {
    DispatchQueue.main.async {
        guard let results = request.results else {
            self.classificationLabel.text = "Unable to classify image.\n\(error!.localizedDescription)"
            return
        }
        // The `results` will always be `VNClassificationObservation`s, as specified by the Core ML model in this project.
        let classifications = results as! [VNClassificationObservation]

View in Source

You might also like...
The example project of inferencing Pose Estimation using Core ML
The example project of inferencing Pose Estimation using Core ML

This project is Pose Estimation on iOS with Core ML. If you are interested in iOS + Machine Learning, visit here you can see various DEMOs. 한국어 README

 iOS multi-functional AI camera: portrait cartoon, ageing and rejuvenation, beauty, filters, artistic effects, etc.
iOS multi-functional AI camera: portrait cartoon, ageing and rejuvenation, beauty, filters, artistic effects, etc.

Magic Camera is an iOS AI camera app based on SwiftUI and CoreML that implements the following features: Portrait Cartoonization, which turns your photos into cartoon avatars Portrait Style Migration, which makes your photos older, younger, hair color, etc Beauty Camera, which supports peeling

JXA and swift code that can perform some macOS situational awareness without generating TCC prompts.

Spotlight Enumeration Kit Repo of Swift and JXA projects to leverage macOS Spotlight db data for the following: TCC folder permissions (TCC-Checker.js

Cardshark is an iOS card counting App that uses state of the art machine learning (YOLO) to classify and count the cards at real time.

Cardshark The game of Blackjack is one of the most popular casino games in the world. It is also the most winnable using a skill called Card Counting.

Includes CreateML and other learning materials (day 26-35) in 100 days of SwiftUI
Includes CreateML and other learning materials (day 26-35) in 100 days of SwiftUI

SwiftUI Day 26 to 35 Simulator.Screen.Recording.-.iPhone.12.-.2021-12-25.at.21.1

Holistically-Nested Edge Detection (HED) using CoreML and Swift
Holistically-Nested Edge Detection (HED) using CoreML and Swift

HED-CoreML Holistically-Nested Edge Detection (HED) using CoreML and Swift This is the repo for tutorial, that contains an example application that ru

Real-time single person pose estimation for Android and iOS.
Real-time single person pose estimation for Android and iOS.

This repository currently implemented the CPM and Hourglass model using TensorFlow. Instead of normal convolution, inverted residuals (also known as M

Vision Camera 📸 The Camera library that sees the vision.
Vision Camera 📸 The Camera library that sees the vision.

Vision Camera 📸 The Camera library that sees the vision. npm i react-native-vision-camera npx pod-install Documentation Guides API Ex

A simple macOS app to read code from images, written purely in Swift using Vision Framework.
A simple macOS app to read code from images, written purely in Swift using Vision Framework.

CodeReader A simple macOS app to read code from images, written purely in Swift using Vision Framework. Usage Drag an image Click the convert button R

Text-cli - Command line tool for extracting text from images using Apple's Vision framework
Text-cli - Command line tool for extracting text from images using Apple's Vision framework

text-cli Command line tool for extracting text from images using Apple's Vision

A Demo using Vision Framework building on Core ML Framework
A Demo using Vision Framework building on Core ML Framework

Core-ML-Sample A Demo using Core ML, Vision Framework and Swift 4. This demo is based on Inception V3 network. You must run it with Xcode 9 and iOS 11

Docker images for Swift on Raspberry Pi and other ARM devices from balena's base images.

Swift on Balena Welcome to Swift on Balena – a set of Docker images for Swift on Raspberry Pi and other ARM devices. These images are based on balena'

The images are fetched by batch of 100 images per searche

Google Images Google Images API The images are fetched by batch of 100 images pe

JSON to Core Data and back. Swift Core Data Sync.
JSON to Core Data and back. Swift Core Data Sync.

Notice: Sync was supported from it's creation back in 2014 until March 2021 Moving forward I won't be able to support this project since I'm no longer

Core Data Generator (CDG for short) is a framework for generation (using Sourcery) of Core Data entities from plain structs/classes/enums.
Core Data Generator (CDG for short) is a framework for generation (using Sourcery) of Core Data entities from plain structs/classes/enums.

Core Data Generator Introduction Features Supported platforms Installation CDG Setup RepositoryType ModelType DataStoreVersion MigrationPolicy Basic U

✂️ Detect and crop faces, barcodes and texts in image with iOS 11 Vision api.
✂️ Detect and crop faces, barcodes and texts in image with iOS 11 Vision api.

ImageDetect ImageDetect is a library developed on Swift. With ImageDetect you can easily detect and crop faces, texts or barcodes in your image with i

A credit card reader and parser for iOS Using Native Vision/VisionKit

card-reader-ios A credit card reader and parser for iOS Using Native Vision/VisionKit May-14-2021.00-43-17.mp4 Instructions Hold camera up to a card a

Screen translator for macOS with Apple Vision API and IBM Watson, Google Cloud Translator
Screen translator for macOS with Apple Vision API and IBM Watson, Google Cloud Translator

Swifty-OCR-Translator Screen translator for macOS with Apple Vision API and IBM Watson, Google Cloud Translator Usage Select Translator Fill in the AP

Real-Time image recognition for iOS with Vision(CoreML) and InceptionV3
Real-Time image recognition for iOS with Vision(CoreML) and InceptionV3

Kesan-ML-iOS Real-Time image recognition for iOS with CoreML and InceptionV3  Test Flight Description Real-Time image recognition Integrating app wit

Owner
Ivan Kolesov
Ivan Kolesov
Text Classifier App for iOS, powered by Apple Vision & CreateML framework

Text Classifier App for iOS, powered by Apple Vision & CreateML framework

Ikmal Azman 2 Sep 15, 2022
TextDetection-CoreML - This project is Text Detection on iOS using Vision built-in model

This project is Text Detection on iOS using Vision built-in model. If you are interested in iOS + Machine Learning, visit here yo

tucan9389 61 Nov 17, 2022
Photo Assessment using Core ML and Metal.

PhotoAssessment Photo Assessment (i.e. quality score) using Core ML and Metal. ?? Article 使用 Metal 和 Core ML 评价照片质量 Parallel Computation using MPS ??

杨萧玉 59 Dec 26, 2022
A CoreML model which classifies images of food

Food101 for CoreML Description This is the Food101 dataset implemented in Apple's new framework called CoreML. The Food101 dataset can predict foods f

Philipp Gabriel 133 Nov 17, 2022
Swift framework for document classification using a Core ML model.

DocumentClassifier Overview DocumentClassifier is a Swift framework for classifying documents into one of five categories (Business, Entertainment, Po

Todd Kramer 40 Nov 15, 2022
iOS Core ML implementation of waifu2x

waifu2x on iOS Introduction This is a Core ML implementation of waifu2x. The target of this project is to run waifu2x models right on iOS devices even

Yi Xie 469 Jan 8, 2023
The example of running Depth Prediction using Core ML

DepthPrediction-CoreML This project is Depth Prediction on iOS with Core ML. If you are interested in iOS + Machine Learning, visit here you can see v

tucan9389 113 Nov 17, 2022
The example project of inferencing Semantic Segementation using Core ML

SemanticSegmentation-CoreML This project is Object Segmentation on iOS with Core ML. If you are interested in iOS + Machine Learning, visit here you c

tucan9389 248 Dec 17, 2022
MXNet to Core ML - iOS sample app

Bring Machine Learning to iOS apps using Apache MXNet and Apple Core ML

Amazon Web Services - Labs 54 Nov 9, 2022
A demo for iOS machine learning framework : Core ML

CoreMLDemo A demo for iOS machine learning framework : Core ML Only Xcode9 and above are supported. Model Places205-GoogLeNet comes from [Apple Machin

null 32 Sep 16, 2022