-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadsample.py
More file actions
33 lines (26 loc) · 852 Bytes
/
readsample.py
File metadata and controls
33 lines (26 loc) · 852 Bytes
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
import os
import glob
import numpy as np
import PIL.Image as Image
import pickle
def read_sample_as_tensor(base_path="training/", split="target"):
'''
read sample from training sample or validation
'''
data_path = os.path.join(base_path, split)
imgs = glob.glob(data_path + "/*.jpg")
np_images = []
for i, img_path in enumerate(imgs):
img = Image.open(img_path)
img_array = np.array(img)
# we only keep colored images
if len(img_array.shape) == 3:
np_images.append(img_array)
return np.asarray(np_images)
def write_images_to_pkl(np_images, path):
with open(path, 'wb') as output:
pickle.dump(np_images, output, pickle.HIGHEST_PROTOCOL)
def read_images_from_pkl(path):
with open(path, 'rb') as input:
images = pickle.load(input)
return images