Beispiel #1
0
def test_anno_image_input_octet_stream_bad_json_filename(img_file, json_file):
    test_anno_image_input = AnnotatedImageInput()
    request = mock.MagicMock(spec=flask.Request)
    image_file_bytes = open(str(img_file), 'rb').read()
    image_file_attr = {
        'filename': 'test_img.png',
        'read.return_value': image_file_bytes,
        'mimetype': 'application/octet-stream',
        'stream': io.BytesIO(image_file_bytes),
    }
    image_file = mock.Mock(**image_file_attr)

    json_file_bytes = open(str(json_file), 'rb').read()
    json_file_attr = {
        'filename': 'annotations.jso',
        'read.return_value': json_file_bytes,
        'mimetype': 'application/octet-stream',
        'stream': io.BytesIO(json_file_bytes),
    }
    json_file = mock.Mock(**json_file_attr)

    request.method = "POST"
    request.files = {"image_file": image_file, "json_file": json_file}

    request.get_data.return_value = None

    with pytest.raises(BadInput) as e:
        test_anno_image_input.handle_request(request, predict_image_and_json)

    assert "unexpected file" in str(e.value)
Beispiel #2
0
def test_anno_image_input_http_request_multipart_octet_streams(
        img_file, json_file):
    test_anno_image_input = AnnotatedImageInput()
    request = mock.MagicMock(spec=flask.Request)
    image_file_bytes = open(str(img_file), 'rb').read()
    image_file_attr = {
        'filename': 'test_img.png',
        'read.return_value': image_file_bytes,
        'mimetype': 'application/octet-stream',
        'stream': io.BytesIO(image_file_bytes),
    }
    image_file = mock.Mock(**image_file_attr)

    json_file_bytes = open(str(json_file), 'rb').read()
    json_file_attr = {
        'filename': 'annotations.json',
        'read.return_value': json_file_bytes,
        'mimetype': 'application/octet-stream',
        'stream': io.BytesIO(json_file_bytes),
    }
    json_file = mock.Mock(**json_file_attr)

    request.method = "POST"
    request.files = {"image_file": image_file, "json_file": json_file}

    request.get_data.return_value = None

    response = test_anno_image_input.handle_request(request,
                                                    predict_image_and_json)

    assert response.status_code == 200
    assert '[10, 10, 3], "kaith"' in str(response.response)
Beispiel #3
0
def test_anno_image_input_cli_image_and_json(capsys, img_file, json_file):
    test_anno_image_input = AnnotatedImageInput()

    test_args = ["--image", img_file, "--annotations", json_file]
    test_anno_image_input.handle_cli(test_args, predict_image_and_json)
    out, _ = capsys.readouterr()

    assert out.strip() == "((10, 10, 3), 'kaith')"
Beispiel #4
0
def test_anno_image_input_cli_image_only(capsys, img_file):
    test_anno_image_input = AnnotatedImageInput()

    test_args = ["--image", img_file]
    test_anno_image_input.handle_cli(test_args, predict_image_only)
    out, _ = capsys.readouterr()

    assert out.strip() == "(10, 10, 3)"
Beispiel #5
0
def test_anno_image_input_aws_lambda_event(img_file, json_file):
    test_anno_image_input = AnnotatedImageInput()

    multipart_data, headers = generate_multipart_body(img_file, json_file)

    aws_lambda_event = {"body": multipart_data, "headers": headers}
    aws_result = test_anno_image_input.handle_aws_lambda_event(
        aws_lambda_event, predict_image_and_json)

    assert aws_result["statusCode"] == 200
    assert aws_result["body"] == '[[10, 10, 3], "kaith"]'
Beispiel #6
0
def test_anno_image_input_http_request_malformatted_input_wrong_input_name():
    test_anno_image_input = AnnotatedImageInput()
    request = mock.MagicMock(spec=flask.Request)

    request.method = "POST"
    request.files = {"abc": None}
    request.headers = {}
    request.get_data.return_value = None

    with pytest.raises(BadInput) as e:
        test_anno_image_input.handle_request(request, predict_image_only)

    assert "unexpected HTTP request format" in str(e.value)
