Пример #1
0
def al2pal(annotations):
    _annolist = AnnoList_pb2.AnnoList()

    for a in annotations:
        _a = _annolist.annotation.add()
        _a.imageName = a.imageName

        for r in a.rects:
            _r = _a.rect.add()
            _r.x1 = r.x1
            _r.y1 = r.y1
            _r.x2 = r.x2
            _r.y2 = r.y2
            _r.score = r.score

            if hasattr(r, 'id'):
                _r.id = r.id

            if hasattr(r, 'track_id'):
                _r.track_id = r.track_id

            if hasattr(r, 'distance3d'):
                _r.distance3d = r.distance3d

            if hasattr(r, 'width3d'):
                _r.width3d = r.width3d

            if hasattr(r, 'height3d'):
                _r.height3d = r.height3d

            if hasattr(r, 'length3d'):
                _r.length3d = r.length3d

    return _annolist
Пример #2
0
def loadPal(filename):
    _annolist = AnnoList_pb2.AnnoList()

    f = open(filename, "rb")
    _annolist.ParseFromString(f.read())
    f.close()

    return _annolist
Пример #3
0
    def add_attribute(self, name, dtype):
        _adesc = AnnoList_pb2.AttributeDesc()
        _adesc.name = name
        if self.attribute_desc:
            _adesc.id = max(
                (self.attribute_desc[d].id for d in self.attribute_desc)) + 1
        else:
            _adesc.id = 0

        if dtype == int:
            _adesc.dtype = AnnoList.TYPE_INT32
        elif dtype == float or dtype == np.float32:
            _adesc.dtype = AnnoList.TYPE_FLOAT
        elif dtype == str:
            _adesc.dtype = AnnoList.TYPE_STRING
        else:
            print "unknown attribute type: ", dtype
            assert (False)

        #print "adding attribute: {}, id: {}, type: {}".format(_adesc.name, _adesc.id, _adesc.dtype);
        self.attribute_desc[name] = _adesc
Пример #4
0
def al2pal(annotations):
    _annolist = AnnoList_pb2.AnnoList()

    #assert(isinstance(annotations, AnnotationLib.AnnoList));

    # check type of attributes, add missing attributes
    for a in annotations:
        for r in a.rects:
            for k, v in r.at.iteritems():
                if not k in annotations.attribute_desc:
                    annotations.add_attribute(k, type(v))
                else:
                    assert (AnnotationLib.is_compatible_attr_type(
                        annotations.attribute_desc[k].dtype, type(v)))

    # check attributes values
    for a in annotations:
        for r in a.rects:
            for k, v in r.at.iteritems():
                if k in annotations.attribute_val_to_str:
                    # don't allow undefined values
                    if not v in annotations.attribute_val_to_str[k]:
                        print "attribute: {}, undefined value: {}".format(
                            k, v)
                        assert (False)

    # store attribute descriptions in pal structure
    for aname, adesc in annotations.attribute_desc.iteritems():
        _annolist.attribute_desc.extend([adesc])

    for a in annotations:
        _a = _annolist.annotation.add()
        _a.imageName = a.imageName

        for r in a.rects:
            _r = _a.rect.add()

            _r.x1 = r.x1
            _r.y1 = r.y1
            _r.x2 = r.x2
            _r.y2 = r.y2

            _r.score = float(r.score)

            if hasattr(r, 'id'):
                _r.id = r.id

            if hasattr(r, 'track_id'):
                _r.track_id = r.track_id

            if hasattr(r, 'at'):
                for k, v in r.at.items():
                    _at = _r.attribute.add()

                    _at.id = annotations.attribute_desc[k].id

                    if annotations.attribute_desc[
                            k].dtype == AnnotationLib.AnnoList.TYPE_INT32:
                        assert (AnnotationLib.is_compatible_attr_type(
                            AnnotationLib.AnnoList.TYPE_INT32, type(v)))
                        _at.val = int(v)
                    elif annotations.attribute_desc[
                            k].dtype == AnnotationLib.AnnoList.TYPE_FLOAT:
                        assert (AnnotationLib.is_compatible_attr_type(
                            AnnotationLib.AnnoList.TYPE_FLOAT, type(v)))
                        _at.fval = float(v)
                    elif annotations.attribute_desc[
                            k].dtype == AnnotationLib.AnnoList.TYPE_STRING:
                        assert (AnnotationLib.is_compatible_attr_type(
                            AnnotationLib.AnnoList.TYPE_STRING, type(v)))
                        _at.strval = str(v)
                    else:
                        assert (false)

    return _annolist