Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

CoughSense logo

CoughSense

AI-Based Cough Acoustic Analysis for Early Respiratory Disease Screening

Python ML DL Accuracy API Live Demo Status

Team Auscultate β€” Aryan Verma (B.Tech AI) & Arfa Alam (B.Tech Civil Engineering)

πŸ”— Live Demo Β Β·Β  Screening Tool Β Β·Β  Analytics Dashboard

Note: hosted on Render's free tier β€” the app may take 30–60s to wake up on first load after inactivity.


Table of Contents


Overview

CoughSense is a machine-learning and deep-learning system that screens for likely respiratory conditions (such as COVID-19) from a short cough recording. It needs nothing more than a smartphone microphone β€” no lab, no clinic visit, no cost. It is a screening aid, not a diagnosis: it answers the narrow, high-value question "should this person get tested?"

The project deliberately implements both classical ML and deep learning side by side and compares them honestly β€” a strong demonstration of how the two approaches behave on small, real clinical data.


Live Demo

The full app β€” custom frontend + FastAPI backend β€” is deployed together on Render:

Page What it does
Home Project overview: problem, approach, results, team
Screening Tool Upload or record a cough β†’ live prediction from all 3 models
Dashboard Dataset composition, per-fold CV results, confusion matrices, feature importance

The deployed version is built from the self-contained deploy/ folder (its own copy of serve.py, frontend/, models/, and a minimal src/), so it can be redeployed anywhere that runs a Dockerfile β€” Render, Fly.io, HuggingFace Docker Spaces, etc.

Render's free tier spins the service down after inactivity β€” the first request after a while may take 30–60 seconds to wake up. Subsequent requests are fast.


The Problem

Access to respiratory screening is limited in low-resource settings: clinics are far, tests cost money, and results take time. Yet the cough itself carries information β€” clinicians have long used cough character (wet vs. dry, productive vs. barking) as a diagnostic cue. If a machine can learn those same acoustic patterns, screening becomes as accessible as a phone call.


Our Solution

A pipeline that takes a cough clip and returns a prediction from three models, with confidence scores and a clear medical disclaimer, exposed through both a REST API and a browser interface:

  1. Feature extraction β€” MFCCs, spectral features, zero-crossing rate, RMS energy (for classical ML); log-mel spectrograms (for the CNN).
  2. Three models β€” Random Forest, XGBoost, and a compact CNN β€” trained and cross-validated.
  3. Explainability β€” SHAP analysis showing why the model predicts what it does.
  4. Next-steps guidance β€” a result-specific panel with general public-health guidance (get a confirmatory test, isolate, monitor symptoms, when to seek emergency care) β€” always deferring to a real test and a healthcare professional, never prescribing treatment.
  5. Web app β€” a live screening tool + an analytics dashboard, deployed together (frontend + backend in one container).

Key Value Points

  • Real-world impact β€” cheap, instant respiratory pre-screening for places with no easy lab access.
  • Underexplored modality β€” audio biomarkers get far less attention than image or text ML, despite solid clinical grounding in cough acoustics.
  • Honest science β€” every number is 5-fold cross-validated and reported with its standard deviation, never a single lucky split.
  • Explainable, not a black box β€” SHAP shows which acoustic features drive each decision, and they match clinical intuition (cough timbre).
  • ML vs DL comparison β€” demonstrates a real, well-documented tradeoff: on small clinical data, gradient-boosted trees beat deep learning.
  • A genuine data-quality experiment β€” we tested scaling the data and learned why quality beats quantity in medical ML (details below).
  • Actionable, responsible guidance β€” a result triggers general public-health next steps (get tested, isolate, monitor, when to seek emergency care), not a diagnosis or treatment prescription.
  • Deployable β€” runs fully locally on free tooling; FastAPI backend + static frontend.

How It Works

 cough.mp3 ──► feature extraction ──►  β”Œβ”€β”€ Random Forest ─┐
                (MFCC / spectral)      β”‚                  β”‚
              ──► mel-spectrogram ──►  β”œβ”€β”€ XGBoost  ─────────► prediction + confidence
                                       β”‚                  β”‚      (+ SHAP explanation)
                                       └── CNN β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
  1. Audio is resampled to 22.05 kHz and standardized to a fixed length.
  2. Two representations are computed: a 37-value statistical feature vector (for the tree models) and a 128Γ—87 log-mel spectrogram (for the CNN).
  3. Each model predicts COVID vs. healthy; the API returns all three with confidence scores.
  4. Training data is expanded 4Γ— with augmentation (noise, pitch-shift, time-shift) β€” applied only to the training split, after the train/test partition, to avoid leakage.