Beispiel #7
0
def test_anno_image_input_aws_lambda_event_bad_content_type(
        img_file, json_file):
    test_anno_image_input = AnnotatedImageInput()

    multipart_data, headers = generate_multipart_body(img_file, json_file)
    headers['Content-Type'] = 'image/jpeg'

    aws_lambda_event = {"body": multipart_data, "headers": headers}

    with pytest.raises(BadInput) as e:
        test_anno_image_input.handle_aws_lambda_event(aws_lambda_event,
                                                      predict_image_and_json)

    assert "only supports multipart/form-data" in str(e.value)
Beispiel #8
0
def test_anno_image_input_batch_request_image_only(img_file):
    adapter = AnnotatedImageInput(is_batch_input=True)

    multipart_data, headers = generate_multipart_body(img_file)
    request = SimpleRequest.from_flask_request(
        Request.from_values(
            data=multipart_data,
            content_type=headers['Content-Type'],
            content_length=headers['Content-Length'],
        ))

    responses = adapter.handle_batch_request([request] * 5, predict_image_only)
    for response in responses:
        assert response.status == 200
        assert response.data == '[10, 10, 3]'
Beispiel #9
0
def test_anno_image_input_extract_args_custom_extension(
        input_adapter, img_file, json_file):
    img_io = io.BytesIO(read_bin(img_file))
    img_io.name = "test.custom"
    json_io = io.BytesIO(read_bin(json_file))
    task = InferenceTask(data=(img_io, json_io))

    args = input_adapter.extract_user_func_args([task])
    for _ in zip(*args):
        pass
    assert task.is_discarded

    input_adapter = AnnotatedImageInput(accept_image_formats=["custom"])
    task = InferenceTask(data=(img_io, json_io))
    args = input_adapter.extract_user_func_args([task])
    for img, json_obj in zip(*args):
        assert img.shape == (10, 10, 3)
        assert json_obj['name'] == "kaith"
Beispiel #10
0
def test_anno_image_input_http_request_only_json_file(json_file):
    test_anno_image_input = AnnotatedImageInput()
    request = mock.MagicMock(spec=flask.Request)
    json_file_bytes = open(str(json_file), 'rb').read()
    json_file_attr = {
        'filename': 'annotations.json',
        'read.return_value': json_file_bytes,
        'mimetype': 'application/json',
        'stream': io.BytesIO(json_file_bytes),
    }
    json_file = mock.Mock(**json_file_attr)

    request.method = "POST"
    request.files = {"json_file": json_file}
    request.headers = {}
    request.get_data.return_value = None

    with pytest.raises(BadInput) as e:
        test_anno_image_input.handle_request(request, predict_image_only)

    assert "requires an image file" in str(e.value)
Beispiel #11
0
def test_anno_image_input_http_request_two_image_files(img_file):
    test_anno_image_input = AnnotatedImageInput()
    request = mock.MagicMock(spec=flask.Request)
    image_file_bytes = open(str(img_file), 'rb').read()
    image_file_attr = {
        'filename': 'test_img.png',
        'read.return_value': image_file_bytes,
        'mimetype': 'image/png',
        'stream': io.BytesIO(image_file_bytes),
    }
    image_file = mock.Mock(**image_file_attr)

    request.method = "POST"
    request.files = {"image_file": image_file, "image_file_2": image_file}
    request.headers = {}
    request.get_data.return_value = None

    with pytest.raises(BadInput) as e:
        test_anno_image_input.handle_request(request, predict_image_only)

    assert "received two images" in str(e.value)
Beispiel #12
0
def test_anno_image_input_http_request_single_image_different_name(img_file):
    test_anno_image_input = AnnotatedImageInput()
    request = mock.MagicMock(spec=flask.Request)
    file_bytes = open(str(img_file), 'rb').read()
    file_attr = {
        'filename': 'test_img.png',
        'read.return_value': file_bytes,
        'mimetype': 'image/png',
        'stream': io.BytesIO(file_bytes),
    }
    file = mock.Mock(**file_attr)

    request.method = "POST"
    request.files = {"a_different_name_used": file}
    request.headers = {}
    request.get_data.return_value = None

    response = test_anno_image_input.handle_request(request,
                                                    predict_image_only)

    assert response.status_code == 200
    assert "[10, 10, 3]" in str(response.response)
