Mehedi258456 commited on
Commit
d152c13
·
verified ·
1 Parent(s): 5923489

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -15
app.py CHANGED
@@ -1,31 +1,49 @@
1
  import gradio as gr
2
- from PIL import Image
3
  import numpy as np
 
 
4
  import torch
5
  from basicsr.archs.rrdbnet_arch import RRDBNet
6
  from realesrgan import RealESRGANer
7
 
8
- model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64,
9
- num_block=23, num_grow_ch=32, scale=4)
10
-
11
- upscaler = RealESRGANer(
12
  scale=4,
13
  model_path='RealESRGAN_x4plus.pth',
14
- model=model,
15
- tile=200,
16
  tile_pad=10,
17
  pre_pad=0,
18
- half=torch.cuda.is_available()
 
19
  )
20
 
21
- def upscale_image(img):
22
  try:
23
- img = np.array(img)
24
- output, _ = upscaler.enhance(img, outscale=4)
25
- return Image.fromarray(output)
 
 
 
 
 
26
  except Exception as e:
27
  return f"Error: {str(e)}"
28
 
29
- gr.Interface(fn=upscale_image, inputs=gr.Image(type="pil"), outputs=gr.Image(type="pil"),
30
- title="AI Image Upscaler 4x",
31
- description="Upload an image to upscale using RealESRGAN (4x)").launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import cv2
3
  import numpy as np
4
+ from PIL import Image
5
+ import os
6
  import torch
7
  from basicsr.archs.rrdbnet_arch import RRDBNet
8
  from realesrgan import RealESRGANer
9
 
10
+ # Load Real-ESRGAN Model
11
+ model = RealESRGANer(
 
 
12
  scale=4,
13
  model_path='RealESRGAN_x4plus.pth',
14
+ model=RRDBNet(num_in_ch=3, num_out_ch=3, nf=64, nb=23, gc=32, scale=4, group=1, norm_type=None, act_type='leakyrelu', mode='NCHW', convtype='Conv'),
15
+ tile=0,
16
  tile_pad=10,
17
  pre_pad=0,
18
+ half=True,
19
+ gpu_id=None if not torch.cuda.is_available() else 0
20
  )
21
 
22
+ def upscale_image(input_image, scale, file_type):
23
  try:
24
+ img = np.array(input_image)
25
+ output, _ = model.enhance(img, outscale=scale)
26
+ output_pil = Image.fromarray(output)
27
+
28
+ output_path = "upscaled_output." + file_type.lower()
29
+ output_pil.save(output_path, dpi=(300, 300))
30
+
31
+ return output_path
32
  except Exception as e:
33
  return f"Error: {str(e)}"
34
 
35
+ with gr.Blocks() as demo:
36
+ gr.Markdown("## 🖼️ AI Image Upscaler (Real-ESRGAN Powered)")
37
+
38
+ with gr.Row():
39
+ image_input = gr.Image(type="pil", label="Upload Image")
40
+ scale_input = gr.Radio([2, 4, 6, 8], label="Upscale Factor", value=4)
41
+ format_input = gr.Dropdown(["PNG", "JPEG", "TIFF"], label="Export Format", value="PNG")
42
+
43
+ output_image = gr.File(label="Download Upscaled Image")
44
+
45
+ upscale_btn = gr.Button("Upscale & Download")
46
+
47
+ upscale_btn.click(fn=upscale_image, inputs=[image_input, scale_input, format_input], outputs=output_image)
48
+
49
+ demo.launch()