Exemple #1
0
def predict(imagePath, trimapPath, outputPath):
    image = cv.imread(imagePath)
    trimap_img = cv.imread(trimapPath)[...,0]

    x_test = np.empty((1, img_rows, img_cols, 4), dtype=np.float32)
    x_test[0, :, :, 0:3] = image / 255.
    x_test[0, :, :, 3] = trimap_img / 255.

    y_pred = final.predict(x_test)
    y_pred = np.reshape(y_pred, (img_rows, img_cols))
    y_pred = y_pred * 255.0
    y_pred = get_final_output(y_pred, trimap_img)
    y_pred = y_pred.astype(np.uint8)

    cv.imwrite(outputPath, y_pred)
    K.clear_session()
Exemple #2
0
        x_test = np.empty((1, img_rows, img_cols, 4), dtype=np.float32)
        x_test[0, :, :, 0:3] = bgr_img / 255.
        x_test[0, :, :, 3] = trimap / 255.

        y_true = np.empty((1, img_rows, img_cols, 2), dtype=np.float32)
        y_true[0, :, :, 0] = alpha / 255.
        y_true[0, :, :, 1] = trimap / 255.

        y_pred = final.predict(x_test)
        # print('y_pred.shape: ' + str(y_pred.shape))

        y_pred = np.reshape(y_pred, (img_rows, img_cols))
        print(y_pred.shape)
        y_pred = y_pred * 255.0
        y_pred = get_final_output(y_pred, trimap)
        y_pred = y_pred.astype(np.uint8)

        sad_loss = compute_sad_loss(y_pred, alpha, trimap)
        mse_loss = compute_mse_loss(y_pred, alpha, trimap)
        str_msg = 'sad_loss: %.4f, mse_loss: %.4f, crop_size: %s' % (sad_loss, mse_loss, str(crop_size))
        print(str_msg)

        out = y_pred.copy()
        draw_str(out, (10, 20), str_msg)
        cv.imwrite('images/{}_out.png'.format(i), out)

        sample_bg = sample_bgs[i]
        bg = cv.imread(os.path.join(bg_test, sample_bg))
        bh, bw = bg.shape[:2]
        wratio = img_cols / bw
    # create patches
    x = np.dstack((img, np.expand_dims(trimap, axis=2))) / 255.
    patches = create_patches(x, PATCH_SIZE)

    # create mat for patches predictions
    patches_count = np.product(
        patch_dims(mat_size=trimap.shape, patch_size=PATCH_SIZE)
    )
    patches_predictions = np.zeros(shape=(patches_count, PATCH_SIZE, PATCH_SIZE))

    # predicting
    for i in range(patches.shape[0]):
        print("Predicting patches {}/{}".format(i + 1, patches_count))

        patch_prediction = final.predict(np.expand_dims(patches[i, :, :, :], axis=0))
        patches_predictions[i] = np.reshape(patch_prediction, (PATCH_SIZE, PATCH_SIZE)) * 255.

    # assemble
    result = assemble_patches(patches_predictions, trimap.shape, PATCH_SIZE)
    result = result[:img_size[0], :img_size[1]]

    prediction = get_final_output(result, trimap).astype(np.uint8)

    # save into files
    cv.imshow("result", prediction)
    cv.imshow("image", img)
    cv.waitKey(0)

    K.clear_session()

