File size: 1,326 Bytes
503c7ba
 
 
57403af
503c7ba
 
c3a4ad0
57403af
503c7ba
57403af
 
503c7ba
57403af
503c7ba
57403af
 
 
 
503c7ba
57403af
 
 
 
 
 
 
 
 
 
 
afa7f02
503c7ba
 
57403af
 
 
 
222ed32
57403af
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image


model_path = "pokemon_transferlearning2.keras"
model = tf.keras.models.load_model(model_path)

# Define the core prediction function
def predict_pokemon(image):
    # Preprocess image
    print(type(image))
    image = Image.fromarray(image.astype('uint8'))  # Convert numpy array to PIL image
    image = image.resize((150, 150)) #resize the image to 150x150
    image = np.array(image)
    image = np.expand_dims(image, axis=0) # same as image[None, ...]
    
    # Predict
    prediction = model.predict(image)
    
    # Apply softmax to get probabilities for each class
    prediction = tf.nn.softmax(prediction)
    
    # Create a dictionary with the probabilities for each Pokemon
    porygon = np.round(float(prediction[0][0]), 2)
    seel = np.round(float(prediction[0][1]), 2)
    vaperon = np.round(float(prediction[0][2]), 2)
    
    return {'Porygon': porygon, 'Seel': seel, 'Vaperon': vaperon}


input_image = gr.Image()
iface = gr.Interface(
    fn=predict_pokemon,
    inputs=input_image, 
    outputs=gr.Label(),
    examples=["images/vaporeon.png", "images/seel.jpg", "images/porygon.png"], 
    description="A simple mlp classification model for image classification using the mnist dataset.")
iface.launch(share=True)