For the complete documentation index, see llms.txt. This page is also available as Markdown.

Food analysis

Use client.foodAnalysis to analyze a meal photo or a description, and to correct a result. These examples use the client from Authentication.

An analysis can take tens of seconds. Show progress, and don't retry a .timeout automatically.

Analyze a photo

Pass the image bytes. The request orients the image, scales its longest edge down to 1,000 pixels, and compresses it to JPEG:

let request = try ScanFoodPhotoRequest(imageData: imageData)
let scan = try await client.foodAnalysis.analyzePhoto(request)

To get imageData from the photo library, call loadTransferable(type: Data.self) on a PhotosPicker item (iOS 16 and later), or loadDataRepresentation(forTypeIdentifier: UTType.image.identifier) on a PHPickerViewController result's item provider (iOS 15).

For an image at a public URL, pass the URL string instead:

let scan = try await client.foodAnalysis.analyzePhoto(.init(image: imageURL.absoluteString))

For a ready-made camera screen, use the native food scanner.

Analyze a description

let meal = try await client.foodAnalysis.analyzeDescription(
    .init(query: "one banana and a bowl of oatmeal")
)

Read the result

Both calls return a FoodScan: detections (always an array, possibly empty), totalNutrients, and an optional mealName. Each detection's food has the food id, the selected serving, and the quantity eaten, so you can log it without another lookup. Food analysis doesn't return a glucose impact; use glucose prediction for that.

Correct a result

Pass the analysis back exactly as it was returned. The SDK rejects, before sending, a detection that's missing its food ID, serving ID, serving quantity, or quantity. A detection's serving weightGrams is sent when the API reported it.

Next: Native food scanner.

Last updated

Was this helpful?