예제 #1
0
    def post(self):
        print("hannahhh du geiles stück")
        """ Creates an annotation """
        args = create_annotation.parse_args()
        image_id = args.get('image_id')
        category_id = args.get('category_id')
        isbbox = args.get('isbbox')
        metadata = args.get('metadata', {})
        segmentation = args.get('segmentation', [])
        keypoints = args.get('keypoints', [])

        image = current_user.images.filter(id=image_id, deleted=False).first()
        if image is None:
            return {"message": "Invalid image id"}, 400

        logger.info(
            f'{current_user.username} has created an annotation for image {image_id} with {isbbox}'
        )
        logger.info(
            f'{current_user.username} has created an annotation for image {image_id}'
        )

        try:
            annotation = AnnotationModel(image_id=image_id,
                                         category_id=category_id,
                                         metadata=metadata,
                                         segmentation=segmentation,
                                         keypoints=keypoints,
                                         isbbox=isbbox)
            annotation.save()
            annotation.mask()
        except (ValueError, TypeError) as e:
            return {'message': str(e)}, 400

        return query_util.fix_ids(annotation)
예제 #2
0
 def create_annotation(image_id):
     annotation = AnnotationModel(
         image_id=image_id,
         category_id=0,
         metadata={},
         segmentation=[[1, 2, 3, 4]],
         keypoints=[1, 2, 3, 4],
         isbbox=False
     )
     annotation.save()
     return annotation.id
예제 #3
0
    def post(self):
        """ Creates an annotation """
        args = create_annotation.parse_args()
        image_id = args.get('image_id')
        category_id = args.get('category_id')
        isbbox = args.get('isbbox')
        metadata = args.get('metadata', {})
        segmentation = args.get('segmentation', [])
        bbox = args.get('bbox', [])
        keypoints = args.get('keypoints', [])

        # change appro after setting login_user
        # image = current_user.images.filter(id=image_id, deleted=False).first()
        # image = ImageModel.objects.get(id=image_id, deleted=False).first()
        image = ImageModel.objects(id=image_id).first()
        if image is None:
            return {"message": "Invalid image id"}, 400

        logger.info(
            f'{current_user.username} has created an annotation for image {image_id} with {isbbox}'
        )
        logger.info(
            f'{current_user.username} has created an annotation for image {image_id}'
        )

        # add condition if user is not authenticed or dataset is public
        try:
            annotation = AnnotationModel(image_id=image_id,
                                         category_id=category_id,
                                         metadata=metadata,
                                         segmentation=segmentation,
                                         bbox=bbox,
                                         keypoints=keypoints,
                                         isbbox=isbbox)
            annotation.save()
        except (ValueError, TypeError) as e:
            return {'message': str(e)}, 400

        return query_util.fix_ids(annotation)
    def create_annotation_from(self, annotation_data, image_id):
        collect_annotation_data = {}
        self.append_events_ant_times_to(collect_annotation_data,
                                        annotation_data)
        self.append_papers_object_to(collect_annotation_data, annotation_data,
                                     image_id)
        self.append_other_data_to(collect_annotation_data, annotation_data,
                                  image_id)

        return AnnotationModel(
            image_id=image_id,
            category_id=annotation_data['category_id'],
            events=collect_annotation_data['events'],
            milliseconds=collect_annotation_data['milliseconds'],
            paper_object=collect_annotation_data['paper_object'],
            segmentation=collect_annotation_data['segmentation'],
            area=collect_annotation_data['area'],
            bbox=collect_annotation_data['bbox'],
            isbbox=collect_annotation_data['isbbox'],
            keypoints=collect_annotation_data['keypoints'],
            metadata=collect_annotation_data['metadata'],
            color=collect_annotation_data['color'],
        )
예제 #5
0
    def post(self):
        """ Creates an annotation """
        args = create_annotation.parse_args()
        image_id = args.get('image_id')
        category_id = args.get('category_id')
        metadata = args.get('metadata', {})
        segmentation = args.get('segmentation', [])
        keypoints = args.get('keypoints', [])

        logger.info(
            f'{current_user.username} has created an annotation for image {image_id}'
        )

        try:
            annotation = AnnotationModel(image_id=image_id,
                                         category_id=category_id,
                                         metadata=metadata,
                                         segmentation=segmentation,
                                         keypoints=keypoints)
            annotation.save()
        except (ValueError, TypeError) as e:
            return {'message': str(e)}, 400

        return query_util.fix_ids(annotation)
