-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.py
62 lines (52 loc) · 2.45 KB
/
display.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
import PIL.Image as Image
import numpy as np
import os, sys
import glob
import cPickle as pkl
def show_reconstructed_images(inputs, outputs, saveImageOnDisk, n = 5):
for i in range(0, n):
img_array = np.copy(inputs[i])
center = (int(np.floor(img_array.shape[0] / 2.)), int(np.floor(img_array.shape[1] / 2.)))
img_array[center[0] - 16:center[0] + 16, center[1] - 16:center[1] + 16, :] = outputs[i]
Image.fromarray(img_array).show()
if saveImageOnDisk: Image.fromarray(img_array).save('PredictedImage{i}.jpg'.format(i=i))
def show_images(inputs, n = 5, saveImageOnDisk= False):
for i in range(0, n):
if saveImageOnDisk:
Image.fromarray(inputs[i]).save('./tmp/PredictedImage{i}.jpg'.format(i=i))
else:
Image.fromarray(inputs[i]).show()
def show_examples(batch_idx, batch_size,
### PATH need to be fixed
mscoco="inpainting/", split="train2014",
caption_path="dict_key_imgID_value_caps_train_and_valid.pkl"):
'''
Show an example of how to read the dataset
'''
data_path = os.path.join(mscoco, split)
caption_path = os.path.join(mscoco, caption_path)
with open(caption_path) as fd:
caption_dict = pkl.load(fd)
print data_path + "/*.jpg"
imgs = glob.glob(data_path + "/*.jpg")
# if batch_idx is 5 and batch_size is 5 then we go at 5x5 index and take the 5 from there
batch_imgs = imgs[batch_idx * batch_size:(batch_idx + 1) * batch_size]
for i, img_path in enumerate(batch_imgs):
img = Image.open(img_path)
img_array = np.array(img)
cap_id = os.path.basename(img_path)[:-4]
### Get input/target from the images
center = (int(np.floor(img_array.shape[0] / 2.)), int(np.floor(img_array.shape[1] / 2.)))
if len(img_array.shape) == 3:
input = np.copy(img_array)
input[center[0] - 16:center[0] + 16, center[1] - 16:center[1] + 16, :] = 0
target = img_array[center[0] - 16:center[0] + 16, center[1] - 16:center[1] + 16, :]
else:
input = np.copy(img_array)
input[center[0] - 16:center[0] + 16, center[1] - 16:center[1] + 16, :] = 0
target = img_array[center[0] - 16:center[0] + 16, center[1] - 16:center[1] + 16]
# Image.fromarray(img_array).show()
Image.fromarray(input).show()
Image.fromarray(target).show()
print img_path
print i, caption_dict[cap_id]