CoolFace
Apppublic

rakshittoast/food-aesthetic

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

๐Ÿฝ๏ธ Food Aesthetic Scoring Service

A machine learning service that automatically scores food images based on their aesthetic quality using a pre-trained MobileNet model.

๐ŸŒŸ Features

  • โ€”AI-Powered Scoring: Uses a fine-tuned MobileNet model to evaluate food aesthetics
  • โ€”RESTful API: FastAPI-based service with multiple endpoints
  • โ€”Batch Processing: Score multiple images at once
  • โ€”Multiple Input Formats: Support for file uploads and base64 encoded images
  • โ€”Real-time Scoring: Get aesthetic scores in milliseconds
  • โ€”CORS Enabled: Ready for web applications

๐Ÿš€ Quick Start

Prerequisites

  • โ€”Python 3.8+
  • โ€”TensorFlow 2.9
  • โ€”4GB+ RAM recommended

Installation

  1. 1.Clone the repository
bash
   git clone <your-repo-url>
   cd food-aesthetic-scoring-service
  1. 1.Install dependencies
bash
   pip install -r requirements.txt
  1. 1.Start the API server
bash
   ./start_api.sh

Or manually:

bash
   python api.py
  1. 1.Test the service
bash
   python demo.py

๐Ÿ“ก API Endpoints

Health Check

http
GET /health

Check if the model is loaded and ready.

Score Single Image

http
POST /score

Score a single food image. Accepts both file uploads and base64 encoded data.

File Upload:

bash
curl -X POST "http://localhost:8000/score" \
     -H "accept: application/json" \
     -H "Content-Type: multipart/form-data" \
     -F "file=@your_food_image.jpg"

Base64 Data:

bash
curl -X POST "http://localhost:8000/score" \
     -H "Content-Type: application/json" \
     -d '{"image_data": "base64_encoded_string", "image_format": "jpeg"}'

Batch Scoring

http
POST /score-batch

Score multiple images at once for efficient processing.

API Information

http
GET /

Get API version and available endpoints.

๐Ÿง  Model Details

The service uses a MobileNet-based neural network that has been specifically trained on food images to predict aesthetic quality. The model:

  • โ€”Takes 224x224 RGB images as input
  • โ€”Outputs scores between 0 and 1 (higher = more aesthetic)
  • โ€”Uses temperature scaling for calibrated predictions
  • โ€”Processes images with aspect ratio preservation

Score Interpretation

Score RangeQuality Level
0.0 - 0.2Low
0.2 - 0.4Below Average
0.4 - 0.6Average
0.6 - 0.8Above Average
0.8 - 1.0High

๐Ÿ’ป Usage Examples

Python Client

python
import requests
from PIL import Image
import base64
import io

# Initialize client
base_url = "http://localhost:8000"

# Score an image file
with open("food_image.jpg", "rb") as f:
    files = {"file": ("image.jpg", f, "image/jpeg")}
    response = requests.post(f"{base_url}/score", files=files)
    
if response.status_code == 200:
    score = response.json()["aesthetic_score"]
    print(f"Aesthetic Score: {score:.4f}")

Base64 Encoding

python
# Convert image to base64
with open("food_image.jpg", "rb") as f:
    image_bytes = f.read()
    base64_data = base64.b64encode(image_bytes).decode()

# Send to API
data = {
    "image_data": base64_data,
    "image_format": "jpeg"
}
response = requests.post(f"{base_url}/score", json=data)

๐Ÿ› ๏ธ Development

Project Structure

food-aesthetic-scoring-service/
โ”œโ”€โ”€ api.py                 # FastAPI server
โ”œโ”€โ”€ food_aesthetics/       # Core ML model
โ”‚   โ”œโ”€โ”€ model.py          # FoodAesthetics class
โ”‚   โ””โ”€โ”€ trained_weights.h5 # Pre-trained model weights
โ”œโ”€โ”€ demo.py              # Usage examples
โ”œโ”€โ”€ requirements.txt      # Python dependencies
โ””โ”€โ”€ start_api.sh         # Server startup script

Running Tests

bash
# Test the API endpoints
python test_api.py

# Test base64 functionality
python test_base64_api.py

Model Training

The model is pre-trained and ready to use. If you need to retrain:

  1. 1.Prepare your food image dataset
  2. 2.Modify the FoodAesthetics class in model.py
  3. 3.Train using TensorFlow/Keras
  4. 4.Save weights to trained_weights.h5

๐Ÿ”ง Configuration

Environment Variables

  • โ€”PORT: API server port (default: 8000)
  • โ€”HOST: API server host (default: 0.0.0.0)

Model Parameters

  • โ€”Input Size: 224x224 pixels
  • โ€”Batch Size: 1 (configurable in model.py)
  • โ€”Temperature: 1.537 (for score calibration)

๐Ÿ“Š Performance

  • โ€”Inference Time: ~50-100ms per image
  • โ€”Memory Usage: ~2-3GB RAM
  • โ€”Concurrent Requests: Supports multiple simultaneous users
  • โ€”Model Loading: ~5-10 seconds on first startup

๐Ÿค Contributing

  1. 1.Fork the repository
  2. 2.Create a feature branch
  3. 3.Make your changes
  4. 4.Add tests if applicable
  5. 5.Submit a pull request

๐Ÿ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

  • โ€”Built with TensorFlow and FastAPI
  • โ€”Model architecture based on MobileNet
  • โ€”Food aesthetics dataset for training

๐Ÿ“ž Support

For questions or issues:

  • โ€”Open an issue on GitHub
  • โ€”Check the API documentation
  • โ€”Review the demo scripts for usage examples

Happy Food Aesthetic Scoring! ๐Ÿ•๐Ÿœ๐Ÿฐ