vivekm commited on
Commit
fa5cbf2
1 Parent(s): 7acbd29

Upload 4 files

Browse files
Files changed (4) hide show
  1. app.py +72 -0
  2. braintumour_model.h5 +3 -0
  3. labels.txt +4 -0
  4. requirements.txt +0 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from keras.models import load_model
3
+ from PIL import Image, ImageOps
4
+ import numpy as np
5
+ # Disable scientific notation for clarity
6
+ np.set_printoptions(suppress=True)
7
+
8
+ # Load the model
9
+ model = load_model("braintumour_model.h5", compile=False)
10
+
11
+ # Load the labels
12
+ class_names = open("labels.txt", "r").readlines()
13
+
14
+ # Function to preprocess the image
15
+ def preprocess_image(image):
16
+ # resizing the image to be at least 224x224 and then cropping from the center
17
+ size = (224, 224)
18
+ image = ImageOps.fit(image, size, Image.Resampling.LANCZOS)
19
+
20
+ # turn the image into a numpy array
21
+ image_array = np.asarray(image)
22
+
23
+ # Normalize the image
24
+ normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1
25
+
26
+ # Return the preprocessed image
27
+ return normalized_image_array
28
+
29
+ # Function to predict the image class
30
+ def predict_image_class(image):
31
+ # Create the array of the right shape to feed into the keras model
32
+ data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
33
+
34
+ # Load the image into the array
35
+ data[0] = image
36
+
37
+ # Predict the model
38
+ prediction = model.predict(data)
39
+ index = np.argmax(prediction)
40
+ class_name = class_names[index]
41
+ confidence_score = prediction[0][index]
42
+
43
+ return class_name[2:], round(confidence_score, 2)*100
44
+
45
+ # Streamlit app
46
+ def main():
47
+ # Set page title and icon
48
+ st.set_page_config(page_title="Brain Tumour Classifier", page_icon="🧠")
49
+
50
+ st.title("Brain Tumour Classifier")
51
+
52
+ # Upload image file
53
+ uploaded_file = st.file_uploader("Upload the MRI", type=["jpg", "jpeg", "png"])
54
+
55
+ if uploaded_file is not None:
56
+ image = Image.open(uploaded_file).convert("RGB")
57
+ st.image(image, caption="Uploaded Image", use_column_width=True)
58
+
59
+ preprocessed_image = preprocess_image(image)
60
+
61
+ # Predict the image class
62
+ class_name, confidence_score = predict_image_class(preprocessed_image)
63
+
64
+ # Display the prediction
65
+ st.subheader("RESULT")
66
+ st.write("Class:", "**" + class_name + "**")
67
+ st.write("Prediction Probability:", "**" + str(confidence_score) + "%**")
68
+
69
+
70
+ # Run the app
71
+ if __name__ == "__main__":
72
+ main()
braintumour_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:378e3392f9db5be6a8055a7a691a670c28275b5d09f6402d2b1b59ee3c205c37
3
+ size 2453432
labels.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ 0 Glioma Tumour
2
+ 1 Meningioma Tumour
3
+ 2 No Tumor
4
+ 3 Pituitary Tumour
requirements.txt ADDED
Binary file (5.83 kB). View file