Handler with VNImageRequestHandler
Firstly below model method, Create handler constant with VNImageRequestHandler
(Option with ciImage).
let handler = VNImageRequestHandler(ciImage: image)
And then, implement DispatchQueue.global(qos: .userInteractive).async
method that will work on the background as we did before while computing the confidence rate.
let handler = VNImageRequestHandler(ciImage: image)
DispatchQueue.global(qos: .userInteractive).async {
}
Create do-catch block and process request with handler.perform().
let handler = VNImageRequestHandler(ciImage: image)
DispatchQueue.global(qos: .userInteractive).async {
do {
try handler.perform([request])
} catch {
print("Err :(")
}
}
it's done. The final state of recognizeImage
function:
func recognizeImage(image: CIImage) {
resultLabel.text = "I'm investigating..."
if let model = try? VNCoreMLModel(for: GoogLeNetPlaces().model) {
let request = VNCoreMLRequest(model: model, completionHandler: { (vnrequest, error) in
if let results = vnrequest.results as? [VNClassificationObservation] {
let topResult = results.first
DispatchQueue.main.async {
let confidenceRate = (topResult?.confidence)! * 100
let rounded = Int (confidenceRate * 100) / 100
self.resultLabel.text = "\(rounded)% it's \(topResult?.identifier ?? "Anonymous")"
}
}
})
let handler = VNImageRequestHandler(ciImage: image)
DispatchQueue.global(qos: .userInteractive).async {
do {
try handler.perform([request])
} catch {
print("Err :(")
}
}
}
}