File size: 1,501 Bytes
8ee033e
 
 
 
26360ef
8ee033e
26360ef
8ee033e
 
26360ef
 
 
8ee033e
26360ef
8ee033e
26360ef
 
 
 
 
8ee033e
26360ef
 
 
8ee033e
26360ef
 
 
8ee033e
26360ef
 
 
 
8ee033e
26360ef
 
 
 
dbebec7
a19deb7
 
8ee033e
a19deb7
 
8ee033e
a19deb7
26360ef
 
 
8ee033e
 
a19deb7
d9130c8
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
import gradio as gr
import numpy as np
from PIL import Image
from keras.models import load_model
import json

# Load your pre-trained model
model = load_model('brain_tumor_model.h5')

# Load examples from JSON file
with open('examples.json', 'r') as f:
    examples_data = json.load(f)

examples = [[example['image']] for example in examples_data]

def predict_image(image):
    try:
        # Resize and preprocess the image
        img = image.resize((128, 128))
        img = np.array(img)

        # Check and convert grayscale to RGB
        if img.shape == (128, 128):  # Grayscale
            img = np.stack((img,) * 3, axis=-1)

        # Normalize the image
        img = img / 255.0
        img = np.expand_dims(img, axis=0)

        # Make the prediction
        prediction = model.predict(img)
        predicted_class = np.argmax(prediction)
        confidence = np.max(prediction)

        return f'{"No tumor detected" if predicted_class == 0 else "Tumor detected"}. Confidence: {confidence:.2f}'
    
    except Exception as e:
        return f"Error: {str(e)}"

# Create the Gradio interface
iface = gr.Interface(
    fn=predict_image,
    inputs=gr.Image(type="pil"),
    outputs=gr.Textbox(),
    title="Brain Tumor Detection AI App",
    description="Upload an image to detect brain tumors.",
    theme="monochrome",
    flagging_options=["Incorrect Diagnosis", "Image Not Clear", "Model Error"],
    examples=examples
)

# Launch the interface
iface.launch(share=True, debug=True)