Spaces:
Runtime error
Runtime error
LovnishVerma
commited on
Commit
•
a00b28d
1
Parent(s):
e769983
Update app.py
Browse files
app.py
CHANGED
@@ -5,14 +5,11 @@ 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 |
-
import os
|
9 |
|
10 |
# Loading Models
|
11 |
braintumor_model = load_model('models/brain_tumor_binary.h5')
|
12 |
|
13 |
# Configuring Streamlit
|
14 |
-
UPLOAD_FOLDER = 'static/uploads'
|
15 |
-
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
|
16 |
st.set_page_config(page_title="Brain Tumor Prediction App", page_icon=":brain:")
|
17 |
|
18 |
def preprocess_imgs(set_name, img_size):
|
@@ -22,59 +19,26 @@ def preprocess_imgs(set_name, img_size):
|
|
22 |
set_new.append(preprocess_input(img))
|
23 |
return np.array(set_new)
|
24 |
|
25 |
-
def crop_imgs(set_name, add_pixels_value=0):
|
26 |
-
set_new = []
|
27 |
-
for img in set_name:
|
28 |
-
gray = cv2.GaussianBlur(img, (5, 5), 0)
|
29 |
-
thresh = cv2.threshold(gray, 45, 255, cv2.THRESH_BINARY)[1]
|
30 |
-
thresh = cv2.erode(thresh, None, iterations=2)
|
31 |
-
thresh = cv2.dilate(thresh, None, iterations=2)
|
32 |
-
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
33 |
-
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
|
34 |
-
c = max(cnts, key=cv2.contourArea)
|
35 |
-
extLeft = tuple(c[c[:, :, 0].argmin()][0])
|
36 |
-
extRight = tuple(c[c[:, :, 0].argmax()][0])
|
37 |
-
extTop = tuple(c[c[:, :, 1].argmin()][0])
|
38 |
-
extBot = tuple(c[c[:, :, 1].argmax()][0])
|
39 |
-
ADD_PIXELS = add_pixels_value
|
40 |
-
new_img = img[extTop[1] - ADD_PIXELS:extBot[1] + ADD_PIXELS,
|
41 |
-
extLeft[0] - ADD_PIXELS:extRight[0] + ADD_PIXELS].copy()
|
42 |
-
set_new.append(new_img)
|
43 |
-
return np.array(set_new)
|
44 |
-
|
45 |
-
# Function to preprocess the image
|
46 |
-
def preprocess_image(file_path):
|
47 |
-
img = image.load_img(file_path, target_size=(200, 200))
|
48 |
-
img_array = image.img_to_array(img)
|
49 |
-
img_array = np.expand_dims(img_array, axis=0)
|
50 |
-
img_array /= 255.0 # Normalize the image
|
51 |
-
return img_array
|
52 |
-
|
53 |
# Handle binary decision
|
54 |
def binary_decision(confidence):
|
55 |
return 1 if confidence >= 0.5 else 0
|
56 |
|
57 |
def predict_braintumor(img):
|
58 |
-
#
|
59 |
-
if
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
# Convert Gradio image data to bytes
|
64 |
img_bytes = img.read()
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
img_gray = cv2.imread(file_path, cv2.IMREAD_GRAYSCALE)
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
74 |
|
75 |
# Crop and preprocess the grayscale image
|
76 |
-
img_processed =
|
77 |
-
img_processed = preprocess_imgs(img_processed, (224, 224))
|
78 |
|
79 |
# Make prediction
|
80 |
pred = braintumor_model.predict(img_processed)
|
@@ -83,7 +47,6 @@ def predict_braintumor(img):
|
|
83 |
confidence = pred[0][0]
|
84 |
return "Brain Tumor Found!" if binary_decision(confidence) == 1 else "Brain Tumor Not Found!"
|
85 |
|
86 |
-
|
87 |
def main():
|
88 |
st.title("Brain Tumor Prediction App")
|
89 |
|
|
|
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(page_title="Brain Tumor Prediction App", page_icon=":brain:")
|
14 |
|
15 |
def preprocess_imgs(set_name, img_size):
|
|
|
19 |
set_new.append(preprocess_input(img))
|
20 |
return np.array(set_new)
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
# Handle binary decision
|
23 |
def binary_decision(confidence):
|
24 |
return 1 if confidence >= 0.5 else 0
|
25 |
|
26 |
def predict_braintumor(img):
|
27 |
+
# If it's a NumPy array, use it directly
|
28 |
+
if isinstance(img, np.ndarray):
|
29 |
+
img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
30 |
+
else:
|
|
|
31 |
# Convert Gradio image data to bytes
|
32 |
img_bytes = img.read()
|
33 |
|
34 |
+
# Convert to NumPy array
|
35 |
+
nparr = np.frombuffer(img_bytes, np.uint8)
|
|
|
|
|
36 |
|
37 |
+
# Decode image
|
38 |
+
img_gray = cv2.imdecode(nparr, cv2.IMREAD_GRAYSCALE)
|
|
|
39 |
|
40 |
# Crop and preprocess the grayscale image
|
41 |
+
img_processed = preprocess_imgs([img_gray], (224, 224))
|
|
|
42 |
|
43 |
# Make prediction
|
44 |
pred = braintumor_model.predict(img_processed)
|
|
|
47 |
confidence = pred[0][0]
|
48 |
return "Brain Tumor Found!" if binary_decision(confidence) == 1 else "Brain Tumor Not Found!"
|
49 |
|
|
|
50 |
def main():
|
51 |
st.title("Brain Tumor Prediction App")
|
52 |
|