Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
220 changes: 45 additions & 175 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,194 +1,64 @@
# FoodSnap AI - Hackathon Project
# 🥗 FoodSnap AI

🎉 **Welcome to the FoodSnap AI Hackathon!** 🎉
**FoodSnap AI** is a lightweight FastAPI application that enables users to obtain nutritional information about food items by simply uploading an image. It uses a pre-trained machine learning model to classify food images and retrieves detailed macronutrient data from a connected database.

Get ready to combine your coding skills with the fascinating world of artificial intelligence and nutrition! We're excited to see what innovative solutions you come up with.
---

## Features

- **Intelligent Food Classification**
Upload an image, and our integrated ResNet50 model identifies the food item.

- **Comprehensive Nutritional Data**
Get calories, carbohydrates, protein, and fats for the identified food.

**The Challenge:**
Your mission, should you choose to accept it, is to develop a solution that can estimate the caloric content (and ideally, macronutrient breakdown – protein, carbs, fats) of a food item from an input image.
- **Quantity-Based Calculations**
Receive accurate nutritional values scaled to your specified quantity.

## Table of Contents
- **Efficient Model Management**
The AI model is loaded once at startup for fast subsequent processing.

1. [Project Overview](#project-overview)
2. [Why This Project?](#why-this-project)
3. [Scope & Implementation Freedom](#project-scope--implementation-freedom)
4. [Key Objectives / Potential Features](#key-objectives--potential-features)
5. [Helpful Resources & APIs](#helpful-resources--apis)
* [Food Image Datasets](#food-image-datasets)
* [Nutrition Information Databases & APIs](#nutrition-information-databases--apis)
* [Machine Learning / AI Tools](#machine-learning--ai-tools)
6. [Getting Started: Git & Submission Workflow](#getting-started-git--submission-workflow)
* [1. Fork the Repository](#1-fork-the-repository)
* [2. Clone Your Forked Repository](#2-clone-your-forked-repository)
* [3. Create a New Branch](#3-create-a-new-branch)
* [4. Develop Your Project](#4-develop-your-project)
* [5. Push Your Branch to Your Fork](#5-push-your-branch-to-your-fork)
* [6. Submit Your Project (Create a Pull Request)](#6-submit-your-project-create-a-pull-request)
7. [What to Include in Your Submission](#what-to-include-in-your-submission)
8. [Judging Criteria (General Pointers)](#judging-criteria-general-pointers)
- **Robust API**
Exposes a well-structured `/foodsnap/v1/upload` endpoint for seamless integration.

- **Data Population (Web Scraping)**
Nutritional data is fetched and updated in the database using web scraping techniques.




---

## Project Overview
## Getting Started

**Project Name:** FoodSnap AI
Follow the steps below to set up and run **FoodSnap AI** on your local machine.

**The Challenge:**
Develop a solution that can estimate the caloric content (and ideally, macronutrient breakdown – protein, carbs, fats) of a food item from an input image.
## Tech Stack

## Why This Project?
- FastAPI
- TensorFlow
- ResNet50 (pre-trained model)
- Beautiful Soup
- Selenium
- Pandas
- SQLAlchemy + PostgreSQL

Understanding calorie intake is crucial for health and fitness. Manually logging food can be tedious. FoodSnap AI aims to simplify this process, making nutritional awareness more accessible to everyone. Imagine snapping a photo of your meal and instantly getting its nutritional information!

## Project Scope & Implementation Freedom
## Installation

You have complete freedom in how you bring FoodSnap AI to life!

* **Platform:** Mobile App (iOS, Android, cross-platform), Web App, Website, or even a Command Line Interface (CLI) tool.
* **Technology Stack:** Use any programming languages, frameworks, libraries, or APIs you prefer. Python, JavaScript, Java, Swift, Kotlin, Ruby, Go – the choice is yours!
* **Approach:** You can use pre-trained machine learning models, train your own, leverage existing food recognition APIs, or come up with a completely novel approach.

## Key Objectives / Potential Features

1. **Image Input:** The system must accept an image of food as input.
2. **Food Identification (Implicit or Explicit):** The system needs to identify the food item(s) in the image. This could be a direct output or an internal step.
3. **Calorie Estimation:** Based on the identified food, estimate its total calories.
4. **Macronutrient Breakdown (Bonus):** If possible, also estimate protein, carbohydrates, and fats.
5. **Portion Size Consideration (Advanced Bonus):** Accurately estimating portion size from an image is challenging but would be a significant enhancement.
6. **User Interface (for non-CLI):** If you're building an app or website, make it user-friendly and intuitive.
7. **Multiple Food Items (Advanced Bonus):** Can your solution handle an image with multiple food items on a plate?

## Helpful Resources & APIs

To get you started, here are some resources that might be useful. You are **not limited** to these and are encouraged to explore!

### Food Image Datasets
*(For training/inspiration if you go the custom ML route)*

* **Food-101:** [https://data.vision.ee.ethz.ch/cvl/datasets_extra/food-101/](https://data.vision.ee.ethz.ch/cvl/datasets_extra/food-101/) (101 food categories, 101,000 images)
* **UECFood-100 / UECFood-256:** [http://foodcam.mobi/dataset.html](http://foodcam.mobi/dataset.html) (Japanese food primarily, good for object detection)
* **Recipe1M+:** [http://pic2recipe.csail.mit.edu/](http://pic2recipe.csail.mit.edu/) (Images and recipes)
* **Google Images / Flickr:** Can be used for scraping specific food images (be mindful of terms of service).

### Nutrition Information Databases & APIs
*(For calorie/macro lookup)*

* **USDA FoodData Central API:** [https://fdc.nal.usda.gov/api-guide.html](https://fdc.nal.usda.gov/api-guide.html) (Comprehensive US food composition database)
* **Edamam Food Database API:** [https://developer.edamam.com/food-database-api](https://developer.edamam.com/food-database-api) (Offers free tier for recipe analysis and food database lookup)
* **Spoonacular API:** [https://spoonacular.com/food-api](https://spoonacular.com/food-api) (Nutrition, recipes, food products, free tier available)
* **MyFitnessPal / CalorieKing / FatSecret:** While direct API access might be limited or paid, these websites are excellent sources for manual data collection or understanding how nutritional information is presented. Web scraping *could* be an option, but always respect `robots.txt` and terms of service.
* **Open Food Facts:** [https://world.openfoodfacts.org/](https://world.openfoodfacts.org/) (A collaborative, free, and open database of food products from around the world. They have an API.)

### Machine Learning / AI Tools

* **TensorFlow / Keras:** For building and training custom models.
* **PyTorch:** Another popular deep learning framework.
* **OpenCV:** For image processing tasks.
* **Pre-trained Image Recognition Models:** (e.g., MobileNet, ResNet, InceptionV3 available via TensorFlow Hub, PyTorch Hub, etc.) These can often be fine-tuned for food recognition.
* **Cloud AI Services:** Google Cloud Vision AI, AWS Rekognition, Azure Computer Vision (these often have free tiers for experimentation and can perform object/food recognition out-of-the-box).

## Getting Started: Git & Submission Workflow

We will be using GitHub for version control and submission. Please follow these steps carefully.

**This Repository (Main Project):** `https://github.com/WeCode-Community-Dev/foodsnap-ai`

### 1. Fork the Repository
* Go to the main project repository: `https://github.com/WeCode-Community-Dev/foodsnap-ai`
* In the top-right corner of the page, click the "**Fork**" button.
* This will create a copy of the repository under your own GitHub account (e.g., `https://github.com/YOUR_USERNAME/foodsnap-ai`). This is *your* personal remote copy.

### 2. Clone Your Forked Repository
* On your GitHub page for *your forked repository* (`https://github.com/YOUR_USERNAME/foodsnap-ai`), click the green "**Code**" button.
* Copy the HTTPS or SSH URL.
* Open your terminal or Git client and run:
```bash
git clone https://github.com/YOUR_USERNAME/foodsnap-ai.git
cd foodsnap-ai
```
(Replace `YOUR_USERNAME` with your actual GitHub username.)

### 3. Create a New Branch
* It's crucial to work on a new branch rather than directly on `main` or `master`.
* Choose a descriptive branch name, for example, `feature/your-team-name` or `solution-john-doe`.
* In your terminal, inside the `foodsnap-ai` directory, run:
```bash
git checkout -b feature/your-team-name
```
(e.g., `git checkout -b feature/awesome-coders` or `git checkout -b solution-jane-doe`)
* You are now on your new branch. Verify by running `git branch`.

### 4. Develop Your Project
* Start coding! Add your files, write your logic, and build your FoodSnap AI solution.
* Commit your changes frequently with clear commit messages:
```bash
# Stage all new and modified files
git add .
# Or stage specific files
# git add path/to/your/file.py path/to/another/file.js

# Commit your changes
git commit -m "feat: Implement image upload functionality"
# Example commit types: feat, fix, docs, style, refactor, test, chore
```

### 5. Push Your Branch to Your Fork
* When you're ready to save your progress to *your remote fork on GitHub*, push your branch:
```bash
git push origin feature/your-team-name
```
(Replace `feature/your-team-name` with your actual branch name.)
* If it's the first time pushing this branch, Git might suggest a command like `git push --set-upstream origin feature/your-team-name`. Use that command.

### 6. Submit Your Project (Create a Pull Request)
* Once your project is complete (or at a submittable stage), go to *your* forked repository on GitHub (`https://github.com/YOUR_USERNAME/foodsnap-ai`).
* You should see a prompt saying "`feature/your-team-name` had recent pushes". Click the "**Compare & pull request**" button.
* If you don't see the prompt, go to the "**Pull requests**" tab and click "**New pull request**".
* **Crucially, ensure the settings are:**
* **Base repository:** `WeCode-Community-Dev/foodsnap-ai`
* **Base branch:** `main` (or `master`, whichever is the default for this repository)
* **Head repository:** `YOUR_USERNAME/foodsnap-ai`
* **Compare branch:** `feature/your-team-name` (your development branch)
* Write a clear title and a detailed description for your Pull Request (PR). Include:
* A brief overview of your solution.
* Technologies used.
* How to run/test your project (setup, commands, etc.).
* Any known issues or limitations.
* A link to a live demo if applicable (e.g., Heroku, Netlify, GitHub Pages).
* Screenshots or a short video showcasing your project can be very helpful!
* Click "**Create pull request**".

## What to Include in Your Submission
*(In your project's directory, pushed to your branch and included in the PR)*

* **Source Code:** All the code for your project.
* **`README.md` (Your Project's README):** This is very important! Your project's `README.md` (different from this main hackathon `README.md`) should include:
* Project Title & Team Name/Members (if applicable).
* A brief description of your FoodSnap AI solution.
* What features you implemented.
* Tech stack used.
* **Clear instructions on how to set up and run *your specific project* locally.** This includes dependencies, environment variables, build steps, and run commands.
* Any API keys or environment variables needed (explain how to get them, but **DO NOT COMMIT ACTUAL KEYS** to the repository). Use a `.env.example` file to show what's needed.
* Link to a live demo (if any).
* **(Optional but Recommended)** A short demo video or presentation slides (you can link these in your PR description or in your project's README).

## Judging Criteria (General Pointers)

While specific criteria might be announced, generally projects are evaluated on:

* **Functionality:** Does it work as intended? Does it achieve the core goal of calorie estimation from an image?
* **Accuracy:** How close are the calorie/macro estimations? (We understand this is complex!)
* **Innovation & Creativity:** Did you come up with a unique approach or an interesting feature?
* **Technical Implementation:** Quality of code, choice of technology, and complexity handled.
* **User Experience (UX/UI):** If applicable, is the application easy and pleasant to use?
* **Presentation/Demo:** How well you explain and showcase your project.
* **Adherence to Submission Guidelines:** Including a good project `README.md` and following the Git workflow.
```bash
# Clone the repository
git clone https://github.com/your-username/foodsnap-ai.git
cd foodsnap-ai

# Create and activate a virtual environment
python -m venv venv
source venv\Scripts\activate

---
# Install dependencies
pip install -r requirements.txt

Good luck, innovators! We can't wait to see your FoodSnap AI creations. Remember to have fun, learn, and collaborate!
# Setup .env file

**Happy Hacking!**
The WeCode Community Dev Team
# Run
python main.py
Binary file added __pycache__/config.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/database.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/main.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/models.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/script.cpython-310.pyc
Binary file not shown.
Binary file added __pycache__/script.cpython-311.pyc
Binary file not shown.
22 changes: 22 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os
from dotenv import load_dotenv

load_dotenv()

class Settings:
# database
DATABASE_URL: str = os.getenv("DATABASE_URL")

# Model
MODEL_PATH: str = os.getenv("MODEL_PATH")
MODEL_NAME: str = "RestNet50"

# API
HOST: str = os.getenv("HOST")
PORT: int = int(os.getenv("PORT"))
CORS_ORIGINS: str = os.getenv("CORS_ORIGINS")


settings = Settings()


Binary file added db/__pycache__/database.cpython-311.pyc
Binary file not shown.
Binary file added db/__pycache__/models.cpython-311.pyc
Binary file not shown.
10 changes: 10 additions & 0 deletions db/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from config import settings


engine = create_engine(settings.DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()

21 changes: 21 additions & 0 deletions db/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from sqlalchemy import Column, Integer, String
from db.database import Base

class CalorieNutritionData(Base):
__tablename__ = 'nutrition_data_project'

id = Column(Integer, primary_key=True, index=True)
dish_name = Column(String, index= True)
calories = Column(String)
carbohydrates = Column(String, default=None)
protein = Column(String, default=None)
fats = Column(String, default=None)
free_sugar = Column(String, default=None)
fibre = Column(String, default=None)
sodium = Column(String, default=None)
calcium = Column(String, default=None)
iron = Column(String, default=None)
vitamin_c = Column(String, default=None)
folate = Column(String, default=None)


51 changes: 51 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from fastapi import FastAPI
from contextlib import asynccontextmanager
import uvicorn
from fastapi.middleware.cors import CORSMiddleware

from config import settings
from db import models
from db.database import engine
from routes import foodsnap
from ml_model.model import food_ai_model

# Global model storage
ml_models = {}

@asynccontextmanager
async def lifespan(app: FastAPI):
""" Calling Model only Single time"""

print(f'\nLoading {settings.MODEL_NAME} model')
ml_models["food_classifier"] = food_ai_model() # code to execute when app is loading
# expose models on app state for access within routes
app.state.ml_models = ml_models
yield
print('\nShutting down the model...') # code to execute when app is shutting down

#FastAPI app
app = FastAPI(
title="FoodSnap AI",
description="AI-powered food classification and nutrition analysis",
version="1.0.0",
lifespan=lifespan
)

# Create database tables
models.Base.metadata.create_all(bind=engine)

app.add_middleware(
CORSMiddleware,
allow_origins= settings.CORS_ORIGINS,
allow_credentials=True, # Allow cookies and authorization headers
allow_methods=["*"], # Allow all HTTP methods (GET, POST, PUT, DELETE, etc.)
allow_headers=["*"], # Allow all headers in the request
)

# router
app.include_router(foodsnap.router)

if __name__ == "__main__":
uvicorn.run(app, host=settings.HOST, port=settings.PORT)


Binary file added ml_model/__pycache__/model.cpython-310.pyc
Binary file not shown.
Binary file added ml_model/__pycache__/model.cpython-311.pyc
Binary file not shown.
Loading