Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,41 +1,46 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
from PIL import Image
|
4 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
def super_resolve_image(input_image,
|
7 |
-
if input_image is None:
|
8 |
return None
|
9 |
|
10 |
try:
|
11 |
-
#
|
12 |
-
if isinstance(input_image, Image.Image):
|
13 |
-
input_image =
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
-
input_image = cv2.cvtColor(input_image, cv2.COLOR_GRAY2RGB)
|
18 |
|
19 |
-
#
|
20 |
-
|
21 |
-
cv2.INTER_LINEAR, # Bilinear interpolation
|
22 |
-
cv2.INTER_CUBIC, # Bicubic interpolation
|
23 |
-
cv2.INTER_LANCZOS4 # Lanczos interpolation
|
24 |
-
]
|
25 |
|
26 |
-
#
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
interpolation=method
|
33 |
-
)
|
34 |
-
return upscaled
|
35 |
-
except Exception as e:
|
36 |
-
print(f"Interpolation method failed: {method}")
|
37 |
|
38 |
-
return
|
39 |
|
40 |
except Exception as e:
|
41 |
print(f"Super-resolution error: {e}")
|
@@ -43,26 +48,35 @@ def super_resolve_image(input_image, scale_factor=4):
|
|
43 |
|
44 |
def create_gradio_interface():
|
45 |
with gr.Blocks() as demo:
|
46 |
-
gr.Markdown("# 🖼️
|
47 |
|
48 |
with gr.Row():
|
49 |
input_image = gr.Image(label="Input Image", type="pil")
|
50 |
-
output_image = gr.Image(label="
|
51 |
|
52 |
with gr.Row():
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
57 |
)
|
58 |
|
59 |
enhance_btn = gr.Button("Enhance Image Resolution")
|
60 |
|
61 |
enhance_btn.click(
|
62 |
fn=super_resolve_image,
|
63 |
-
inputs=[input_image,
|
64 |
outputs=output_image
|
65 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
return demo
|
68 |
|
|
|
1 |
import gradio as gr
|
2 |
+
import requests
|
3 |
from PIL import Image
|
4 |
+
from io import BytesIO
|
5 |
+
from diffusers import LDMSuperResolutionPipeline
|
6 |
+
import torch
|
7 |
+
import numpy as np
|
8 |
+
|
9 |
+
# Device configuration
|
10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
+
model_id = "CompVis/ldm-super-resolution-4x-openimages"
|
12 |
+
|
13 |
+
# Load model
|
14 |
+
try:
|
15 |
+
pipeline = LDMSuperResolutionPipeline.from_pretrained(model_id)
|
16 |
+
pipeline = pipeline.to(device)
|
17 |
+
except Exception as e:
|
18 |
+
print(f"Model loading error: {e}")
|
19 |
+
pipeline = None
|
20 |
|
21 |
+
def super_resolve_image(input_image, num_steps=50):
|
22 |
+
if input_image is None or pipeline is None:
|
23 |
return None
|
24 |
|
25 |
try:
|
26 |
+
# Ensure input is PIL Image
|
27 |
+
if not isinstance(input_image, Image.Image):
|
28 |
+
input_image = Image.fromarray(input_image)
|
29 |
|
30 |
+
# Resize input to 128x128 if needed
|
31 |
+
input_image = input_image.resize((128, 128), Image.LANCZOS)
|
|
|
32 |
|
33 |
+
# Ensure image is RGB
|
34 |
+
input_image = input_image.convert("RGB")
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
# Run super resolution
|
37 |
+
upscaled_image = pipeline(
|
38 |
+
input_image,
|
39 |
+
num_inference_steps=num_steps,
|
40 |
+
eta=1
|
41 |
+
).images[0]
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
+
return np.array(upscaled_image)
|
44 |
|
45 |
except Exception as e:
|
46 |
print(f"Super-resolution error: {e}")
|
|
|
48 |
|
49 |
def create_gradio_interface():
|
50 |
with gr.Blocks() as demo:
|
51 |
+
gr.Markdown("# 🖼️ LDM Super Resolution")
|
52 |
|
53 |
with gr.Row():
|
54 |
input_image = gr.Image(label="Input Image", type="pil")
|
55 |
+
output_image = gr.Image(label="Super-Resolved Image")
|
56 |
|
57 |
with gr.Row():
|
58 |
+
num_steps = gr.Slider(
|
59 |
+
minimum=10,
|
60 |
+
maximum=200,
|
61 |
+
value=50,
|
62 |
+
label="Inference Steps"
|
63 |
)
|
64 |
|
65 |
enhance_btn = gr.Button("Enhance Image Resolution")
|
66 |
|
67 |
enhance_btn.click(
|
68 |
fn=super_resolve_image,
|
69 |
+
inputs=[input_image, num_steps],
|
70 |
outputs=output_image
|
71 |
)
|
72 |
+
|
73 |
+
# Example images for quick testing
|
74 |
+
gr.Examples(
|
75 |
+
examples=[
|
76 |
+
"https://user-images.githubusercontent.com/38061659/199705896-b48e17b8-b231-47cd-a270-4ffa5a93fa3e.png"
|
77 |
+
],
|
78 |
+
inputs=input_image
|
79 |
+
)
|
80 |
|
81 |
return demo
|
82 |
|