Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,38 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
-
from PIL import Image
|
| 4 |
import numpy as np
|
| 5 |
-
|
| 6 |
-
|
|
|
|
| 7 |
model_path = "pokemon_classifier_xception.keras"
|
| 8 |
-
|
| 9 |
-
#model.load_weights(model_path)
|
| 10 |
model = tf.keras.models.load_model(model_path)
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
# Define regression function
|
| 15 |
-
def predict_regression(image):
|
| 16 |
# Preprocess image
|
|
|
|
| 17 |
image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
|
| 18 |
-
image = image.resize((
|
| 19 |
image = np.array(image)
|
| 20 |
-
|
| 21 |
# Predict
|
| 22 |
-
prediction = model.predict(image
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
input_image = gr.Image()
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
import tensorflow as tf
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
|
| 9 |
model_path = "pokemon_classifier_xception.keras"
|
|
|
|
|
|
|
| 10 |
model = tf.keras.models.load_model(model_path)
|
| 11 |
+
|
| 12 |
+
# Define the core prediction function
|
| 13 |
+
def predict_pokemon(image):
|
|
|
|
|
|
|
| 14 |
# Preprocess image
|
| 15 |
+
print(type(image))
|
| 16 |
image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
|
| 17 |
+
image = image.resize((150, 150)) #resize the image to 150x150
|
| 18 |
image = np.array(image)
|
| 19 |
+
image = np.expand_dims(image, axis=0) # same as image[None, ...]
|
| 20 |
# Predict
|
| 21 |
+
prediction = model.predict(image)
|
| 22 |
+
# Apply softmax to get probabilities for each class
|
| 23 |
+
prediction = tf.nn.softmax(prediction)
|
| 24 |
+
# Create a dictionary with the probabilities for each Pokemon
|
| 25 |
+
abra = np.round(float(prediction[0][0]), 2)
|
| 26 |
+
aerodactyl = np.round(float(prediction[0][1]), 2)
|
| 27 |
+
arcanine = np.round(float(prediction[0][2]), 2)
|
| 28 |
+
return {'abra': abra, 'aerodactyl': aerodactyl, 'arcanine': arcanine}
|
| 29 |
+
|
| 30 |
+
|
| 31 |
input_image = gr.Image()
|
| 32 |
+
iface = gr.Interface(
|
| 33 |
+
fn=predict_pokemon,
|
| 34 |
+
inputs=input_image,
|
| 35 |
+
outputs=gr.Label(),
|
| 36 |
+
examples=[],
|
| 37 |
+
description="A simple mlp classification model for image classification using the mnist dataset.")
|
| 38 |
+
iface.launch(share=True)
|