import gradio as gr | |
import tensorflow as tf | |
from PIL import Image | |
import numpy as np | |
# Cargue su modelo (aquí el archivo Keras .h5 como ejemplo) | |
# Load your model (here the Keras .h5 file as an example) | |
# Carica il tuo modello (qui il file Keras .h5 come esempio) | |
# Cargue su modelo (aquí el archivo Keras .h5 como ejemplo) | |
# 加载你的模型(这里以 Keras .h5 文件为例) | |
# Lade dein Modell (hier als Beispiel die Keras .h5 Datei) | |
model = tf.keras.models.load_model('pokemon_model.keras') | |
# Class names should match your dataset | |
# I nomi delle classi devono corrispondere al tuo set di dati | |
# Los nombres de las clases deben coincidir con su conjunto de datos. | |
# Klassennamen, sollten deinem Dataset entsprechen | |
# 类名应该与你的数据集匹配 | |
class_names = ['Jolteon', 'Kakuna', 'Mr. Mime'] | |
def classify_image(image): | |
image = Image.fromarray(image.astype('uint8'), 'RGB') | |
img = image.resize((150, 150)) | |
img_array = tf.keras.preprocessing.image.img_to_array(img) | |
img_array = tf.expand_dims(img_array, 0) # 创建一个批次, # Crea un batch, # Erstelle einen Batch, # Create a batch, # Crear un lote, | |
predictions = model.predict(img_array) | |
predicted_class = class_names[np.argmax(predictions[0])] | |
confidence = np.max(predictions[0]) | |
return {predicted_class: float(confidence)} | |
image_input = gr.Image() # Rimuove il parametro `shape`, # Entferne den `shape` Parameter, # 删除`shape`参数, # Eliminar el parámetro `forma`, # Remove the `shape` parameter, | |
label = gr.Label(num_top_classes=3) | |
iface = gr.Interface( | |
fn=classify_image, | |
inputs=image_input, | |
outputs=label, | |
title='Pokémon Classifier', | |
description='Upload an image of Jolteon, Kakuna, or Mr. Mime and the classifier will tell you which one it is and the confidence level of the prediction.').launch() | |
#iface.launch() |