-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasets.py
172 lines (135 loc) · 5.43 KB
/
datasets.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import os
import numpy as np
import cv2
import torch
from torch.utils.data.dataset import Dataset
from torchvision import transforms
from PIL import Image, ImageFilter, ImageFile, ImageOps
ImageFile.LOAD_TRUNCATED_IMAGES = True
class Gaze360(Dataset):
def __init__(self, path, root, transform, angle, binwidth, num_bins, train=True):
self.num_bins = num_bins
self.transform = transform
self.root = root
self.orig_list_len = 0
self.angle = angle
self.binwidth=binwidth
self.lines = []
if isinstance(path, list):
for i in path:
with open(i) as f:
print("here")
line = f.readlines()
line.pop(0)
self.lines.extend(line)
else:
print("WILL FILTER OOOOOOO")
with open(path) as f:
lines = f.readlines()
lines.pop(0)
self.orig_list_len = len(lines)
for line in lines:
gaze2d = line.strip().split(" ")[5]
label = np.array(gaze2d.split(",")).astype("float")
if abs((label[0]*180/np.pi)) <= angle and abs((label[1]*180/np.pi)) <= angle:
self.lines.append(line)
print("{} items removed from dataset that have an angle > {}".format(self.orig_list_len-len(self.lines), angle))
print(self.orig_list_len, len(self.lines))
def __len__(self):
return len(self.lines)
def __getitem__(self, idx):
line = self.lines[idx]
line = line.strip().split(" ")
face = line[0]
lefteye = line[1]
righteye = line[2]
name = line[3]
gaze2d = line[5]
label = np.array(gaze2d.split(",")).astype("float")
label = torch.from_numpy(label).type(torch.FloatTensor)
yaw = label[0]* 180 / np.pi
pitch = label[1]* 180 / np.pi
img = Image.open(os.path.join(self.root, face))
if self.transform:
img = self.transform(img)
# Bin values
# bins = np.array(range(-1*self.angle, self.angle, self.binwidth))
# print(self.num_bins)
bins = np.array(range(-1*self.num_bins, self.num_bins + self.binwidth, self.binwidth))
# print(bins)
binned_pose = np.digitize([yaw, pitch], bins)
# print(binned_pose)
labels = binned_pose
cont_labels = torch.FloatTensor([yaw, pitch])
return img, labels, cont_labels, name
class Mpiigaze(Dataset):
def __init__(self, pathorg, root, transform,angle, binwidth, num_bins, fold=0):
self.num_bins = num_bins
self.transform = transform
self.root = root
self.orig_list_len = 0
self.lines = []
# path=pathorg.copy()
path=pathorg
# if train==True:
# path.pop(fold)
# else:
# path=path[fold]
self.binwidth=binwidth
self.angle = angle
# if isinstance(path, list):
if isinstance(path, str) and os.path.isdir(path):
folder = os.listdir(path)
folder.sort()
folder = [os.path.join(path, j) for j in folder]
for i in folder:
with open(i) as f:
lines = f.readlines()
lines.pop(0)
self.orig_list_len += len(lines)
for line in lines:
gaze2d = line.strip().split(" ")[7]
label = np.array(gaze2d.split(",")).astype("float")
if abs((label[0]*180/np.pi)) <= angle and abs((label[1]*180/np.pi)) <= angle:
self.lines.append(line)
else:
with open(path) as f:
lines = f.readlines()
lines.pop(0)
self.orig_list_len += len(lines)
for line in lines:
gaze2d = line.strip().split(" ")[7]
label = np.array(gaze2d.split(",")).astype("float")
if abs((label[0]*180/np.pi)) <= angle and abs((label[1]*180/np.pi)) <= angle:
self.lines.append(line)
print("{} items removed from dataset that have an angle > {}".format(self.orig_list_len-len(self.lines),angle))
print(self.orig_list_len, len(self.lines))
def __len__(self):
return len(self.lines)
def __getitem__(self, idx):
line = self.lines[idx]
line = line.strip().split(" ")
name = line[3]
gaze2d = line[7]
head2d = line[8]
lefteye = line[1]
righteye = line[2]
face = line[0]
label = np.array(gaze2d.split(",")).astype("float")
label = torch.from_numpy(label).type(torch.FloatTensor)
yaw = label[0]* 180 / np.pi
pitch = label[1]* 180 / np.pi
img = Image.open(os.path.join(self.root, face))
if self.transform:
img = self.transform(img)
# Bin values
# bins = np.array(range(-1*self.angle, self.angle, self.binwidth))
# print(self.num_bins)
# bins = np.array(range(-1*self.num_bins//2, self.num_bins//2 + self.binwidth, self.binwidth))
bins = np.array(range(-1*self.num_bins, self.num_bins + self.binwidth, self.binwidth))
# print(bins)
binned_pose = np.digitize([yaw, pitch], bins)
# print(binned_pose)
labels = binned_pose
cont_labels = torch.FloatTensor([yaw, pitch])
return img, labels, cont_labels, name