def __getitem__(self, index): joints_data = self.data[index].copy() # Leer la imagen del disco image = cv2.imread(joints_data['imgPath'], cv2.IMREAD_COLOR | cv2.IMREAD_IGNORE_ORIENTATION) if self.color_rgb: image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) if image is None: raise ValueError('Fail to read %s' % image) joints = joints_data['joints'] joints_vis = joints_data['joints_visibility'] c = joints_data['center'] s = joints_data['scale'] score = joints_data['score'] if 'score' in joints_data else 1 r = 0 # Aplicar aumento de datos if self.is_train: if self.half_body_prob and \ random.random() < self.half_body_prob and \ np.sum(joints_vis[:, 0]) > self.nof_joints_half_body: c_half_body, s_half_body = self._half_body_transform(joints, joints_vis) if c_half_body is not None and s_half_body is not None: c, s = c_half_body, s_half_body sf = self.scale_factor rf = self.rotation_factor if self.scale: s = s * np.clip(random.random() * sf + 1, 1 - sf, 1 + sf) # Un factor de escala aleatorio en [1 - sf, 1 + sf] if self.rotate_prob and random.random() < self.rotate_prob: r = np.clip(random.random() * rf, -rf * 2, rf * 2) # Un factor de rotación aleatorio en [-2 * rf, 2 * rf] else: r = 0 if self.flip_prob and random.random() < self.flip_prob: image = image[:, ::-1, :] joints, joints_vis = fliplr_joints(joints, joints_vis, image.shape[1], self.flip_pairs) c[0] = image.shape[1] - c[0] - 1 #Aplicamos transformación en las articulaciones y la imagen. trans = get_affine_transform(c, s, self.pixel_std, r, self.image_size) image = cv2.warpAffine( image, trans, (int(self.image_size[0]), int(self.image_size[1])), flags=cv2.INTER_LINEAR ) for i in range(self.nof_joints): if joints_vis[i, 0] > 0.: joints[i, 0:2] = affine_transform(joints[i, 0:2], trans) # Convertir imagen a tensor y normalizar if self.transform is not None: image = self.transform(image) target, target_weight = self._generate_target(joints, joints_vis) # Actualizar metadatos joints_data['joints'] = joints joints_data['joints_visibility'] = joints_vis joints_data['center'] = c joints_data['scale'] = s joints_data['rotation'] = r joints_data['score'] = score return image, target.astype(np.float32), target_weight.astype(np.float32), joints_data
def __getitem__(self, index): joints_data = self.data[index].copy() # Read the image from disk image = cv2.imread(joints_data['imgPath'], cv2.IMREAD_COLOR | cv2.IMREAD_IGNORE_ORIENTATION) if self.color_rgb: image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) if image is None: raise ValueError('Fail to read %s' % image) joints = joints_data['joints'] joints_vis = joints_data['joints_visibility'] c = joints_data['center'] s = joints_data['scale'] score = joints_data['score'] if 'score' in joints_data else 1 r = 0 # Apply data augmentation if self.is_train: if self.half_body_prob and \ random.random() < self.half_body_prob and \ np.sum(joints_vis[:, 0]) > self.nof_joints_half_body: c_half_body, s_half_body = self._half_body_transform(joints, joints_vis) if c_half_body is not None and s_half_body is not None: c, s = c_half_body, s_half_body sf = self.scale_factor rf = self.rotation_factor if self.scale: s = s * np.clip(random.random() * sf + 1, 1 - sf, 1 + sf) # A random scale factor in [1 - sf, 1 + sf] if self.rotate_prob and random.random() < self.rotate_prob: r = np.clip(random.random() * rf, -rf * 2, rf * 2) # A random rotation factor in [-2 * rf, 2 * rf] else: r = 0 if self.flip_prob and random.random() < self.flip_prob: image = image[:, ::-1, :] joints, joints_vis = fliplr_joints(joints, joints_vis, image.shape[1], self.flip_pairs) c[0] = image.shape[1] - c[0] - 1 # Apply affine transform on joints and image trans = get_affine_transform(c, s, self.pixel_std, r, self.image_size) image = cv2.warpAffine( image, trans, (int(self.image_size[0]), int(self.image_size[1])), flags=cv2.INTER_LINEAR ) for i in range(self.nof_joints): if joints_vis[i, 0] > 0.: joints[i, 0:2] = affine_transform(joints[i, 0:2], trans) # Convert image to tensor and normalize if self.transform is not None: # I could remove this check image = self.transform(image) target, target_weight = self._generate_target(joints, joints_vis) # Update metadata joints_data['joints'] = joints joints_data['joints_visibility'] = joints_vis joints_data['center'] = c joints_data['scale'] = s joints_data['rotation'] = r joints_data['score'] = score return image, target.astype(np.float32), target_weight.astype(np.float32), joints_data