def _make_label_id_map(self, src_label_cat, label_mapping, default_action): dst_label_cat = LabelCategories(attributes=src_label_cat.attributes) id_mapping = {} for src_index, src_label in enumerate(src_label_cat.items): dst_label = label_mapping.get(src_label.name) if not dst_label and default_action == self.DefaultAction.keep: dst_label = src_label.name # keep unspecified as is if not dst_label: continue dst_index = dst_label_cat.find(dst_label)[0] if dst_index is None: dst_label_cat.add(dst_label, src_label.parent, src_label.attributes) dst_index = dst_label_cat.find(dst_label)[0] id_mapping[src_index] = dst_index if log.getLogger().isEnabledFor(log.DEBUG): log.debug("Label mapping:") for src_id, src_label in enumerate(src_label_cat.items): if id_mapping.get(src_id): log.debug("#%s '%s' -> #%s '%s'", src_id, src_label.name, id_mapping[src_id], dst_label_cat.items[id_mapping[src_id]].name ) else: log.debug("#%s '%s' -> <deleted>", src_id, src_label.name) self._map_id = lambda src_id: id_mapping.get(src_id, None) self._categories[AnnotationType.label] = dst_label_cat
def make_voc_categories(label_map=None): if label_map is None: label_map = make_voc_label_map() categories = {} label_categories = LabelCategories() label_categories.attributes.update(['difficult', 'truncated', 'occluded']) for label, desc in label_map.items(): label_categories.add(label, attributes=desc[2]) for part in OrderedDict((k, None) for k in chain( *(desc[1] for desc in label_map.values()))): label_categories.add(part) categories[AnnotationType.label] = label_categories has_colors = any(v[0] is not None for v in label_map.values()) if not has_colors: # generate new colors colormap = generate_colormap(len(label_map)) else: # only copy defined colors label_id = lambda label: label_categories.find(label)[0] colormap = { label_id(name): desc[0] for name, desc in label_map.items() if desc[0] is not None } mask_categories = MaskCategories(colormap) mask_categories.inverse_colormap # pylint: disable=pointless-statement categories[AnnotationType.mask] = mask_categories return categories
def make_camvid_categories(label_map=None): if label_map is None: label_map = CamvidLabelMap # There must always be a label with color (0, 0, 0) at index 0 bg_label = find(label_map.items(), lambda x: x[1] == (0, 0, 0)) if bg_label is not None: bg_label = bg_label[0] else: bg_label = 'background' if bg_label not in label_map: has_colors = any(v is not None for v in label_map.values()) color = (0, 0, 0) if has_colors else None label_map[bg_label] = color label_map.move_to_end(bg_label, last=False) categories = {} label_categories = LabelCategories() for label, desc in label_map.items(): label_categories.add(label) categories[AnnotationType.label] = label_categories has_colors = any(v is not None for v in label_map.values()) if not has_colors: # generate new colors colormap = generate_colormap(len(label_map)) else: # only copy defined colors label_id = lambda label: label_categories.find(label)[0] colormap = { label_id(name): (desc[0], desc[1], desc[2]) for name, desc in label_map.items() } mask_categories = MaskCategories(colormap) mask_categories.inverse_colormap # pylint: disable=pointless-statement categories[AnnotationType.mask] = mask_categories return categories