nielitropar commited on
Commit
4890af3
·
1 Parent(s): e2ed424

Upload 2 files

Browse files
Files changed (2) hide show
  1. app (2).py +126 -0
  2. requirements (1).txt +6 -0
app (2).py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import gradio as gr
3
+ import cv2
4
+ import numpy as np
5
+ from tensorflow.keras.models import load_model
6
+ from tensorflow.keras.applications.vgg16 import preprocess_input
7
+ from tensorflow.keras.preprocessing import image
8
+
9
+ # Loading Models
10
+ braintumor_model = load_model('models/brain_tumor_binary.h5')
11
+
12
+ # Configuring Streamlit
13
+ st.set_page_config(
14
+ page_title="Brain Tumor Prediction App",
15
+ page_icon=":brain:",
16
+ layout="centered",
17
+ )
18
+
19
+ # Streamlit app title and description
20
+ st.title("Brain Tumor Prediction App")
21
+ st.write(
22
+ "Upload an image, and the app will predict whether a brain tumor is present or not."
23
+ )
24
+
25
+ # Add Streamlit sidebar for additional information or options
26
+ st.sidebar.title("About")
27
+ st.sidebar.info(
28
+ "This app uses a trained model to predict brain tumors from images. "
29
+ "The model is based on VGG16 architecture."
30
+ )
31
+
32
+ # Function to preprocess the image
33
+ def preprocess_image(img):
34
+ # If it's a NumPy array, use it directly
35
+ if isinstance(img, np.ndarray):
36
+ img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
37
+ else:
38
+ # Convert Gradio image data to bytes
39
+ img_bytes = img.read()
40
+
41
+ # Convert to NumPy array
42
+ nparr = np.frombuffer(img_bytes, np.uint8)
43
+
44
+ # Decode image
45
+ img_gray = cv2.imdecode(nparr, cv2.IMREAD_GRAYSCALE)
46
+
47
+ # Crop and preprocess the grayscale image
48
+ img_processed = preprocess_imgs([img_gray], (224, 224))
49
+
50
+ return img_processed
51
+
52
+ # Handle binary decision
53
+ def binary_decision(confidence):
54
+ return 1 if confidence >= 0.5 else 0
55
+
56
+ def predict_braintumor(img):
57
+ # Preprocess the image
58
+ img_processed = preprocess_image(img)
59
+
60
+ # Make prediction
61
+ pred = braintumor_model.predict(img_processed)
62
+
63
+ # Handle binary decision
64
+ confidence = pred[0][0]
65
+ return "Brain Tumor Not Found!" if binary_decision(confidence) == 1 else "Brain Tumor Found!"
66
+
67
+ def preprocess_imgs(set_name, img_size):
68
+ set_new = []
69
+ for img in set_name:
70
+ img = cv2.resize(img, dsize=img_size, interpolation=cv2.INTER_CUBIC)
71
+ set_new.append(preprocess_input(img))
72
+ return np.array(set_new)
73
+
74
+ def crop_imgs(set_name, add_pixels_value=0):
75
+ set_new = []
76
+ for img in set_name:
77
+ gray = cv2.GaussianBlur(img, (5, 5), 0)
78
+ thresh = cv2.threshold(gray, 45, 255, cv2.THRESH_BINARY)[1]
79
+ thresh = cv2.erode(thresh, None, iterations=2)
80
+ thresh = cv2.dilate(thresh, None, iterations=2)
81
+ cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
82
+ cnts = cnts[0] if len(cnts) == 2 else cnts[1]
83
+ c = max(cnts, key=cv2.contourArea)
84
+ extLeft = tuple(c[c[:, :, 0].argmin()][0])
85
+ extRight = tuple(c[c[:, :, 0].argmax()][0])
86
+ extTop = tuple(c[c[:, :, 1].argmin()][0])
87
+ extBot = tuple(c[c[:, :, 1].argmax()][0])
88
+ ADD_PIXELS = add_pixels_value
89
+ new_img = img[extTop[1] - ADD_PIXELS:extBot[1] + ADD_PIXELS,
90
+ extLeft[0] - ADD_PIXELS:extRight[0] + ADD_PIXELS].copy()
91
+ set_new.append(new_img)
92
+ return np.array(set_new)
93
+
94
+ # Gradio interface
95
+ iface = gr.Interface(
96
+ fn=predict_braintumor,
97
+ inputs="image",
98
+ outputs="text",
99
+ examples=[["examples/1_no.jpeg"], ["examples/2_no.jpeg"], ["examples/3_no.jpg"], ["examples/Y1.jpg"], ["examples/Y2.jpg"], ["examples/Y3.jpg"]],
100
+ live=True # Allows real-time updates without restarting the app
101
+ )
102
+
103
+ # Display Gradio interface
104
+ iface.launch()
105
+
106
+ # Streamlit components below the Gradio interface
107
+ uploaded_file = st.file_uploader("Choose an MRI image", type=["jpg", "jpeg"])
108
+
109
+ if uploaded_file is not None:
110
+ # Display the uploaded image
111
+ st.image(uploaded_file, caption="Uploaded MRI Image.", use_column_width=True)
112
+
113
+ # Perform prediction when the "Predict" button is clicked
114
+ if st.button("Predict"):
115
+ # Preprocess the image
116
+ img_array = preprocess_image(uploaded_file)
117
+
118
+ # Make prediction
119
+ pred = braintumor_model.predict(img_array)
120
+
121
+ # Handle binary decision
122
+ confidence = pred[0][0]
123
+ result = "Brain Tumor Not Found!" if binary_decision(confidence) == 1 else "Brain Tumor Found!"
124
+
125
+ # Display the prediction result
126
+ st.write(result)
requirements (1).txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ streamlit
2
+ opencv-python
3
+ numpy
4
+ tensorflow
5
+ pillow
6
+ gradio