Spaces:
Runtime error
Runtime error
File size: 11,336 Bytes
53bfa33 | 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 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | import argparse
import math
import os
from enum import IntEnum
from pathlib import Path
import cv2
import face_alignment
import numpy as np
import numpy.linalg as npla
from PIL import Image
from diffusers.utils import load_image
landmarks_2D_new = np.array(
[
[0.000213256, 0.106454], # 17
[0.0752622, 0.038915], # 18
[0.18113, 0.0187482], # 19
[0.29077, 0.0344891], # 20
[0.393397, 0.0773906], # 21
[0.586856, 0.0773906], # 22
[0.689483, 0.0344891], # 23
[0.799124, 0.0187482], # 24
[0.904991, 0.038915], # 25
[0.98004, 0.106454], # 26
[0.490127, 0.203352], # 27
[0.490127, 0.307009], # 28
[0.490127, 0.409805], # 29
[0.490127, 0.515625], # 30
[0.36688, 0.587326], # 31
[0.426036, 0.609345], # 32
[0.490127, 0.628106], # 33
[0.554217, 0.609345], # 34
[0.613373, 0.587326], # 35
[0.121737, 0.216423], # 36
[0.187122, 0.178758], # 37
[0.265825, 0.179852], # 38
[0.334606, 0.231733], # 39
[0.260918, 0.245099], # 40
[0.182743, 0.244077], # 41
[0.645647, 0.231733], # 42
[0.714428, 0.179852], # 43
[0.793132, 0.178758], # 44
[0.858516, 0.216423], # 45
[0.79751, 0.244077], # 46
[0.719335, 0.245099], # 47
[0.254149, 0.780233], # 48
[0.726104, 0.780233], # 54
],
dtype=np.float32,
)
class FaceType(IntEnum):
# enumerating in order "next contains prev"
HALF = 0
MID_FULL = 1
FULL = 2
FULL_NO_ALIGN = 3
WHOLE_FACE = 4
WHOLE_FACE_NO_ALIGN = 5
HEAD = 10
HEAD_NO_ALIGN = 20
MARK_ONLY = (100,) # no align at all, just embedded faceinfo
@staticmethod
def fromString(s):
r = from_string_dict.get(s.lower())
if r is None:
raise Exception("FaceType.fromString value error")
return r
@staticmethod
def toString(face_type):
return to_string_dict[face_type]
to_string_dict = {
FaceType.HALF: "half_face",
FaceType.MID_FULL: "midfull_face",
FaceType.FULL: "full_face",
FaceType.FULL_NO_ALIGN: "full_face_no_align",
FaceType.WHOLE_FACE: "whole_face",
FaceType.WHOLE_FACE_NO_ALIGN: "whole_face_no_align",
FaceType.HEAD: "head",
FaceType.HEAD_NO_ALIGN: "head_no_align",
FaceType.MARK_ONLY: "mark_only",
}
from_string_dict = {to_string_dict[x]: x for x in to_string_dict.keys()}
FaceType_to_padding_remove_align = {
FaceType.HALF: (0.0, False),
FaceType.MID_FULL: (0.0675, False),
FaceType.FULL: (0.2109375, False),
FaceType.FULL_NO_ALIGN: (0.2109375, True),
FaceType.WHOLE_FACE: (0.40, False),
FaceType.WHOLE_FACE_NO_ALIGN: (0.40, True),
FaceType.HEAD: (0.70, False),
FaceType.HEAD_NO_ALIGN: (0.70, True),
}
def umeyama(src, dst, estimate_scale):
"""Estimate N-D similarity transformation with or without scaling.
Parameters
----------
src : (M, N) array
Source coordinates.
dst : (M, N) array
Destination coordinates.
estimate_scale : bool
Whether to estimate scaling factor.
Returns
-------
T : (N + 1, N + 1)
The homogeneous similarity transformation matrix. The matrix contains
NaN values only if the problem is not well-conditioned.
References
----------
.. [1] "Least-squares estimation of transformation parameters between two
point patterns", Shinji Umeyama, PAMI 1991, DOI: 10.1109/34.88573
"""
num = src.shape[0]
dim = src.shape[1]
# Compute mean of src and dst.
src_mean = src.mean(axis=0)
dst_mean = dst.mean(axis=0)
# Subtract mean from src and dst.
src_demean = src - src_mean
dst_demean = dst - dst_mean
# Eq. (38).
A = np.dot(dst_demean.T, src_demean) / num
# Eq. (39).
d = np.ones((dim,), dtype=np.double)
if np.linalg.det(A) < 0:
d[dim - 1] = -1
T = np.eye(dim + 1, dtype=np.double)
U, S, V = np.linalg.svd(A)
# Eq. (40) and (43).
rank = np.linalg.matrix_rank(A)
if rank == 0:
return np.nan * T
elif rank == dim - 1:
if np.linalg.det(U) * np.linalg.det(V) > 0:
T[:dim, :dim] = np.dot(U, V)
else:
s = d[dim - 1]
d[dim - 1] = -1
T[:dim, :dim] = np.dot(U, np.dot(np.diag(d), V))
d[dim - 1] = s
else:
T[:dim, :dim] = np.dot(U, np.dot(np.diag(d), V))
if estimate_scale:
# Eq. (41) and (42).
scale = 1.0 / src_demean.var(axis=0).sum() * np.dot(S, d)
else:
scale = 1.0
T[:dim, dim] = dst_mean - scale * np.dot(T[:dim, :dim], src_mean.T)
T[:dim, :dim] *= scale
return T
def transform_points(points, mat, invert=False):
if invert:
mat = cv2.invertAffineTransform(mat)
points = np.expand_dims(points, axis=1)
points = cv2.transform(points, mat, points.shape)
points = np.squeeze(points)
return points
def estimate_averaged_yaw(landmarks):
# Works much better than solvePnP if landmarks from "3DFAN"
if not isinstance(landmarks, np.ndarray):
landmarks = np.array(landmarks)
l = (
(landmarks[27][0] - landmarks[0][0])
+ (landmarks[28][0] - landmarks[1][0])
+ (landmarks[29][0] - landmarks[2][0])
) / 3.0
r = (
(landmarks[16][0] - landmarks[27][0])
+ (landmarks[15][0] - landmarks[28][0])
+ (landmarks[14][0] - landmarks[29][0])
) / 3.0
return float(r - l)
def polygon_area(x, y):
return 0.5 * np.abs(np.dot(x, np.roll(y, 1)) - np.dot(y, np.roll(x, 1)))
def get_transform_mat(image_landmarks, output_size, face_type, scale=1.0):
if not isinstance(image_landmarks, np.ndarray):
image_landmarks = np.array(image_landmarks)
# estimate landmarks transform from global space to local aligned space with bounds [0..1]
mat = umeyama(
np.concatenate([image_landmarks[17:49], image_landmarks[54:55]]),
landmarks_2D_new,
True,
)[0:2]
# get corner points in global space
g_p = transform_points(
np.float32([(0, 0), (1, 0), (1, 1), (0, 1), (0.5, 0.5)]), mat, True
)
g_c = g_p[4]
# calc diagonal vectors between corners in global space
tb_diag_vec = (g_p[2] - g_p[0]).astype(np.float32)
tb_diag_vec /= npla.norm(tb_diag_vec)
bt_diag_vec = (g_p[1] - g_p[3]).astype(np.float32)
bt_diag_vec /= npla.norm(bt_diag_vec)
# calc modifier of diagonal vectors for scale and padding value
padding, remove_align = FaceType_to_padding_remove_align.get(face_type, 0.0)
mod = (1.0 / scale) * (npla.norm(g_p[0] - g_p[2]) * (padding * np.sqrt(2.0) + 0.5))
if face_type == FaceType.WHOLE_FACE:
# adjust vertical offset for WHOLE_FACE, 7% below in order to cover more forehead
vec = (g_p[0] - g_p[3]).astype(np.float32)
vec_len = npla.norm(vec)
vec /= vec_len
g_c += vec * vec_len * 0.07
elif face_type == FaceType.HEAD:
# assuming image_landmarks are 3D_Landmarks extracted for HEAD,
# adjust horizontal offset according to estimated yaw
yaw = estimate_averaged_yaw(transform_points(image_landmarks, mat, False))
hvec = (g_p[0] - g_p[1]).astype(np.float32)
hvec_len = npla.norm(hvec)
hvec /= hvec_len
yaw *= np.abs(math.tanh(yaw * 2)) # Damp near zero
g_c -= hvec * (yaw * hvec_len / 2.0)
# adjust vertical offset for HEAD, 50% below
vvec = (g_p[0] - g_p[3]).astype(np.float32)
vvec_len = npla.norm(vvec)
vvec /= vvec_len
g_c += vvec * vvec_len * 0.50
# calc 3 points in global space to estimate 2d affine transform
if not remove_align:
l_t = np.array(
[g_c - tb_diag_vec * mod, g_c + bt_diag_vec * mod, g_c + tb_diag_vec * mod]
)
else:
# remove_align - face will be centered in the frame but not aligned
l_t = np.array(
[
g_c - tb_diag_vec * mod,
g_c + bt_diag_vec * mod,
g_c + tb_diag_vec * mod,
g_c - bt_diag_vec * mod,
]
)
# get area of face square in global space
area = polygon_area(l_t[:, 0], l_t[:, 1])
# calc side of square
side = np.float32(math.sqrt(area) / 2)
# calc 3 points with unrotated square
l_t = np.array([g_c + [-side, -side], g_c + [side, -side], g_c + [side, side]])
# calc affine transform from 3 global space points to 3 local space points size of 'output_size'
pts2 = np.float32(((0, 0), (output_size, 0), (output_size, output_size)))
l_t = l_t.astype(np.float32)
mat = cv2.getAffineTransform(l_t, pts2)
return mat
def extract_faces(model, image, face_image_size, face_type=FaceType.WHOLE_FACE):
# take the first three channels (R, G, B)
array = np.array(image)[:, :, :3]
preds = model.get_landmarks(array)
face_images = []
image_to_face_matrices = []
for face_landmarks in preds:
image_to_face_mat = get_transform_mat(
face_landmarks, face_image_size, face_type
)
face_array = cv2.warpAffine(
array,
image_to_face_mat,
(face_image_size, face_image_size),
cv2.INTER_LANCZOS4,
borderValue=(255, 255, 255),
)
image_to_face_matrices.append(image_to_face_mat)
face_images.append(Image.fromarray(face_array))
return face_images, image_to_face_matrices
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--image_path",
type=str,
default="",
help="Path to input image",
)
parser.add_argument(
"--image_size",
type=int,
default=256,
help="Output image size",
)
parser.add_argument(
"--output_folder",
type=str,
default="./face_images",
help="Path to output folder",
)
parser.add_argument(
"--face_type",
type=str,
default="whole_face",
help=(
"Face type to extract (e.g., half_face, midfull_face, full_face, "
"full_face_no_align, whole_face, whole_face_no_align, head, "
"head_no_align, mark_only.)"
),
)
args = parser.parse_args()
# Convert face_type string to FaceType enum
try:
args.face_type = FaceType.fromString(args.face_type)
except Exception:
raise ValueError(f"Invalid face_type: {args.face_type}")
return args
if __name__ == "__main__":
args = parse_args()
# sfd for SFD, dlib for Dlib and folder for existing bounding boxes.
fa = face_alignment.FaceAlignment(
face_alignment.LandmarksType.TWO_D, face_detector="sfd"
)
pil_image = load_image(args.image_path)
face_images, image_to_face_matrices = extract_faces(
fa, pil_image, args.image_size, args.face_type
)
# Make sure the output folder exists
os.makedirs(args.output_folder, exist_ok=True)
input_filename = Path(args.image_path).stem
for i, face_image in enumerate(face_images):
face_image.save(Path(args.output_folder, f"{input_filename}_{i:02}.png"))
|