-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathutils.py
63 lines (47 loc) · 1.85 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
# ------------------------------------------------------------
# Real-Time Style Transfer Implementation
# Licensed under The MIT License [see LICENSE for details]
# Written by Cheng-Bin Jin
# Email: [email protected]
# ------------------------------------------------------------
import os
import sys
import scipy.misc
import numpy as np
def imread(path, is_gray_scale=False, img_size=None):
if is_gray_scale:
img = scipy.misc.imread(path, flatten=True).astype(np.float32)
else:
img = scipy.misc.imread(path, mode='RGB').astype(np.float32)
if not (img.ndim == 3 and img.shape[2] == 3):
img = np.dstack((img, img, img))
if img_size is not None:
img = scipy.misc.imresize(img, img_size)
return img
def imsave(path, img):
img = np.clip(img, 0, 255).astype(np.uint8)
scipy.misc.imsave(path, img)
def all_files_under(path, extension=None, append_path=True, sort=True):
if append_path:
if extension is None:
filenames = [os.path.join(path, fname) for fname in os.listdir(path)]
else:
filenames = [os.path.join(path, fname)
for fname in os.listdir(path) if fname.endswith(extension)]
else:
if extension is None:
filenames = [os.path.basename(fname) for fname in os.listdir(path)]
else:
filenames = [os.path.basename(fname)
for fname in os.listdir(path) if fname.endswith(extension)]
if sort:
filenames = sorted(filenames)
return filenames
def exists(p, msg):
assert os.path.exists(p), msg
def print_metrics(itr, kargs):
print("*** Iteration {} ====> ".format(itr))
for name, value in kargs.items():
print("{} : {}, ".format(name, value))
print("")
sys.stdout.flush()