-
Notifications
You must be signed in to change notification settings - Fork 356
Expand file tree
/
Copy pathdeepfacelive.py
More file actions
1261 lines (1005 loc) · 36.3 KB
/
deepfacelive.py
File metadata and controls
1261 lines (1005 loc) · 36.3 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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
import time
from dataclasses import dataclass
from typing import List, Tuple
from logging import getLogger
import numpy as np
import cv2
import ailia
# import original modules
sys.path.append("../../util")
from arg_utils import get_base_parser, update_parser, get_savepath # noqa
from model_utils import check_and_download_models # noqa
from detector_utils import load_image # noqa
from image_utils import normalize_image
from nms_utils import nms_boxes # noqa
from webcamera_utils import get_capture, get_writer # noqa
from util_math import *
from util_affine import *
from FLandmarks2D import ELandmarks2D, FLandmarks2D, face_ulmrks_cut
logger = getLogger(__name__)
# ======================
# Parameters
# ======================
WEIGHT_PATH = "generator.onnx"
MODEL_PATH = "generator.onnx.prototxt"
WEIGHT_YOLOV5FACE_PATH = "YoloV5Face.onnx"
MODEL_YOLOV5FACE_PATH = "YoloV5Face.onnx.prototxt"
WEIGHT_CENTERFACE_PATH = "CenterFace.onnx"
MODEL_CENTERFACE_PATH = "CenterFace.onnx.prototxt"
WEIGHT_S3FD_PATH = "S3FD.onnx"
MODEL_S3FD_PATH = "S3FD.onnx.prototxt"
WEIGHT_FACEMESH_PATH = "FaceMesh.onnx"
MODEL_FACEMESH_PATH = "FaceMesh.onnx.prototxt"
WEIGHT_INSIGHTFACE_PATH = "InsightFace2D106.onnx"
MODEL_INSIGHTFACE_PATH = "InsightFace2D106.onnx.prototxt"
REMOTE_PATH = "https://storage.googleapis.com/ailia-models/deepfacelive/"
IMAGE_DIR_PATH = "sample/"
SOURCE_PATH = "Kim_Chen_Yin.png"
SAVE_IMAGE_PATH = "output.png"
IMG_SIZE = 256
# ======================
# Arguemnt Parser Config
# ======================
parser = get_base_parser("DeepFaceLive", IMAGE_DIR_PATH, SAVE_IMAGE_PATH)
parser.add_argument("-src", "--source", default=SOURCE_PATH, help="source image")
parser.add_argument(
"--detector",
default="yolov5",
choices=("yolov5", "centerface", "s3fd"),
help="Face Detector",
)
parser.add_argument(
"--window_size",
type=int,
default=480,
help="Image size when detecting. Multiple of 32. 0 is auto.",
)
parser.add_argument(
"--threshold", type=float, default=0.5, help="face detection threshold. 0.0-1.0"
)
parser.add_argument("--max_faces", type=int, default=1, help="max faces")
parser.add_argument(
"--temporal_smoothing", type=int, default=1, help="temporal smoothing"
)
parser.add_argument(
"--marker",
default="facemesh",
choices=("facemesh", "insightface"),
help="Face Marker",
)
parser.add_argument(
"--marker_coverage", type=float, default=1.4, help="marker coverage"
)
parser.add_argument(
"--marker_temporal_smoothing", type=int, default=1, help="marker temporal smoothing"
)
parser.add_argument(
"--align_mode",
default="from_rect",
choices=("from_rect", "from_points"),
help="align mode",
)
parser.add_argument("--face_coverage", type=float, default=2.2, help="face coverage")
parser.add_argument(
"--resolution", type=int, default=224, help="resolution of aligned face"
)
parser.add_argument("--relative_power", type=float, default=1.0, help="relative power")
parser.add_argument("--onnx", action="store_true", help="execute onnxruntime version.")
args = update_parser(parser)
# ======================
# Class Definitions
# ======================
@dataclass
class FaceSwapInfo:
face_urect: np.ndarray = None
face_pose: np.ndarray = None
face_ulmrks: FLandmarks2D = None
face_resolution: int = None
face_align_image: np.ndarray = None
face_align_lmrks_mask: np.ndarray = None
face_swap_image: np.ndarray = None
face_align_ulmrks: FLandmarks2D = None
# ======================
# Secondaty Functions
# ======================
def setup_yolov5face(net):
def np_sigmoid(x: np.ndarray):
"""
sigmoid with safe check of overflow
"""
x = -x
c = x > np.log(np.finfo(x.dtype).max)
x[c] = 0.0
result = 1 / (1 + np.exp(x))
result[c] = 0.0
return result
def process_pred(pred, img_w, img_h, anchor):
pred_h = pred.shape[-3]
pred_w = pred.shape[-2]
anchor = np.float32(anchor)[None, :, None, None, :]
(
_xv,
_yv,
) = np.meshgrid(
np.arange(pred_w),
np.arange(pred_h),
)
grid = (
np.stack((_xv, _yv), 2)
.reshape((1, 1, pred_h, pred_w, 2))
.astype(np.float32)
)
stride = (img_w // pred_w, img_h // pred_h)
pred[..., [0, 1, 2, 3, 4]] = np_sigmoid(pred[..., [0, 1, 2, 3, 4]])
pred[..., 0:2] = (pred[..., 0:2] * 2 - 0.5 + grid) * stride
pred[..., 2:4] = (pred[..., 2:4] * 2) ** 2 * anchor
return pred
def predict(img):
N, C, H, W = img.shape
# feedforward
if not args.onnx:
output = net.predict([img])
else:
output = net.run(None, {"in": img})
# YoloV5Face returns 3x [N,C*16,H,W].
# C = [cx,cy,w,h,thres, 5*x,y of landmarks, cls_id ]
# Transpose and cut first 5 channels.
pred0, pred1, pred2 = [
pred.reshape((N, C, 16, pred.shape[-2], pred.shape[-1])).transpose(
0, 1, 3, 4, 2
)[..., 0:5]
for pred in output
]
pred0 = process_pred(pred0, W, H, anchor=[[4, 5], [8, 10], [13, 16]]).reshape(
(1, -1, 5)
)
pred1 = process_pred(
pred1, W, H, anchor=[[23, 29], [43, 55], [73, 105]]
).reshape((1, -1, 5))
pred2 = process_pred(
pred2, W, H, anchor=[[146, 217], [231, 300], [335, 433]]
).reshape((1, -1, 5))
preds = np.concatenate([pred0, pred1, pred2], 1)[..., :5]
return preds
def extract(
img,
threshold: float = 0.3,
fixed_window=0,
min_face_size=8,
augment=False,
):
"""
arguments
img np.ndarray ndim 2,3,4
fixed_window(0) int size
0 mean don't use
fit image in fixed window
downscale if bigger than window
pad if smaller than window
increases performance, but decreases accuracy
min_face_size(8)
augment(False) bool augment image to increase accuracy
decreases performance
returns a list of [l,t,r,b] for every batch dimension of img
"""
H, W, _ = img.shape
if H > 2048 or W > 2048:
fixed_window = 2048
img = img[None, ...]
if fixed_window != 0:
fixed_window = max(32, max(1, fixed_window // 32) * 32)
img, img_scale = fit_in(
img, fixed_window, fixed_window, pad_to_target=True, allow_upscale=False
)
else:
img = pad_to_next_divisor(img, 64, 64)
img_scale = 1.0
_, H, W, _ = img.shape
img = img / 255.0
feed_img = img.transpose(0, 3, 1, 2).astype(np.float32)
preds = predict(feed_img)
if augment:
feed_img = img[:, :, ::-1, :].transpose(0, 3, 1, 2)
rl_preds = predict(feed_img)
rl_preds[:, :, 0] = W - rl_preds[:, :, 0]
preds = np.concatenate([preds, rl_preds], 1)
faces_per_batch = []
for pred in preds:
pred = pred[pred[..., 4] >= threshold]
x, y, w, h, score = pred.T
l, t, r, b = x - w / 2, y - h / 2, x + w / 2, y + h / 2
boxes = np.stack((l, t, r, b), axis=1)
keep = nms_boxes(boxes, score, 0.5)
l, t, r, b = l[keep], t[keep], r[keep], b[keep]
faces = []
for l, t, r, b in np.stack([l, t, r, b], -1):
if img_scale != 1.0:
l, t, r, b = (
l / img_scale,
t / img_scale,
r / img_scale,
b / img_scale,
)
if min(r - l, b - t) < min_face_size:
continue
faces.append((l, t, r, b))
faces_per_batch.append(faces)
return faces_per_batch
return extract
def setup_centerface(net):
def refine(heatmap, offset, scale, h, w, threshold):
heatmap = heatmap[0]
scale0, scale1 = scale[0, :, :], scale[1, :, :]
offset0, offset1 = offset[0, :, :], offset[1, :, :]
c0, c1 = np.where(heatmap > threshold)
bboxlist = []
if len(c0) > 0:
for i in range(len(c0)):
s0, s1 = (
np.exp(scale0[c0[i], c1[i]]) * 4,
np.exp(scale1[c0[i], c1[i]]) * 4,
)
o0, o1 = offset0[c0[i], c1[i]], offset1[c0[i], c1[i]]
s = heatmap[c0[i], c1[i]]
x1, y1 = max(0, (c1[i] + o1 + 0.5) * 4 - s1 / 2), max(
0, (c0[i] + o0 + 0.5) * 4 - s0 / 2
)
x1, y1 = min(x1, w), min(y1, h)
bboxlist.append([x1, y1, min(x1 + s1, w), min(y1 + s0, h), s])
bboxlist = np.array(bboxlist, dtype=np.float32)
keep = nms_boxes(bboxlist[:, :4], bboxlist[:, 4], 0.3)
bboxlist = bboxlist[keep, :]
bboxlist = [x for x in bboxlist if x[-1] >= 0.5]
return bboxlist
def extract(
img,
threshold: float = 0.5,
fixed_window=0,
min_face_size=40,
):
H, W, _ = img.shape
img = img[None, ...]
if fixed_window != 0:
fixed_window = max(64, max(1, fixed_window // 32) * 32)
img, img_scale = fit_in(
img, fixed_window, fixed_window, pad_to_target=True, allow_upscale=False
)
else:
img = pad_to_next_divisor(img, 64, 64)
img_scale = 1.0
_, H, W, _ = img.shape
img = img[..., ::-1]
feed_img = img.transpose(0, 3, 1, 2).astype(np.float32)
# feedforward
if not args.onnx:
output = net.predict([feed_img])
else:
output = net.run(None, {"in": feed_img})
heatmaps, scales, offsets = output
faces_per_batch = []
for heatmap, offset, scale in zip(heatmaps, offsets, scales):
faces = []
for face in refine(heatmap, offset, scale, H, W, threshold):
l, t, r, b, c = face
if img_scale != 1.0:
l, t, r, b = (
l / img_scale,
t / img_scale,
r / img_scale,
b / img_scale,
)
bt = b - t
if min(r - l, bt) < min_face_size:
continue
b += bt * 0.1
faces.append((l, t, r, b))
faces_per_batch.append(faces)
return faces_per_batch
return extract
def setup_s3fd(net):
def refine(olist, threshold):
bboxlist = []
variances = [0.1, 0.2]
for i in range(len(olist) // 2):
ocls, oreg = olist[i * 2], olist[i * 2 + 1]
stride = 2 ** (i + 2) # 4,8,16,32,64,128
for hindex, windex in [*zip(*np.where(ocls[1, :, :] > threshold))]:
axc, ayc = stride / 2 + windex * stride, stride / 2 + hindex * stride
score = ocls[1, hindex, windex]
loc = np.ascontiguousarray(oreg[:, hindex, windex]).reshape((1, 4))
priors = np.array([[axc, ayc, stride * 4, stride * 4]])
bbox = np.concatenate(
(
priors[:, :2] + loc[:, :2] * variances[0] * priors[:, 2:],
priors[:, 2:] * np.exp(loc[:, 2:] * variances[1]),
),
1,
)
bbox[:, :2] -= bbox[:, 2:] / 2
bbox[:, 2:] += bbox[:, :2]
x1, y1, x2, y2 = bbox[0]
bboxlist.append([x1, y1, x2, y2, score])
if len(bboxlist) != 0:
bboxlist = np.array(bboxlist)
keep = nms_boxes(bboxlist[:, :4], bboxlist[:, 4], 0.3)
bboxlist = bboxlist[keep, :]
bboxlist = [x for x in bboxlist if x[-1] >= 0.5]
return bboxlist
def extract(
img,
threshold: float = 0.3,
fixed_window=0,
min_face_size=8,
):
img = img[None, ...]
if fixed_window != 0:
fixed_window = max(64, max(1, fixed_window // 32) * 32)
img, img_scale = fit_in(
img, fixed_window, fixed_window, pad_to_target=True, allow_upscale=False
)
else:
img = pad_to_next_divisor(img, 64, 64)
img_scale = 1.0
img = img - [104, 117, 123]
feed_img = img.transpose(0, 3, 1, 2).astype(np.float32)
# feedforward
if not args.onnx:
output = net.predict([feed_img])
else:
output = net.run(None, {"in": feed_img})
batches_bbox = output
faces_per_batch = []
for batch in range(img.shape[0]):
bbox = refine([x[batch] for x in batches_bbox], threshold)
faces = []
for l, t, r, b, c in bbox:
if img_scale != 1.0:
l, t, r, b = (
l / img_scale,
t / img_scale,
r / img_scale,
b / img_scale,
)
bt = b - t
if min(r - l, bt) < min_face_size:
continue
b += bt * 0.1
faces.append((l, t, r, b))
faces_per_batch.append(faces)
return faces_per_batch
return extract
def setup_google_facemesh(net):
input_height = 192
input_width = 192
def from_3D_468_landmarks(lmrks):
""" """
mat = np.empty((3, 3))
mat[0, :] = (lmrks[454] - lmrks[234]) / np.linalg.norm(lmrks[454] - lmrks[234])
mat[1, :] = (lmrks[152] - lmrks[6]) / np.linalg.norm(lmrks[152] - lmrks[6])
mat[2, :] = np.cross(mat[0, :], mat[1, :])
pitch, yaw, roll = rotation_matrix_to_euler(mat)
face_rect = np.array([pitch, yaw * 2, roll], np.float32)
return face_rect
def extract(img):
"""
arguments
img np.ndarray HW,HWC,NHWC uint8/float32
returns (N,468,3)
"""
H, W, _ = img.shape
h_scale = H / input_height
w_scale = W / input_width
img = resize(img, (input_width, input_height))
img = img / 255
img = np.expand_dims(img, axis=0)
img = img.astype(np.float32)
# feedforward
if not args.onnx:
output = net.predict([img])
else:
output = net.run(None, {"input_1": img})
lmrks = output[0]
lmrks = lmrks.reshape((-1, 468, 3))
lmrks *= (w_scale, h_scale, 1)
face_pose = from_3D_468_landmarks(lmrks[0])
return lmrks[0], face_pose
return extract
def setup_insightface_2d106(net):
input_height = 192
input_width = 192
def extract(img):
H, W, _ = img.shape
h_scale = H / input_height
w_scale = W / input_width
img = resize(img, (input_width, input_height))
img = img[..., ::-1]
img = img.transpose(2, 0, 1) # HWC -> CHW
img = np.expand_dims(img, axis=0)
img = img.astype(np.float32)
# feedforward
if not args.onnx:
output = net.predict([img])
else:
output = net.run(None, {"data": img})
lmrks = output[0]
lmrks = lmrks.reshape((1, 106, 2))
lmrks /= 2.0
lmrks += (0.5, 0.5)
lmrks *= (w_scale, h_scale)
lmrks *= (W, H)
return lmrks[0], None
return extract
def fit_in(
img, TW=None, TH=None, pad_to_target: bool = False, allow_upscale: bool = False
) -> float:
"""
fit image in w,h keeping aspect ratio
TW,TH int/None target width,height
pad_to_target bool pad remain area with zeros
allow_upscale bool if image smaller than TW,TH it will be upscaled
interpolation ImageProcessor.Interpolation. value
returns scale float value
"""
ndim = img.ndim
N, H, W, C = (1,) + img.shape if ndim == 3 else img.shape
if TW is not None and TH is None:
scale = TW / W
elif TW is None and TH is not None:
scale = TH / H
elif TW is not None and TH is not None:
SW = W / TW
SH = H / TH
scale = 1.0
if SW > 1.0 or SH > 1.0 or (SW < 1.0 and SH < 1.0):
scale /= max(SW, SH)
else:
raise ValueError("TW or TH should be specified")
if not allow_upscale and scale > 1.0:
scale = 1.0
if scale != 1.0:
img = resize(img, (int(W * scale), int(H * scale)))
if pad_to_target:
_, H, W, _ = (1,) + img.shape if ndim == 3 else img.shape
w_pad = (TW - W) if TW is not None else 0
h_pad = (TH - H) if TH is not None else 0
if w_pad != 0 or h_pad != 0:
if 3 < ndim:
img = np.pad(img, ((0, 0), (0, h_pad), (0, w_pad), (0, 0)))
else:
img = np.pad(img, ((0, h_pad), (0, w_pad), (0, 0)))
return img, scale
def resize(
img,
size: Tuple,
):
"""
resize to (W,H)
"""
ndim = img.ndim
N, H, W, C = (1,) + img.shape if ndim == 3 else img.shape
TW, TH = size
if W != TW or H != TH:
if 3 < ndim:
img = img.transpose((1, 2, 0, 3)).reshape((H, W, N * C))
img = cv2.resize(img, (TW, TH), interpolation=cv2.INTER_LINEAR)
if 3 < ndim:
H, W = img.shape[0:2]
img = img.reshape((H, W, N, C)).transpose((2, 0, 1, 3))
return img
def pad_to_next_divisor(img, dw=None, dh=None):
"""
pad image to next divisor of width/height
dw,dh int
"""
_, H, W, _ = img.shape
w_pad = 0
if dw is not None:
w_pad = W % dw
if w_pad != 0:
w_pad = dw - w_pad
h_pad = 0
if dh is not None:
h_pad = H % dh
if h_pad != 0:
h_pad = dh - h_pad
if w_pad != 0 or h_pad != 0:
img = np.pad(img, ((0, 0), (0, h_pad), (0, w_pad), (0, 0)))
return img
def as_4pts(pts, w_h=None) -> np.ndarray:
"""
get rect as 4 pts
0--3
| |
1--2
w_h(None) provide (w,h) to scale uniform rect to target size
returns np.ndarray (4,2) 4 pts with w,h
"""
if w_h is not None:
return pts * w_h
return pts.copy()
def sort_by_area_size(rects: List[np.ndarray]):
"""
sort list of FRect by largest area descend
"""
rects = [(rect, polygon_area(as_4pts(rect))) for rect in rects]
rects = sorted(rects, key=lambda x: x[1], reverse=True)
rects = [x[0] for x in rects]
return rects
def face_urect_cut(
fsi,
img: np.ndarray,
coverage: float,
output_size: int,
x_offset: float = 0,
y_offset: float = 0,
):
"""
Cut the face to square of output_size from img with given coverage using this rect
returns image,
uni_mat uniform matrix to transform uniform img space to uniform cutted space
"""
uni_rect = np.array(
[
[0.0, 0.0],
[0.0, 1.0],
[1.0, 1.0],
[1.0, 0.0],
],
dtype=np.float32,
)
# Face rect is not a square, also rect can be rotated
h, w = img.shape[0:2]
# Get scaled rect pts to target img
pts = as_4pts(fsi.face_urect, w_h=(w, h))
# Estimate transform from global space to local aligned space with bounds [0..1]
mat = umeyama(pts, uni_rect, True)
# get corner points in global space
g_p = transform_points(invert(mat), [(0, 0), (1, 0), (1, 1), (0, 1), (0.5, 0.5)])
g_c = g_p[4]
h_vec = (g_p[1] - g_p[0]).astype(np.float32)
v_vec = (g_p[3] - g_p[0]).astype(np.float32)
# calc diagonal vectors between corners in global space
tb_diag_vec = segment_to_vector(g_p[0], g_p[2]).astype(np.float32)
bt_diag_vec = segment_to_vector(g_p[3], g_p[1]).astype(np.float32)
mod = segment_length(g_p[0], g_p[4]) * coverage
g_c += h_vec * x_offset + v_vec * y_offset
l_t = np.array(
[g_c - tb_diag_vec * mod, g_c + bt_diag_vec * mod, g_c + tb_diag_vec * mod],
np.float32,
)
src_pts, dst_pts = l_t, np.float32(
((0, 0), (output_size, 0), (output_size, output_size))
)
mat = cv2.getAffineTransform(np.float32(src_pts), np.float32(dst_pts))
src_pts, dst_pts = (l_t / (w, h)).astype(np.float32), np.float32(
((0, 0), (1, 0), (1, 1))
)
uni_mat = cv2.getAffineTransform(np.float32(src_pts), np.float32(dst_pts))
face_image = cv2.warpAffine(img, mat, (output_size, output_size), cv2.INTER_CUBIC)
return face_image, uni_mat
def face_ulmrks_transform(face_ulmrks, mat, invert=False) -> "FLandmarks2D":
"""
Tranforms FLandmarks2D using affine mat and returns new FLandmarks2D()
mat : np.ndarray
"""
if invert:
mat = cv2.invertAffineTransform(mat)
ulmrks = face_ulmrks.ulmrks.copy()
ulmrks = np.expand_dims(ulmrks, axis=1)
ulmrks = cv2.transform(ulmrks, mat, ulmrks.shape).squeeze()
return FLandmarks2D(type=face_ulmrks.type, ulmrks=ulmrks)
def get_convexhull_mask(
face_align_ulmrks, h_w, color=(1,), dtype=np.float32
) -> np.ndarray:
""" """
h, w = h_w
ch = len(color)
lmrks = (face_align_ulmrks.ulmrks * h_w).astype(np.int32)
mask = np.zeros((h, w, ch), dtype=dtype)
cv2.fillConvexPoly(mask, cv2.convexHull(lmrks), color)
return mask
# ======================
# Main functions
# ======================
def face_detector(
models,
tar_img,
threshold=0.5,
fixed_window_size=448,
max_faces=1,
temporal_smoothing=1,
):
H, W, _ = tar_img.shape
extract = models["face_detector"]
rects = extract(
tar_img,
threshold=threshold,
fixed_window=fixed_window_size,
)
rects = rects[0]
# to list of FaceURect
rects = [
np.array([[l, t], [l, b], [r, b], [r, t]], np.float32)
for l, t, r, b in [[l / W, t / H, r / W, b / H] for l, t, r, b in rects]
]
rects = sort_by_area_size(rects)
fsi_list = []
if len(rects) != 0:
max_faces = max_faces
if max_faces != 0 and len(rects) > max_faces:
rects = rects[:max_faces]
if temporal_smoothing != 1:
if len(getattr(face_detector, "temporal_rects", [])) != len(rects):
face_detector.temporal_rects = [[] for _ in range(len(rects))]
for face_id, face_urect in enumerate(rects):
if temporal_smoothing != 1:
if len(face_detector.temporal_rects[face_id]) == 0:
face_detector.temporal_rects[face_id].append(as_4pts(face_urect))
face_detector.temporal_rects[face_id] = face_detector.temporal_rects[
face_id
][-temporal_smoothing:]
face_urect = np.mean(face_detector.temporal_rects[face_id], 0)
if polygon_area(face_urect) != 0:
fsi_list.append(FaceSwapInfo(face_urect=face_urect))
return fsi_list
def face_marker(models, frame_image, fsi_list, coverage=1.4, temporal_smoothing=1):
if temporal_smoothing != 1 and (
len(getattr(face_marker, "temporal_lmrks", [])) != len(fsi_list)
):
face_marker.temporal_lmrks = [[] for _ in range(len(fsi_list))]
for face_id, fsi in enumerate(fsi_list):
if fsi.face_urect is None:
continue
# Cut the face to feed to the face marker
face_image, face_uni_mat = face_urect_cut(fsi, frame_image, coverage, 192)
H, W, _ = face_image.shape
extract = models["face_marker"]
lmrks, face_pose = extract(face_image)
if temporal_smoothing != 1:
if len(face_marker.temporal_lmrks[face_id]) == 0:
face_marker.temporal_lmrks[face_id].append(lmrks)
face_marker.temporal_lmrks[face_id] = face_marker.temporal_lmrks[face_id][
-temporal_smoothing:
]
lmrks = np.mean(face_marker.temporal_lmrks[face_id], 0)
fsi.face_pose = face_pose
lmrks = lmrks[..., 0:2] / (W, H)
face_ulmrks = FLandmarks2D(
type=(
ELandmarks2D.L468
if lmrks.shape[0] == 468
else ELandmarks2D.L106 if lmrks.shape[0] == 106 else None
),
ulmrks=lmrks,
)
face_ulmrks = face_ulmrks_transform(face_ulmrks, face_uni_mat, invert=True)
fsi.face_ulmrks = face_ulmrks
return fsi_list
def face_aligner(
frame_image, fsi_list, align_mode="from_rect", coverage=2.2, resolution=256
):
exclude_moving_parts = True
head_mode = False
freeze_z_rotation = False
x_offset = y_offset = 0.0
for _, fsi in enumerate(fsi_list):
if fsi.face_ulmrks is None:
continue
head_yaw = None
face_ulmrks = fsi.face_ulmrks
fsi.face_resolution = resolution
if align_mode == "from_rect":
face_align_img, uni_mat = face_urect_cut(
fsi,
frame_image,
coverage=coverage,
output_size=resolution,
x_offset=x_offset,
y_offset=y_offset,
)
elif align_mode == "from_points":
face_align_img, uni_mat = face_ulmrks_cut(
fsi.face_ulmrks,
frame_image,
coverage + (1.0 if head_mode else 0.0),
resolution,
exclude_moving_parts=exclude_moving_parts,
head_yaw=head_yaw,
x_offset=x_offset,
y_offset=y_offset - 0.08 + (-0.50 if head_mode else 0.0),
freeze_z_rotation=freeze_z_rotation,
)
fsi.face_align_ulmrks = face_ulmrks_transform(face_ulmrks, uni_mat)
fsi.face_align_image = face_align_img
# Due to FaceAligner is not well loaded, we can make lmrks mask here
face_align_lmrks_mask_img = get_convexhull_mask(
fsi.face_align_ulmrks,
face_align_img.shape[:2],
color=(255,),
dtype=np.uint8,
)
fsi.face_align_lmrks_mask = face_align_lmrks_mask_img
return fsi_list
def face_animator(models, src_img, fsi_list, relative_power=1.0):
animator_face_id = 0
for i, fsi in enumerate(fsi_list):
if animator_face_id == i:
if fsi.face_align_image is None:
continue
face_align_image = fsi.face_align_image
H, W, _ = face_align_image.shape
net = models["net"]
if getattr(face_animator, "driving_ref_motion", None) is None:
face_animator.driving_ref_motion = extract_motion(net, face_align_image)
anim_image = generate(
net,
src_img,
face_align_image,
face_animator.driving_ref_motion,
power=relative_power,
)
anim_image = resize(anim_image, (W, H))
fsi.face_swap_image = anim_image
break
return fsi_list
def extract_motion(net, img: np.ndarray):
in_src = np.zeros((1, 3, IMG_SIZE, IMG_SIZE), np.float32)
feed_img = normalize_image(
resize(img, (IMG_SIZE, IMG_SIZE))[..., ::-1], normalize_type="127.5"
)
feed_img = feed_img.transpose(2, 0, 1) # HWC -> CHW
feed_img = np.expand_dims(feed_img, axis=0)
feed_img = feed_img.astype(np.float32)
in_drv_start_motion = np.zeros((1, 20), np.float32)
in_power = np.zeros((1,), np.float32)
# feedforward
if not args.onnx:
output = net.predict([in_src, feed_img, in_drv_start_motion, in_power])
else:
output = net.run(
None,
{
"in_src": in_src,
"in_drv": feed_img,
"in_drv_start_motion": in_drv_start_motion,
"in_power": in_power,
},
)
out_drv_motion = output[1]
return out_drv_motion
def generate(
net,
img_source: np.ndarray,
img_driver: np.ndarray,
driver_start_motion: np.ndarray,
power,
):
H, W, _ = img_source.shape
in_src = normalize_image(
resize(img_source, (IMG_SIZE, IMG_SIZE))[..., :3][..., ::-1],
normalize_type="127.5",
)
in_src = in_src.transpose(2, 0, 1) # HWC -> CHW
in_src = np.expand_dims(in_src, axis=0)
in_src = in_src.astype(np.float32)
in_drv = normalize_image(
resize(img_driver, (IMG_SIZE, IMG_SIZE))[..., ::-1], normalize_type="127.5"
)
in_drv = in_drv.transpose(2, 0, 1) # HWC -> CHW
in_drv = np.expand_dims(in_drv, axis=0)
in_drv = in_drv.astype(np.float32)