Janeka commited on
Commit
75f3697
·
verified ·
1 Parent(s): 4483744

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -17
app.py CHANGED
@@ -1,31 +1,41 @@
1
  import gradio as gr
2
- from basicsr.archs.rrdbnet_arch import RRDBNet
 
3
  from realesrgan import RealESRGANer
 
4
 
5
  # Initialize Real-ESRGAN
6
- model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32)
7
- upsampler = RealESRGANer(
8
- scale=4,
9
- model_path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth",
10
- model=model,
11
- tile=400, # Reduce if you get memory errors
12
- tile_pad=10
13
- )
 
 
 
14
 
15
- def enhance(image):
16
- # Convert to RGB if needed
17
- if image.mode != 'RGB':
18
- image = image.convert('RGB')
 
19
 
20
  # Enhance image
21
- output, _ = upsampler.enhance(np.array(image), outscale=4)
 
22
  return Image.fromarray(output)
23
 
 
24
  demo = gr.Interface(
25
- fn=enhance,
26
  inputs=gr.Image(type="pil", label="Input Image"),
27
- outputs=gr.Image(type="pil", label="Enhanced (4x)"),
28
- title="Professional Image Enhancement"
 
 
29
  )
30
 
31
  demo.launch()
 
1
  import gradio as gr
2
+ import numpy as np
3
+ from PIL import Image
4
  from realesrgan import RealESRGANer
5
+ from basicsr.archs.rrdbnet_arch import RRDBNet
6
 
7
  # Initialize Real-ESRGAN
8
+ def init_enhancer():
9
+ model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32)
10
+ return RealESRGANer(
11
+ scale=4,
12
+ model_path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth",
13
+ model=model,
14
+ tile=400,
15
+ tile_pad=10,
16
+ pre_pad=0,
17
+ half=False
18
+ )
19
 
20
+ enhancer = init_enhancer()
21
+
22
+ def enhance_image(input_img):
23
+ # Convert to numpy array
24
+ img_array = np.array(input_img)
25
 
26
  # Enhance image
27
+ output, _ = enhancer.enhance(img_array, outscale=2) # 2x upscale
28
+
29
  return Image.fromarray(output)
30
 
31
+ # Gradio interface
32
  demo = gr.Interface(
33
+ fn=enhance_image,
34
  inputs=gr.Image(type="pil", label="Input Image"),
35
+ outputs=gr.Image(type="pil", label="Enhanced"),
36
+ title="Professional Image Enhancement",
37
+ examples=["example.jpg"] if os.path.exists("example.jpg") else None,
38
+ allow_flagging="never"
39
  )
40
 
41
  demo.launch()