Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
model_path = "Xception.keras"
|
| 6 |
+
model = tf.keras.models.load_model(model_path)
|
| 7 |
+
# Define the core prediction function
|
| 8 |
+
def predict_pokemon(image):
|
| 9 |
+
# Preprocess image
|
| 10 |
+
print(type(image))
|
| 11 |
+
image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
|
| 12 |
+
image = image.resize((150, 150)) #resize the image to 28x28 and converts it to gray scale
|
| 13 |
+
image = np.array(image)
|
| 14 |
+
image = np.expand_dims(image, axis=0) # same as image[None, ...]
|
| 15 |
+
|
| 16 |
+
# Predict
|
| 17 |
+
prediction = model.predict(image)
|
| 18 |
+
|
| 19 |
+
# Because the output layer was dense(0) without an activation function, we need to apply sigmoid to get the probability
|
| 20 |
+
# we could also change the output layer to dense(1, activation='sigmoid')
|
| 21 |
+
prediction = np.round(prediction, 2)
|
| 22 |
+
# Separate the probabilities for each class
|
| 23 |
+
P_aloevera = prediction[0][0] # Probability for class 'abra'
|
| 24 |
+
P_curcuma = prediction[0][1] # Probability for class 'beedrill'
|
| 25 |
+
p_guava = prediction[0][2] # Probability for class 'sandshrew'
|
| 26 |
+
return {'aloevera': P_aloevera, 'curcuma': P_curcuma, 'guava': p_guava}
|
| 27 |
+
# Create the Gradio interface
|
| 28 |
+
input_image = gr.Image()
|
| 29 |
+
iface = gr.Interface(
|
| 30 |
+
fn=predict_pokemon,
|
| 31 |
+
inputs=input_image,
|
| 32 |
+
outputs=gr.Label(),
|
| 33 |
+
examples=["images/aloevera0.jpg", "images/curcuma51.jpg", "images/guava10.jpg"],
|
| 34 |
+
description="A simple mlp classification model for image classification using the mnist dataset.")
|
| 35 |
+
iface.launch(share=True)
|