Deploying CNN to iOS
You need to drag-and-drop the Core ML file generated in the previous section into your project to start working with the model.
Imports:
import Foundation import Vision import AVFoundation import UIKit
At first, let's define some data structures. An enumeration for possible classification results:
enum FaceExpressions: String {
case angry = "angry"
case anxious = "anxious"
case neutral = "neutral"
case happy = "happy"
case sad = "sad"
}An enum for errors of the classifier:
enum ClassifierError: Error {
case unableToResizeBuffer
case noResults
}Classifier is a wrapper singleton for Core ML model:
class Classifier {
public static let shared = Classifier()
private let visionModel: VNCoreMLModel
var visionRequests = [VNRequest]()
var completion: ((_ label: [(FaceExpressions, Double)], _ error: Error?)->())?private init() {
guard let visionModel = try? VNCoreMLModel(for: Emotions().model) else {
fatalError("Could not load model")
}
self.visionModel = visionModel...