Food Recognition Delegate

We'll now resolve the error introduced earlier by implementing FoodRecognitionDelegate:

extension ViewController: FoodRecognitionDelegate {
    func recognitionResults(candidates: FoodCandidates?, image: UIImage?) {
        guard !isProcessingResult else { return }
        
        if let candidates = candidates?.barcodeCandidates,
           let candidate = candidates.first {
            print("Found barcode: \(candidate.value)")
        }
        
        if let candidates = candidates?.packagedFoodCandidates,
           let candidate = candidates.first {
            print("Found packaged food: \(candidate.packagedFoodCode)")
        }
        
        if let candidates = candidates?.detectedCandidates,
           let candidate = candidates.first {
            isProcessingResult = true
            PassioNutritionAI.shared.fetchFoodItemFor(passioID: candidate.passioID) { [weak self] foodItem in
                if let foodItem {
                    let nutrients250g = foodItem.nutrients(weight: Measurement<UnitMass>(value: 250.0, unit: .grams))
                    
                    if let calories = nutrients250g.calories()?.converted(to: .kilocalories) {
                        print("Food: \(foodItem.foodItemName): \(calories.value) calories")
                    }
                }
                self?.isProcessingResult = false
            }
        }
    }
}

If at any point you need help from the Passio team, please reach out to us at support@passiolife.com

Last updated