def _parse_image_labels(label): if label is None: return etai.ImageLabels() if isinstance(label, dict): image_labels = etai.ImageLabels() for name, _label in label.items(): if _label is not None: image_labels.merge_labels(_label.to_image_labels(name=name)) return image_labels return label.labels
def draw_labeled_image(sample, label_fields, outpath, annotation_config=None): """Draws an annotated version of the image sample with its label field(s) overlaid to disk. Args: sample: a :class:`fiftyone.core.sample.Sample` instance label_fields: the list of :class:`fiftyone.core.labels.ImageLabel` fields to render outpath: the path to write the annotated image annotation_config (None): an :class:`AnnotationConfig` specifying how to render the annotations """ if annotation_config is None: annotation_config = _DEFAULT_ANNOTATION_CONFIG image_labels = etai.ImageLabels() for label_field in label_fields: label = sample[label_field] if label is None: continue image_labels.merge_labels(label.to_image_labels(name=label_field)) img = etai.read(sample.filepath) anno_img = etaa.annotate_image(img, image_labels, annotation_config=annotation_config) etai.write(anno_img, outpath)
def _make_image_labels_dataset(img, images_dir, num_samples=4, num_objects_per_sample=3): exts = [".jpg", ".png"] samples = [] for idx in range(num_samples): filepath = os.path.join(images_dir, "%06d%s" % (idx, exts[idx % len(exts)])) etai.write(img, filepath) image_labels = etai.ImageLabels() _label = random.choice(["sun", "rain", "snow"]) image_labels.add_attribute(etad.CategoricalAttribute("label", _label)) for _ in range(num_objects_per_sample): _label = random.choice(["cat", "dog", "bird", "rabbit"]) _xtl = 0.8 * random.random() _ytl = 0.8 * random.random() _bounding_box = etag.BoundingBox.from_coords( _xtl, _ytl, _xtl + 0.2, _ytl + 0.2) image_labels.add_object( etao.DetectedObject(label=_label, bounding_box=_bounding_box)) samples.append( fo.Sample( filepath=filepath, ground_truth=fo.ImageLabels(labels=image_labels), )) dataset = fo.Dataset() dataset.add_samples(samples) return dataset
def _parse_bdd_annotation(d, frame_size): image_labels = etai.ImageLabels() # Frame attributes frame_attrs = d.get("attributes", {}) image_labels.attrs = _make_attributes(frame_attrs) # Objects objects = d.get("labels", []) for obj in objects: if "box2d" not in obj: continue label = obj["category"] bbox = obj["box2d"] bounding_box = etag.BoundingBox.from_abs_coords( bbox["x1"], bbox["y1"], bbox["x2"], bbox["y2"], frame_size=frame_size, ) obj_attrs = obj.get("attributes", {}) attrs = _make_attributes(obj_attrs) image_labels.add_object( etao.DetectedObject( label=label, bounding_box=bounding_box, attrs=attrs, )) return fol.ImageLabels(labels=image_labels)
def to_image_labels(self, name=None): """Returns an ``eta.core.image.ImageLabels`` representation of this instance. Args: name (None): the name of the label field Returns: an ``eta.core.image.ImageLabels`` """ return etai.ImageLabels(mask=self.mask)
def to_image_labels(self, name=None): """Returns an ``eta.core.image.ImageLabels`` representation of this instance. Args: name (None): the name of the label field Returns: an ``eta.core.image.ImageLabels`` """ image_labels = etai.ImageLabels() image_labels.add_object(self.to_detected_object(name=name)) return image_labels
def condense_image_labels_field( dataset, label_field, prefix=None, labels_dict=None, keep_label_fields=False, ): """Condenses multiple :class:`fiftyone.core.labels.Label`` fields into a single :class:`fiftyone.core.labels.ImageLabels` field. Provide either ``prefix`` or ``labels_dict`` to customize the fields that are condensed. If you provide neither, all :class:`fiftyone.core.labels.Label`` fields are condensed. Args: dataset: a :class:`fiftyone.core.dataset.Dataset` label_field: the name of the :class:`fiftyone.core.labels.ImageLabels` field to create prefix (None): a label field prefix; all :class:`fiftyone.core.labels.Label` fields matching this prefix are merged into ``label_field``, with the prefix removed from the names of the labels labels_dict (None): a dictionary mapping names of :class:`fiftyone.core.labels.Label` fields to names to give them in the condensed :class:`fiftyone.core.labels.ImageLabels` keep_label_fields (False): whether to keep the input label fields after ``label_field`` is created. By default, the fields are deleted """ if prefix is None: prefix = "" if labels_dict is None: labels_dict = _get_label_dict_for_prefix(dataset, prefix) logger.info("Condensing image labels into field '%s'", label_field) with fou.ProgressBar() as pb: for sample in pb(dataset): image_labels = etai.ImageLabels() for field_name, name in labels_dict.items(): image_labels.merge_labels( sample[field_name].to_image_labels(name=name)) if not keep_label_fields: sample.clear_field(field_name) sample[label_field] = fol.ImageLabels(labels=image_labels) sample.save() if not keep_label_fields: for field_name in labels_dict: dataset.delete_sample_field(field_name)
def to_image_labels(self, name=None): """Returns an ``eta.core.image.ImageLabels`` representation of this instance. Args: name (None): the name of the label field Returns: an ``eta.core.image.ImageLabels`` """ image_labels = etai.ImageLabels() image_labels.add_attribute( etad.CategoricalAttribute(name, self.label, confidence=self.confidence)) return image_labels
def to_image_labels(self, name=None): """Returns an ``eta.core.image.ImageLabels`` representation of this instance. Args: name (None): the name of the label field Returns: an ``eta.core.image.ImageLabels`` """ image_labels = etai.ImageLabels() # pylint: disable=not-an-iterable for detection in self.detections: image_labels.add_object(detection.to_detected_object(name=name)) return image_labels
def to_image_labels(self, name=None): """Returns an ``eta.core.image.ImageLabels`` representation of this instance. Args: name (None): the name of the label field Returns: an ``eta.core.image.ImageLabels`` """ image_labels = etai.ImageLabels() # pylint: disable=not-an-iterable for keypoint in self.keypoints: image_labels.add_keypoints(keypoint.to_eta_keypoints(name=name)) return image_labels
def _parse_bdd_annotation(d, frame_size): image_labels = etai.ImageLabels() # Frame attributes frame_attrs = d.get("attributes", {}) image_labels.attrs = _make_attributes(frame_attrs) # Objects and polylines labels = d.get("labels", []) for label in labels: if "box2d" in label: dobj = _parse_bdd_object(label, frame_size) image_labels.add_object(dobj) if "poly2d" in label: polylines = _parse_bdd_polylines(label, frame_size) image_labels.add_polylines(polylines) return fol.ImageLabels(labels=image_labels)
def to_image_labels(self, name=None): """Returns an ``eta.core.image.ImageLabels`` representation of this instance. Args: name (None): the name of the label field Returns: an ``eta.core.image.ImageLabels`` """ image_labels = etai.ImageLabels() # pylint: disable=not-an-iterable for classification in self.classifications: image_labels.add_attribute( etad.CategoricalAttribute( name, classification.label, confidence=classification.confidence, )) return image_labels
def _make_bdd_annotation(image_labels_or_dict, metadata, filename): # Convert to `eta.core.image.ImageLabels` format if isinstance(image_labels_or_dict, dict): image_labels = etai.ImageLabels() for name, label in image_labels_or_dict.items(): if label is not None: image_labels.merge_labels(label.to_image_labels(name=name)) else: image_labels = image_labels_or_dict.labels # Frame attributes frame_attrs = {a.name: a.value for a in image_labels.attrs} # Objects labels = [] frame_size = (metadata.width, metadata.height) for idx, obj in enumerate(image_labels.objects): tlx, tly, w, h = obj.bounding_box.coords_in(frame_size=frame_size) labels.append({ "attributes": {a.name: a.value for a in obj.attrs}, "box2d": { "x1": tlx, "x2": tlx + w, "y1": tly, "y2": tly + h, }, "category": obj.label, "id": idx, "manualAttributes": True, "manualShape": True, }) return { "name": filename, "attributes": frame_attrs, "labels": labels, }
def draw_labeled_image(sample, outpath, label_fields=None, annotation_config=None): """Draws an annotated version of the image sample with its label field(s) overlaid to disk. Args: sample: a :class:`fiftyone.core.sample.Sample` instance outpath: the path to write the annotated image label_fields (None): a list of :class:`fiftyone.core.labels.ImageLabel` fields to render. If omitted, all compatiable fields are rendered annotation_config (None): an :class:`AnnotationConfig` specifying how to render the annotations """ if label_fields is None: label_fields = _get_image_label_fields(sample) if annotation_config is None: annotation_config = AnnotationConfig.default() image_labels = etai.ImageLabels() for label_field in label_fields: label = sample[label_field] if label is None: continue image_labels.merge_labels(label.to_image_labels(name=label_field)) img = etai.read(sample.filepath) anno_img = etaa.annotate_image(img, image_labels, annotation_config=annotation_config) etai.write(anno_img, outpath)
def _make_bdd_annotation(image_labels_or_dict, metadata, filename): # Convert to `eta.core.image.ImageLabels` format if isinstance(image_labels_or_dict, dict): image_labels = etai.ImageLabels() for name, label in image_labels_or_dict.items(): if label is not None: image_labels.merge_labels(label.to_image_labels(name=name)) else: image_labels = image_labels_or_dict.labels labels = [] frame_size = (metadata.width, metadata.height) uuid = -1 # Frame attributes frame_attrs = {a.name: a.value for a in image_labels.attrs} # Objects for obj in image_labels.objects: uuid += 1 tlx, tly, w, h = obj.bounding_box.coords_in(frame_size=frame_size) labels.append( { "id": uuid, "category": obj.label, "manualAttributes": True, "manualShape": True, "attributes": {a.name: a.value for a in obj.attrs}, "box2d": {"x1": tlx, "x2": tlx + w, "y1": tly, "y2": tly + h}, } ) # Polylines for polyline in image_labels.polylines: uuid += 1 vertices = polyline.coords_in(frame_size=frame_size) types = polyline.attrs.get_attr_value_with_name("types", None) labels.append( { "id": uuid, "category": polyline.label, "manualAttributes": True, "manualShape": True, "attributes": { a.name: a.value for a in polyline.attrs if a.name != "types" }, "poly2d": { "types": types, "closed": polyline.closed, "vertices": vertices, }, } ) return { "name": filename, "attributes": frame_attrs, "labels": labels, }