Saikatgh/food-calorie-estimation
0
๐ฝ๏ธ Food Calorie Estimation
An AI-powered food calorie estimation system that combines YOLOv8n-seg for food detection and segmentation with advanced depth estimation to calculate accurate calorie content.
๐ Features
- Food Detection & Segmentation: Uses YOLOv8n-seg to detect and segment food items
- Volume Estimation: Advanced depth estimation for accurate volume calculation
- Calorie Calculation: Estimates calories based on food type, volume, and nutritional density
- Flutter API: RESTful endpoints optimized for Flutter mobile apps
- Web Interface: Interactive Gradio interface for testing
๐ฅ Supported Food Items
The system currently supports South Asian/Bengali cuisine:
- Beef curry
- Biriyani
- Chicken curry
- Egg
- Egg curry
- Eggplants
- Fish
- Khichuri
- Potato mash
- Rice
๐ฑ Flutter Integration
API Endpoints
1. Estimate Calories
POST /api/estimate_caloriesRequest Format:
{
"image_base64": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ...",
"confidence": 0.5
}Response Format:
{
"success": true,
"food_items_detected": 2,
"nutrition_summary": {
"total_calories": 450.2,
"total_protein_g": 25.1,
"total_carbohydrates_g": 45.3,
"total_fat_g": 18.7,
"total_weight_g": 320.5,
"food_count": 2
},
"detailed_food_items": [
{
"class_name": "Rice",
"confidence": 0.89,
"calories": 195.0,
"volume_ml": 150.0,
"weight_g": 112.5
}
],
"api_version": "1.0",
"timestamp": 1703123456
}2. Health Check
GET /api/health3. Supported Foods
GET /api/supported_foodsFlutter Code Example
import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;
class CalorieEstimationAPI {
static const String baseUrl = 'https://your-space-name.hf.space';
static Future<Map<String, dynamic>> estimateCalories(File imageFile) async {
try {
// Convert image to base64
List<int> imageBytes = await imageFile.readAsBytes();
String base64Image = base64Encode(imageBytes);
// Prepare request
final response = await http.post(
Uri.parse('$baseUrl/api/estimate_calories'),
headers: {
'Content-Type': 'application/json',
},
body: jsonEncode({
'image_base64': 'data:image/jpeg;base64,$base64Image',
'confidence': 0.5,
}),
);
if (response.statusCode == 200) {
return jsonDecode(response.body);
} else {
throw Exception('Failed to estimate calories: ${response.statusCode}');
}
} catch (e) {
throw Exception('Error: $e');
}
}
}๐ ๏ธ Deployment Instructions
1. Prepare Files for Hugging Face Spaces
- Copy these files to your Hugging Face Space:
app.py(main application)requirements.txt(dependencies)README.md(this file)- All your Python modules
labels.txt
2. Create Hugging Face Space
- Go to Hugging Face Spaces
- Click "Create new Space"
- Choose:
- SDK: Gradio
- Hardware: CPU Basic (or GPU if needed)
- Visibility: Public
3. Upload Files
Upload all the files to your space repository.
4. Configure Space
The space will automatically build and deploy. You can access:
- Web Interface:
https://your-username-space-name.hf.space - API Endpoints:
https://your-username-space-name.hf.space/api/...
๐ง Local Development
# Install dependencies
pip install -r requirements.txt
# Run the app
python app.py๐ System Architecture
Flutter App โ HTTP Request โ Hugging Face Space โ YOLOv8n-seg โ Depth Estimation โ Calorie Calculation โ JSON Response โ Flutter App๐ฏ Usage Examples
Web Interface
- Upload a food image
- Adjust confidence threshold
- Click "Analyze Food"
- View results and visualization
Flutter Integration
- Capture/select food image
- Send to API endpoint
- Parse JSON response
- Display nutrition information
๐ค Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request
๐ License
This project is licensed under the MIT License.
๐ Acknowledgments
- Ultralytics YOLOv8 for object detection
- Gradio for the web interface
- Hugging Face Spaces for hosting
