mercelv commited on
Commit
8a4c886
·
verified ·
1 Parent(s): 5d4074a

Upload 9 files

Browse files
Files changed (7) hide show
  1. 1 no.jpeg +0 -0
  2. Y101.jpg +0 -0
  3. app.py +115 -50
  4. glioma1.jpg +0 -0
  5. meningioma.jpg +0 -0
  6. notumor.jpg +0 -0
  7. pituitary.jpg +0 -0
1 no.jpeg ADDED
Y101.jpg ADDED
app.py CHANGED
@@ -3,8 +3,91 @@ import tensorflow as tf
3
  import numpy as np
4
  from PIL import Image
5
 
6
- # Load the saved ResNet model
7
- model_resnet = []#tf.keras.models.load_model("path_to_your_saved_model/resnet_model.h5")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  # Set page configuration and layout
10
  st.set_page_config(
@@ -13,8 +96,27 @@ st.set_page_config(
13
  layout="wide",
14
  )
15
 
16
- # Header logo
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  st.image("RCAIoT_logo.png", use_column_width=False)
 
18
 
19
  # Main content
20
  col1, col2 = st.columns([1, 1])
@@ -23,18 +125,8 @@ with col1:
23
  st.header("Upload Image")
24
  uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"], key="upload_image")
25
  if uploaded_image is not None:
26
- # Display the uploaded image with fixed initial height and width using HTML/CSS
27
  st.image(uploaded_image, caption="Uploaded Image", use_column_width=False, width=300)
28
- st.markdown(
29
- """
30
- <style>
31
- img {
32
- max-height: 300px;
33
- }
34
- </style>
35
- """,
36
- unsafe_allow_html=True,
37
- )
38
 
39
  with col2:
40
  st.header("Results")
@@ -47,12 +139,13 @@ with col2:
47
  display: flex;
48
  justify-content: space-between;
49
  align-items: center;
 
50
  }
51
  .analyze-reset-buttons button {
52
  flex: 1;
53
  margin: 10px;
54
  padding: 10px;
55
- background-color: #3366ff;
56
  color: white;
57
  font-size: 16px;
58
  text-align: center;
@@ -62,19 +155,17 @@ with col2:
62
  transition: background-color 0.3s ease;
63
  }
64
  .analyze-reset-buttons button:hover {
65
- background-color: #2353b4;
66
  }
67
  .results-text {
68
  font-size: 24px;
69
  font-weight: bold;
70
  margin-top: 20px;
71
- color: #000;
72
  }
73
  .results-values {
74
  font-size: 18px;
75
  font-weight: bold;
76
  margin-top: 10px;
77
- color: blue;
78
  }
79
  </style>
