-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
69 lines (58 loc) · 1.73 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
"""
Scipy version > 0.18 is needed, due to 'mode' option from scipy.misc.imread function
"""
import os
import glob
import h5py
import random
import matplotlib.pyplot as plt
from PIL import Image # for loading images as YCbCr format
import scipy.misc
import scipy.ndimage
import numpy as np
import tensorflow as tf
FLAGS = tf.app.flags.FLAGS
def transform(images):
return np.array(images)/127.5 - 1.
def inverse_transform(images):
return (images+1.)/2
def prepare_data(sess, dataset):
"""
Args:
dataset: choose train dataset or test dataset
For train dataset, output data would be ['.../t1.bmp', '.../t2.bmp', ..., '.../t99.bmp']
"""
# import pdb
# pdb.set_trace()
filenames = os.listdir(dataset)
data_dir = os.path.join(os.getcwd(), dataset)
data = glob.glob(os.path.join(data_dir, "*.png"))
data = data + glob.glob(os.path.join(data_dir, "*.jpg"))
return data
def imread(path, is_grayscale=False):
"""
Read image using its path.
Default value is gray-scale, and image is read by YCbCr format as the paper said.
"""
# import pdb
# pdb.set_trace()
if is_grayscale:
return scipy.misc.imread(path, flatten=True).astype(np.float)
else:
return scipy.misc.imread(path).astype(np.float)
def imsave(image, path):
# import pdb
# pdb.set_trace()
imsaved = (inverse_transform(image)).astype(np.float)
return scipy.misc.imsave(path, imsaved)
def get_image(image_path,is_grayscale=False):
image = imread(image_path, is_grayscale)
# import pdb
# pdb.set_trace()
#return transform(image)
return image/255
def get_lable(image_path,is_grayscale=False):
image = imread(image_path, is_grayscale)
return image/255.
def imsave_lable(image, path):
return scipy.misc.imsave(path, image*255)