Spaces:
Sleeping
Sleeping
tmafantiri
commited on
Commit
•
8ecf583
1
Parent(s):
a0e63dd
Update app.py
Browse files
app.py
CHANGED
@@ -1,46 +1,23 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import numpy as np
|
3 |
-
from PIL import Image
|
4 |
-
from keras.models import load_model
|
5 |
-
import json
|
6 |
-
|
7 |
-
# Load the pre-trained model
|
8 |
-
model = load_model('brain_tumor_model.h5')
|
9 |
-
|
10 |
-
# Function to process the image and make predictions
|
11 |
def predict_image(image):
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
img
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
examples = json.load(f)
|
35 |
-
|
36 |
-
# Create the Hugging Face Space
|
37 |
-
iface = gr.Interface(
|
38 |
-
fn=predict_image,
|
39 |
-
inputs=gr.Image(type="pil"),
|
40 |
-
outputs=gr.Textbox(),
|
41 |
-
title="Brain Tumor Detection AI App",
|
42 |
-
description="Upload an image to detect brain tumors.",
|
43 |
-
examples=examples
|
44 |
-
)
|
45 |
-
|
46 |
-
iface.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
def predict_image(image):
|
2 |
+
try:
|
3 |
+
# Resize the image
|
4 |
+
img = image.resize((128, 128))
|
5 |
+
# Convert the image to a NumPy array
|
6 |
+
img = np.array(img)
|
7 |
+
# Check if the image has 3 color channels
|
8 |
+
if img.shape == (128, 128): # If grayscale, convert to RGB
|
9 |
+
img = np.stack((img,) * 3, axis=-1)
|
10 |
+
# Add a batch dimension
|
11 |
+
img = np.expand_dims(img, axis=0) / 255.0 # Normalize the image
|
12 |
+
# Make the prediction
|
13 |
+
prediction = model.predict(img)
|
14 |
+
# Get the predicted class and confidence level
|
15 |
+
predicted_class = np.argmax(prediction)
|
16 |
+
confidence = np.max(prediction) * 100 # Convert to percentage
|
17 |
+
# Return the results
|
18 |
+
if predicted_class == 0:
|
19 |
+
return f'No tumor detected. Confidence: {confidence:.2f}%'
|
20 |
+
else:
|
21 |
+
return f'Tumor detected. Confidence: {confidence:.2f}%'
|
22 |
+
except Exception as e:
|
23 |
+
return f'Error processing image: {str(e)}'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|