Пример #1
0
def predict_68pts(param, roi_box):
    pts68 = reconstruct_vertex(param, dense=False)
    pts68 = pts68[:2, :]
    sx, sy, ex, ey = roi_box
    scale_x = (ex - sx) / 120
    scale_y = (ey - sy) / 120
    pts68[0, :] = pts68[0, :] * scale_x + sx
    pts68[1, :] = pts68[1, :] * scale_y + sy

    return pts68
Пример #2
0
def _predict_vertices(param, roi_box, dense):
    vertex = reconstruct_vertex(param, dense=dense)
    sx, sy, ex, ey = roi_box
    scale_x = (ex - sx) / 120
    scale_y = (ey - sy) / 120
    vertex[0, :] = vertex[0, :] * scale_x + sx
    vertex[1, :] = vertex[1, :] * scale_y + sy

    s = (scale_x + scale_y) / 2
    vertex[2, :] *= s

    return vertex
Пример #3
0
def gen_3d_vertex():
    filelists = 'test.data/AFLW2000-3D_crop.list'
    root = 'test.data/AFLW-2000-3D/'
    fns = open(filelists).read().strip().split('\n')
    params = _load('results/params_aflw2000_xgtu.npy')

    for i in range(100):
        fn = fns[i]
        vertex = reconstruct_vertex(params[i], dense=True)
        wfp = osp.join('results/AFLW-2000-3D_vertex/',
                       fn.replace('.jpg', '.mat'))
        print(wfp)
        sio.savemat(wfp, {'vertex': vertex})
Пример #4
0
def draw_landmarks():
    root_ori = 'test.data/AFLW-2000-3D'
    root = 'test.data/AFLW2000-3D_crop'
    filelists = 'test.data/AFLW2000-3D_crop.list'
    fns = open(filelists).read().strip().split('\n')
    params = _load('results/params_aflw2000_xgtu.npy')

    for i in range(len(fns)):
        plt.close()
        img_fp = osp.join(root_ori, fns[i])
        img = io.imread(img_fp)
        lms = reconstruct_vertex(params[i], dense=False)
        lms = convert_to_ori(lms, i)
        # lms = convert_to_ori_frmMat(lms, [root + fns[i]])

        # print(lms.shape)
        fig = plt.figure(figsize=plt.figaspect(.5))
        # fig = plt.figure(figsize=(8, 4))
        ax = fig.add_subplot(1, 2, 1)
        ax.imshow(img)

        alpha = 0.8
        markersize = 1.5
        lw = 1.2
        color = 'w'
        markeredgecolor = 'b'

        nums = [0, 17, 22, 27, 31, 36, 42, 48, 60, 68]
        for ind in range(len(nums) - 1):
            l, r = nums[ind], nums[ind + 1]
            ax.plot(lms[0, l:r],
                    lms[1, l:r],
                    color=color,
                    lw=lw,
                    alpha=alpha - 0.1)

            ax.plot(lms[0, l:r],
                    lms[1, l:r],
                    marker='o',
                    linestyle='None',
                    markersize=markersize,
                    color=color,
                    markeredgecolor=markeredgecolor,
                    alpha=alpha)

        ax.axis('off')

        plt.savefig('results/3dLandmarks_proj_resnet50_4chls/' +
                    img_fp.split('/')[-1])
Пример #5
0
def gen_3d_vertex():
    filelists = 'test.data/AFLW2000-3D_crop.list'
    root = 'AFLW-2000-3D/'
    fns = open(filelists).read().strip().split('\n')
    params = _load('res/params_aflw2000.npy')

    sel = [
        '00427', '00439', '00475', '00477', '00497', '00514', '00562', '00623',
        '01045', '01095', '01104', '01506', '01621', '02214', '02244', '03906',
        '04157'
    ]
    sel = list(map(lambda x: f'image{x}.jpg', sel))
    for i in range(2000):
        fn = fns[i]
        if fn in sel:
            vertex = reconstruct_vertex(params[i], dense=True)
            wfp = osp.join('res/AFLW-2000-3D_vertex/',
                           fn.replace('.jpg', '.mat'))
            print(wfp)
            sio.savemat(wfp, {'vertex': vertex})
Пример #6
0
def benchmark_aflw2000_params(params):
    outputs = []
    for i in range(params.shape[0]):
        lm = reconstruct_vertex(params[i])
        outputs.append(lm[:2, :])
    return _benchmark_aflw2000(outputs)
