Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/FaceMaskOverlay/lib/datasets/aflw.py
3443 views
1
# ------------------------------------------------------------------------------
2
# Copyright (c) Microsoft
3
# Licensed under the MIT License.
4
# Created by Tianheng Cheng([email protected]), Yang Zhao
5
# ------------------------------------------------------------------------------
6
7
import os
8
import random
9
10
import torch
11
import torch.utils.data as data
12
import pandas as pd
13
from PIL import Image, ImageFile
14
import numpy as np
15
16
from ..utils.transforms import fliplr_joints, crop, generate_target, transform_pixel
17
18
ImageFile.LOAD_TRUNCATED_IMAGES = True
19
20
21
class AFLW(data.Dataset):
22
"""AFLW
23
"""
24
def __init__(self, cfg, is_train=True, transform=None):
25
# specify annotation file for dataset
26
if is_train:
27
self.csv_file = cfg.DATASET.TRAINSET
28
else:
29
self.csv_file = cfg.DATASET.TESTSET
30
31
self.is_train = is_train
32
self.transform = transform
33
self.data_root = cfg.DATASET.ROOT
34
self.input_size = cfg.MODEL.IMAGE_SIZE
35
self.output_size = cfg.MODEL.HEATMAP_SIZE
36
self.sigma = cfg.MODEL.SIGMA
37
self.scale_factor = cfg.DATASET.SCALE_FACTOR
38
self.rot_factor = cfg.DATASET.ROT_FACTOR
39
self.label_type = cfg.MODEL.TARGET_TYPE
40
self.flip = cfg.DATASET.FLIP
41
self.mean = np.array([0.485, 0.456, 0.406], dtype=np.float32)
42
self.std = np.array([0.229, 0.224, 0.225], dtype=np.float32)
43
# load annotations
44
self.landmarks_frame = pd.read_csv(self.csv_file)
45
46
def __len__(self):
47
return len(self.landmarks_frame)
48
49
def __getitem__(self, idx):
50
51
image_path = os.path.join(self.data_root,
52
self.landmarks_frame.iloc[idx, 0])
53
scale = self.landmarks_frame.iloc[idx, 1]
54
box_size = self.landmarks_frame.iloc[idx, 2]
55
56
center_w = self.landmarks_frame.iloc[idx, 3]
57
center_h = self.landmarks_frame.iloc[idx, 4]
58
center = torch.Tensor([center_w, center_h])
59
60
pts = self.landmarks_frame.iloc[idx, 5:].values
61
pts = pts.astype('float').reshape(-1, 2)
62
63
scale *= 1.25
64
nparts = pts.shape[0]
65
img = np.array(Image.open(image_path).convert('RGB'), dtype=np.float32)
66
67
r = 0
68
if self.is_train:
69
scale = scale * (random.uniform(1 - self.scale_factor,
70
1 + self.scale_factor))
71
r = random.uniform(-self.rot_factor, self.rot_factor) \
72
if random.random() <= 0.6 else 0
73
if random.random() <= 0.5 and self.flip:
74
img = np.fliplr(img)
75
pts = fliplr_joints(pts, width=img.shape[1], dataset='AFLW')
76
center[0] = img.shape[1] - center[0]
77
78
img = crop(img, center, scale, self.input_size, rot=r)
79
80
target = np.zeros((nparts, self.output_size[0], self.output_size[1]))
81
tpts = pts.copy()
82
83
for i in range(nparts):
84
if tpts[i, 1] > 0:
85
tpts[i, 0:2] = transform_pixel(tpts[i, 0:2]+1, center,
86
scale, self.output_size, rot=r)
87
target[i] = generate_target(target[i], tpts[i]-1, self.sigma,
88
label_type=self.label_type)
89
img = img.astype(np.float32)
90
img = (img/255.0 - self.mean) / self.std
91
img = img.transpose([2, 0, 1])
92
target = torch.Tensor(target)
93
tpts = torch.Tensor(tpts)
94
center = torch.Tensor(center)
95
96
meta = {'index': idx, 'center': center, 'scale': scale,
97
'pts': torch.Tensor(pts), 'tpts': tpts, 'box_size': box_size}
98
99
return img, target, meta
100
101
102
if __name__ == '__main__':
103
104
pass
105
106