Пример #1
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)
Пример #2
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, ""))
Пример #3
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, ""))