-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
55 lines (40 loc) · 1.63 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from image_resizer import image_preprocess_resizing, image_postprocess_resizing
import cv2
import numpy as np
import onnxruntime as rt
model_path="pretrained_models/RealESRGAN_ANIME_6B_512x512.onnx"
sess = rt.InferenceSession(model_path, providers=['CPUExecutionProvider'])
input_name = sess.get_inputs()[0].name
img = cv2.imread("input.jpg", cv2.IMREAD_UNCHANGED)
img, padding, old_dims = image_preprocess_resizing(img, square_size=512) # 512 is the size of the image you want to resize to (512x512)
h_input, w_input = img.shape[0:2]
img = img.astype(np.float32)
if np.max(img) > 256: # 16-bit image
max_range = 65535
print('Input is a 16-bit image')
else:
print('Input is not a 16-bit image resetting to 8-bit...')
max_range = 255
# Normalize the image
img = img / max_range
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = np.transpose(img, (2, 0, 1)).astype(np.float16)
# inference
pred = sess.run(None, {input_name: img[None, ...]})[0]
# Convert back to 8-bit image
outscale = 4
output_img = pred[0].transpose(1,2,0)
# Denormalize the image
if max_range == 65535: # 16-bit image
output = (output_img * 65535.0).round().astype(np.uint16)
else:
output = (output_img * 255.0).round().astype(np.uint8)
output = cv2.resize(
output, (
int(w_input * outscale),
int(h_input * outscale),
), interpolation=cv2.INTER_LANCZOS4)
# Save the output image
output = cv2.cvtColor(output, cv2.COLOR_RGB2BGR)
output = image_postprocess_resizing(output, padding, old_dims, outscale) # Here you can change the upscale factor default:1
cv2.imwrite("result.jpg", output)