-
Notifications
You must be signed in to change notification settings - Fork 1
/
warp.py
166 lines (139 loc) · 6.12 KB
/
warp.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
# -*- coding:utf-8 -*-
import os
import sys
import cv2
import numpy as np
from utils import tqdm
# from ...sphereface.matlab_cp2tform import get_similarity_transform_for_cv2
submodule_path = os.path.abspath('../../sphereface')
sys.path.append(submodule_path)
from matlab_cp2tform import get_similarity_transform_for_cv2
# 这里注意x代表的是横轴,y代表的是纵轴
imgSize = np.array([128, 128])
coord5points = {
'p': np.array([
(30.2946, 51.6963,), # Left eye
(65.5318, 51.5014,), # Right eye
(48.0252, 71.7366,), # Nose tip
(33.5493, 92.3655,), # Mouth left corner
(62.7299, 92.2041,), # Mouth right corner
]) + np.array([16, 8]), # [16, 8] is an offset, and i forget why i add it, just leave it alone
'c': np.array([
(30.2946, 51.6963,), # Left eye
(65.5318, 51.5014,), # Right eye
(48.5851, 79.0356,), # Nose tip
(31.4217, 98.5210,), # Mouth left corner
(66.2410, 98.4563,), # Mouth right corner
]) + np.array([16, 8]), # [16, 8] is an offset, and i forget why i add it, just leave it alone
}
imgSize *= 4
for key, value in coord5points.items():
coord5points[key] = value * 4
# change imgSize to tuple
imgSize = tuple(map(int, imgSize))
def calc_eye_point(landmark, is_right_eye=0):
offset = is_right_eye * 2
t = np.array([
landmark[8+offset],
landmark[9+offset],
])
return t.mean(axis=0)
def get_img5point(landmark):
return np.array([
calc_eye_point(landmark, is_right_eye=0), # Left eye
calc_eye_point(landmark, is_right_eye=1), # Right eye
landmark[12], # Nose tip
landmark[14], # Mouth left corner
landmark[16], # Mouth right corner
])
def warp_img(img, landmark, image_type):
img5point = get_img5point(landmark)
img = img.astype(np.uint8)
M = cv2.estimateRigidTransform(img5point, coord5points[image_type], fullAffine=True)
img = cv2.warpAffine(img, M, imgSize)
landmark = np.array(landmark)
landmark = np.concatenate((
landmark,
np.ones((landmark.shape[0], 1))
), axis=1)
M = np.concatenate((
M,
np.array(((0, 0, 1),)),
), axis=0)
landmark = np.dot(landmark, M.T)[:, :2]
landmark = [tuple(x) for x in landmark]
return img, landmark
def alignment(src_img, src_landmark, resize_factor=2):
offset = 2
ref_pts = [
[30.2946+offset, 51.6963+offset],
[65.5318+offset, 51.5014+offset],
[48.0252+offset, 71.7366+offset],
[33.5493+offset, 92.3655+offset],
[62.7299+offset, 92.2041+offset],
]
crop_size = (96+offset*2, 112+offset*2)
src_pts = get_img5point(src_landmark) # float64
src_pts = np.array(src_pts).reshape(5, 2)
s = np.array(src_pts).astype(np.float32)
r = np.array(ref_pts).astype(np.float32) * resize_factor
crop_size = (crop_size[0]*resize_factor, crop_size[1]*resize_factor)
tfm = get_similarity_transform_for_cv2(s, r) # (2,3)
face_img = cv2.warpAffine(src_img, tfm, crop_size) # 仿射变换,crop_size为变换后的大小,tfm是仿射矩阵
# face_img (232,200,3)
dst_landmark = np.array(src_landmark)
dst_landmark = np.concatenate((
dst_landmark,
np.ones((dst_landmark.shape[0], 1))
), axis=1) # (17,3)
tfm = np.concatenate((
tfm,
np.array(((0, 0, 1),)), # (1,3)
), axis=0) # (3,3)
dst_landmark = np.dot(dst_landmark, tfm.T)[:, :2]
dst_landmark = [tuple(x) for x in dst_landmark]
return face_img, dst_landmark
def generate_dataset_face_frontalization():
import config
from datas import get_dirs, get_image, get_overview
from utils import dataset_iterator, perpare_dataset_dir, im_str_to_np
# perpare_dataset_dir
new_dataset_names = ['frontalization_dataset']
new_dataset_dirs = []
for new_dataset_name in new_dataset_names:
new_dataset_dirs.append(perpare_dataset_dir(new_dataset_name, __file__))
new_images_dir = os.path.join(new_dataset_dirs[0], config.WC_original_images_dir_name)
new_landmarks_dir = os.path.join(new_dataset_dirs[0], config.WC_landmarks_dir_name)
# face_frontalization
for people_name, image_type, image_name, landmark in tqdm(dataset_iterator(config.WC_original_dataset_name)):
im_str = get_image(config.WC_original_dataset_name, people_name, image_name, show_landmark=0)
im = im_str_to_np(im_str)
im, landmark = alignment(im, landmark) # duiqi(Affine Transformation)
people_name = people_name.replace('_', ' ')
file_dir = os.path.join(new_images_dir, people_name)
os.makedirs(file_dir, exist_ok=True)
# os.chdir(file_dir)
if not cv2.imwrite(os.path.join(file_dir, image_name+'.jpg'), im):
assert 0, "cv2.imwrite failed"
file_dir = os.path.join(new_landmarks_dir, people_name)
os.makedirs(file_dir, exist_ok=True)
file_path = os.path.join(file_dir, image_name+'.txt')
with open(file_path, 'w') as file:
for ld in landmark:
file.write('%f %f\n' % ld)
# 转正却没有剔除
if __name__ == '__main__':
# from . import config
# from .utils import dataset_iterator, get_image
# def imshow(im):
# import matplotlib.pyplot as plt
# plt.imshow(im[:, :, ::-1])
# plt.show()
# dataset_name = config.WC_original_dataset_name
# for people_name, image_type, image_name, landmark in dataset_iterator(dataset_name):
# im_str = get_image(dataset_name, people_name, image_name, show_landmark=0)
# im = np.fromstring(im_str, np.uint8)
# im = cv2.imdecode(im, cv2.IMREAD_COLOR)
# im = warp_img(im, landmark)
# imshow(im)
generate_dataset_face_frontalization()