Exemple #1
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;
Exemple #2
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