|
1 | | -# Pneumonia Detection from X-Ray Images |
| 1 | +# Pneumonia Detection from Chest X-Ray Images |
2 | 2 |
|
3 | 3 |  |
4 | 4 |  |
5 | 5 |  |
6 | 6 |  |
| 7 | + |
7 | 8 |
|
8 | | -A deep learning web application that detects pneumonia from chest X-ray images using a Convolutional Neural Network (CNN). Upload an X-ray and get an instant prediction. |
| 9 | +A deep-learning web app that classifies a chest X-ray as **Normal** or **Pneumonia** |
| 10 | +using a Convolutional Neural Network (CNN). Upload an X-ray and get a prediction with a |
| 11 | +confidence score. |
9 | 12 |
|
10 | | -## About |
| 13 | +> ⚠️ **Educational project — not a medical device.** Do not use for diagnosis or |
| 14 | +> treatment. See [DISCLAIMER.md](DISCLAIMER.md). |
11 | 15 |
|
12 | | -Pneumonia is an infection in the lungs that inflames the air sacs in one or both lungs. It is caused by a bacteria called *Streptococcus pneumoniae* that usually lives in the upper respiratory tract. More than **10 million people in India** are affected by this annually. Doctors use a chest X-ray to confirm the infection and its location. Thus developing an automatic system for detecting the disease can help to treat patients quickly. With powerful Artificial Intelligence algorithms, the medical field has evolved in a very vast manner. |
| 16 | +## About |
13 | 17 |
|
14 | | -**Deep Learning Convolutional Neural Networks (CNN)** is the go-to algorithm when it comes to image classification. CNN models are so powerful that they are already in use for many disease detections. Moreover, CNN models can be used to analyze and find patterns in images. We build a CNN model to analyze the given X-ray image patterns and predict whether the patient is infected or not. |
| 18 | +Pneumonia inflames the air sacs in one or both lungs and is diagnosed clinically, often |
| 19 | +with the help of a chest X-ray. CNNs are a strong fit for image classification, and this |
| 20 | +project trains one to recognise pneumonia patterns in X-rays as a learning exercise in |
| 21 | +applied deep learning. |
15 | 22 |
|
16 | 23 | ## Dataset |
17 | 24 |
|
18 | | -**Chest X-Ray Images (Pneumonia)** — [Kaggle Dataset](https://www.kaggle.com/paultimothymooney/chest-xray-pneumonia) (1.24 GB) |
| 25 | +**Chest X-Ray Images (Pneumonia)** — [Kaggle](https://www.kaggle.com/paultimothymooney/chest-xray-pneumonia) |
| 26 | +(~1.24 GB, ~5,860 images across `train` / `val` / `test`, classes `NORMAL` and `PNEUMONIA`). |
| 27 | + |
| 28 | +## Model |
| 29 | + |
| 30 | +A compact Keras `Sequential` CNN trained on 75×75 grayscale X-rays: |
19 | 31 |
|
20 | | -## Features |
| 32 | +`Conv2D(64) → MaxPooling2D → Flatten → Dense(512) → Dense(256) → Dense(1, sigmoid)` |
21 | 33 |
|
22 | | -- **CNN Model** — Custom convolutional neural network trained on chest X-ray images |
23 | | -- **Web Interface** — Flask-based UI for easy image upload and prediction |
24 | | -- **Instant Results** — Upload an X-ray and get immediate classification |
25 | | -- **Training Notebook** — Jupyter notebook with full model training pipeline |
| 34 | +Binary cross-entropy loss, Adam optimizer. The full pipeline is in |
| 35 | +[`CNN_model.ipynb`](CNN_model.ipynb), which saves the trained weights to `cnn_model.h5`. |
26 | 36 |
|
27 | | -## Tech Stack |
| 37 | +## Tech stack |
28 | 38 |
|
29 | 39 | | Component | Technology | |
30 | 40 | |-----------|-----------| |
31 | | -| **Model** | TensorFlow, Keras (CNN) | |
32 | | -| **Backend** | Flask, Gevent | |
33 | | -| **Frontend** | HTML, CSS, JavaScript | |
34 | | -| **Image Processing** | NumPy, Keras Preprocessing | |
| 41 | +| Model | TensorFlow / Keras (CNN) | |
| 42 | +| Backend | Flask | |
| 43 | +| Frontend | HTML, CSS, JavaScript (jQuery) | |
| 44 | +| Image processing | NumPy, Pillow | |
35 | 45 |
|
36 | | -## Project Structure |
| 46 | +## Project structure |
37 | 47 |
|
38 | 48 | ``` |
39 | | -├── CNN_model.ipynb # Model training notebook |
40 | | -├── app.py # Flask web application |
| 49 | +├── app.py # Flask web application |
| 50 | +├── CNN_model.ipynb # Model training notebook |
| 51 | +├── requirements.txt # Python dependencies |
| 52 | +├── DISCLAIMER.md # Medical disclaimer |
| 53 | +├── tests/ |
| 54 | +│ └── test_smoke.py # Smoke tests (TensorFlow mocked) |
41 | 55 | ├── templates/ |
42 | | -│ └── index.html # Web interface |
| 56 | +│ └── index.html # Web interface |
43 | 57 | └── static/ |
44 | | - ├── css/ |
45 | | - ├── js/ |
46 | | - └── images/ |
| 58 | + ├── css/ ├── js/ └── images/ |
| 59 | +``` |
| 60 | + |
| 61 | +## Getting started |
| 62 | + |
| 63 | +### 1. Install dependencies |
| 64 | + |
| 65 | +```bash |
| 66 | +python -m venv .venv && source .venv/bin/activate # Windows: .venv\Scripts\activate |
| 67 | +pip install -r requirements.txt |
47 | 68 | ``` |
48 | 69 |
|
49 | | -## Getting Started |
| 70 | +### 2. Get a trained model |
50 | 71 |
|
51 | | -### Prerequisites |
| 72 | +Either train it yourself — download the [dataset](https://www.kaggle.com/paultimothymooney/chest-xray-pneumonia) |
| 73 | +and run `CNN_model.ipynb` (set `DATA_DIR` to your local dataset path); it saves |
| 74 | +`cnn_model.h5` — or drop an existing `cnn_model.h5` in the project root. |
| 75 | + |
| 76 | +### 3. Run the web app |
52 | 77 |
|
53 | 78 | ```bash |
54 | | -pip install tensorflow keras flask numpy gevent |
| 79 | +python app.py # serves on http://localhost:5000 |
55 | 80 | ``` |
56 | 81 |
|
57 | | -### Train the Model |
| 82 | +Open `http://localhost:5000`, upload a chest X-ray, and click **Predict**. |
58 | 83 |
|
59 | | -1. Download the [dataset from Kaggle](https://www.kaggle.com/paultimothymooney/chest-xray-pneumonia) |
60 | | -2. Run `CNN_model.ipynb` to train and save the model as `cnn_model.h5` |
| 84 | +## API |
61 | 85 |
|
62 | | -### Run the Web App |
| 86 | +| Route | Method | Description | |
| 87 | +|-------|--------|-------------| |
| 88 | +| `/` | GET | Web UI | |
| 89 | +| `/health` | GET | Liveness probe → `{"status":"ok","model_present":bool}` | |
| 90 | +| `/predict` | POST | multipart `image` file → `{"label","pneumonia","confidence"}` | |
63 | 91 |
|
64 | 92 | ```bash |
65 | | -python app.py |
| 93 | +curl -F "image=@chest_xray.png" http://localhost:5000/predict |
| 94 | +# {"label":"Pneumonia detected","pneumonia":true,"confidence":87.3} |
66 | 95 | ``` |
67 | 96 |
|
68 | | -Navigate to `http://localhost:5000`, upload a chest X-ray image, and get the prediction. |
| 97 | +## Tests |
| 98 | + |
| 99 | +Smoke tests mock TensorFlow, so they run in seconds without the weights or dataset: |
| 100 | + |
| 101 | +```bash |
| 102 | +pip install pytest && pytest -q |
| 103 | +``` |
69 | 104 |
|
70 | | -## Author |
| 105 | +## Limitations |
71 | 106 |
|
72 | | -**Sanjay Santhanam** |
| 107 | +- Trained only on the Kaggle dataset; performance on X-rays from other equipment, |
| 108 | + populations, or preprocessing pipelines is not guaranteed. |
| 109 | +- 75×75 grayscale input discards fine detail a clinical model would retain. |
| 110 | +- Binary output only (Normal vs Pneumonia) — it does not identify pneumonia type, |
| 111 | + severity, or location, and gives no uncertainty calibration. |
| 112 | +- **Not validated for clinical use.** See [DISCLAIMER.md](DISCLAIMER.md). |
73 | 113 |
|
74 | | ---- |
| 114 | +## License |
75 | 115 |
|
76 | | -⭐ Star this repo if you found it useful! |
| 116 | +[MIT](LICENSE) © Sanjay Santhanam |
0 commit comments