File size: 1,299 Bytes
02c5bdc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image
model_path = "mabel_transferlearning.keras"
model = tf.keras.models.load_model(model_path)

# Define the core prediction function
def predict_pokemons(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 28x28 and converts it to gray scale
    image = np.array(image)
    image = np.expand_dims(image, axis=0) # same as image[None, ...]
    
    # Predict
    prediction = model.predict(image)
    
    # Apply sigmoid to get probabilities
    prediction_prob = tf.sigmoid(prediction).numpy()

    p_Abra = round(prediction_prob[0][0], 2)
    p_Pikachu = round(prediction_prob[0][1], 2)
    p_Beedrill = round(prediction_prob[0][2], 2)

    return{'Abra': p_Abra, 'Pikachu': p_Pikachu, 'Beedrill': p_Beedrill}

# Create the Gradio interface
input_image = gr.Image()
iface = gr.Interface(
    fn=predict_pokemons,
    inputs=input_image, 
    outputs=gr.Label(),
    examples=["Abra1.png", "Abra2.png", "Abra3.jpg", "Beedrill1.jpg", "Beedrill2.jpg", "Beedrill3.png", "Pikachu1.png", "Pikachu2.jpg", "Pikachu3.png"], 
    description="Pokemon Classifier")
iface.launch()