A comprehensive full-stack application for comparing images using both computer vision metrics and OpenAI's multimodal LLM for semantic analysis. The platform consists of a FastAPI backend service and an intuitive Streamlit web interface.
- Computer Vision Metrics: MSE, SSIM, Histogram Correlation, Histogram Chi-Squared
- LLM Semantic Analysis: OpenAI GPT-4 Vision for conceptual similarity scoring
- Async Processing: Fast, non-blocking image comparison
- RESTful API: Clean, documented endpoints with automatic OpenAPI docs
- Health Monitoring: Built-in health checks and configuration endpoints
- Drag & Drop Upload: Easy image uploading with preview
- Real-time Progress: Visual progress tracking during analysis
- Interactive Dashboard: Clean, responsive UI for viewing results
- Detailed Metrics: Visual representation of similarity scores
- Error Handling: User-friendly error messages and troubleshooting
- Python 3.8+
- OpenAI API key (optional, for LLM features)
- 2GB+ RAM recommended for image processing
git clone https://github.com/yourusername/image-similarity-platform.git
cd image-similarity-platform
mkdir -p backend frontend scripts tests
# Move your files to appropriate directories
mv main.py backend/
mv image_utils.py backend/
mv app.py frontend/
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txt
cp .env.example .env
Edit .env
and add your OpenAI API key:
OPENAI_API_KEY=your_openai_api_key_here
Terminal 1 - Backend:
cd backend
python main.py
Terminal 2 - Frontend:
cd frontend
streamlit run app.py
chmod +x scripts/*.sh
./scripts/start_both.sh
docker-compose up -d
Access the applications:
- Web Interface: http://localhost:8501
- API Documentation: http://localhost:8000/docs
- API Endpoints: http://localhost:8000
- Open your browser to http://localhost:8501
- Upload images using the drag-and-drop interface:
- Upload a source image on the left
- Upload a target image on the right
- Click "๐ฌ Analyze Similarity" to start the comparison
- View results in an organized dashboard:
- Computer Vision metrics with explanations
- LLM semantic analysis with conceptual scoring
- Progress tracking during analysis
- Visual Preview: See your uploaded images before analysis
- Progress Tracking: Real-time updates during processing
- Error Handling: Clear error messages if something goes wrong
- Responsive Design: Works on desktop and mobile devices
POST /compare-images/
Upload two images for similarity comparison.
Parameters:
source_image_file
(file): First image to comparetarget_image_file
(file): Second image to compareinclude_llm
(bool, optional): Enable LLM semantic comparison (default: true)llm_timeout
(int, optional): Timeout for LLM API call in seconds (10-300, default: 60)
Response:
{
"request_info": {
"source_filename": "image1.jpg",
"target_filename": "image2.jpg",
"include_llm": true,
"llm_timeout": 60
},
"cv_scores": {
"mse": 1250.5,
"ssim": 0.85,
"histogram_correlation": 0.92,
"histogram_chi_squared": 15.3
},
"llm_comparison": {
"status": "completed",
"description": "Both images show similar outdoor landscapes...",
"conceptual_score_str": "High similarity (8/10)",
"conceptual_score_value": 8,
"model_used": "gpt-4-vision-preview"
},
"summary": {
"cv_scores_available": true,
"llm_comparison_completed": true,
"total_metrics": 4
}
}
GET /health
Check API health and configuration status.
GET /config
Get current API configuration settings.
POST /test-openai/
Test OpenAI API connectivity without processing images.
GET /
Get API information and available endpoints.
- MSE (Mean Squared Error): Lower values indicate higher similarity (0 = identical)
- SSIM (Structural Similarity Index): Higher values indicate higher similarity (1 = identical)
- Histogram Correlation: Higher values indicate higher similarity (1 = identical)
- Histogram Chi-Squared: Lower values indicate higher similarity (0 = identical)
- Conceptual Score: 1-10 scale where 10 represents highest semantic similarity
- Description: Natural language explanation of the comparison
# Backend only
docker build -f Dockerfile.backend -t similarity-backend .
docker run -p 8000:8000 -e OPENAI_API_KEY=your_key_here similarity-backend
# Frontend only
docker build -f Dockerfile.frontend -t similarity-frontend .
docker run -p 8501:8501 similarity-frontend
# Full stack
docker-compose up -d
docker-compose up -d
import requests
url = "http://localhost:8000/compare-images/"
with open("image1.jpg", "rb") as f1, open("image2.jpg", "rb") as f2:
files = {
"source_image_file": f1,
"target_image_file": f2
}
params = {
"include_llm": True,
"llm_timeout": 60
}
response = requests.post(url, files=files, params=params)
result = response.json()
print(f"SSIM Score: {result['cv_scores']['ssim']}")
print(f"LLM Score: {result['llm_comparison']['conceptual_score_value']}")
curl -X POST "http://localhost:8000/compare-images/" \
-F "[email protected]" \
-F "[email protected]" \
-G -d "include_llm=true" -d "llm_timeout=60"
const formData = new FormData();
formData.append('source_image_file', sourceFile);
formData.append('target_image_file', targetFile);
const response = await fetch('http://localhost:8000/compare-images/?include_llm=true', {
method: 'POST',
body: formData
});
const result = await response.json();
console.log('Similarity scores:', result.cv_scores);
console.log('LLM analysis:', result.llm_comparison);
Variable | Description | Default |
---|---|---|
OPENAI_API_KEY |
OpenAI API key for LLM features | None |
HOST |
Server host address | 0.0.0.0 |
PORT |
Server port | 8000 |
LOG_LEVEL |
Logging level | info |
UPLOADS_DIR |
Temporary upload directory | uploads |
- JPEG/JPG
- PNG
- BMP
- TIFF
- WebP
- All formats supported by OpenCV
Run the test suite:
pytest tests/
Run with coverage:
pytest --cov=. tests/
- Typical response time: 1-3 seconds (CV only), 5-15 seconds (with LLM)
- Memory usage: ~200MB base + image size
- Concurrent requests: Supports async processing
- Rate limits: Dependent on OpenAI API limits
- Temporary files are automatically cleaned up
- No persistent storage of uploaded images
- CORS is enabled for all origins (configure for production)
- API key is required for LLM features
- Input validation for file types and sizes
-
OpenAI API Key Not Found
Solution: Set OPENAI_API_KEY environment variable
-
Image Loading Errors
Solution: Ensure images are valid and in supported formats
-
Timeout Errors
Solution: Increase llm_timeout parameter or check network connectivity
-
Memory Issues
Solution: Resize large images before upload, increase server memory
Enable debug logging:
LOG_LEVEL=debug python main.py
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Create an issue on GitHub for bug reports
- Check the
/health
endpoint for system status - Review logs for detailed error information
- Consult OpenAI documentation for LLM-related issues
- Batch image processing
- Additional CV metrics (PSNR, LPIPS)
- Support for other LLM providers
- Image preprocessing options
- Results caching
- API rate limiting
- Database integration for history
Made with โค๏ธ using FastAPI and OpenAI