Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,48 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from PIL import Image
|
3 |
-
import numpy as np
|
4 |
-
import tensorflow as tf
|
5 |
-
from tensorflow.keras.preprocessing import image
|
6 |
-
|
7 |
-
# Function to preprocess the uploaded image
|
8 |
-
def preprocess_uploaded_image(uploaded_image, target_size):
|
9 |
-
img = Image.open(uploaded_image)
|
10 |
-
img = img.resize(target_size)
|
11 |
-
img_array = image.img_to_array(img)
|
12 |
-
img_array = np.expand_dims(img_array, axis=0)
|
13 |
-
return img_array
|
14 |
-
|
15 |
-
# Function to load the model and make predictions
|
16 |
-
def predict_image_class(model_path, uploaded_image, target_size):
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
import numpy as np
|
4 |
+
import tensorflow as tf
|
5 |
+
from tensorflow.keras.preprocessing import image
|
6 |
+
|
7 |
+
# Function to preprocess the uploaded image
|
8 |
+
def preprocess_uploaded_image(uploaded_image, target_size):
|
9 |
+
img = Image.open(uploaded_image)
|
10 |
+
img = img.resize(target_size)
|
11 |
+
img_array = image.img_to_array(img)
|
12 |
+
img_array = np.expand_dims(img_array, axis=0)
|
13 |
+
return img_array
|
14 |
+
|
15 |
+
# Function to load the model and make predictions
|
16 |
+
def predict_image_class(model_path, uploaded_image, target_size):
|
17 |
+
try:
|
18 |
+
loaded_model = tf.keras.models.load_model(model_path)
|
19 |
+
img = preprocess_uploaded_image(uploaded_image, target_size)
|
20 |
+
prediction = loaded_model.predict(img)
|
21 |
+
class_idx = np.argmax(prediction)
|
22 |
+
return class_idx
|
23 |
+
except Exception as e:
|
24 |
+
st.error(f"Error loading the model: {e}")
|
25 |
+
return None
|
26 |
+
|
27 |
+
def main():
|
28 |
+
st.title("Heart Disease Image Classifier")
|
29 |
+
uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
30 |
+
|
31 |
+
if uploaded_image is not None:
|
32 |
+
st.image(uploaded_image, caption="Uploaded Image", use_column_width=True)
|
33 |
+
st.write("")
|
34 |
+
with st.spinner("Classifying..."):
|
35 |
+
# Classify the uploaded image
|
36 |
+
class_idx = predict_image_class("model.h5", uploaded_image, target_size=(224, 224))
|
37 |
+
|
38 |
+
if class_idx is not None:
|
39 |
+
if class_idx == 0:
|
40 |
+
st.write("The patient doesn't have heart disease")
|
41 |
+
else:
|
42 |
+
st.write("The patient has heart disease")
|
43 |
+
else:
|
44 |
+
st.error("Failed to classify the image. Please try again.")
|
45 |
+
|
46 |
+
# Run the Streamlit app
|
47 |
+
if __name__ == "__main__":
|
48 |
+
main()
|