Yadvendra commited on
Commit
8750a41
·
verified ·
1 Parent(s): 106a8b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -21
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(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}**")
 
 
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}**")