hfaus commited on
Commit
dcc88d1
1 Parent(s): af0e084

Upload CelebA_bbox_and_facepoints.py

Browse files
Files changed (1) hide show
  1. CelebA_bbox_and_facepoints.py +50 -28
CelebA_bbox_and_facepoints.py CHANGED
@@ -29,7 +29,10 @@ _REPO = "https://huggingface.co/datasets/hfaus/CelebA_bbox_and_facepoints/resolv
29
  _URLS = {
30
  "train": f"{_REPO}/celebA_train.zip",
31
  "validation": f"{_REPO}/celebA_val.zip",
32
- "test": f"{_REPO}/celebA_test.zip"
 
 
 
33
  }
34
 
35
  class CelebA(datasets.GeneratorBasedBuilder):
@@ -50,15 +53,15 @@ class CelebA(datasets.GeneratorBasedBuilder):
50
  "righteye": datasets.Sequence(datasets.Value("int32")),
51
  "nose": datasets.Sequence(datasets.Value("int32")),
52
  "leftmouth": datasets.Sequence(datasets.Value("int32")),
53
- "rightmouth": datasets.Sequence(datasets.Value("int32")),
54
  }
55
- ),
56
  }
57
  ),
58
  supervised_keys=None,
59
  homepage=_HOMEPAGE,
60
  license=_LICENSE,
61
- citation=_CITATION,
62
  )
63
 
64
  def _split_generators(self, dl_manager):
@@ -68,49 +71,67 @@ class CelebA(datasets.GeneratorBasedBuilder):
68
  name=datasets.Split.TRAIN,
69
  gen_kwargs={
70
  "split": "train",
71
- "data_dir": data_dir["train"]
72
- },
 
 
 
 
73
  ),
74
  datasets.SplitGenerator(
75
  name=datasets.Split.TEST,
76
  gen_kwargs={
77
  "split": "test",
78
- "data_dir": data_dir["test"]
79
- },
 
 
 
 
80
  ),
81
  datasets.SplitGenerator(
82
  name=datasets.Split.VALIDATION,
83
  gen_kwargs={
84
  "split": "val",
85
- "data_dir": data_dir["validation"]
86
- },
87
- ),
 
 
 
 
88
  ]
89
 
90
- def _generate_examples(self, split, data_dir):
91
- bbox_fname = os.path.join(data_dir, "list_bbox_celeba.txt")
92
- landmarks_fname = os.path.join(data_dir, "list_landmarks_celeba.txt")
93
 
94
  #Abrimos los dos ficheros
95
- fichero1 = open(bbox_fname, "r", encoding="utf-8")
96
- fichero2 = open(landmarks_fname, "r", encoding="utf-8")
 
97
 
98
- #Creamos una lista a partir del contenido de Fichero2
99
- lista = fichero1.readlines()
100
- for i, bbox_line in enumerate(lista):
101
 
102
- # Se leen las líneas de ambos ficheros
103
- bbox_line = bbox_line.rstrip()
104
- if not ".jpg" in bbox_line:
105
  break
106
- landmarks_line = fichero1.readline(i)
107
- bbox = " ".join(bbox_line.split()).split(" ")
108
- landmarks = " ".join(landmarks_line.split()).split(" ")
109
- image_name = bbox[0];
110
- image_file_path = os.path.join(data_dir, bbox[0])
 
111
 
112
- # Read number of bounding boxes
 
 
113
  bbox_total = [int(bbox[1]), int(bbox[2]), int(bbox[3]), int(bbox[4])]
 
 
 
 
114
  facial_landmarks = {
115
  'lefteye': [landmarks[1], landmarks[2]],
116
  'righteye': [landmarks[3], landmarks[4]],
@@ -122,5 +143,6 @@ class CelebA(datasets.GeneratorBasedBuilder):
122
  yield idx, {"image": image_file_path, "facial_landmarks": facial_landmarks, "bbox": bbox_total}
123
  idx += 1
124
  else:
 
125
  fichero1.close()
126
  fichero2.close()
 
29
  _URLS = {
30
  "train": f"{_REPO}/celebA_train.zip",
31
  "validation": f"{_REPO}/celebA_val.zip",
32
+ "test": f"{_REPO}/celebA_test.zip",
33
+ "eval_partition_file": f"{_REPO}/list_eval_partition.txt",
34
+ "bbox_file": f"{_REPO}/list_bbox_celeba.txt",
35
+ "landmarks_file": f"{_REPO}/list_landmarks_celeba.txt"
36
  }
37
 
38
  class CelebA(datasets.GeneratorBasedBuilder):
 
53
  "righteye": datasets.Sequence(datasets.Value("int32")),
54
  "nose": datasets.Sequence(datasets.Value("int32")),
55
  "leftmouth": datasets.Sequence(datasets.Value("int32")),
56
+ "rightmouth": datasets.Sequence(datasets.Value("int32"))
57
  }
