CoolFace
Apppublic

Saikatgh/food-calorie-estimation

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
App README

๐Ÿฝ๏ธ 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_calories

Request Format:

json
{
  "image_base64": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ...",
  "confidence": 0.5
}

Response Format:

json
{
  "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/health
3. Supported Foods
GET /api/supported_foods

Flutter Code Example

dart
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

  1. 1.Copy these files to your Hugging Face Space:
  2. 2.app.py (main application)
  3. 3.requirements.txt (dependencies)
  4. 4.README.md (this file)
  5. 5.All your Python modules
  6. 6.labels.txt

2. Create Hugging Face Space

  1. 1.Go to Hugging Face Spaces
  2. 2.Click "Create new Space"
  3. 3.Choose:
  4. 4.SDK: Gradio
  5. 5.Hardware: CPU Basic (or GPU if needed)
  6. 6.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

bash
# 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

  1. 1.Upload a food image
  2. 2.Adjust confidence threshold
  3. 3.Click "Analyze Food"
  4. 4.View results and visualization

Flutter Integration

  1. 1.Capture/select food image
  2. 2.Send to API endpoint
  3. 3.Parse JSON response
  4. 4.Display nutrition information

๐Ÿค Contributing

  1. 1.Fork the repository
  2. 2.Create a feature branch
  3. 3.Make your changes
  4. 4.Submit a pull request

๐Ÿ“„ License

This project is licensed under the MIT License.

๐Ÿ™ Acknowledgments