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