Created
March 10, 2023 17:13
-
-
Save matheusruschel/4276b943ab6ef633e844c67a0ff54f83 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
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