This project presents an Image Analysis API that performs object detection on images. The API is built using Flask and is containerized using Docker. The object detection is performed using the ONNX models (FasterRCNN and MobileNetV2), and the API can be deployed in a Kubernetes cluster.
The API exposes the following endpoints:
/get-image
(GET): Fetches an image from a dataset (I get image from the subset of the COCO 2017 validation dataset)./process-image
(POST): Accepts an image and optional processing parameters, and returns the processed image./run-image-object-detection
(POST): Performs object detection on the uploaded image and returns the results, including the image with bounding boxes and detection scores.
- Build the Docker image:
docker build -t image-analysis-api .
- Run the Docker container:
docker run -p 5000:5000 image-analysis-api
- Create the Kubernetes deployment:
kubectl apply -f deployment.yaml (k8s_deployment.yaml)
- Create the Kubernetes service:
kubectl apply -f service.yaml
- Access the API at the service's external IP address.
- Create the Kubernetes scaling:
kubectl apply -f k8s_scaling.yaml
The API is configured to run with 3 replicas in the Kubernetes deployment, providing basic load balancing. Further scaling can be achieved by adjusting the number of replicas based on the load and utilizing horizontal pod autoscaling.
For efficient and cost-effective deployment of the run-image-object-detection
endpoint, the following strategies can be considered:
- Model Optimization: Optimize the ONNX models to reduce computational overhead and increase inference speed.
- Caching: Implement caching to avoid redundant computations and improve response times.
- Load Balancing: Utilize load balancing to distribute traffic evenly across multiple instances of the API.
- Autoscaling: Implement autoscaling to dynamically adjust the number of API instances based on the current load.
The code is structured and organized to maintain readability and scalability, with potential for further documentation and optimizations.
- Flask was chosen for building the API due to its simplicity and ease of setup.
- The ONNX models are deployed using the ONNX Runtime for efficient inference.
- Docker and Kubernetes are used for containerization and orchestration, respectively, to ensure scalability and ease of deployment.
- Unit tests have been created to verify the functionality of each endpoint.
- Integration tests are implemented in the
test_integration.py
script. It tests the/get-image
,/process-image
, and/run-image-object-detection
endpoints to ensure they return a 200 status code, indicating successful integration. The script has been updated to use random images from the dataset instead of hardcoded filenames, making the tests more realistic and effective. - Performance tests are implemented in the
test_performance.py
script using the Locust framework. It defines a WebsiteUser class with tasks to test the/get-image
,/process-image
, and/run-image-object-detection
endpoints under load. This script also now selects random images from the dataset for a more realistic testing scenario. - Error handling tests are implemented in the
test_error_handling.py
script. It tests the/get-image
and/process-image
endpoints with invalid inputs to ensure they return appropriate error status codes (400). These tests help to verify that the API handles errors gracefully.
- Flask for API development
- Docker for containerization
- Kubernetes for orchestration
- ONNX Runtime for model deployment
- Open-source packages and models such as PyTorch for Object Detection model((FasterRCNN and MobileNetV2)
If the system encounters issues, the following steps can be taken to troubleshoot:
- Check the logs for any error messages or anomalies.
- Verify the integrity of the ONNX models and test them separately to ensure they are functioning correctly.
- Validate the API endpoints individually to identify the source of the issue.
- If necessary, rollback to a previous stable version of the API and investigate the cause of the issue.
To scale the API to handle a larger volume of requests, the following strategies can be employed:
- Increase the number of API replicas in the Kubernetes deployment to distribute the load more evenly.
- Implement a queue system to manage the request load and prevent overload of the API.
- Optimize the ONNX models further to reduce inference time and increase throughput.
If given more time, the following enhancements could be considered:
- Implementing additional features such as image segmentation and classification.
- Enhancing the API with a user interface for easier interaction.
- Integrating the API with other services to form a complete image analysis solution.
- Set up a CI/CD pipeline for automated testing and deployment.
- Conduct comprehensive testing and deployment
- Implement machine learning capabilities to enhance object detection accuracy.
Feedback and contributions are welcome. Please submit issues and pull requests for any enhancements or fixes.