Real-time audio classification system that detects and logs important sound events (Alarms, Clanging, Help, Movement) while filtering out background noise. Built with TensorFlow Lite for efficient edge deployment.
- Real-time audio monitoring with TensorFlow Lite
- 80% confidence threshold for high-accuracy detection
- Background noise filtering (not logged to database)
- SQLite database for local event storage
- Optional notifications: Email, SMS (Twilio), Discord, Slack, Webhooks, Firebase
- Configurable alerts with cooldown periods
- Cross-platform: Test on PC, deploy to Raspberry Pi
- Python 3.8+
- Microphone
- TensorFlow 2.8+ (full version)
- Raspberry Pi 3B+ or newer
- Raspberry Pi OS (Bullseye or newer)
- USB Microphone
- Python 3.8+
- tflite_runtime (lightweight)
ece-198/
βββ raspberry_pi_monitor.py # Main monitoring script
βββ test_model.py # Test TFLite model
βββ view_database.py # Query/view database
βββ config.py # Configuration & API keys
βββ converted_tflite/
β βββ soundclassifier_with_metadata.tflite # TFLite model
β βββ labels.txt # Classes: Alarms, Background Noise, Clanging, Help, Movement
βββ tm-my-audio-model/ # Original Teachable Machine model (TF.js)
β βββ model.json
β βββ metadata.json
β βββ weights.bin
βββ audio_events.db # SQLite database (auto-created)
βββ requirements.txt # PC dependencies
βββ requirements_pi.txt # Raspberry Pi dependencies
βββ RASPBERRY_PI_SETUP.md # Complete Pi deployment guide
βββ README.md # This file
git clone https://github.com/KerelosGayed/ece-198.git
cd ece-198On PC (Windows):
# Create virtual environment
python -m venv venv
# Activate venv
.\venv\Scripts\Activate.ps1
# Install dependencies
pip install tensorflow sounddevice scipy requestsOn Raspberry Pi:
# Install system dependencies
sudo apt-get update
sudo apt-get install -y portaudio19-dev python3-pip
# Install Python packages
pip3 install --extra-index-url https://google-coral.github.io/py-repo/ tflite_runtime
pip3 install -r requirements_pi.txtEdit config.py to customize:
# Detection settings
CONFIDENCE_THRESHOLD = 0.80 # 80% minimum confidence
SAMPLE_RATE = 44100 # Audio sample rate
AUDIO_DURATION = 1.0 # Seconds per clip
# Alert settings
ALERT_ON_EVENTS = ["Help", "Alarms"] # Only these trigger alerts
ALERT_CONFIDENCE_THRESHOLD = 0.85 # 85% for alerts
# Add API keys for notifications (optional)
EMAIL_ENABLED = False
SMS_ENABLED = False
DISCORD_ENABLED = False
# See config.py for full configuration options# On PC (with venv activated):
python test_model.py
# On Raspberry Pi:
python3 test_model.pyOn PC:
.\venv\Scripts\Activate.ps1
python raspberry_pi_monitor.pyOn Raspberry Pi:
python3 raspberry_pi_monitor.py# View recent events
python view_database.py recent 20
# View statistics
python view_database.py summary
# Export to CSV
python view_database.py export events.csv- Audio Capture: 1-second audio clips at 44.1kHz
- Preprocessing: Normalize raw audio to model input format [1, 44032]
- Inference: TFLite model classifies audio into 5 categories
- Filtering:
- Requires β₯80% confidence
- Background Noise is ignored (not logged)
- Logging: Events saved to SQLite database with timestamp
- Alerts: Optional notifications via Email/SMS/Discord/Slack/Webhooks
| Event | Description | Logged to DB | Can Trigger Alerts |
|---|---|---|---|
| Alarms | Alarm sounds | β Yes | β Yes |
| Clanging | Metal/object impacts | β Yes | β Yes |
| Help | Voice calling for help | β Yes | β Yes (Default) |
| Movement | Physical movement sounds | β Yes | β Yes |
| Background Noise | Ambient/general noise | β Filtered | β No |
Table: audio_events
| Column | Type | Description |
|---|---|---|
id |
INTEGER | Auto-increment primary key |
timestamp |
TEXT | ISO 8601 format |
event_type |
TEXT | Alarms, Clanging, Help, or Movement |
confidence |
REAL | 0.0 - 1.0 (model confidence) |
all_predictions |
TEXT | Full breakdown of all class predictions |
Example Row:
id: 1
timestamp: 2025-11-10T23:37:24.859284
event_type: 3 Help
confidence: 0.9492
all_predictions: 0 Alarms: 2.14%, 1 Background Noise: 1.52%, 2 Clanging: 0.98%, 3 Help: 94.92%, 4 Movement: 0.44%
# Detection thresholds
CONFIDENCE_THRESHOLD = 0.80 # 80% minimum to log
ALERT_CONFIDENCE_THRESHOLD = 0.85 # 85% minimum to alert
# Audio configuration
SAMPLE_RATE = 44100 # Hz (match model training)
AUDIO_DURATION = 1.0 # Seconds per clip
AUDIO_CHANNELS = 1 # Mono
AUDIO_DEVICE_INDEX = None # None = default mic
# Database
DATABASE_PATH = "audio_events.db"
# Model paths
MODEL_PATH = "converted_tflite/soundclassifier_with_metadata.tflite"
LABELS_PATH = "converted_tflite/labels.txt"
# Display options
VERBOSE = True # Show all detections# Which events trigger alerts?
ALERT_ON_EVENTS = ["Help", "Alarms"] # Empty [] = all events
# Cooldown prevents alert spam
ALERT_COOLDOWN = 60 # Seconds between alerts for same event typeEMAIL_ENABLED = True
EMAIL_SMTP_SERVER = "smtp.gmail.com"
EMAIL_SMTP_PORT = 587
EMAIL_FROM = "your-email@gmail.com"
EMAIL_PASSWORD = "your-app-password" # Not regular password!
EMAIL_TO = ["recipient1@gmail.com", "recipient2@gmail.com"]Get Gmail App Password:
- Go to https://myaccount.google.com/apppasswords
- Select "Mail" and generate password
- Use generated 16-character password (not your regular Gmail password)
SMS_ENABLED = True
TWILIO_ACCOUNT_SID = "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
TWILIO_AUTH_TOKEN = "your_auth_token"
TWILIO_FROM_NUMBER = "+15551234567"
TWILIO_TO_NUMBERS = ["+15559876543", "+15551111111"]Get Twilio Credentials:
- Sign up at https://www.twilio.com/
- Get Account SID and Auth Token from dashboard
- Purchase a phone number or use trial number
DISCORD_ENABLED = True
DISCORD_WEBHOOK_URL = "https://discord.com/api/webhooks/123456789/abcdefgh..."Create Discord Webhook:
- Server Settings β Integrations β Webhooks
- Click "New Webhook"
- Copy webhook URL
SLACK_ENABLED = True
SLACK_WEBHOOK_URL = "https://hooks.slack.com/services/T00000000/B00000000/XXXX"Create Slack Webhook:
- Go to https://api.slack.com/messaging/webhooks
- Create new incoming webhook
- Copy webhook URL
WEBHOOK_ENABLED = True
WEBHOOK_URL = "https://your-server.com/api/audio-events"
WEBHOOK_SECRET = "your-secret-key" # Optional authenticationWebhook POST Format:
{
"event_type": "3 Help",
"confidence": 0.9492,
"timestamp": "2025-11-10T23:37:24.859284",
"predictions": {
"0 Alarms": 0.0214,
"1 Background Noise": 0.0152,
"2 Clanging": 0.0098,
"3 Help": 0.9492,
"4 Movement": 0.0044
}
}FIREBASE_ENABLED = True
FIREBASE_CREDENTIALS_PATH = "firebase-credentials.json"
FIREBASE_COLLECTION = "audio_events"Setup Firebase:
- Go to https://console.firebase.google.com/
- Create project β Firestore Database
- Project Settings β Service Accounts β Generate new private key
- Save JSON file as
firebase-credentials.json
# On PC:
python -c "import sounddevice as sd; print(sd.query_devices())"
# On Raspberry Pi:
python3 -c "import sounddevice as sd; print(sd.query_devices())"Edit config.py:
AUDIO_DEVICE_INDEX = 2 # Use device index from list above# Show 20 most recent events
python view_database.py recent 20
# Show summary statistics
python view_database.py summary
# Show events from last hour
python view_database.py hour
# Export to CSV
python view_database.py export my_events.csv# Open database
sqlite3 audio_events.db
# SQL queries
SELECT * FROM audio_events ORDER BY timestamp DESC LIMIT 10;
SELECT event_type, COUNT(*) FROM audio_events GROUP BY event_type;
SELECT * FROM audio_events WHERE confidence > 0.9;
.quitimport sqlite3
from datetime import datetime, timedelta
conn = sqlite3.connect('audio_events.db')
cursor = conn.cursor()
# Get events from last hour
one_hour_ago = (datetime.now() - timedelta(hours=1)).isoformat()
cursor.execute('''
SELECT timestamp, event_type, confidence
FROM audio_events
WHERE timestamp > ?
ORDER BY timestamp DESC
''', (one_hour_ago,))
for row in cursor.fetchall():
print(f"{row[0]} | {row[1]} | {row[2]:.1%}")
conn.close()"Index out of bounds"
- Fixed! Audio preprocessing now matches model input shape [1, 44032]
"No module named 'tflite_runtime'"
# On PC: Use full TensorFlow
pip install tensorflow
# On Raspberry Pi: Install tflite_runtime
pip3 install --extra-index-url https://google-coral.github.io/py-repo/ tflite_runtime"No default input device"
# List devices
python -c "import sounddevice as sd; print(sd.query_devices())"
# Set specific device in config.py
AUDIO_DEVICE_INDEX = 1 # Replace with your device index"PortAudio error"
# Windows: Install Microsoft C++ Redistributable
# Raspberry Pi:
sudo apt-get install portaudio19-dev
pip3 install sounddevice --force-reinstall"config.py not found"
- Make sure
config.pyexists in the same directory - Never delete or rename
config.py
"ImportError: No module named 'config'"
# Make sure you're in the right directory
cd ece-198
python raspberry_pi_monitor.py-
Never commit
config.pywith real API keys to GitHub- Already in
.gitignore
- Already in
-
Use environment variables for production:
import os
EMAIL_PASSWORD = os.environ.get('EMAIL_PASSWORD', 'default-value')- Restrict file permissions on Raspberry Pi:
chmod 600 config.py
chmod 600 firebase-credentials.json// Firestore rules
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /audio_events/{document} {
// Only allow authenticated writes from your device
allow write: if request.auth != null;
allow read: if request.auth != null;
}
}
}See RASPBERRY_PI_SETUP.md for complete deployment guide including:
- System setup and dependencies
- File transfer methods
- Auto-start on boot (systemd service)
- Remote access and monitoring
- Performance optimization
# Lower sample rate (less quality, faster)
SAMPLE_RATE = 16000 # Instead of 44100
# Process longer clips less frequently
AUDIO_DURATION = 2.0 # Instead of 1.0# Higher confidence threshold = fewer logs
CONFIDENCE_THRESHOLD = 0.90 # Instead of 0.80VERBOSE = False # Only show logged events- Source: Teachable Machine (Google)
- Format: TensorFlow Lite
- Input: Raw audio waveform [1, 44032] samples (~1 second at 44.1kHz)
- Output: 5 class probabilities (softmax)
- Classes: 0 Alarms, 1 Background Noise, 2 Clanging, 3 Help, 4 Movement
- PC: Use full TensorFlow (larger but has GPU support)
- Pi: Use tflite_runtime (10x smaller, optimized for ARM)
- Same code works on both thanks to try/except import
Issues and pull requests welcome! Areas for improvement:
- Add more notification channels
- Web dashboard for real-time monitoring
- Model retraining pipeline
- Multi-device coordination
- Advanced audio preprocessing
MIT License - See LICENSE file for details
- Issues: https://github.com/KerelosGayed/ece-198/issues
- Documentation: See RASPBERRY_PI_SETUP.md for deployment details
Built with β€οΈ for ECE-198
-
Never commit master-key.txt to git
echo "master-key.txt" >> .gitignore
-
Rotate encryption keys periodically
- Back up old key
- Generate new key
- Re-encrypt existing data
-
Use environment variables for secrets
# Never hardcode credentials in code export MONGO_URI="mongodb+srv://..."
-
Restrict MongoDB user permissions
- Read/write only to required database
- No admin privileges
-
Enable MongoDB Atlas audit logs
- Track all data access
- Alert on suspicious activity
For issues specific to:
- Teachable Machine: Check model.tflite input shape matches preprocessing
- MongoDB: Verify connection string and network access
- Audio: Test with
arecordbefore running Python - Raspberry Pi: Ensure system updated and USB power adequate
- Collect training data in actual hospital environment
- Retrain model with real samples for better accuracy
- Create nurse dashboard to visualize MongoDB data
- Add light sensor for environmental context
- Implement real-time alerts for critical events