Spaces:
Runtime error
Runtime error
import gradio as gr | |
import tensorflow as tf | |
import requests | |
from PIL import Image | |
import numpy as np | |
# Cargando el modelo | |
inception_net = tf.keras.applications.MobileNetV2() | |
# Obteniendo las etiquetas | |
respuesta = requests.get("https://git.io/JJkYN") | |
etiquetas = respuesta.text.split("\n") | |
def redimensionar_imagen(img_array, target_size=(224, 224)): | |
img = Image.fromarray(img_array) | |
img = img.resize(target_size) | |
return np.array(img) | |
def clasifica_imagen(inp): | |
# Redimensionar la imagen | |
inp = redimensionar_imagen(inp) | |
# Verificar la forma actual de la imagen | |
if inp.shape != (224, 224, 3): | |
raise ValueError(f"Expected input shape (224, 224, 3), but got {inp.shape}") | |
# Hacer prediccion | |
inp = tf.keras.applications.mobilenet_v2.preprocess_input(inp) | |
prediction = inception_net.predict(inp.reshape((-1, 224, 224, 3))).flatten() | |
confidences = {etiquetas[i]: float(prediction[i]) for i in range(1000)} | |
return confidences | |
demo = gr.Interface(fn=clasifica_imagen, | |
inputs=gr.Image(), | |
outputs=gr.Label(num_top_classes=3), | |
) | |
demo.launch(debug=True) | |