Janeka commited on
Commit
b967e7e
·
verified ·
1 Parent(s): 57ab916

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -31
app.py CHANGED
@@ -1,37 +1,49 @@
1
  import gradio as gr
2
- import cv2
3
- import numpy as np
4
  from PIL import Image
 
 
5
 
6
- def enhance_image(input_img):
7
- try:
8
- # Convert to numpy array if needed
9
- if isinstance(input_img, Image.Image):
10
- input_img = np.array(input_img)
11
-
12
- # Initialize OpenCV super-resolution
13
- sr = cv2.dnn_superres.DnnSuperResImpl_create()
14
-
15
- # Load a lightweight model (e.g., EDSR x2)
16
- # Note: You'll need to add "EDSR_x2.pb" to your Space!
17
- sr.readModel("EDSR_x2.pb")
18
- sr.setModel("edsr", 2) # 2x scaling
19
-
20
- # Upscale
21
- enhanced_img = sr.upsample(input_img)
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
- iface = gr.Interface(
30
- fn=enhance_image,
31
- inputs=gr.Image(label="Upload Image"),
32
- outputs=gr.Image(label="Enhanced"),
33
- title="🚀 CPU Image Enhancer",
34
- description="Upscales images 2x using OpenCV (runs on CPU)."
35
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- iface.launch()
 
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()