Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
597 lines (487 loc) · 23.1 KB
/
Copy pathutils.py
File metadata and controls
597 lines (487 loc) · 23.1 KB
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# Copyright 2024-2025 The Alibaba Wan Team Authors. All rights reserved.
import os
import cv2
import math
import random
import numpy as np
def resize_face_crop(face_image, size=512):
"""Direction-aware resize for the face-encoder crop (fixes a real bug).
Every call site previously did a bare ``cv2.resize(face_image, (512, 512))``
with no interpolation flag, which defaults to INTER_LINEAR for BOTH
directions. For any shot where the face occupies less than 512px in-frame
(the common half-/full-body case), that is an UPSCALE using the softest
available filter, actively blurring the fine detail (wrinkles, eye
creases) that carries a microexpression before it ever reaches the face
encoder. Use INTER_AREA for genuine downscales (correct anti-aliasing) and
INTER_LANCZOS4 for upscales (sharpest upsample cv2 offers).
"""
h, w = face_image.shape[:2]
interp = cv2.INTER_AREA if (h > size or w > size) else cv2.INTER_LANCZOS4
return cv2.resize(face_image, (size, size), interpolation=interp)
def get_mask_boxes(mask):
y_coords, x_coords = np.nonzero(mask)
x_min = x_coords.min()
x_max = x_coords.max()
y_min = y_coords.min()
y_max = y_coords.max()
bbox = np.array([x_min, y_min, x_max, y_max]).astype(np.int32)
return bbox
def get_aug_mask(body_mask, w_len=10, h_len=20):
body_bbox = get_mask_boxes(body_mask)
bbox_wh = body_bbox[2:4] - body_bbox[0:2]
w_slice = np.int32(bbox_wh[0] / w_len)
h_slice = np.int32(bbox_wh[1] / h_len)
for each_w in range(body_bbox[0], body_bbox[2], w_slice):
w_start = min(each_w, body_bbox[2])
w_end = min((each_w + w_slice), body_bbox[2])
for each_h in range(body_bbox[1], body_bbox[3], h_slice):
h_start = min(each_h, body_bbox[3])
h_end = min((each_h + h_slice), body_bbox[3])
if body_mask[h_start:h_end, w_start:w_end].sum() > 0:
body_mask[h_start:h_end, w_start:w_end] = 1
return body_mask
def get_mask_body_img(img_copy, hand_mask, k=7, iterations=1):
kernel = np.ones((k, k), np.uint8)
dilation = cv2.dilate(hand_mask, kernel, iterations=iterations)
mask_hand_img = img_copy * (1 - dilation[:, :, None])
return mask_hand_img, dilation
# NOTE: this 4-arg get_face_bboxes is DEAD CODE — a second def of the same
# name at line ~310 (3-arg, no ratio_aug) shadows it at module scope, and the
# one live call site (nodes.py) uses the 3-arg form. Nothing can reach this
# version under this name. Left in place (not deleted) since removing it is
# out of scope here; do not "fix" its bugs expecting it to run anywhere.
def get_face_bboxes(kp2ds, scale, image_shape, ratio_aug):
h, w = image_shape
kp2ds_face = kp2ds.copy()[23:91, :2]
min_x, min_y = np.min(kp2ds_face, axis=0)
max_x, max_y = np.max(kp2ds_face, axis=0)
initial_width = max_x - min_x
initial_height = max_y - min_y
initial_area = initial_width * initial_height
expanded_area = initial_area * scale
new_width = np.sqrt(expanded_area * (initial_width / initial_height))
new_height = np.sqrt(expanded_area * (initial_height / initial_width))
delta_width = (new_width - initial_width) / 2
delta_height = (new_height - initial_height) / 4
if ratio_aug:
if random.random() > 0.5:
delta_width += random.uniform(0, initial_width // 10)
else:
delta_height += random.uniform(0, initial_height // 10)
expanded_min_x = max(min_x - delta_width, 0)
expanded_max_x = min(max_x + delta_width, w)
expanded_min_y = max(min_y - 3 * delta_height, 0)
expanded_max_y = min(max_y + delta_height, h)
return [int(expanded_min_x), int(expanded_max_x), int(expanded_min_y), int(expanded_max_y)]
def calculate_new_size(orig_w, orig_h, target_area, divisor=64):
target_ratio = orig_w / orig_h
def check_valid(w, h):
if w <= 0 or h <= 0:
return False
return (w * h <= target_area and
w % divisor == 0 and
h % divisor == 0)
def get_ratio_diff(w, h):
return abs(w / h - target_ratio)
def round_to_64(value, round_up=False, divisor=64):
if round_up:
return divisor * ((value + (divisor - 1)) // divisor)
return divisor * (value // divisor)
possible_sizes = []
max_area_h = int(np.sqrt(target_area / target_ratio))
max_area_w = int(max_area_h * target_ratio)
max_h = round_to_64(max_area_h, round_up=True, divisor=divisor)
max_w = round_to_64(max_area_w, round_up=True, divisor=divisor)
for h in range(divisor, max_h + divisor, divisor):
ideal_w = h * target_ratio
w_down = round_to_64(ideal_w)
w_up = round_to_64(ideal_w, round_up=True)
for w in [w_down, w_up]:
if check_valid(w, h, divisor):
possible_sizes.append((w, h, get_ratio_diff(w, h)))
if not possible_sizes:
raise ValueError("Can not find suitable size")
possible_sizes.sort(key=lambda x: (-x[0] * x[1], x[2]))
best_w, best_h, _ = possible_sizes[0]
return int(best_w), int(best_h)
def resize_by_area(image, target_area, keep_aspect_ratio=True, divisor=64, padding_color=(0, 0, 0)):
h, w = image.shape[:2]
try:
new_w, new_h = calculate_new_size(w, h, target_area, divisor)
except:
aspect_ratio = w / h
if keep_aspect_ratio:
new_h = math.sqrt(target_area / aspect_ratio)
new_w = target_area / new_h
else:
new_w = new_h = math.sqrt(target_area)
new_w, new_h = int((new_w // divisor) * divisor), int((new_h // divisor) * divisor)
interpolation = cv2.INTER_AREA if (new_w * new_h < w * h) else cv2.INTER_LINEAR
resized_image = padding_resize(image, height=new_h, width=new_w, padding_color=padding_color,
interpolation=interpolation)
return resized_image
def padding_resize(img_ori, height=512, width=512, padding_color=(0, 0, 0), interpolation=cv2.INTER_LINEAR):
ori_height = img_ori.shape[0]
ori_width = img_ori.shape[1]
channel = img_ori.shape[2]
img_pad = np.zeros((height, width, channel), dtype=img_ori.dtype)
if channel == 1:
img_pad[:, :, 0] = padding_color[0]
else:
img_pad[:, :, 0] = padding_color[0]
img_pad[:, :, 1] = padding_color[1]
img_pad[:, :, 2] = padding_color[2]
if (ori_height / ori_width) > (height / width):
new_width = int(height / ori_height * ori_width)
img = cv2.resize(img_ori, (new_width, height), interpolation=interpolation)
padding = int((width - new_width) / 2)
if len(img.shape) == 2:
img = img[:, :, np.newaxis]
img_pad[:, padding: padding + new_width, :] = img
else:
new_height = int(width / ori_width * ori_height)
img = cv2.resize(img_ori, (width, new_height), interpolation=interpolation)
padding = int((height - new_height) / 2)
if len(img.shape) == 2:
img = img[:, :, np.newaxis]
img_pad[padding: padding + new_height, :, :] = img
return img_pad
def resize_to_bounds(img_ori, height=512, width=512, padding_color=(0, 0, 0), interpolation=cv2.INTER_LINEAR, extra_padding=64, crop_target_image=None):
# Find non-black pixel bounds
if crop_target_image is not None:
ref = crop_target_image
if ref.ndim == 2:
mask = ref > 0
else:
mask = np.any(ref != 0, axis=2)
coords = np.argwhere(mask)
if coords.size == 0:
# All black, fallback to full image
y0, x0 = 0, 0
y1, x1 = img_ori.shape[0], img_ori.shape[1]
else:
y0, x0 = coords.min(axis=0)
y1, x1 = coords.max(axis=0) + 1
# Intended crop bounds with padding
pad_y0 = y0 - extra_padding
pad_x0 = x0 - extra_padding
pad_y1 = y1 + extra_padding
pad_x1 = x1 + extra_padding
# Actual crop bounds clipped to image
crop_y0 = max(pad_y0, 0)
crop_x0 = max(pad_x0, 0)
crop_y1 = min(pad_y1, img_ori.shape[0])
crop_x1 = min(pad_x1, img_ori.shape[1])
crop_img = img_ori[crop_y0:crop_y1, crop_x0:crop_x1]
# Pad if needed
pad_top = crop_y0 - pad_y0
pad_left = crop_x0 - pad_x0
pad_bottom = pad_y1 - crop_y1
pad_right = pad_x1 - crop_x1
if any([pad_top, pad_left, pad_bottom, pad_right]):
channel = crop_img.shape[2] if crop_img.ndim == 3 else 1
crop_img = np.pad(
crop_img,
((pad_top, pad_bottom), (pad_left, pad_right)) + ((0, 0),) if channel > 1 else ((pad_top, pad_bottom), (pad_left, pad_right)),
mode='constant', constant_values=0
)
else:
if img_ori.ndim == 2:
mask = img_ori > 0
else:
mask = np.any(img_ori != 0, axis=2)
coords = np.argwhere(mask)
if coords.size == 0:
# All black, fallback to original
crop_img = img_ori
else:
y0, x0 = coords.min(axis=0)
y1, x1 = coords.max(axis=0) + 1
pad_y0 = y0 - extra_padding
pad_x0 = x0 - extra_padding
pad_y1 = y1 + extra_padding
pad_x1 = x1 + extra_padding
crop_y0 = max(pad_y0, 0)
crop_x0 = max(pad_x0, 0)
crop_y1 = min(pad_y1, img_ori.shape[0])
crop_x1 = min(pad_x1, img_ori.shape[1])
crop_img = img_ori[crop_y0:crop_y1, crop_x0:crop_x1]
pad_top = crop_y0 - pad_y0
pad_left = crop_x0 - pad_x0
pad_bottom = pad_y1 - crop_y1
pad_right = pad_x1 - crop_x1
if any([pad_top, pad_left, pad_bottom, pad_right]):
channel = crop_img.shape[2] if crop_img.ndim == 3 else 1
crop_img = np.pad(
crop_img,
((pad_top, pad_bottom), (pad_left, pad_right)) + ((0, 0),) if channel > 1 else ((pad_top, pad_bottom), (pad_left, pad_right)),
mode='constant', constant_values=0
)
ori_height = crop_img.shape[0]
ori_width = crop_img.shape[1]
channel = crop_img.shape[2] if crop_img.ndim == 3 else 1
img_pad = np.zeros((height, width, channel), dtype=crop_img.dtype)
if channel == 1:
img_pad[:, :, 0] = padding_color[0]
else:
for c in range(channel):
img_pad[:, :, c] = padding_color[c % len(padding_color)]
# Resize cropped image to fit target size, preserving aspect ratio
crop_aspect = ori_width / ori_height
target_aspect = width / height
if crop_aspect > target_aspect:
new_width = width
new_height = int(width / crop_aspect)
else:
new_height = height
new_width = int(height * crop_aspect)
img = cv2.resize(crop_img, (new_width, new_height), interpolation=interpolation)
if img.ndim == 2:
img = img[:, :, np.newaxis]
y_pad = (height - new_height) // 2
x_pad = (width - new_width) // 2
img_pad[y_pad:y_pad + new_height, x_pad:x_pad + new_width, :] = img
return img_pad
def get_frame_indices(frame_num, video_fps, clip_length, train_fps):
start_frame = 0
times = np.arange(0, clip_length) / train_fps
frame_indices = start_frame + np.round(times * video_fps).astype(int)
frame_indices = np.clip(frame_indices, 0, frame_num - 1)
return frame_indices.tolist()
def get_face_bboxes(kp2ds, scale, image_shape):
h, w = image_shape
kp2ds_face = kp2ds.copy()[1:] * (w, h)
min_x, min_y = np.min(kp2ds_face, axis=0)
max_x, max_y = np.max(kp2ds_face, axis=0)
initial_width = max_x - min_x
initial_height = max_y - min_y
if initial_width < 2 or initial_height < 2:
# Degenerate detection (all landmarks collapsed). Dividing by a
# zero-width face used to NaN and crash the node. Frame-centred
# placeholder; the caller already replaces x2<=x1 / y2<=y1 boxes.
side = min(h, w, 128)
return [int((w - side) // 2), int((w + side) // 2),
int((h - side) // 2), int((h + side) // 2)]
initial_area = initial_width * initial_height
expanded_area = initial_area * scale
new_width = np.sqrt(expanded_area * (initial_width / initial_height))
new_height = np.sqrt(expanded_area * (initial_height / initial_width))
delta_width = (new_width - initial_width) / 2
# Bug-fix (Wan-Animate spec 1.2): was `/ 4` here with the vertical room
# split 3:1 upward:downward below (3x toward the forehead/hair, 1x toward
# the chin). Mouth-corner pull, jaw tension, and chin dimpling are core
# microexpression carriers and were getting clipped tighter than the
# upper face. `/ 2` (matching delta_width) + applying it EVENLY above
# and below (both `1.4 *`, verified by unit test to land at an exact 1:1
# up:down ratio) gives the chin/jaw as much room as the forehead, instead
# of the old 3:1 forehead bias.
delta_height = (new_height - initial_height) / 2
expanded_min_x = max(min_x - delta_width, 0)
expanded_max_x = min(max_x + delta_width, w)
expanded_min_y = max(min_y - 1.4 * delta_height, 0)
expanded_max_y = min(max_y + 1.4 * delta_height, h)
return [int(expanded_min_x), int(expanded_max_x), int(expanded_min_y), int(expanded_max_y)]
# Source-pixel floor for face_images. Below this, Lanczos to 512 invents
# most of the pixels (128px → 4.0x; 46px → 11x). On a WIDE shot the
# detected face can be 46px; expanding the crop to 128px around the same
# centre is the paper's own scale-augmentation (in-distribution) and
# matches Kijai's wide-shot table (128–192). Close-ups already sit well
# above this and are left face-tight.
_SOURCE_FACE_MIN_PX = 128
def get_face_bboxes_central(kp2ds, scale, image_shape):
"""Retired ``central_face`` crop — now the same box as ``get_face_bboxes``.
The HunyuanPortrait eyebrow-to-mouth crop was the 46×46 failure: on a
wide 832×480 plate it halved the already-small face, then Lanczos to
512 invented 99% of the pixels (eyeballs ~4px). Wan-Animate was trained
on the FULL 68-point box (``process_pipepline.py``, scale=1.3). Saved
workflows that still say ``central_face`` must not keep shipping that
undersampled tile, so this is a straight alias.
"""
return get_face_bboxes(kp2ds, scale, image_shape)
# ============================================================
# Wan-Animate paper-driven face preprocessing helpers
# ============================================================
# These helpers implement the four levers identified in the
# Wan-Animate paper (arXiv:2509.14055) for improving gaze
# reenactment fidelity:
# 1. Eye-centred face crop (place eyes in upper third)
# 2. Stabilised crop position across frames
# 3. CFG on face conditioning (passthrough output, see nodes.py)
# 4. Quality gating (blur / brightness on the eye region)
def adjust_bbox_eye_upper_third(face_bbox, eye_xy_pixel, frame_w, frame_h,
eye_y_fraction=0.30):
"""Vertically shift a face bbox so eye landmarks fall at ``eye_y_fraction``
of the crop height (measured from the top).
Args:
face_bbox: (x1, x2, y1, y2) in pixel space.
eye_xy_pixel: (eye_cx, eye_cy) midpoint of both eye centres in
full-frame pixel space.
frame_w, frame_h: full frame dimensions.
eye_y_fraction: target normalised eye row inside the crop
(default 0.30 = upper third).
Returns:
New (x1, x2, y1, y2) bbox of identical width/height, vertically
shifted (and clamped to image bounds). Width/height never change.
"""
x1, x2, y1, y2 = face_bbox
crop_h = int(y2 - y1)
if crop_h <= 0 or eye_xy_pixel is None:
return (int(x1), int(x2), int(y1), int(y2))
_eye_cx, eye_cy = eye_xy_pixel
desired_y1 = float(eye_cy) - float(eye_y_fraction) * float(crop_h)
new_y1 = int(np.clip(desired_y1, 0, max(0, frame_h - crop_h)))
new_y2 = new_y1 + crop_h
return (int(x1), int(x2), int(new_y1), int(new_y2))
def apply_eye_offset_to_center(center_xy, eye_xy_pixel, crop_size, frame_h,
eye_y_fraction=0.30):
"""Like ``adjust_bbox_eye_upper_third`` but operates on a crop CENTER
point instead of a finalised (x1,x2,y1,y2) bbox, so the eye-offset can be
folded into the target-center TRAJECTORY before temporal smoothing runs
(Wan-Animate spec 1.3), instead of shifting an already-smoothed bbox
afterward with a fresh, unsmoothed per-frame eye-landmark read.
Mathematically equivalent to applying ``adjust_bbox_eye_upper_third`` to
the eventual (x1,x2,y1,y2) bbox at this crop_size: a bbox's y-center is
``y1 + crop_size/2``, so shifting the center the same way the bbox's y1
would have shifted lands the eyes at the same ``eye_y_fraction`` row.
Args:
center_xy: (cx, cy) crop center in full-frame pixel space.
eye_xy_pixel: (eye_cx, eye_cy) midpoint of both eye centres in
full-frame pixel space, or None.
crop_size: the (square) crop's pixel height at this frame.
frame_h: full frame height.
eye_y_fraction: target normalised eye row inside the crop.
Returns:
(cx, new_cy) — x is never touched, only the vertical position.
"""
cx, cy = center_xy
if crop_size is None or crop_size <= 0 or eye_xy_pixel is None:
return (float(cx), float(cy))
_eye_cx, eye_cy = eye_xy_pixel
desired_y1 = float(eye_cy) - float(eye_y_fraction) * float(crop_size)
new_y1 = float(np.clip(desired_y1, 0.0, max(0.0, float(frame_h) - float(crop_size))))
return (float(cx), new_y1 + float(crop_size) / 2.0)
def compute_eye_midpoint_from_face_kps(face_kps_norm, frame_w, frame_h):
"""Return the pixel-space midpoint of both eyes from a normalised
dlib-68 face landmark array (the layout used by
``pose_metas[i]['keypoints_face']``).
Right-eye contour: dlib 36-41 -> array indices 37-42.
Left-eye contour: dlib 42-47 -> array indices 43-48.
Index 0 is the body-anchored face anchor (skipped).
Returns:
(eye_cx, eye_cy) in pixel space, or ``None`` if landmarks
are missing/invalid.
"""
if face_kps_norm is None:
return None
arr = np.asarray(face_kps_norm)
if arr.ndim != 2 or arr.shape[0] < 49 or arr.shape[1] < 2:
return None
eye_idx = list(range(37, 49))
eye_pts = arr[eye_idx, :2]
if not np.isfinite(eye_pts).all():
return None
if arr.shape[1] >= 3:
conf = arr[eye_idx, 2]
if float(np.mean(conf)) < 0.05:
return None
eye_norm = np.mean(eye_pts, axis=0)
return (float(eye_norm[0]) * float(frame_w),
float(eye_norm[1]) * float(frame_h))
def compute_frame_blur_score(frame_rgb):
"""Laplacian variance -- higher = sharper.
Args:
frame_rgb: (H, W, 3) array, float32 [0,1] or uint8.
Returns:
float Laplacian variance.
"""
arr = frame_rgb
if arr.dtype != np.uint8:
arr = (np.clip(arr, 0.0, 1.0) * 255.0).astype(np.uint8)
if arr.ndim == 3:
gray = cv2.cvtColor(arr, cv2.COLOR_RGB2GRAY)
else:
gray = arr
return float(cv2.Laplacian(gray, cv2.CV_64F).var())
def compute_eye_region_brightness(frame_rgb, top_frac=0.30, bottom_frac=0.55):
"""Mean luma of a horizontal strip across the eye region of a face crop.
With eye-upper-third alignment, eyes fall around 25-35% of the crop
height. A strip from ``top_frac`` to ``bottom_frac`` covers the eye
sockets safely.
Returns:
Mean luma in [0, 1].
"""
arr = frame_rgb
if arr.dtype == np.uint8:
arr = arr.astype(np.float32) / 255.0
arr = np.clip(arr, 0.0, 1.0)
h = arr.shape[0]
y0 = int(max(0, top_frac * h))
y1 = int(min(h, bottom_frac * h))
if y1 <= y0:
return 0.0
strip = arr[y0:y1]
if strip.ndim == 3:
luma = 0.2126 * strip[..., 0] + 0.7152 * strip[..., 1] + 0.0722 * strip[..., 2]
else:
luma = strip
return float(np.mean(luma))
def _eye_line_similarity(src_l, src_r, dst_l, dst_r):
"""2D similarity transform (uniform scale + rotation + translation)
mapping the SRC eye-corner pair onto the DST eye-corner pair.
Returns a callable ``f(points) -> transformed_points``. Uses ONLY the
two outer eye corners as the rigid anchor — deliberately NOT the chin or
mouth corners (which move with expression and would partially cancel out
the very signal ``amplify_landmarks_from_neutral`` below wants to keep).
This corrects in-plane head roll and apparent scale (distance changes);
it does NOT undo out-of-plane yaw/pitch — an honest partial correction,
not a full 3D head-pose normalization.
"""
src_l = np.asarray(src_l, dtype=np.float64)
src_r = np.asarray(src_r, dtype=np.float64)
dst_l = np.asarray(dst_l, dtype=np.float64)
dst_r = np.asarray(dst_r, dtype=np.float64)
src_vec = src_r - src_l
dst_vec = dst_r - dst_l
src_len = np.linalg.norm(src_vec)
dst_len = np.linalg.norm(dst_vec)
if src_len < 1e-6 or dst_len < 1e-6:
return lambda pts: np.asarray(pts, dtype=np.float64).copy()
scale = dst_len / src_len
src_ang = math.atan2(src_vec[1], src_vec[0])
dst_ang = math.atan2(dst_vec[1], dst_vec[0])
theta = dst_ang - src_ang
c, s = math.cos(theta), math.sin(theta)
R = np.array([[c, -s], [s, c]], dtype=np.float64)
src_mid = (src_l + src_r) / 2.0
dst_mid = (dst_l + dst_r) / 2.0
def transform(points):
pts = np.asarray(points, dtype=np.float64)
return (pts - src_mid) @ (scale * R).T + dst_mid
return transform
def amplify_landmarks_from_neutral(cur_pts, neutral_pts, cur_eye_l, cur_eye_r,
neutral_eye_l, neutral_eye_r, amplify=1.0):
"""Wan-Animate spec 2.3: push each CURRENT landmark a bit further along
the direction it ALREADY moved from the neutral reference — amplifying
real, detected motion so more of it survives the face encoder's
fixed-capacity motion-basis compression. Never synthesizes new motion.
Args:
cur_pts, neutral_pts: (N,2) landmark arrays in the SAME crop-local
pixel coordinate system (e.g. both from a 512x512 face crop).
cur_eye_l/r, neutral_eye_l/r: outer eye-corner anchors (see
``_eye_line_similarity``) used to rigid-align neutral onto the
current frame's head pose before taking the displacement, so the
amplified delta is expression motion, not head motion.
amplify: >1.0 pushes further from neutral; 1.0 = no-op (returns
cur_pts unchanged).
Returns:
(N,2) amplified landmark array in the current frame's coordinates —
feed this as ``dst_lms`` to ``_face_warp.warp_face`` with ``cur_pts``
as ``src_lms`` to move the REAL pixels there.
"""
cur_pts = np.asarray(cur_pts, dtype=np.float64)
if amplify <= 1.001:
return cur_pts.copy()
align = _eye_line_similarity(neutral_eye_l, neutral_eye_r, cur_eye_l, cur_eye_r)
neutral_aligned = align(neutral_pts)
displacement = cur_pts - neutral_aligned
return cur_pts + displacement * (float(amplify) - 1.0)