This is a FastAPI-based image processing service that allows users to upload images, perform various transformations, and store the processed images in Firebase Storage. The service provides endpoints to upload images, retrieve image details, and query transformation frequencies.
- Setup
- Running the Service
- Design Decisions
- Endpoints
- Example Request for Upload Image
- Testing
- Firebase Setup
-
Clone the Repository
git clone https://github.com/yourusername/image-processing-service.git cd image-processing-service -
Create a .env File Create a
.envfile in the project directory and add the following variables:DATABASE_URL=your_database_url FIREBASE_CREDENTIALS=path/to/cred.json FIREBASE_STORAGE_BUCKET=image-processing-service-ce70c.appspot.com -
Install Dependencies If you are using Docker, you can skip this step as the dependencies will be installed in the Docker container. Otherwise:
pip install -r requirements.txt
-
Build the Docker Image
docker-compose build
-
Run the Service
docker-compose up
The service will be available at http://localhost:8000.
-
Start the application:
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
Replace
main:appwith the appropriate module and app name if necessary.
- Framework Choice: FastAPI was chosen for its performance and ease of use with asynchronous programming.
- Stateless Service: The service is designed to be stateless, allowing for easy scaling and load balancing.
- Database: SQLAlchemy is used as the ORM for database interactions, providing a clear structure for data models.
- Image Processing: The image processing functionality is modularized in a separate utility function to keep the code clean and maintainable.
- Testing: Unit tests are included to ensure the functionality of key endpoints and business logic.
| Method | Endpoint | Description |
|---|---|---|
| POST | /upload-image/ | Upload an image and specify transformation parameters (resize, grayscale, rotation). |
| GET | /get-image/{image_id} | Retrieve details of a processed image by its ID, including the public URL and transformations. |
| GET | /most-frequent-transformation | Get the most frequently applied transformation across all images. |
| GET | /latest-transformations | Retrieve the latest transformations applied to images. |
| GET | /ranking-images | Get a ranking of images based on the number of transformations applied to each. |
To upload an image with various transformations, you can use the following curl command:
curl -X POST "http://127.0.0.1:8000/upload-image/?width=100&height=100&grayscale=true&rotation=90" -F "[email protected]"- Method: POST
- URL:
http://127.0.0.1:8000/upload-image/ - Query Parameters:
width=100: Resize the image to a width of 100 pixels.height=100: Resize the image to a height of 100 pixels.grayscale=true: Convert the image to grayscale.rotation=90: Rotate the image by 90 degrees.
- File:
test.jpeg: The image file to be uploaded and processed.
On a successful upload, the response would look like this:
{
"image_id": "123e4567-e89b-12d3-a456-426614174000",
"public_url": "https://storage.googleapis.com/image-processing-service-ce70c.appspot.com/images/123e4567-e89b-12d3-a456-426614174000.png",
"message": "Upload and transformation successful!"
}To run the unit tests for this service, you can use pytest.
To execute the tests, run the following command in the root directory of the project:
pytest test/test_main.pyThis command will discover and run all the test cases defined in the test/test_main.py file. You should see output indicating which tests passed or failed.
Ensure that your database and any required services are running if your tests depend on them.
- Go to the Firebase Console.
- Create a new project (or use an existing one).
- Enable Firebase Storage in the project settings.
- Go to the Project Settings > Service accounts.
- Click on Generate new private key to download the JSON file.
- Place this JSON file in your project directory and update the path in your
.envfile.
Ensure that your Firebase Storage rules allow read and write access for testing purposes:
service firebase.storage {
match /b/{bucket}/o {
match /images/{imageId} {
allow read, write: if request.auth != null; // Adjust according to your security needs
}
}
}
- Adjust the security rules based on your application's authentication mechanism before deploying to production.
This image processing service provides a robust and flexible way to handle image uploads and transformations, leveraging Firebase for storage. Follow the setup instructions to get started with your own instance of the service.