import random
import numpy as np
from keras.utils import to_categorical
from keras.preprocessing import image
from os import listdir
from os.path import isdir, join
def load_data(container_path='datasets', folders=['A', 'B', 'C'],
size=2000, test_split=0.2, seed=0):
"""
Loads sign language dataset.
"""
filenames, labels = [], []
for label, folder in enumerate(folders):
folder_path = join(container_path, folder)
images = [join(folder_path, d)
for d in sorted(listdir(folder_path))]
labels.extend(len(images) * [label])
filenames.extend(images)
random.seed(seed)
data = list(zip(filenames, labels))
random.shuffle(data)
data = data[:size]
filenames, labels = zip(*data)
x = paths_to_tensor(filenames).astype('float32')/255
y = to_categorical(np.array(labels))
x_train = np.array(x[:int(len(x) * (1 - test_split))])
y_train = np.array(y[:int(len(x) * (1 - test_split))])
x_test = np.array(x[int(len(x) * (1 - test_split)):])
y_test = np.array(y[int(len(x) * (1 - test_split)):])
return (x_train, y_train), (x_test, y_test)
def path_to_tensor(img_path, size):
img = image.load_img(img_path, target_size=(size, size))
x = image.img_to_array(img)
return np.expand_dims(x, axis=0)
def paths_to_tensor(img_paths, size=50):
list_of_tensors = [path_to_tensor(img_path, size) for img_path in img_paths]
return np.vstack(list_of_tensors)