Exemple #4
0
def test_alphacom(config, resume):
    config['data_loader']['args']['batch_size'] = 1
    test_data_loader = alpha_com_dataloader(config)
    batch_size = config['data_loader']['args']['batch_size']

    # build model architecture
    model = get_instance(module_arch, 'arch', config)
    model.summary()

    # get function handles of loss and metrics
    # loss_fn = getattr(module_loss, config['loss'])
    metric_fns = [getattr(module_metric, met) for met in config['metrics']]

    # load state dict
    checkpoint = torch.load(resume)
    state_dict = checkpoint['state_dict']
    if config['n_gpu'] > 1:
        model = torch.nn.DataParallel(model)
    model.load_state_dict(state_dict)

    # prepare model for testing
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    model = model.to(device)
    model.eval()  # 一般有drop 或者 bn用这个

    with torch.no_grad():
        # for i, data in enumerate(tqdm(test_data_loader)):
        for i, data in enumerate(test_data_loader):
            image = data[0][0].permute(1, 2, 0)
            h, w, c = image.size()
            new_h = h - h % 32
            new_w = w - w % 32
            # print(image.size(), h, w)
            image = image.numpy()
            # print(image.shape, new_h, new_w)
            image = cv2.resize(image, (new_w, new_h),
                               interpolation=cv2.INTER_LINEAR)
            # print(image.shape)
            image = torch.from_numpy(image).permute(
                2, 0, 1)[np.newaxis, :, :, :].to(device)
            trimap1 = data[1][0].permute(1, 2, 0)
            trimap2 = data[2][0].permute(1, 2, 0)
            trimap3 = data[3][0].permute(1, 2, 0)
            image_name = data[4][0]

            # print(trimap1.size())
            trimap1 = trimap1.numpy()
            # print(trimap1.shape)
            # for i in trimap1: print(i)
            # cv2.imshow("213414",trimap1)
            # cv2.waitKey(0)
            trimap1 = cv2.resize(trimap1, (new_w, new_h),
                                 interpolation=cv2.INTER_LINEAR)
            trimap1 = torch.from_numpy(trimap1)[np.newaxis,
                                                np.newaxis, :, :].to(device)
            trimap2 = trimap2.numpy()
            trimap2 = cv2.resize(trimap2, (new_w, new_h),
                                 interpolation=cv2.INTER_LINEAR)
            trimap2 = torch.from_numpy(trimap2)[np.newaxis,
                                                np.newaxis, :, :].to(device)
            trimap3 = trimap3.numpy()
            trimap3 = cv2.resize(trimap3, (new_w, new_h),
                                 interpolation=cv2.INTER_LINEAR)
            trimap3 = torch.from_numpy(trimap3)[np.newaxis,
                                                np.newaxis, :, :].to(device)

            # print(image.size(), trimap1.size(), trimap2.size(), trimap3.size())
            if config['arch']['args']['stage'] == 0:
                pred1 = model(torch.cat((image, trimap1), dim=1))
                pred2 = model(torch.cat((image, trimap2), dim=1))
                pred3 = model(torch.cat((image, trimap3), dim=1))
            else:
                _, pred1 = model(torch.cat((image, trimap1), dim=1))
                _, pred2 = model(torch.cat((image, trimap2), dim=1))
                _, pred3 = model(torch.cat((image, trimap3), dim=1))

            pred1 *= 255
            pred2 *= 255
            pred3 *= 255
            pred1 = pred1[0].permute(1, 2, 0).cpu().numpy()
            image1 = get_final_output(pred1, trimap1)
            image1 = cv2.resize(image1, (w, h), interpolation=cv2.INTER_LINEAR)
            pred2 = pred2[0].permute(1, 2, 0).cpu().numpy()
            image2 = get_final_output(pred1, trimap2)
            image2 = cv2.resize(image2, (w, h), interpolation=cv2.INTER_LINEAR)
            pred3 = pred3[0].permute(1, 2, 0).cpu().numpy()
            image3 = get_final_output(pred1, trimap3)
            image3 = cv2.resize(image3, (w, h), interpolation=cv2.INTER_LINEAR)

            # print(image.size())
            # print(pred1.shape,pred2.shape,pred3.shape)
            # pred3 = cv2.resize(pred3, (w, h), interpolation=cv2.INTER_LINEAR)
            cv2.imwrite("alpha_com_output/Trimap1/{}".format(image_name),
                        image1)
            cv2.imwrite("alpha_com_output/Trimap2/{}".format(image_name),
                        image2)
            cv2.imwrite("alpha_com_output/Trimap3/{}".format(image_name),
                        image3)
            print(i)
