tmafantiri commited on
Commit
8ecf583
1 Parent(s): a0e63dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -45
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
- # Resize the image
13
- img = image.resize((128, 128))
14
- # Convert the image to a NumPy array
15
- img = np.array(img)
16
- # Check if the image has 3 color channels
17
- if img.shape == (128, 128): # If grayscale, convert to RGB
18
- img = np.stack((img,) * 3, axis=-1)
19
- # Add a batch dimension
20
- img = np.expand_dims(img, axis=0) / 255.0 # Normalize the image
21
- # Make the prediction
22
- prediction = model.predict(img)
23
- # Get the predicted class and confidence level
24
- predicted_class = np.argmax(prediction)
25
- confidence = np.max(prediction) * 100 # Convert to percentage
26
- # Return the results
27
- if predicted_class == 0:
28
- return f'No tumor detected. Confidence: {confidence:.2f}%'
29
- else:
30
- return f'Tumor detected. Confidence: {confidence:.2f}%'
31
-
32
- # Load examples from the JSON file
33
- with open('examples.json') as f:
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)}'