Exemplo n.º 1
0
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
Exemplo n.º 2
0
    def to_detected_object(self, name=None):
        """Returns an ``eta.core.objects.DetectedObject`` representation of
        this instance.

        Args:
            name (None): the name of the label field

        Returns:
            an ``eta.core.objects.DetectedObject``
        """
        label = self.label

        # pylint: disable=unpacking-non-sequence
        tlx, tly, w, h = self.bounding_box
        brx = tlx + w
        bry = tly + h
        bounding_box = etag.BoundingBox.from_coords(tlx, tly, brx, bry)

        confidence = self.confidence

        # pylint: disable=no-member
        attrs = etad.AttributeContainer()
        for attr_name, attr in self.attributes.items():
            attrs.add(etad.CategoricalAttribute(attr_name, attr.value))

        return etao.DetectedObject(
            label=label,
            bounding_box=bounding_box,
            confidence=confidence,
            name=name,
            attrs=attrs,
        )
Exemplo n.º 3
0
def _make_attribute(name, value):
    if isinstance(value, bool):
        return etad.BooleanAttribute(name, value)

    if etau.is_numeric(value):
        return etad.NumericAttribute(name, value)

    return etad.CategoricalAttribute(name, value)
Exemplo n.º 4
0
    def to_eta_attribute(self):
        """Returns an ``eta.core.data.Attribute`` representation of the
        attribute.

        Returns:
            an ``eta.core.data.Attribute``
        """
        return etad.CategoricalAttribute(self.name, self.value)
Exemplo n.º 5
0
    def to_eta_attribute(self):
        """Returns an ``eta.core.data.Attribute`` representation of the
        attribute.

        Returns:
            an ``eta.core.data.Attribute``
        """
        if isinstance(self.value, bool):
            return etad.BooleanAttribute(self.name, self.value)

        if etau.is_numeric(self.value):
            return etad.NumericAttribute(self.name, self.value)

        return etad.CategoricalAttribute(self.name, self.value)
Exemplo n.º 6
0
def _to_eta_attributes(attributes):
    attrs = etad.AttributeContainer()
    for attr_name, attr in attributes.items():
        attr_value = attr.value
        if isinstance(attr_value, bool):
            _attr = etad.BooleanAttribute(attr_name, attr_value)
        elif etau.is_numeric(attr_value):
            _attr = etad.NumericAttribute(attr_name, attr_value)
        else:
            _attr = etad.CategoricalAttribute(attr_name, str(attr_value))

        attrs.add(_attr)

    return attrs
Exemplo n.º 7
0
    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
Exemplo n.º 8
0
    def to_schema(self):
        """Returns an ``eta.core.image.ImageLabelsSchema`` representation of
        the task labels.

        Returns:
            an ``eta.core.image.ImageLabelsSchema``
        """
        schema = etai.ImageLabelsSchema()

        for label in self.labels:
            _label = label["name"]
            schema.add_object_label(_label)
            for attribute in label.get("attributes", []):
                _name = attribute["name"]
                _categories = attribute["categories"]
                for _value in _categories:
                    _attr = etad.CategoricalAttribute(_name, _value)
                    schema.add_object_attribute(_label, _attr)

        return schema
Exemplo n.º 9
0
    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