def run():
    start = time.time()
    # Convert string of image data to uint8
    if 'data' not in request.files:
        return jsonify({'error': 'missing file param `data`'}), 400
    data = request.files['data'].read()
    if len(data) == 0:
        return jsonify({'error': 'empty image'}), 400

    # Convert string data to PIL Image
    img = Image.open(io.BytesIO(data))

    # Ensure i,qge size is under 1024
    if img.size[0] > 1024 or img.size[1] > 1024:
        img.thumbnail((1024, 1024))

    # Process Image
    res = u2net.run(np.array(img))
    res = res.resize((img.size), resample=Image.BILINEAR)  # remove resample

    # empty_img = Image.new("RGBA", (img.size), 0)
    bg = Image.open('bg.jpg')
    bg = bg.resize((img.size), resample=Image.BILINEAR)
    # new_img = Image.composite(img, empty_img, res.convert("L"))
    new_img = Image.composite(img, bg, res.convert("L"))

    # Save to buffer
    buffer = io.BytesIO()
    new_img.save(buffer, "PNG")
    buffer.seek(0)
    # Print stats
    logging.info(f'Completed in {time.time() - start:.2f}s')
    new_img.save("test.png")
    return send_file(buffer, mimetype='image/png')
Exemplo n.º 2
0
def run():
    start = time.time()

    # Convert string of image data to uint8
    if 'data' not in request.files:
        return jsonify({'error': 'missing file param `data`'}), 400
    data = request.files['data'].read()
    if len(data) == 0:
        return jsonify({'error': 'empty image'}), 400

    # Convert string data to PIL Image
    img = Image.open(io.BytesIO(data))

    # Ensure i,qge size is under 1024
    if img.size[0] > 1024 or img.size[1] > 1024:
        img.thumbnail((1024, 1024))

    # Process Image
    res = u2net.run(np.array(img))

    # Save to buffer
    buff = io.BytesIO()
    res.save(buff, 'PNG')
    buff.seek(0)

    # Print stats
    logging.info(f'Completed in {time.time() - start:.2f}s')

    # Return data
    return send_file(buff, mimetype='image/png')
Exemplo n.º 3
0
def run():
    # Convert string data to PIL Image
    img = Image.open(io.BytesIO(b64decode(request.form['image'])))

    # Process Image
    res = u2net.run(img)

    # Save to buffer
    buff = io.BytesIO()
    res.save(buff, 'PNG')
    buff.seek(0)

    # Return data
    return send_file(buff, mimetype='image/png')
Exemplo n.º 4
0
def run():
    start = time.time()

    # Convert string of image data to uint8
    if 'data' not in request.files:
        return jsonify({'error': 'missing file param `data`'}), 400
    data = request.files['data'].read()
    if len(data) == 0:
        return jsonify({'error': 'empty image'}), 400

    # Convert string data to PIL Image
    img = Image.open(io.BytesIO(data))

    # Ensure i,qge size is under 1024
    if img.size[0] > 1024 or img.size[1] > 1024:
        img.thumbnail((1024, 1024))

    # Process Image
    res = u2net.run(np.array(img))

    # Convert to BW and make it to the size of original image
    mask = res.convert('L').resize((img.width, img.height))

    # Making final composite
    logging.info(' > compositing final image...')
    empty = Image.new("RGBA", img.size, 0)
    img = Image.composite(img, empty, mask)

    # Save to buffer
    buff = io.BytesIO()
    img.save(buff, 'PNG')
    buff.seek(0)

    # Print stats
    logging.info(f'Completed in {time.time() - start:.2f}s')

    # Return data
    return send_file(buff, mimetype='image/png')
Exemplo n.º 5
0
def process_frames(original_filename, output_filename, mask_filename,
                   thumbnail_filename):
    # change to gpu(0) for faster processing
    vr = VideoReader(original_filename, ctx=cpu(0))

    height, width, layers = vr[0].shape
    print(f'\u001b[33mInput frame {height}x{width}x{layers}\u001b[0m')

    fourcc = cv2.VideoWriter_fourcc(*'FFV1')
    video = cv2.VideoWriter(output_filename + '.lossless.mkv', fourcc,
                            vr.get_avg_fps(), (width, height))
    video_mask = cv2.VideoWriter(mask_filename + '.lossless.mkv', fourcc,
                                 vr.get_avg_fps(), (320, 320))

    # solid color image
    keying_bg = create_keying_background(width, height, (0, 255, 0))

    for frame in vr:

        # convert to numpy format
        frame_np = frame.asnumpy()

        # run u2net
        mask_np = u2net.run(frame_np)

        # write frame to mask video
        mask_np_uint8 = (mask_np * 255).astype(np.uint8)
        mask_np_bgr = np.stack([mask_np_uint8] * 3,
                               axis=-1)  # https://stackoverflow.com/a/40119878
        video_mask.write(mask_np_bgr)

        # resize u2net output (320x320) to original frame resolution
        mask_cv2 = cv2.resize(mask_np, (width, height))

        # scale mask values from the range [0, 1] to [0, 255]
        mask_cv2_uint8 = (mask_cv2 * 255).astype(np.uint8)

        # thresholding the mask to have clear outlines
        ret, mask_cv2_uint8 = cv2.threshold(mask_cv2_uint8, 10, 255,
                                            cv2.THRESH_BINARY)

        # compute inverse mask
        mask_cv2_uint8_inv = cv2.bitwise_not(mask_cv2_uint8)

        # apply mask to image and merge with keying background
        frame_fg = cv2.bitwise_and(frame_np, frame_np, mask=mask_cv2_uint8)
        frame_bg = cv2.bitwise_and(keying_bg,
                                   keying_bg,
                                   mask=mask_cv2_uint8_inv)
        output_cv2 = frame_fg + frame_bg

        # convert the color space back to BGR
        output_cv2 = cv2.cvtColor(output_cv2, cv2.COLOR_RGB2BGR)

        video.write(output_cv2)

    cv2.destroyAllWindows()
    video.release()
    video_mask.release()

    # encode videos to h264
    thumbnail_proc = start_thumbnail(output_filename + '.lossless.mkv',
                                     thumbnail_filename)
    video_enc_proc = start_encode_video(output_filename + '.lossless.mkv',
                                        output_filename)
    video_mask_enc_proc = start_encode_video(mask_filename + '.lossless.mkv',
                                             mask_filename)
    assert thumbnail_proc.wait() == 0, 'Thumbnail encoding failed'
    assert video_enc_proc.wait() == 0, 'Video encoding failed'
    assert video_mask_enc_proc.wait() == 0, 'Mask video encoding failed'