This project provides a REST API for predicting pneumonia in chest X-ray images using a pre-trained model built with FastAI.
This project uses FastAI and Django to create an API that accepts chest X-ray images and returns a prediction of whether the image indicates pneumonia. The model is exported from FastAI and loaded into a Django REST Framework backend to serve predictions.
Follow these steps to set up the project locally.
git clone https://github.com/yourusername/pneumonia-detection-api.git
cd pneumonia-detection-apipython -m venv venv
source venv/bin/activate # For Windows use `venv\Scripts\activate`pip install -r requirements.txtPlace your exported FastAI model (e.g., exported_model.pkl) in the models/ directory.
python manage.py migratepython manage.py runserverThe API will be available at http://127.0.0.1:8000.
You can use the API by sending a POST request to the /predict/ endpoint with an image file of a chest X-ray.
Example request:
curl -X POST -F "image=@path/to/your/xray_image.jpg" http://127.0.0.1:8000/api/predict/The response will contain the predicted label (Pneumonia or Normal) and the confidence score.
- Method: POST
- Description: Accepts an image of a chest X-ray and returns the prediction (Pneumonia/Normal) with confidence score.
- Request:
image: The chest X-ray image to analyze (JPEG/PNG format).
- Response:
{ "prediction": "Pneumonia", "confidence": 0.95 }
This project uses a pre-trained model saved as exported_model.pkl using FastAI. The model was trained to detect pneumonia from chest X-ray images.
To train a similar model, follow these steps:
- Collect and preprocess the chest X-ray dataset.
- Use FastAI to build and train the model.
- Export the model using the
learn.export()method.
- Python 3.7+
- Django 3.0+
- FastAI
- PyTorch
- Django REST Framework
Install dependencies with:
pip install -r requirements.txtThis project is licensed under the MIT License - see the LICENSE file for details.