Reputation: 712
I am making an app to classify food images. I trained a model using a dataset found from Kaggle. When press the classify button I am getting this error in console:
Error Domain=com.apple.vis Code=9 "Could not create Espresso context" UserInfo={NSLocalizedDescription=Could not create Espresso context}
So I was looking for solution by googling and found in apple developer forum that it is M1 Mac specific issue. What should I do now?
Classify class:
import Foundation
import CoreML
import Vision
import UIKit
class VisionClasifier{
private let model : VNCoreMLModel
private var completion: (String) -> Void = { _ in }
private lazy var request:[VNCoreMLRequest] = {
let request = VNCoreMLRequest(model: model){ request, error in
guard let results = request.results as? [VNClassificationObservation] else{
return
}
if !results.isEmpty{
if let result = results.first{
self.completion(result.identifier)
}
}
}
request.imageCropAndScaleOption = .centerCrop
return [request]
}()
init?(mlModel:MLModel?){
if let m = mlModel{
if let model = try? VNCoreMLModel(for: m){
self.model = model
}else{
return nil
}
}else{
return nil
}
}
func classify(_ image:UIImage, completion:@escaping (String)->Void){
self.completion = completion
DispatchQueue.global().async {
guard let image = image.cgImage else{
return
}
let handler = VNImageRequestHandler(cgImage: image, options: [:])
do{
try handler.perform(self.request)
}catch{
print(error) // -> here is the error
}
}
}
}
Is this an M1 Mac specific error?
Upvotes: 1
Views: 573
Reputation: 156
Yes, it can be a M1 specific error.
after line:
request.imageCropAndScaleOption = .centerCrop
add
request.usesCPUOnly = true
Upvotes: 2