Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,51 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import tensorflow as tf
|
| 3 |
from PIL import Image
|
| 4 |
-
import numpy as np
|
| 5 |
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
model = tf.keras.models.load_model(
|
| 9 |
|
| 10 |
-
|
| 11 |
-
# Bild vorverarbeiten
|
| 12 |
-
image = Image.fromarray(image.astype('uint8')).convert('RGB')
|
| 13 |
-
image = image.resize((150, 150)) # Anpassung der Größe an das Modell
|
| 14 |
-
image = np.array(image) / 255.0 # Normalisieren
|
| 15 |
-
image = np.expand_dims(image, axis=0) # Hinzufügen der Batch-Dimension
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
# Gradio
|
| 25 |
input_image = gr.Image()
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
)
|
| 33 |
-
|
| 34 |
-
# Interface starten
|
| 35 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from PIL import Image
|
|
|
|
| 3 |
|
| 4 |
+
# Load your custom regression model
|
| 5 |
+
model_path = "transferlearning_pokemon.keras"
|
| 6 |
|
| 7 |
+
#model.load_weights(model_path)
|
| 8 |
+
model = tf.keras.models.load_model(model_path)
|
| 9 |
|
| 10 |
+
labels = ['Abra', 'Ditto', 'Gengar']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
def predict_pokemons(image):
|
| 13 |
+
# Preprocess the image
|
| 14 |
+
print(type(image))
|
| 15 |
+
image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
|
| 16 |
+
image = image.resize((150, 150)) # Resize the image as per the model's input requirement
|
| 17 |
+
image = np.array(image)
|
| 18 |
+
image = np.expand_dims(image, axis=0) # Add batch dimension
|
| 19 |
+
|
| 20 |
+
# Predict
|
| 21 |
+
predictions = model.predict(image)
|
| 22 |
+
|
| 23 |
+
# Convert the logits to probabilities using softmax
|
| 24 |
+
probabilities = tf.nn.softmax(predictions[0]).numpy()
|
| 25 |
+
|
| 26 |
+
# Create a dictionary to hold the probabilities for each class
|
| 27 |
+
results = {class_names[i]: float(np.round(probabilities[i], 2)) for i in range(len(class_names))}
|
| 28 |
+
return results
|
| 29 |
|
| 30 |
+
# Define regression function
|
| 31 |
+
def predict_regression(image):
|
| 32 |
+
# Preprocess image
|
| 33 |
+
image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
|
| 34 |
+
image = image.resize((150, 150))
|
| 35 |
+
image = np.array(image)
|
| 36 |
+
print(image.shape)
|
| 37 |
+
# Predict
|
| 38 |
+
prediction = model.predict(image[None, ...]) # Assuming single regression value
|
| 39 |
+
print(prediction)
|
| 40 |
+
confidences = str(prediction)
|
| 41 |
+
return confidences
|
| 42 |
|
| 43 |
+
# Create Gradio interface
|
| 44 |
input_image = gr.Image()
|
| 45 |
+
output_text = gr.Textbox(label="Predicted Value")
|
| 46 |
+
interface = gr.Interface(fn=predict_pokemons,
|
| 47 |
+
inputs=input_image,
|
| 48 |
+
outputs=gr.Label(),
|
| 49 |
+
examples=["images/images_0.png", "images/images_1.png", "images/images_2.png", "images/images_3.png"],
|
| 50 |
+
description="A simple pokemon classification model based on Xception and Pokemon Images (https://www.kaggle.com/datasets/mikoajkolman/pokemon-images-first-generation17000-files).")
|
| 51 |
+
interface.launch()
|
|
|
|
|
|
|
|
|