File size: 1,279 Bytes
379a946
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78e09d5
47b9a22
379a946
 
 
 
 
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
41
42
43
import gradio as gr
import tensorflow as tf
from PIL import Image
import numpy as np

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


labels = ['Bulbasaur', 'Jigglypuff', 'Rhyhorn']
    
def predict_image(image):
    # Preprocess image
    image = Image.fromarray(image.astype('uint8'))  # Convert numpy array to PIL image
    image = image.resize((224, 224))  # Resize the image to 224x224 pixels
    image = np.array(image) / 255.0   # Convert to float and normalize

    # Ensure the image has 3 color channels
    if image.ndim == 2:  # If grayscale, convert to RGB
        image = np.stack((image,)*3, axis=-1)

    prediction = model.predict(image[None, ...])  # Adding batch dimension
    confidences = {labels[i]: float(prediction[0][i]) for i in range(len(labels))}
    return confidences


input_image = gr.Image()
output_text = gr.Textbox(label="Predicted Value")


iface = gr.Interface(
    fn=predict_image,
    inputs=input_image, 
    outputs=gr.Label(),
    title="Pokémon Classifier",
    examples=["pokemon/Bulbasaur.png", "pokemon/Jigglypuff.png", "pokemon/Rhyhorn.png"],
    description="Upload an image of Bulbasaur, Jigglypuff, or Rhyhorn and the classifier will predict which one it is."
)



iface.launch()