File size: 1,954 Bytes
3c4d877
 
 
 
 
 
 
 
 
934e107
3c4d877
 
 
 
3ab8166
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a8052b
a00b28d
 
 
 
e769983
 
ca99111
a00b28d
 
e769983
a00b28d
 
3c4d877
 
3ab8166
6a8052b
 
 
 
 
 
 
 
 
 
3c4d877
 
 
 
 
1db9a6e
cd196c7
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
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:")

# Customizing Gradio appearance
gr.set_config(
    display_name=title,
    interface_color="rgba(255, 99, 71, 0.8)",  # Adjust color as needed
    live=True
)

# Configuring Gradio
iface = gr.Interface(
    fn="predict_braintumor",
    inputs="image",
    outputs="text",
    live=True,
    interpretation="default",
    examples=[["examples/1_no.jpeg"], ["examples/2_no.jpeg"], ["examples/3_no.jpg"], ["examples/Y1.jpg"], ["examples/Y2.jpg"], ["examples/Y3.jpg"]],
    title=title,
    description=description,
    article=article
)
iface.launch()

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!"