Beispiel #1
0
    def list(self, request, project_id):
        username = request.user
        user_id = AccountManager.get_id_by_username(username)
        dataset_manager = DatasetManager()
        if not Permission.hasPermission(user_id, 'list_dataset', project_id):
            raise PermissionDenied
        per_page = int(request.GET.get(key="per_page", default=PER_PAGE))
        page = int(request.GET.get(key="page", default=1))
        sort_key = request.GET.get(key="sort_key", default=SORT_KEY)
        reverse_flag = request.GET.get(key="reverse_flag", default="false")
        is_reverse = (reverse_flag == "true")
        search_keyword = request.GET.get(key="search", default="")

        contents = dataset_manager.get_datasets(project_id, user_id, sort_key,
                                                is_reverse, per_page, page,
                                                search_keyword)
        return HttpResponse(content=json.dumps(contents),
                            status=200,
                            content_type='application/json')
Beispiel #2
0
def file_upload(request, project_id, original_id):
    username = request.user
    user_id = AccountManager.get_id_by_username(username)
    if not Permission.hasPermission(user_id, 'create_original', project_id):
        raise PermissionDenied
    try:
        file = request.FILES['file']
        original_manager = OriginalManager()
        data = original_manager.save_file(project_id, original_id, file)
        response = JSONResponse(data, mimetype=response_mimetype(request))
        response['Content-Disposition'] = 'inline; filename=files.json'
        return response

    except Exception:  # FIXME
        ServiceLog.error(traceback.format_exc())
        data = json.dumps({'status': 'NG'})
        return HttpResponse(content=data,
                            status=400,
                            content_type='application/json')
Beispiel #3
0
    def create(self, request, project_id):
        username = request.user
        user_id = AccountManager.get_id_by_username(username)
        if not Permission.hasPermission(user_id, 'create_calibration',
                                        project_id):
            raise PermissionDenied
        file = request.FILES['file']
        if int(request.META['CONTENT_LENGTH']) > 2048:
            raise ValidationError()
        file_path = '/tmp/' + file.name

        # write
        with open(file_path, 'ab') as destination:
            for chunk in file.chunks():
                destination.write(chunk)

        # validation
        try:
            camera_extrinsic_mat, camera_mat, dist_coeff = parse_calib(
                file_path)
        except Exception:
            raise ValidationError()
        serializer = CalibrationSerializer(
            data={
                'project':
                int(project_id),
                'name':
                file.name,
                'content':
                json.dumps({
                    'camera_extrinsic_mat': camera_extrinsic_mat,
                    'camera_mat': camera_mat,
                    'dist_coeff': dist_coeff
                }),
            })
        if not serializer.is_valid():
            raise ValidationError()
        serializer.save()
        contents = {}
        os.remove(file_path)
        response = JSONResponse(contents, mimetype=response_mimetype(request))
        response['Content-Disposition'] = 'inline; filename=files.json'
        return response
Beispiel #4
0
def download_archived_link(request, project_id, annotation_id):
    username = request.user
    user_id = AccountManager.get_id_by_username(username)
    annotation_manager = AnnotationManager()
    dataset_id = annotation_manager.get_annotation(annotation_id)['dataset_id']
    original_id = DatasetManager().get_dataset(user_id,
                                               dataset_id)['original_id']
    storage_id = OriginalManager().get_original(project_id,
                                                original_id)['storage_id']
    storage = StorageSerializer().get_storage(project_id, storage_id)
    if storage['storage_type'] == 'LOCAL_NFS':
        content = request.build_absolute_uri(request.path) + 'local/'
    elif storage['storage_type'] == 'AWS_S3':
        archive_path = annotation_manager.get_archive_path(annotation_id)
        content = AwsS3Client().get_s3_down_url(
            storage['storage_config']['bucket'], archive_path)
    else:
        raise UnknownStorageTypeError
    return HttpResponse(status=200, content=content, content_type='text/plain')
Beispiel #5
0
    def create(self, request, project_id):
        username = request.user
        user_id = AccountManager.get_id_by_username(username)
        dataset_manager = DatasetManager()
        if not Permission.hasPermission(user_id, 'create_dataset', project_id):
            raise PermissionDenied
        name = request.data.get('name')
        file_path = request.data.get('file_path')
        frame_count = int(request.data.get('frame_count'))
        original_id = request.data.get('original_id', None)
        candidates = request.data.get('candidates')
        dataset_id = dataset_manager.create_dataset(name, file_path,
                                                    frame_count, original_id,
                                                    project_id, candidates)

        contents = dataset_manager.get_dataset(user_id, dataset_id)
        return HttpResponse(status=201,
                            content=contents,
                            content_type='application/json')
Beispiel #6
0
def frame(request, project_id, annotation_id, frame):
    username = request.user
    user_id = AccountManager.get_id_by_username(username)
    annotation_manager = AnnotationManager()
    if request.method == 'GET':
        if not Permission.hasPermission(user_id, 'get_label', project_id):
            raise PermissionDenied
        labels = annotation_manager.get_frame_labels(
            project_id, annotation_id, frame)
        return HttpResponse(content=json.dumps(labels), status=200, content_type='application/json')

    else:
        if not Permission.hasPermission(user_id, 'create_label', project_id):
            raise PermissionDenied
        created = request.data.get('created', "")
        edited = request.data.get('edited', "")
        deleted = request.data.get('deleted', "")
        annotation_manager.set_frame_label(user_id, project_id, annotation_id, frame, created, edited, deleted)
        return HttpResponse(status=201)
