zhawszenthen commited on
Commit
9979851
1 Parent(s): bfe3ab7
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ from PIL import Image
4
+ import numpy as np
5
+
6
+ model_path = "brain_classification.keras"
7
+ model = tf.keras.models.load_model(model_path)
8
+
9
+ labels = ['glioma_tumor', 'meningioma_tumor', 'no_tumor', 'pituitary_tumor']
10
+
11
+ def predict_image(image):
12
+ # Bild vorverarbeiten
13
+ image = Image.fromarray(image.astype('uint8')) # Konvertierung des Numpy-Arrays in ein PIL-Bild
14
+ image = image.resize((224, 224)) # Bildgröße anpassen auf 112x112 Pixel
15
+ image = np.array(image) / 255.0 # In Float konvertieren und normalisieren
16
+
17
+ # Sicherstellen, dass das Bild 3 Farbkanäle hat
18
+ if image.ndim == 2: # Wenn das Bild grau ist, in RGB konvertieren
19
+ image = np.stack((image,)*3, axis=-1)
20
+
21
+ # Vorhersage
22
+ prediction = model.predict(image[None, ...]) # Batch-Dimension hinzufügen
23
+ confidences = {labels[i]: float(prediction[0][i]) for i in range(len(labels))}
24
+ return confidences
25
+
26
+ # Gradio-Interface
27
+ input_image = gr.Image()
28
+ output_text = gr.Textbox(label="Predicted Value")
29
+
30
+ iface = gr.Interface(
31
+ fn=predict_image,
32
+ inputs=input_image,
33
+ outputs=gr.Label(),
34
+ title="Brain tumor classification",
35
+ examples=[], # Ensure this is not a directory path
36
+ description="Please upload an image of your MRI brain scan. The model will classify if you've got a tumor or not. Who needs a radiologist anyway?"
37
+ )
38
+
39
+ iface.launch(share=True)