Пример #7
0
def main(args):
    # 1. load pretained model
    checkpoint_fp = 'models/phase1_wpdc_vdc_v2.pth.tar'
    arch = 'mobilenet_1'

    checkpoint = torch.load(
        checkpoint_fp, map_location=lambda storage, loc: storage)['state_dict']
    model = getattr(mobilenet_v1, arch)(num_classes=62)

    model_dict = model.state_dict()
    for k in checkpoint.keys():
        model_dict[k.replace('module.', '')] = checkpoint[k]
    model.load_state_dict(model_dict)
    if args.mode == 'gpu':
        cudnn.benchmark = True
        model = model.cuda()
    model.eval()

    # 2. load dlib model
    dlib_landmark_model = 'models/shape_predictor_68_face_landmarks.dat'
    face_detector = dlib.get_frontal_face_detector()
    face_regressor = dlib.shape_predictor(dlib_landmark_model)

    # 3. forward pass: one step strategy
    tri = sio.loadmat('visualize/tri.mat')['tri']
    for img_fp in args.files:
        img_ori = cv2.imread(img_fp)
        if args.dlib_bbox:
            rects = face_detector(img_ori, 1)
        else:
            rects = []

        if len(rects) == 0:
            rects = dlib.rectangles()
            rect_fp = img_fp + '.bbox'
            lines = open(rect_fp).read().strip().split('\n')[1:]
            for l in lines:
                l, r, t, b = [int(_) for _ in l.split(' ')[1:]]
                rect = dlib.rectangle(l, r, t, b)
                rects.append(rect)

        pts_dlib = []
        pts_res = []
        ind = 0
        suffix = get_suffix(img_fp)
        for rect in rects:
            # landmark & crop
            pts = face_regressor(img_ori, rect).parts()
            pts = np.array([[pt.x, pt.y] for pt in pts]).T
            pts_dlib.append(pts)

            roi_box = calc_roi_box(pts)
            img = crop_img(img_ori, roi_box)

            # forward: one step
            img = cv2.resize(img,
                             dsize=(120, 120),
                             interpolation=cv2.INTER_LINEAR)
            transform = transforms.Compose(
                [ToTensorGjz(),
                 NormalizeGjz(mean=127.5, std=128)])
            input = transform(img).unsqueeze(0)
            with torch.no_grad():
                if args.mode == 'gpu':
                    input = input.cuda()
                param = model(input)
                param = param.squeeze().cpu().numpy().flatten().astype(
                    np.float32)

            # 68 pts
            pts68 = predict_68pts(param, roi_box)

            if args.box_init == 'two':
                roi_box_step2 = calc_roi_box(pts68)
                img_step2 = crop_img(img_ori, roi_box_step2)
                img_step2 = cv2.resize(img_step2,
                                       dsize=(120, 120),
                                       interpolation=cv2.INTER_LINEAR)
                input = transform(img_step2).unsqueeze(0)
                with torch.no_grad():
                    if args.mode == 'gpu':
                        input = input.cuda()
                    param = model(input)
                    param = param.squeeze().cpu().numpy().flatten().astype(
                        np.float32)
                pts68 = predict_68pts(param, roi_box_step2)

            pts_res.append(pts68)

            # dense face vertices
            if args.dump_ply or args.dump_vertex:
                vertices = reconstruct_vertex(param, dense=True)
            if args.dump_ply:
                dump_to_ply(
                    vertices, tri,
                    '{}_{}.ply'.format(img_fp.replace(suffix, ''), ind))
            if args.dump_vertex:
                dump_vertex(
                    vertices, '{}_{}.mat'.format(img_fp.replace(suffix, ''),
                                                 ind))

            ind += 1

        draw_landmarks(img_ori,
                       pts_res,
                       wfp=img_fp.replace(suffix, '_3DDFA.jpg'),
                       show_flg=args.show_flg)
Пример #8
0
def draw_landmarks():
    filelists = 'test.data/AFLW2000-3D_crop.list'
    root = 'AFLW-2000-3D/'
    fns = open(filelists).read().strip().split('\n')
    params = _load('res/params_aflw2000.npy')

    for i in range(2000):
        plt.close()
        img_fp = osp.join(root, fns[i])
        img = io.imread(img_fp)
        lms = reconstruct_vertex(params[i], dense=False)
        lms = convert_to_ori(lms, i)

        # print(lms.shape)
        fig = plt.figure(figsize=plt.figaspect(.5))
        # fig = plt.figure(figsize=(8, 4))
        ax = fig.add_subplot(1, 2, 1)
        ax.imshow(img)

        alpha = 0.8
        markersize = 4
        lw = 1.5
        color = 'w'
        markeredgecolor = 'black'

        nums = [0, 17, 22, 27, 31, 36, 42, 48, 60, 68]
        for ind in range(len(nums) - 1):
            l, r = nums[ind], nums[ind + 1]
            ax.plot(lms[0, l:r],
                    lms[1, l:r],
                    color=color,
                    lw=lw,
                    alpha=alpha - 0.1)

            ax.plot(lms[0, l:r],
                    lms[1, l:r],
                    marker='o',
                    linestyle='None',
                    markersize=markersize,
                    color=color,
                    markeredgecolor=markeredgecolor,
                    alpha=alpha)

        ax.axis('off')

        # 3D
        ax = fig.add_subplot(1, 2, 2, projection='3d')
        lms[1] = img.shape[1] - lms[1]
        lms[2] = -lms[2]

        # print(lms)
        ax.scatter(lms[0], lms[2], lms[1], c="cyan", alpha=1.0, edgecolor='b')

        for ind in range(len(nums) - 1):
            l, r = nums[ind], nums[ind + 1]
            ax.plot3D(lms[0, l:r], lms[2, l:r], lms[1, l:r], color='blue')

        ax.view_init(elev=5., azim=-95)
        # ax.set_xlabel('x')
        # ax.set_ylabel('y')
        # ax.set_zlabel('z')

        ax.set_xticklabels([])
        ax.set_yticklabels([])
        ax.set_zticklabels([])

        plt.tight_layout()
        # plt.show()

        wfp = f'res/AFLW-2000-3D/{osp.basename(img_fp)}'
        plt.savefig(wfp, dpi=200)