Beispiel #13
0
def test_anno_image_input_batch_request_skip_bad(img_file, json_file):
    adapter = AnnotatedImageInput(is_batch_input=True)

    multipart_data, headers = generate_multipart_body(img_file, json_file)

    empty_request = SimpleRequest(headers=headers, data=None)

    request = SimpleRequest.from_flask_request(
        Request.from_values(
            data=multipart_data,
            content_type=headers['Content-Type'],
            content_length=headers['Content-Length'],
        ))

    image = ("image.jpg", open(img_file, "rb").read())
    json = ("annotations.jso", open(json_file, "rb").read())
    files = {"image.invalid": image, "annotations.invalid": json}
    bad_data, content_type = encode_multipart_formdata(files)

    bad_request = SimpleRequest.from_flask_request(
        Request.from_values(
            data=bad_data,
            content_type=content_type,
            content_length=len(bad_data),
        ))

    responses = adapter.handle_batch_request(
        [empty_request, request, bad_request], predict_image_and_json)

    assert len(responses) == 3
    assert responses[0] is None
    assert responses[1].status == 200 and responses[
        1].data == '[[10, 10, 3], "kaith"]'
    assert responses[2] is None

    bad_responses = adapter.handle_batch_request([empty_request],
                                                 predict_image_and_json)
    assert len(bad_responses) == 1
    assert bad_responses[0] is None
Beispiel #14
0
def test_anno_image_input_custom_accept_extension_not_accepted(img_file):
    test_anno_image_input = AnnotatedImageInput(
        accept_image_formats=[".custom"])
    request = mock.MagicMock(spec=flask.Request)
    file_bytes = open(str(img_file), 'rb').read()
    file_attr = {
        'filename': 'test_img.jpg',
        'read.return_value': file_bytes,
        'mimetype': 'image/jpg',
        'stream': io.BytesIO(file_bytes),
    }
    file = mock.Mock(**file_attr)

    request.method = "POST"
    request.files = {"test_img": file}
    request.headers = {}
    request.get_data.return_value = None

    with pytest.raises(BadInput) as e:
        test_anno_image_input.handle_request(request, predict_image_only)

    assert "Input file not in supported format list" in str(e.value)
Beispiel #15
0
def test_anno_image_input_octet_stream_json(img_file):
    test_anno_image_input = AnnotatedImageInput(
        accept_image_formats=[".custom"])
    request = mock.MagicMock(spec=flask.Request)
    file_bytes = open(str(img_file), 'rb').read()
    file_attr = {
        'filename': 'test_img.custom',
        'read.return_value': file_bytes,
        'mimetype': 'application/octet-stream',
        'stream': io.BytesIO(file_bytes),
    }
    file = mock.Mock(**file_attr)

    request.method = "POST"
    request.files = {"a_different_name_used": file}
    request.headers = {}
    request.get_data.return_value = None

    response = test_anno_image_input.handle_request(request,
                                                    predict_image_only)

    assert response.status_code == 200
    assert "[10, 10, 3]" in str(response.response)
Beispiel #16
0
def test_anno_image_input_cli_relative_paths(capsys, img_file, json_file):
    test_anno_image_input = AnnotatedImageInput()

    try:
        # This fails on Windows if our file is on a different drive
        relative_image_path = os.path.relpath(img_file)
        relative_annotation_path = os.path.relpath(json_file)
    except ValueError:
        # Switch to the drive with the files image on it, and try again
        os.chdir(img_file[:2])
        relative_image_path = os.path.relpath(img_file)
        relative_annotation_path = os.path.relpath(json_file)

    test_args = [
        "--image",
        relative_image_path,
        "--annotations",
        relative_annotation_path,
    ]
    test_anno_image_input.handle_cli(test_args, predict_image_and_json)
    out, _ = capsys.readouterr()

    assert out.strip() == "((10, 10, 3), 'kaith')"
Beispiel #17
0
def input_adapter():
    return AnnotatedImageInput()
Beispiel #18
0
def test_anno_image_input_check_config():
    adapter = AnnotatedImageInput()
    config = adapter.config
    assert isinstance(config["accept_image_formats"], list) and isinstance(
        config["pilmode"], str)
Beispiel #19
0
def test_anno_image_input_check_request_schema():
    adapter = AnnotatedImageInput()
    assert isinstance(adapter.request_schema, dict)
Beispiel #20
0
def test_anno_image_input_check_pip_deps():
    adapter = AnnotatedImageInput()
    assert isinstance(adapter.pip_dependencies, list)