CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
veeralakrishna

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: veeralakrishna/DataCamp-Project-Solutions-Python
Path: blob/master/ASL Recognition with Deep Learning/datasets/sign_language.py
Views: 1229
1
import random
2
import numpy as np
3
from keras.utils import to_categorical
4
from keras.preprocessing import image
5
from os import listdir
6
from os.path import isdir, join
7
8
9
def load_data(container_path='datasets', folders=['A', 'B', 'C'],
10
size=2000, test_split=0.2, seed=0):
11
"""
12
Loads sign language dataset.
13
"""
14
15
filenames, labels = [], []
16
17
for label, folder in enumerate(folders):
18
folder_path = join(container_path, folder)
19
images = [join(folder_path, d)
20
for d in sorted(listdir(folder_path))]
21
labels.extend(len(images) * [label])
22
filenames.extend(images)
23
24
random.seed(seed)
25
data = list(zip(filenames, labels))
26
random.shuffle(data)
27
data = data[:size]
28
filenames, labels = zip(*data)
29
30
31
# Get the images
32
x = paths_to_tensor(filenames).astype('float32')/255
33
# Store the one-hot targets
34
y = to_categorical(np.array(labels)) # Use to_categorical directly
35
36
x_train = np.array(x[:int(len(x) * (1 - test_split))])
37
y_train = np.array(y[:int(len(x) * (1 - test_split))])
38
x_test = np.array(x[int(len(x) * (1 - test_split)):])
39
y_test = np.array(y[int(len(x) * (1 - test_split)):])
40
41
return (x_train, y_train), (x_test, y_test)
42
43
44
def path_to_tensor(img_path, size):
45
# loads RGB image as PIL.Image.Image type
46
img = image.load_img(img_path, target_size=(size, size))
47
# convert PIL.Image.Image type to 3D tensor
48
x = image.img_to_array(img)
49
# convert 3D tensor to 4D tensor
50
return np.expand_dims(x, axis=0)
51
52
def paths_to_tensor(img_paths, size=50):
53
list_of_tensors = [path_to_tensor(img_path, size) for img_path in img_paths]
54
return np.vstack(list_of_tensors)
55
56