Beispiel #7
0
    def create(self, request):
        username = request.user
        user_id = AccountManager.get_id_by_username(username)
        name = request.data.get('name', None)

        # create project
        serializer = ProjectSerializer(
            data={
                'name': name,
                'description': request.data.get('description', None),
                'label_type': request.data.get('label_type', None),
                'owner_id': user_id
            })
        if not serializer.is_valid():
            raise ValidationError
        serializer.save()

        # create klassset
        project_manager = ProjectManager()
        project_id = project_manager.get_project_id_by_name(name)
        klassset_manager = KlasssetManager()
        klassset = request.data.get('klasses')
        klassset_manager.set_klassset(project_id, user_id, klassset)

        # create storage
        storage = StorageSerializer(
            data={
                'storage_type':
                request.data.get('storage_type', None),
                'storage_config':
                json.dumps(request.data.get('storage_config', None)),
                'project':
                project_id
            })
        if not storage.is_valid():
            raise ValidationError
        storage.save()

        return HttpResponse(status=201,
                            content={},
                            content_type='application/json')
Beispiel #8
0
    def create(self, request, project_id):
        username = request.user
        user_id = AccountManager.get_id_by_username(username)
        if not Permission.hasPermission(user_id, 'create_storage', project_id):
            raise PermissionDenied

        serializer = StorageSerializer(
            data={
                'storage_type':
                request.data.get('storage_type', None),
                'storage_config':
                json.dumps(request.data.get('storage_config', None)),
                'project':
                project_id
            })
        if not serializer.is_valid():
            raise ValidationError
        content = serializer.save()
        return HttpResponse(status=201,
                            content=content,
                            content_type='application/json')
Beispiel #9
0
def annotation(request, project_id, annotation_id):
    username = request.user
    user_id = AccountManager.get_id_by_username(username)
    annotation_manager = AnnotationManager()
    if request.method == 'GET':
        if not Permission.hasPermission(user_id, 'get_annotationwork', project_id):
            raise PermissionDenied
        contents = annotation_manager.get_annotation(annotation_id)
        return HttpResponse(content=json.dumps(contents), status=200, content_type='application/json')

    elif request.method == 'POST':
        file_path = request.data.get('file_path')
        file_name = request.data.get('file_name')
        annotation_manager.set_archive(annotation_id, file_path, file_name)
        return HttpResponse(status=201)

    else:
        if not Permission.hasPermission(user_id, 'delete_annotationwork', project_id):
            raise PermissionDenied
        annotation_manager.delete_annotation(annotation_id)
        return HttpResponse(status=204)
Beispiel #10
0
def download_link(request, project_id, dataset_id, candidate_id, frame):
    username = request.user
    user_id = AccountManager.get_id_by_username(username)
    if not Permission.hasPermission(user_id, 'get_annotationwork', project_id):
        raise PermissionDenied

    dataset = DatasetManager().get_dataset(user_id, dataset_id)
    original = OriginalManager().get_original(project_id,
                                              dataset['original_id'])
    storage = StorageSerializer().get_storage(project_id,
                                              original['storage_id'])

    if storage['storage_type'] == 'LOCAL_NFS':
        content = request.build_absolute_uri(request.path) + 'image/'
    elif storage['storage_type'] == 'AWS_S3':
        ext = __get_extension(candidate_id)
        key = (dataset['file_path'] + candidate_id + '_' +
               str(frame).zfill(6) + ext)
        content = AwsS3Client().get_s3_down_url(
            storage['storage_config']['bucket'], key)
    else:
        raise UnknownStorageTypeError
    return HttpResponse(status=200, content=content, content_type='text/plain')
Beispiel #11
0
def annotations(request, project_id):
    username = request.user
    user_id = AccountManager.get_id_by_username(username)
    annotation_manager = AnnotationManager()
    if request.method == 'GET':
        if not Permission.hasPermission(user_id, 'list_annotationwork',
                                        project_id):
            raise PermissionDenied
        per_page = int(request.GET.get(key="per_page", default=PER_PAGE))
        page = int(request.GET.get(key="page", default=1))
        sort_key = request.GET.get(key="sort_key", default=SORT_KEY)
        reverse_flag = request.GET.get(key="reverse_flag", default="false")
        is_reverse = (reverse_flag == "true")
        search_keyword = request.GET.get(key="search", default="")

        contents = annotation_manager.list_annotations(project_id, sort_key,
                                                       is_reverse, per_page,
                                                       page, search_keyword)

        return HttpResponse(content=json.dumps(contents),
                            status=200,
                            content_type='application/json')
    else:
        if not Permission.hasPermission(user_id, 'create_annotationwork',
                                        project_id):
            raise PermissionDenied

        name = request.data.get('name')
        dataset_id = request.data.get('dataset_id')

        annotation_id = annotation_manager.create_annotation(
            user_id, project_id, name, dataset_id)

        contents = annotation_manager.get_annotation(annotation_id)
        return HttpResponse(status=201,
                            content=json.dumps(contents),
                            content_type='application/json')