Example #1
0
def run_pix2pix(img, net, opt):
    if opt.netG == 'HD':
        img = impro.resize(img, 512)
    else:
        img = impro.resize(img, 128)
    img = data.im2tensor(img, use_gpu=opt.use_gpu)
    img_fake = net(img)
    img_fake = data.tensor2im(img_fake)
    return img_fake
Example #2
0
def run_styletransfer(opt, net, img):
    if opt.output_size != 0:
        if 'resize' in opt.preprocess and 'resize_scale_width' not in opt.preprocess:
            img = impro.resize(img, opt.output_size)
        elif 'resize_scale_width' in opt.preprocess:
            img = cv2.resize(img, (opt.output_size, opt.output_size))
        img = img[0:4 * int(img.shape[0] / 4), 0:4 * int(img.shape[1] / 4), :]

    if 'edges' in opt.preprocess:
        if opt.canny > 100:
            canny_low = opt.canny - 50
            canny_high = np.clip(opt.canny + 50, 0, 255)
        elif opt.canny < 50:
            canny_low = np.clip(opt.canny - 25, 0, 255)
            canny_high = opt.canny + 25
        else:
            canny_low = opt.canny - int(opt.canny / 2)
            canny_high = opt.canny + int(opt.canny / 2)
        img = cv2.Canny(img, opt.canny - 50, opt.canny + 50)
        if opt.only_edges:
            return img
        img = data.im2tensor(img,
                             use_gpu=opt.use_gpu,
                             gray=True,
                             use_transform=False,
                             is0_1=False)
    else:
        img = data.im2tensor(img,
                             use_gpu=opt.use_gpu,
                             gray=False,
                             use_transform=True)
    img = net(img)
    img = data.tensor2im(img)
    return img
Example #3
0
def loadimage(imagepaths, maskpaths, opt, test_flag=False):
    batchsize = len(imagepaths)
    images = np.zeros((batchsize, 3, opt.finesize, opt.finesize),
                      dtype=np.float32)
    masks = np.zeros((batchsize, 1, opt.finesize, opt.finesize),
                     dtype=np.float32)
    for i in range(len(imagepaths)):
        img = impro.resize(impro.imread(imagepaths[i]), opt.loadsize)
        mask = impro.resize(impro.imread(maskpaths[i], mod='gray'),
                            opt.loadsize)
        img, mask = data.random_transform_image(img, mask, opt.finesize,
                                                test_flag)
        images[i] = (img.transpose((2, 0, 1)) / 255.0)
        masks[i] = (mask.reshape(1, 1, opt.finesize, opt.finesize) / 255.0)
    images = Totensor(images, opt.use_gpu)
    masks = Totensor(masks, opt.use_gpu)

    return images, masks
Example #4
0
def run_segment(img, net, size=360, use_gpu=True):
    img = impro.resize(img, size)
    img = data.im2tensor(img,
                         use_gpu=use_gpu,
                         bgr2rgb=False,
                         use_transform=False,
                         is0_1=True)
    mask = net(img)
    mask = data.tensor2im(mask, gray=True, rgb2bgr=False, is0_1=True)
    return mask
