-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
59 lines (49 loc) · 1.91 KB
/
dataset.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 csv
import cv2
import numpy as np
import torch
from string import ascii_lowercase
from torch.utils.data import Dataset
# Utility function
def read_csv(csv_file):
with open(csv_file, newline='') as f:
reader = csv.reader(f)
data = list(reader)
return data
class SignLangDataset(Dataset):
"""Sign language dataset"""
def __init__(self, csv_file, root_dir, class_index_map=None, transform=None):
"""
Args:
csv_file (string): Path to the csv file with annotations.
root_dir (string): Directory with all the images.
transform (callable, optional): Optional transform to be applied on a sample.
"""
self.data = read_csv(os.path.join(root_dir, csv_file))
self.root_dir = root_dir
self.class_index_map = class_index_map
self.transform = transform
# List of class names in order
self.class_names = list(map(str, list(range(10)))) + list(ascii_lowercase)
def __len__(self):
"""
Calculates the length of the dataset-
"""
return len(self.data)
def __getitem__(self, idx):
"""
Returns one sample (dict consisting of an image and its label)
"""
if torch.is_tensor(idx):
idx = idx.tolist()
# Read the image and labels
image_path = os.path.join(self.root_dir, self.data[idx][1])
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
# Shape of the image should be H,W,C where C=1
image = np.expand_dims(image, 0)
# The label is the index of the class name in the list ['0','1',...,'9','a','b',...'z']
# because we should have integer labels in the range 0-35 (for 36 classes)
label = self.class_names.index(self.data[idx][0])
sample = {'image': image, 'label': label}
return sample