Update app.py
Browse files
app.py
CHANGED
|
@@ -2,10 +2,15 @@ import numpy as np
|
|
| 2 |
from tensorflow.keras.models import load_model
|
| 3 |
from PIL import Image
|
| 4 |
import gradio as gr
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
# Load model
|
| 7 |
model = load_model("unet_model.h5", compile=False)
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
# Preprocess function
|
| 10 |
def preprocess_image(image, target_size=(192, 176)):
|
| 11 |
image = image.resize((target_size[1], target_size[0])) # width, height
|
|
@@ -14,34 +19,42 @@ def preprocess_image(image, target_size=(192, 176)):
|
|
| 14 |
image = np.expand_dims(image, axis=-1)
|
| 15 |
return np.expand_dims(image, axis=0)
|
| 16 |
|
| 17 |
-
# Prediction function
|
| 18 |
-
def predict(img):
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
img = img.convert("L")
|
| 21 |
|
| 22 |
-
# Preprocess
|
| 23 |
input_data = preprocess_image(img)
|
| 24 |
|
| 25 |
-
# Model prediction
|
| 26 |
pred = model.predict(input_data)[0]
|
| 27 |
|
| 28 |
-
# Remove channel
|
| 29 |
if pred.ndim == 3 and pred.shape[-1] == 1:
|
| 30 |
pred = np.squeeze(pred, axis=-1)
|
| 31 |
|
| 32 |
-
# Convert to image
|
| 33 |
pred_img = (pred * 255).astype(np.uint8)
|
| 34 |
pred_img = Image.fromarray(pred_img)
|
| 35 |
|
| 36 |
return pred_img
|
| 37 |
|
| 38 |
-
# Gradio
|
| 39 |
interface = gr.Interface(
|
| 40 |
fn=predict,
|
| 41 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
| 42 |
outputs=gr.Image(type="pil", label="Denoised Output"),
|
| 43 |
title="U-Net Image Denoising",
|
| 44 |
-
description="Upload a grayscale image
|
|
|
|
| 45 |
)
|
| 46 |
|
|
|
|
| 47 |
interface.launch()
|
|
|
|
| 2 |
from tensorflow.keras.models import load_model
|
| 3 |
from PIL import Image
|
| 4 |
import gradio as gr
|
| 5 |
+
from datasets import load_dataset
|
| 6 |
+
import random
|
| 7 |
|
| 8 |
+
# Load pre-trained model
|
| 9 |
model = load_model("unet_model.h5", compile=False)
|
| 10 |
|
| 11 |
+
# Load Hugging Face dataset
|
| 12 |
+
dataset = load_dataset("AIOmarRehan/Cropped_Yale_Faces")
|
| 13 |
+
|
| 14 |
# Preprocess function
|
| 15 |
def preprocess_image(image, target_size=(192, 176)):
|
| 16 |
image = image.resize((target_size[1], target_size[0])) # width, height
|
|
|
|
| 19 |
image = np.expand_dims(image, axis=-1)
|
| 20 |
return np.expand_dims(image, axis=0)
|
| 21 |
|
| 22 |
+
# Prediction function
|
| 23 |
+
def predict(img=None, use_dataset=False):
|
| 24 |
+
if use_dataset:
|
| 25 |
+
# Pick a random sample from the dataset
|
| 26 |
+
example = random.choice(dataset["train"])
|
| 27 |
+
img = example["image"]
|
| 28 |
+
|
| 29 |
+
if img is None:
|
| 30 |
+
return None
|
| 31 |
+
|
| 32 |
img = img.convert("L")
|
| 33 |
|
|
|
|
| 34 |
input_data = preprocess_image(img)
|
| 35 |
|
|
|
|
| 36 |
pred = model.predict(input_data)[0]
|
| 37 |
|
|
|
|
| 38 |
if pred.ndim == 3 and pred.shape[-1] == 1:
|
| 39 |
pred = np.squeeze(pred, axis=-1)
|
| 40 |
|
|
|
|
| 41 |
pred_img = (pred * 255).astype(np.uint8)
|
| 42 |
pred_img = Image.fromarray(pred_img)
|
| 43 |
|
| 44 |
return pred_img
|
| 45 |
|
| 46 |
+
# Gradio Interface
|
| 47 |
interface = gr.Interface(
|
| 48 |
fn=predict,
|
| 49 |
+
inputs=[
|
| 50 |
+
gr.Image(type="pil", label="Upload Image"),
|
| 51 |
+
gr.Checkbox(label="Use Random Dataset Image")
|
| 52 |
+
],
|
| 53 |
outputs=gr.Image(type="pil", label="Denoised Output"),
|
| 54 |
title="U-Net Image Denoising",
|
| 55 |
+
description="Upload a grayscale image or use a random image from the Cropped Yale Faces dataset. "
|
| 56 |
+
"The U-Net model will provide a denoised output."
|
| 57 |
)
|
| 58 |
|
| 59 |
+
# Launch the app
|
| 60 |
interface.launch()
|