File size: 4,597 Bytes
7f51798
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# https://github.com/xinntao/facexlib/blob/master/inference/inference_matting.py

from tqdm import tqdm, trange
import argparse
from pathlib import Path
import cv2
import numpy as np
import torch.nn.functional as F
from torchvision.transforms.functional import normalize

from facexlib.matting import init_matting_model
from facexlib.utils import img2tensor


def matt_single(args):
    modnet = init_matting_model()

    # read image
    img = cv2.imread(args.img_path) / 255.
    # unify image channels to 3
    if len(img.shape) == 2:
        img = img[:, :, None]
    if img.shape[2] == 1:
        img = np.repeat(img, 3, axis=2)
    elif img.shape[2] == 4:
        img = img[:, :, 0:3]

    img_t = img2tensor(img, bgr2rgb=True, float32=True)
    normalize(img_t, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True)
    img_t = img_t.unsqueeze(0).cuda()

    # resize image for input
    _, _, im_h, im_w = img_t.shape
    ref_size = 512
    if max(im_h, im_w) < ref_size or min(im_h, im_w) > ref_size:
        if im_w >= im_h:
            im_rh = ref_size
            im_rw = int(im_w / im_h * ref_size)
        elif im_w < im_h:
            im_rw = ref_size
            im_rh = int(im_h / im_w * ref_size)
    else:
        im_rh = im_h
        im_rw = im_w
    im_rw = im_rw - im_rw % 32
    im_rh = im_rh - im_rh % 32
    img_t = F.interpolate(img_t, size=(im_rh, im_rw), mode='area')

    # inference
    _, _, matte = modnet(img_t, True)

    # resize and save matte
    matte = F.interpolate(matte, size=(im_h, im_w), mode='area')
    matte = matte[0][0].data.cpu().numpy()
    cv2.imwrite(args.save_path, (matte * 255).astype('uint8'))

    # get foreground
    matte = matte[:, :, None]
    foreground = img * matte + np.full(img.shape, 1) * (1 - matte)
    cv2.imwrite(args.save_path.replace('.png', '_fg.png'), foreground * 255)

def matt_directory(args): # for extracting ffhq imgs foreground 
    modnet = init_matting_model()

    all_imgs = list(Path(args.img_dir_path).rglob('*.png'))
    print('all imgs: ', len(all_imgs))

    tgt_dir_path = '/mnt/lustre/share/yslan/ffhq/unzipped_ffhq_matte/'
    # tgt_img_path = '/mnt/lustre/share/yslan/ffhq/unzipped_ffhq_matting/'

    for img_path in tqdm(all_imgs):

        # read image
        # img = cv2.imread(args.img_path) / 255.
        img = cv2.imread(str(img_path)) / 255.

        relative_img_path = Path(img_path).relative_to('/mnt/lustre/share/yslan/ffhq/unzipped_ffhq_512/')
        tgt_save_path = tgt_dir_path / relative_img_path

        (tgt_save_path.parent).mkdir(parents=True, exist_ok=True)

        # unify image channels to 3
        if len(img.shape) == 2:
            img = img[:, :, None]
        if img.shape[2] == 1:
            img = np.repeat(img, 3, axis=2)
        elif img.shape[2] == 4:
            img = img[:, :, 0:3]

        img_t = img2tensor(img, bgr2rgb=True, float32=True)
        normalize(img_t, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True)
        img_t = img_t.unsqueeze(0).cuda()

        # resize image for input
        _, _, im_h, im_w = img_t.shape
        ref_size = 512
        if max(im_h, im_w) < ref_size or min(im_h, im_w) > ref_size:
            if im_w >= im_h:
                im_rh = ref_size
                im_rw = int(im_w / im_h * ref_size)
            elif im_w < im_h:
                im_rw = ref_size
                im_rh = int(im_h / im_w * ref_size)
        else:
            im_rh = im_h
            im_rw = im_w
        im_rw = im_rw - im_rw % 32
        im_rh = im_rh - im_rh % 32
        img_t = F.interpolate(img_t, size=(im_rh, im_rw), mode='area')

        # inference
        _, _, matte = modnet(img_t, True)

        # resize and save matte
        matte = F.interpolate(matte, size=(im_h, im_w), mode='area')
        matte = matte[0][0].data.cpu().numpy()
        # cv2.imwrite(args.save_path, (matte * 255).astype('uint8'))
        cv2.imwrite(str(tgt_save_path), (matte * 255).astype('uint8'))

        assert tgt_save_path.exists()

        # get foreground
        # matte = matte[:, :, None]
        # foreground = img * matte + np.full(img.shape, 1) * (1 - matte)
        # cv2.imwrite(args.save_path.replace('.png', '_fg.png'), foreground * 255)

    pass


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--img_path', type=str, default='assets/test.jpg')
    parser.add_argument('--save_path', type=str, default='test_matting.png')

    parser.add_argument('--img_dir_path', type=str, default='assets', required=False)
    args = parser.parse_args()

    # matt_single(args)
    matt_directory(args)