Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,42 +3,40 @@ import tensorflow as tf
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
model_path = "pokemon-model_transferlearning.keras"
|
| 9 |
model = tf.keras.models.load_model(model_path)
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
import tensorflow as tf
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
def
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
image = image.resize((150, 150)) # Resize the image to 150x150
|
| 20 |
-
image = np.array(image)
|
| 21 |
-
image = np.expand_dims(image, axis=0) # Add batch dimension
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
probabilities_dict = {pokemon_class: round(float(probability), 2) for pokemon_class, probability in zip(pokemon_classes, probabilities[0])}
|
| 32 |
|
| 33 |
-
return
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
input_image = gr.Image()
|
| 37 |
iface = gr.Interface(
|
| 38 |
-
fn=
|
| 39 |
-
inputs=
|
| 40 |
-
outputs=
|
| 41 |
-
live=True
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
# Pfad zum gespeicherten Modell
|
|
|
|
| 7 |
model_path = "pokemon-model_transferlearning.keras"
|
| 8 |
model = tf.keras.models.load_model(model_path)
|
| 9 |
|
| 10 |
+
# Definieren der Klassennamen
|
| 11 |
+
labels = ['Articuno', 'Bulbasaur', 'Charmander']
|
|
|
|
| 12 |
|
| 13 |
+
# Funktion zur Klassifizierung
|
| 14 |
+
def classify_pokemon(image):
|
| 15 |
+
if image is None:
|
| 16 |
+
return {"Error": "No image uploaded"}
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Bildvorverarbeitung
|
| 19 |
+
image = Image.fromarray(image).resize((150, 150))
|
| 20 |
+
image = np.array(image) / 255.0
|
| 21 |
+
image = np.expand_dims(image, axis=0)
|
| 22 |
|
| 23 |
+
# Vorhersage
|
| 24 |
+
prediction = model.predict(image)
|
| 25 |
+
predicted_class = np.argmax(prediction[0])
|
| 26 |
+
confidence = np.max(prediction[0])
|
| 27 |
|
| 28 |
+
# Konfidenzwerte
|
| 29 |
+
confidences = {labels[i]: np.round(float(prediction[0][i]), 2) for i in range(len(labels))}
|
|
|
|
| 30 |
|
| 31 |
+
return confidences, f"Predicted: {labels[predicted_class]}, Confidence: {confidence:.2f}"
|
| 32 |
|
| 33 |
+
# Erstellen einer Gradio-Schnittstelle
|
|
|
|
| 34 |
iface = gr.Interface(
|
| 35 |
+
fn=classify_pokemon,
|
| 36 |
+
inputs=gr.Image(),
|
| 37 |
+
outputs=["label", "text"],
|
| 38 |
+
live=True
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Starten der Schnittstelle
|
| 42 |
+
iface.launch()
|