rakshittoast/food-aesthetic
0
๐ฝ๏ธ 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
- Clone the repository
git clone <your-repo-url>
cd food-aesthetic-scoring-service- Install dependencies
pip install -r requirements.txt- Start the API server
./start_api.shOr manually:
python api.py- Test the service
python demo.py๐ก API Endpoints
Health Check
GET /healthCheck if the model is loaded and ready.
Score Single Image
POST /scoreScore a single food image. Accepts both file uploads and base64 encoded data.
File Upload:
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:
curl -X POST "http://localhost:8000/score" \
-H "Content-Type: application/json" \
-d '{"image_data": "base64_encoded_string", "image_format": "jpeg"}'Batch Scoring
POST /score-batchScore multiple images at once for efficient processing.
API Information
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
๐ป Usage Examples
Python Client
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
# 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 scriptRunning Tests
# Test the API endpoints
python test_api.py
# Test base64 functionality
python test_base64_api.pyModel Training
The model is pre-trained and ready to use. If you need to retrain:
- Prepare your food image dataset
- Modify the
FoodAestheticsclass inmodel.py - Train using TensorFlow/Keras
- 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
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- 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! ๐๐๐ฐ
