Esempio n. 1
0
 def __init__(self,
              img_size,
              labels=None,
              img_tags=None,
              img_description="",
              pixelwise_scores_labels=None,
              custom_data=None):
     '''
     The constructor for Annotation class.
     :param img_size(tuple): size of the image
     :param labels(list): list of Label class objects
     :param img_tags(list): list of image tags
     :param img_description(str): image description
     :param pixelwise_scores_labels(list)
     '''
     if not isinstance(img_size, (tuple, list)):
         raise TypeError(
             '{!r} has to be a tuple or a list. Given type "{}".'.format(
                 'img_size', type(img_size)))
     self._img_size = tuple(img_size)
     self._img_description = img_description
     self._img_tags = take_with_default(img_tags, TagCollection())
     self._labels = []
     self._add_labels_impl(self._labels, take_with_default(labels, []))
     self._pixelwise_scores_labels = [
     ]  # @TODO: store pixelwise scores as usual geometry labels
     self._add_labels_impl(self._pixelwise_scores_labels,
                           take_with_default(pixelwise_scores_labels, []))
     self._custom_data = take_with_default(custom_data, {})
Esempio n. 2
0
    def from_json(cls, data, project_meta: ProjectMeta):
        '''
        The function from_json convert Label from json format to Label class object. If there is no ObjClass from
        input json format in ProjectMeta, it generate RuntimeError error.
        :param data: input label in json format
        :param project_meta: ProjectMeta class object
        :return: Label class object
        '''
        obj_class_name = data[LabelJsonFields.OBJ_CLASS_NAME]
        obj_class = project_meta.get_obj_class(obj_class_name)
        if obj_class is None:
            raise RuntimeError(
                f'Failed to deserialize a Label object from JSON: label class name {obj_class_name!r} '
                f'was not found in the given project meta.')

        if obj_class.geometry_type is AnyGeometry:
            geometry_type_actual = GET_GEOMETRY_FROM_STR(
                data[GEOMETRY_TYPE] if GEOMETRY_TYPE in
                data else data[GEOMETRY_SHAPE])
            geometry = geometry_type_actual.from_json(data)
        else:
            geometry = obj_class.geometry_type.from_json(data)

        return cls(geometry=geometry,
                   obj_class=obj_class,
                   tags=TagCollection.from_json(data[LabelJsonFields.TAGS],
                                                project_meta.tag_metas),
                   description=data.get(LabelJsonFields.DESCRIPTION, ""))
 def from_json(cls, data, project_meta: ProjectMeta):
     obj_class_name = data[LabelJsonFields.OBJ_CLASS_NAME]
     obj_class = project_meta.get_obj_class(obj_class_name)
     return cls(geometry=obj_class.geometry_type.from_json(data),
                obj_class=obj_class,
                tags=TagCollection.from_json(data[LabelJsonFields.TAGS], project_meta.obj_tag_metas),
                description=data.get(LabelJsonFields.DESCRIPTION, ""))
 def __init__(self, geometry: Geometry, obj_class: ObjClass, tags: TagCollection = None, description: str = ""):
     self._geometry = geometry
     self._obj_class = obj_class
     self._tags = take_with_default(tags, TagCollection())
     self._description = description
     self._validate_geometry_type()
     self._validate_geometry()
Esempio n. 5
0
 def inference(self, img, ann):
     output = infer_on_img(img, self.input_size, self.model)
     tag_id = np.argmax(output)
     score = output[tag_id]
     tag_name = self.idx_to_classification_tags[tag_id]
     tag = Tag(self.classification_tags.get(tag_name), round(float(score), 4))
     tags = TagCollection([tag])
     return Annotation(ann.img_size, img_tags=tags)
Esempio n. 6
0
def make_renamed_tags(tags: TagCollection, tag_meta_mapper: TagMetaMapper, skip_missing=True) -> TagCollection:
    renamed_tags = []
    for tag in tags:
        dest_tag_meta = tag_meta_mapper.map(tag.meta)
        if dest_tag_meta is not None:
            renamed_tags.append(tag.clone(meta=dest_tag_meta))
        elif not skip_missing:
            raise KeyError('Tag named {} could not be mapped to a destination name.'.format(tag.meta.name))
    return TagCollection(items=renamed_tags)
