File size: 4,211 Bytes
3c4d877
 
 
 
 
 
 
 
 
e7a8b14
3c4d877
 
e7a8b14
 
 
 
 
 
 
 
 
 
 
3c4d877
e7a8b14
 
 
 
 
 
 
 
6a8052b
a00b28d
 
 
 
e769983
 
ca99111
a00b28d
 
e769983
a00b28d
 
3c4d877
 
3ab8166
6a8052b
 
 
 
 
 
 
 
 
 
3c4d877
 
 
 
 
1db9a6e
cd196c7
7f9fd20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7a8b14
 
7f9fd20
e7a8b14
 
7f9fd20
e7a8b14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import streamlit as st
import gradio as gr
import cv2
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.applications.vgg16 import preprocess_input
from tensorflow.keras.preprocessing import image

# Loading Models
braintumor_model = load_model('models/brain_tumor_binary.h5')

# Configuring Streamlit
st.set_page_config(
    page_title="Brain Tumor Prediction App",
    page_icon=":brain:",
    layout="centered",
)

# Streamlit app title and description
st.title("Brain Tumor Prediction App")
st.write(
    "Upload an image, and the app will predict whether a brain tumor is present or not."
)

# Add Streamlit sidebar for additional information or options
st.sidebar.title("About")
st.sidebar.info(
    "This app uses a trained model to predict brain tumors from images. "
    "The model is based on VGG16 architecture."
)

# Function to preprocess the image
def preprocess_image(img):
    # If it's a NumPy array, use it directly
    if isinstance(img, np.ndarray):
        img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    else:
        # Convert Gradio image data to bytes
        img_bytes = img.read()

        # Convert to NumPy array
        nparr = np.frombuffer(img_bytes, np.uint8)

        # Decode image
        img_gray = cv2.imdecode(nparr, cv2.IMREAD_GRAYSCALE)

    # Crop and preprocess the grayscale image
    img_processed = preprocess_imgs([img_gray], (224, 224))

    return img_processed

# Handle binary decision
def binary_decision(confidence):
    return 1 if confidence >= 0.5 else 0

def predict_braintumor(img):
    # Preprocess the image
    img_processed = preprocess_image(img)

    # Make prediction
    pred = braintumor_model.predict(img_processed)

    # Handle binary decision
    confidence = pred[0][0]
    return "Brain Tumor Not Found!" if binary_decision(confidence) == 1 else "Brain Tumor Found!"

def preprocess_imgs(set_name, img_size):
    set_new = []
    for img in set_name:
        img = cv2.resize(img, dsize=img_size, interpolation=cv2.INTER_CUBIC)
        set_new.append(preprocess_input(img))
    return np.array(set_new)

def crop_imgs(set_name, add_pixels_value=0):
    set_new = []
    for img in set_name:
        gray = cv2.GaussianBlur(img, (5, 5), 0)
        thresh = cv2.threshold(gray, 45, 255, cv2.THRESH_BINARY)[1]
        thresh = cv2.erode(thresh, None, iterations=2)
        thresh = cv2.dilate(thresh, None, iterations=2)
        cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        cnts = cnts[0] if len(cnts) == 2 else cnts[1]
        c = max(cnts, key=cv2.contourArea)
        extLeft = tuple(c[c[:, :, 0].argmin()][0])
        extRight = tuple(c[c[:, :, 0].argmax()][0])
        extTop = tuple(c[c[:, :, 1].argmin()][0])
        extBot = tuple(c[c[:, :, 1].argmax()][0])
        ADD_PIXELS = add_pixels_value
        new_img = img[extTop[1] - ADD_PIXELS:extBot[1] + ADD_PIXELS,
                      extLeft[0] - ADD_PIXELS:extRight[0] + ADD_PIXELS].copy()
        set_new.append(new_img)
    return np.array(set_new)

# Gradio interface
iface = gr.Interface(
    fn=predict_braintumor,
    inputs="image",
    outputs="text",
    examples=[["examples/1_no.jpeg"], ["examples/2_no.jpeg"], ["examples/3_no.jpg"], ["examples/Y1.jpg"], ["examples/Y2.jpg"], ["examples/Y3.jpg"]],
    live=True  # Allows real-time updates without restarting the app
)

# Display Gradio interface
iface.launch()

# Streamlit components below the Gradio interface
uploaded_file = st.file_uploader("Choose an MRI image", type=["jpg", "jpeg"])

if uploaded_file is not None:
    # Display the uploaded image
    st.image(uploaded_file, caption="Uploaded MRI Image.", use_column_width=True)
    
    # Perform prediction when the "Predict" button is clicked
    if st.button("Predict"):
        # Preprocess the image
        img_array = preprocess_image(uploaded_file)

        # Make prediction
        pred = braintumor_model.predict(img_array)

        # Handle binary decision
        confidence = pred[0][0]
        result = "Brain Tumor Not Found!" if binary_decision(confidence) == 1 else "Brain Tumor Found!"

        # Display the prediction result
        st.write(result)