def upload_image(project_id): """ Upload image to a project Args: project_id: The id of the project """ project = maybe_get_project(project_id) if 'image' in request.files: file = request.files['image'] project = project image = Image(project_id=project.id) try: image.save_image_to_project(file) except IOError: abort(400, 'Unable to save image') project.images.append(image) try: db.session.add(image) db.session.commit() except IntegrityError: db.session.rollback() abort(400, 'Failed to add image') return construct_msg('Image added successfully'), 200 else: abort(400, 'Missing \'image\' in request')
def test_invalid_project(): app = create_app(TEST_MODE) with app.app_context(): db.reflect() db.drop_all() db.create_all() db.create_all() with open('tests/cat.jpg', 'rb') as fd: img = FileStorage(fd) image = Image(project_id=0) try: image.save_image_to_project(img) assert False except HTTPException as exception: assert 400 == exception.code
def upload_zip(project_id): """ Upload zip file of images to project Args: project_id -- id of project to upload zip file to Returns: HTTP status code and message of zip file upload """ def filter_condition(name): split = name.lower().split('.') return split and split[-1] in VALID_IMG_EXTENSIONS project = maybe_get_project(project_id) project_dir = get_project_dir(project) if 'zip' in request.files: file = request.files['zip'] extension = parse_and_validate_file_extension(file.filename, {'zip'}) new_file_name = '%s.%s' % (str(uuid.uuid4()), extension) zip_path = os.path.join(project_dir, new_file_name) file.save(zip_path) zip_file = ZipFile(zip_path) image_names = zip_file.namelist() name_map = {} for image_name in filter(filter_condition, image_names): if image_name in name_map: continue image = Image(project_id=project.id) image.save_empty_image(image_name) project.images.append(image) try: db.session.add(image) db.session.commit() except IntegrityError: db.session.rollback() abort(400, 'Failed to add image') name_map[image_name] = image.image_storage_path zip_file.close() multiprocessing.Process(target=unzip_process, args=(zip_path, name_map)).start() return ( construct_msg('Zip uploaded successfully, please wait for unzip'), 200) else: abort(400, 'Missing \'zip\' in request')
def test_image_and_project(client): image = Image() project = Project() project.images.append(image) assert len(project.images) == 1 assert image == project.images[0] assert project == image.project
def test_annotation_and_image(client): annotation = Annotation() image = Image() image.annotations.append(annotation) assert len(image.annotations) == 1 assert annotation == image.annotations[0] assert image == annotation.original_image annotation1 = Annotation() image.annotations.append(annotation1) assert len(image.annotations) == 2 assert annotation1 == image.annotations[1] assert image == annotation1.original_image
def test_image_repr(client): image = Image(image_name='cracked_building.png') assert str(image) == '<Image: %r>' % 'cracked_building.png'
def local_client(): if os.path.isdir(Config.STATIC_STORAGE_DIR): shutil.rmtree(Config.STATIC_STORAGE_DIR) app = create_app(TEST_MODE) with app.app_context(): db.reflect() db.drop_all() db.create_all() rw_permission = ProjectPermission( access_type=AccessTypeEnum.READ_WRITE) r_permission = ProjectPermission(access_type=AccessTypeEnum.READ_ONLY) admin = User(username='******', email='email@test_load.com', privileges=PrivilegesEnum.ADMIN) admin.set_password('TestTest1') admin.project_permissions.append(rw_permission) db.session.add(admin) db.session.commit() annotator = User(username='******', email='email2@test_load.com', privileges=PrivilegesEnum.ANNOTATOR) annotator.set_password('TestTest2') annotator.project_permissions.append(r_permission) db.session.add(annotator) db.session.commit() annotator_2 = User(username='******', email='email3@test_load.com') annotator_2.set_password('TestTest3') db.session.add(annotator_2) db.session.commit() project = Project(project_name='TestProject', created_by=admin.id) db.session.add(project) db.session.commit() project.permissions.append(rw_permission) project.permissions.append(r_permission) test_image = Image(image_name='cat.jpg', image_url='test_dir/test_dir_2/cat.jpg', project_id=project.id) db.session.add(test_image) db.session.commit() test_image_2 = Image(image_name='dog.png', image_url='test_dir/test_dir_2/dog.png', project_id=project.id) db.session.add(test_image_2) db.session.commit() test_image_3 = Image(image_name='cat.jpg', image_url='tests/cat.jpg', image_storage_path='tests/cat.jpg', project_id=project.id) db.session.add(test_image_3) db.session.commit() client = app.test_client() yield client
def get_project_fixture(): app = create_app(TEST_MODE) with app.app_context(): db.reflect() db.drop_all() db.create_all() user = User(username=ADMIN_USERNAME, email='success@test_project.com', privileges=PrivilegesEnum.ADMIN) user.set_password(ADMIN_PWD) db.session.add(user) db.session.commit() project = Project(project_name='test1', created_by=user.id) db.session.add(project) db.session.commit() image1 = Image(project_id=1, image_name='cracked_building.png') db.session.add(image1) db.session.commit() label1 = Label(label_name='asdf1', project_id=project.id) db.session.add(label1) label2 = Label(label_name='asdf2', project_id=project.id) db.session.add(label2) db.session.commit() annotation1 = Annotation(image_id=1, project_id=1, creator_id=1, label_id=1, data=b'1234', vector=b'1234') db.session.add(annotation1) annotation2 = Annotation(image_id=1, project_id=1, creator_id=1, label_id=2, data=b'1234', vector=b'1234') db.session.add(annotation2) db.session.commit() # Create project directory if not os.path.isdir('static-dir/1'): os.makedirs('static-dir/1') permission = ProjectPermission(access_type=AccessTypeEnum.READ_WRITE) user.project_permissions.append(permission) project.permissions.append(permission) user = User(username=ANNOTATOR_USERNAME, email='no_privilege@test_project.com', privileges=PrivilegesEnum.ANNOTATOR) user.set_password(ANNOTATOR_PWD) db.session.add(user) db.session.commit() permission = ProjectPermission(access_type=AccessTypeEnum.READ_ONLY) user.project_permissions.append(permission) project.permissions.append(permission) db.session.commit() client = app.test_client() yield client