File size: 1,383 Bytes
9a45868
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from tensorflow.keras.models import load_model
import numpy as np
from PIL import Image
from keras.preprocessing import image

def preprocess_image(image_path, target_size=(500, 500)):
    img = Image.open(image_path).convert('L')  # Convert to grayscale
    img_array = image.img_to_array(img)
    img_array = np.expand_dims(img_array, axis=0)
    img_array = img_array.astype('float32') / 255.0  # Normalize pixel values between 0 and 1
    return img_array

def denoise_image(image_path):
    # Load the denoising autoencoder model
    model = load_model('model.h5')
    
    # Preprocess the image
    img_array = preprocess_image(image_path)
    
    # Denoise the image using the autoencoder model
    denoised_img = model.predict(img_array)
    denoised_img = np.squeeze(denoised_img)  # Remove batch dimension
    
    # Convert the denoised image array to PIL Image
    denoised_img = (denoised_img * 255).astype(np.uint8)
    denoised_img_pil = Image.fromarray(denoised_img)
    
    return denoised_img_pil

image_interface = gr.Interface(
    fn=denoise_image,
    inputs=gr.Image(sources=['upload'], type='filepath', label='Upload Image'),
    outputs=gr.Image(label='Denoised Image', type='pil', height=500, width=500),
    title='Image Denoiser',
    description='Upload an image to denoise.',
    allow_flagging=False
)

image_interface.launch()