Skip to content

Commit f047739

Browse files
Merge pull request #4 from dimitribarbot/add-retina-face-detector
Add retina face detector
2 parents 9afde87 + 1a5db8e commit f047739

File tree

11 files changed

+713
-19
lines changed

11 files changed

+713
-19
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Pickle files have all been converted to safetensors by Kijai. If necessary, they
4848
### Face detectors
4949
For human mode, you can either use the original default [Insightface](https://github.com/deepinsight/insightface), or [Google's MediaPipe](https://github.com/google-ai-edge/mediapipe), or [Face Alignment](https://github.com/1adrianb/face-alignment) (see [Settings](#settings) section above or [API](#api) section below).
5050

51-
Biggest difference is the license: Insightface is strictly for NON-COMMERCIAL use. MediaPipe is a bit worse at detection, and can't run on GPU in Windows, though it's much faster on CPU compared to Insightface. Face Alignment can use blazeface back camera model (or SFD), it's far better for smaller faces than MediaPipe, that only can use the blazeface short model. The warmup on the first run when using this can take a long time, but subsequent runs are quick.
51+
Biggest difference is the license: Insightface is strictly for NON-COMMERCIAL use. MediaPipe is a bit worse at detection, and can't run on GPU in Windows, though it's much faster on CPU compared to Insightface. Face Alignment can use blazeface back camera model (or SFD or RetinaFace), it's far better for smaller faces than MediaPipe, that only can use the blazeface short model. The warmup on the first run when using this can take a long time, but subsequent runs are quick.
5252

5353
Insightface models go here (automatically downloaded if the folder is not present during first run): `stable-diffusion-webui/models/insightface/models/buffalo_l`. If necessary, they can be downloaded from: https://github.com/deepinsight/insightface/releases/download/v0.7/buffalo_l.zip.
5454

@@ -73,7 +73,7 @@ Parameters are the same as LivePortrait ones (see output of command `python infe
7373
- `save_output`: `true` if you want output videos to be saved in `output_dir` (as in LivePortrait), `false` otherwise.
7474
- `use_model_cache`: `true` if you want live portrait and face detector models to be cached for subsequent calls using same models, `false` otherwise.
7575
- `human_face_detector`: `insightface`, `mediapipe` or `facealignment`. Face detector to be used by human inference. Default to the `Human face detector` UI setting if defined or `insightface` if not set neither in settings nor in endpoint body.
76-
- `face_alignment_detector`: `blazeface`, `blazeface_back_camera` or `sfd`. Face detector to be used by human inference when Face Alignment is selected as `human_face_detector`. Default to the `Face alignment detector` UI setting if defined or `blazeface_back_camera` if not set neither in settings nor in endpoint body.
76+
- `face_alignment_detector`: `blazeface`, `blazeface_back_camera`, `retinaface` or `sfd`. Face detector to be used by human inference when Face Alignment is selected as `human_face_detector`. Default to the `Face alignment detector` UI setting if defined or `blazeface_back_camera` if not set neither in settings nor in endpoint body.
7777
- `face_alignment_detector_device`: `cuda`, `cpu` or `mps`. Device to be used by face detector when Face Alignment is selected as `human_face_detector`. Default to `cuda`.
7878
- `face_alignment_detector_dtype`: `fp16`, `bf16` or `fp32`. Device type to be used by face detector when Face Alignment is selected as `human_face_detector`. Default to `fp16`.
7979
- `source_face_index`: Face index (0-based) to be cropped in the source image or video if `flag_do_crop` is set to `True`.

liveportrait/config/crop_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class CropConfig(PrintableConfig):
4444
vy_ratio_crop_driving_video: float = -0.1 # adjust x offset
4545
direction: str = "large-small" # direction of cropping
4646
########## face alignment option ##########
47-
face_alignment_detector: Literal['blazeface', 'blazeface_back_camera', 'sfd'] = 'blazeface_back_camera'
47+
face_alignment_detector: Literal['blazeface', 'blazeface_back_camera', 'retinaface', 'sfd'] = 'blazeface_back_camera'
4848
face_alignment_detector_device: Literal['cuda', 'cpu', 'mps'] = 'cuda'
4949
face_alignment_detector_dtype: Literal['fp16', 'bf16', 'fp32'] = 'fp16'
5050
########## face index ##########

liveportrait/gradio_pipeline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ def execute_image_retargeting(
344344
if input_lip_ratio != self.source_lip_ratio:
345345
combined_lip_ratio_tensor = self.live_portrait_wrapper.calc_combined_lip_ratio([[float(input_lip_ratio)]], source_lmk_user)
346346
lip_delta = self.live_portrait_wrapper.retarget_lip(x_s_user, combined_lip_ratio_tensor)
347-
print(lip_delta)
347+
# print(lip_delta)
348348
x_d_new = x_d_new + \
349349
(eyes_delta if eyes_delta is not None else 0) + \
350350
(lip_delta if lip_delta is not None else 0)

liveportrait/utils/cropper.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ def __init__(self, **kwargs) -> None:
5050
else:
5151
try:
5252
if torch.backends.mps.is_available():
53-
# Shape inference currently fails with CoreMLExecutionProvider
54-
# for the retinaface model
5553
device = "mps"
5654
face_analysis_wrapper_provider = ["CPUExecutionProvider"]
5755
else:

liveportrait/utils/cropper_face_alignment.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ def __init__(self, **kwargs) -> None:
5252
else:
5353
try:
5454
if torch.backends.mps.is_available():
55-
# Shape inference currently fails with CoreMLExecutionProvider
56-
# for the retinaface model
5755
device = "mps"
5856
else:
5957
device = "cuda"
@@ -67,11 +65,21 @@ def __init__(self, **kwargs) -> None:
6765
)
6866
self.human_landmark_runner.warmup()
6967

70-
if 'blazeface' in face_detector:
71-
face_detector_kwargs = {'back_model': face_detector == 'blazeface_back_camera'}
72-
self.fa = FaceAlignment(LandmarksType.TWO_D, flip_input=False, device=face_detector_device, dtype=face_detector_dtype, face_detector='blazeface', face_detector_kwargs=face_detector_kwargs)
68+
if face_detector == 'blazeface':
69+
face_detector_kwargs = {'back_model': face_detector_dtype == 'blazeface_back_camera'}
70+
elif face_detector == 'retinaface':
71+
face_detector_kwargs = {'fp16': face_detector == torch.float16}
7372
else:
74-
self.fa = FaceAlignment(LandmarksType.TWO_D, flip_input=False, device=face_detector_device, dtype=face_detector_dtype, face_detector=face_detector)
73+
face_detector_kwargs = {}
74+
75+
self.fa = FaceAlignment(
76+
LandmarksType.TWO_D,
77+
flip_input=False,
78+
device=face_detector_device,
79+
dtype=face_detector_dtype,
80+
face_detector=face_detector,
81+
face_detector_kwargs=face_detector_kwargs
82+
)
7583

7684
if self.image_type == "animal_face":
7785
from .animal_landmark_runner import XPoseRunner as AnimalLandmarkRunner
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .retinaface_detector import RetinaFaceDetector as FaceDetector

0 commit comments

Comments
 (0)