Spaces:
No application file
No application file
culture
commited on
Commit
·
25a346f
1
Parent(s):
a15dca5
Delete scripts/parse_landmark.py
Browse files- scripts/parse_landmark.py +0 -85
scripts/parse_landmark.py
DELETED
@@ -1,85 +0,0 @@
|
|
1 |
-
import cv2
|
2 |
-
import json
|
3 |
-
import numpy as np
|
4 |
-
import os
|
5 |
-
import torch
|
6 |
-
from basicsr.utils import FileClient, imfrombytes
|
7 |
-
from collections import OrderedDict
|
8 |
-
|
9 |
-
# ---------------------------- This script is used to parse facial landmarks ------------------------------------- #
|
10 |
-
# Configurations
|
11 |
-
save_img = False
|
12 |
-
scale = 0.5 # 0.5 for official FFHQ (512x512), 1 for others
|
13 |
-
enlarge_ratio = 1.4 # only for eyes
|
14 |
-
json_path = 'ffhq-dataset-v2.json'
|
15 |
-
face_path = 'datasets/ffhq/ffhq_512.lmdb'
|
16 |
-
save_path = './FFHQ_eye_mouth_landmarks_512.pth'
|
17 |
-
|
18 |
-
print('Load JSON metadata...')
|
19 |
-
# use the official json file in FFHQ dataset
|
20 |
-
with open(json_path, 'rb') as f:
|
21 |
-
json_data = json.load(f, object_pairs_hook=OrderedDict)
|
22 |
-
|
23 |
-
print('Open LMDB file...')
|
24 |
-
# read ffhq images
|
25 |
-
file_client = FileClient('lmdb', db_paths=face_path)
|
26 |
-
with open(os.path.join(face_path, 'meta_info.txt')) as fin:
|
27 |
-
paths = [line.split('.')[0] for line in fin]
|
28 |
-
|
29 |
-
save_dict = {}
|
30 |
-
|
31 |
-
for item_idx, item in enumerate(json_data.values()):
|
32 |
-
print(f'\r{item_idx} / {len(json_data)}, {item["image"]["file_path"]} ', end='', flush=True)
|
33 |
-
|
34 |
-
# parse landmarks
|
35 |
-
lm = np.array(item['image']['face_landmarks'])
|
36 |
-
lm = lm * scale
|
37 |
-
|
38 |
-
item_dict = {}
|
39 |
-
# get image
|
40 |
-
if save_img:
|
41 |
-
img_bytes = file_client.get(paths[item_idx])
|
42 |
-
img = imfrombytes(img_bytes, float32=True)
|
43 |
-
|
44 |
-
# get landmarks for each component
|
45 |
-
map_left_eye = list(range(36, 42))
|
46 |
-
map_right_eye = list(range(42, 48))
|
47 |
-
map_mouth = list(range(48, 68))
|
48 |
-
|
49 |
-
# eye_left
|
50 |
-
mean_left_eye = np.mean(lm[map_left_eye], 0) # (x, y)
|
51 |
-
half_len_left_eye = np.max((np.max(np.max(lm[map_left_eye], 0) - np.min(lm[map_left_eye], 0)) / 2, 16))
|
52 |
-
item_dict['left_eye'] = [mean_left_eye[0], mean_left_eye[1], half_len_left_eye]
|
53 |
-
# mean_left_eye[0] = 512 - mean_left_eye[0] # for testing flip
|
54 |
-
half_len_left_eye *= enlarge_ratio
|
55 |
-
loc_left_eye = np.hstack((mean_left_eye - half_len_left_eye + 1, mean_left_eye + half_len_left_eye)).astype(int)
|
56 |
-
if save_img:
|
57 |
-
eye_left_img = img[loc_left_eye[1]:loc_left_eye[3], loc_left_eye[0]:loc_left_eye[2], :]
|
58 |
-
cv2.imwrite(f'tmp/{item_idx:08d}_eye_left.png', eye_left_img * 255)
|
59 |
-
|
60 |
-
# eye_right
|
61 |
-
mean_right_eye = np.mean(lm[map_right_eye], 0)
|
62 |
-
half_len_right_eye = np.max((np.max(np.max(lm[map_right_eye], 0) - np.min(lm[map_right_eye], 0)) / 2, 16))
|
63 |
-
item_dict['right_eye'] = [mean_right_eye[0], mean_right_eye[1], half_len_right_eye]
|
64 |
-
# mean_right_eye[0] = 512 - mean_right_eye[0] # # for testing flip
|
65 |
-
half_len_right_eye *= enlarge_ratio
|
66 |
-
loc_right_eye = np.hstack(
|
67 |
-
(mean_right_eye - half_len_right_eye + 1, mean_right_eye + half_len_right_eye)).astype(int)
|
68 |
-
if save_img:
|
69 |
-
eye_right_img = img[loc_right_eye[1]:loc_right_eye[3], loc_right_eye[0]:loc_right_eye[2], :]
|
70 |
-
cv2.imwrite(f'tmp/{item_idx:08d}_eye_right.png', eye_right_img * 255)
|
71 |
-
|
72 |
-
# mouth
|
73 |
-
mean_mouth = np.mean(lm[map_mouth], 0)
|
74 |
-
half_len_mouth = np.max((np.max(np.max(lm[map_mouth], 0) - np.min(lm[map_mouth], 0)) / 2, 16))
|
75 |
-
item_dict['mouth'] = [mean_mouth[0], mean_mouth[1], half_len_mouth]
|
76 |
-
# mean_mouth[0] = 512 - mean_mouth[0] # for testing flip
|
77 |
-
loc_mouth = np.hstack((mean_mouth - half_len_mouth + 1, mean_mouth + half_len_mouth)).astype(int)
|
78 |
-
if save_img:
|
79 |
-
mouth_img = img[loc_mouth[1]:loc_mouth[3], loc_mouth[0]:loc_mouth[2], :]
|
80 |
-
cv2.imwrite(f'tmp/{item_idx:08d}_mouth.png', mouth_img * 255)
|
81 |
-
|
82 |
-
save_dict[f'{item_idx:08d}'] = item_dict
|
83 |
-
|
84 |
-
print('Save...')
|
85 |
-
torch.save(save_dict, save_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|