Ejemplo n.º 1
0
async def crop_uri(
    uri: AnyHttpUrl, width: int = 500, height: int = 500, face_percent: int = 50,
):
    """Returns a cropped form of the document image."""

    # Set up Cropper instance and crop
    c = Cropper(width=width, height=height, face_percent=face_percent)

    # Crop
    r = requests.get(uri, stream=True)
    if r.status_code != 200:
        # TODO
        return None
    file = io.BytesIO(r.content)
    mime, extension = get_mime(file)
    fastapi_logger.info(f"Reading file: {mime}, {extension}")

    img = open_file(file)
    img_array = c.crop(img)
    if img_array is None:
        # TODO
        return {"face detected": None}

    # Convert to bytes
    is_success, img_buffer = cv2.imencode(extension, img_array)
    if not is_success:
        # TODO
        return {"face detected": None}
    byte_im = img_buffer.tobytes()
    return await upload_img_file(img=byte_im, ext=extension, mime=mime)
Ejemplo n.º 2
0
async def crop(
    width: int = 500,
    height: int = 500,
    face_percent: int = 50,
    file: UploadFile = File(...),
):
    """Returns a cropped form of the document image."""
    mime, extension = get_mime(file.file)
    fastapi_logger.info(f"Reading file: {file.filename}, mime: {mime}")
    if "image" not in mime:
        return Response(status_code=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE)

    # Set up Cropper instance and crop
    c = Cropper(width=width, height=height, face_percent=face_percent)

    # Crop
    img = open_file(file.file)
    img_array = c.crop(img)
    if img_array is None:
        return {"success": False, "description": "No face detected", "url": None}

    url = upload_blob(img=img_array, ext=extension, mime=mime)
    return {
        "success": True,
        "description": "Face detected, cropped at url provided.",
        "url": url,
    }
Ejemplo n.º 3
0
def test_adjust_boundaries(values, expected_result):
    """Trigger the following: [h1 < 0, h2 > imgw, v1 < 0, v2 > imgh]"""
    # TODO: the padding code section is critically broken and
    # needs to be rewritten anyways. This section is more of
    # the draft of the proper testing section once the code is
    # fixed.
    imgh, imgw, h1, h2, v1, v2 = values
    c = Cropper()
    result = c._crop_positions(imgh, imgw, h1, h2, v1, v2)
    assert result == expected_result
Ejemplo n.º 4
0
def test_detect_face_in_cropped_image(height, width, integration):
    """An image cropped by Cropper should have a face detectable.
    This defends us against image warping or crops outside the region
    of interest.
    """
    c = Cropper(height=height, width=width)
    faces = [f for f in glob("tests/test/*") if not f.endswith("md")]
    print(faces)
    for face in faces:
        try:
            img_array = c.crop(face)
        except (AttributeError, TypeError):
            pass
        if img_array is not None:
            assert c.crop(img_array) is not None
Ejemplo n.º 5
0
def test_open_file_invalid_filetype_returns_None():
    c = Cropper()
    with pytest.raises(ImageReadError) as e:
        c.crop("asdf")
    assert e.type == ImageReadError
    assert "ImageReadError" in str(e)
Ejemplo n.º 6
0
def test_obama_has_a_face():
    loc = "tests/data/obama.jpg"
    obama = cv2.imread(loc)
    c = Cropper()
    assert len(c.crop(obama)) == 500
Ejemplo n.º 7
0
def test_crop_noise_returns_none():
    loc = "tests/data/noise.png"
    noise = cv2.imread(loc)
    c = Cropper()
    assert c.crop(noise) is None