Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,47 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
|
|
|
|
3 |
import tensorflow as tf
|
4 |
|
5 |
-
# Cargar el
|
6 |
-
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
def predict(img1, img2):
|
10 |
-
# Preprocesar las imágenes: convertir a escala de grises, redimensionar, normalizar
|
11 |
-
img1 = img1.convert('L').resize((28, 28))
|
12 |
-
img2 = img2.convert('L').resize((28, 28))
|
13 |
-
|
14 |
-
arr1 = np.array(img1).astype('float32') / 255.0
|
15 |
-
arr2 = np.array(img2).astype('float32') / 255.0
|
16 |
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
fn=predict,
|
28 |
inputs=[
|
29 |
-
gr.Image(label="Imagen 1"
|
30 |
-
gr.Image(label="Imagen 2"
|
31 |
],
|
32 |
-
outputs=
|
33 |
-
title="
|
34 |
-
description="Sube dos imágenes de dígitos
|
35 |
)
|
36 |
|
37 |
-
|
38 |
-
iface.launch()
|
39 |
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
+
from tensorflow.keras.models import load_model
|
4 |
+
from PIL import Image
|
5 |
import tensorflow as tf
|
6 |
|
7 |
+
# 🧠 Cargar modelo (asegúrate de que el archivo esté en la raíz del repositorio)
|
8 |
+
def euclidean_distance(vects):
|
9 |
+
x, y = vects
|
10 |
+
sum_square = tf.reduce_sum(tf.square(x - y), axis=1, keepdims=True)
|
11 |
+
return tf.sqrt(tf.maximum(sum_square, tf.keras.backend.epsilon()))
|
12 |
|
13 |
+
model = load_model("mnist_siamese_model.keras", custom_objects={'euclidean_distance': euclidean_distance})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
# 📌 Preprocesar imágenes
|
16 |
+
def preprocess(img):
|
17 |
+
img = img.convert("L").resize((28, 28))
|
18 |
+
img = np.array(img).astype("float32") / 255.0
|
19 |
+
img = np.expand_dims(img, axis=-1) # (28, 28, 1)
|
20 |
+
return img
|
21 |
|
22 |
+
# 🔍 Función de predicción
|
23 |
+
def predict(img1, img2):
|
24 |
+
img1 = preprocess(img1)
|
25 |
+
img2 = preprocess(img2)
|
26 |
+
img1 = np.expand_dims(img1, axis=0)
|
27 |
+
img2 = np.expand_dims(img2, axis=0)
|
28 |
+
|
29 |
+
distance = model.predict([img1, img2])[0][0]
|
30 |
+
threshold = 0.5
|
31 |
+
same = distance < threshold
|
32 |
+
return f"¿Mismo dígito? {'Sí' if same else 'No'} (distancia: {distance:.4f})"
|
33 |
+
|
34 |
+
# 🎛️ Interfaz Gradio
|
35 |
+
interface = gr.Interface(
|
36 |
fn=predict,
|
37 |
inputs=[
|
38 |
+
gr.Image(type="pil", label="Imagen 1"),
|
39 |
+
gr.Image(type="pil", label="Imagen 2")
|
40 |
],
|
41 |
+
outputs="text",
|
42 |
+
title="Modelo Siamese con MNIST",
|
43 |
+
description="Sube dos imágenes de dígitos para verificar si representan el mismo número."
|
44 |
)
|
45 |
|
46 |
+
interface.launch()
|
|
|
47 |
|