Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,44 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
def
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import hf_hub_download
|
| 3 |
+
model_path = hf_hub_download(repo_id="Bmo411/DenoisingAutoencoder", filename="autoencoder_complete_model_Fourier.keras")
|
| 4 |
+
import tensorflow as tf
|
| 5 |
|
| 6 |
+
def degrade_image(image, downscale_factor=4):
|
| 7 |
+
"""Reduce la calidad de la imagen reduciendo su tamaño y volviéndola a escalar."""
|
| 8 |
+
h, w = image.shape[:2]
|
| 9 |
|
| 10 |
+
# Reducir tamaño (forzando pérdida de calidad)
|
| 11 |
+
small_img = cv2.resize(image, (w // downscale_factor, h // downscale_factor), interpolation=cv2.INTER_AREA)
|
| 12 |
+
|
| 13 |
+
# Volver a escalarla al tamaño original SIN mejorar calidad (interpolación brusca)
|
| 14 |
+
degraded_img = cv2.resize(small_img, (w, h), interpolation=cv2.INTER_NEAREST)
|
| 15 |
+
|
| 16 |
+
return degraded_img
|
| 17 |
+
|
| 18 |
+
def preprocess_image(image, std_dev=0.1, downscale_factor=4, target_size=(256, 256)):
|
| 19 |
+
"""Recibe una imagen en numpy array, la degrada en calidad, le agrega ruido y la normaliza."""
|
| 20 |
+
|
| 21 |
+
# Reducir calidad primero
|
| 22 |
+
degraded_img = degrade_image(image, downscale_factor)
|
| 23 |
+
|
| 24 |
+
# Redimensionar a tamaño esperado por el modelo
|
| 25 |
+
resized_img = cv2.resize(degraded_img, target_size, interpolation=cv2.INTER_AREA)
|
| 26 |
+
|
| 27 |
+
# Normalizar
|
| 28 |
+
resized_img = resized_img.astype(np.float32) / 255.0
|
| 29 |
+
|
| 30 |
+
# Agregar ruido gaussiano
|
| 31 |
+
noise = np.random.normal(0, std_dev, resized_img.shape) # Media=0, desviación estándar=std_dev
|
| 32 |
+
noisy_img = resized_img + noise # Sumar el ruido a la imagen
|
| 33 |
+
noisy_img = np.clip(noisy_img, 0, 1) # Asegurar valores en el rango [0,1]
|
| 34 |
+
|
| 35 |
+
return noisy_img
|
| 36 |
+
|
| 37 |
+
def Denoiser(imagen):
|
| 38 |
+
model = tf.keras.models.load_model("autoencoder_complete_model_Fourier.keras", compile=True)
|
| 39 |
+
image = preprocess_image(imagen)
|
| 40 |
+
reconstruct = model.predict(image)
|
| 41 |
+
return image,recostruct
|
| 42 |
+
|
| 43 |
+
demo = gr.Interface(fn=greet, inputs="image", outputs=["image","reconstruct"], ou)
|
| 44 |
demo.launch()
|