Spaces:
Sleeping
Sleeping
zhawszenthen
commited on
Commit
•
357e4b2
1
Parent(s):
d265125
update app
Browse files
app.py
CHANGED
@@ -3,21 +3,29 @@ 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 |
-
|
12 |
-
|
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
|
15 |
-
image = np.array(image) /
|
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))}
|
@@ -31,9 +39,9 @@ iface = gr.Interface(
|
|
31 |
fn=predict_image,
|
32 |
inputs=input_image,
|
33 |
outputs=gr.Label(),
|
34 |
-
title="Brain
|
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)
|
|
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
5 |
|
6 |
+
# Laden des Modells
|
7 |
model_path = "brain_classification.keras"
|
8 |
model = tf.keras.models.load_model(model_path)
|
9 |
|
10 |
+
# Klassenlabels
|
11 |
labels = ['glioma_tumor', 'meningioma_tumor', 'no_tumor', 'pituitary_tumor']
|
12 |
|
13 |
+
# Bildvorverarbeitung
|
14 |
+
def preprocess_image(image):
|
15 |
image = Image.fromarray(image.astype('uint8')) # Konvertierung des Numpy-Arrays in ein PIL-Bild
|
16 |
+
image = image.resize((224, 224)) # Bildgröße anpassen auf 224x224 Pixel
|
17 |
+
image = np.array(image) / 127.5 - 1.0 # In Float konvertieren und normalisieren auf [-1, 1]
|
18 |
|
19 |
# Sicherstellen, dass das Bild 3 Farbkanäle hat
|
20 |
if image.ndim == 2: # Wenn das Bild grau ist, in RGB konvertieren
|
21 |
image = np.stack((image,)*3, axis=-1)
|
22 |
|
23 |
+
return image
|
24 |
+
|
25 |
+
# Vorhersagefunktion
|
26 |
+
def predict_image(image):
|
27 |
+
image = preprocess_image(image) # Verwende die aktualisierte Vorverarbeitung
|
28 |
+
|
29 |
# Vorhersage
|
30 |
prediction = model.predict(image[None, ...]) # Batch-Dimension hinzufügen
|
31 |
confidences = {labels[i]: float(prediction[0][i]) for i in range(len(labels))}
|
|
|
39 |
fn=predict_image,
|
40 |
inputs=input_image,
|
41 |
outputs=gr.Label(),
|
42 |
+
title="Brain Tumor Classification",
|
|
|
43 |
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?"
|
44 |
)
|
45 |
|
46 |
+
# Starten der Gradio-Oberfläche
|
47 |
iface.launch(share=True)
|