File size: 10,459 Bytes
22257c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
"""
 # Copyright 2020 Adobe
 # All Rights Reserved.
 
 # NOTICE: Adobe permits you to use, modify, and distribute this file in
 # accordance with the terms of the Adobe license agreement accompanying
 # it.
 
"""

import os, glob, time, sys
import numpy as np
import cv2
from src.dataset.utils.Av2Flau_Convertor import Av2Flau_Convertor
import platform

if platform.release() == '4.4.0-83-generic':
    src_dir = r'/mnt/ntfs/Dataset/TalkingToon/VoxCeleb2_imagetranslation/raw_fl3d'
    mp4_dir = r'/mnt/ntfs/Dataset/VoxCeleb2/train_set/dev/mp4'
else:
    src_dir = r'/mnt/nfs/work1/kalo/yangzhou/VoxCeleb2/train_set/dev/mp4'
    out_dir = r'/mnt/nfs/scratch1/yangzhou/VoxCeleb2_compressed_imagetranslation'

def landmark_extraction(si, ei):
    '''

    :param si: start index
    :param ei: end index
    :return: save extracted landmarks to out_dir
    '''

    for folder_name in ['raw_wav', 'raw_fl3d', 'register_fl3d', 'dump', 'tmp_v', 'nn_result', 'ckpt', 'log']:
        try:
            os.mkdir(os.path.join(out_dir, folder_name))
        except:
            pass


    if(not os.path.isfile(os.path.join(out_dir, 'filename_index_new.txt'))):
        # generate all file list

        clip_len_count = [0] * 500
        id_clip_list = []

        ids = glob.glob1(src_dir, '*')
        ids.sort()
        for id in ids:
            print(id)
            clips = glob.glob1(os.path.join(src_dir, id), '*')
            clips.sort()
            for clip in clips:
                videos = glob.glob1(os.path.join(src_dir, id, clip), '*.mp4')
                clip_len_count[len(videos)] +=1
                # if(len(videos) > 10 and len(videos) < 30):
                #     id_clip_list.append((id, clip))
                id_clip_list.append((id, clip))

        print(clip_len_count)
        print(len(id_clip_list))

        files = []
        for id, clip in id_clip_list:
            cur_src_dir = os.path.join(src_dir, id, clip)
            cur_files = glob.glob1(cur_src_dir, '*.mp4')

            cur_files = np.random.permutation(cur_files)[0:1]

            cur_files = ['{}_x_{}_x_{}'.format(id, clip, f) for f in cur_files]

            files += cur_files

        with open(os.path.join(out_dir, 'filename_index_new.txt'), 'w') as f:
            for i, file in enumerate(files):
                f.write('{} {}\n'.format(i, file))
    else:
        with open(os.path.join(out_dir, 'filename_index_new.txt'), 'r') as f:
            lines = f.readlines()

        print(sys.argv)
        for line in lines[si:ei]:
            st = time.time()
            idx, file = int(line.split(' ')[0]), line.split(' ')[1][:-1]

            # # check if exists
            # video_dir = os.path.join(src_dir, file)
            # if ('\\' in video_dir):
            #     video_name = video_dir.split('\\')[-1]
            # else:
            #     video_name = video_dir.split('/')[-1]
            # save_name = os.path.join(out_dir.replace('VoxCeleb2_compressed_imagetranslation',
            #                                          'VoxCeleb2_imagetranslation'),
            #                          'raw_fl3d/fan_{:05d}_{}_3d.txt'.format(idx, video_name[:-4]))
            # if(os.path.isfile(save_name)):
            #     print('==> File {} {} exist, just copy'.format(idx, video_name[:-4]))
            #     shutil.copy(save_name,
            #                 os.path.join(out_dir, 'raw_fl3d/fan_{:05d}_{}_3d.txt'.format(idx, video_name[:-4])))
            #     continue

            c = Av2Flau_Convertor(video_dir=os.path.join(src_dir, file),
                                  out_dir=out_dir, idx=idx)
            c.convert() #  (save_audio=False, register=False, show=False)
            print('Idx: {}, Processed time (min): {}'.format(idx, (time.time() - st) / 60.0))

