예제 #1
0
    def _write_shape(self, shape):
        if shape.label is None:
            return

        shape_data = OrderedDict([
            ("label", self._get_label(shape.label).name),
            ("occluded", str(int(shape.attributes.get('occluded', False)))),
        ])

        if shape.type == AnnotationType.bbox:
            shape_data.update(OrderedDict([
                ("xtl", "{:.2f}".format(shape.points[0])),
                ("ytl", "{:.2f}".format(shape.points[1])),
                ("xbr", "{:.2f}".format(shape.points[2])),
                ("ybr", "{:.2f}".format(shape.points[3]))
            ]))
        else:
            shape_data.update(OrderedDict([
                ("points", ';'.join((
                    ','.join((
                        "{:.2f}".format(x),
                        "{:.2f}".format(y)
                    )) for x, y in pairs(shape.points))
                )),
            ]))

        shape_data['z_order'] = str(int(shape.z_order))
        if shape.group:
            shape_data['group_id'] = str(shape.group)

        if shape.type == AnnotationType.bbox:
            self._writer.open_box(shape_data)
        elif shape.type == AnnotationType.polygon:
            self._writer.open_polygon(shape_data)
        elif shape.type == AnnotationType.polyline:
            self._writer.open_polyline(shape_data)
        elif shape.type == AnnotationType.points:
            self._writer.open_points(shape_data)
        else:
            raise NotImplementedError("unknown shape type")

        for attr_name, attr_value in shape.attributes.items():
            if isinstance(attr_value, bool):
                attr_value = 'true' if attr_value else 'false'
            if attr_name in self._get_label(shape.label).attributes:
                self._writer.add_attribute(OrderedDict([
                    ("name", str(attr_name)),
                    ("value", str(attr_value)),
                ]))

        if shape.type == AnnotationType.bbox:
            self._writer.close_box()
        elif shape.type == AnnotationType.polygon:
            self._writer.close_polygon()
        elif shape.type == AnnotationType.polyline:
            self._writer.close_polyline()
        elif shape.type == AnnotationType.points:
            self._writer.close_points()
        else:
            raise NotImplementedError("unknown shape type")
예제 #2
0
    def _write_shape(self, shape, item):
        if shape.label is None:
            log.warning("Item %s: skipping a %s with no label", item.id,
                        shape.type.name)
            return

        label_name = self._get_label(shape.label).name
        shape_data = OrderedDict([
            ("label", label_name),
            ("occluded", str(int(shape.attributes.get('occluded', False)))),
        ])

        if shape.type == AnnotationType.bbox:
            shape_data.update(
                OrderedDict([("xtl", "{:.2f}".format(shape.points[0])),
                             ("ytl", "{:.2f}".format(shape.points[1])),
                             ("xbr", "{:.2f}".format(shape.points[2])),
                             ("ybr", "{:.2f}".format(shape.points[3]))]))
        else:
            shape_data.update(
                OrderedDict([
                    ("points", ';'.join((','.join(
                        ("{:.2f}".format(x), "{:.2f}".format(y)))
                                         for x, y in pairs(shape.points)))),
                ]))

        shape_data['z_order'] = str(int(shape.z_order))
        if shape.group:
            shape_data['group_id'] = str(shape.group)

        if shape.type == AnnotationType.bbox:
            self._writer.open_box(shape_data)
        elif shape.type == AnnotationType.polygon:
            self._writer.open_polygon(shape_data)
        elif shape.type == AnnotationType.polyline:
            self._writer.open_polyline(shape_data)
        elif shape.type == AnnotationType.points:
            self._writer.open_points(shape_data)
        else:
            raise NotImplementedError("unknown shape type")

        for attr_name, attr_value in shape.attributes.items():
            if attr_name in self._context._builtin_attrs:
                continue
            if isinstance(attr_value, bool):
                attr_value = 'true' if attr_value else 'false'
            if self._context._allow_undeclared_attrs or \
                    attr_name in self._get_label_attrs(shape.label):
                self._writer.add_attribute(
                    OrderedDict([
                        ("name", str(attr_name)),
                        ("value", str(attr_value)),
                    ]))
            else:
                log.warning(
                    "Item %s: skipping undeclared "
                    "attribute '%s' for label '%s' "
                    "(allow with --allow-undeclared-attrs option)", item.id,
                    attr_name, label_name)

        if shape.type == AnnotationType.bbox:
            self._writer.close_box()
        elif shape.type == AnnotationType.polygon:
            self._writer.close_polygon()
        elif shape.type == AnnotationType.polyline:
            self._writer.close_polyline()
        elif shape.type == AnnotationType.points:
            self._writer.close_points()
        else:
            raise NotImplementedError("unknown shape type")