isana25 commited on
Commit
4417c7f
·
verified ·
1 Parent(s): 3516978

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+
3
+ import cv2
4
+ import numpy as np
5
+ import tensorflow as tf
6
+ import gradio as gr
7
+
8
+ # 1. Load your trained model
9
+ model = tf.keras.models.load_model("best_model.h5")
10
+
11
+ # 2. Denoising pipeline function
12
+ def process_and_denoise(image):
13
+ """
14
+ Input: HxW grayscale or color image (numpy array).
15
+ Outputs: (orig, noisy, denoised) all as 64×64 uint8 arrays.
16
+ """
17
+ # --- Preprocess Original ---
18
+ # If color, convert to gray:
19
+ if image.ndim == 3 and image.shape[2] == 3:
20
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
21
+ else:
22
+ # could be (H,W,1) or (H,W)
23
+ gray = image[...,0] if image.ndim == 3 else image
24
+
25
+ # Resize to 64×64
26
+ orig = cv2.resize(gray, (64,64))
27
+ orig_norm = orig.astype(np.float32) / 255.0
28
+
29
+ # --- Add Gaussian Noise ---
30
+ sigma = 0.1 # same noise level you trained with
31
+ noisy = orig_norm + sigma * np.random.randn(*orig_norm.shape)
32
+ noisy = np.clip(noisy, 0.0, 1.0)
33
+
34
+ # --- Denoise with the autoencoder ---
35
+ inp = noisy[np.newaxis, ..., np.newaxis] # shape (1,64,64,1)
36
+ pred = model.predict(inp)[0, ..., 0] # shape (64,64)
37
+
38
+ # --- Convert all to uint8 for display ---
39
+ orig_disp = (orig_norm * 255).astype(np.uint8)
40
+ noisy_disp = (noisy * 255).astype(np.uint8)
41
+ recon_disp = (pred * 255).astype(np.uint8)
42
+
43
+ return orig_disp, noisy_disp, recon_disp
44
+
45
+ # 3. Build Gradio interface with 3 outputs
46
+ demo = gr.Interface(
47
+ fn=process_and_denoise,
48
+ inputs=gr.Image(type="numpy", label="Input Image"),
49
+ outputs=[
50
+ gr.Image(type="numpy", label="Original (64×64)"),
51
+ gr.Image(type="numpy", label="Noisy (σ=0.1)"),
52
+ gr.Image(type="numpy", label="Reconstructed")
53
+ ],
54
+ title="Denoising Autoencoder Demo",
55
+ description=(
56
+ "Upload any image (grayscale or color). "
57
+ "This app converts it to 64×64 grayscale, adds Gaussian noise, "
58
+ "then denoises it with the trained autoencoder. "
59
+ "See all three side by side!"
60
+ )
61
+ )
62
+
63
+ if __name__ == "__main__":
64
+ demo.launch(server_name="0.0.0.0", share=True)