Spaces:
Sleeping
Sleeping
Upload files
Browse filesupload project files
- app.py +53 -0
- cero.png +0 -0
- cinco.png +0 -0
- cnn_mnist_model.h5 +3 -0
- cuatro.png +0 -0
- dos.png +0 -0
- dos_v2.png +0 -0
- nueve.png +0 -0
- seis.png +0 -0
- tres.png +0 -0
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
# Cargar el modelo guardado
|
| 8 |
+
try:
|
| 9 |
+
model = tf.keras.models.load_model('cnn_mnist_model.h5')
|
| 10 |
+
print("Modelo CNN cargado exitosamente.")
|
| 11 |
+
except Exception as e:
|
| 12 |
+
print(f"Error al cargar el modelo: {e}")
|
| 13 |
+
model = None
|
| 14 |
+
|
| 15 |
+
def predict_image(image):
|
| 16 |
+
if model is None:
|
| 17 |
+
return "Error: Modelo no cargado"
|
| 18 |
+
|
| 19 |
+
try:
|
| 20 |
+
# Convertir la imagen de Gradio a escala de grises y redimensionar a 28x28
|
| 21 |
+
img = image.convert('L')
|
| 22 |
+
img = img.resize((28, 28), Image.LANCZOS)
|
| 23 |
+
|
| 24 |
+
# Convertir a un array de NumPy
|
| 25 |
+
img_array = np.array(img)
|
| 26 |
+
|
| 27 |
+
# Normalizar los valores de los p铆xeles al rango de 0.0 a 1.0
|
| 28 |
+
processed_image = img_array.astype('float32') / 255.0
|
| 29 |
+
|
| 30 |
+
# Redimensionar para el modelo CNN (1, 28, 28, 1)
|
| 31 |
+
processed_image = processed_image.reshape(1, 28, 28, 1)
|
| 32 |
+
|
| 33 |
+
# Hacer la predicci贸n
|
| 34 |
+
prediction = model.predict(processed_image)
|
| 35 |
+
predicted_class = np.argmax(prediction, axis=1)[0]
|
| 36 |
+
|
| 37 |
+
return f"Predicci贸n: {predicted_class}"
|
| 38 |
+
|
| 39 |
+
except Exception as e:
|
| 40 |
+
return f"Error: {str(e)}"
|
| 41 |
+
|
| 42 |
+
# Crear la interfaz de Gradio
|
| 43 |
+
interface = gr.Interface(
|
| 44 |
+
fn=predict_image,
|
| 45 |
+
inputs=gr.Image(type="pil", label="Sube una imagen de un d铆gito (28x28, escala de grises)"),
|
| 46 |
+
outputs=gr.Textbox(label="Resultado"),
|
| 47 |
+
title="Reconocimiento de D铆gitos con CNN",
|
| 48 |
+
description="Sube una imagen de un d铆gito escrito a mano para predecirlo usando un modelo CNN."
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Lanzar la interfaz
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
interface.launch()
|
cero.png
ADDED
|
cinco.png
ADDED
|
cnn_mnist_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f8963d2f857350fbbcce7f6cbb5eb4469d988920070aa270d7df5bee5aca18fd
|
| 3 |
+
size 2743672
|
cuatro.png
ADDED
|
dos.png
ADDED
|
dos_v2.png
ADDED
|
nueve.png
ADDED
|
seis.png
ADDED
|
tres.png
ADDED
|