Last active
November 19, 2019 08:47
-
-
Save mohdsanadzakirizvi/09c4a4e3e081b318b5df381a7bca2dab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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