|
|
import gradio as gr |
|
|
from PIL import Image |
|
|
import numpy as np |
|
|
import tensorflow as tf |
|
|
import matplotlib.pyplot as plt |
|
|
|
|
|
|
|
|
class_names = ['airplane', 'automobile', 'bird'] |
|
|
|
|
|
|
|
|
model = tf.keras.models.load_model('final_model.keras') |
|
|
|
|
|
|
|
|
mean = np.array([0.4914, 0.4822, 0.4465]) |
|
|
std = np.array([0.2470, 0.2435, 0.2616]) |
|
|
|
|
|
|
|
|
def load_and_preprocess_image(image): |
|
|
img = Image.open(image) |
|
|
img = img.resize((32, 32)) |
|
|
img = np.array(img) |
|
|
|
|
|
|
|
|
if img.ndim == 2: |
|
|
img = np.stack([img] * 3, axis=-1) |
|
|
|
|
|
img = img.astype('float32') |
|
|
img = (img / 255.0 - mean) / std |
|
|
|
|
|
|
|
|
img = np.expand_dims(img, axis=0) |
|
|
return img |
|
|
|
|
|
|
|
|
def predict_image(image): |
|
|
|
|
|
img = load_and_preprocess_image(image) |
|
|
|
|
|
|
|
|
prediction = model.predict(img) |
|
|
|
|
|
|
|
|
probabilities = tf.nn.softmax(prediction[0]).numpy() |
|
|
|
|
|
|
|
|
results = {class_names[i]: float(probabilities[i]) * 100 for i in range(len(class_names))} |
|
|
|
|
|
|
|
|
result_str = "\n".join([f"{class_name}: {prob:.2f}%" for class_name, prob in results.items()]) |
|
|
return result_str |
|
|
|
|
|
|
|
|
interface = gr.Interface( |
|
|
fn=predict_image, |
|
|
inputs=gr.Image(type="filepath", label="Sube una imagen"), |
|
|
outputs=gr.Textbox(label="Probabilidades"), |
|
|
title="Clasificador de im谩genes CIFAR-10", |
|
|
description="Sube una imagen de un avi贸n, autom贸vil o p谩jaro para ver las probabilidades de cada clase." |
|
|
) |
|
|
|
|
|
|
|
|
interface.launch() |