Ii
commited on
Delete face_align.py.txt
Browse files- face_align.py.txt +0 -141
face_align.py.txt
DELETED
|
@@ -1,141 +0,0 @@
|
|
| 1 |
-
import cv2
|
| 2 |
-
import numpy as np
|
| 3 |
-
from skimage import transform as trans
|
| 4 |
-
|
| 5 |
-
src1 = np.array([[51.642, 50.115], [57.617, 49.990], [35.740, 69.007],
|
| 6 |
-
[51.157, 89.050], [57.025, 89.702]],
|
| 7 |
-
dtype=np.float32)
|
| 8 |
-
#<--left
|
| 9 |
-
src2 = np.array([[45.031, 50.118], [65.568, 50.872], [39.677, 68.111],
|
| 10 |
-
[45.177, 86.190], [64.246, 86.758]],
|
| 11 |
-
dtype=np.float32)
|
| 12 |
-
|
| 13 |
-
#---frontal
|
| 14 |
-
src3 = np.array([[39.730, 51.138], [72.270, 51.138], [56.000, 68.493],
|
| 15 |
-
[42.463, 87.010], [69.537, 87.010]],
|
| 16 |
-
dtype=np.float32)
|
| 17 |
-
|
| 18 |
-
#-->right
|
| 19 |
-
src4 = np.array([[46.845, 50.872], [67.382, 50.118], [72.737, 68.111],
|
| 20 |
-
[48.167, 86.758], [67.236, 86.190]],
|
| 21 |
-
dtype=np.float32)
|
| 22 |
-
|
| 23 |
-
#-->right profile
|
| 24 |
-
src5 = np.array([[54.796, 49.990], [60.771, 50.115], [76.673, 69.007],
|
| 25 |
-
[55.388, 89.702], [61.257, 89.050]],
|
| 26 |
-
dtype=np.float32)
|
| 27 |
-
|
| 28 |
-
src = np.array([src1, src2, src3, src4, src5])
|
| 29 |
-
src_map = {112: src, 224: src * 2}
|
| 30 |
-
|
| 31 |
-
arcface_src = np.array(
|
| 32 |
-
[[38.2946, 51.6963], [73.5318, 51.5014], [56.0252, 71.7366],
|
| 33 |
-
[41.5493, 92.3655], [70.7299, 92.2041]],
|
| 34 |
-
dtype=np.float32)
|
| 35 |
-
|
| 36 |
-
arcface_src = np.expand_dims(arcface_src, axis=0)
|
| 37 |
-
|
| 38 |
-
# In[66]:
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
# lmk is prediction; src is template
|
| 42 |
-
def estimate_norm(lmk, image_size=112, mode='arcface'):
|
| 43 |
-
assert lmk.shape == (5, 2)
|
| 44 |
-
tform = trans.SimilarityTransform()
|
| 45 |
-
lmk_tran = np.insert(lmk, 2, values=np.ones(5), axis=1)
|
| 46 |
-
min_M = []
|
| 47 |
-
min_index = []
|
| 48 |
-
min_error = float('inf')
|
| 49 |
-
if mode == 'arcface':
|
| 50 |
-
if image_size == 112:
|
| 51 |
-
src = arcface_src
|
| 52 |
-
else:
|
| 53 |
-
src = float(image_size) / 112 * arcface_src
|
| 54 |
-
else:
|
| 55 |
-
src = src_map[image_size]
|
| 56 |
-
for i in np.arange(src.shape[0]):
|
| 57 |
-
tform.estimate(lmk, src[i])
|
| 58 |
-
M = tform.params[0:2, :]
|
| 59 |
-
results = np.dot(M, lmk_tran.T)
|
| 60 |
-
results = results.T
|
| 61 |
-
error = np.sum(np.sqrt(np.sum((results - src[i])**2, axis=1)))
|
| 62 |
-
# print(error)
|
| 63 |
-
if error < min_error:
|
| 64 |
-
min_error = error
|
| 65 |
-
min_M = M
|
| 66 |
-
min_index = i
|
| 67 |
-
return min_M, min_index
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
def norm_crop(img, landmark, image_size=112, mode='arcface'):
|
| 71 |
-
M, pose_index = estimate_norm(landmark, image_size, mode)
|
| 72 |
-
warped = cv2.warpAffine(img, M, (image_size, image_size), borderValue=0.0)
|
| 73 |
-
return warped
|
| 74 |
-
|
| 75 |
-
def square_crop(im, S):
|
| 76 |
-
if im.shape[0] > im.shape[1]:
|
| 77 |
-
height = S
|
| 78 |
-
width = int(float(im.shape[1]) / im.shape[0] * S)
|
| 79 |
-
scale = float(S) / im.shape[0]
|
| 80 |
-
else:
|
| 81 |
-
width = S
|
| 82 |
-
height = int(float(im.shape[0]) / im.shape[1] * S)
|
| 83 |
-
scale = float(S) / im.shape[1]
|
| 84 |
-
resized_im = cv2.resize(im, (width, height))
|
| 85 |
-
det_im = np.zeros((S, S, 3), dtype=np.uint8)
|
| 86 |
-
det_im[:resized_im.shape[0], :resized_im.shape[1], :] = resized_im
|
| 87 |
-
return det_im, scale
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
def transform(data, center, output_size, scale, rotation):
|
| 91 |
-
scale_ratio = scale
|
| 92 |
-
rot = float(rotation) * np.pi / 180.0
|
| 93 |
-
#translation = (output_size/2-center[0]*scale_ratio, output_size/2-center[1]*scale_ratio)
|
| 94 |
-
t1 = trans.SimilarityTransform(scale=scale_ratio)
|
| 95 |
-
cx = center[0] * scale_ratio
|
| 96 |
-
cy = center[1] * scale_ratio
|
| 97 |
-
t2 = trans.SimilarityTransform(translation=(-1 * cx, -1 * cy))
|
| 98 |
-
t3 = trans.SimilarityTransform(rotation=rot)
|
| 99 |
-
t4 = trans.SimilarityTransform(translation=(output_size / 2,
|
| 100 |
-
output_size / 2))
|
| 101 |
-
t = t1 + t2 + t3 + t4
|
| 102 |
-
M = t.params[0:2]
|
| 103 |
-
cropped = cv2.warpAffine(data,
|
| 104 |
-
M, (output_size, output_size),
|
| 105 |
-
borderValue=0.0)
|
| 106 |
-
return cropped, M
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
def trans_points2d(pts, M):
|
| 110 |
-
new_pts = np.zeros(shape=pts.shape, dtype=np.float32)
|
| 111 |
-
for i in range(pts.shape[0]):
|
| 112 |
-
pt = pts[i]
|
| 113 |
-
new_pt = np.array([pt[0], pt[1], 1.], dtype=np.float32)
|
| 114 |
-
new_pt = np.dot(M, new_pt)
|
| 115 |
-
#print('new_pt', new_pt.shape, new_pt)
|
| 116 |
-
new_pts[i] = new_pt[0:2]
|
| 117 |
-
|
| 118 |
-
return new_pts
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
def trans_points3d(pts, M):
|
| 122 |
-
scale = np.sqrt(M[0][0] * M[0][0] + M[0][1] * M[0][1])
|
| 123 |
-
#print(scale)
|
| 124 |
-
new_pts = np.zeros(shape=pts.shape, dtype=np.float32)
|
| 125 |
-
for i in range(pts.shape[0]):
|
| 126 |
-
pt = pts[i]
|
| 127 |
-
new_pt = np.array([pt[0], pt[1], 1.], dtype=np.float32)
|
| 128 |
-
new_pt = np.dot(M, new_pt)
|
| 129 |
-
#print('new_pt', new_pt.shape, new_pt)
|
| 130 |
-
new_pts[i][0:2] = new_pt[0:2]
|
| 131 |
-
new_pts[i][2] = pts[i][2] * scale
|
| 132 |
-
|
| 133 |
-
return new_pts
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
def trans_points(pts, M):
|
| 137 |
-
if pts.shape[1] == 2:
|
| 138 |
-
return trans_points2d(pts, M)
|
| 139 |
-
else:
|
| 140 |
-
return trans_points3d(pts, M)
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|