Esempio n. 7
0
 def from_json(cls, data, project_meta):
     img_size_dict = data[AnnotationJsonFields.IMG_SIZE]
     img_height = img_size_dict[AnnotationJsonFields.IMG_SIZE_HEIGHT]
     img_width = img_size_dict[AnnotationJsonFields.IMG_SIZE_WIDTH]
     img_size = (img_height, img_width)
     labels = [Label.from_json(label_json, project_meta) for label_json in data[AnnotationJsonFields.LABELS]]
     return cls(img_size=img_size,
                labels=labels,
                img_tags=TagCollection.from_json(data[AnnotationJsonFields.IMG_TAGS], project_meta.img_tag_metas),
                img_description=data.get(AnnotationJsonFields.IMG_DESCRIPTION, ""))
Esempio n. 8
0
 def __init__(self, img_size, labels=None, img_tags=None, img_description="", pixelwise_scores_labels=None):
     if not isinstance(img_size, (tuple, list)):
         raise TypeError('{!r} has to be a tuple or a list. Given type "{}".'.format('img_size', type(img_size)))
     self._img_size = tuple(img_size)
     self._img_description = img_description
     self._img_tags = take_with_default(img_tags, TagCollection())
     self._labels = []
     self._add_labels_impl(self._labels, take_with_default(labels, []))
     self._pixelwise_scores_labels = []      # This field is not serialized. @TODO: create another class AnnotationExtended???
     self._add_labels_impl(self._pixelwise_scores_labels, take_with_default(pixelwise_scores_labels, []))
Esempio n. 9
0
    def setUp(self):
        self._obj_class_gt = ObjClass(name='a', geometry_type=Rectangle)
        self._obj_class_pred = ObjClass(name='b', geometry_type=Rectangle)
        self._confidence_tag_meta = TagMeta(name='confidence', value_type=TagValueType.ANY_NUMBER)
        self._meta = ProjectMeta(
            obj_classes=ObjClassCollection([self._obj_class_gt, self._obj_class_pred]),
            tag_metas=TagMetaCollection([self._confidence_tag_meta]))

        # Will match self._pred_obj_1
        self._gt_obj_1 = Label(obj_class=self._obj_class_gt, geometry=Rectangle(0, 0, 10, 10))

        # Will match self._pred_obj_3
        self._gt_obj_2 = Label(obj_class=self._obj_class_gt, geometry=Rectangle(13, 13, 15, 15))

        # Will be a false negative
        self._gt_obj_3 = Label(obj_class=self._obj_class_gt, geometry=Rectangle(43, 43, 45, 45))

        # Will match self._gt_obj_1
        self._pred_obj_1 = Label(
            obj_class=self._obj_class_pred,
            geometry=Rectangle(0, 0, 9, 9),
            tags=TagCollection([Tag(meta=self._confidence_tag_meta, value=0.7)]))

        # Will be a false positive (self._pred_obj_1 has higher IoU).
        self._pred_obj_2 = Label(
            obj_class=self._obj_class_pred,
            geometry=Rectangle(0, 0, 8, 8),
            tags=TagCollection([Tag(meta=self._confidence_tag_meta, value=0.6)]))

        # Will match self._gt_obj_2
        self._pred_obj_3 = Label(
            obj_class=self._obj_class_pred,
            geometry=Rectangle(13, 13, 15, 15),
            tags=TagCollection([Tag(meta=self._confidence_tag_meta, value=0.1)]))

        # More false positives.
        self._pred_objs_fp = [
            Label(obj_class=self._obj_class_pred,
                  geometry=Rectangle(20, 20, 30, 30),
                  tags=TagCollection([Tag(meta=self._confidence_tag_meta, value=v / 100)]))
            for v in range(15, 85, 10)]

        self._metric_calculator = MAPMetric(class_mapping={'a': 'b'}, iou_threshold=0.5)
Esempio n. 10
0
 def delete_tags_by_name(self, tag_names):
     '''
     The function delete_tags_by_name removes tags by their names from current Annotation object and return the copy
     of the current  Annotation object
     :param tag_names: list of the tag names to be delete
     :return: Annotation class object with the new list of the tags
     '''
     retained_tags = [
         tag for tag in self._img_tags.items()
         if tag.meta.name not in tag_names
     ]
     return self.clone(img_tags=TagCollection(items=retained_tags))
Esempio n. 11
0
 def __init__(self, geometry: Geometry, obj_class: ObjClass, tags: TagCollection = None, description: str = ""):
     '''
     :param geometry: Geometry class of the object(point, rectangle, polygon, bitmap, line)
     :param obj_class: Class of objects (person, car, etc) with necessary properties: name, type of geometry (Polygon, Rectangle, ...)
                and RGB color. Only one class can be associated with Label.
     :param tags: TagCollection object
     :param description: description of the label
     '''
     self._geometry = geometry
     self._obj_class = obj_class
     self._tags = take_with_default(tags, TagCollection())
     self._description = description
     self._validate_geometry_type()
     self._validate_geometry()
