-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
executable file
·76 lines (65 loc) · 2.21 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
import scipy.misc
import numpy as np
import random
import tensorflow as tf
def get_image_fit(image_path):
im = imread(image_path)
new_width = ((im.shape[0] // 32)*32)
resize = scipy.misc.imresize(im, new_width/im.shape[0])
new_height = (((im.shape[1] // 32) + 1)*32)
res = np.ones((resize.shape[0], new_height, 3))
res[:resize.shape[0], :resize.shape[1], :] = resize
res = scipy.misc.imresize(res, (new_width,new_height))
res = transform(res)
return res
def get_image(image_path):
im = imread(image_path)
resize = scipy.misc.imresize(im, 512/im.shape[0])
if resize.shape[1] < 512:
res = np.ones((512,512, 3))
res[:resize.shape[0], :resize.shape[1], :] = resize
elif resize.shape[1] >= 512:
res = resize[:512,:512]
res = scipy.misc.imresize(res, (512,512))
res = transform(res)
return res
def get_image_class(image_path):
im = imread(image_path)
#print(im.shape)
if im.shape[0] > 80 or im.shape[1] > 80:
res = np.zeros((16,16))
else:
resize = scipy.misc.imresize(im, 16/im.shape[0], interp="nearest")
if resize.shape[1] < 16:
res = np.zeros((16,16))
res[:resize.shape[0], :resize.shape[1]] = resize[:,:,0]
elif resize.shape[1] >= 16:
res = resize[:16, :16, 0]
res = scipy.misc.imresize(res, (16,16))
res = transform(res)
return np.expand_dims(res, 3)
def transform(image):
return np.array(image)/255.0
def center_crop(x, crop_h, crop_w=None, resize_w=108):
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 x[j:j+crop_h, i:i+crop_w]
def imread(path):
readimage = scipy.misc.imread(path,mode="RGB").astype(np.float)
return readimage
def merge_color(images, size):
h, w = images.shape[1], images.shape[2]
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
return img
def ims(name, img):
# print img[:10][:10]
scipy.misc.toimage(img, cmin=0, cmax=1).save(name)
def sigmoid(x):
return 1 / (1 + np.exp(-x))