Skip to content

mrzasad/ai-macronutrients-detector

Repository files navigation

Food Nutrition Detector

Overview

Food Nutrition Detector is a small ASP.NET Core (.NET 8) Web API that uses Azure OpenAI to identify food in an uploaded image and estimate basic nutritional values. The API accepts an image and returns a JSON object with the detected food name and nutrient estimates (calories, carbs, protein, fat) along with optional notes.

Key components

  • Food_Nutrition_Detector.Services.OpenAiNutritionService -- wraps Azure AI OpenAI client to send an image and prompt to a deployed model and parse the JSON response.
  • Food_Nutrition_Detector.Controllers.NutritionController -- exposes a single analyze endpoint that accepts an image upload.
  • Food_Nutrition_Detector.Models.NutritionInfo -- the response model returned by the analyze endpoint.

Prerequisites

  • .NET 8 SDK
  • An Azure OpenAI resource with a deployment that accepts chat + image input
  • An API key and endpoint for the Azure OpenAI resource

Configuration

Add your Azure OpenAI settings to appsettings.json (or appsettings.Development.json) under the AzureOpenAI section. Example:

{ "AzureOpenAI": { "Endpoint": "https://YOUR_RESOURCE.openai.azure.com", "ApiKey": "YOUR_API_KEY", "Deployment": "your-deployment-name" } }

How to run

  1. Restore packages and run the project using dotnet (or open in Visual Studio).
  2. When running in Development the project exposes Swagger UI at /swagger for manual testing.

API: Analyze Image

  • Endpoint: POST /api/nutrition/analyze
  • Content type: multipart/form-data
  • Form field: file (the image to analyze)

Request example (multipart/form-data):

  • file: (binary image file)

Response

The API returns a JSON document matching the NutritionInfo model:

{ "foodName": "string", // Detected food name "calories": 0.0, // Estimated calories (kcal) "carbs": 0.0, // Estimated carbs (grams) "protein": 0.0, // Estimated protein (grams) "fat": 0.0, // Estimated fat (grams) "notes": "string" // Additional info or parsing errors }

Notes and error handling

  • If the AI response cannot be parsed as JSON, the API will still return a NutritionInfo object with the Notes property populated to indicate the parsing failure and include the raw AI text.
  • The accuracy of the estimates depends on the model and the image quality. Treat returned values as rough estimates only.

Extending or customizing

  • Update the prompt in OpenAiNutritionService to refine the JSON shape or request units, confidence, or additional nutrients.
  • If you want richer responses, consider altering NutritionInfo and the expected JSON format in the service deserialization.

About

The Food Nutrition Detector is an ASP.NET Core (.NET 8) Web API that uses Azure OpenAI to identify food in an uploaded image and estimate basic nutritional values. The API accepts an image and returns a JSON object with the detected food name and nutrient estimates along with optional notes.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages