Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,49 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import numpy as np
|
| 4 |
from PIL import Image
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
def enhance_image(
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
return Image.fromarray(enhanced_img)
|
| 24 |
-
except Exception as e:
|
| 25 |
-
print(f"Error: {e}")
|
| 26 |
-
return input_img # Fallback to original
|
| 27 |
|
| 28 |
# Gradio Interface
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from super_image import EdsrModel, ImageLoader
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
+
import requests
|
| 5 |
+
import io
|
| 6 |
|
| 7 |
+
def enhance_image(input_image, scale: int = 2):
|
| 8 |
+
# Convert Gradio input to PIL Image
|
| 9 |
+
if isinstance(input_image, str): # If it's a file path
|
| 10 |
+
image = Image.open(input_image)
|
| 11 |
+
else: # If it's a numpy array from Gradio
|
| 12 |
+
image = Image.fromarray(input_image)
|
| 13 |
+
|
| 14 |
+
# Load the pre-trained EDSR model
|
| 15 |
+
model = EdsrModel.from_pretrained('eugenesiow/edsr-base', scale=scale)
|
| 16 |
+
|
| 17 |
+
# Process the image
|
| 18 |
+
inputs = ImageLoader.load_image(image)
|
| 19 |
+
preds = model(inputs)
|
| 20 |
+
|
| 21 |
+
# Return the enhanced image
|
| 22 |
+
return ImageLoader.save_image(preds, output_path=None) # Returns PIL Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Gradio Interface
|
| 25 |
+
with gr.Blocks() as demo:
|
| 26 |
+
gr.Markdown("## 🚀 EDSR Image Upscaler (2x, 3x, 4x)")
|
| 27 |
+
with gr.Row():
|
| 28 |
+
with gr.Column():
|
| 29 |
+
input_image = gr.Image(label="Upload Image", type="pil")
|
| 30 |
+
scale = gr.Dropdown(choices=[2, 3, 4], value=2, label="Upscale Factor")
|
| 31 |
+
submit_btn = gr.Button("Enhance")
|
| 32 |
+
with gr.Column():
|
| 33 |
+
output_image = gr.Image(label="Enhanced Image")
|
| 34 |
+
|
| 35 |
+
submit_btn.click(
|
| 36 |
+
fn=enhance_image,
|
| 37 |
+
inputs=[input_image, scale],
|
| 38 |
+
outputs=output_image
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
gr.Markdown("""
|
| 42 |
+
**How it works:**
|
| 43 |
+
- Uses EDSR (Enhanced Deep Super-Resolution) base model
|
| 44 |
+
- Supports 2x, 3x, and 4x upscaling
|
| 45 |
+
- Runs on CPU (may be slow for large images)
|
| 46 |
+
- Try with images <1000px for best performance
|
| 47 |
+
""")
|
| 48 |
|
| 49 |
+
demo.launch()
|