Skip to content

Instantly share code, notes, and snippets.

@mohdsanadzakirizvi
Last active November 19, 2019 08:47
Show Gist options
  • Save mohdsanadzakirizvi/09c4a4e3e081b318b5df381a7bca2dab to your computer and use it in GitHub Desktop.
Save mohdsanadzakirizvi/09c4a4e3e081b318b5df381a7bca2dab to your computer and use it in GitHub Desktop.
// MARK: - Methods
extension ViewController {
func imageClassify(image: CIImage) {
answerLabel.text = "detecting..."
// Load the ML model through its generated class
guard let model = try? VNCoreMLModel(for: Resnet50().model) else {
fatalError("can't load Places ML model")
}
// Create a Vision request with completion handler
let request = VNCoreMLRequest(model: model) { [weak self] request, error in
let results = request.results as? [VNClassificationObservation]
var outputText = ""
for res in results!{
outputText += "\(Int(res.confidence * 100))% it's \(res.identifier)\n"
}
DispatchQueue.main.async { [weak self] in
self?.answerLabel.text! = outputText
}
}
// Run the CoreML3 Resnet50 classifier on global dispatch queue
let handler = VNImageRequestHandler(ciImage: image)
DispatchQueue.global(qos: .userInteractive).async {
do {
try handler.perform([request])
} catch {
print(error)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment