I have made a object detection model and when I load my model in xcode I can see the expected results when inputing an image in the preview page of the model. However, when I am trying to use the model in my code it is return 0 detections, however when i put the same image in preview it returns expected result? Anyone know what could be wrong?
func detectLeaf(in image: UIImage) -> Int {
var leafDetected = 0
guard let modelURL = Bundle.main.url(forResource: "LeafModel", withExtension: "mlmodelc") else {
print("Model file is missing")
return leafDetected
}
do {
let visionModel = try VNCoreMLModel(for: MLModel(contentsOf: modelURL))
let objectRecognition = VNCoreMLRequest(model: visionModel) { request, error in
if let results = request.results {
print(results.count)
print(results)
leafDetected = results.count
}
}
let handler = VNImageRequestHandler(cgImage: image.cgImage!)
try handler.perform([objectRecognition])
return leafDetected
} catch {
print("Error loading model: \(error.localizedDescription)")
return 0
}
}