%%writefile app.py
import streamlit as st
import joblib
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from tensorflow.keras.applications.inception_v3 import preprocess_input
from tensorflow.keras.applications.inception_v3 import decode_predictions
def main():
# Load the best saved models
loaded_densenet121_model = load_model(β/content/best_DenseNet121_model.h5β)
svm_classifier = joblib.load(β/content/svm_classifier.pklβ)
# Set page title and favicon
st.set_page_config(page_title="Cardiac Disease Prediction & ECG Image Classification", page_icon="β
")
# Create a sidebar for options
st.sidebar.title("Options")
option = st.sidebar.radio("Select an Option", ["Predict Cardiac Disease", "Classify ECG Image"])
# Create the main content area
st.title("Cardiac Disease Prediction and ECG Image Classification")
if option == "Predict Cardiac Disease":
st.header("Cardiac Disease Prediction")
st.write("Enter Patient Parameters:")
# Create input fields for patient data
age = st.slider("Age", min_value=0, max_value=100, step=1)
gender = st.radio("Gender", ["Male", "Female"])
chest_pain = st.selectbox("Chest Pain Type", ["Typical Angina", "Atypical Angina", "Non-anginal Pain", "Asymptomatic"])
resting_bp = st.number_input("Resting Blood Pressure", min_value=0)
serum_cholesterol = st.number_input("Serum Cholesterol (mg/dl)", min_value=0)
resting_ecg = st.selectbox("Resting Electrocardiographic Results", ["Normal", "ST-T Wave Abnormality", "Left Ventricular Hypertrophy"])
max_heart_rate = st.number_input("Max Heart Rate", min_value=0)
old_peak = st.number_input("Oldpeak", min_value=0.0)
slope = st.selectbox("Slope of Peak Exercise ST Segment", ["Upsloping", "Flat", "Downsloping"])
num_major_vessels = st.number_input("Number of Major Vessels", min_value=0)
if st.button("Predict Cardiac Disease"):
# Preprocess the input data
gender = 1 if gender == "Male" else 0
chest_pain = ["Typical Angina", "Atypical Angina", "Non-anginal Pain", "Asymptomatic"].index(chest_pain)
resting_ecg = ["Normal", "ST-T Wave Abnormality", "Left Ventricular Hypertrophy"].index(resting_ecg)
input_data = [age, gender, chest_pain, resting_bp, serum_cholesterol, resting_ecg, max_heart_rate, old_peak, slope, num_major_vessels]
input_data = np.array(input_data).reshape(1, -1)
# Make predictions
result_svm = svm_classifier.predict(input_data)
# Display the results
st.subheader("Cardiac Disease Prediction Results:")
if result_svm[0] == 0:
st.error("High likelihood of heart disease.")
else:
st.success("Low likelihood of heart disease")
if option == "Classify ECG Image":
st.header("ECG Image Classification")
uploaded_file = st.file_uploader("Choose an ECG image...", type=["jpg", "png"])
if uploaded_file:
st.subheader("Selected ECG Image:")
st.image(uploaded_file, use_column_width=True)
if st.button("Classify ECG Image"):
# Perform ECG image classification using InceptionV3
img = load_img(uploaded_file, target_size=(299, 299))
img_array = img_to_array(img)
img_array = preprocess_input(img_array)
img_array = np.expand_dims(img_array, axis=0)
predictions = loaded_densenet121_model.predict(img_array)
decoded_predictions = decode_predictions(predictions, top=1)[0]
st.subheader("ECG Image Classification Results:")
st.write(f"Predicted class: {decoded_predictions[0][1]}")
Run the Streamlit app
if name == βmainβ:
main()