def convert(self, devkit_dir): """ Args: devkit_dir: path to VOC2012 devkit dir (e.g. VOCdevkit/VOC2012) """ devkit_dir = get_path(devkit_dir, is_directory=True) image_set_file = devkit_dir / 'ImageSets' / 'Segmentation' / 'test.txt' mask_dir = Path('SegmentationClass') image_dir = Path('JPEGImages') annotations = [] for image in read_txt(image_set_file): annotation = SegmentationAnnotation( str(image_dir / '{}.jpg'.format(image)), str(mask_dir / '{}.png'.format(image)), mask_loader=GTMaskLoader.SCIPY) annotations.append(annotation) meta = { 'label_map': dict(enumerate(_VOC_CLASSES_SEGMENTATION)), 'background_label': 0, 'segmentation_colors': _SEGMENTATION_COLORS } return annotations, meta
def prepare_annotation(self, ann_file: Path, train=False, landmarks_map=None): positive_pairs, negative_pairs = [], [] ann_lines = read_txt(ann_file) for line in ann_lines[1:]: # skip header pair = line.strip().split() if len(pair) == 3: positive_pairs.append(pair) elif len(pair) == 4: negative_pairs.append(pair) all_images = set() positive_data, all_images = self.convert_positive( positive_pairs, all_images) negative_data, all_images = self.convert_negative( negative_pairs, all_images) annotations = [] for image in all_images: annotation = ReIdentificationClassificationAnnotation( image, positive_data[image], negative_data[image]) if landmarks_map: image_landmarks = landmarks_map.get(image) annotation.metadata['keypoints'] = image_landmarks if train: annotation.metadata['train'] = True annotations.append(annotation) return annotations
def convert(self, devkit_dir, has_background=True): """ Args: devkit_dir: path to VOC2007 devkit dir (e.g. .../VOCdevkit/VOC2007) has_background: allows to add background label to label map """ if isinstance(has_background, str): has_background = string_to_bool(has_background) class_to_ind = prepare_detection_labels(has_background) devkit_dir = get_path(devkit_dir, is_directory=True) annotation_directory = get_path(devkit_dir / 'Annotations', is_directory=True) images_directory = get_path(devkit_dir / 'JPEGImages', is_directory=True) detections = [] image_set_file = devkit_dir / 'ImageSets' / 'Main' / 'test.txt' for image in tqdm(read_txt(image_set_file, sep=None)): file_path = annotation_directory / '{}.xml'.format(image) tree = ET.parse(str(file_path)) identifier = tree.find('.//filename').text image_path = images_directory / identifier if not image_path.is_file(): raise FileNotFoundError("{}: {}".format( os.strerror(errno.ENOENT), image_path)) labels, x_mins, y_mins, x_maxs, y_maxs = [], [], [], [], [] difficult_indices = [] for entry in tree.getroot(): if not entry.tag.startswith('object'): continue bbox = entry.find('bndbox') difficult = int(entry.find('difficult').text) if difficult == 1: difficult_indices.append(len(labels)) labels.append(class_to_ind[entry.find('name').text]) x_mins.append(float(bbox.find('xmin').text) - 1) y_mins.append(float(bbox.find('ymin').text) - 1) x_maxs.append(float(bbox.find('xmax').text) - 1) y_maxs.append(float(bbox.find('ymax').text) - 1) image_annotation = DetectionAnnotation(identifier, labels, x_mins, y_mins, x_maxs, y_maxs) image_annotation.metadata['difficult_boxes'] = difficult_indices detections.append(image_annotation) meta = {'label_map': reverse_label_map(class_to_ind)} if has_background: meta['background_label'] = 0 return detections, meta
def convert(self, pairs_file, train_file=None, landmarks_file=None): landmarks_map = {} if landmarks_file: for landmark_line in read_txt(landmarks_file): landmark_line = landmark_line.split('\t') landmarks_map[landmark_line[0]] = [ int(point) for point in landmark_line[1:] ] test_annotations = self.prepare_annotation(pairs_file, True, landmarks_map) if train_file: train_annotations = self.prepare_annotation( train_file, True, landmarks_map) test_annotations += train_annotations return test_annotations, {}
def convert(self, wider_annotation: str, label_start=1): """ Args: wider_annotation: path to wider validation file label_start: start index for labels """ wider_annotation = get_path(wider_annotation) image_annotations = read_txt(wider_annotation) image_ids = [] for image_id, line in enumerate(image_annotations): if '.jpg' in line: image_ids.append(image_id) annotations = [] for image_id in image_ids: identifier = image_annotations[image_id] bbox_count = image_annotations[image_id + 1] bbox_lines = image_annotations[image_id + 2:image_id + 2 + int(bbox_count)] x_mins, y_mins, x_maxs, y_maxs = [], [], [], [] for bbox in bbox_lines: x_min, y_min, x_max, y_max = convert_bboxes_xywh_to_x1y1x2y2( *(map(float, (bbox.split(' ')[0:4])))) x_mins.append(x_min) y_mins.append(y_min) x_maxs.append(x_max) y_maxs.append(y_max) annotations.append( DetectionAnnotation(identifier, [int(label_start)] * len(x_mins), x_mins, y_mins, x_maxs, y_maxs)) return annotations, { 'label_map': { 0: '__background__', int(label_start): 'face' }, 'background_label': 0 }
def _read_labels(labels_file): """Extract label names from labels.txt file""" return read_txt(labels_file)
def rename_identifiers(annotation_list, images_file: str): for annotation, image in zip(annotation_list, read_txt(images_file)): annotation.identifier = image return annotation_list