Spaces:
Runtime error
Runtime error
LovnishVerma
commited on
Commit
•
1db9a6e
1
Parent(s):
9e85036
Update app.py
Browse files
app.py
CHANGED
@@ -47,21 +47,19 @@ def crop_imgs(set_name, add_pixels_value=0):
|
|
47 |
return np.array(set_new)
|
48 |
|
49 |
# Function to preprocess the image
|
50 |
-
def preprocess_image(
|
51 |
-
|
52 |
-
img = image.array_to_img(img_array, target_size=(200, 200))
|
53 |
img_array = image.img_to_array(img)
|
54 |
img_array = np.expand_dims(img_array, axis=0)
|
55 |
img_array /= 255.0 # Normalize the image
|
56 |
return img_array
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
img_gray = cv2.cvtColor(img_array, cv2.COLOR_BGR2GRAY)
|
65 |
|
66 |
# Crop and preprocess the grayscale image
|
67 |
img_processed = crop_imgs([img_gray])
|
@@ -71,9 +69,8 @@ def predict_braintumor(img_input):
|
|
71 |
pred = braintumor_model.predict(img_processed)
|
72 |
|
73 |
# Handle binary decision
|
74 |
-
|
75 |
-
|
76 |
-
return pred
|
77 |
|
78 |
def main():
|
79 |
st.title("Brain Tumor Prediction App")
|
@@ -85,48 +82,36 @@ def main():
|
|
85 |
st.write("")
|
86 |
st.write("Classifying...")
|
87 |
|
88 |
-
# Read the contents of the uploaded file
|
89 |
file_contents = uploaded_file.read()
|
90 |
-
uploaded_array = np.asarray(bytearray(file_contents), dtype=np.uint8)
|
91 |
-
uploaded_img = cv2.imdecode(uploaded_array, -1)
|
92 |
|
93 |
# Save the uploaded file
|
94 |
-
|
95 |
-
|
96 |
-
file_path = os.path.join(UPLOAD_FOLDER, filename)
|
97 |
|
98 |
-
|
99 |
-
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
else:
|
104 |
-
# Make prediction for Gradio (direct input of image)
|
105 |
-
result = predict_braintumor(uploaded_img)
|
106 |
|
107 |
-
# Handle binary decision
|
108 |
-
result_text = "Brain Tumor Found!" if result == 1 else "Brain Tumor Not Found!"
|
109 |
-
|
110 |
# Display prediction
|
111 |
st.subheader("Prediction:")
|
112 |
-
|
|
|
|
|
|
|
113 |
|
114 |
if __name__ == "__main__":
|
115 |
-
# Streamlit
|
116 |
-
|
117 |
-
main()
|
118 |
|
119 |
-
# Gradio
|
120 |
iface = gr.Interface(
|
121 |
fn=predict_braintumor,
|
122 |
inputs="image",
|
123 |
outputs="text",
|
124 |
-
examples
|
125 |
-
["examples/1 no.jpeg"],
|
126 |
-
["examples/2 no.jpeg"],
|
127 |
-
["examples/3 yes.jpg"],
|
128 |
-
["examples/4 yes.jpg"]
|
129 |
-
],
|
130 |
enable_queue=True
|
131 |
)
|
132 |
iface.launch()
|
|
|
47 |
return np.array(set_new)
|
48 |
|
49 |
# Function to preprocess the image
|
50 |
+
def preprocess_image(file_path):
|
51 |
+
img = image.load_img(file_path, target_size=(200, 200))
|
|
|
52 |
img_array = image.img_to_array(img)
|
53 |
img_array = np.expand_dims(img_array, axis=0)
|
54 |
img_array /= 255.0 # Normalize the image
|
55 |
return img_array
|
56 |
|
57 |
+
# Handle binary decision
|
58 |
+
def binary_decision(confidence):
|
59 |
+
return 1 if confidence >= 0.5 else 0
|
60 |
+
|
61 |
+
def predict_braintumor(img_path):
|
62 |
+
img_gray = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
|
|
|
63 |
|
64 |
# Crop and preprocess the grayscale image
|
65 |
img_processed = crop_imgs([img_gray])
|
|
|
69 |
pred = braintumor_model.predict(img_processed)
|
70 |
|
71 |
# Handle binary decision
|
72 |
+
confidence = pred[0][0]
|
73 |
+
return binary_decision(confidence)
|
|
|
74 |
|
75 |
def main():
|
76 |
st.title("Brain Tumor Prediction App")
|
|
|
82 |
st.write("")
|
83 |
st.write("Classifying...")
|
84 |
|
85 |
+
# Read the contents of the uploaded file
|
86 |
file_contents = uploaded_file.read()
|
|
|
|
|
87 |
|
88 |
# Save the uploaded file
|
89 |
+
filename = secure_filename(uploaded_file.name)
|
90 |
+
file_path = os.path.join(UPLOAD_FOLDER, filename)
|
|
|
91 |
|
92 |
+
with open(file_path, "wb") as f:
|
93 |
+
f.write(file_contents)
|
94 |
|
95 |
+
# Make prediction
|
96 |
+
result = predict_braintumor(file_path)
|
|
|
|
|
|
|
97 |
|
|
|
|
|
|
|
98 |
# Display prediction
|
99 |
st.subheader("Prediction:")
|
100 |
+
if result == 1:
|
101 |
+
st.success("Brain Tumor Found!")
|
102 |
+
else:
|
103 |
+
st.success("Brain Tumor Not Found!")
|
104 |
|
105 |
if __name__ == "__main__":
|
106 |
+
# Streamlit app
|
107 |
+
main()
|
|
|
108 |
|
109 |
+
# Gradio interface
|
110 |
iface = gr.Interface(
|
111 |
fn=predict_braintumor,
|
112 |
inputs="image",
|
113 |
outputs="text",
|
114 |
+
examples=[["examples/1_no.jpg"], ["examples/2_no.jpg"], ["examples/3_yes.jpg"], ["examples/4_yes.jpg"]],
|
|
|
|
|
|
|
|
|
|
|
115 |
enable_queue=True
|
116 |
)
|
117 |
iface.launch()
|