This repository contains a comprehensive solution for real-time breathing phase classification using machine learning models. The system can detect three breath phases: inhale, exhale, and silence from audio recordings, with additional support for tensometer-based automatic data labeling.
- Multiple ML Architectures: Transformer, LSTM, and Spectrum-based models with different performance characteristics
- Real-time Classification: Live audio processing and breath phase detection
- Tensometer Integration: Synchronized breathing data collection using tensometer sensors via Bluetooth
- Cross-platform Mobile App: Flutter application for Windows, Android, and future iOS support
- ONNX Model Export: Convert trained models to ONNX format for deployment
- Advanced Data Generation: Tools for creating labeled training data from tensometer and microphone readings
- Model Evaluation Tools: Comprehensive evaluation and visualization utilities
breathing-classification-v2/
βββ breathing_model/ # Core ML package
β βββ model/ # Current model implementations
β β βββ transformer/ # Latest transformer architecture
β β βββ trained_models/ # Pre-trained model checkpoints
β β βββ silence_detector/ # Silence detection model
β β βββ invalid_data_filter/ # Breath sounds detection using autoencoder
β βββ data/ # Data processing and generation
β β βββ generators/ # Training data generation tools
β β βββ train/ # Training datasets
β β βββ eval_seen_people/ # Evaluation datasets (subjects seen during training)
β β βββ eval_unseen_people/ # Evaluation datasets (completely unseen subjects)
β βββ archive/ # Legacy model implementations
β β βββ lstm/ # LSTM-based models
β β βββ spectrum/ # Frequency-domain models
β β βββ transformer_model/ # Previous transformer implementation
β βββ requirements.txt # Python dependencies
βββ app/ # Flutter mobile application
β βββ lib/ # Dart source code
β βββ assets/ # App resources and models
β βββ plugins/ # Custom platform plugins
β βββ README.md # App-specific documentation
βββ docs/ # Project documentation
βββ LICENSE # MIT License
- Python 3.12
- Flutter SDK 3.7.0+
- Dart SDK (included with Flutter)
- Platform-specific SDKs for target deployment
- Microphone for real-time audio capture
- Tensometer device (optional, for data collection)
-
Clone the repository:
git clone https://github.com/peterprospl12/breathing-classification-v2.git cd breathing-classification-v2 -
Set up a Python virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install Python dependencies:
cd breathing_model pip install -r requirements.txt
-
Install Flutter SDK (if not already installed):
- Follow the official Flutter installation guide
- Ensure Flutter is added to your system PATH
-
Navigate to the app directory and install dependencies:
cd app flutter pub get -
Run the application:
# For development on different platforms: flutter run -d windows # Windows desktop flutter run -d android # Android device/emulator
# Real-time breathing classification
from breathing_model.model.transformer.inference.main import main as run_inference
# Run real-time classification with visualization
run_inference()The latest transformer-based architecture provides state-of-the-art performance:
Architecture Details:
- Input: Mel-spectrogram features (128 mel bins)
- Model: Transformer encoder with 6 layers, 8 attention heads
- Output: 3-class classification (inhale, exhale, silence)
- Sample Rate: 44.1 kHz
- Chunk Length: 0.3 seconds
Usage Example:
from breathing_model.model.transformer.inference.main import main
# Start real-time classification with GUI
main()Configuration:
Model parameters can be adjusted in breathing_model/model/transformer/config.yaml
The project includes legacy implementations that are preserved in the archive directory:
Traditional sequential model for breath phase classification located in breathing_model/archive/lstm/.
Previous transformer implementation with ONNX export support located in breathing_model/archive/transformer_model/.
The project includes comprehensive tools for collecting labeled breathing data using tensometer sensors:
Features:
- Bluetooth Integration: Connects to FT7 tensometer devices
- Synchronized Recording: Simultaneous audio and tensometer data capture
- Automatic Labeling: Generates breathing phase labels from tensometer readings
- Multi-format Output: Supports various data formats for training
Usage:
# Start data collection session
python breathing_model/data/generators/tenso_model_based_data_generation/data_recorder.pyData Processing:
# Process collected tensometer data
python breathing_model/data/generators/tenso_model_based_data_generation/tenso_model_based_data_gen.pyFor scenarios without tensometer hardware:
# Manual data generation tools
python breathing_model/data/generators/manual_data_gen.pycd breathing_model/model/transformer
python train.pyModify training parameters in breathing_model/model/transformer/config.yaml:
- Batch size, learning rate, epochs
- Audio processing parameters
- Model architecture settings
The Flutter application provides a user-friendly interface for real-time breathing monitoring with cross-platform support.
- Real-time Visualization: Live breathing pattern display with smooth animations
- Phase Classification: Visual indicators for inhale, exhale, and silence phases
- Breath Counting: Automatic inhale/exhale cycle counting
- Audio Recording: Built-in recording capabilities with permission handling
- Cross-platform: Supports Windows, Android, with future iOS support
cd app
flutter run -d windowsflutter run -d androidiOS support may be added in future releases.
- Models: Place ONNX models in
app/assets/models/ - Permissions: Microphone access is automatically requested
- Themes: Customizable UI themes available
For detailed app setup instructions, see app/README.md.
Comprehensive project documentation is available in the docs/ directory:
- Business Presentation: Overview of project goals and outcomes
- System Requirements: Detailed technical specifications
- Model Evaluation: Performance analysis and benchmarks
- Project Organization: Development methodology and infrastructure
- Architecture Diagrams: Technical architecture visualizations
# If PyAudio installation fails on Windows:
pip install pipwin
pipwin install pyaudio
# For Linux/macOS PyAudio issues:
sudo apt-get install portaudio19-dev # Ubuntu/Debian
brew install portaudio # macOS# Clear Flutter cache
flutter clean
flutter pub get
# Check Flutter installation
flutter doctor# List available audio devices (Python)
python -c "import pyaudio; p = pyaudio.PyAudio(); [print(f'{i}: {p.get_device_info_by_index(i)}') for i in range(p.get_device_count())]"Available audio devices will also be listed when running manual data generator on realtime (inference).
- GPU Acceleration: Ensure CUDA is available for PyTorch training
- Audio Latency: Adjust chunk size in config for real-time performance
- Model Size: ONNX is required for mobile deployment (model can't be run on mobiles without ONNX)
This project is licensed under the MIT License - see the LICENSE file for details.
MIT License
Copyright (c) 2024 Piotr Sulewski
- Piotr Sulewski - peterprospl12
- Tomasz Sankowski - tomaszsankowski
- Iwo Czartowski - IwsonHD
For questions, issues, or collaboration opportunities:
- GitHub Issues: Create an issue
- Project Repository: breathing-classification-v2
Feel free to contact any of the Authors.
This project aims to advance breathing pattern analysis through machine learning, contributing to health monitoring and medical research applications.