Exemple #5
0
        cv.imwrite('images/{}_trimap.png'.format(i), np.array(trimap).astype(np.uint8))
        cv.imwrite('images/{}_alpha.png'.format(i), np.array(alpha).astype(np.uint8))

        x_test = np.empty((1, 320, 320, 4), dtype=np.float32)
        x_test[0, :, :, 0:3] = bgr_img / 255.
        x_test[0, :, :, 3] = trimap / 255.

        y_true = np.empty((1, 320, 320, 2), dtype=np.float32)
        y_true[0, :, :, 0] = alpha / 255.
        y_true[0, :, :, 1] = trimap / 255.

        y_pred = final.predict(x_test)
        # print('y_pred.shape: ' + str(y_pred.shape))

        y_pred = np.reshape(y_pred, (img_rows, img_cols))
        print(y_pred.shape)
        y_pred = y_pred * 255.0
        y_pred = get_final_output(y_pred, trimap)
        y_pred = y_pred.astype(np.uint8)

        sad_loss = compute_sad_loss(y_pred, alpha, trimap)
        mse_loss = compute_mse_loss(y_pred, alpha, trimap)
        str_msg = 'sad_loss: %.4f, mse_loss: %.4f, crop_size: %s' % (sad_loss, mse_loss, str(crop_size))
        print(str_msg)

        out = y_pred
        draw_str(out, (10, 20), str_msg)
        cv.imwrite('images/{}_out.png'.format(i), out)

    K.clear_session()
Exemple #6
0
    ap.add_argument("-i", "--image", help="path to the image file")
    ap.add_argument("-t", "--trimap", help="path to the trimap file")
    args = vars(ap.parse_args())
    image_path = args["image"]
    trimap_path = args["trimap"]

    if image_path is None:
        image_path = 'images/image.jpg'
    if trimap_path is None:
        trimap_path = 'images/trimap.jpg'

    print('Start processing image: {}'.format(image_path))

    x_test = np.empty((1, img_rows, img_cols, 4), dtype=np.float32)
    bgr_img = cv.imread(image_path)
    trimap = cv.imread(trimap_path, 0)

    x_test[0, :, :, 0:3] = bgr_img / 255.
    x_test[0, :, :, 3] = trimap / 255.

    out = final.predict(x_test)
    out = np.reshape(out, (img_rows, img_cols))
    print(out.shape)
    out = out * 255.0
    out = get_final_output(out, trimap)
    out = out.astype(np.uint8)
    cv.imshow('out', out)
    cv.imwrite('images/out.png', out)
    cv.waitKey(0)
    cv.destroyAllWindows()