Example #5
0
def cleanmosaic_video_fusion(opt, netG, netM):
    path = opt.media_path
    N = opt.N
    if 'HD' in os.path.basename(opt.model_path):
        INPUT_SIZE = 256
    else:
        INPUT_SIZE = 128
    fps, imagepaths, height, width = video_init(opt, path)
    positions = get_mosaic_positions(opt, netM, imagepaths, savemask=True)

    # clean mosaic
    img_pool = np.zeros((height, width, 3 * N), dtype='uint8')
    for i, imagepath in enumerate(imagepaths, 0):
        x, y, size = positions[i][0], positions[i][1], positions[i][2]

        # image read stream
        mask = cv2.imread(os.path.join('./tmp/mosaic_mask', imagepath), 0)
        if i == 0:
            for j in range(0, N):
                img_pool[:, :, j * 3:(j + 1) * 3] = impro.imread(
                    os.path.join(
                        './tmp/video2image',
                        imagepaths[np.clip(i + j - 12, 0,
                                           len(imagepaths) - 1)]))
        else:
            img_pool[:, :, 0:(N - 1) * 3] = img_pool[:, :, 3:N * 3]
            img_pool[:, :, (N - 1) * 3:] = impro.imread(
                os.path.join(
                    './tmp/video2image',
                    imagepaths[np.clip(i + 12, 0,
                                       len(imagepaths) - 1)]))
        img_origin = img_pool[:, :,
                              int((N - 1) / 2) * 3:(int((N - 1) / 2) + 1) * 3]

        if size == 0:  # can not find mosaic,
            cv2.imwrite(os.path.join('./tmp/replace_mosaic', imagepath),
                        img_origin)
        else:

            mosaic_input = np.zeros((INPUT_SIZE, INPUT_SIZE, 3 * N + 1),
                                    dtype='uint8')
            mosaic_input[:, :, 0:N * 3] = impro.resize(
                img_pool[y - size:y + size, x - size:x + size, :], INPUT_SIZE)
            mask_input = impro.resize(mask, np.min(
                img_origin.shape[:2]))[y - size:y + size, x - size:x + size]
            mosaic_input[:, :, -1] = impro.resize(mask_input, INPUT_SIZE)

            mosaic_input = data.im2tensor(mosaic_input,
                                          bgr2rgb=False,
                                          use_gpu=opt.use_gpu,
                                          use_transform=False,
                                          is0_1=False)
            unmosaic_pred = netG(mosaic_input)
            img_fake = data.tensor2im(unmosaic_pred,
                                      rgb2bgr=False,
                                      is0_1=False)
            img_result = impro.replace_mosaic(img_origin, img_fake, mask, x, y,
                                              size, opt.no_feather)
            cv2.imwrite(os.path.join('./tmp/replace_mosaic', imagepath),
                        img_result)
        print('\r',
              'Clean Mosaic:' + str(i + 1) + '/' + str(len(imagepaths)),
              util.get_bar(100 * i / len(imagepaths), num=35),
              end='')
    print()
    ffmpeg.image2video(
        fps, './tmp/replace_mosaic/output_%05d.' + opt.tempimage_type,
        './tmp/voice_tmp.mp3',
        os.path.join(
            opt.result_dir,
            os.path.splitext(os.path.basename(path))[0] + '_clean.mp4'))
Example #6
0
        mask_avg = mask.astype(np.float64)
        mask_flag = True
    else:
        mask_avg += mask.astype(np.float64)

mask_avg = np.clip(mask_avg / len(imagepaths), 0, 255).astype('uint8')
mask_avg = impro.mask_threshold(mask_avg, 20, 64)
if not opt.all_mosaic_area:
    mask_avg = impro.find_mostlikely_ROI(mask_avg)
# x, y, size, area = impro.boundingSquare(mask_avg, Ex_mul=random.uniform(1.1, 1.5))

cnt = 0
for i in range(len(imagepaths)):
    x, y, size, area = impro.boundingSquare(masks[i], Ex_mul=1.5)
    if area > opt.minmaskarea and size > opt.minsize * 1.5 and impro.Q_lapulase(imgs[i]) > opt.quality:
        img = impro.resize(imgs[i][y - size:y + size, x - size:x + size], opt.outsize, interpolation=cv2.INTER_CUBIC)
        mask = impro.resize(masks[i][y - size:y + size, x - size:x + size], opt.outsize, interpolation=cv2.INTER_CUBIC)
        img_mosaic = mosaic.addmosaic_random(img, mask)

        cv2.imwrite(os.path.join(test_A_path, str(0) + '_' + '%05d' % (i + 1) + '.jpg'), img_mosaic)
        cv2.imwrite(os.path.join(test_B_path, str(0) + '_' + '%05d' % (i + 1) + '.jpg'), img)
    else:
        print(imagepaths[i])
        cnt += 1

print("bad pic:", cnt)

'''
python3 z_make_images_dataset.py --datadir ../datasets/demosaic/video2images --savedir ../datasets/demosaic

'''