Spaces:
Sleeping
Sleeping
Added EDI
Browse files- app.py +64 -7
- models/edge_directed_interpolation.py +49 -0
app.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Existing imports
|
| 6 |
from models.lancros_interpolation import upsample_lancros
|
|
@@ -12,18 +14,64 @@ from models.sr_gan import srgan_upscale
|
|
| 12 |
# ✅ New import for Random Forest Super Resolution
|
| 13 |
from CV_Project.models.random_forest_sr import random_forest_upscale
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
lancros_page = gr.Interface(
|
| 16 |
fn=upsample_lancros,
|
| 17 |
-
inputs=[gr.Image(label="Low Resolution Image"),
|
| 18 |
-
gr.Slider(2, 6, step=1, value=2, label="Upscaling Factor"),],
|
| 19 |
outputs=gr.Image(type="pil", label="High Resolution Images"),
|
| 20 |
title="Lancros Upsampling"
|
| 21 |
)
|
| 22 |
|
| 23 |
fourier_page = gr.Interface(
|
| 24 |
fn=fourier_upscale,
|
| 25 |
-
inputs=[gr.Image(label="Low Resolution Image"),
|
| 26 |
-
gr.Slider(2, 6, step=1, value=2, label="Upscaling Factor"),],
|
| 27 |
outputs=gr.Image(type="pil", label="High Resolution Images"),
|
| 28 |
title="Fourier Upsampling"
|
| 29 |
)
|
|
@@ -49,7 +97,6 @@ srgan_page = gr.Interface(
|
|
| 49 |
title="GAN based Super Resolution"
|
| 50 |
)
|
| 51 |
|
| 52 |
-
# ✅ Random Forest page
|
| 53 |
random_forest_page = gr.Interface(
|
| 54 |
fn=random_forest_upscale,
|
| 55 |
inputs=[gr.Image(label="Low Resolution Image")],
|
|
@@ -57,9 +104,19 @@ random_forest_page = gr.Interface(
|
|
| 57 |
title="Random Forest based Super Resolution"
|
| 58 |
)
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
demo = gr.TabbedInterface(
|
| 61 |
-
[lancros_page, fourier_page, autoencoder_page, srgan_page,espcn_page,random_forest_page],
|
| 62 |
-
["Lancros Interpolation", "Fourier Interpolation", "Autoencoder based Super Resolution", "GAN based Super Resolution",
|
|
|
|
| 63 |
title="Image Super Resolution"
|
| 64 |
)
|
| 65 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
import os
|
| 4 |
+
import numpy as np
|
| 5 |
+
import cv2
|
| 6 |
|
| 7 |
# Existing imports
|
| 8 |
from models.lancros_interpolation import upsample_lancros
|
|
|
|
| 14 |
# ✅ New import for Random Forest Super Resolution
|
| 15 |
from CV_Project.models.random_forest_sr import random_forest_upscale
|
| 16 |
|
| 17 |
+
# ✅ EDI (Edge Directed Interpolation) method
|
| 18 |
+
def edge_directed_interpolation(lr_img_pil, scale=2):
|
| 19 |
+
# Convert to grayscale numpy array
|
| 20 |
+
lr_img = np.array(lr_img_pil.convert("L"))
|
| 21 |
+
h, w = lr_img.shape
|
| 22 |
+
hr_h, hr_w = h * scale, w * scale
|
| 23 |
+
|
| 24 |
+
hr_img = np.zeros((hr_h, hr_w), dtype=np.uint8)
|
| 25 |
+
|
| 26 |
+
# Copy original pixels to even positions
|
| 27 |
+
for i in range(h):
|
| 28 |
+
for j in range(w):
|
| 29 |
+
hr_img[i * scale, j * scale] = lr_img[i, j]
|
| 30 |
+
|
| 31 |
+
# Interpolate diagonal pixels
|
| 32 |
+
for i in range(0, hr_h, scale):
|
| 33 |
+
for j in range(0, hr_w, scale):
|
| 34 |
+
if i + scale < hr_h and j + scale < hr_w:
|
| 35 |
+
p1 = hr_img[i, j]
|
| 36 |
+
p2 = hr_img[i, j + scale]
|
| 37 |
+
p3 = hr_img[i + scale, j]
|
| 38 |
+
p4 = hr_img[i + scale, j + scale]
|
| 39 |
+
|
| 40 |
+
d1 = abs(int(p1) - int(p4))
|
| 41 |
+
d2 = abs(int(p2) - int(p3))
|
| 42 |
+
|
| 43 |
+
interp = (int(p1) + int(p4)) // 2 if d1 < d2 else (int(p2) + int(p3)) // 2
|
| 44 |
+
hr_img[i + scale // 2, j + scale // 2] = interp
|
| 45 |
+
|
| 46 |
+
# Fill remaining zero pixels
|
| 47 |
+
for i in range(0, hr_h):
|
| 48 |
+
for j in range(0, hr_w):
|
| 49 |
+
if hr_img[i, j] == 0:
|
| 50 |
+
neighbors = []
|
| 51 |
+
if i - 1 >= 0:
|
| 52 |
+
neighbors.append(hr_img[i - 1, j])
|
| 53 |
+
if i + 1 < hr_h:
|
| 54 |
+
neighbors.append(hr_img[i + 1, j])
|
| 55 |
+
if j - 1 >= 0:
|
| 56 |
+
neighbors.append(hr_img[i, j - 1])
|
| 57 |
+
if j + 1 < hr_w:
|
| 58 |
+
neighbors.append(hr_img[i, j + 1])
|
| 59 |
+
if neighbors:
|
| 60 |
+
hr_img[i, j] = np.mean(neighbors).astype(np.uint8)
|
| 61 |
+
|
| 62 |
+
return Image.fromarray(hr_img)
|
| 63 |
+
|
| 64 |
+
# === Interfaces === #
|
| 65 |
lancros_page = gr.Interface(
|
| 66 |
fn=upsample_lancros,
|
| 67 |
+
inputs=[gr.Image(label="Low Resolution Image"), gr.Slider(2, 6, step=1, value=2, label="Upscaling Factor")],
|
|
|
|
| 68 |
outputs=gr.Image(type="pil", label="High Resolution Images"),
|
| 69 |
title="Lancros Upsampling"
|
| 70 |
)
|
| 71 |
|
| 72 |
fourier_page = gr.Interface(
|
| 73 |
fn=fourier_upscale,
|
| 74 |
+
inputs=[gr.Image(label="Low Resolution Image"), gr.Slider(2, 6, step=1, value=2, label="Upscaling Factor")],
|
|
|
|
| 75 |
outputs=gr.Image(type="pil", label="High Resolution Images"),
|
| 76 |
title="Fourier Upsampling"
|
| 77 |
)
|
|
|
|
| 97 |
title="GAN based Super Resolution"
|
| 98 |
)
|
| 99 |
|
|
|
|
| 100 |
random_forest_page = gr.Interface(
|
| 101 |
fn=random_forest_upscale,
|
| 102 |
inputs=[gr.Image(label="Low Resolution Image")],
|
|
|
|
| 104 |
title="Random Forest based Super Resolution"
|
| 105 |
)
|
| 106 |
|
| 107 |
+
# ✅ EDI Page
|
| 108 |
+
edi_page = gr.Interface(
|
| 109 |
+
fn=edge_directed_interpolation,
|
| 110 |
+
inputs=[gr.Image(label="Low Resolution Image"), gr.Slider(2, 4, step=1, value=2, label="Upscaling Factor")],
|
| 111 |
+
outputs=gr.Image(type="pil", label="High Resolution Image"),
|
| 112 |
+
title="Edge Directed Interpolation"
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
# === Tabs === #
|
| 116 |
demo = gr.TabbedInterface(
|
| 117 |
+
[lancros_page, fourier_page, autoencoder_page, srgan_page, espcn_page, random_forest_page, edi_page],
|
| 118 |
+
["Lancros Interpolation", "Fourier Interpolation", "Autoencoder based Super Resolution", "GAN based Super Resolution",
|
| 119 |
+
"EspCN Super Resolution", "Random Forest based Super Resolution", "Edge Directed Interpolation"],
|
| 120 |
title="Image Super Resolution"
|
| 121 |
)
|
| 122 |
|
models/edge_directed_interpolation.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import cv2
|
| 3 |
+
|
| 4 |
+
def edge_directed_interpolation(lr_img_pil, scale=2):
|
| 5 |
+
lr_img = np.array(lr_img_pil.convert("L"))
|
| 6 |
+
h, w = lr_img.shape
|
| 7 |
+
hr_h, hr_w = h * scale, w * scale
|
| 8 |
+
|
| 9 |
+
hr_img = np.zeros((hr_h, hr_w), dtype=np.uint8)
|
| 10 |
+
|
| 11 |
+
for i in range(h):
|
| 12 |
+
for j in range(w):
|
| 13 |
+
hr_img[i * scale, j * scale] = lr_img[i, j]
|
| 14 |
+
|
| 15 |
+
for i in range(0, hr_h, scale):
|
| 16 |
+
for j in range(0, hr_w, scale):
|
| 17 |
+
if i + scale < hr_h and j + scale < hr_w:
|
| 18 |
+
p1 = hr_img[i, j]
|
| 19 |
+
p2 = hr_img[i, j + scale]
|
| 20 |
+
p3 = hr_img[i + scale, j]
|
| 21 |
+
p4 = hr_img[i + scale, j + scale]
|
| 22 |
+
|
| 23 |
+
d1 = abs(int(p1) - int(p4))
|
| 24 |
+
d2 = abs(int(p2) - int(p3))
|
| 25 |
+
|
| 26 |
+
if d1 < d2:
|
| 27 |
+
interp = (int(p1) + int(p4)) // 2
|
| 28 |
+
else:
|
| 29 |
+
interp = (int(p2) + int(p3)) // 2
|
| 30 |
+
|
| 31 |
+
hr_img[i + scale // 2, j + scale // 2] = interp
|
| 32 |
+
|
| 33 |
+
for i in range(hr_h):
|
| 34 |
+
for j in range(hr_w):
|
| 35 |
+
if hr_img[i, j] == 0:
|
| 36 |
+
neighbors = []
|
| 37 |
+
if i - 1 >= 0:
|
| 38 |
+
neighbors.append(hr_img[i - 1, j])
|
| 39 |
+
if i + 1 < hr_h:
|
| 40 |
+
neighbors.append(hr_img[i + 1, j])
|
| 41 |
+
if j - 1 >= 0:
|
| 42 |
+
neighbors.append(hr_img[i, j - 1])
|
| 43 |
+
if j + 1 < hr_w:
|
| 44 |
+
neighbors.append(hr_img[i, j + 1])
|
| 45 |
+
if neighbors:
|
| 46 |
+
hr_img[i, j] = np.mean(neighbors).astype(np.uint8)
|
| 47 |
+
|
| 48 |
+
hr_img_pil = Image.fromarray(hr_img)
|
| 49 |
+
return hr_img_pil
|