Skip to content

Instantly share code, notes, and snippets.

@matheusruschel
Created March 10, 2023 17:13
Show Gist options
  • Save matheusruschel/4276b943ab6ef633e844c67a0ff54f83 to your computer and use it in GitHub Desktop.
Save matheusruschel/4276b943ab6ef633e844c67a0ff54f83 to your computer and use it in GitHub Desktop.
import CoreML
import Vision
class FlowerRecognizer {
let model: VNCoreMLModel
init() {
let modelURL = Bundle.main.url(forResource: "FlowerModel", withExtension: "mlmodel")!
model = try! VNCoreMLModel(for: MLModel(contentsOf: modelURL))
}
func recognizeFlower(image: UIImage) -> String {
let request = VNCoreMLRequest(model: model) { request, error in
guard let results = request.results as? [VNClassificationObservation],
let topResult = results.first else {
return
}
print("Recognized flower: \(topResult.identifier)")
}
let handler = VNImageRequestHandler(cgImage: image.cgImage!, options: [:])
try! handler.perform([request])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment