Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,37 +3,39 @@ import tensorflow as tf
|
|
3 |
import numpy as np
|
4 |
from PIL import Image
|
5 |
|
6 |
-
|
7 |
-
model_path = "pokemon-
|
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))
|
15 |
image = np.array(image)
|
16 |
-
image = np.expand_dims(image, axis=0)
|
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 |
-
#
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
|
30 |
-
return
|
31 |
|
|
|
|
|
32 |
|
33 |
-
|
34 |
iface = gr.Interface(
|
35 |
fn=predict_pokemon,
|
36 |
inputs=input_image,
|
37 |
-
outputs=gr.Label(),
|
38 |
-
description="A simple
|
39 |
-
iface.launch(share=True)
|
|
|
3 |
import numpy as np
|
4 |
from PIL import Image
|
5 |
|
6 |
+
# Load the model
|
7 |
+
model_path = "pokemon-predict-model_transferlearning.keras"
|
8 |
model = tf.keras.models.load_model(model_path)
|
9 |
|
10 |
+
# Define the core prediction function
|
11 |
def predict_pokemon(image):
|
12 |
# Preprocess image
|
|
|
13 |
image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
|
14 |
+
image = image.resize((150, 150)) # Resize the image to match model input size
|
15 |
image = np.array(image)
|
16 |
+
image = np.expand_dims(image, axis=0) # Add batch dimension
|
|
|
17 |
|
18 |
+
# Predict
|
19 |
prediction = model.predict(image)
|
20 |
|
21 |
# Apply softmax to get probabilities for each class
|
22 |
prediction = tf.nn.softmax(prediction)
|
23 |
|
24 |
+
# Extract class names from the provided list
|
25 |
+
class_names = ['Abra', 'Aerodactyl', 'Alakazam', 'Arbok', 'Arcanine', 'Articuno', 'Beedrill', 'Bellsprout', 'Blastoise', 'Bulbasaur', 'Butterfree', 'Caterpie', 'Chansey', 'Charizard', 'Charmander', 'Charmeleon', 'Clefable', 'Clefairy', 'Cloyster', 'Cubone', 'Dewgong', 'Diglett', 'Ditto', 'Dodrio', 'Doduo', 'Dragonair', 'Dragonite', 'Dratini', 'Drowzee', 'Dugtrio', 'Eevee', 'Ekans', 'Electabuzz', 'Electrode', 'Exeggcute', 'Exeggutor', 'Farfetchd', 'Fearow', 'Flareon', 'Gastly', 'Gengar', 'Geodude', 'Gloom', 'Golbat', 'Goldeen', 'Golduck', 'Graveler', 'Grimer', 'Growlithe', 'Gyarados', 'Haunter', 'Hitmonchan', 'Hitmonlee', 'Horsea', 'Hypno', 'Ivysaur', 'Jigglypuff', 'Jolteon', 'Jynx', 'Kabutops', 'Kadabra', 'Kakuna', 'Kangaskhan', 'Kingler', 'Koffing', 'Lapras', 'Lickitung', 'Machamp', 'Machoke', 'Machop', 'Magikarp', 'Magmar', 'Magnemite', 'Magneton', 'Mankey', 'Marowak', 'Meowth', 'Metapod', 'Mew', 'Mewtwo', 'Moltres', 'Mr. Mime', 'MrMime', 'Nidoking', 'Nidoqueen', 'Nidorina', 'Nidorino', 'Ninetales', 'Oddish', 'Omanyte', 'Omastar', 'Parasect', 'Pidgeot', 'Pidgeotto', 'Pidgey', 'Pikachu', 'Pinsir', 'Poliwag', 'Poliwhirl', 'Poliwrath', 'Ponyta', 'Porygon', 'Primeape', 'Psyduck', 'Raichu', 'Rapidash', 'Raticate', 'Rattata', 'Rhydon', 'Rhyhorn', 'Sandshrew', 'Sandslash', 'Scyther', 'Seadra', 'Seaking', 'Seel', 'Shellder', 'Slowbro', 'Slowpoke', 'Snorlax', 'Spearow', 'Squirtle', 'Starmie', 'Staryu', 'Tangela', 'Tauros', 'Tentacool', 'Tentacruel', 'Vaporeon', 'Venomoth', 'Venonat', 'Venusaur', 'Victreebel', 'Vileplume', 'Voltorb', 'Vulpix', 'Wartortle', 'Weedle', 'Weepinbell', 'Weezing', 'Wigglytuff', 'Zapdos', 'Zubat']
|
26 |
+
|
27 |
+
# Create a dictionary with the probabilities for each class
|
28 |
+
probabilities = {class_names[i]: np.round(float(prediction[0][i]), 2) for i in range(len(class_names))}
|
29 |
|
30 |
+
return probabilities
|
31 |
|
32 |
+
# Update input component to accept image uploads
|
33 |
+
input_image = gr.inputs.Image()
|
34 |
|
35 |
+
# Launch the interface
|
36 |
iface = gr.Interface(
|
37 |
fn=predict_pokemon,
|
38 |
inputs=input_image,
|
39 |
+
outputs=gr.outputs.Label(),
|
40 |
+
description="A simple MLP classification model for Pokémon image classification.")
|
41 |
+
iface.launch(share=True)
|