Results

5-fold stratified cross-validation on the Virufy clinical dataset (121 clips: 48 COVID, 73 healthy):

Model Accuracy Std Dev Type
XGBoost 85.1% Β±6.2% Classical ML (best)
Random Forest 83.5% Β±6.9% Classical ML
CNN (augmented) 78.5% Β±9.3% Deep Learning

Always cite the number with its spread β€” e.g. "85.1% Β± 6.2% (5-fold CV)". On a 121-clip dataset a single train/test split is too noisy to trust; cross-validation is the honest metric.

Why the CNN trails the tree models: with ~100 training clips, a CNN can't learn robust spectrogram patterns the way it would with thousands of samples. Hand-crafted MFCC features inject decades of audio-engineering knowledge, letting tree models generalize from far less data β€” a well-documented tradeoff in applied ML, not a bug.


Explainability

We apply SHAP (SHapley Additive exPlanations) to the XGBoost model to quantify each feature's contribution. The analysis confirms that MFCC-based timbral features carry the strongest signal β€” which aligns with clinical intuition: the "wet vs. dry" quality a doctor listens for is exactly what these coefficients encode. The model is learning something real, not a spurious artifact.

Figures (in reports/figures/): SHAP summary & bar plots, ROC curves (XGBoost AUC β‰ˆ 0.93), confusion matrices.


Data Quality Experiment

We tested whether more data would help by scaling from 121 clinical clips to 321 using COUGHVID (a 30,000-clip crowdsourced corpus). Counter-intuitively, accuracy dropped from 85% to ~65%.

Investigation showed why: COUGHVID labels are self-reported and inherently noisy, while Virufy labels are laboratory-confirmed. Even after quality filters (cough_detected β‰₯ 0.8, SNR sorting) the recovery was small. We also tried a Random-Forest + XGBoost ensemble and an enriched 102-feature set β€” neither beat the simple 37-feature XGBoost.

Lesson: in medical ML, data quality beats quantity. The final system uses the clean clinical data.

This experiment is itself a strength β€” it shows real experimentation, investigation, and a reasoned engineering decision rather than blindly stacking data.


Tech Stack

Layer Tools
Language Python 3.11
Classical ML scikit-learn (Random Forest), XGBoost
Deep Learning PyTorch (CNN)
Audio librosa, soundfile, ffmpeg
Explainability SHAP
Backend FastAPI + Uvicorn
Frontend HTML / CSS / JavaScript (Web Audio API)
Visualization matplotlib

Project Structure

cough-detect/
β”œβ”€β”€ assets/                 # logo
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ raw/                # cough clips, one folder per class (covid/, healthy/)
β”‚   └── processed/          # extracted features + spectrograms
β”œβ”€β”€ models/                 # trained models (RF, XGBoost, CNN)
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ home.html           # project overview / landing page
β”‚   β”œβ”€β”€ index.html          # live screening tool
β”‚   β”œβ”€β”€ dashboard.html      # analytics dashboard
β”‚   └── favicon.svg
β”œβ”€β”€ deploy/                 # self-contained copy for Docker deployment (Render, HF Spaces, etc.)
β”‚   β”œβ”€β”€ Dockerfile
β”‚   β”œβ”€β”€ serve.py             # backend + serves the frontend together
β”‚   β”œβ”€β”€ requirements.txt
β”‚   β”œβ”€β”€ frontend/            # copy of the 3 pages above
β”‚   β”œβ”€β”€ models/               # copy of the trained models
β”‚   └── src/                  # copy of features.py + dl_model.py
β”œβ”€β”€ reports/
β”‚   β”œβ”€β”€ CoughSense_Technical_Report.docx
β”‚   β”œβ”€β”€ figures/            # SHAP, ROC, confusion matrices
β”‚   └── test_log.csv        # validation log
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ features.py             # feature extraction
β”‚   β”œβ”€β”€ build_dataset.py        # build feature datasets
β”‚   β”œβ”€β”€ ml_baseline.py          # train RF + XGBoost
β”‚   β”œβ”€β”€ dl_model.py             # CNN architecture + training
β”‚   β”œβ”€β”€ augment.py              # audio augmentation
β”‚   β”œβ”€β”€ build_augmented_dataset.py
β”‚   β”œβ”€β”€ dl_model_augmented.py   # train CNN on augmented data
β”‚   β”œβ”€β”€ cross_validate.py       # 5-fold CV for all models
β”‚   β”œβ”€β”€ ensemble.py             # RF + XGBoost ensemble experiment
β”‚   β”œβ”€β”€ enhanced_features.py    # richer-feature experiment
β”‚   β”œβ”€β”€ explain.py              # SHAP + ROC + confusion plots
β”‚   β”œβ”€β”€ test_log.py             # testing & validation log
β”‚   β”œβ”€β”€ serve.py                # FastAPI inference API
β”‚   β”œβ”€β”€ download_more_data.py   # optional COUGHVID downloader
β”‚   β”œβ”€β”€ reset_coughvid.py       # revert to clinical-only data
β”‚   └── run_all.py              # runs the whole pipeline in order
β”œβ”€β”€ requirements.txt
└── README.md

