-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbrain.py
109 lines (43 loc) · 1.92 KB
/
brain.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import streamlit as st
from tensorflow.keras.models import load_model
import numpy as np
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from PIL import Image
st.header("Brain Tumor Detection")
st.caption("Upload an image. ")
st.caption("The application will infer the one label out of 4 labels: 'no_tumor', 'pituitary_tumor', 'meningioma_tumor', 'glioma_tumor'.")
st.caption("Warning: Do not click Recognize button before uploading image. It will result in error.")
model = load_model("Brain_Tumor_Image_Classification_Model(2).h5",compile=False)
model.compile(
loss = 'categorical_crossentropy',
optimizer = 'Adam',
metrics = ['accuracy']
)
# Define the class names
class_names = ['no_tumor', 'pituitary_tumor', 'meningioma_tumor', 'glioma_tumor']
# Fxn
@st.cache_data
def load_image(image_file):
img = Image.open(image_file)
return img
imgpath = st.file_uploader("Choose a file", type =['png', 'jpeg', 'jpg'])
if imgpath is not None:
img = load_image(imgpath )
st.image(img, width=224)
def predict_label(image2):
imgLoaded = load_img(image2, target_size=(224, 224))
# Convert the image to an array
img_array = img_to_array(imgLoaded) #print(img_array)
#print(img_array.shape)
img_array = np.reshape(img_array, (1, 224, 224, 3))
# Get the model predictions
predictions = model.predict(img_array)
#print("predictions:", predictions)
# Get the class index with the highest predicted probability
class_index = predictions.argmax()
# Get the predicted class label
predicted_label = class_names[class_index]
return predicted_label
if st.button('Recognise'):
predicted_label = predict_label(imgpath)
st.write("The image is predicted to be '{}'.".format(predicted_label))