예제 #6
0
def import_annotations(task_id, dataset_id, coco_json):

    task = TaskModel.objects.get(id=task_id)
    dataset = DatasetModel.objects.get(id=dataset_id)

    task.update(status="PROGRESS")
    socket = create_socket()

    task.info("Beginning Import")

    images = ImageModel.objects(dataset_id=dataset.id)
    categories = CategoryModel.objects

    coco_images = coco_json.get('images', [])
    coco_annotations = coco_json.get('annotations', [])
    coco_categories = coco_json.get('categories', [])

    task.info(f"Importing {len(coco_categories)} categories, "
              f"{len(coco_images)} images, and "
              f"{len(coco_annotations)} annotations")

    total_items = sum(
        [len(coco_categories),
         len(coco_annotations),
         len(coco_images)])
    progress = 0

    task.info("===== Importing Categories =====")
    # category id mapping  ( file : database )
    categories_id = {}

    # Create any missing categories
    for category in coco_categories:

        category_name = category.get('name')
        category_id = category.get('id')
        category_model = categories.filter(name__iexact=category_name).first()

        if category_model is None:
            task.warning(
                f"{category_name} category not found (creating a new one)")

            new_category = CategoryModel(
                name=category_name,
                keypoint_edges=category.get('skeleton', []),
                keypoint_labels=category.get('keypoints', []))
            new_category.save()

            category_model = new_category
            dataset.categories.append(new_category.id)

        task.info(f"{category_name} category found")
        # map category ids
        categories_id[category_id] = category_model.id

        # update progress
        progress += 1
        task.set_progress((progress / total_items) * 100, socket=socket)

    dataset.update(set__categories=dataset.categories)

    task.info("===== Loading Images =====")
    # image id mapping ( file: database )
    images_id = {}
    categories_by_image = {}

    # Find all images
    for image in coco_images:
        image_id = image.get('id')
        image_filename = image.get('file_name')

        # update progress
        progress += 1
        task.set_progress((progress / total_items) * 100, socket=socket)

        image_model = images.filter(file_name__exact=image_filename).all()

        if len(image_model) == 0:
            task.warning(f"Could not find image {image_filename}")
            continue

        if len(image_model) > 1:
            task.error(
                f"Too many images found with the same file name: {image_filename}"
            )
            continue

        task.info(f"Image {image_filename} found")
        image_model = image_model[0]
        images_id[image_id] = image_model
        categories_by_image[image_id] = list()

    task.info("===== Import Annotations =====")
    for annotation in coco_annotations:

        image_id = annotation.get('image_id')
        category_id = annotation.get('category_id')
        segmentation = annotation.get('segmentation', [])
        keypoints = annotation.get('keypoints', [])
        # is_crowd = annotation.get('iscrowed', False)
        area = annotation.get('area', 0)
        bbox = annotation.get('bbox', [0, 0, 0, 0])
        isbbox = annotation.get('isbbox', False)

        progress += 1
        task.set_progress((progress / total_items) * 100, socket=socket)

        has_segmentation = len(segmentation) > 0
        has_keypoints = len(keypoints) > 0
        if not has_segmentation and not has_keypoints:
            task.warning(
                f"Annotation {annotation.get('id')} has no segmentation or keypoints"
            )
            continue

        try:
            image_model = images_id[image_id]
            category_model_id = categories_id[category_id]
            image_categories = categories_by_image[image_id]
        except KeyError:
            task.warning(
                f"Could not find image assoicated with annotation {annotation.get('id')}"
            )
            continue

        annotation_model = AnnotationModel.objects(
            image_id=image_model.id,
            category_id=category_model_id,
            segmentation=segmentation,
            keypoints=keypoints).first()

        if annotation_model is None:
            task.info(f"Creating annotation data ({image_id}, {category_id})")

            annotation_model = AnnotationModel(image_id=image_model.id)
            annotation_model.category_id = category_model_id

            annotation_model.color = annotation.get('color')
            annotation_model.metadata = annotation.get('metadata', {})

            if has_segmentation:
                annotation_model.segmentation = segmentation
                annotation_model.area = area
                annotation_model.bbox = bbox

            if has_keypoints:
                annotation_model.keypoints = keypoints

            annotation_model.isbbox = isbbox
            annotation_model.save()

            image_categories.append(category_id)
        else:
            annotation_model.update(deleted=False, isbbox=isbbox)
            task.info(
                f"Annotation already exists (i:{image_id}, c:{category_id})")

    for image_id in images_id:
        image_model = images_id[image_id]
        category_ids = categories_by_image[image_id]
        all_category_ids = list(image_model.category_ids)
        all_category_ids += category_ids

        num_annotations = AnnotationModel.objects(
            Q(image_id=image_id) & Q(deleted=False)
            & (Q(area__gt=0) | Q(keypoints__size__gt=0))).count()

        image_model.update(set__annotated=True,
                           set__category_ids=list(set(all_category_ids)),
                           set__num_annotations=num_annotations)

    task.set_progress(100, socket=socket)