Team Contributions

Aryan Verma β€” Machine Learning & Deep Learning lead. Audio feature extraction (MFCCs, spectral features), Random Forest / XGBoost / CNN model design and training, 5-fold cross-validation, data augmentation, ensemble and enhanced-feature experiments, SHAP explainability, and the FastAPI inference backend.

Arfa Alam β€” Frontend, documentation & validation. Web interface (screening tool + analytics dashboard UI/UX), project documentation (technical report and README), testing & validation (running cough samples through the system and logging predictions), and dataset organization (sorting and cleaning cough clips into class folders).


Setup & Installation

Windows (recommended: Python 3.11)

# 1. Virtual environment (3.11 β€” some libs don't build cleanly on 3.13/3.14 yet)
py -3.11 -m venv venv
.\venv\Scripts\Activate.ps1
#    If activation is blocked: Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

# 2. Dependencies
pip install -r requirements.txt

# 3. ffmpeg (needed to decode .mp3/.webm). If you don't have it and winget is unavailable:
pip install imageio-ffmpeg
python -c "import imageio_ffmpeg,shutil,os; exe=imageio_ffmpeg.get_ffmpeg_exe(); d=os.path.dirname(exe); shutil.copy(exe, os.path.join(d,'ffmpeg.exe')); print('ffmpeg ready at', d)"
#    then add that printed folder to PATH for the session:
#    $env:PATH = "<that folder>;" + $env:PATH

Any OS (quick)

pip install -r requirements.txt      # use a venv
cd src
python run_all.py                    # runs the whole pipeline in order

run_all.py chains every stage (features β†’ train RF/XGBoost β†’ augment β†’ train CNN β†’ cross-validate β†’ SHAP/plots). Run stages individually any time β€” see src/.


Running the App

The web interface needs two terminals.

Terminal 1 β€” backend (API):

cd src
uvicorn serve:app --reload --port 8000
# wait for "Application startup complete"

Terminal 2 β€” frontend:

cd frontend
python -m http.server 5500

Open http://localhost:5500 in your browser. Look for the API ONLINE badge (top-right). Upload or record a cough to get a live prediction; click Dashboard for the analytics view.

Windows note: activate the venv in each new terminal and re-apply the ffmpeg PATH line if audio decoding fails.


Testing & Validation

Run a batch of clips through all three models and log the predictions:

cd src
python test_log.py --n 20

Prints a per-clip table (actual vs. each model's prediction vs. consensus) and saves reports/test_log.csv. On a 20-clip sample, RF and XGBoost each scored 90% and the 3-model consensus 90% β€” consistent with the cross-validated results.


Limitations

  • Small, single-source dataset β€” not yet validated across populations, devices, or other respiratory conditions. Results are a proof of concept for the approach, not a clinical-grade claim.
  • Binary scope β€” currently COVID vs. healthy; more conditions need more labeled audio.
  • Screening, not diagnosis β€” the tool detects acoustic patterns, not disease. The medical disclaimer must stay in any demo.

Future Work

  • Larger, multi-source clinical datasets to close the CNN gap.
  • Multimodal fusion β€” combine cough audio with a short self-reported symptom form.
  • Grad-CAM visualizations of the spectrogram regions the CNN attends to.
  • Extend to additional respiratory conditions (asthma, bronchitis, TB).
  • A conversational assistant layer on top of the current next-steps guidance, for more personalized (but still non-prescriptive) follow-up questions.

Disclaimer

CoughSense is a research prototype and screening aid, not a medical device and not a diagnosis. It must not be used to make health decisions. Always consult a qualified healthcare professional.


Team Auscultate β€” CoughSense β€” Not for clinical use

About

Cough-based COVID screening using Random Forest, XGBoost & CNN on audio features. FastAPI + web dashboard.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages