Skip to content

Latest commit

Β 

History

History
325 lines (249 loc) Β· 8.3 KB

File metadata and controls

325 lines (249 loc) Β· 8.3 KB

Parkinson's Disease Detection System

A complete end-to-end machine learning pipeline for detecting Parkinson's disease from voice audio recordings (.wav files).

πŸ“‹ Overview

This system extracts acoustic features from voice recordings and uses machine learning classifiers to distinguish between:

  • Healthy individuals (label: 0)
  • Parkinson's Disease patients (label: 1)

🎯 Features

  • Feature Extraction: MFCC, Chroma, Spectral Centroid, Spectral Bandwidth, Spectral Rolloff, Zero Crossing Rate
  • Multiple Models: Random Forest, Logistic Regression, SVM
  • Dimensionality Reduction: PCA with configurable variance retention
  • Production Ready: Saved models, scalers, and preprocessing pipelines
  • Terminal Prediction: Easy-to-use command-line interface

πŸ“ Project Structure

final-project/
β”œβ”€β”€ feature_extraction.py    # Extract audio features and create CSV
β”œβ”€β”€ train.py                 # Train models without PCA
β”œβ”€β”€ pca_train.py             # Train models with PCA
β”œβ”€β”€ predict.py               # Terminal-based prediction
β”œβ”€β”€ utils.py                 # Helper functions
β”œβ”€β”€ generate_sample_data.py  # Generate synthetic test data
β”œβ”€β”€ requirements.txt         # Python dependencies
β”œβ”€β”€ README.md               # This file
β”‚
β”œβ”€β”€ dataset/                # Audio data directory
β”‚   β”œβ”€β”€ healthy/           # Healthy control .wav files
β”‚   └── parkinson/         # Parkinson's patient .wav files
β”‚
β”œβ”€β”€ models/                 # Saved models and preprocessors
β”‚   β”œβ”€β”€ RandomForest_no_pca.joblib
β”‚   β”œβ”€β”€ LogisticRegression_no_pca.joblib
β”‚   β”œβ”€β”€ SVM_no_pca.joblib
β”‚   β”œβ”€β”€ RandomForest_pca.joblib
β”‚   β”œβ”€β”€ LogisticRegression_pca.joblib
β”‚   β”œβ”€β”€ SVM_pca.joblib
β”‚   β”œβ”€β”€ scaler_no_pca.joblib
β”‚   β”œβ”€β”€ scaler_pca.joblib
β”‚   └── pca_model.joblib
β”‚
β”œβ”€β”€ plots/                  # Generated visualizations
β”‚   β”œβ”€β”€ confusion_matrix_*.png
β”‚   β”œβ”€β”€ accuracy_comparison_*.png
β”‚   β”œβ”€β”€ feature_importance_*.png
β”‚   └── pca_explained_variance.png
β”‚
β”œβ”€β”€ features.csv            # Extracted features
β”œβ”€β”€ model_results.csv       # Model performance metrics
└── best_model.txt          # Best performing model

πŸš€ Quick Start

1. Install Dependencies

pip install -r requirements.txt

2. Generate/Prepare Data

Option A: Generate synthetic test data

python generate_sample_data.py --output_dir ./dataset --samples 200

Option B: Use your own dataset

Place your .wav files in the following structure:

dataset/
β”œβ”€β”€ healthy/
β”‚   β”œβ”€β”€ sample1.wav
β”‚   β”œβ”€β”€ sample2.wav
β”‚   └── ...
└── parkinson/
    β”œβ”€β”€ sample1.wav
    β”œβ”€β”€ sample2.wav
    └── ...

3. Extract Features

python feature_extraction.py --data_dir ./dataset --output features.csv

4. Train Models (Without PCA)

python train.py --data features.csv --output_dir ./models

5. Train Models (With PCA)

python pca_train.py --data features.csv --output_dir ./models --variance 0.95

6. Make Predictions

# Use best model automatically
python predict.py --file path/to/audio.wav

# Use specific model
python predict.py --file audio.wav --model RandomForest

# Verbose output
python predict.py --file audio.wav --verbose

# Record from microphone and predict (NEW!)
python predict.py --record

# Record 5 seconds and predict
python predict.py --record --duration 5

🎀 Record Your Own Voice

Built-in Recording (Recommended)

# Install PyAudio first (see installation below)

# Record 3 seconds and predict immediately
python predict.py --record

# Record 5 seconds and predict
python predict.py --record --duration 5

External Recording

macOS (QuickTime):

  1. Open QuickTime Player
  2. File β†’ New Audio Recording
  3. Record your voice (say "ahhh" for 3-5 seconds)
  4. Save as .wav file
  5. python predict.py --file my_recording.wav

Linux (arecord):

arecord -d 3 -r 22050 -c 1 -f S16_LE my_voice.wav
python predict.py --file my_voice.wav

Windows (Voice Recorder):

  1. Use Voice Recorder app
  2. Save as .wav
  3. python predict.py --file my_recording.wav

πŸ“ Recording Tips for Best Results

Tip Recommendation
Environment Quiet room, minimal background noise
Distance 6-12 inches from microphone
What to say Sustain "ahhh" or "eee" for 3-5 seconds
Format .wav file (any sample rate, auto-converted)
Duration 3-5 seconds ideal

πŸ”§ PyAudio Installation

# macOS
brew install portaudio
pip install pyaudio

# Linux
sudo apt-get install portaudio19-dev
pip install pyaudio

# Windows
pip install pyaudio
# Or download wheel from: https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio

πŸ“Š Feature Extraction

The system extracts the following features using librosa:

Feature Type Description Count
MFCC Mean of each coefficient 20
MFCC Standard deviation of each coefficient 20
Chroma Mean of each chroma feature 12
Chroma Standard deviation of each chroma feature 12
Spectral Centroid Mean and Std 2
Spectral Bandwidth Mean and Std 2
Spectral Rolloff Mean and Std 2
Zero Crossing Rate Mean and Std 2
Total 72

πŸ€– Models

Without PCA

  • Random Forest: 100 estimators, max depth 10
  • Logistic Regression: L2 regularization, 1000 iterations
  • SVM: RBF kernel, C=1.0

With PCA

Same models trained on PCA-transformed features (95% variance retained)

πŸ“ˆ Output Files

model_results.csv

Contains performance metrics for all models:

  • Model name
  • PCA usage flag
  • Accuracy, Precision, Recall, F1 Score

best_model.txt

Name of the best performing model (by accuracy)

Plots

  • Confusion matrices for all 6 models
  • Accuracy comparison bar charts
  • Feature importance visualization
  • PCA explained variance (scree plot)

🎯 Prediction Output

==================================================
PARKINSON'S DISEASE DETECTION - PREDICTION
==================================================
Using Model: RandomForest_PCA
Prediction: Parkinson Detected
Confidence: 87.45%
==================================================

πŸ”§ Configuration

Training Configuration (train.py, pca_train.py)

  • Test split: 15%
  • Random state: 42 (reproducible)
  • PCA variance: 90-95% (configurable)

Feature Extraction (feature_extraction.py)

  • Sample rate: 22050 Hz
  • MFCC coefficients: 20
  • Chroma features: 12

πŸ“ Command Reference

Feature Extraction

python feature_extraction.py -d ./dataset -o features.csv

Training

# Without PCA
python train.py -d features.csv -o ./models

# With PCA
python pca_train.py -d features.csv -v 0.95

Prediction

# Basic
python predict.py -f audio.wav

# With specific model
python predict.py -f audio.wav -m SVM_PCA

# Verbose
python predict.py -f audio.wav -v

πŸ” Exit Codes (predict.py)

Code Meaning
0 Healthy detected
1 Parkinson's detected
2 File not found
3 Invalid file format
4 Feature extraction failed
99 Unexpected error

πŸ“š Dependencies

  • librosa >= 0.10.0
  • numpy >= 1.24.0
  • pandas >= 2.0.0
  • scikit-learn >= 1.3.0
  • matplotlib >= 3.7.0
  • seaborn >= 0.12.0
  • joblib >= 1.3.0
  • soundfile >= 0.12.0

⚠️ Important Notes

  1. Synthetic Data: The included generate_sample_data.py creates synthetic audio for testing. For production use, replace with real voice recordings from:

    • Healthy control group
    • Parkinson's disease patients
  2. Model Selection: The best model is automatically selected based on accuracy. Check best_model.txt to see which model is being used.

  3. Preprocessing Consistency: The same preprocessing pipeline (scaler + PCA) used in training is automatically applied during prediction.

  4. Reproducibility: All random operations use random_state=42 for reproducible results.

πŸ“„ License

MIT License

🀝 Contributing

This is a complete, production-ready system. Feel free to extend with:

  • Additional feature types
  • More classifiers
  • Deep learning models
  • Real-time audio processing
  • Web/GUI interface