Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
model_path = "pokemon-model_transferlearning.keras"
|
7 |
+
model = tf.keras.models.load_model(model_path)
|
8 |
+
|
9 |
+
# Define the core prediction function
|
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) # Expand dimensions to create batch size of 1
|
17 |
+
|
18 |
+
# Predict
|
19 |
+
prediction = model.predict(image)
|
20 |
+
|
21 |
+
# Assuming the model's output layer uses softmax activation and there are three outputs
|
22 |
+
prediction = prediction.flatten()
|
23 |
+
predictions = np.round(prediction, 2) # Flatten the predictions and round them
|
24 |
+
|
25 |
+
# Separate the probabilities for each class
|
26 |
+
p_clefairy = predictions[0] # Probability for Clefairy
|
27 |
+
p_snorlax = predictions[1] # Probability for Snorlax
|
28 |
+
p_squirtle = predictions[2] # Probability for Squirtle
|
29 |
+
|
30 |
+
return {
|
31 |
+
'clefairy': p_clefairy,
|
32 |
+
'snorlax': p_snorlax,
|
33 |
+
'squirtle': p_squirtle
|
34 |
+
}
|
35 |
+
|
36 |
+
# Create the Gradio interface
|
37 |
+
input_image = gr.Image()
|
38 |
+
iface = gr.Interface(
|
39 |
+
fn=predict_pokemon,
|
40 |
+
inputs=input_image,
|
41 |
+
outputs=gr.Label(),
|
42 |
+
examples=[r"images\squirtle\0.png", r"images\squirtle\00000003.png", r"images\snorlax\00000003.png",
|
43 |
+
r"images\squirtle\4.png", r"images\snorlax\00000017.png",
|
44 |
+
r"images\snorlax\4eb284a359474f0cb43ffe82d03abbe9.jpg",
|
45 |
+
r"images\clefairy\5fb558d9c96e4e469100636eb6c8627e.jpg",
|
46 |
+
r"images\clefairy\00000024.png", r"images\clefairy\00000103.jpg"],
|
47 |
+
description="A simple mlp classification model for image classification using the mnist dataset.")
|
48 |
+
iface.launch()
|