-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
336 lines (264 loc) · 10 KB
/
main.py
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# import os
# import sys
# import cv2
# import numpy as np
# from PIL import Image
# import dlib
# from matplotlib import pyplot as plt
# import torch
# import dnnlib
# import legacy
# import PIL.Image
# import numpy as np
# import imageio
# from tqdm import tqdm
# # Set working directory
# project_root = "/Users/oscarwu_admin_1.0/repos/facial_detection_gan_pytorch"
# os.chdir(project_root)
# # Update sys.path
# sys.path.insert(0, os.path.join(project_root, "stylegan2-ada-pytorch"))
# # Constants
# NETWORK = "https://nvlabs-fi-cdn.nvidia.com/stylegan2-ada-pytorch/pretrained/ffhq.pkl"
# STEPS = 150
# FPS = 30
# FREEZE_STEPS = 30
# # Read image files
# img_list = []
# for file in os.listdir(project_root):
# if file.endswith((".jpg", ".jpeg", ".png", ".bmp")):
# img_list.append(file)
# if len(img_list) < 2:
# print("Upload at least 2 images for morphing.")
# import cv2
# import numpy as np
# from PIL import Image
# import dlib
# from matplotlib import pyplot as plt
# detector = dlib.get_frontal_face_detector()
# predictor = dlib.shape_predictor('shape_predictor_5_face_landmarks.dat')
# def crop_stylegan(img):
# dets = detector(img, 1)
# if len(dets) == 0:
# raise ValueError("No face detected")
# d = dets[0]
# shape = predictor(img, d)
# x1, y1 = shape.part(0).x, shape.part(0).y
# x2, y2 = shape.part(2).x, shape.part(2).y
# x3, y3 = shape.part(4).x, shape.part(4).y
# center = dlib.point((x1 + x2) // 2, (y1 + y2) // 2)
# width = np.linalg.norm(np.array([x1, y1]) - np.array([x3, y3]))
# size = int(width * 2.2)
# half_size = size // 2
# left, top = center.x - half_size, center.y - half_size
# right, bottom = left + size, top + size
# cropped_img = img[top:bottom, left:right]
# cropped_img = cv2.resize(cropped_img, (1024, 1024))
# return cropped_img
# def process_images(img_list):
# cropped_images = []
# for img_name in img_list:
# img = cv2.imread(img_name)
# if img is None:
# raise ValueError(f"{img_name} not found")
# cropped_img = crop_stylegan(img)
# cropped_images.append(cropped_img)
# img_rgb = cv2.cvtColor(cropped_img, cv2.COLOR_BGR2RGB)
# plt.imshow(img_rgb)
# plt.title(f'cropped {img_name}')
# plt.show()
# return cropped_images
# cropped_images = process_images(img_list)
# # Generate GAN images and latent vectors for each input image
# gan_images = []
# latent_vectors = []
# # to execute shell commands
# import subprocess
# subprocess.run(cmd, shell=True, check=True)
# # for i, cropped_img in enumerate(cropped_images):
# # cv2.imwrite(f"cropped_{i}.png", cropped_img)
# # # HIDE OUTPUT
# # cmd = f"python /content/stylegan2-ada-pytorch/projector.py "\
# # f"--save-video 0 --num-steps 1000 --outdir=out_{i} "\
# # f"--target=cropped_{i}.png --network={NETWORK}"
# # !{cmd} # cannot use in python script
# # img_gan = cv2.imread(f'/content/out_{i}/proj.png')
# # img_rgb = cv2.cvtColor(img_gan, cv2.COLOR_BGR2RGB)
# # plt.imshow(img_rgb)
# # plt.title(f'gan-image-{i}')
# # plt.show()
# # latent_vector = np.load(f'/content/out_{i}/projected_w.npz')['w']
# # gan_images.append(img_gan)
# # latent_vectors.append(latent_vector)
# for i, cropped_img in enumerate(cropped_images):
# cv2.imwrite(f"cropped_{i}.png", cropped_img)
# cmd = f"python {project_root}/stylegan2-ada-pytorch/projector.py "\
# f"--save-video 0 --num-steps 1000 --outdir=out_{i} "\
# f"--target=cropped_{i}.png --network={NETWORK}"
# subprocess.run(cmd, shell=True, check=True)
# img_gan = cv2.imread(f'{project_root}/out_{i}/proj.png')
# img_rgb = cv2.cvtColor(img_gan, cv2.COLOR_BGR2RGB)
# plt.imshow(img_rgb)
# plt.title(f'gan-image-{i}')
# plt.show()
# latent_vector = np.load(f'{project_root}/out_{i}/projected_w.npz')['w']
# gan_images.append(img_gan)
# latent_vectors.append(latent_vector)
# # Create morph video with all images
# import torch
# import dnnlib
# import legacy
# import PIL.Image
# import numpy as np
# import imageio
# from tqdm.notebook import tqdm
# network_pkl = "https://nvlabs-fi-cdn.nvidia.com/stylegan2"\
# "-ada-pytorch/pretrained/ffhq.pkl"
# device = torch.device('cuda')
# with dnnlib.util.open_url(network_pkl) as fp:
# G = legacy.load_network_pkl(fp)['G_ema']\
# .requires_grad_(False).to(device)
# video = imageio.get_writer('/content/movie.mp4', mode='I', fps=FPS, codec='libx264', bitrate='16M')
# for idx in range(len(latent_vectors) - 1):
# lvec1 = latent_vectors[idx]
# lvec2 = latent_vectors[idx + 1]
# diff = lvec2 - lvec1
# step = diff / STEPS
# current = lvec1.copy()
# for j in tqdm(range(STEPS)):
# z = torch.from_numpy(current).to(device)
# synth_image = G.synthesis(z, noise_mode='const')
# synth_image = (synth_image + 1) * (255/2)
# synth_image = synth_image.permute(0, 2, 3, 1).clamp(0, 255)\
# .to(torch.uint8)[0].cpu().numpy()
# repeat = FREEZE_STEPS if j == 0 or j == (STEPS - 1) else 1
# for i in range(repeat):
# video.append_data(synth_image)
# current = current + step
# video.close()
# # With the following lines:
# output_file = "morph_movie.mp4"
# print(f"Generated output file: {output_file}")
# from google.colab import drive
# drive.mount('/content/drive')
# stylegan2 yielded better results than stylegan3 for feature vectors of selfies, so I'll use v2
# 150 imgs for imgs b/w 2 imgs uploaded
# 30 images @ beginning and end since otherwise it's just jumping sequences
# NETWORK = "https://nvlabs-fi-cdn.nvidia.com/"\
# "stylegan2-ada-pytorch/pretrained/ffhq.pkl"
# STEPS = 150
# FPS = 30
# FREEZE_STEPS = 30
# # HIDE OUTPUT
# import os
# from google.colab import files
# uploaded_files = files.upload()
# img_list = []
# for k, v in uploaded_files.items():
# _, ext = os.path.splitext(k)
# os.remove(k)
# image_name = f"{k}{ext}"
# open(image_name, 'wb').write(v)
# img_list.append(image_name)
# if len(img_list) < 2:
# print("Upload at least 2 images for morphing.")
# # HIDE OUTPUT
# # 5 facial landmark predictor - base of mouth and nose
# !wget http://dlib.net/files/shape_predictor_5_face_landmarks.dat.bz2
# !bzip2 -d shape_predictor_5_face_landmarks.dat.bz2
# # HIDE OUTPUT
# import sys
# !git clone https://github.com/NVlabs/stylegan2-ada-pytorch.git
# !pip install ninja
# sys.path.insert(0, "/content/stylegan2-ada-pytorch")
# import cv2
# import numpy as np
# from PIL import Image
# import dlib
# from matplotlib import pyplot as plt
# detector = dlib.get_frontal_face_detector()
# predictor = dlib.shape_predictor('shape_predictor_5_face_landmarks.dat')
# def crop_stylegan(img):
# dets = detector(img, 1)
# if len(dets) == 0:
# raise ValueError("No face detected")
# d = dets[0]
# shape = predictor(img, d)
# x1, y1 = shape.part(0).x, shape.part(0).y
# x2, y2 = shape.part(2).x, shape.part(2).y
# x3, y3 = shape.part(4).x, shape.part(4).y
# center = dlib.point((x1 + x2) // 2, (y1 + y2) // 2)
# width = np.linalg.norm(np.array([x1, y1]) - np.array([x3, y3]))
# size = int(width * 2.2)
# half_size = size // 2
# left, top = center.x - half_size, center.y - half_size
# right, bottom = left + size, top + size
# cropped_img = img[top:bottom, left:right]
# cropped_img = cv2.resize(cropped_img, (1024, 1024))
# return cropped_img
# def process_images(img_list):
# cropped_images = []
# for img_name in img_list:
# img = cv2.imread(img_name)
# if img is None:
# raise ValueError(f"{img_name} not found")
# cropped_img = crop_stylegan(img)
# cropped_images.append(cropped_img)
# img_rgb = cv2.cvtColor(cropped_img, cv2.COLOR_BGR2RGB)
# plt.imshow(img_rgb)
# plt.title(f'cropped {img_name}')
# plt.show()
# return cropped_images
# cropped_images = process_images(img_list)
# # Generate GAN images and latent vectors for each input image
# gan_images = []
# latent_vectors = []
# for i, cropped_img in enumerate(cropped_images):
# cv2.imwrite(f"cropped_{i}.png", cropped_img)
# # HIDE OUTPUT
# cmd = f"python /content/stylegan2-ada-pytorch/projector.py "\
# f"--save-video 0 --num-steps 1000 --outdir=out_{i} "\
# f"--target=cropped_{i}.png --network={NETWORK}"
# !{cmd}
# img_gan = cv2.imread(f'/content/out_{i}/proj.png')
# img_rgb = cv2.cvtColor(img_gan, cv2.COLOR_BGR2RGB)
# plt.imshow(img_rgb)
# plt.title(f'gan-image-{i}')
# plt.show()
# latent_vector = np.load(f'/content/out_{i}/projected_w.npz')['w']
# gan_images.append(img_gan)
# latent_vectors.append(latent_vector)
# # Create morph video with all images
# import torch
# import dnnlib
# import legacy
# import PIL.Image
# import numpy as np
# import imageio
# from tqdm.notebook import tqdm
# network_pkl = "https://nvlabs-fi-cdn.nvidia.com/stylegan2"\
# "-ada-pytorch/pretrained/ffhq.pkl"
# device = torch.device('cuda')
# with dnnlib.util.open_url(network_pkl) as fp:
# G = legacy.load_network_pkl(fp)['G_ema']\
# .requires_grad_(False).to(device)
# video = imageio.get_writer('/content/movie.mp4', mode='I', fps=FPS, codec='libx264', bitrate='16M')
# for idx in range(len(latent_vectors) - 1):
# lvec1 = latent_vectors[idx]
# lvec2 = latent_vectors[idx + 1]
# diff = lvec2 - lvec1
# step = diff / STEPS
# current = lvec1.copy()
# for j in tqdm(range(STEPS)):
# z = torch.from_numpy(current).to(device)
# synth_image = G.synthesis(z, noise_mode='const')
# synth_image = (synth_image + 1) * (255/2)
# synth_image = synth_image.permute(0, 2, 3, 1).clamp(0, 255)\
# .to(torch.uint8)[0].cpu().numpy()
# repeat = FREEZE_STEPS if j == 0 or j == (STEPS - 1) else 1
# for i in range(repeat):
# video.append_data(synth_image)
# current = current + step
# video.close()
# # HIDE OUTPUT
# from google.colab import files
# files.download("morph_movie.mp4")