Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,39 @@
|
|
| 1 |
-
|
| 2 |
import gradio as gr
|
| 3 |
import tensorflow as tf
|
| 4 |
-
from PIL import Image
|
| 5 |
import numpy as np
|
|
|
|
| 6 |
|
| 7 |
-
labels = ['Haunter', 'Gengar', 'Ditto', 'Vulpix']
|
| 8 |
-
|
| 9 |
-
def predict_pokemon_type(uploaded_file):
|
| 10 |
-
if uploaded_file is None:
|
| 11 |
-
return "No file uploaded.", None, "No prediction"
|
| 12 |
-
|
| 13 |
-
model = tf.keras.models.load_model('pokemon-model_2_transferlearning.keras')
|
| 14 |
-
|
| 15 |
-
# Load the image from the file path
|
| 16 |
-
with Image.open(uploaded_file) as img:
|
| 17 |
-
img = img.resize((150, 150))
|
| 18 |
-
img_array = np.array(img)
|
| 19 |
-
|
| 20 |
-
prediction = model.predict(np.expand_dims(img_array, axis=0))
|
| 21 |
-
confidences = {labels[i]: np.round(float(prediction[0][i]), 2) for i in range(len(labels))}
|
| 22 |
-
|
| 23 |
-
# Identify the most confident prediction
|
| 24 |
-
confidences = {labels[i]: np.round(float(prediction[0][i]), 2) for i in range(len(labels))}
|
| 25 |
-
|
| 26 |
-
return img, confidences
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
iface = gr.Interface(
|
| 30 |
-
fn=
|
| 31 |
-
inputs=
|
| 32 |
-
outputs=
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
iface.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
model_path = "pokemon-model_2_transferlearning.keras"
|
| 8 |
+
model = tf.keras.models.load_model(model_path)
|
| 9 |
+
|
| 10 |
+
def predict_pokemon(image):
|
| 11 |
+
# Preprocess image
|
| 12 |
+
print(type(image))
|
| 13 |
+
image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
|
| 14 |
+
image = image.resize((150, 150)) #resize the image to 150x150
|
| 15 |
+
image = np.array(image)
|
| 16 |
+
image = np.expand_dims(image, axis=0) # same as image[None, ...]
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
prediction = model.predict(image)
|
| 20 |
+
|
| 21 |
+
# Apply softmax to get probabilities for each class
|
| 22 |
+
prediction = tf.nn.softmax(prediction)
|
| 23 |
+
|
| 24 |
+
# Create a dictionary with the probabilities for each Pokemon
|
| 25 |
+
evee = np.round(float(prediction[0][0]), 2)
|
| 26 |
+
farfetched = np.round(float(prediction[0][1]), 2)
|
| 27 |
+
graveler = np.round(float(prediction[0][2]), 2)
|
| 28 |
+
venonta = np.round(float(prediction[0][3]), 2)
|
| 29 |
+
|
| 30 |
+
return {'Evee': evee, 'Farfetched': farfetched, 'Graveler': graveler, 'Venonta': venonta}
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
input_image = gr.Image()
|
| 34 |
iface = gr.Interface(
|
| 35 |
+
fn=predict_pokemon,
|
| 36 |
+
inputs=input_image,
|
| 37 |
+
outputs=gr.Label(),
|
| 38 |
+
description="A simple mlp classification model for image classification using the mnist dataset.")
|
| 39 |
+
iface.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|