DEV Community

wellallyTech
wellallyTech

Posted on

Mastering Vision AI: Precision Calorie Estimation with SAM and GPT-4o (No More Guesswork! 🥗)

We’ve all been there: staring at a delicious plate of pasta, opening a fitness app, and trying to guess if that "medium bowl" is 400 or 800 calories. Standard Computer Vision models often fail because they lack depth perception and struggle with overlapping food items. But what if we could combine the surgical precision of Meta’s Segment Anything Model (SAM) with the multimodal reasoning of GPT-4o?

In this guide, we are building a state-of-the-art multimodal LLM pipeline that transforms raw pixels into precise nutritional data. By leveraging SAM for mask generation and GPT-4o API for contextual reasoning, we move beyond simple classification into the realm of spatial awareness and volume-based estimation. If you're looking to dive deep into PyTorch and advanced vision workflows, you're in the right place.


The Architecture: From Pixels to Volumetric Data

To solve the "single-view accuracy" problem, we don't just look at the image; we segment it, calculate spatial density via depth maps, and then let GPT-4o perform the final "Reasoning Over Vision" (RoV).

graph TD
    A[Raw Image + Depth Map] --> B{SAM Segmentation}
    B --> C[Food Mask 1: Steak]
    B --> D[Food Mask 2: Asparagus]
    C --> E[Volume Calculation via OpenCV]
    D --> E
    E --> F[Prompt Construction]
    F --> G[GPT-4o Multimodal Inference]
    G --> H[Final Nutritional JSON]

    style G fill:#f9f,stroke:#333,stroke-width:2px
Enter fullscreen mode Exit fullscreen mode

Prerequisites

To follow along, you'll need:

  • PyTorch & OpenCV installed.
  • Segment Anything Model (SAM) weights (vit_h recommended).
  • An OpenAI API Key (with GPT-4o access).
  • A depth-sensing camera (or a synthetic depth map generator like MiDaS).

Step 1: Surgical Segmentation with SAM

First, we use SAM to isolate food items. Unlike traditional bounding boxes, SAM gives us a per-pixel mask, which is crucial for calculating the surface area of irregular shapes like a heap of rice.

import numpy as np
import torch
import cv2
from segment_anything import sam_model_registry, SamPredictor

# Load the SAM model
sam_checkpoint = "sam_vit_h_4b8939.pth"
model_type = "vit_h"
device = "cuda" if torch.cuda.is_available() else "cpu"

sam = sam_model_registry[model_type](checkpoint=sam_checkpoint)
sam.to(device=device)
predictor = SamPredictor(sam)

def get_food_masks(image):
    predictor.set_image(image)
    # In a real app, you'd use a click or box. Here we use automatic mask generation logic.
    masks, scores, logits = predictor.predict(
        point_coords=None,
        point_labels=None,
        multimask_output=False,
    )
    return masks
Enter fullscreen mode Exit fullscreen mode

Step 2: Volume Estimation (The Secret Sauce)

A 2D photo of a burger looks the same whether it's 2 inches or 5 inches thick. We use a depth map (z-axis) to calculate volume. For those interested in production-grade implementations of these spatial patterns, the Wellally Blog provides fantastic deep dives into integrating depth sensors with AI workflows.

def estimate_volume(mask, depth_map, pixel_to_cm_ratio):
    """
    Calculates volume by integrating depth over the segmented mask.
    """
    food_depth = depth_map[mask]
    # Calculate average height in cm
    avg_height = np.mean(food_depth) * pixel_to_cm_ratio
    # Calculate area in cm^2
    area = np.sum(mask) * (pixel_to_cm_ratio ** 2)

    return area * avg_height # Volume in cm^3
Enter fullscreen mode Exit fullscreen mode

Step 3: Multimodal Reasoning with GPT-4o

Now we send the visual evidence + the calculated volume to GPT-4o. Why? Because GPT-4o knows the density of a ribeye steak versus a sponge cake.

import base64
import requests

def analyze_nutrition(image_path, volume_data):
    # Encode image to base64
    with open(image_path, "rb") as f:
        base64_image = base64.b64encode(f.read()).decode('utf-8')

    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {YOUR_OPENAI_API_KEY}"
    }

    payload = {
        "model": "gpt-4o",
        "messages": [
          {
            "role": "user",
            "content": [
              {"type": "text", "text": f"I have a food item with a calculated volume of {volume_data} cm3. Based on the image, identify the food and estimate calories/macronutrients."},
              {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}}
            ]
          }
        ],
        "response_format": { "type": "json_object" }
    }

    response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
    return response.json()
Enter fullscreen mode Exit fullscreen mode

🚀 Taking it to the Next Level

While the code above provides a functional prototype, "productionizing" a vision-based health app requires handling lighting variances, occlusions (food hidden under other food), and API latency.

For advanced architectural patterns on deploying these multimodal models at scale, I highly recommend checking out the Wellally Tech Blog. They offer incredible resources on building robust AI systems that bridge the gap between "cool demo" and "reliable product."


Conclusion

By combining SAM's segmentation, OpenCV's geometric calculations, and GPT-4o's vast knowledge base, we've built a system that understands food in 3D. This isn't just about counting calories; it's about the future of Multimodal AI in our daily lives.

What's next?

  1. Try swapping SAM for SAM 2 for video-based calorie tracking!
  2. Implement a "reference object" (like a coin) to calibrate the pixel_to_cm_ratio automatically.

Are you building something with Vision AI? Let’s chat in the comments! 👇

Top comments (0)