Urgent.News

What's breaking now, across thousands of outlets.

AI

Stop Guessing Your Macros: Building a Precise Calorie Estimator with SAM and GPT-4o ๐Ÿฅ—๐Ÿš€

Weโ€™ve all been there: staring at a delicious plate of pasta, trying to figure out if itโ€™s 400 calories or 800 calories for our fitness tracker. Traditional Dietary Analysis apps often fail because they can't distinguish between different food items on a single plate or accurately estimate portion sizes. In this tutorial, we are going to bridge that gap using a Multimodal Vision pipeline. Byโ€ฆ

In this tutorial, we will build a precise calorie estimator using a combination of the Segment Anything Model (SAM) and GPT-4o API. The goal is to overcome the limitations of traditional dietary analysis apps that struggle to differentiate between food items on a single plate and accurately estimate portion sizes. The architecture involves several key steps:

1. User uploads a photo of their meal.

2. The image is preprocessed using OpenCV to prepare it for SAM.

3. SAM generates masks for each distinct food item in the image, providing spatial context that helps GPT-4o understand the scale and boundaries of each dish.

4. GPT-4o receives the original image and the masked metadata in a structured JSON format. It analyzes the image and the segment clues to identify food items, estimate their weight in grams, and provide detailed nutritional information such as calories, protein, fats, and carbs.

5. The final calorie and macro report is returned to the user.

To implement this system, you will need Python 3.9 or higher, an OpenAI API key for accessing GPT-4o, PyTorch for running SAM locally, and FastAPI for creating the API layer. The tutorial provides code snippets for each step, starting with segmenting the plate using SAM and ending with setting up the FastAPI backend to handle API requests and responses.

Written by urgent.news from Dev.to's reporting โ€” not their text. Machine-written โ€” may contain errors; check the original before relying on it.

Read the original at dev.to โ†’

More in AI

Mojeed Abisiga: Fostering Innovation, Advancement and Professional Development of Global Data, AI Community

Mojeed Abisiga is an award-winning Data & AI Leader, entrepreneur, technology educator, public speaker, and Forbes Technology Council member, recognised as a Marquis Whoโ€™s Who Honoree, an Einsteinโ€ฆ

  • Mojeed Abisiga is CEO and founder of AI Every Time and DataGlobal Hub
  • Passionate about making complex information accessible and useful worldwide
  • Envisions AI augmenting human decision-making in information-intensive industries

Beyond the Monolithic Skill: Architecting Hierarchical Sub-Agents with Mixed Model Tiers

Once you have persistent memory and clean session hygiene, the next hurdle is skill design. Here is how we avoid skill fragmentation while keeping execution fast and token-efficient.

  • Implement hierarchical sub-agents within unified skills to avoid Monolithic Skill Trap
  • Assign lightweight models to rigid tasks for quick execution and cost efficiency
  • Reserve medium-tier models for nuanced tasks requiring contextual depth and stylistic judgment

More from Tuesday 8 September โ†’