-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
33 lines (26 loc) · 1.06 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
import os
import numpy as np
import pandas as pd
from PIL import Image
from torch.utils.data import Dataset
class ScreenshotDataset(Dataset):
def __init__(self, csv_file, root_dir, transform=None):
super(ScreenshotDataset, self).__init__()
self.data = []
self.root_dir = root_dir
self.transform = transform
self.annotations = pd.read_csv(csv_file)
self.class_names = os.listdir(root_dir)
for index, name in enumerate(self.class_names):
files = os.listdir(os.path.join(root_dir, name))
self.data += list(zip(files, [index] * len(files)))
def __len__(self):
return len(self.annotations)
def __getitem__(self, index):
img_file, label = self.data[index]
root_and_dir = os.path.join(self.root_dir, self.class_names[label])
image = np.array(Image.open(os.path.join(root_and_dir, img_file)))
if self.transform is not None:
augmentations = self.transform(image=image)
image = augmentations["image"]
return image, label