80
  """,
@@ -82,35 +173,9 @@ with col2:
82
  )
83
 
84
  st.markdown("<div class='analyze-reset-buttons'>", unsafe_allow_html=True)
85
- if st.button("Analyze", key="analyze_button"):
86
- if uploaded_image is not None:
87
- # Preprocess the uploaded image
88
- img = Image.open(uploaded_image)
89
- img = img.resize((64, 64)) # Resize the image to match the model input size
90
- img = np.array(img)
91
- img = tf.keras.applications.resnet50.preprocess_input(img)
92
- img = np.expand_dims(img, axis=0) # Add batch dimension
93
-
94
- # Make predictions using the loaded model
95
- predictions = model_resnet.predict(img)
96
-
97
- # Get the class label with the highest probability
98
- class_label = np.argmax(predictions)
99
- confidence = predictions[0][class_label]
100
-
101
- # Display prediction and confidence
102
- st.markdown(
103
- f"<div class='results-text'>Predicted Class: <span class='results-values'>{class_label}</span></div>",
104
- unsafe_allow_html=True,
105
- )
106
- st.markdown(
107
- f"<div class='results-text'>Confidence Level: <span class='results-values'>{confidence:.2f}</span></div>",
108
- unsafe_allow_html=True,
109
- )
110
- else:
111
- st.warning("Please upload an image before clicking 'Analyze'.")
112
- if st.button("Reset", key="reset_button"):
113
- st.session_state.uploaded_image = None
114
- uploaded_image = None
115
- st.empty() # Clear the results container
116
- st.markdown("</div>", unsafe_allow_html=True)
 
3
  import numpy as np
4
  from PIL import Image
5
 
6
+ # Load the saved models
7
+ model_vgg = tf.keras.models.load_model("best_model.h5")
8
+ classes_vgg = ["No Tumor detected", "Tumor detected"]
9
+
10
+ model_resnet = tf.keras.models.load_model("Tumor_GliMeninPitu_model.h5")
11
+ classes_resnet = ["Glioma", "Meningioma", "No Tumor", "Pituitary"]
12
+
13
+ # Function to preprocess image
14
+ def preprocess_image(uploaded_image, target_size):
15
+ img = Image.open(uploaded_image)
16
+
17
+ # Check if the image is grayscale
18
+ if img.mode == 'L':
19
+ # Convert grayscale to RGB by repeating the single channel
20
+ img = img.convert('RGB')
21
+ st.info("Gray scale image has been converted to three channels.")
22
+
23
+ # Resize the image
24
+ img = img.resize(target_size)
25
+
26
+ # Convert image to numpy array and preprocess
27
+ img_array = np.array(img)
28
+
29
+ # Ensure the image has three channels
30
+ if img_array.shape[-1] == 4:
31
+ img_array = img_array[:, :, :3]
32
+
33
+ img_array = tf.keras.applications.vgg16.preprocess_input(img_array)
34
+
35
+ # Add batch dimension
36
+ img_array = np.expand_dims(img_array, axis=0)
37
+
38
+ return img_array
39
+
40
+ # Function to analyze binary classification
41
+ def analyze_binary(uploaded_image, model, classes):
42
+ if uploaded_image is not None:
43
+ # Preprocess the uploaded image
44
+ img_array = preprocess_image(uploaded_image, (224, 224))
45
+ if img_array is not None:
46
+ # Make predictions using the loaded model
47
+ predictions = model.predict(img_array)
48
+
49
+ # Get the class label with the highest probability
50
+ class_label = np.argmax(predictions)
51
+ pred_class = classes[class_label]
52
+ confidence = predictions[0][class_label]
53
+
54
+ # Display prediction and confidence with stylish colors
55
+ st.markdown(
56
+ f"<div class='results-text' style='color: #009688;'>Prediction: <span class='results-values'>{pred_class}</span></div>",
57
+ unsafe_allow_html=True,
58
+ )
59
+ st.markdown(
60
+ f"<div class='results-text' style='color: #E91E63;'>Confidence Level: <span class='results-values'>{confidence:.2%}</span></div>",
61
+ unsafe_allow_html=True,
62
+ )
63
+ else:
64
+ st.warning("Please upload an image before clicking 'Analyze Binary'.")
65
+
66
+ # Function to analyze multiclass classification
67
+ def analyze_multiclass(uploaded_image, model, classes):
68
+ if uploaded_image is not None:
69
+ # Preprocess the uploaded image
70
+ img_array = preprocess_image(uploaded_image, (224, 224))
71
+ if img_array is not None:
72
+ # Make predictions using the loaded model
73
+ predictions = model.predict(img_array)
74
+
75
+ # Get the class label with the highest probability
76
+ class_label = np.argmax(predictions)
77
+ pred_class = classes[class_label]
78
+ confidence = predictions[0][class_label]
79
+
80
+ # Display prediction and confidence with stylish colors
81
+ st.markdown(
82
+ f"<div class='results-text' style='color: #4CAF50;'>Prediction: <span class='results-values'>{pred_class}</span></div>",
83
+ unsafe_allow_html=True,
84
+ )
85
+ st.markdown(
86
+ f"<div class='results-text' style='color: #FFC107;'>Confidence Level: <span class='results-values'>{confidence:.2%}</span></div>",
87
+ unsafe_allow_html=True,
88
+ )
89
+ else:
90
+ st.warning("Please upload an image before clicking 'Analyze Multiclass'.")
91
 
92
  # Set page configuration and layout
93
  st.set_page_config(
 
96
  layout="wide",
97
  )
98
 
99
+ # Header logo with a colorful border
100
+ st.markdown(
101
+ """
102
+ <style>
103
+ .header-logo {
104
+ display: flex;
105
+ justify-content: center;
106
+ align-items: center;
107
+ margin-bottom: 20px;
108
+ padding: 20px;
109
+ background-color: #2196F3;
110
+ border-radius: 10px;
111
+ color: white;
112
+ }
113
+ </style>
114
+ """,
115
+ unsafe_allow_html=True,
116
+ )
117
+ st.markdown("<div class='header-logos'>", unsafe_allow_html=True)
118
  st.image("RCAIoT_logo.png", use_column_width=False)
119
+ st.markdown("</div>", unsafe_allow_html=True)
120
 
121
  # Main content
122
  col1, col2 = st.columns([1, 1])
 
125
  st.header("Upload Image")
126
  uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"], key="upload_image")
127
  if uploaded_image is not None:
128
+ # Display the uploaded image with fixed initial height and width
129
  st.image(uploaded_image, caption="Uploaded Image", use_column_width=False, width=300)
 
 
 
 
 
 
 
 
 
 
130
 
131
  with col2:
132
  st.header("Results")
 
139
  display: flex;
140
  justify-content: space-between;
141
  align-items: center;
142
+ margin-top: 20px;
143
  }
144
  .analyze-reset-buttons button {
145
  flex: 1;
146
  margin: 10px;
147
  padding: 10px;
148
+ background-color: #2196F3;
149
  color: white;
150
  font-size: 16px;
151
  text-align: center;
 
155
  transition: background-color 0.3s ease;
156
  }
157
  .analyze-reset-buttons button:hover {
158
+ background-color: #1565C0;
159
  }
160
  .results-text {
161
  font-size: 24px;
162
  font-weight: bold;
163
  margin-top: 20px;
 
164
  }
165
  .results-values {
166
  font-size: 18px;
167
  font-weight: bold;
168
  margin-top: 10px;
 
169
  }
170
  </style>
171
  """,
 
173
  )
174
 
175
  st.markdown("<div class='analyze-reset-buttons'>", unsafe_allow_html=True)
176
+
177
+ if st.button("Analyze Binary", key="analyze_binary_button"):
178
+ analyze_binary(uploaded_image, model_vgg, classes_vgg)
179
+
180
+ if st.button("Analyze Multiclass", key="analyze_multiclass_button"):
181
+ analyze_multiclass(uploaded_image, model_resnet, classes_resnet)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
glioma1.jpg ADDED
meningioma.jpg ADDED
notumor.jpg ADDED
pituitary.jpg ADDED