Esempio n. 12
0
    def from_json(cls, data, project_meta):
        '''
        The function from_json convert annotation from json format to Annotation class object. If one of the labels
        of annotation in json format cannot be convert to Label class object it generate exception error.
        :param data: input annotation in json format
        :param project_meta: ProjectMeta class object
        :return: Annotation class object
        '''
        img_size_dict = data[AnnotationJsonFields.IMG_SIZE]
        img_height = img_size_dict[AnnotationJsonFields.IMG_SIZE_HEIGHT]
        img_width = img_size_dict[AnnotationJsonFields.IMG_SIZE_WIDTH]
        img_size = (img_height, img_width)
        try:
            labels = [
                Label.from_json(label_json, project_meta)
                for label_json in data[AnnotationJsonFields.LABELS]
            ]
        except Exception:
            logger.fatal(
                'Failed to deserialize annotation from JSON format. One of the Label objects could not be '
                'deserialized')
            raise

        custom_data = data.get(AnnotationJsonFields.CUSTOM_DATA, {})
        prob_labels = None
        if AnnotationJsonFields.PROBABILITY_LABELS in custom_data and \
                AnnotationJsonFields.PROBABILITY_CLASSES in custom_data:

            prob_classes = ObjClassCollection.from_json(
                custom_data[AnnotationJsonFields.PROBABILITY_CLASSES])

            # @TODO: tony, maybe link with project meta (add probability classes???)
            prob_project_meta = ProjectMeta(obj_classes=prob_classes)
            prob_labels = [
                Label.from_json(label_json, prob_project_meta) for label_json
                in custom_data[AnnotationJsonFields.PROBABILITY_LABELS]
            ]

            custom_data.pop(AnnotationJsonFields.PROBABILITY_CLASSES)
            custom_data.pop(AnnotationJsonFields.PROBABILITY_LABELS)

        return cls(img_size=img_size,
                   labels=labels,
                   img_tags=TagCollection.from_json(
                       data[AnnotationJsonFields.IMG_TAGS],
                       project_meta.tag_metas),
                   img_description=data.get(
                       AnnotationJsonFields.IMG_DESCRIPTION, ""),
                   pixelwise_scores_labels=prob_labels,
                   custom_data=custom_data)
Esempio n. 13
0
 def from_json(cls, data, project_meta):
     img_size_dict = data[AnnotationJsonFields.IMG_SIZE]
     img_height = img_size_dict[AnnotationJsonFields.IMG_SIZE_HEIGHT]
     img_width = img_size_dict[AnnotationJsonFields.IMG_SIZE_WIDTH]
     img_size = (img_height, img_width)
     try:
         labels = [Label.from_json(label_json, project_meta) for label_json in data[AnnotationJsonFields.LABELS]]
     except Exception:
         logger.fatal('Failed to deserialize annotation from JSON format. One of the Label objects could not be '
                      'deserialized')
         raise
     return cls(img_size=img_size,
                labels=labels,
                img_tags=TagCollection.from_json(data[AnnotationJsonFields.IMG_TAGS], project_meta.tag_metas),
                img_description=data.get(AnnotationJsonFields.IMG_DESCRIPTION, ""))
Esempio n. 14
0
    def from_json(cls, data, project_meta: ProjectMeta):
        obj_class_name = data[LabelJsonFields.OBJ_CLASS_NAME]
        obj_class = project_meta.get_obj_class(obj_class_name)
        if obj_class is None:
            raise RuntimeError(
                f'Failed to deserialize a Label object from JSON: label class name {obj_class_name!r} '
                f'was not found in the given project meta.')

        if obj_class.geometry_type is AnyGeometry:
            geometry_type_actual = GET_GEOMETRY_FROM_STR(
                data[GEOMETRY_TYPE] if GEOMETRY_TYPE in
                data else data[GEOMETRY_SHAPE])
            geometry = geometry_type_actual.from_json(data)
        else:
            geometry = obj_class.geometry_type.from_json(data)

        return cls(geometry=geometry,
                   obj_class=obj_class,
                   tags=TagCollection.from_json(data[LabelJsonFields.TAGS],
                                                project_meta.tag_metas),
                   description=data.get(LabelJsonFields.DESCRIPTION, ""))
Esempio n. 15
0
 def delete_tags_by_name(self, tag_names):
     retained_tags = [
         tag for tag in self._img_tags.items()
         if tag.meta.name not in tag_names
     ]
     return self.clone(img_tags=TagCollection(items=retained_tags))