How kholay47/ecg-heartbeat-classification works
Project Type & Purpose
• This is a small full-stack web application whose backend is a Flask server (app.py) serving a machine-learning model (RandomForestClassifier) to classify ECG signals as Normal or Abnormal. The frontend is a single HTML template (index.html).
File Structure Analysis
• Top-level:
• app.py → Flask application, request handlers
• ecg.ipynb → Jupyter notebook used offline to train and evaluate the model
• model.pkl → (implied by README) persisted RandomForestClassifier
• scaler.pkl → (implied by README) persisted StandardScaler
• templates/ → directory for Flask’s HTML templates
└ index.html → form for uploading ECG CSV, plus result display
• README.md, LICENSE
Main Components
a. User Interface (Browser)
• index.html served by Flask – upload form & result page
b. Flask Backend (app.py)
• HTTP GET “/” → render index.html
• HTTP POST “/classify” → accept uploaded CSV
c. Preprocessing Module
• pandas reads CSV → DataFrame
• StandardScaler (scaler.pkl) transforms features
d. Prediction Module
• RandomForestClassifier (model.pkl) predicts labels
e. Result Aggregator
• Count & percentage calculator
• Passes data back into template context
f. Offline Training Pipeline (ecg.ipynb)
• Data loading (PTB-XL CSV) → train/test split
• StandardScaler + RandomForestClassifier → model.pkl & scaler.pkl
g. External Libraries & Dependencies
• flask, pandas, scikit-learn, joblib
Component Relationships & Data Flow
• Browser → HTTP POST CSV → Flask →
→ pandas loads into DataFrame → scaler.transform → model.predict → aggregator →
→ Flask renders index.html with results → Browser
• On startup, Flask loads model.pkl & scaler.pkl into memory
• Offline: ecg.ipynb writes model.pkl & scaler.pkl (not part of runtime)
Architectural Patterns & Principles
• Separation of Concerns:
• Presentation Layer (templates)
• Application Layer (Flask request handlers)
• Business Logic Layer (preprocessing + prediction)
• Stateless request-response: no session management or persistent DB
• Artifact Reuse: model and scaler loaded once at startup