Yadvendra commited on
Commit
106a8b6
·
verified ·
1 Parent(s): 3583bd6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -23
app.py CHANGED
@@ -1,52 +1,40 @@
1
  import streamlit as st
2
- from tensorflow.keras.models import load_model
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(uploaded_image, target_size=(224, 224)):
9
- img = Image.open(uploaded_image) # Open the uploaded image
10
- img = img.resize(target_size) # Resize to match model's input shape
11
- img_array = np.array(img) # Convert to numpy array
12
- img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
13
- img_array = img_array / 255.0 # Normalize pixel values
14
  return img_array
15
 
16
- # Load your pre-trained model (assuming it's in HDF5 format)
17
- @st.cache(allow_output_mutation=True) # Cache the model to avoid reloading it each time
18
  def load_cnn_model():
19
- model = load_model('dementia_cnn_model.h5') # Replace with your model path
20
  return model
21
 
22
  # Streamlit App UI
23
  st.title("Dementia Detection using CNN")
24
  st.write("Upload a brain scan (JPG format), and the model will predict its class.")
25
 
26
- # Upload image button
27
  uploaded_file = st.file_uploader("Choose a JPG image...", type="jpg")
28
 
29
- # If an image is uploaded, process it
30
  if uploaded_file is not None:
31
  st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
32
  st.write("Classifying...")
 
 
33
 
34
- # Preprocess the uploaded image
35
- processed_image = load_and_preprocess_image(uploaded_file, target_size=(224, 224))
36
-
37
- # Load the model
38
- model = load_cnn_model()
39
-
40
- # Make predictions
41
  predictions = model.predict(processed_image)
42
  predicted_class = np.argmax(predictions, axis=1)
43
 
44
- # Map the class index to the actual class names
45
  class_names = ['Moderate Dementia', 'Very Mild Dementia', 'Mild Dementia', 'Non Demented']
46
  result = class_names[predicted_class[0]]
47
 
48
- # Display the prediction
49
  st.write(f"Predicted Class: **{result}**")
50
- else:
51
- st.write("Please upload an image to classify.")
52
-
 
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(uploaded_image, target_size=(224, 224)):
9
+ img = Image.open(uploaded_image)
10
+ img = img.resize(target_size)
11
+ img_array = np.array(img)
12
+ img_array = np.expand_dims(img_array, axis=0)
13
+ img_array = img_array / 255.0
14
  return img_array
15
 
16
+ # Load your pre-trained model
 
17
  def load_cnn_model():
18
+ model = tf.keras.models.load_model('dementia_cnn_model.h5') # Ensure correct loading
19
  return model
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("Classifying...")
30
+
31
+ processed_image = load_and_preprocess_image(uploaded_file)
32
 
33
+ model = load_cnn_model() # Load the model
 
 
 
 
 
 
34
  predictions = model.predict(processed_image)
35
  predicted_class = np.argmax(predictions, axis=1)
36
 
 
37
  class_names = ['Moderate Dementia', 'Very Mild Dementia', 'Mild Dementia', 'Non Demented']
38
  result = class_names[predicted_class[0]]
39
 
 
40
  st.write(f"Predicted Class: **{result}**")