-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsign_language.py
60 lines (46 loc) · 1.77 KB
/
sign_language.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
import random
import numpy as np
from keras.utils import np_utils, 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)
# Get the images
x = paths_to_tensor(filenames).astype('float32')/255
# Store the one-hot targets
y = 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):
# loads RGB image as PIL.Image.Image type
img = image.load_img(img_path, target_size=(size, size))
# convert PIL.Image.Image type to 3D tensor
x = image.img_to_array(img)
# convert 3D tensor to 4D tensor
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)
"""
num_types = len(data['target_names'])
targets = np_utils.to_categorical(np.array(data['target']), num_types)
"""