A FastAPI-based microservice that compares two base64-encoded images using Google's Gemini (Generative AI) and returns a similarity score along with an explanation of differences and similarities.
- Accepts two images in base64 format.
- Uses Google Gemini (
gemini-1.5-pro
) to analyze and compare the images. - Returns:
- Similarity Score (0 to 100)
- Human-readable explanation of the differences and similarities.
- Clone the Repository
git clone https://github.com/raks-javac/image-comparison-api.git
cd image-comparison-api
- Create a virtual environment and install dependencies
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txt
- Set up Environment Variables
Create a .env
file in the root directory and add your Google API key:
GOOGLE_API_KEY=your_google_api_key_here
- Run the App
uvicorn main:app --reload
The app will be available at: http://localhost:8000
Check if the API is running.
Response:
{
"message": "Image Comparison API is running"
}
Compares two base64-encoded images.
Request Body:
{
"image1": "data:image/png;base64,iVBORw0K...",
"image2": "data:image/png;base64,iVBORw0K..."
}
Response:
{
"similarity_score": 87.5,
"explanation": "Both images share similar structure and content, but differ slightly in color and background elements."
}
.
├── main.py # Main FastAPI app
├── models/
│ └── image_models.py # Pydantic request/response models
├── .env # Environment variables
└── requirements.txt # Python dependencies
- Python 3.8+
- Valid Google API key with access to Gemini 1.5 Pro
- Dependencies listed in
requirements.txt
:- fastapi
- python-dotenv
- google-generativeai
- pillow
- uvicorn
Contributions are welcome! Please fork the repo and submit a pull request.
This project is licensed under the MIT License.
Your Name
GitHub: @Raks-Javac