Spaces:
Build error
Build error
ali-ghamdan
commited on
Commit
•
ec603d9
1
Parent(s):
ffef5dd
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,47 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
from basicsr.archs.rrdbnet_arch import RRDBNet
|
6 |
|
7 |
+
from realesrgan import RealESRGANer
|
8 |
+
from realesrgan.archs.srvgg_arch import SRVGGNetCompact
|
9 |
|
10 |
+
|
11 |
+
def upscale(image: Image, model: str, outscale: int = 4, name: str = None):
|
12 |
+
if model.startswith("https://"):
|
13 |
+
model_name = name
|
14 |
+
else:
|
15 |
+
model_name = model.split('.')[0]
|
16 |
+
if model_name in ['RealESRGAN_x4plus', 'RealESRNet_x4plus']: # x4 RRDBNet model
|
17 |
+
m = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
|
18 |
+
netscale = 4
|
19 |
+
elif model_name in ['RealESRGAN_x4plus_anime_6B']: # x4 RRDBNet model with 6 blocks
|
20 |
+
m = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=6, num_grow_ch=32, scale=4)
|
21 |
+
netscale = 4
|
22 |
+
elif model_name in ['RealESRGAN_x2plus']: # x2 RRDBNet model
|
23 |
+
m = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2)
|
24 |
+
netscale = 2
|
25 |
+
elif model_name in ['realesr-animevideov3']: # x4 VGG-style model (XS size)
|
26 |
+
m = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=16, upscale=4, act_type='prelu')
|
27 |
+
netscale = 4
|
28 |
+
|
29 |
+
upsampler = RealESRGANer(
|
30 |
+
scale=netscale, model_path=model, model=m, device='cpu', half=False, tile=0, tile_pad=10, pre_pad=0)
|
31 |
+
|
32 |
+
img = cv2.imread(image, cv2.IMREAD_UNCHANGED)
|
33 |
+
|
34 |
+
try:
|
35 |
+
output = upsampler.enhance(img, outscale=outscale)
|
36 |
+
except Exception as e:
|
37 |
+
print(e)
|
38 |
+
return None
|
39 |
+
else:
|
40 |
+
return np.asarray(Image.fromarray(cv2.cvtColor(output, cv2.COLOR_BGR2RGB)))
|
41 |
+
|
42 |
+
|
43 |
+
gr.Interface(
|
44 |
+
upscale, [gr.Image(type="pil"), 'text', 'text', 'text'],
|
45 |
+
gr.Image(type="pil", label="Output"),
|
46 |
+
title='hi',
|
47 |
+
description='desc').launch()
|