jefercania commited on
Commit
ff3781e
1 Parent(s): cd497ff

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import requests
4
+ from PIL import Image
5
+ import numpy as np
6
+
7
+ # Cargando el modelo
8
+ inception_net = tf.keras.applications.MobileNetV2()
9
+
10
+ # Obteniendo las etiquetas
11
+ respuesta = requests.get("https://git.io/JJkYN")
12
+ etiquetas = respuesta.text.split("\n")
13
+
14
+ def redimensionar_imagen(img_array, target_size=(224, 224)):
15
+ img = Image.fromarray(img_array)
16
+ img = img.resize(target_size)
17
+ return np.array(img)
18
+
19
+ def clasifica_imagen(inp):
20
+ # Redimensionar la imagen
21
+ inp = redimensionar_imagen(inp)
22
+
23
+ # Verificar la forma actual de la imagen
24
+ if inp.shape != (224, 224, 3):
25
+ raise ValueError(f"Expected input shape (224, 224, 3), but got {inp.shape}")
26
+
27
+ # Hacer prediccion
28
+ inp = tf.keras.applications.mobilenet_v2.preprocess_input(inp)
29
+ prediction = inception_net.predict(inp.reshape((-1, 224, 224, 3))).flatten()
30
+ confidences = {etiquetas[i]: float(prediction[i]) for i in range(1000)}
31
+ return confidences
32
+
33
+ demo = gr.Interface(fn=clasifica_imagen,
34
+ inputs=gr.Image(),
35
+ outputs=gr.Label(num_top_classes=3),
36
+ )
37
+
38
+ demo.launch(debug=True)