-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.py
161 lines (131 loc) · 4.51 KB
/
utils.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""
Some codes from https://github.com/Newmu/dcgan_code
"""
import math
import json
import pprint
from PIL import Image
import scipy.misc
import numpy as np
import math
import pdb
from skimage.transform import resize
pp = pprint.PrettyPrinter()
get_stddev = lambda x, k_h, k_w: 1/math.sqrt(k_w*k_h*x.get_shape()[-1])
def create_mask(images):
mask = [images >-1.][0]*1.
return mask
def get_image(input_,sr_label_,ang_label_,image_size,is_crop=True):
LF_wid = input_.shape[1]
LF_hei = input_.shape[0]
randx = np.random.randint(LF_wid - image_size)
randy = np.random.randint(LF_hei - image_size)
inputs = transform(input_,randx,randy,image_size)
sr_gt = transform(sr_label_,randx,randy,image_size)
ang_gt = transform(np.expand_dims(ang_label_,axis=-1),randx,randy,image_size)
return np.concatenate((inputs,sr_gt,ang_gt),axis=2)
def get_image_test(input_):
inputs = input_/255.0
return inputs
"""
def get_image_test(input_,sr_label_,ang_label_):
inputs = input_/255.0
sr_gt = sr_label_
ang_gt = np.expand_dims(ang_label_,axis=-1)
return np.concatenate((inputs,sr_gt,ang_gt),axis=2)
"""
def imread(path):
#image = Image.open(path)
#image = image.convert('YCbCr')
image = scipy.misc.imread(path).astype(np.float)
return image
def rgb2ycbcr(image):
Y = (0.257*image[:,:,0])+(0.504*image[:,:,1])+(0.098*image[:,:,2]) + 16.0
return Y
def save_images(images, size, image_path):
return imsave(inverse_transform(images), size, image_path)
def merge_images(images, size):
return inverse_transform(images)
def inverse_normalize(images):
batchnum = images.shape[0]
inv_ = np.zeros((images.shape[0],images.shape[1],images.shape[2],3)).astype(float)
for batch in range(batchnum):
y = images[batch,:,:,0]
x = images[batch,:,:,1]
z = np.ones((images.shape[1],images.shape[2])).astype(float)
is_zero = (x == -1).astype(int)
norm = np.sqrt(np.power(x,2)+np.power(y,2)+1.)
yy = y/norm
xx = x/norm
zz = z/norm
inv = np.dstack((yy,xx,zz))
inv = (inv*2.0)+1.
inv[is_zero ==1]= 0.0
inv_[batch,:,:,:] = inv
return inv_
def merge(images, size):
h, w = images.shape[1], images.shape[2]
if images.shape[-1] == 3:
img = np.zeros((h * size[0], w * size[1], 3))
for idx, image in enumerate(images):
i = idx % size[1]
j = idx / size[1]
img[j*h:j*h+h, i*w:i*w+w, :] = image
else:
img = np.zeros((h * size[0], w * size[1]))
for idx, image in enumerate(images):
i = idx % size[1]
j = idx / size[1]
img[j*h:j*h+h, i*w:i*w+w] = np.squeeze(image)
return img
def imsave(images, size, path):
return scipy.misc.imsave(path, merge(images, size))
def center_crop(x, crop_h, crop_w=None, resize_w=64):
if crop_w is None:
crop_w = crop_h
h, w = x.shape[:2]
j = int(round((h - crop_h)/2.))
i = int(round((w - crop_w)/2.))
return scipy.misc.imresize(x[j:j+crop_h, i:i+crop_w],
[resize_w, resize_w])
def transform_normal(image, npx, randx,randy,is_crop=True):
# npx : # of pixels width/height of image
if is_crop:
cropped_image = random_crop(image, npx,randx,randy)
#cropped_image = center_crop(image, npx)
else:
cropped_image = image
mean = 1.0
std = 0.05
cropped_image = cropped_image * np.random.normal(mean,std)
max_val = np.max(cropped_image)
cropped_image = cropped_image /max_val
#scipy.misc.imshow(cropped_image)
#print('cropped image dim:',cropped_image.shape)
#print('x:%d y:%d' % (randx,randy))
return np.array(cropped_image)*2. -1.
def random_crop(x,randx,randy,npx):
#npx =64
return x[randy:randy+npx, randx:randx+npx,:]
def transform(image, randx,randy,image_size,is_crop=True):
# npx : # of pixels width/height of image
if is_crop:
cropped_image = random_crop(image,randx,randy,image_size)
else:
cropped_image = image
return np.array(cropped_image)/255.0
def inverse_transform(images):
return (images+1.)/2.
def make_gif(images, fname, duration=2, true_image=False):
import moviepy.editor as mpy
def make_frame(t):
try:
x = images[int(len(images)/duration*t)]
except:
x = images[-1]
if true_image:
return x.astype(np.uint8)
else:
return ((x+1)/2*255).astype(np.uint8)
clip = mpy.VideoClip(make_frame, duration=duration)
clip.write_gif(fname, fps = len(images) / duration)