def landmark_image_to_data(si, ei, show=False):
    '''
    DROPPED DUE TO LARGE DISK SPACE CONSUME
    :param si:
    :param ei:
    :param show:
    :return:
    '''
    # load landmark
    print(src_dir)
    fls_filenames = glob.glob1(src_dir, '*')
    print(fls_filenames)
    pf = {}

    for i, fls_filename in enumerate(fls_filenames):

        fls = np.loadtxt(os.path.join(src_dir, fls_filename))
        print(i, '/', len(fls_filenames), fls.shape)

        mp4_filename = fls_filename[:-4].split('_x_')
        mp4_id = mp4_filename[0].split('_')[-1]
        mp4_vname = mp4_filename[1]
        mp4_vid = mp4_filename[2][:-3]
        print(mp4_id, mp4_vname, mp4_vid)
        video_dir = os.path.join(mp4_dir, mp4_id, mp4_vname, mp4_vid+'.mp4')
        print('video_dir : ' + video_dir)
        video = cv2.VideoCapture(video_dir)
        if (video.isOpened() == False):
            print('Unable to open video file')
            exit(0)

        if(show==True):
            length = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
            fps = video.get(cv2.CAP_PROP_FPS)
            w = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
            h = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
            print('Process Video {}, len: {}, FPS: {:.2f}, W X H: {} x {}'.format(video_dir, length, fps, w, h))
            writer = cv2.VideoWriter('a.mp4', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, (512, 256))

            # skip first several frames due to landmark extraction
            start_idx = fls[0, 0].astype(int)
            print('Skip beginning # {} frames'.format(start_idx))

            for _ in range(start_idx):
                ret, img_video = video.read()

             # save video and landmark in parallel
            for j in range(fls.shape[0]):
                img_fl = np.ones(shape=(224, 224, 3)) * 255
                idx = fls[j, 0]
                fl = fls[j, 1:].astype(int)
                img_fl = vis_landmark_on_img(img_fl, np.reshape(fl, (68, 3)))

                ret, img_video = video.read()

                frame = np.concatenate((img_fl, img_video), axis=1)
                frame = cv2.resize(frame, (512, 256))
                writer.write(frame.astype(np.uint8))

            video.release()
            writer.release()
            cv2.destroyAllWindows()

            exit(0)

        else:
            # skip first several frames due to landmark extraction
            start_idx = fls[0, 0].astype(int)
            print('Skip beginning # {} frames'.format(start_idx))
            for _ in range(start_idx):
                ret, img_video = video.read()

            # save video and landmark in parallel
            frames = []
            for j in range(fls.shape[0]):
                img_fl = np.ones(shape=(224, 224, 3)) * 255
                idx = fls[j, 0]
                fl = fls[j, 1:].astype(int)
                img_fl = vis_landmark_on_img(img_fl, np.reshape(fl, (68, 3)))

                ret, img_video = video.read()

                frame = np.concatenate((img_fl, img_video), axis=2)
                frame = cv2.resize(frame, (256, 256)) # 256 x 256  6
                frames.append(frame)
            frames = np.stack(frames, axis=0).astype(int) # N x 256 x 256 x 6
            pf[fls_filename] = frames

    # save to pickle file
    # with open('train_data.pickle', 'wb') as handle:
    #     pickle.dump(pf, handle)


def vis_landmark_on_img(img, shape, linewidth=2):
    '''
    Visualize landmark on images.
    '''

    def draw_curve(idx_list, color=(0, 255, 0), loop=False, lineWidth=linewidth):
        for i in idx_list:
            cv2.line(img, (shape[i, 0], shape[i, 1]), (shape[i + 1, 0], shape[i + 1, 1]), color, lineWidth)
        if (loop):
            cv2.line(img, (shape[idx_list[0], 0], shape[idx_list[0], 1]),
                     (shape[idx_list[-1] + 1, 0], shape[idx_list[-1] + 1, 1]), color, lineWidth)

    draw_curve(list(range(0, 16)), color=(255, 144, 25))  # jaw
    draw_curve(list(range(17, 21)), color=(50, 205, 50))  # eye brow
    draw_curve(list(range(22, 26)), color=(50, 205, 50))
    draw_curve(list(range(27, 35)), color=(208, 224, 63))  # nose
    draw_curve(list(range(36, 41)), loop=True, color=(71, 99, 255))  # eyes
    draw_curve(list(range(42, 47)), loop=True, color=(71, 99, 255))
    draw_curve(list(range(48, 59)), loop=True, color=(238, 130, 238))  # mouth
    draw_curve(list(range(60, 67)), loop=True, color=(238, 130, 238))

    return img


def vis_landmark_on_img98(img, shape, linewidth=2):
    '''
    Visualize landmark on images.
    '''

    def draw_curve(idx_list, color=(0, 255, 0), loop=False, lineWidth=linewidth):
        for i in idx_list:
            cv2.line(img, (shape[i, 0], shape[i, 1]), (shape[i + 1, 0], shape[i + 1, 1]), color, lineWidth)
        if (loop):
            cv2.line(img, (shape[idx_list[0], 0], shape[idx_list[0], 1]),
                     (shape[idx_list[-1] + 1, 0], shape[idx_list[-1] + 1, 1]), color, lineWidth)

    draw_curve(list(range(0, 32)), color=(255, 144, 25))  # jaw
    draw_curve(list(range(33, 41)), color=(50, 205, 50), loop=True)  # eye brow
    draw_curve(list(range(42, 50)), color=(50, 205, 50), loop=True)
    draw_curve(list(range(51, 59)), color=(208, 224, 63))  # nose
    draw_curve(list(range(60, 67)), loop=True, color=(71, 99, 255))  # eyes
    draw_curve(list(range(68, 75)), loop=True, color=(71, 99, 255))
    draw_curve(list(range(76, 87)), loop=True, color=(238, 130, 238))  # mouth
    draw_curve(list(range(88, 95)), loop=True, color=(238, 130, 238))

    return img


def vis_landmark_on_img74(img, shape, linewidth=2):
    '''
    Visualize landmark on images.
    '''

    def draw_curve(idx_list, color=(0, 255, 0), loop=False, lineWidth=linewidth):
        for i in idx_list:
            cv2.line(img, (shape[i, 0], shape[i, 1]), (shape[i + 1, 0], shape[i + 1, 1]), color, lineWidth)
        if (loop):
            cv2.line(img, (shape[idx_list[0], 0], shape[idx_list[0], 1]),
                     (shape[idx_list[-1] + 1, 0], shape[idx_list[-1] + 1, 1]), color, lineWidth)

    draw_curve(list(range(0, 16)), color=(255, 144, 25))  # jaw
    draw_curve(list(range(17, 21)), color=(50, 205, 50), loop=False)  # eye brow
    draw_curve(list(range(22, 26)), color=(50, 205, 50), loop=False)
    draw_curve(list(range(27, 35)), color=(208, 224, 63))  # nose
    draw_curve(list(range(36, 43)), loop=True, color=(71, 99, 255))  # eyes
    draw_curve(list(range(44, 51)), loop=True, color=(71, 99, 255))
    draw_curve(list(range(52, 63)), loop=True, color=(238, 130, 238))  # mouth
    draw_curve(list(range(64, 71)), loop=True, color=(238, 130, 238))

    return img