File size: 1,862 Bytes
a42c30f
a3d8190
 
 
a42c30f
a3d8190
 
 
a42c30f
a3d8190
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4faaf16
a42c30f
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
40
41
42
43
44
import gradio as gr
from huggingface_hub import hf_hub_download
model_path = hf_hub_download(repo_id="Bmo411/DenoisingAutoencoder", filename="autoencoder_complete_model_Fourier.keras")
import tensorflow as tf

def degrade_image(image, downscale_factor=4):
    """Reduce la calidad de la imagen reduciendo su tamaño y volviéndola a escalar."""
    h, w = image.shape[:2]

    # Reducir tamaño (forzando pérdida de calidad)
    small_img = cv2.resize(image, (w // downscale_factor, h // downscale_factor), interpolation=cv2.INTER_AREA)

    # Volver a escalarla al tamaño original SIN mejorar calidad (interpolación brusca)
    degraded_img = cv2.resize(small_img, (w, h), interpolation=cv2.INTER_NEAREST)

    return degraded_img

def preprocess_image(image, std_dev=0.1, downscale_factor=4, target_size=(256, 256)):
    """Recibe una imagen en numpy array, la degrada en calidad, le agrega ruido y la normaliza."""
    
    # Reducir calidad primero
    degraded_img = degrade_image(image, downscale_factor)

    # Redimensionar a tamaño esperado por el modelo
    resized_img = cv2.resize(degraded_img, target_size, interpolation=cv2.INTER_AREA)
    
    # Normalizar
    resized_img = resized_img.astype(np.float32) / 255.0

    # Agregar ruido gaussiano
    noise = np.random.normal(0, std_dev, resized_img.shape)  # Media=0, desviación estándar=std_dev
    noisy_img = resized_img + noise  # Sumar el ruido a la imagen
    noisy_img = np.clip(noisy_img, 0, 1)  # Asegurar valores en el rango [0,1]

    return noisy_img

def Denoiser(imagen):
    model = tf.keras.models.load_model("autoencoder_complete_model_Fourier.keras", compile=True) 
    image = preprocess_image(imagen)
    reconstruct = model.predict(image)
    return image,recostruct 
    
demo = gr.Interface(fn=Denoiser, inputs="image", outputs=["image","reconstruct"])
demo.launch()