Spaces:
Runtime error
Runtime error
File size: 1,172 Bytes
ff3781e |
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 |
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)
|