-
Notifications
You must be signed in to change notification settings - Fork 53
/
dataset_webcari.py
executable file
·46 lines (36 loc) · 1.49 KB
/
dataset_webcari.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
import os
from PIL import Image
from torch.utils.data import Dataset
class WebCariDataset(Dataset):
def __init__(self, path, transform):
self.photo_list = self.load_photo_path(path)
self.cari_list = self.load_cari_path(path)
self.length1 = len(self.photo_list)
self.length2 = len(self.cari_list)
self.transform = transform
def load_cari_path(self, path):
cari_list = []
for id in os.listdir(path):
id_path = os.path.join(path, id)
for fname in os.listdir(id_path):
if fname.startswith('C') and fname.endswith('.jpg'):
file_path = os.path.join(id_path, fname)
cari_list.append(file_path)
return cari_list
def load_photo_path(self, path):
photo_list = []
for id in os.listdir(path):
id_path = os.path.join(path, id)
for fname in os.listdir(id_path):
if fname.startswith('P') and fname.endswith('.jpg'):
file_path = os.path.join(id_path, fname)
photo_list.append(file_path)
return photo_list
def __len__(self):
return max(self.length1, self.length2)
def __getitem__(self, index):
idx = index % self.length1
photo = self.transform(Image.open(self.photo_list[idx]).convert('RGB'))
idx = index % self.length2
cari = self.transform(Image.open(self.cari_list[idx]).convert('RGB'))
return index, photo, cari