diff --git a/dataloader/face3d.py b/dataloader/face3d.py index d354cff..f9bb1c2 100644 --- a/dataloader/face3d.py +++ b/dataloader/face3d.py @@ -388,77 +388,77 @@ def __getitem__(self, idx): return img, face, head_point, gt_point -class RetailGazeDataset(Dataset): - def __init__(self, root_dir, mat_file, training='train', include_path=False, input_size=224, output_size=64, - imshow=False, use_gtbox=True): - assert (training in set(['train', 'test', 'test_prediction'])) - self.root_dir = root_dir - self.mat_file = mat_file - self.training = training - self.include_path = include_path - self.input_size = input_size - self.output_size = output_size - self.imshow = imshow - self.transform = _get_transform(input_size) - self.transform2 = _get_transform2() - self.use_gtbox = use_gtbox - - with open(mat_file, 'rb') as f: - self.data = pickle.load(f) - self.image_num = len(self.data) - - print("Number of Images:", self.image_num) - logging.info('%s contains %d images' % (self.mat_file, self.image_num)) - - def create_mask(self, seg_idx, width=640, height=480): - seg_idx = seg_idx.astype(np.int64) - seg_mask = np.zeros((height, width)).astype(np.uint8) - for i in range(seg_idx.shape[0]): - seg_mask[seg_idx[i, 1], seg_idx[i, 0]] = 255 - return seg_mask - - def __len__(self): - return self.image_num - - def __getitem__(self, idx): - - gaze_inside = True - data = self.data[idx] - image_path = data['filename'] - image_path = os.path.join(self.root_dir, image_path) - - image_path = image_path.replace('\\', '/') - img = Image.open(image_path) - img = img.convert('RGB') - width, height = img.size - # Get bounding boxes and class labels as well as gt index for gazed object - gt_bboxes, gt_labels = np.zeros(1), np.zeros(1) - gt_labels = np.expand_dims(gt_labels, axis=0) - hbox = data['ann']['hbox'] - - x_min, y_min, x_max, y_max = hbox - # Crop the face - face = img.crop((int(x_min), int(y_min), int(x_max), int(y_max))) - # Eyes_loc - eye_x = int(((x_min+x_max)/2)) - eye_y = int(((y_min+y_max)/2)) - eye_x = int((eye_x/640)*224) - eye_y = int((eye_y/480)*224) - eyes_loc = [eye_x, eye_y] - - if self.imshow: - img.save("img_aug.jpg") - face.save('face_aug.jpg') - - if self.transform is not None: - img = self.transform(img) - face = self.transform2(face) - if self.training == 'test': - return img, face, eyes_loc, image_path - elif self.training == 'test_prediction': - return img, face, gt_bboxes, gt_labels - else: - return img, face +# class RetailGazeDataset(Dataset): +# def __init__(self, root_dir, mat_file, training='train', include_path=False, input_size=224, output_size=64, +# imshow=False, use_gtbox=True): +# assert (training in set(['train', 'test', 'test_prediction'])) +# self.root_dir = root_dir +# self.mat_file = mat_file +# self.training = training +# self.include_path = include_path +# self.input_size = input_size +# self.output_size = output_size +# self.imshow = imshow +# self.transform = _get_transform(input_size) +# self.transform2 = _get_transform2() +# self.use_gtbox = use_gtbox + +# with open(mat_file, 'rb') as f: +# self.data = pickle.load(f) +# self.image_num = len(self.data) + +# print("Number of Images:", self.image_num) +# logging.info('%s contains %d images' % (self.mat_file, self.image_num)) + +# def create_mask(self, seg_idx, width=640, height=480): +# seg_idx = seg_idx.astype(np.int64) +# seg_mask = np.zeros((height, width)).astype(np.uint8) +# for i in range(seg_idx.shape[0]): +# seg_mask[seg_idx[i, 1], seg_idx[i, 0]] = 255 +# return seg_mask + +# def __len__(self): +# return self.image_num + +# def __getitem__(self, idx): + +# gaze_inside = True +# data = self.data[idx] +# image_path = data['filename'] +# image_path = os.path.join(self.root_dir, image_path) + +# image_path = image_path.replace('\\', '/') +# img = Image.open(image_path) +# img = img.convert('RGB') +# width, height = img.size +# # Get bounding boxes and class labels as well as gt index for gazed object +# gt_bboxes, gt_labels = np.zeros(1), np.zeros(1) +# gt_labels = np.expand_dims(gt_labels, axis=0) +# hbox = data['ann']['hbox'] + +# x_min, y_min, x_max, y_max = hbox +# # Crop the face +# face = img.crop((int(x_min), int(y_min), int(x_max), int(y_max))) +# # Eyes_loc +# eye_x = int(((x_min+x_max)/2)) +# eye_y = int(((y_min+y_max)/2)) +# eye_x = int((eye_x/640)*224) +# eye_y = int((eye_y/480)*224) +# eyes_loc = [eye_x, eye_y] + +# if self.imshow: +# img.save("img_aug.jpg") +# face.save('face_aug.jpg') + +# if self.transform is not None: +# img = self.transform(img) +# face = self.transform2(face) +# if self.training == 'test': +# return img, face, eyes_loc, image_path +# elif self.training == 'test_prediction': +# return img, face, gt_bboxes, gt_labels +# else: +# return img, face class RetailGaze(Dataset): @@ -532,15 +532,15 @@ def __getitem__(self, idx): # face.save('/home/primesh/Desktop/face.jpg') # segmentation mask layers - seg_path='/media/primesh/F4D0EA80D0EA49061/PROJECTS/FYP/Gaze detection/code/scripts/seg_mask' - masks=os.listdir(seg_path) - for mask in masks[:]: - if not(mask.endswith('.png')): - masks.remove(mask) - layers=[] - for mask in masks: - layer=cv2.imread(seg_path+'/'+mask, 0) - layers.append(layer) + # seg_path='/media/primesh/F4D0EA80D0EA49061/PROJECTS/FYP/Gaze detection/code/scripts/seg_mask' + # masks=os.listdir(seg_path) + # for mask in masks[:]: + # if not(mask.endswith('.png')): + # masks.remove(mask) + # layers=[] + # for mask in masks: + # layer=cv2.imread(seg_path+'/'+mask, 0) + # layers.append(layer) if self.imshow: img.save("img_aug.jpg") @@ -554,120 +554,121 @@ def __getitem__(self, idx): elif self.training == 'test_prediction': pass elif self.training == 'inference': - return img, face, head, gt_label, head_box, image_path, layers + # return img, face, head, gt_label, head_box, image_path, layers + pass else: return img, face, head, gt_label, head_box, image_path -class RetailGaze2(Dataset): - def __init__(self, root_dir, mat_file, training='train', include_path=False, input_size=224, output_size=64, imshow = False, use_gtbox=False): - assert (training in set(['train', 'test'])) - self.root_dir = root_dir - self.mat_file = mat_file - self.training = training - self.include_path = include_path - self.input_size = input_size - self.output_size = output_size - self.imshow = imshow - self.transform = _get_transform(input_size) - self.use_gtbox= use_gtbox +# class RetailGaze2(Dataset): +# def __init__(self, root_dir, mat_file, training='train', include_path=False, input_size=224, output_size=64, imshow = False, use_gtbox=False): +# assert (training in set(['train', 'test'])) +# self.root_dir = root_dir +# self.mat_file = mat_file +# self.training = training +# self.include_path = include_path +# self.input_size = input_size +# self.output_size = output_size +# self.imshow = imshow +# self.transform = _get_transform(input_size) +# self.use_gtbox= use_gtbox - with open(mat_file, 'rb') as f: - self.data = pickle.load(f) - self.image_num = len(self.data) +# with open(mat_file, 'rb') as f: +# self.data = pickle.load(f) +# self.image_num = len(self.data) - print("Number of Images:", self.image_num) - # logging.info('%s contains %d images' % (self.mat_file, self.image_num)) +# print("Number of Images:", self.image_num) +# # logging.info('%s contains %d images' % (self.mat_file, self.image_num)) - def __len__(self): - return self.image_num +# def __len__(self): +# return self.image_num - def __getitem__(self, idx): - gaze_inside = True - data = self.data[idx] - image_path = data['filename'] - image_path = os.path.join(self.root_dir, image_path) +# def __getitem__(self, idx): +# gaze_inside = True +# data = self.data[idx] +# image_path = data['filename'] +# image_path = os.path.join(self.root_dir, image_path) - gaze = [float(data['gaze_cx'])/640, float(data['gaze_cy'])/480] - # eyess = np.array([eye[0],eye[1]]).astype(np.float) - gaze_x, gaze_y = gaze +# gaze = [float(data['gaze_cx'])/640, float(data['gaze_cy'])/480] +# # eyess = np.array([eye[0],eye[1]]).astype(np.float) +# gaze_x, gaze_y = gaze - image_path = image_path.replace('\\', '/') - img = Image.open(image_path) - img = img.convert('RGB') - width, height = img.size - #Get bounding boxes and class labels as well as gt index for gazed object - gt_bboxes, gt_labels = np.zeros(1), np.zeros(1) - gt_labels = np.expand_dims(gt_labels, axis=0) - width, height = img.size - hbox = np.copy(data['ann']['hbox']) - x_min, y_min, x_max, y_max = hbox - head_x=((x_min+x_max)/2)/640 - head_y=((y_min+y_max)/2)/480 - eye = np.array([head_x, head_y]) - eye_x, eye_y = eye - k = 0.1 - x_min = (eye_x - 0.15) * width - y_min = (eye_y - 0.15) * height - x_max = (eye_x + 0.15) * width - y_max = (eye_y + 0.15) * height - if x_min < 0: - x_min = 0 - if y_min < 0: - y_min = 0 - if x_max < 0: - x_max = 0 - if y_max < 0: - y_max = 0 - if x_min > 1: - x_min = 1 - if y_min > 1: - y_min = 1 - if x_max > 1: - x_max = 1 - if y_max > 1: - y_max = 1 - x_min -= k * abs(x_max - x_min) - y_min -= k * abs(y_max - y_min) - x_max += k * abs(x_max - x_min) - y_max += k * abs(y_max - y_min) - x_min, y_min, x_max, y_max = map(float, [x_min, y_min, x_max, y_max]) - if self.use_gtbox: - gt_bboxes = np.copy(data['ann']['bboxes']) / [640, 480, 640, 480] - gt_labels = np.copy(data['ann']['labels']) - # gtbox = gt_bboxes[gaze_idx] - face = img.crop((int(x_min), int(y_min), int(x_max), int(y_max))) - head_x=((x_min+x_max)/2)/640 - head_y=((y_min+y_max)/2)/480 - head = np.array([head_x, head_y]) +# image_path = image_path.replace('\\', '/') +# img = Image.open(image_path) +# img = img.convert('RGB') +# width, height = img.size +# #Get bounding boxes and class labels as well as gt index for gazed object +# gt_bboxes, gt_labels = np.zeros(1), np.zeros(1) +# gt_labels = np.expand_dims(gt_labels, axis=0) +# width, height = img.size +# hbox = np.copy(data['ann']['hbox']) +# x_min, y_min, x_max, y_max = hbox +# head_x=((x_min+x_max)/2)/640 +# head_y=((y_min+y_max)/2)/480 +# eye = np.array([head_x, head_y]) +# eye_x, eye_y = eye +# k = 0.1 +# x_min = (eye_x - 0.15) * width +# y_min = (eye_y - 0.15) * height +# x_max = (eye_x + 0.15) * width +# y_max = (eye_y + 0.15) * height +# if x_min < 0: +# x_min = 0 +# if y_min < 0: +# y_min = 0 +# if x_max < 0: +# x_max = 0 +# if y_max < 0: +# y_max = 0 +# if x_min > 1: +# x_min = 1 +# if y_min > 1: +# y_min = 1 +# if x_max > 1: +# x_max = 1 +# if y_max > 1: +# y_max = 1 +# x_min -= k * abs(x_max - x_min) +# y_min -= k * abs(y_max - y_min) +# x_max += k * abs(x_max - x_min) +# y_max += k * abs(y_max - y_min) +# x_min, y_min, x_max, y_max = map(float, [x_min, y_min, x_max, y_max]) +# if self.use_gtbox: +# gt_bboxes = np.copy(data['ann']['bboxes']) / [640, 480, 640, 480] +# gt_labels = np.copy(data['ann']['labels']) +# # gtbox = gt_bboxes[gaze_idx] +# face = img.crop((int(x_min), int(y_min), int(x_max), int(y_max))) +# head_x=((x_min+x_max)/2)/640 +# head_y=((y_min+y_max)/2)/480 +# head = np.array([head_x, head_y]) - gt_label = np.array([gaze_x, gaze_y]) - head_box = np.array([x_min/640, y_min/480, x_max/640, y_max/480]) +# gt_label = np.array([gaze_x, gaze_y]) +# head_box = np.array([x_min/640, y_min/480, x_max/640, y_max/480]) - #plot gaze point - # i = cv2.imread(image_path) - # i=cv2.resize(i, (448, 448)) - # p=(gt_label*448).astype(np.int) - # p2=(head*448).astype(np.int) - # x,y=p - # x2, y2=p2 - # i = cv2.circle(i, (x,y), radius=0, color=(0, 0, 255), thickness=4) - # i = cv2.circle(i, (x2,y2), radius=0, color=(0, 0, 255), thickness=8) - # cv2.imwrite('/home/primesh/Desktop/fig.jpg', i) - # face.save('/home/primesh/Desktop/face.jpg') +# #plot gaze point +# # i = cv2.imread(image_path) +# # i=cv2.resize(i, (448, 448)) +# # p=(gt_label*448).astype(np.int) +# # p2=(head*448).astype(np.int) +# # x,y=p +# # x2, y2=p2 +# # i = cv2.circle(i, (x,y), radius=0, color=(0, 0, 255), thickness=4) +# # i = cv2.circle(i, (x2,y2), radius=0, color=(0, 0, 255), thickness=8) +# # cv2.imwrite('/home/primesh/Desktop/fig.jpg', i) +# # face.save('/home/primesh/Desktop/face.jpg') - if self.imshow: - img.save("img_aug.jpg") +# if self.imshow: +# img.save("img_aug.jpg") - if self.transform is not None: - img = self.transform(img) - face = self.transform(face) +# if self.transform is not None: +# img = self.transform(img) +# face = self.transform(face) - if self.training == 'test': - return img, face, head, gt_label, head_box, image_path - elif self.training == 'test_prediction': - pass - else: - return img, face, head, gt_label, head_box, image_path +# if self.training == 'test': +# return img, face, head, gt_label, head_box, image_path +# elif self.training == 'test_prediction': +# pass +# else: +# return img, face, head, gt_label, head_box, image_path # class RetailGaze1(Dataset): # def __init__(self, root_dir, mat_file, training='train', include_path=False, input_size=224, output_size=64, imshow = False, use_gtbox=False): @@ -909,4 +910,105 @@ def __getitem__(self, idx): # if self.training == 'test': # return img, face, head_channel, head_box, object_channel,gaze_final,eye,gt_bboxes,gt_labels, gaze, image_path, layers, seg_folder, masks # else: -# return img, face, head_channel, object_channel,eyes_loc, image_path, gaze_inside , shifted_grids, gaze_final \ No newline at end of file +# return img, face, head_channel, object_channel,eyes_loc, image_path, gaze_inside , shifted_grids, gaze_final + + +# class RetailGaze(Dataset): +# def __init__(self, root_dir, mat_file, training='train', include_path=False, input_size=224, output_size=64, imshow = False, use_gtbox=False): +# assert (training in set(['train', 'test'])) +# self.root_dir = root_dir +# self.mat_file = mat_file +# self.training = training +# self.include_path = include_path +# self.input_size = input_size +# self.output_size = output_size +# self.imshow = imshow +# self.transform = _get_transform(input_size) +# self.use_gtbox= use_gtbox + +# with open(mat_file, 'rb') as f: +# self.data = pickle.load(f) +# self.image_num = len(self.data) + +# print("Number of Images:", self.image_num) +# # logging.info('%s contains %d images' % (self.mat_file, self.image_num)) + +# def __len__(self): +# return self.image_num + +# def __getitem__(self, idx): +# gaze_inside = True +# data = self.data[idx] +# image_path = data['filename'] +# image_path = os.path.join(self.root_dir, image_path) + +# gaze = [float(data['gaze_cx'])/640, float(data['gaze_cy'])/480] +# # eyess = np.array([eye[0],eye[1]]).astype(np.float) +# gaze_x, gaze_y = gaze + +# image_path = image_path.replace('\\', '/') +# img = Image.open(image_path) +# img = img.convert('RGB') +# width, height = img.size +# #Get bounding boxes and class labels as well as gt index for gazed object +# gt_bboxes, gt_labels = np.zeros(1), np.zeros(1) +# gt_labels = np.expand_dims(gt_labels, axis=0) +# width, height = img.size +# hbox = np.copy(data['ann']['hbox']) +# x_min, y_min, x_max, y_max = hbox +# head_x=((x_min+x_max)/2)/640 +# head_y=((y_min+y_max)/2)/480 +# eye = np.array([head_x, head_y]) +# eye_x, eye_y = eye +# k = 0.1 +# x_min = (eye_x - 0.15) * width +# y_min = (eye_y - 0.15) * height +# x_max = (eye_x + 0.15) * width +# y_max = (eye_y + 0.15) * height +# if x_min < 0: +# x_min = 0 +# if y_min < 0: +# y_min = 0 +# if x_max < 0: +# x_max = 0 +# if y_max < 0: +# y_max = 0 +# if x_min > 1: +# x_min = 1 +# if y_min > 1: +# y_min = 1 +# if x_max > 1: +# x_max = 1 +# if y_max > 1: +# y_max = 1 +# x_min -= k * abs(x_max - x_min) +# y_min -= k * abs(y_max - y_min) +# x_max += k * abs(x_max - x_min) +# y_max += k * abs(y_max - y_min) +# x_min, y_min, x_max, y_max = map(float, [x_min, y_min, x_max, y_max]) +# if self.use_gtbox: +# gt_bboxes = np.copy(data['ann']['bboxes']) / [640, 480, 640, 480] +# gt_labels = np.copy(data['ann']['labels']) +# # gtbox = gt_bboxes[gaze_idx] +# face = img.crop((int(x_min), int(y_min), int(x_max), int(y_max))) +# head_x=((x_min+x_max)/2)/640 +# head_y=((y_min+y_max)/2)/480 +# head = np.array([head_x, head_y]) + +# gt_label = np.array([gaze_x, gaze_y]) +# head_box = np.array([x_min/640, y_min/480, x_max/640, y_max/480]) + +# if self.imshow: +# img.save("img_aug.jpg") + +# if self.transform is not None: +# img = self.transform(img) +# face = self.transform(face) + +# if self.training == 'test': +# return img, face, head, gt_label, head_box, image_path +# elif self.training == 'test_prediction': +# pass +# else: +# return img, face, head, gt_label, head_box, image_path +