58
+ )
59
  }
60
  ),
61
  supervised_keys=None,
62
  homepage=_HOMEPAGE,
63
  license=_LICENSE,
64
+ citation=_CITATION
65
  )
66
 
67
  def _split_generators(self, dl_manager):
 
71
  name=datasets.Split.TRAIN,
72
  gen_kwargs={
73
  "split": "train",
74
+ "data_dir": data_dir["train"],
75
+ "partition_type": 0,
76
+ "eval_partition_file": data_dir["eval_partition_file"],
77
+ "bbox_file": data_dir["bbox_file"],
78
+ "landmarks_file": data_dir["landmarks_file"]
79
+ }
80
  ),
81
  datasets.SplitGenerator(
82
  name=datasets.Split.TEST,
83
  gen_kwargs={
84
  "split": "test",
85
+ "data_dir": data_dir["test"],
86
+ "partition_type": 1,
87
+ "eval_partition_file": data_dir["eval_partition_file"],
88
+ "bbox_file": data_dir["bbox_file"],
89
+ "landmarks_file": data_dir["landmarks_file"]
90
+ }
91
  ),
92
  datasets.SplitGenerator(
93
  name=datasets.Split.VALIDATION,
94
  gen_kwargs={
95
  "split": "val",
96
+ "data_dir": data_dir["validation"],
97
+ "partition_type": 2,
98
+ "eval_partition_file": data_dir["eval_partition_file"],
99
+ "bbox_file": data_dir["bbox_file"],
100
+ "landmarks_file": data_dir["landmarks_file"]
101
+ }
102
+ )
103
  ]
104
 
105
+ def _generate_examples(self, split, data_dir, partition_type, eval_partition_file, bbox_file, landmarks_file):
 
 
106
 
107
  #Abrimos los dos ficheros
108
+ fichero0 = open(eval_partition_file, "r", encoding="utf-8")
109
+ fichero1 = open(bbox_file, "r", encoding="utf-8")
110
+ fichero2 = open(landmarks_file, "r", encoding="utf-8")
111
 
112
+ #Creamos una lista a partir del contenido de Fichero 0
113
+ lista = fichero0.readlines()
114
+ for i, eval_line in enumerate(lista):
115
 
116
+ # Se obtiene el tipo de split
117
+ eval_line = eval_line.rstrip()
118
+ if not ".jpg" in eval_line:
119
  break
120
+ strip_type_in_line = int(" ".join(eval_line.split()).split(" ")[1])
121
+ if strip_type_in_line != partition_type:
122
+ break
123
+
124
+ # Se obtiene la imágen
125
+ image_file_path = os.path.join(data_dir, eval_line[0])
126
 
127
+ # Se lee la línea de bbox
128
+ bbox_line = fichero1.readline(i+2)
129
+ bbox = " ".join(bbox_line.split()).split(" ")
130
  bbox_total = [int(bbox[1]), int(bbox[2]), int(bbox[3]), int(bbox[4])]
131
+
132
+ # Se lee la línea de landmarks
133
+ landmarks_line = fichero2.readline(i+2)
134
+ landmarks = " ".join(landmarks_line.split()).split(" ")
135
  facial_landmarks = {
136
  'lefteye': [landmarks[1], landmarks[2]],
137
  'righteye': [landmarks[3], landmarks[4]],
 
143
  yield idx, {"image": image_file_path, "facial_landmarks": facial_landmarks, "bbox": bbox_total}
144
  idx += 1
145
  else:
146
+ fichero0.close()
147
  fichero1.close()
148
  fichero2.close()