Testing Smart Nutrition API

Introduction

A smart nutrition API that fetches the nutritional information from pictures of labelled products. As we will see next, the API returns somewhat close nutritional metrics compared to the actual labels at around 3.0% error rate.

The technical details are mentioned in the GitHub documentation and with the docker image and instructions present in the DockerHub. There is also a comprehensive and experimental guide to setup Kubernetes cluster and deploy the API container in a pod.

Following are minimalistic examples of development.yaml and service.yaml for K8s deployment:

# development.yaml - fill the following required API keys as env variable.
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nutrition-api
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nutrition-api
  template:
    metadata:
      labels:
        app: nutrition-api
    spec:
      containers:
      - name: nutrition-api
        image: tkdutta/smart-nutrition-api:v1
        ports:
        - containerPort: 5000
        env:
        - name: GOOGLE_API_KEY
          value: ""
        - name: GROQ_API_KEY
          value: ""
# service.yaml
apiVersion: v1
kind: Service
metadata:
  name: nutrition-api-service
spec:
  type: NodePort
  selector:
    app: nutrition-api
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8000
      nodePort: 30007

Short description of the endpoints

Here is a simple refresher of the endpoints of the API (with more details is the baove mentioned documentations):

  1. /product-name: Extract product name from an uploaded image.

    • Payload: Form-data with key “product” as the image file.

    • Returns: json file with the extracted product name.

  2. /nutrition: Get nutritional information for a given product name. Returns a json file.

    • Payload: Value from the /product-name output.

    • Returns: json file with it’s nutritional values.

Using the API

  1. Passing the following image in the /product-name endpoint.

    Source: Amazon (Cadbury Dairy Milk Silk Oreo Chocolate Bars, 58.5 g)

  2. We get the following json output based on the above image provided:

     {
         "name": "dairymilk silk oreo"
     }
    
  3. Passing the above product name in the /nutrition endpoint to get the following output:

     {
         "data": {
             "Nutritional Content": {
                 "Added Sugars": "40.5 g",
                 "Carbohydrates": "56.5 g",
                 "Cholesterol": "13.7 mg",
                 "Energy": "559 kcal",
                 "Protein": "6.1 g",
                 "Saturated Fat": "21.15 g",
                 "Sodium": "212 mg",
                 "Total Fat": "34.85 g",
                 "Total Sugars": "48.6 g",
                 "Trans Fat": "0.15 g"
             }
         },
         "heading": "Nutritional Content of Cadbury Dairy Milk Silk Oreo per 100g"
     }
    

Validating the output

Let’s validate the output with the actual label in the product:

Nutritional contentsAPI output (per 100 g)Label (per 100 g)Error %
Energy559.0 kcal563.0 kcal0.7 %
Carbohydrates56.5 g56.9 g0.7 %
Protien6.1 g6.0 g-1.67 %
Sugars48.6 g48.9 g0.6 %
Total Fat34.85 g34.8 g-0.14 %
Saturated Fat21.15 g22.3 g0.67 %
Sodium212.0 mg207.0 mg-2.42 %
Cholesterol13.7 mg17.4 mg21.26 %

Therefore, the Mean Absolute Percentage Error (MAPE) of the above is ~3.8%

Performing the same analysis for multiple products across various brands give an average error rate of ~3.0%

Governance, Trust & Disclaimer

  1. Nutrition data is approximate and may differ from actual food content. Use as guidance, not as medical advice.

  2. Not a medical-grade app. For general use only and is not to be considered as ultimate source of truth without proper research.

  3. Data is extracted through general internet scraping.

  4. To be treated like a navigation app - great for guidance, but not a substitute for a doctor’s prescription or lab-tested food assay.

Future Prospects

  • Develop a mobile UI app (Android) wrapped around the API.

  • Enable users to scan product labels in real time (e.g., in supermarkets or malls).

  • Provide instant nutritional information fetched via the API.

  • Allow side-by-side comparison of nutritional values across multiple products.

  • Make nutrition tracking more accessible and practical for everyday choices.

0
Subscribe to my newsletter

Read articles from Tuhin Kumar Dutta directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tuhin Kumar Dutta
Tuhin Kumar Dutta

I decode data, craft AI solutions, and write about everything from algorithms to analytics. Here to share what I learn and learn from what I share. 🚀 Data Scientist | AI Enthusiast | Building intelligent systems & simplifying complexity through code and curiosity. Sharing insights, projects, and deep dives in ML, data, and innovation.