Spaces:
Running
Running
Upload 2 files
Browse files- super_resolution/__init__.py +1 -0
- super_resolution/bsrgan.py +84 -0
super_resolution/__init__.py
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
from .bsrgan import BSRGAN
|
super_resolution/bsrgan.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
|
3 |
+
import cv2
|
4 |
+
|
5 |
+
from insightface import model_zoo
|
6 |
+
from dofaker.utils import download_file, get_model_url
|
7 |
+
|
8 |
+
|
9 |
+
class BSRGAN:
|
10 |
+
|
11 |
+
def __init__(self, name='bsrgan', root='weights/models', scale=1) -> None:
|
12 |
+
_, model_file = download_file(get_model_url(name),
|
13 |
+
save_dir=root,
|
14 |
+
overwrite=False)
|
15 |
+
self.scale = scale
|
16 |
+
providers = model_zoo.model_zoo.get_default_providers()
|
17 |
+
self.session = model_zoo.model_zoo.PickableInferenceSession(
|
18 |
+
model_file, providers=providers)
|
19 |
+
|
20 |
+
self.input_mean = 0.0
|
21 |
+
self.input_std = 255.0
|
22 |
+
inputs = self.session.get_inputs()
|
23 |
+
self.input_names = []
|
24 |
+
for inp in inputs:
|
25 |
+
self.input_names.append(inp.name)
|
26 |
+
outputs = self.session.get_outputs()
|
27 |
+
output_names = []
|
28 |
+
for out in outputs:
|
29 |
+
output_names.append(out.name)
|
30 |
+
self.output_names = output_names
|
31 |
+
assert len(
|
32 |
+
self.output_names
|
33 |
+
) == 1, "The output number of BSRGAN model should be 1, but got {}, please check your model.".format(
|
34 |
+
len(self.output_names))
|
35 |
+
output_shape = outputs[0].shape
|
36 |
+
input_cfg = inputs[0]
|
37 |
+
input_shape = input_cfg.shape
|
38 |
+
self.input_shape = input_shape
|
39 |
+
print('image super resolution shape:', self.input_shape)
|
40 |
+
|
41 |
+
def forward(self, image, image_format='bgr'):
|
42 |
+
if isinstance(image, str):
|
43 |
+
image = cv2.imread(image, 1)
|
44 |
+
image_format = 'bgr'
|
45 |
+
elif isinstance(image, np.ndarray):
|
46 |
+
if image_format == 'bgr':
|
47 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
48 |
+
elif image_format == 'rgb':
|
49 |
+
pass
|
50 |
+
else:
|
51 |
+
raise UserWarning(
|
52 |
+
"BSRGAN not support image format {}".format(image_format))
|
53 |
+
else:
|
54 |
+
raise UserWarning(
|
55 |
+
"BSRGAN input must be str or np.ndarray, but got {}.".format(
|
56 |
+
type(image)))
|
57 |
+
img = (image - self.input_mean) / self.input_std
|
58 |
+
pred = self.session.run(self.output_names,
|
59 |
+
{self.input_names[0]: img})[0]
|
60 |
+
return pred
|
61 |
+
|
62 |
+
def get(self, img, image_format='bgr'):
|
63 |
+
if image_format.lower() == 'bgr':
|
64 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
65 |
+
elif image_format.lower() == 'rgb':
|
66 |
+
pass
|
67 |
+
else:
|
68 |
+
raise UserWarning(
|
69 |
+
"gfpgan not support image format {}".format(image_format))
|
70 |
+
h, w, c = img.shape
|
71 |
+
blob = cv2.dnn.blobFromImage(
|
72 |
+
img,
|
73 |
+
1.0 / self.input_std, (w, h),
|
74 |
+
(self.input_mean, self.input_mean, self.input_mean),
|
75 |
+
swapRB=False)
|
76 |
+
pred = self.session.run(self.output_names,
|
77 |
+
{self.input_names[0]: blob})[0]
|
78 |
+
image_aug = pred.transpose((0, 2, 3, 1))[0]
|
79 |
+
rgb_aug = np.clip(self.input_std * image_aug + self.input_mean, 0,
|
80 |
+
255).astype(np.uint8)
|
81 |
+
rgb_aug = cv2.resize(rgb_aug,
|
82 |
+
(int(w * self.scale), int(h * self.scale)))
|
83 |
+
bgr_aug = rgb_aug[:, :, ::-1]
|
84 |
+
return bgr_aug
|