This repository has been archived by the owner on Sep 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict-bicubic-enhance.py
66 lines (48 loc) · 1.95 KB
/
predict-bicubic-enhance.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
55
56
57
58
59
60
61
62
63
64
65
# import specific necessary packages
from keras.models import Sequential, load_model, Input, Model
#from keras.layers import Conv2D, Conv2DTranspose, Input, Activation
#from keras.optimizers import SGD, Adam
import cv2
import numpy as np
import math
import os
# define main prediction function
def predict():
# Display weight summary
print("Loading trained weight...")
model = load_model('my_model-fsrcnn-anime-tanakitint.h5')
model.summary()
# get scale settings from text file ---
# default bicubic scale is 2
try:
settings = open("settings/settings_bicubic.txt", "r")
bicubic_scale = settings.read()
BICUBIC_SCALE = float(bicubic_scale) #stored in variable
except Exception as e:
print("Error occured. You might be misconfig the settings, please check the bicubic settings again.")
print("Please make sure that bicubic setting must not a zero or negative numbers." + '\n')
print("Error! " + str(e) + '\n')
INPUT_NAME = "input/1.png"
OUTPUT_NAME = "output/1-bicubic-enhanced.png"
# Read image
img = cv2.imread(INPUT_NAME, cv2.IMREAD_COLOR)
# Enlarge image with Bicubic interpolation method
img = cv2.resize(img, None, fx=BICUBIC_SCALE, fy=BICUBIC_SCALE, interpolation=cv2.INTER_CUBIC)
# convert from BGR to YCrCb
img = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
shape = img.shape
Y_img = cv2.resize(img[:, :, 0], (shape[1], shape[0]), cv2.INTER_CUBIC)
Y = np.zeros((1, img.shape[0], img.shape[1], 1), dtype=float)
Y[0, :, :, 0] = Y_img.astype(float) / 255.
# prediction
pre = model.predict(Y, batch_size=1) * 255.
pre[pre[:] > 255] = 255
pre[pre[:] < 0] = 0
pre = pre.astype(np.uint8)
img[:, : ,0] = pre[0, :, :, 0]
# convert from YCrCb to BGR and save image
img = cv2.cvtColor(img, cv2.COLOR_YCrCb2BGR)
cv2.imwrite(OUTPUT_NAME, img)
# print success!
print("Prediction success!")
predict()