-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
59 lines (46 loc) · 2.36 KB
/
helpers.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
import os
import glob
import numpy as np
from skimage.transform import resize
from skimage.io import imread
def get_image_paths(data_path, categories, num_train_per_cat):
"""
This function returns lists containing the file path for each train
and test image, as well as lists with the label of each train and
test image. By default both lists will be 1500x1, where each
entry is a char array (or string).
"""
num_categories = len(categories) # number of scene categories.
# This paths for each training and test image. By default it will have 1500
# entries (15 categories * 100 training and test examples each)
train_image_paths = [None] * (num_categories * num_train_per_cat)
test_image_paths = [None] * (num_categories * num_train_per_cat)
# The name of the category for each training and test image. With the
# default setup, these arrays will actually be the same, but they are built
# independently for clarity and ease of modification.
train_labels = [None] * (num_categories * num_train_per_cat)
test_labels = [None] * (num_categories * num_train_per_cat)
for i, cat in enumerate(categories):
images = glob.glob(os.path.join(data_path, 'train', cat, '*.jpg'))
for j in range(num_train_per_cat):
train_image_paths[i * num_train_per_cat + j] = images[j]
train_labels[i * num_train_per_cat + j] = cat
images = glob.glob(os.path.join(data_path, 'test', cat, '*.jpg'))
for j in range(num_train_per_cat):
test_image_paths[i * num_train_per_cat + j] = images[j]
test_labels[i * num_train_per_cat + j] = cat
return train_image_paths, test_image_paths, train_labels, test_labels
def load_images(image_paths):
'''
This function will load all the images in the image_paths list and resize them to 256x256.
Inputs:
image_paths: a Python list of image path strings with length n
Outputs:
images: An nx256x256 numpy matrix where b is the number of images.
The first dimension contains the reference to the 256x256 numpy matrix
representing the corresponding image.
'''
images = np.zeros((len(image_paths), 256, 256), dtype='float64')
for i, path in enumerate(image_paths):
images[i, :, :] = resize(imread(path, as_gray=True), (256, 256), anti_aliasing=True)
return images