Update app.py
Browse files
app.py
CHANGED
@@ -1,40 +1,53 @@
|
|
1 |
import streamlit as st
|
2 |
-
import tensorflow as tf
|
3 |
-
from tensorflow.keras.preprocessing import image
|
4 |
import numpy as np
|
|
|
|
|
5 |
from PIL import Image
|
|
|
6 |
|
7 |
# Function to load and preprocess the uploaded image
|
8 |
-
def load_and_preprocess_image(
|
9 |
-
|
10 |
-
img =
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
# Streamlit App UI
|
22 |
st.title("Dementia Detection using CNN")
|
23 |
st.write("Upload a brain scan (JPG format), and the model will predict its class.")
|
24 |
|
|
|
25 |
uploaded_file = st.file_uploader("Choose a JPG image...", type="jpg")
|
26 |
|
27 |
if uploaded_file is not None:
|
28 |
st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
|
29 |
-
st.write("
|
30 |
|
|
|
31 |
processed_image = load_and_preprocess_image(uploaded_file)
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
predicted_class = np.argmax(predictions, axis=1)
|
36 |
|
37 |
-
|
38 |
-
|
39 |
|
40 |
-
|
|
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
import numpy as np
|
3 |
+
import cv2
|
4 |
+
import tensorflow as tf
|
5 |
from PIL import Image
|
6 |
+
from sklearn.preprocessing import LabelEncoder # Assuming you have this imported as well
|
7 |
|
8 |
# Function to load and preprocess the uploaded image
|
9 |
+
def load_and_preprocess_image(uploaded_file):
|
10 |
+
# Convert uploaded file to a format suitable for OpenCV
|
11 |
+
img = Image.open(uploaded_file)
|
12 |
+
img = img.convert("RGB") # Convert to RGB if it's in another format
|
13 |
+
img = np.array(img) # Convert to NumPy array
|
14 |
+
img = cv2.resize(img, (224, 224)) # Resize the image to 224x224
|
15 |
+
img = img / 255.0 # Normalize pixel values
|
16 |
+
img = np.reshape(img, (1, 224, 224, 3)) # Reshape for prediction
|
17 |
+
return img
|
18 |
+
|
19 |
+
# Function to predict the image class
|
20 |
+
def predict_image(img):
|
21 |
+
predictions = model.predict(img) # Make a prediction
|
22 |
+
predicted_class_index = np.argmax(predictions[0]) # Get the predicted class index
|
23 |
+
return predicted_class_index
|
24 |
+
|
25 |
+
# Function to get class label
|
26 |
+
def get_class_label(predicted_class_index):
|
27 |
+
return label_encoder.inverse_transform([predicted_class_index])[0] # Get class label
|
28 |
+
|
29 |
+
# Load your pre-trained model (Make sure this matches the version used during training)
|
30 |
+
model = tf.keras.models.load_model('dementia_cnn_model.h5')
|
31 |
|
32 |
# Streamlit App UI
|
33 |
st.title("Dementia Detection using CNN")
|
34 |
st.write("Upload a brain scan (JPG format), and the model will predict its class.")
|
35 |
|
36 |
+
# File uploader for user to upload images
|
37 |
uploaded_file = st.file_uploader("Choose a JPG image...", type="jpg")
|
38 |
|
39 |
if uploaded_file is not None:
|
40 |
st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
|
41 |
+
st.write("Detecting...")
|
42 |
|
43 |
+
# Load and preprocess the image
|
44 |
processed_image = load_and_preprocess_image(uploaded_file)
|
45 |
|
46 |
+
# Make prediction
|
47 |
+
predicted_class_index = predict_image(processed_image)
|
|
|
48 |
|
49 |
+
# Get predicted class label
|
50 |
+
predicted_class_label = get_class_label(predicted_class_index)
|
51 |
|
52 |
+
# Display the result
|
53 |
+
st.write(f"The predicted class is: **{predicted_class_label}**")
|