denoiser / app.py
ahn1305's picture
Add application file
9a45868
raw
history blame contribute delete
No virus
1.38 kB
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()