A demo Android application that helps users make informed and healthier dietary choices by scanning food items and receiving detailed nutritional information.
The Nutrition App combines computer vision, natural language processing, and real-time data storage to provide comprehensive nutritional analysis of food items. Users can scan food via images, and the app identifies the food category using TensorFlow Lite, then leverages the OpenAI API to provide detailed nutritional information including calorie count, macronutrient breakdown, and other nutrition facts.
- Image-Based Food Scanning: Scan food items via camera or image gallery
- AI-Powered Food Identification: Uses TensorFlow Lite to identify food categories
- Intelligent Nutritional Analysis: Leverages OpenAI GPT API to analyze and interpret food descriptions
- Comprehensive Nutrition Facts: Displays calorie count, macronutrient breakdown (carbs, proteins, fats), vitamins, and minerals
- Scan History Tracking: Track scan history for each user
- User Authentication: Secure authentication with Firebase Auth
- Cloud Data Storage: Store and retrieve data using Firebase Firestore
- Frontend: Flutter (Dart)
- Backend/API: Flask (Python), OpenAI GPT API
- ML Model: TensorFlow Lite
- Database & Authentication: Firebase (Firestore and Firebase Auth)
- Platform: Android
nutrition-app/
├── app.py # Flask backend API server
├── requirements.txt # Python dependencies
├── .env # Environment variables (not tracked)
├── .gitignore # Git ignore rules
└── README.md # This file
Note: The Flutter frontend code is maintained in a separate repository.
The backend is a Flask REST API that provides nutritional information for food items using the OpenAI GPT API.
- Python 3.8 or higher
- pip (Python package manager)
- OpenAI API key
-
Clone the repository:
git clone https://github.com/frxyoz/nutrition-app cd nutrition-app -
Create a virtual environment (recommended):
python -m venv venv # On Windows venv\Scripts\activate # On macOS/Linux source venv/bin/activate
-
Install dependencies:
pip install -r requirements.txt
-
Create a
.envfile in the root directory:touch .env
-
Add your OpenAI API key to the
.envfile:OPENAI_API_KEY=your_openai_api_key_here⚠️ Important: Never commit your.envfile to version control. It's already included in.gitignore.
-
Start the Flask development server:
python app.py
The server will start on
http://0.0.0.0:5000with debug mode enabled. -
For production deployment, use gunicorn:
gunicorn -w 4 -b 0.0.0.0:5000 app:app
GET /test/<message>
A simple test endpoint to verify the server is running.
Parameters:
message(string, path parameter): Any test message
Example Request:
curl http://localhost:5000/test/helloExample Response:
{
"message": "hello"
}GET /convo/<message>
Retrieves comprehensive nutritional information for a given food item using OpenAI GPT.
Parameters:
message(string, path parameter): Name of the food item
Example Request:
curl http://localhost:5000/convo/appleExample Response:
{
"FoodName": "Apple",
"Calories": "95",
"RecommendedDailyIntake": "200",
"EatFrequency": "2",
"Description": "Apples are a nutritious fruit rich in fiber, vitamin C, and various antioxidants. They support heart health, aid digestion, and may help with weight management.",
"Nutrients": {
"Carbohydrates": "25",
"Proteins": "0.5",
"Fats": "0.3",
"Vitamins": ["Vitamin C", "Vitamin A", "Vitamin K"],
"Minerals": ["Potassium", "Calcium", "Magnesium"]
}
}Response Fields:
FoodName: Name of the food itemCalories: Estimated calories per serving (whole number)RecommendedDailyIntake: Recommended daily intake in grams (whole number)EatFrequency: Suggested frequency of consumption per day (whole number)Description: Brief description including nutritional benefitsNutrients: Detailed breakdown of macronutrients, vitamins, and minerals
Error Response:
{
"error": "Error description"
}-
Using curl:
# Test endpoint curl http://localhost:5000/test/working # Get nutrition info for an apple curl http://localhost:5000/convo/apple # Get nutrition info for chicken breast curl http://localhost:5000/convo/chicken%20breast
-
Using a web browser:
- Navigate to
http://localhost:5000/test/hello - Navigate to
http://localhost:5000/convo/banana
- Navigate to
-
Using Python:
import requests response = requests.get('http://localhost:5000/convo/orange') print(response.json())
The frontend is a Flutter-based Android application that provides the user interface for scanning and viewing nutritional information.
- Flutter SDK (version 3.0.0 or higher recommended)
- Android Studio or VS Code with Flutter extensions
- Android SDK and emulator or physical Android device
- Firebase project with Firestore and Authentication enabled
-
Install Flutter: Follow the official Flutter installation guide for your operating system:
-
Verify Flutter installation:
flutter doctor
-
Clone the frontend repository (if separate):
Note: The Flutter frontend code is maintained in a separate repository. Contact the project maintainer or check the project organization for the frontend repository URL.
git clone <frontend-repo-url> cd <frontend-directory>
-
Install dependencies:
flutter pub get
-
Firebase Setup:
- Create a Firebase project at https://console.firebase.google.com
- Add an Android app to your Firebase project
- Download the
google-services.jsonfile - Place it in the
android/app/directory - Enable Firestore and Firebase Authentication in the Firebase console
-
TensorFlow Lite Model:
- Place your trained TensorFlow Lite model file in the
assets/directory - Update the model path in the app configuration
- Place your trained TensorFlow Lite model file in the
-
Backend API Configuration:
- Update the API base URL in the app configuration to point to your Flask backend
- Example:
http://your-server-ip:5000orhttps://your-domain.com
-
Connect an Android device or start an emulator:
flutter devices
-
Run the app:
flutter run
-
For release build:
flutter build apk --release
The APK will be generated at
build/app/outputs/flutter-apk/app-release.apk
- Camera Integration: Take photos of food items directly from the app
- Gallery Selection: Choose existing photos from the device gallery
- Real-time Analysis: Get instant nutritional information after scanning
- History View: Browse through previously scanned food items
- User Profile: Manage user account and preferences
- Offline Support: Cache nutritional data for offline access
Problem: ModuleNotFoundError: No module named 'flask'
- Solution: Ensure you've activated the virtual environment and installed dependencies:
source venv/bin/activate # or venv\Scripts\activate on Windows pip install -r requirements.txt
Problem: openai.OpenAIError: The api_key client option must be set
- Solution: Verify your
.envfile exists and contains the correct OpenAI API key:OPENAI_API_KEY=sk-...
Problem: CORS errors when accessing from frontend
- Solution: The Flask app already has CORS enabled via
Flask-Cors. Ensure the frontend is making requests to the correct backend URL.
Problem: Port 5000 already in use
- Solution: Either stop the process using port 5000 (AirDrop service on MacOS systems) or modify
app.pyto use a different port:# In app.py, change the last line from: app.run(host='0.0.0.0', port=5000, debug=True) # To: app.run(host='0.0.0.0', port=8080, debug=True)
Problem: google-services.json not found
- Solution: Download the configuration file from your Firebase project and place it in
android/app/
Problem: TensorFlow Lite model fails to load
- Solution: Verify the model file exists in the
assets/directory and the path is correctly specified in the app configuration
Problem: Network request failed
- Solution:
- Check that the Flask backend is running
- Verify the API base URL is correctly configured in the frontend
- For Android emulator, use
http://10.0.2.2:5000instead oflocalhost - For physical device, ensure both device and server are on the same network
This is a demo project developed for educational purposes. Feel free to fork and modify for your own learning.
This project is for educational purposes only.
Developed by Olric Zeng, Coding Minds intern 2024.