Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,39 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
-
from PIL import Image
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
|
| 6 |
-
# Load your custom regression model
|
| 7 |
-
model_path = "pokemon_transferlearning2.keras"
|
| 8 |
-
model = tf.keras.models.load_model(model_path)
|
| 9 |
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
# Define
|
| 13 |
-
def
|
| 14 |
# Preprocess image
|
|
|
|
| 15 |
image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
|
| 16 |
-
image = image.resize((150, 150))
|
| 17 |
-
image = np.array(image)
|
| 18 |
-
image = np.expand_dims(image, axis=0)
|
| 19 |
-
|
| 20 |
-
# Print statements for debugging
|
| 21 |
-
print(f"Image shape (after preprocessing): {image.shape}")
|
| 22 |
-
print(f"Image data (sample): {image[0, :5, :5, 0]}") # Print a small sample of the data for inspection
|
| 23 |
-
|
| 24 |
# Predict
|
| 25 |
-
prediction = model.predict(image)
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
confidences = {labels[i]: np.round(float(prediction[0][i]), 2) for i in range(len(labels))}
|
| 29 |
-
return confidences
|
| 30 |
|
| 31 |
-
# Create Gradio interface
|
| 32 |
input_image = gr.Image()
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
interface.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-transferlearning2.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 |
+
print(type(image))
|
| 14 |
image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
|
| 15 |
+
image = image.resize((150, 150)) #resize the image to 150x150
|
| 16 |
+
image = np.array(image)
|
| 17 |
+
image = np.expand_dims(image, axis=0) # same as image[None, ...]
|
| 18 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
# Predict
|
| 20 |
+
prediction = model.predict(image)
|
| 21 |
+
|
| 22 |
+
# Apply softmax to get probabilities for each class
|
| 23 |
+
prediction = tf.nn.softmax(prediction)
|
| 24 |
+
|
| 25 |
+
# Create a dictionary with the probabilities for each Pokemon
|
| 26 |
+
porygon = np.round(float(prediction[0][0]), 2)
|
| 27 |
+
seel = np.round(float(prediction[0][1]), 2)
|
| 28 |
+
vaperon = np.round(float(prediction[0][2]), 2)
|
| 29 |
+
|
| 30 |
+
return {'Porygon': porygon, 'Seel': seel, 'Vaperon': vaperon}
|
| 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)
|
|
|