Exemple #7
0
def video_test(videopath, models_path, videomask_path, is_rotate=False):
    cam = cv.VideoCapture(videopath)
    width = int(cam.get(cv.CAP_PROP_FRAME_WIDTH))
    height = int(cam.get(cv.CAP_PROP_FRAME_HEIGHT))

    cam_mask = cv.VideoCapture(videomask_path)
    width_mask = int(cam_mask.get(cv.CAP_PROP_FRAME_WIDTH))
    height_mask = int(cam_mask.get(cv.CAP_PROP_FRAME_HEIGHT))

    pretrained_path = models_path  #'models/final.87-0.0372.hdf5'         #'models/final.42-0.0398.hdf5'
    encoder_decoder = build_encoder_decoder()
    final = build_refinement(encoder_decoder)
    final.load_weights(pretrained_path)
    print(final.summary())

    tri_videopath = videopath[:-4] + '_tri.mp4'
    tri_video = video_save(tri_videopath, w=width_mask, h=height_mask)

    matting_videopath = videopath[:-4] + '_out.mp4'
    matting_video = video_save(matting_videopath, w=width_mask, h=height_mask)

    comp_videopath = videopath[:-4] + '_comp.mp4'
    comp_video = video_save(comp_videopath, w=width_mask, h=height_mask)

    while (cam.isOpened() and cam_mask.isOpened()):
        start_time = time.time()
        ret, frame = cam.read()
        ret, frame_mask = cam_mask.read()

        if is_rotate:
            frame = imutils.rotate_bound(frame, 90)
            frame_mask = imutils.rotate_bound(frame_mask, 90)
        #             print(frame.shape)
        if frame is None:
            print('Error image!')
            break
        if frame_mask is None:
            print('Error mask image!')
            break

        bg_h, bg_w = height, width
        print('bg_h, bg_w: ' + str((bg_h, bg_w)))

        #         a = get_alpha_test(image_name)
        a = cv.cvtColor(frame_mask, cv.COLOR_BGR2GRAY)
        _, a = cv.threshold(a, 240, 255, cv.THRESH_BINARY)

        a_h, a_w = height_mask, width_mask
        print('a_h, a_w: ' + str((a_h, a_w)))

        alpha = np.zeros((bg_h, bg_w), np.float32)

        alpha[0:a_h, 0:a_w] = a
        trimap = generate_trimap_withmask(alpha)
        #         fg = np.array(np.greater_equal(a, 255).astype(np.float32))
        #         cv.imshow('test_show',fg)
        different_sizes = [(320, 320), (320, 320), (320, 320), (480, 480),
                           (640, 640)]
        crop_size = random.choice(different_sizes)

        bgr_img = frame
        alpha = alpha
        trimap = trimap
        #         cv.imwrite('images/{}_image.png'.format(i), np.array(bgr_img).astype(np.uint8))
        #         cv.imwrite('images/{}_trimap.png'.format(i), np.array(trimap).astype(np.uint8))
        #         cv.imwrite('images/{}_alpha.png'.format(i), np.array(alpha).astype(np.uint8))

        #         x_test = np.empty((1, img_rows, img_cols, 4), dtype=np.float32)
        #         x_test[0, :, :, 0:3] = bgr_img / 255.
        #         x_test[0, :, :, 3] = trimap / 255.

        x_test = np.empty((1, 320, 320, 4), dtype=np.float32)
        bgr_img1 = cv.resize(bgr_img, (320, 320))
        trimap1 = cv.resize(trimap, (320, 320))
        x_test[0, :, :, 0:3] = bgr_img1 / 255.
        x_test[0, :, :, 3] = trimap1 / 255.

        y_true = np.empty((1, img_rows, img_cols, 2), dtype=np.float32)
        y_true[0, :, :, 0] = alpha / 255.
        y_true[0, :, :, 1] = trimap / 255.

        y_pred = final.predict(x_test)
        # print('y_pred.shape: ' + str(y_pred.shape))

        #         y_pred = np.reshape(y_pred, (img_rows, img_cols))
        y_pred = np.reshape(y_pred, (320, 320))
        print(y_pred.shape)
        y_pred = cv.resize(y_pred, (width, height))
        y_pred = y_pred * 255.0
        cv.imshow('pred', y_pred)
        y_pred = get_final_output(y_pred, trimap)

        y_pred = y_pred.astype(np.uint8)

        sad_loss = compute_sad_loss(y_pred, alpha, trimap)
        mse_loss = compute_mse_loss(y_pred, alpha, trimap)
        str_msg = 'sad_loss: %.4f, mse_loss: %.4f, crop_size: %s' % (
            sad_loss, mse_loss, str(crop_size))
        print(str_msg)

        out = y_pred.copy()
        comp = composite_alpha(frame, out)
        draw_str(out, (10, 20), str_msg)

        trimap_show = np.stack((trimap, trimap, trimap), -1)
        out_show = cv.merge((out, out, out))
        #         print(trimap_show.shape,out_show.shape,comp.shape)
        tri_video.write(trimap_show)
        matting_video.write(out_show)
        comp_video.write(comp)
        #         cv.imwrite('images/{}_out.png'.format(filename[6:]), out)

        #### composite background
        #             sample_bg = sample_bgs[i]
        #             bg = cv.imread(os.path.join(bg_test, sample_bg))
        #             bh, bw = bg.shape[:2]
        #             wratio = img_cols / bw
        #             hratio = img_rows / bh
        #             ratio = wratio if wratio > hratio else hratio
        #             if ratio > 1:
        #                 bg = cv.resize(src=bg, dsize=(math.ceil(bw * ratio), math.ceil(bh * ratio)), interpolation=cv.INTER_CUBIC)
        #     #         im, bg = composite4(bgr_img, bg, y_pred, img_cols, img_rows)
        #             im, bg = composite4(bgr_img, bg, y_pred, img_cols, img_rows)
        #     #         cv.imwrite('images/{}_compose.png'.format(filename[6:]), im)
        #     #         cv.imwrite('images/{}_new_bg.png'.format(i), bg)

        print("Time: {:.2f} s / img".format(time.time() - start_time))

        cv.imshow('out', out)
        cv.imshow('frame', frame)
        cv.imshow('comp', comp)
        cv.imshow('trimap', trimap)

        if cv.waitKey(1) & 0xFF == ord('q'):
            break
    cam.release()
    cam_mask.release()
    tri_video.release()
    matting_video.release()
    comp_video.release()

    cv.destroyAllWindows()