1 Commits
3.5.1 ... 3.0.1

Author SHA1 Message Date
e6dc132952 modified content_analyser.py 2024-11-03 22:48:34 +01:00
233 changed files with 7049 additions and 12127 deletions

View File

@@ -1,2 +0,0 @@
[run]
patch = subprocess

View File

@@ -1,5 +1,5 @@
[flake8] [flake8]
select = E22, E23, E24, E27, E3, E4, E7, F, I1, I2 select = E3, E4, F, I1, I2
per-file-ignores = facefusion.py:E402, install.py:E402 per-file-ignores = facefusion.py:E402, install.py:E402
plugins = flake8-import-order plugins = flake8-import-order
application_import_names = facefusion application_import_names = facefusion

3
.github/FUNDING.yml vendored
View File

@@ -1 +1,2 @@
custom: [ buymeacoffee.com/facefusion, ko-fi.com/facefusion ] github: henryruhs
custom: [ buymeacoffee.com/henryruhs, paypal.me/henryruhs ]

BIN
.github/preview.png vendored Normal file → Executable file

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 MiB

After

Width:  |  Height:  |  Size: 1.2 MiB

View File

@@ -8,10 +8,10 @@ jobs:
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Set up Python 3.12 - name: Set up Python 3.10
uses: actions/setup-python@v5 uses: actions/setup-python@v5
with: with:
python-version: '3.12' python-version: '3.10'
- run: pip install flake8 - run: pip install flake8
- run: pip install flake8-import-order - run: pip install flake8-import-order
- run: pip install mypy - run: pip install mypy
@@ -22,17 +22,17 @@ jobs:
test: test:
strategy: strategy:
matrix: matrix:
os: [ macos-latest, ubuntu-latest, windows-latest ] os: [ macos-13, ubuntu-latest, windows-latest ]
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Set up FFmpeg - name: Set up FFmpeg
uses: AnimMouse/setup-ffmpeg@v1 uses: FedericoCarboni/setup-ffmpeg@v3
- name: Set up Python 3.12 - name: Set up Python 3.10
uses: actions/setup-python@v5 uses: actions/setup-python@v5
with: with:
python-version: '3.12' python-version: '3.10'
- run: python install.py --onnxruntime default --skip-conda - run: python install.py --onnxruntime default --skip-conda
- run: pip install pytest - run: pip install pytest
- run: pytest - run: pytest
@@ -44,10 +44,10 @@ jobs:
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Set up FFmpeg - name: Set up FFmpeg
uses: FedericoCarboni/setup-ffmpeg@v3 uses: FedericoCarboni/setup-ffmpeg@v3
- name: Set up Python 3.12 - name: Set up Python 3.10
uses: actions/setup-python@v5 uses: actions/setup-python@v5
with: with:
python-version: '3.12' python-version: '3.10'
- run: python install.py --onnxruntime default --skip-conda - run: python install.py --onnxruntime default --skip-conda
- run: pip install coveralls - run: pip install coveralls
- run: pip install pytest - run: pip install pytest

4
.gitignore vendored
View File

@@ -1,7 +1,5 @@
__pycache__
.assets .assets
.claude
.caches .caches
.idea
.jobs .jobs
.idea
.vscode .vscode

View File

@@ -1,3 +1,3 @@
OpenRAIL-AS license MIT license
Copyright (c) 2025 Henry Ruhs Copyright (c) 2024 Henry Ruhs

View File

@@ -5,7 +5,7 @@ FaceFusion
[![Build Status](https://img.shields.io/github/actions/workflow/status/facefusion/facefusion/ci.yml.svg?branch=master)](https://github.com/facefusion/facefusion/actions?query=workflow:ci) [![Build Status](https://img.shields.io/github/actions/workflow/status/facefusion/facefusion/ci.yml.svg?branch=master)](https://github.com/facefusion/facefusion/actions?query=workflow:ci)
[![Coverage Status](https://img.shields.io/coveralls/facefusion/facefusion.svg)](https://coveralls.io/r/facefusion/facefusion) [![Coverage Status](https://img.shields.io/coveralls/facefusion/facefusion.svg)](https://coveralls.io/r/facefusion/facefusion)
![License](https://img.shields.io/badge/license-OpenRAIL--AS-green) ![License](https://img.shields.io/badge/license-MIT-green)
Preview Preview
@@ -17,7 +17,7 @@ Preview
Installation Installation
------------ ------------
Be aware, the [installation](https://docs.facefusion.io/installation) needs technical skills and is not recommended for beginners. In case you are not comfortable using a terminal, our [Windows Installer](http://windows-installer.facefusion.io) and [macOS Installer](http://macos-installer.facefusion.io) get you started. Be aware, the [installation](https://docs.facefusion.io/installation) needs technical skills and is not recommended for beginners. In case you are not comfortable using a terminal, our [Windows Installer](https://windows-installer.facefusion.io) and [macOS Installer](https://macos-installer.facefusion.io) get you started.
Usage Usage
@@ -35,9 +35,7 @@ options:
commands: commands:
run run the program run run the program
headless-run run the program in headless mode headless-run run the program in headless mode
batch-run run the program in batch mode
force-download force automate downloads and exit force-download force automate downloads and exit
benchmark benchmark the program
job-list list jobs by status job-list list jobs by status
job-create create a drafted job job-create create a drafted job
job-submit submit a drafted job to become a queued job job-submit submit a drafted job to become a queued job

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 94 KiB

View File

@@ -1,19 +1,12 @@
[paths] [paths]
temp_path =
jobs_path = jobs_path =
source_paths = source_paths =
target_path = target_path =
output_path = output_path =
[patterns]
source_pattern =
target_pattern =
output_pattern =
[face_detector] [face_detector]
face_detector_model = face_detector_model =
face_detector_size = face_detector_size =
face_detector_margin =
face_detector_angles = face_detector_angles =
face_detector_score = face_detector_score =
@@ -33,16 +26,10 @@ reference_face_distance =
reference_frame_number = reference_frame_number =
[face_masker] [face_masker]
face_occluder_model =
face_parser_model =
face_mask_types = face_mask_types =
face_mask_areas =
face_mask_regions =
face_mask_blur = face_mask_blur =
face_mask_padding = face_mask_padding =
face_mask_regions =
[voice_extractor]
voice_extractor_model =
[frame_extraction] [frame_extraction]
trim_frame_start = trim_frame_start =
@@ -52,27 +39,21 @@ keep_temp =
[output_creation] [output_creation]
output_image_quality = output_image_quality =
output_image_scale = output_image_resolution =
output_audio_encoder = output_audio_encoder =
output_audio_quality =
output_audio_volume =
output_video_encoder = output_video_encoder =
output_video_preset = output_video_preset =
output_video_quality = output_video_quality =
output_video_scale = output_video_resolution =
output_video_fps = output_video_fps =
skip_audio =
[processors] [processors]
processors = processors =
age_modifier_model = age_modifier_model =
age_modifier_direction = age_modifier_direction =
background_remover_model =
background_remover_color =
deep_swapper_model =
deep_swapper_morph =
expression_restorer_model = expression_restorer_model =
expression_restorer_factor = expression_restorer_factor =
expression_restorer_areas =
face_debugger_items = face_debugger_items =
face_editor_model = face_editor_model =
face_editor_eyebrow_direction = face_editor_eyebrow_direction =
@@ -91,41 +72,30 @@ face_editor_head_yaw =
face_editor_head_roll = face_editor_head_roll =
face_enhancer_model = face_enhancer_model =
face_enhancer_blend = face_enhancer_blend =
face_enhancer_weight =
face_swapper_model = face_swapper_model =
face_swapper_pixel_boost = face_swapper_pixel_boost =
face_swapper_weight =
frame_colorizer_model = frame_colorizer_model =
frame_colorizer_size = frame_colorizer_size =
frame_colorizer_blend = frame_colorizer_blend =
frame_enhancer_model = frame_enhancer_model =
frame_enhancer_blend = frame_enhancer_blend =
lip_syncer_model = lip_syncer_model =
lip_syncer_weight =
[uis] [uis]
open_browser = open_browser =
ui_layouts = ui_layouts =
ui_workflow = ui_workflow =
[download]
download_providers =
download_scope =
[benchmark]
benchmark_mode =
benchmark_resolutions =
benchmark_cycle_count =
[execution] [execution]
execution_device_ids = execution_device_id =
execution_providers = execution_providers =
execution_thread_count = execution_thread_count =
execution_queue_count =
[memory] [memory]
video_memory_strategy = video_memory_strategy =
system_memory_limit = system_memory_limit =
[misc] [misc]
skip_download =
log_level = log_level =
halt_on_error =

View File

@@ -1,7 +1,7 @@
import os import os
import sys import sys
from facefusion.types import AppContext from facefusion.typing import AppContext
def detect_app_context() -> AppContext: def detect_app_context() -> AppContext:

View File

@@ -1,10 +1,10 @@
from facefusion import state_manager from facefusion import state_manager
from facefusion.filesystem import get_file_name, is_video, resolve_file_paths from facefusion.filesystem import is_image, is_video, list_directory
from facefusion.jobs import job_store from facefusion.jobs import job_store
from facefusion.normalizer import normalize_fps, normalize_space from facefusion.normalizer import normalize_fps, normalize_padding
from facefusion.processors.core import get_processors_modules from facefusion.processors.core import get_processors_modules
from facefusion.types import ApplyStateItem, Args from facefusion.typing import ApplyStateItem, Args
from facefusion.vision import detect_video_fps from facefusion.vision import create_image_resolutions, create_video_resolutions, detect_image_resolution, detect_video_fps, detect_video_resolution, pack_resolution
def reduce_step_args(args : Args) -> Args: def reduce_step_args(args : Args) -> Args:
@@ -15,14 +15,6 @@ def reduce_step_args(args : Args) -> Args:
return step_args return step_args
def reduce_job_args(args : Args) -> Args:
job_args =\
{
key: args[key] for key in args if key in job_store.get_job_keys()
}
return job_args
def collect_step_args() -> Args: def collect_step_args() -> Args:
step_args =\ step_args =\
{ {
@@ -43,44 +35,33 @@ def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
# general # general
apply_state_item('command', args.get('command')) apply_state_item('command', args.get('command'))
# paths # paths
apply_state_item('temp_path', args.get('temp_path'))
apply_state_item('jobs_path', args.get('jobs_path')) apply_state_item('jobs_path', args.get('jobs_path'))
apply_state_item('source_paths', args.get('source_paths')) apply_state_item('source_paths', args.get('source_paths'))
apply_state_item('target_path', args.get('target_path')) apply_state_item('target_path', args.get('target_path'))
apply_state_item('output_path', args.get('output_path')) apply_state_item('output_path', args.get('output_path'))
# patterns
apply_state_item('source_pattern', args.get('source_pattern'))
apply_state_item('target_pattern', args.get('target_pattern'))
apply_state_item('output_pattern', args.get('output_pattern'))
# face detector # face detector
apply_state_item('face_detector_model', args.get('face_detector_model')) apply_state_item('face_detector_model', args.get('face_detector_model'))
apply_state_item('face_detector_size', args.get('face_detector_size')) apply_state_item('face_detector_size', args.get('face_detector_size'))
apply_state_item('face_detector_margin', normalize_space(args.get('face_detector_margin')))
apply_state_item('face_detector_angles', args.get('face_detector_angles')) apply_state_item('face_detector_angles', args.get('face_detector_angles'))
apply_state_item('face_detector_score', args.get('face_detector_score')) apply_state_item('face_detector_score', args.get('face_detector_score'))
# face landmarker # face landmarker
apply_state_item('face_landmarker_model', args.get('face_landmarker_model')) apply_state_item('face_landmarker_model', args.get('face_landmarker_model'))
apply_state_item('face_landmarker_score', args.get('face_landmarker_score')) apply_state_item('face_landmarker_score', args.get('face_landmarker_score'))
# face selector # face selector
apply_state_item('face_selector_mode', args.get('face_selector_mode')) state_manager.init_item('face_selector_mode', args.get('face_selector_mode'))
apply_state_item('face_selector_order', args.get('face_selector_order')) state_manager.init_item('face_selector_order', args.get('face_selector_order'))
apply_state_item('face_selector_age_start', args.get('face_selector_age_start')) state_manager.init_item('face_selector_age_start', args.get('face_selector_age_start'))
apply_state_item('face_selector_age_end', args.get('face_selector_age_end')) state_manager.init_item('face_selector_age_end', args.get('face_selector_age_end'))
apply_state_item('face_selector_gender', args.get('face_selector_gender')) state_manager.init_item('face_selector_gender', args.get('face_selector_gender'))
apply_state_item('face_selector_race', args.get('face_selector_race')) state_manager.init_item('face_selector_race', args.get('face_selector_race'))
apply_state_item('reference_face_position', args.get('reference_face_position')) state_manager.init_item('reference_face_position', args.get('reference_face_position'))
apply_state_item('reference_face_distance', args.get('reference_face_distance')) state_manager.init_item('reference_face_distance', args.get('reference_face_distance'))
apply_state_item('reference_frame_number', args.get('reference_frame_number')) state_manager.init_item('reference_frame_number', args.get('reference_frame_number'))
# face masker # face masker
apply_state_item('face_occluder_model', args.get('face_occluder_model'))
apply_state_item('face_parser_model', args.get('face_parser_model'))
apply_state_item('face_mask_types', args.get('face_mask_types')) apply_state_item('face_mask_types', args.get('face_mask_types'))
apply_state_item('face_mask_areas', args.get('face_mask_areas'))
apply_state_item('face_mask_regions', args.get('face_mask_regions'))
apply_state_item('face_mask_blur', args.get('face_mask_blur')) apply_state_item('face_mask_blur', args.get('face_mask_blur'))
apply_state_item('face_mask_padding', normalize_space(args.get('face_mask_padding'))) apply_state_item('face_mask_padding', normalize_padding(args.get('face_mask_padding')))
# voice extractor apply_state_item('face_mask_regions', args.get('face_mask_regions'))
apply_state_item('voice_extractor_model', args.get('voice_extractor_model'))
# frame extraction # frame extraction
apply_state_item('trim_frame_start', args.get('trim_frame_start')) apply_state_item('trim_frame_start', args.get('trim_frame_start'))
apply_state_item('trim_frame_end', args.get('trim_frame_end')) apply_state_item('trim_frame_end', args.get('trim_frame_end'))
@@ -88,19 +69,30 @@ def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('keep_temp', args.get('keep_temp')) apply_state_item('keep_temp', args.get('keep_temp'))
# output creation # output creation
apply_state_item('output_image_quality', args.get('output_image_quality')) apply_state_item('output_image_quality', args.get('output_image_quality'))
apply_state_item('output_image_scale', args.get('output_image_scale')) if is_image(args.get('target_path')):
output_image_resolution = detect_image_resolution(args.get('target_path'))
output_image_resolutions = create_image_resolutions(output_image_resolution)
if args.get('output_image_resolution') in output_image_resolutions:
apply_state_item('output_image_resolution', args.get('output_image_resolution'))
else:
apply_state_item('output_image_resolution', pack_resolution(output_image_resolution))
apply_state_item('output_audio_encoder', args.get('output_audio_encoder')) apply_state_item('output_audio_encoder', args.get('output_audio_encoder'))
apply_state_item('output_audio_quality', args.get('output_audio_quality'))
apply_state_item('output_audio_volume', args.get('output_audio_volume'))
apply_state_item('output_video_encoder', args.get('output_video_encoder')) apply_state_item('output_video_encoder', args.get('output_video_encoder'))
apply_state_item('output_video_preset', args.get('output_video_preset')) apply_state_item('output_video_preset', args.get('output_video_preset'))
apply_state_item('output_video_quality', args.get('output_video_quality')) apply_state_item('output_video_quality', args.get('output_video_quality'))
apply_state_item('output_video_scale', args.get('output_video_scale')) if is_video(args.get('target_path')):
output_video_resolution = detect_video_resolution(args.get('target_path'))
output_video_resolutions = create_video_resolutions(output_video_resolution)
if args.get('output_video_resolution') in output_video_resolutions:
apply_state_item('output_video_resolution', args.get('output_video_resolution'))
else:
apply_state_item('output_video_resolution', pack_resolution(output_video_resolution))
if args.get('output_video_fps') or is_video(args.get('target_path')): if args.get('output_video_fps') or is_video(args.get('target_path')):
output_video_fps = normalize_fps(args.get('output_video_fps')) or detect_video_fps(args.get('target_path')) output_video_fps = normalize_fps(args.get('output_video_fps')) or detect_video_fps(args.get('target_path'))
apply_state_item('output_video_fps', output_video_fps) apply_state_item('output_video_fps', output_video_fps)
apply_state_item('skip_audio', args.get('skip_audio'))
# processors # processors
available_processors = [ get_file_name(file_path) for file_path in resolve_file_paths('facefusion/processors/modules') ] available_processors = list_directory('facefusion/processors/modules')
apply_state_item('processors', args.get('processors')) apply_state_item('processors', args.get('processors'))
for processor_module in get_processors_modules(available_processors): for processor_module in get_processors_modules(available_processors):
processor_module.apply_args(args, apply_state_item) processor_module.apply_args(args, apply_state_item)
@@ -109,22 +101,16 @@ def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('ui_layouts', args.get('ui_layouts')) apply_state_item('ui_layouts', args.get('ui_layouts'))
apply_state_item('ui_workflow', args.get('ui_workflow')) apply_state_item('ui_workflow', args.get('ui_workflow'))
# execution # execution
apply_state_item('execution_device_ids', args.get('execution_device_ids')) apply_state_item('execution_device_id', args.get('execution_device_id'))
apply_state_item('execution_providers', args.get('execution_providers')) apply_state_item('execution_providers', args.get('execution_providers'))
apply_state_item('execution_thread_count', args.get('execution_thread_count')) apply_state_item('execution_thread_count', args.get('execution_thread_count'))
# download apply_state_item('execution_queue_count', args.get('execution_queue_count'))
apply_state_item('download_providers', args.get('download_providers'))
apply_state_item('download_scope', args.get('download_scope'))
# benchmark
apply_state_item('benchmark_mode', args.get('benchmark_mode'))
apply_state_item('benchmark_resolutions', args.get('benchmark_resolutions'))
apply_state_item('benchmark_cycle_count', args.get('benchmark_cycle_count'))
# memory # memory
apply_state_item('video_memory_strategy', args.get('video_memory_strategy')) apply_state_item('video_memory_strategy', args.get('video_memory_strategy'))
apply_state_item('system_memory_limit', args.get('system_memory_limit')) apply_state_item('system_memory_limit', args.get('system_memory_limit'))
# misc # misc
apply_state_item('skip_download', args.get('skip_download'))
apply_state_item('log_level', args.get('log_level')) apply_state_item('log_level', args.get('log_level'))
apply_state_item('halt_on_error', args.get('halt_on_error'))
# jobs # jobs
apply_state_item('job_id', args.get('job_id')) apply_state_item('job_id', args.get('job_id'))
apply_state_item('job_status', args.get('job_status')) apply_state_item('job_status', args.get('job_status'))

View File

@@ -3,26 +3,25 @@ from typing import Any, List, Optional
import numpy import numpy
import scipy import scipy
from numpy.typing import NDArray from numpy._typing import NDArray
from facefusion.ffmpeg import read_audio_buffer from facefusion.ffmpeg import read_audio_buffer
from facefusion.filesystem import is_audio from facefusion.filesystem import is_audio
from facefusion.types import Audio, AudioFrame, Fps, Mel, MelFilterBank, Spectrogram from facefusion.typing import Audio, AudioFrame, Fps, Mel, MelFilterBank, Spectrogram
from facefusion.voice_extractor import batch_extract_voice from facefusion.voice_extractor import batch_extract_voice
@lru_cache(maxsize = 64) @lru_cache(maxsize = 128)
def read_static_audio(audio_path : str, fps : Fps) -> Optional[List[AudioFrame]]: def read_static_audio(audio_path : str, fps : Fps) -> Optional[List[AudioFrame]]:
return read_audio(audio_path, fps) return read_audio(audio_path, fps)
def read_audio(audio_path : str, fps : Fps) -> Optional[List[AudioFrame]]: def read_audio(audio_path : str, fps : Fps) -> Optional[List[AudioFrame]]:
audio_sample_rate = 48000 sample_rate = 48000
audio_sample_size = 16 channel_total = 2
audio_channel_total = 2
if is_audio(audio_path): if is_audio(audio_path):
audio_buffer = read_audio_buffer(audio_path, audio_sample_rate, audio_sample_size, audio_channel_total) audio_buffer = read_audio_buffer(audio_path, sample_rate, channel_total)
audio = numpy.frombuffer(audio_buffer, dtype = numpy.int16).reshape(-1, 2) audio = numpy.frombuffer(audio_buffer, dtype = numpy.int16).reshape(-1, 2)
audio = prepare_audio(audio) audio = prepare_audio(audio)
spectrogram = create_spectrogram(audio) spectrogram = create_spectrogram(audio)
@@ -31,22 +30,21 @@ def read_audio(audio_path : str, fps : Fps) -> Optional[List[AudioFrame]]:
return None return None
@lru_cache(maxsize = 64) @lru_cache(maxsize = 128)
def read_static_voice(audio_path : str, fps : Fps) -> Optional[List[AudioFrame]]: def read_static_voice(audio_path : str, fps : Fps) -> Optional[List[AudioFrame]]:
return read_voice(audio_path, fps) return read_voice(audio_path, fps)
def read_voice(audio_path : str, fps : Fps) -> Optional[List[AudioFrame]]: def read_voice(audio_path : str, fps : Fps) -> Optional[List[AudioFrame]]:
voice_sample_rate = 48000 sample_rate = 48000
voice_sample_size = 16 channel_total = 2
voice_channel_total = 2 chunk_size = 240 * 1024
voice_chunk_size = 240 * 1024 step_size = 180 * 1024
voice_step_size = 180 * 1024
if is_audio(audio_path): if is_audio(audio_path):
audio_buffer = read_audio_buffer(audio_path, voice_sample_rate, voice_sample_size, voice_channel_total) audio_buffer = read_audio_buffer(audio_path, sample_rate, channel_total)
audio = numpy.frombuffer(audio_buffer, dtype = numpy.int16).reshape(-1, 2) audio = numpy.frombuffer(audio_buffer, dtype = numpy.int16).reshape(-1, 2)
audio = batch_extract_voice(audio, voice_chunk_size, voice_step_size) audio = batch_extract_voice(audio, chunk_size, step_size)
audio = prepare_voice(audio) audio = prepare_voice(audio)
spectrogram = create_spectrogram(audio) spectrogram = create_spectrogram(audio)
audio_frames = extract_audio_frames(spectrogram, fps) audio_frames = extract_audio_frames(spectrogram, fps)
@@ -62,20 +60,6 @@ def get_audio_frame(audio_path : str, fps : Fps, frame_number : int = 0) -> Opti
return None return None
def extract_audio_frames(spectrogram : Spectrogram, fps : Fps) -> List[AudioFrame]:
audio_frames = []
mel_filter_total = 80
audio_step_size = 16
indices = numpy.arange(0, spectrogram.shape[1], mel_filter_total / fps).astype(numpy.int16)
indices = indices[indices >= audio_step_size]
for index in indices:
start = max(0, index - audio_step_size)
audio_frames.append(spectrogram[:, start:index])
return audio_frames
def get_voice_frame(audio_path : str, fps : Fps, frame_number : int = 0) -> Optional[AudioFrame]: def get_voice_frame(audio_path : str, fps : Fps, frame_number : int = 0) -> Optional[AudioFrame]:
if is_audio(audio_path): if is_audio(audio_path):
voice_frames = read_static_voice(audio_path, fps) voice_frames = read_static_voice(audio_path, fps)
@@ -86,8 +70,8 @@ def get_voice_frame(audio_path : str, fps : Fps, frame_number : int = 0) -> Opti
def create_empty_audio_frame() -> AudioFrame: def create_empty_audio_frame() -> AudioFrame:
mel_filter_total = 80 mel_filter_total = 80
audio_step_size = 16 step_size = 16
audio_frame = numpy.zeros((mel_filter_total, audio_step_size)).astype(numpy.int16) audio_frame = numpy.zeros((mel_filter_total, step_size)).astype(numpy.int16)
return audio_frame return audio_frame
@@ -100,10 +84,10 @@ def prepare_audio(audio : Audio) -> Audio:
def prepare_voice(audio : Audio) -> Audio: def prepare_voice(audio : Audio) -> Audio:
audio_sample_rate = 48000 sample_rate = 48000
audio_resample_rate = 16000 resample_rate = 16000
audio_resample_factor = round(len(audio) * audio_resample_rate / audio_sample_rate)
audio = scipy.signal.resample(audio, audio_resample_factor) audio = scipy.signal.resample(audio, int(len(audio) * resample_rate / sample_rate))
audio = prepare_audio(audio) audio = prepare_audio(audio)
return audio return audio
@@ -117,20 +101,19 @@ def convert_mel_to_hertz(mel : Mel) -> NDArray[Any]:
def create_mel_filter_bank() -> MelFilterBank: def create_mel_filter_bank() -> MelFilterBank:
audio_sample_rate = 16000
audio_frequency_min = 55.0
audio_frequency_max = 7600.0
mel_filter_total = 80 mel_filter_total = 80
mel_bin_total = 800 mel_bin_total = 800
sample_rate = 16000
min_frequency = 55.0
max_frequency = 7600.0
mel_filter_bank = numpy.zeros((mel_filter_total, mel_bin_total // 2 + 1)) mel_filter_bank = numpy.zeros((mel_filter_total, mel_bin_total // 2 + 1))
mel_frequency_range = numpy.linspace(convert_hertz_to_mel(audio_frequency_min), convert_hertz_to_mel(audio_frequency_max), mel_filter_total + 2) mel_frequency_range = numpy.linspace(convert_hertz_to_mel(min_frequency), convert_hertz_to_mel(max_frequency), mel_filter_total + 2)
indices = numpy.floor((mel_bin_total + 1) * convert_mel_to_hertz(mel_frequency_range) / audio_sample_rate).astype(numpy.int16) indices = numpy.floor((mel_bin_total + 1) * convert_mel_to_hertz(mel_frequency_range) / sample_rate).astype(numpy.int16)
for index in range(mel_filter_total): for index in range(mel_filter_total):
start = indices[index] start = indices[index]
end = indices[index + 1] end = indices[index + 1]
mel_filter_bank[index, start:end] = scipy.signal.windows.triang(end - start) mel_filter_bank[index, start:end] = scipy.signal.windows.triang(end - start)
return mel_filter_bank return mel_filter_bank
@@ -141,3 +124,16 @@ def create_spectrogram(audio : Audio) -> Spectrogram:
spectrogram = scipy.signal.stft(audio, nperseg = mel_bin_total, nfft = mel_bin_total, noverlap = mel_bin_overlap)[2] spectrogram = scipy.signal.stft(audio, nperseg = mel_bin_total, nfft = mel_bin_total, noverlap = mel_bin_overlap)[2]
spectrogram = numpy.dot(mel_filter_bank, numpy.abs(spectrogram)) spectrogram = numpy.dot(mel_filter_bank, numpy.abs(spectrogram))
return spectrogram return spectrogram
def extract_audio_frames(spectrogram : Spectrogram, fps : Fps) -> List[AudioFrame]:
mel_filter_total = 80
step_size = 16
audio_frames = []
indices = numpy.arange(0, spectrogram.shape[1], mel_filter_total / fps).astype(numpy.int16)
indices = indices[indices >= step_size]
for index in indices:
start = max(0, index - step_size)
audio_frames.append(spectrogram[:, start:index])
return audio_frames

View File

@@ -1,111 +0,0 @@
import hashlib
import os
import statistics
import tempfile
from time import perf_counter
from typing import Iterator, List
import facefusion.choices
from facefusion import content_analyser, core, state_manager
from facefusion.cli_helper import render_table
from facefusion.download import conditional_download, resolve_download_url
from facefusion.face_store import clear_static_faces
from facefusion.filesystem import get_file_extension
from facefusion.types import BenchmarkCycleSet
from facefusion.vision import count_video_frame_total, detect_video_fps
def pre_check() -> bool:
conditional_download('.assets/examples',
[
resolve_download_url('examples-3.0.0', 'source.jpg'),
resolve_download_url('examples-3.0.0', 'source.mp3'),
resolve_download_url('examples-3.0.0', 'target-240p.mp4'),
resolve_download_url('examples-3.0.0', 'target-360p.mp4'),
resolve_download_url('examples-3.0.0', 'target-540p.mp4'),
resolve_download_url('examples-3.0.0', 'target-720p.mp4'),
resolve_download_url('examples-3.0.0', 'target-1080p.mp4'),
resolve_download_url('examples-3.0.0', 'target-1440p.mp4'),
resolve_download_url('examples-3.0.0', 'target-2160p.mp4')
])
return True
def run() -> Iterator[List[BenchmarkCycleSet]]:
benchmark_resolutions = state_manager.get_item('benchmark_resolutions')
benchmark_cycle_count = state_manager.get_item('benchmark_cycle_count')
state_manager.init_item('source_paths', [ '.assets/examples/source.jpg', '.assets/examples/source.mp3' ])
state_manager.init_item('face_landmarker_score', 0)
state_manager.init_item('temp_frame_format', 'bmp')
state_manager.init_item('output_audio_volume', 0)
state_manager.init_item('output_video_preset', 'ultrafast')
state_manager.init_item('video_memory_strategy', 'tolerant')
benchmarks = []
target_paths = [ facefusion.choices.benchmark_set.get(benchmark_resolution) for benchmark_resolution in benchmark_resolutions if benchmark_resolution in facefusion.choices.benchmark_set ]
for target_path in target_paths:
state_manager.init_item('target_path', target_path)
state_manager.init_item('output_path', suggest_output_path(state_manager.get_item('target_path')))
benchmarks.append(cycle(benchmark_cycle_count))
yield benchmarks
def cycle(cycle_count : int) -> BenchmarkCycleSet:
process_times = []
video_frame_total = count_video_frame_total(state_manager.get_item('target_path'))
state_manager.init_item('output_video_fps', detect_video_fps(state_manager.get_item('target_path')))
if state_manager.get_item('benchmark_mode') == 'warm':
core.conditional_process()
for index in range(cycle_count):
if state_manager.get_item('benchmark_mode') == 'cold':
content_analyser.analyse_image.cache_clear()
content_analyser.analyse_video.cache_clear()
clear_static_faces()
start_time = perf_counter()
core.conditional_process()
end_time = perf_counter()
process_times.append(end_time - start_time)
average_run = round(statistics.mean(process_times), 2)
fastest_run = round(min(process_times), 2)
slowest_run = round(max(process_times), 2)
relative_fps = round(video_frame_total * cycle_count / sum(process_times), 2)
return\
{
'target_path': state_manager.get_item('target_path'),
'cycle_count': cycle_count,
'average_run': average_run,
'fastest_run': fastest_run,
'slowest_run': slowest_run,
'relative_fps': relative_fps
}
def suggest_output_path(target_path : str) -> str:
target_file_extension = get_file_extension(target_path)
return os.path.join(tempfile.gettempdir(), hashlib.sha1().hexdigest()[:8] + target_file_extension)
def render() -> None:
benchmarks = []
headers =\
[
'target_path',
'cycle_count',
'average_run',
'fastest_run',
'slowest_run',
'relative_fps'
]
for benchmark in run():
benchmarks = benchmark
contents = [ list(benchmark_set.values()) for benchmark_set in benchmarks ]
render_table(headers, contents)

View File

@@ -1,53 +0,0 @@
from typing import List
import cv2
from facefusion.types import CameraPoolSet
CAMERA_POOL_SET : CameraPoolSet =\
{
'capture': {}
}
def get_local_camera_capture(camera_id : int) -> cv2.VideoCapture:
camera_key = str(camera_id)
if camera_key not in CAMERA_POOL_SET.get('capture'):
camera_capture = cv2.VideoCapture(camera_id)
if camera_capture.isOpened():
CAMERA_POOL_SET['capture'][camera_key] = camera_capture
return CAMERA_POOL_SET.get('capture').get(camera_key)
def get_remote_camera_capture(camera_url : str) -> cv2.VideoCapture:
if camera_url not in CAMERA_POOL_SET.get('capture'):
camera_capture = cv2.VideoCapture(camera_url)
if camera_capture.isOpened():
CAMERA_POOL_SET['capture'][camera_url] = camera_capture
return CAMERA_POOL_SET.get('capture').get(camera_url)
def clear_camera_pool() -> None:
for camera_capture in CAMERA_POOL_SET.get('capture').values():
camera_capture.release()
CAMERA_POOL_SET['capture'].clear()
def detect_local_camera_ids(id_start : int, id_end : int) -> List[int]:
local_camera_ids = []
for camera_id in range(id_start, id_end):
cv2.setLogLevel(0)
camera_capture = get_local_camera_capture(camera_id)
cv2.setLogLevel(3)
if camera_capture and camera_capture.isOpened():
local_camera_ids.append(camera_id)
return local_camera_ids

View File

@@ -2,141 +2,31 @@ import logging
from typing import List, Sequence from typing import List, Sequence
from facefusion.common_helper import create_float_range, create_int_range from facefusion.common_helper import create_float_range, create_int_range
from facefusion.types import Angle, AudioEncoder, AudioFormat, AudioTypeSet, BenchmarkMode, BenchmarkResolution, BenchmarkSet, DownloadProvider, DownloadProviderSet, DownloadScope, EncoderSet, ExecutionProvider, ExecutionProviderSet, FaceDetectorModel, FaceDetectorSet, FaceLandmarkerModel, FaceMaskArea, FaceMaskAreaSet, FaceMaskRegion, FaceMaskRegionSet, FaceMaskType, FaceOccluderModel, FaceParserModel, FaceSelectorMode, FaceSelectorOrder, Gender, ImageFormat, ImageTypeSet, JobStatus, LogLevel, LogLevelSet, Race, Score, TempFrameFormat, UiWorkflow, VideoEncoder, VideoFormat, VideoMemoryStrategy, VideoPreset, VideoTypeSet, VoiceExtractorModel from facefusion.typing import Angle, ExecutionProviderSet, FaceDetectorSet, FaceLandmarkerModel, FaceMaskRegion, FaceMaskType, FaceSelectorMode, FaceSelectorOrder, Gender, JobStatus, LogLevelSet, OutputAudioEncoder, OutputVideoEncoder, OutputVideoPreset, Race, Score, TempFrameFormat, UiWorkflow, VideoMemoryStrategy
video_memory_strategies : List[VideoMemoryStrategy] = [ 'strict', 'moderate', 'tolerant' ]
face_detector_set : FaceDetectorSet =\ face_detector_set : FaceDetectorSet =\
{ {
'many': [ '640x640' ], 'many': [ '640x640' ],
'retinaface': [ '160x160', '320x320', '480x480', '512x512', '640x640' ], 'retinaface': [ '160x160', '320x320', '480x480', '512x512', '640x640' ],
'scrfd': [ '160x160', '320x320', '480x480', '512x512', '640x640' ], 'scrfd': [ '160x160', '320x320', '480x480', '512x512', '640x640' ],
'yolo_face': [ '640x640' ], 'yoloface': [ '640x640' ]
'yunet': [ '640x640' ]
} }
face_detector_models : List[FaceDetectorModel] = list(face_detector_set.keys())
face_landmarker_models : List[FaceLandmarkerModel] = [ 'many', '2dfan4', 'peppa_wutz' ] face_landmarker_models : List[FaceLandmarkerModel] = [ 'many', '2dfan4', 'peppa_wutz' ]
face_selector_modes : List[FaceSelectorMode] = [ 'many', 'one', 'reference' ] face_selector_modes : List[FaceSelectorMode] = [ 'many', 'one', 'reference' ]
face_selector_orders : List[FaceSelectorOrder] = [ 'left-right', 'right-left', 'top-bottom', 'bottom-top', 'small-large', 'large-small', 'best-worst', 'worst-best' ] face_selector_orders : List[FaceSelectorOrder] = [ 'left-right', 'right-left', 'top-bottom', 'bottom-top', 'small-large', 'large-small', 'best-worst', 'worst-best' ]
face_selector_genders : List[Gender] = [ 'female', 'male' ] face_selector_genders : List[Gender] = ['female', 'male']
face_selector_races : List[Race] = [ 'white', 'black', 'latino', 'asian', 'indian', 'arabic' ] face_selector_races : List[Race] = ['white', 'black', 'latino', 'asian', 'indian', 'arabic']
face_occluder_models : List[FaceOccluderModel] = [ 'many', 'xseg_1', 'xseg_2', 'xseg_3' ] face_mask_types : List[FaceMaskType] = [ 'box', 'occlusion', 'region' ]
face_parser_models : List[FaceParserModel] = [ 'bisenet_resnet_18', 'bisenet_resnet_34' ] face_mask_regions : List[FaceMaskRegion] = [ 'skin', 'left-eyebrow', 'right-eyebrow', 'left-eye', 'right-eye', 'glasses', 'nose', 'mouth', 'upper-lip', 'lower-lip' ]
face_mask_types : List[FaceMaskType] = [ 'box', 'occlusion', 'area', 'region' ] temp_frame_formats : List[TempFrameFormat] = [ 'bmp', 'jpg', 'png' ]
face_mask_area_set : FaceMaskAreaSet =\ output_audio_encoders : List[OutputAudioEncoder] = [ 'aac', 'libmp3lame', 'libopus', 'libvorbis' ]
{ output_video_encoders : List[OutputVideoEncoder] = [ 'libx264', 'libx265', 'libvpx-vp9', 'h264_nvenc', 'hevc_nvenc', 'h264_amf', 'hevc_amf', 'h264_videotoolbox', 'hevc_videotoolbox' ]
'upper-face': [ 0, 1, 2, 31, 32, 33, 34, 35, 14, 15, 16, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17 ], output_video_presets : List[OutputVideoPreset] = [ 'ultrafast', 'superfast', 'veryfast', 'faster', 'fast', 'medium', 'slow', 'slower', 'veryslow' ]
'lower-face': [ 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 35, 34, 33, 32, 31 ],
'mouth': [ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67 ]
}
face_mask_region_set : FaceMaskRegionSet =\
{
'skin': 1,
'left-eyebrow': 2,
'right-eyebrow': 3,
'left-eye': 4,
'right-eye': 5,
'glasses': 6,
'nose': 10,
'mouth': 11,
'upper-lip': 12,
'lower-lip': 13
}
face_mask_areas : List[FaceMaskArea] = list(face_mask_area_set.keys())
face_mask_regions : List[FaceMaskRegion] = list(face_mask_region_set.keys())
voice_extractor_models : List[VoiceExtractorModel] = [ 'kim_vocal_1', 'kim_vocal_2', 'uvr_mdxnet' ] image_template_sizes : List[float] = [ 0.25, 0.5, 0.75, 1, 1.5, 2, 2.5, 3, 3.5, 4 ]
video_template_sizes : List[int] = [ 240, 360, 480, 540, 720, 1080, 1440, 2160, 4320 ]
audio_type_set : AudioTypeSet =\
{
'flac': 'audio/flac',
'm4a': 'audio/mp4',
'mp3': 'audio/mpeg',
'ogg': 'audio/ogg',
'opus': 'audio/opus',
'wav': 'audio/x-wav'
}
image_type_set : ImageTypeSet =\
{
'bmp': 'image/bmp',
'jpeg': 'image/jpeg',
'png': 'image/png',
'tiff': 'image/tiff',
'webp': 'image/webp'
}
video_type_set : VideoTypeSet =\
{
'avi': 'video/x-msvideo',
'm4v': 'video/mp4',
'mkv': 'video/x-matroska',
'mp4': 'video/mp4',
'mpeg': 'video/mpeg',
'mov': 'video/quicktime',
'mxf': 'application/mxf',
'webm': 'video/webm',
'wmv': 'video/x-ms-wmv'
}
audio_formats : List[AudioFormat] = list(audio_type_set.keys())
image_formats : List[ImageFormat] = list(image_type_set.keys())
video_formats : List[VideoFormat] = list(video_type_set.keys())
temp_frame_formats : List[TempFrameFormat] = [ 'bmp', 'jpeg', 'png', 'tiff' ]
output_encoder_set : EncoderSet =\
{
'audio': [ 'flac', 'aac', 'libmp3lame', 'libopus', 'libvorbis', 'pcm_s16le', 'pcm_s32le' ],
'video': [ 'libx264', 'libx264rgb', 'libx265', 'libvpx-vp9', 'h264_nvenc', 'hevc_nvenc', 'h264_amf', 'hevc_amf', 'h264_qsv', 'hevc_qsv', 'h264_videotoolbox', 'hevc_videotoolbox', 'rawvideo' ]
}
output_audio_encoders : List[AudioEncoder] = output_encoder_set.get('audio')
output_video_encoders : List[VideoEncoder] = output_encoder_set.get('video')
output_video_presets : List[VideoPreset] = [ 'ultrafast', 'superfast', 'veryfast', 'faster', 'fast', 'medium', 'slow', 'slower', 'veryslow' ]
benchmark_modes : List[BenchmarkMode] = [ 'warm', 'cold' ]
benchmark_set : BenchmarkSet =\
{
'240p': '.assets/examples/target-240p.mp4',
'360p': '.assets/examples/target-360p.mp4',
'540p': '.assets/examples/target-540p.mp4',
'720p': '.assets/examples/target-720p.mp4',
'1080p': '.assets/examples/target-1080p.mp4',
'1440p': '.assets/examples/target-1440p.mp4',
'2160p': '.assets/examples/target-2160p.mp4'
}
benchmark_resolutions : List[BenchmarkResolution] = list(benchmark_set.keys())
execution_provider_set : ExecutionProviderSet =\
{
'cuda': 'CUDAExecutionProvider',
'tensorrt': 'TensorrtExecutionProvider',
'directml': 'DmlExecutionProvider',
'rocm': 'ROCMExecutionProvider',
'migraphx': 'MIGraphXExecutionProvider',
'openvino': 'OpenVINOExecutionProvider',
'coreml': 'CoreMLExecutionProvider',
'cpu': 'CPUExecutionProvider'
}
execution_providers : List[ExecutionProvider] = list(execution_provider_set.keys())
download_provider_set : DownloadProviderSet =\
{
'github':
{
'urls':
[
'https://github.com'
],
'path': '/facefusion/facefusion-assets/releases/download/{base_name}/{file_name}'
},
'huggingface':
{
'urls':
[
'https://huggingface.co',
'https://hf-mirror.com'
],
'path': '/facefusion/{base_name}/resolve/main/{file_name}'
}
}
download_providers : List[DownloadProvider] = list(download_provider_set.keys())
download_scopes : List[DownloadScope] = [ 'lite', 'full' ]
video_memory_strategies : List[VideoMemoryStrategy] = [ 'strict', 'moderate', 'tolerant' ]
log_level_set : LogLevelSet =\ log_level_set : LogLevelSet =\
{ {
@@ -145,25 +35,30 @@ log_level_set : LogLevelSet =\
'info': logging.INFO, 'info': logging.INFO,
'debug': logging.DEBUG 'debug': logging.DEBUG
} }
log_levels : List[LogLevel] = list(log_level_set.keys())
execution_provider_set : ExecutionProviderSet =\
{
'cpu': 'CPUExecutionProvider',
'coreml': 'CoreMLExecutionProvider',
'cuda': 'CUDAExecutionProvider',
'directml': 'DmlExecutionProvider',
'openvino': 'OpenVINOExecutionProvider',
'rocm': 'ROCMExecutionProvider',
'tensorrt': 'TensorrtExecutionProvider'
}
ui_workflows : List[UiWorkflow] = [ 'instant_runner', 'job_runner', 'job_manager' ] ui_workflows : List[UiWorkflow] = [ 'instant_runner', 'job_runner', 'job_manager' ]
job_statuses : List[JobStatus] = [ 'drafted', 'queued', 'completed', 'failed' ] job_statuses : List[JobStatus] = [ 'drafted', 'queued', 'completed', 'failed' ]
benchmark_cycle_count_range : Sequence[int] = create_int_range(1, 10, 1)
execution_thread_count_range : Sequence[int] = create_int_range(1, 32, 1) execution_thread_count_range : Sequence[int] = create_int_range(1, 32, 1)
execution_queue_count_range : Sequence[int] = create_int_range(1, 4, 1)
system_memory_limit_range : Sequence[int] = create_int_range(0, 128, 4) system_memory_limit_range : Sequence[int] = create_int_range(0, 128, 4)
face_detector_margin_range : Sequence[int] = create_int_range(0, 100, 1)
face_detector_angles : Sequence[Angle] = create_int_range(0, 270, 90) face_detector_angles : Sequence[Angle] = create_int_range(0, 270, 90)
face_detector_score_range : Sequence[Score] = create_float_range(0.0, 1.0, 0.05) face_detector_score_range : Sequence[Score] = create_float_range(0.0, 1.0, 0.05)
face_landmarker_score_range : Sequence[Score] = create_float_range(0.0, 1.0, 0.05) face_landmarker_score_range : Sequence[Score] = create_float_range(0.0, 1.0, 0.05)
face_mask_blur_range : Sequence[float] = create_float_range(0.0, 1.0, 0.05) face_mask_blur_range : Sequence[float] = create_float_range(0.0, 1.0, 0.05)
face_mask_padding_range : Sequence[int] = create_int_range(0, 100, 1) face_mask_padding_range : Sequence[int] = create_int_range(0, 100, 1)
face_selector_age_range : Sequence[int] = create_int_range(0, 100, 1) face_selector_age_range : Sequence[int] = create_int_range(0, 100, 1)
reference_face_distance_range : Sequence[float] = create_float_range(0.0, 1.0, 0.05) reference_face_distance_range : Sequence[float] = create_float_range(0.0, 1.5, 0.05)
output_image_quality_range : Sequence[int] = create_int_range(0, 100, 1) output_image_quality_range : Sequence[int] = create_int_range(0, 100, 1)
output_image_scale_range : Sequence[float] = create_float_range(0.25, 8.0, 0.25)
output_audio_quality_range : Sequence[int] = create_int_range(0, 100, 1)
output_audio_volume_range : Sequence[int] = create_int_range(0, 100, 1)
output_video_quality_range : Sequence[int] = create_int_range(0, 100, 1) output_video_quality_range : Sequence[int] = create_int_range(0, 100, 1)
output_video_scale_range : Sequence[float] = create_float_range(0.25, 8.0, 0.25)

View File

@@ -1,35 +0,0 @@
from typing import List, Tuple
from facefusion.logger import get_package_logger
from facefusion.types import TableContent, TableHeader
def render_table(headers : List[TableHeader], contents : List[List[TableContent]]) -> None:
package_logger = get_package_logger()
table_column, table_separator = create_table_parts(headers, contents)
package_logger.critical(table_separator)
package_logger.critical(table_column.format(*headers))
package_logger.critical(table_separator)
for content in contents:
content = [ str(value) for value in content ]
package_logger.critical(table_column.format(*content))
package_logger.critical(table_separator)
def create_table_parts(headers : List[TableHeader], contents : List[List[TableContent]]) -> Tuple[str, str]:
column_parts = []
separator_parts = []
widths = [ len(header) for header in headers ]
for content in contents:
for index, value in enumerate(content):
widths[index] = max(widths[index], len(str(value)))
for width in widths:
column_parts.append('{:<' + str(width) + '}')
separator_parts.append('-' * width)
return '| ' + ' | '.join(column_parts) + ' |', '+-' + '-+-'.join(separator_parts) + '-+'

View File

@@ -1,5 +1,5 @@
import platform import platform
from typing import Any, Iterable, Optional, Reversible, Sequence from typing import Any, Optional, Sequence
def is_linux() -> bool: def is_linux() -> bool:
@@ -15,11 +15,11 @@ def is_windows() -> bool:
def create_int_metavar(int_range : Sequence[int]) -> str: def create_int_metavar(int_range : Sequence[int]) -> str:
return '[' + str(int_range[0]) + '..' + str(int_range[-1]) + ':' + str(calculate_int_step(int_range)) + ']' return '[' + str(int_range[0]) + '..' + str(int_range[-1]) + ':' + str(calc_int_step(int_range)) + ']'
def create_float_metavar(float_range : Sequence[float]) -> str: def create_float_metavar(float_range : Sequence[float]) -> str:
return '[' + str(float_range[0]) + '..' + str(float_range[-1]) + ':' + str(calculate_float_step(float_range)) + ']' return '[' + str(float_range[0]) + '..' + str(float_range[-1]) + ':' + str(calc_float_step(float_range)) + ']'
def create_int_range(start : int, end : int, step : int) -> Sequence[int]: def create_int_range(start : int, end : int, step : int) -> Sequence[int]:
@@ -42,43 +42,31 @@ def create_float_range(start : float, end : float, step : float) -> Sequence[flo
return float_range return float_range
def calculate_int_step(int_range : Sequence[int]) -> int: def calc_int_step(int_range : Sequence[int]) -> int:
return int_range[1] - int_range[0] return int_range[1] - int_range[0]
def calculate_float_step(float_range : Sequence[float]) -> float: def calc_float_step(float_range : Sequence[float]) -> float:
return round(float_range[1] - float_range[0], 2) return round(float_range[1] - float_range[0], 2)
def cast_int(value : Any) -> Optional[int]: def cast_int(value : Any) -> Optional[Any]:
try: try:
return int(value) return int(value)
except (ValueError, TypeError): except (ValueError, TypeError):
return None return None
def cast_float(value : Any) -> Optional[float]: def cast_float(value : Any) -> Optional[Any]:
try: try:
return float(value) return float(value)
except (ValueError, TypeError): except (ValueError, TypeError):
return None return None
def cast_bool(value : Any) -> Optional[bool]:
if value == 'True':
return True
if value == 'False':
return False
return None
def get_first(__list__ : Any) -> Any: def get_first(__list__ : Any) -> Any:
if isinstance(__list__, Iterable):
return next(iter(__list__), None) return next(iter(__list__), None)
return None
def get_last(__list__ : Any) -> Any: def get_last(__list__ : Any) -> Any:
if isinstance(__list__, Reversible):
return next(reversed(__list__), None) return next(reversed(__list__), None)
return None

View File

@@ -1,74 +1,92 @@
from configparser import ConfigParser from configparser import ConfigParser
from typing import List, Optional from typing import Any, List, Optional
from facefusion import state_manager from facefusion import state_manager
from facefusion.common_helper import cast_bool, cast_float, cast_int from facefusion.common_helper import cast_float, cast_int
CONFIG_PARSER = None CONFIG = None
def get_config_parser() -> ConfigParser: def get_config() -> ConfigParser:
global CONFIG_PARSER global CONFIG
if CONFIG_PARSER is None: if CONFIG is None:
CONFIG_PARSER = ConfigParser() CONFIG = ConfigParser()
CONFIG_PARSER.read(state_manager.get_item('config_path'), encoding = 'utf-8') CONFIG.read(state_manager.get_item('config_path'), encoding = 'utf-8')
return CONFIG_PARSER return CONFIG
def clear_config_parser() -> None: def clear_config() -> None:
global CONFIG_PARSER global CONFIG
CONFIG_PARSER = None CONFIG = None
def get_str_value(section : str, option : str, fallback : Optional[str] = None) -> Optional[str]: def get_str_value(key : str, fallback : Optional[str] = None) -> Optional[str]:
config_parser = get_config_parser() value = get_value_by_notation(key)
if config_parser.has_option(section, option) and config_parser.get(section, option).strip(): if value or fallback:
return config_parser.get(section, option) return str(value or fallback)
return fallback
def get_int_value(section : str, option : str, fallback : Optional[str] = None) -> Optional[int]:
config_parser = get_config_parser()
if config_parser.has_option(section, option) and config_parser.get(section, option).strip():
return config_parser.getint(section, option)
return cast_int(fallback)
def get_float_value(section : str, option : str, fallback : Optional[str] = None) -> Optional[float]:
config_parser = get_config_parser()
if config_parser.has_option(section, option) and config_parser.get(section, option).strip():
return config_parser.getfloat(section, option)
return cast_float(fallback)
def get_bool_value(section : str, option : str, fallback : Optional[str] = None) -> Optional[bool]:
config_parser = get_config_parser()
if config_parser.has_option(section, option) and config_parser.get(section, option).strip():
return config_parser.getboolean(section, option)
return cast_bool(fallback)
def get_str_list(section : str, option : str, fallback : Optional[str] = None) -> Optional[List[str]]:
config_parser = get_config_parser()
if config_parser.has_option(section, option) and config_parser.get(section, option).strip():
return config_parser.get(section, option).split()
if fallback:
return fallback.split()
return None return None
def get_int_list(section : str, option : str, fallback : Optional[str] = None) -> Optional[List[int]]: def get_int_value(key : str, fallback : Optional[str] = None) -> Optional[int]:
config_parser = get_config_parser() value = get_value_by_notation(key)
if config_parser.has_option(section, option) and config_parser.get(section, option).strip(): if value or fallback:
return list(map(int, config_parser.get(section, option).split())) return cast_int(value or fallback)
if fallback: return None
return list(map(int, fallback.split()))
def get_float_value(key : str, fallback : Optional[str] = None) -> Optional[float]:
value = get_value_by_notation(key)
if value or fallback:
return cast_float(value or fallback)
return None
def get_bool_value(key : str, fallback : Optional[str] = None) -> Optional[bool]:
value = get_value_by_notation(key)
if value == 'True' or fallback == 'True':
return True
if value == 'False' or fallback == 'False':
return False
return None
def get_str_list(key : str, fallback : Optional[str] = None) -> Optional[List[str]]:
value = get_value_by_notation(key)
if value or fallback:
return [ str(value) for value in (value or fallback).split(' ') ]
return None
def get_int_list(key : str, fallback : Optional[str] = None) -> Optional[List[int]]:
value = get_value_by_notation(key)
if value or fallback:
return [ cast_int(value) for value in (value or fallback).split(' ') ]
return None
def get_float_list(key : str, fallback : Optional[str] = None) -> Optional[List[float]]:
value = get_value_by_notation(key)
if value or fallback:
return [ cast_float(value) for value in (value or fallback).split(' ') ]
return None
def get_value_by_notation(key : str) -> Optional[Any]:
config = get_config()
if '.' in key:
section, name = key.split('.')
if section in config and name in config[section]:
return config[section][name]
if key in config:
return config[key]
return None return None

View File

@@ -1,146 +1,64 @@
from functools import lru_cache from functools import lru_cache
from typing import List, Tuple
import cv2
import numpy import numpy
from tqdm import tqdm from tqdm import tqdm
from facefusion import inference_manager, state_manager, translator from facefusion import inference_manager, state_manager, wording
from facefusion.common_helper import is_macos from facefusion.download import conditional_download_hashes, conditional_download_sources
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
from facefusion.execution import has_execution_provider
from facefusion.filesystem import resolve_relative_path from facefusion.filesystem import resolve_relative_path
from facefusion.thread_helper import conditional_thread_semaphore from facefusion.thread_helper import conditional_thread_semaphore
from facefusion.types import Detection, DownloadScope, DownloadSet, ExecutionProvider, Fps, InferencePool, ModelSet, VisionFrame from facefusion.typing import Fps, InferencePool, ModelOptions, ModelSet, VisionFrame
from facefusion.vision import detect_video_fps, fit_contain_frame, read_image, read_video_frame from facefusion.vision import count_video_frame_total, detect_video_fps, get_video_frame, read_image
MODEL_SET : ModelSet =\
{
'open_nsfw':
{
'hashes':
{
'content_analyser':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/open_nsfw.hash',
'path': resolve_relative_path('../.assets/models/open_nsfw.hash')
}
},
'sources':
{
'content_analyser':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/open_nsfw.onnx',
'path': resolve_relative_path('../.assets/models/open_nsfw.onnx')
}
},
'size': (224, 224),
'mean': [ 104, 117, 123 ]
}
}
PROBABILITY_LIMIT = 1.00
RATE_LIMIT = 10
STREAM_COUNTER = 0 STREAM_COUNTER = 0
@lru_cache()
def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
return\
{
'nsfw_1':
{
'__metadata__':
{
'vendor': 'EraX',
'license': 'Apache-2.0',
'year': 2024
},
'hashes':
{
'content_analyser':
{
'url': resolve_download_url('models-3.3.0', 'nsfw_1.hash'),
'path': resolve_relative_path('../.assets/models/nsfw_1.hash')
}
},
'sources':
{
'content_analyser':
{
'url': resolve_download_url('models-3.3.0', 'nsfw_1.onnx'),
'path': resolve_relative_path('../.assets/models/nsfw_1.onnx')
}
},
'size': (640, 640),
'mean': (0.0, 0.0, 0.0),
'standard_deviation': (1.0, 1.0, 1.0)
},
'nsfw_2':
{
'__metadata__':
{
'vendor': 'Marqo',
'license': 'Apache-2.0',
'year': 2024
},
'hashes':
{
'content_analyser':
{
'url': resolve_download_url('models-3.3.0', 'nsfw_2.hash'),
'path': resolve_relative_path('../.assets/models/nsfw_2.hash')
}
},
'sources':
{
'content_analyser':
{
'url': resolve_download_url('models-3.3.0', 'nsfw_2.onnx'),
'path': resolve_relative_path('../.assets/models/nsfw_2.onnx')
}
},
'size': (384, 384),
'mean': (0.5, 0.5, 0.5),
'standard_deviation': (0.5, 0.5, 0.5)
},
'nsfw_3':
{
'__metadata__':
{
'vendor': 'Freepik',
'license': 'MIT',
'year': 2025
},
'hashes':
{
'content_analyser':
{
'url': resolve_download_url('models-3.3.0', 'nsfw_3.hash'),
'path': resolve_relative_path('../.assets/models/nsfw_3.hash')
}
},
'sources':
{
'content_analyser':
{
'url': resolve_download_url('models-3.3.0', 'nsfw_3.onnx'),
'path': resolve_relative_path('../.assets/models/nsfw_3.onnx')
}
},
'size': (448, 448),
'mean': (0.48145466, 0.4578275, 0.40821073),
'standard_deviation': (0.26862954, 0.26130258, 0.27577711)
}
}
def get_inference_pool() -> InferencePool: def get_inference_pool() -> InferencePool:
model_names = [ 'nsfw_1', 'nsfw_2', 'nsfw_3' ] model_sources = get_model_options().get('sources')
_, model_source_set = collect_model_downloads() return inference_manager.get_inference_pool(__name__, model_sources)
return inference_manager.get_inference_pool(__name__, model_names, model_source_set)
def clear_inference_pool() -> None: def clear_inference_pool() -> None:
model_names = [ 'nsfw_1', 'nsfw_2', 'nsfw_3' ] inference_manager.clear_inference_pool(__name__)
inference_manager.clear_inference_pool(__name__, model_names)
def resolve_execution_providers() -> List[ExecutionProvider]: def get_model_options() -> ModelOptions:
if is_macos() and has_execution_provider('coreml'): return MODEL_SET.get('open_nsfw')
return [ 'cpu' ]
return state_manager.get_item('execution_providers')
def collect_model_downloads() -> Tuple[DownloadSet, DownloadSet]:
model_set = create_static_model_set('full')
model_hash_set = {}
model_source_set = {}
for content_analyser_model in [ 'nsfw_1', 'nsfw_2', 'nsfw_3' ]:
model_hash_set[content_analyser_model] = model_set.get(content_analyser_model).get('hashes').get('content_analyser')
model_source_set[content_analyser_model] = model_set.get(content_analyser_model).get('sources').get('content_analyser')
return model_hash_set, model_source_set
def pre_check() -> bool: def pre_check() -> bool:
model_hash_set, model_source_set = collect_model_downloads() download_directory_path = resolve_relative_path('../.assets/models')
model_hashes = get_model_options().get('hashes')
model_sources = get_model_options().get('sources')
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set) return conditional_download_hashes(download_directory_path, model_hashes) and conditional_download_sources(download_directory_path, model_sources)
def analyse_stream(vision_frame : VisionFrame, video_fps : Fps) -> bool: def analyse_stream(vision_frame : VisionFrame, video_fps : Fps) -> bool:
@@ -153,95 +71,54 @@ def analyse_stream(vision_frame : VisionFrame, video_fps : Fps) -> bool:
def analyse_frame(vision_frame : VisionFrame) -> bool: def analyse_frame(vision_frame : VisionFrame) -> bool:
return detect_nsfw(vision_frame) vision_frame = prepare_frame(vision_frame)
probability = forward(vision_frame)
return probability > PROBABILITY_LIMIT
@lru_cache() def forward(vision_frame : VisionFrame) -> float:
def analyse_image(image_path : str) -> bool: content_analyser = get_inference_pool().get('content_analyser')
vision_frame = read_image(image_path)
return analyse_frame(vision_frame)
@lru_cache()
def analyse_video(video_path : str, trim_frame_start : int, trim_frame_end : int) -> bool:
video_fps = detect_video_fps(video_path)
frame_range = range(trim_frame_start, trim_frame_end)
rate = 0.0
total = 0
counter = 0
with tqdm(total = len(frame_range), desc = translator.get('analysing'), unit = 'frame', ascii = ' =', disable = state_manager.get_item('log_level') in [ 'warn', 'error' ]) as progress:
for frame_number in frame_range:
if frame_number % int(video_fps) == 0:
vision_frame = read_video_frame(video_path, frame_number)
total += 1
if analyse_frame(vision_frame):
counter += 1
if counter > 0 and total > 0:
rate = counter / total * 100
progress.set_postfix(rate = rate)
progress.update()
return bool(rate > 10.0)
def detect_nsfw(vision_frame : VisionFrame) -> bool:
is_nsfw_1 = detect_with_nsfw_1(vision_frame)
is_nsfw_2 = detect_with_nsfw_2(vision_frame)
is_nsfw_3 = detect_with_nsfw_3(vision_frame)
return False
def detect_with_nsfw_1(vision_frame : VisionFrame) -> bool:
detect_vision_frame = prepare_detect_frame(vision_frame, 'nsfw_1')
detection = forward_nsfw(detect_vision_frame, 'nsfw_1')
detection_score = numpy.max(numpy.amax(detection[:, 4:], axis = 1))
return bool(detection_score > 0.2)
def detect_with_nsfw_2(vision_frame : VisionFrame) -> bool:
detect_vision_frame = prepare_detect_frame(vision_frame, 'nsfw_2')
detection = forward_nsfw(detect_vision_frame, 'nsfw_2')
detection_score = detection[0] - detection[1]
return bool(detection_score > 0.25)
def detect_with_nsfw_3(vision_frame : VisionFrame) -> bool:
detect_vision_frame = prepare_detect_frame(vision_frame, 'nsfw_3')
detection = forward_nsfw(detect_vision_frame, 'nsfw_3')
detection_score = (detection[2] + detection[3]) - (detection[0] + detection[1])
return bool(detection_score > 10.5)
def forward_nsfw(vision_frame : VisionFrame, model_name : str) -> Detection:
content_analyser = get_inference_pool().get(model_name)
with conditional_thread_semaphore(): with conditional_thread_semaphore():
detection = content_analyser.run(None, probability = content_analyser.run(None,
{ {
'input': vision_frame 'input': vision_frame
})[0] })[0][0][1]
if model_name in [ 'nsfw_2', 'nsfw_3' ]: return probability
return detection[0]
return detection
def prepare_detect_frame(temp_vision_frame : VisionFrame, model_name : str) -> VisionFrame: def prepare_frame(vision_frame : VisionFrame) -> VisionFrame:
model_set = create_static_model_set('full').get(model_name) model_size = get_model_options().get('size')
model_size = model_set.get('size') model_mean = get_model_options().get('mean')
model_mean = model_set.get('mean') vision_frame = cv2.resize(vision_frame, model_size).astype(numpy.float32)
model_standard_deviation = model_set.get('standard_deviation') vision_frame -= numpy.array(model_mean).astype(numpy.float32)
vision_frame = numpy.expand_dims(vision_frame, axis = 0)
return vision_frame
detect_vision_frame = fit_contain_frame(temp_vision_frame, model_size)
detect_vision_frame = detect_vision_frame[:, :, ::-1] / 255.0 @lru_cache(maxsize = None)
detect_vision_frame -= model_mean def analyse_image(image_path : str) -> bool:
detect_vision_frame /= model_standard_deviation frame = read_image(image_path)
detect_vision_frame = numpy.expand_dims(detect_vision_frame.transpose(2, 0, 1), axis = 0).astype(numpy.float32) return analyse_frame(frame)
return detect_vision_frame
@lru_cache(maxsize = None)
def analyse_video(video_path : str, start_frame : int, end_frame : int) -> bool:
video_frame_total = count_video_frame_total(video_path)
video_fps = detect_video_fps(video_path)
frame_range = range(start_frame or 0, end_frame or video_frame_total)
rate = 0.0
counter = 0
with tqdm(total = len(frame_range), desc = wording.get('analysing'), unit = 'frame', ascii = ' =', disable = state_manager.get_item('log_level') in [ 'warn', 'error' ]) as progress:
for frame_number in frame_range:
if frame_number % int(video_fps) == 0:
frame = get_video_frame(video_path, frame_number)
if analyse_frame(frame):
counter += 1
rate = counter * int(video_fps) / len(frame_range) * 100
progress.update()
progress.set_postfix(rate = rate)
return rate > RATE_LIMIT

View File

@@ -1,28 +1,35 @@
import inspect
import itertools
import shutil import shutil
import signal import signal
import sys import sys
from time import time from time import time
from facefusion import benchmarker, cli_helper, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, hash_helper, logger, state_manager, translator, voice_extractor import numpy
from facefusion.args import apply_args, collect_job_args, reduce_job_args, reduce_step_args
from facefusion import content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, logger, process_manager, state_manager, voice_extractor, wording
from facefusion.args import apply_args, collect_job_args, reduce_step_args
from facefusion.common_helper import get_first
from facefusion.content_analyser import analyse_image, analyse_video
from facefusion.download import conditional_download_hashes, conditional_download_sources from facefusion.download import conditional_download_hashes, conditional_download_sources
from facefusion.exit_helper import hard_exit, signal_exit from facefusion.exit_helper import conditional_exit, graceful_exit, hard_exit
from facefusion.filesystem import get_file_extension, get_file_name, is_image, is_video, resolve_file_paths, resolve_file_pattern from facefusion.face_analyser import get_average_face, get_many_faces, get_one_face
from facefusion.face_selector import sort_and_filter_faces
from facefusion.face_store import append_reference_face, clear_reference_faces, get_reference_faces
from facefusion.ffmpeg import copy_image, extract_frames, finalize_image, merge_video, replace_audio, restore_audio
from facefusion.filesystem import filter_audio_paths, is_image, is_video, list_directory, resolve_relative_path
from facefusion.jobs import job_helper, job_manager, job_runner from facefusion.jobs import job_helper, job_manager, job_runner
from facefusion.jobs.job_list import compose_job_list from facefusion.jobs.job_list import compose_job_list
from facefusion.memory import limit_system_memory from facefusion.memory import limit_system_memory
from facefusion.processors.core import get_processors_modules from facefusion.processors.core import get_processors_modules
from facefusion.program import create_program from facefusion.program import create_program
from facefusion.program_helper import validate_args from facefusion.program_helper import validate_args
from facefusion.types import Args, ErrorCode from facefusion.statistics import conditional_log_statistics
from facefusion.workflows import image_to_image, image_to_video from facefusion.temp_helper import clear_temp_directory, create_temp_directory, get_temp_file_path, get_temp_frame_paths, move_temp_file
from facefusion.typing import Args, ErrorCode
from facefusion.vision import get_video_frame, pack_resolution, read_image, read_static_images, restrict_image_resolution, restrict_video_fps, restrict_video_resolution, unpack_resolution
def cli() -> None: def cli() -> None:
if pre_check(): signal.signal(signal.SIGINT, lambda signal_number, frame: graceful_exit(0))
signal.signal(signal.SIGINT, signal_exit)
program = create_program() program = create_program()
if validate_args(program): if validate_args(program):
@@ -34,56 +41,36 @@ def cli() -> None:
route(args) route(args)
else: else:
program.print_help() program.print_help()
else:
hard_exit(2)
else:
hard_exit(2)
def route(args : Args) -> None: def route(args : Args) -> None:
system_memory_limit = state_manager.get_item('system_memory_limit') system_memory_limit = state_manager.get_item('system_memory_limit')
if system_memory_limit and system_memory_limit > 0: if system_memory_limit and system_memory_limit > 0:
limit_system_memory(system_memory_limit) limit_system_memory(system_memory_limit)
if state_manager.get_item('command') == 'force-download': if state_manager.get_item('command') == 'force-download':
error_code = force_download() error_code = force_download()
hard_exit(error_code) return conditional_exit(error_code)
if state_manager.get_item('command') == 'benchmark':
if not common_pre_check() or not processors_pre_check() or not benchmarker.pre_check():
hard_exit(2)
benchmarker.render()
if state_manager.get_item('command') in [ 'job-list', 'job-create', 'job-submit', 'job-submit-all', 'job-delete', 'job-delete-all', 'job-add-step', 'job-remix-step', 'job-insert-step', 'job-remove-step' ]: if state_manager.get_item('command') in [ 'job-list', 'job-create', 'job-submit', 'job-submit-all', 'job-delete', 'job-delete-all', 'job-add-step', 'job-remix-step', 'job-insert-step', 'job-remove-step' ]:
if not job_manager.init_jobs(state_manager.get_item('jobs_path')): if not job_manager.init_jobs(state_manager.get_item('jobs_path')):
hard_exit(1) hard_exit(1)
error_code = route_job_manager(args) error_code = route_job_manager(args)
hard_exit(error_code) hard_exit(error_code)
if not pre_check():
return conditional_exit(2)
if state_manager.get_item('command') == 'run': if state_manager.get_item('command') == 'run':
import facefusion.uis.core as ui import facefusion.uis.core as ui
if not common_pre_check() or not processors_pre_check(): if not common_pre_check() or not processors_pre_check():
hard_exit(2) return conditional_exit(2)
for ui_layout in ui.get_ui_layouts_modules(state_manager.get_item('ui_layouts')): for ui_layout in ui.get_ui_layouts_modules(state_manager.get_item('ui_layouts')):
if not ui_layout.pre_check(): if not ui_layout.pre_check():
hard_exit(2) return conditional_exit(2)
ui.init()
ui.launch() ui.launch()
if state_manager.get_item('command') == 'headless-run': if state_manager.get_item('command') == 'headless-run':
if not job_manager.init_jobs(state_manager.get_item('jobs_path')): if not job_manager.init_jobs(state_manager.get_item('jobs_path')):
hard_exit(1) hard_exit(1)
error_code = process_headless(args) error_core = process_headless(args)
hard_exit(error_code) hard_exit(error_core)
if state_manager.get_item('command') == 'batch-run':
if not job_manager.init_jobs(state_manager.get_item('jobs_path')):
hard_exit(1)
error_code = process_batch(args)
hard_exit(error_code)
if state_manager.get_item('command') in [ 'job-run', 'job-run-all', 'job-retry', 'job-retry-all' ]: if state_manager.get_item('command') in [ 'job-run', 'job-run-all', 'job-retry', 'job-retry-all' ]:
if not job_manager.init_jobs(state_manager.get_item('jobs_path')): if not job_manager.init_jobs(state_manager.get_item('jobs_path')):
hard_exit(1) hard_exit(1)
@@ -92,22 +79,20 @@ def route(args : Args) -> None:
def pre_check() -> bool: def pre_check() -> bool:
if sys.version_info < (3, 10): if sys.version_info < (3, 9):
logger.error(translator.get('python_not_supported').format(version = '3.10'), __name__) logger.error(wording.get('python_not_supported').format(version = '3.9'), __name__)
return False return False
if not shutil.which('curl'): if not shutil.which('curl'):
logger.error(translator.get('curl_not_installed'), __name__) logger.error(wording.get('curl_not_installed'), __name__)
return False return False
if not shutil.which('ffmpeg'): if not shutil.which('ffmpeg'):
logger.error(translator.get('ffmpeg_not_installed'), __name__) logger.error(wording.get('ffmpeg_not_installed'), __name__)
return False return False
return True return True
def common_pre_check() -> bool: def common_pre_check() -> bool:
common_modules =\ modules =\
[ [
content_analyser, content_analyser,
face_classifier, face_classifier,
@@ -118,10 +103,7 @@ def common_pre_check() -> bool:
voice_extractor voice_extractor
] ]
content_analyser_content = inspect.getsource(content_analyser).encode() return all(module.pre_check() for module in modules)
content_analyser_hash = hash_helper.create_hash(content_analyser_content)
return all(module.pre_check() for module in common_modules)
def processors_pre_check() -> bool: def processors_pre_check() -> bool:
@@ -131,28 +113,64 @@ def processors_pre_check() -> bool:
return True return True
def conditional_process() -> ErrorCode:
start_time = time()
for processor_module in get_processors_modules(state_manager.get_item('processors')):
if not processor_module.pre_process('output'):
return 2
conditional_append_reference_faces()
if is_image(state_manager.get_item('target_path')):
return process_image(start_time)
if is_video(state_manager.get_item('target_path')):
return process_video(start_time)
return 0
def conditional_append_reference_faces() -> None:
if 'reference' in state_manager.get_item('face_selector_mode') and not get_reference_faces():
source_frames = read_static_images(state_manager.get_item('source_paths'))
source_faces = get_many_faces(source_frames)
source_face = get_average_face(source_faces)
if is_video(state_manager.get_item('target_path')):
reference_frame = get_video_frame(state_manager.get_item('target_path'), state_manager.get_item('reference_frame_number'))
else:
reference_frame = read_image(state_manager.get_item('target_path'))
reference_faces = sort_and_filter_faces(get_many_faces([ reference_frame ]))
reference_face = get_one_face(reference_faces, state_manager.get_item('reference_face_position'))
append_reference_face('origin', reference_face)
if source_face and reference_face:
for processor_module in get_processors_modules(state_manager.get_item('processors')):
abstract_reference_frame = processor_module.get_reference_frame(source_face, reference_face, reference_frame)
if numpy.any(abstract_reference_frame):
abstract_reference_faces = sort_and_filter_faces(get_many_faces([ abstract_reference_frame ]))
abstract_reference_face = get_one_face(abstract_reference_faces, state_manager.get_item('reference_face_position'))
append_reference_face(processor_module.__name__, abstract_reference_face)
def force_download() -> ErrorCode: def force_download() -> ErrorCode:
download_directory_path = resolve_relative_path('../.assets/models')
available_processors = list_directory('facefusion/processors/modules')
common_modules =\ common_modules =\
[ [
content_analyser, content_analyser,
face_classifier, face_classifier,
face_detector, face_detector,
face_landmarker, face_landmarker,
face_masker,
face_recognizer, face_recognizer,
face_masker,
voice_extractor voice_extractor
] ]
available_processors = [ get_file_name(file_path) for file_path in resolve_file_paths('facefusion/processors/modules') ]
processor_modules = get_processors_modules(available_processors) processor_modules = get_processors_modules(available_processors)
for module in common_modules + processor_modules: for module in common_modules + processor_modules:
if hasattr(module, 'create_static_model_set'): if hasattr(module, 'MODEL_SET'):
for model in module.create_static_model_set(state_manager.get_item('download_scope')).values(): for model in module.MODEL_SET.values():
model_hash_set = model.get('hashes') model_hashes = model.get('hashes')
model_source_set = model.get('sources') model_sources = model.get('sources')
if model_hash_set and model_source_set: if model_hashes and model_sources:
if not conditional_download_hashes(model_hash_set) or not conditional_download_sources(model_source_set): if not conditional_download_hashes(download_directory_path, model_hashes) or not conditional_download_sources(download_directory_path, model_sources):
return 1 return 1
return 0 return 0
@@ -163,116 +181,117 @@ def route_job_manager(args : Args) -> ErrorCode:
job_headers, job_contents = compose_job_list(state_manager.get_item('job_status')) job_headers, job_contents = compose_job_list(state_manager.get_item('job_status'))
if job_contents: if job_contents:
cli_helper.render_table(job_headers, job_contents) logger.table(job_headers, job_contents)
return 0 return 0
return 1 return 1
if state_manager.get_item('command') == 'job-create': if state_manager.get_item('command') == 'job-create':
if job_manager.create_job(state_manager.get_item('job_id')): if job_manager.create_job(state_manager.get_item('job_id')):
logger.info(translator.get('job_created').format(job_id = state_manager.get_item('job_id')), __name__) logger.info(wording.get('job_created').format(job_id = state_manager.get_item('job_id')), __name__)
return 0 return 0
logger.error(translator.get('job_not_created').format(job_id = state_manager.get_item('job_id')), __name__) logger.error(wording.get('job_not_created').format(job_id = state_manager.get_item('job_id')), __name__)
return 1 return 1
if state_manager.get_item('command') == 'job-submit': if state_manager.get_item('command') == 'job-submit':
if job_manager.submit_job(state_manager.get_item('job_id')): if job_manager.submit_job(state_manager.get_item('job_id')):
logger.info(translator.get('job_submitted').format(job_id = state_manager.get_item('job_id')), __name__) logger.info(wording.get('job_submitted').format(job_id = state_manager.get_item('job_id')), __name__)
return 0 return 0
logger.error(translator.get('job_not_submitted').format(job_id = state_manager.get_item('job_id')), __name__) logger.error(wording.get('job_not_submitted').format(job_id = state_manager.get_item('job_id')), __name__)
return 1 return 1
if state_manager.get_item('command') == 'job-submit-all': if state_manager.get_item('command') == 'job-submit-all':
if job_manager.submit_jobs(state_manager.get_item('halt_on_error')): if job_manager.submit_jobs():
logger.info(translator.get('job_all_submitted'), __name__) logger.info(wording.get('job_all_submitted'), __name__)
return 0 return 0
logger.error(translator.get('job_all_not_submitted'), __name__) logger.error(wording.get('job_all_not_submitted'), __name__)
return 1 return 1
if state_manager.get_item('command') == 'job-delete': if state_manager.get_item('command') == 'job-delete':
if job_manager.delete_job(state_manager.get_item('job_id')): if job_manager.delete_job(state_manager.get_item('job_id')):
logger.info(translator.get('job_deleted').format(job_id = state_manager.get_item('job_id')), __name__) logger.info(wording.get('job_deleted').format(job_id = state_manager.get_item('job_id')), __name__)
return 0 return 0
logger.error(translator.get('job_not_deleted').format(job_id = state_manager.get_item('job_id')), __name__) logger.error(wording.get('job_not_deleted').format(job_id = state_manager.get_item('job_id')), __name__)
return 1 return 1
if state_manager.get_item('command') == 'job-delete-all': if state_manager.get_item('command') == 'job-delete-all':
if job_manager.delete_jobs(state_manager.get_item('halt_on_error')): if job_manager.delete_jobs():
logger.info(translator.get('job_all_deleted'), __name__) logger.info(wording.get('job_all_deleted'), __name__)
return 0 return 0
logger.error(translator.get('job_all_not_deleted'), __name__) logger.error(wording.get('job_all_not_deleted'), __name__)
return 1 return 1
if state_manager.get_item('command') == 'job-add-step': if state_manager.get_item('command') == 'job-add-step':
step_args = reduce_step_args(args) step_args = reduce_step_args(args)
if job_manager.add_step(state_manager.get_item('job_id'), step_args): if job_manager.add_step(state_manager.get_item('job_id'), step_args):
logger.info(translator.get('job_step_added').format(job_id = state_manager.get_item('job_id')), __name__) logger.info(wording.get('job_step_added').format(job_id = state_manager.get_item('job_id')), __name__)
return 0 return 0
logger.error(translator.get('job_step_not_added').format(job_id = state_manager.get_item('job_id')), __name__) logger.error(wording.get('job_step_not_added').format(job_id = state_manager.get_item('job_id')), __name__)
return 1 return 1
if state_manager.get_item('command') == 'job-remix-step': if state_manager.get_item('command') == 'job-remix-step':
step_args = reduce_step_args(args) step_args = reduce_step_args(args)
if job_manager.remix_step(state_manager.get_item('job_id'), state_manager.get_item('step_index'), step_args): if job_manager.remix_step(state_manager.get_item('job_id'), state_manager.get_item('step_index'), step_args):
logger.info(translator.get('job_remix_step_added').format(job_id = state_manager.get_item('job_id'), step_index = state_manager.get_item('step_index')), __name__) logger.info(wording.get('job_remix_step_added').format(job_id = state_manager.get_item('job_id'), step_index = state_manager.get_item('step_index')), __name__)
return 0 return 0
logger.error(translator.get('job_remix_step_not_added').format(job_id = state_manager.get_item('job_id'), step_index = state_manager.get_item('step_index')), __name__) logger.error(wording.get('job_remix_step_not_added').format(job_id = state_manager.get_item('job_id'), step_index = state_manager.get_item('step_index')), __name__)
return 1 return 1
if state_manager.get_item('command') == 'job-insert-step': if state_manager.get_item('command') == 'job-insert-step':
step_args = reduce_step_args(args) step_args = reduce_step_args(args)
if job_manager.insert_step(state_manager.get_item('job_id'), state_manager.get_item('step_index'), step_args): if job_manager.insert_step(state_manager.get_item('job_id'), state_manager.get_item('step_index'), step_args):
logger.info(translator.get('job_step_inserted').format(job_id = state_manager.get_item('job_id'), step_index = state_manager.get_item('step_index')), __name__) logger.info(wording.get('job_step_inserted').format(job_id = state_manager.get_item('job_id'), step_index = state_manager.get_item('step_index')), __name__)
return 0 return 0
logger.error(translator.get('job_step_not_inserted').format(job_id = state_manager.get_item('job_id'), step_index = state_manager.get_item('step_index')), __name__) logger.error(wording.get('job_step_not_inserted').format(job_id = state_manager.get_item('job_id'), step_index = state_manager.get_item('step_index')), __name__)
return 1 return 1
if state_manager.get_item('command') == 'job-remove-step': if state_manager.get_item('command') == 'job-remove-step':
if job_manager.remove_step(state_manager.get_item('job_id'), state_manager.get_item('step_index')): if job_manager.remove_step(state_manager.get_item('job_id'), state_manager.get_item('step_index')):
logger.info(translator.get('job_step_removed').format(job_id = state_manager.get_item('job_id'), step_index = state_manager.get_item('step_index')), __name__) logger.info(wording.get('job_step_removed').format(job_id = state_manager.get_item('job_id'), step_index = state_manager.get_item('step_index')), __name__)
return 0 return 0
logger.error(translator.get('job_step_not_removed').format(job_id = state_manager.get_item('job_id'), step_index = state_manager.get_item('step_index')), __name__) logger.error(wording.get('job_step_not_removed').format(job_id = state_manager.get_item('job_id'), step_index = state_manager.get_item('step_index')), __name__)
return 1 return 1
return 1 return 1
def route_job_runner() -> ErrorCode: def route_job_runner() -> ErrorCode:
if state_manager.get_item('command') == 'job-run': if state_manager.get_item('command') == 'job-run':
logger.info(translator.get('running_job').format(job_id = state_manager.get_item('job_id')), __name__) logger.info(wording.get('running_job').format(job_id = state_manager.get_item('job_id')), __name__)
if job_runner.run_job(state_manager.get_item('job_id'), process_step): if job_runner.run_job(state_manager.get_item('job_id'), process_step):
logger.info(translator.get('processing_job_succeeded').format(job_id = state_manager.get_item('job_id')), __name__) logger.info(wording.get('processing_job_succeed').format(job_id = state_manager.get_item('job_id')), __name__)
return 0 return 0
logger.info(translator.get('processing_job_failed').format(job_id = state_manager.get_item('job_id')), __name__) logger.info(wording.get('processing_job_failed').format(job_id = state_manager.get_item('job_id')), __name__)
return 1 return 1
if state_manager.get_item('command') == 'job-run-all': if state_manager.get_item('command') == 'job-run-all':
logger.info(translator.get('running_jobs'), __name__) logger.info(wording.get('running_jobs'), __name__)
if job_runner.run_jobs(process_step, state_manager.get_item('halt_on_error')): if job_runner.run_jobs(process_step):
logger.info(translator.get('processing_jobs_succeeded'), __name__) logger.info(wording.get('processing_jobs_succeed'), __name__)
return 0 return 0
logger.info(translator.get('processing_jobs_failed'), __name__) logger.info(wording.get('processing_jobs_failed'), __name__)
return 1 return 1
if state_manager.get_item('command') == 'job-retry': if state_manager.get_item('command') == 'job-retry':
logger.info(translator.get('retrying_job').format(job_id = state_manager.get_item('job_id')), __name__) logger.info(wording.get('retrying_job').format(job_id = state_manager.get_item('job_id')), __name__)
if job_runner.retry_job(state_manager.get_item('job_id'), process_step): if job_runner.retry_job(state_manager.get_item('job_id'), process_step):
logger.info(translator.get('processing_job_succeeded').format(job_id = state_manager.get_item('job_id')), __name__) logger.info(wording.get('processing_job_succeed').format(job_id = state_manager.get_item('job_id')), __name__)
return 0 return 0
logger.info(translator.get('processing_job_failed').format(job_id = state_manager.get_item('job_id')), __name__) logger.info(wording.get('processing_job_failed').format(job_id = state_manager.get_item('job_id')), __name__)
return 1 return 1
if state_manager.get_item('command') == 'job-retry-all': if state_manager.get_item('command') == 'job-retry-all':
logger.info(translator.get('retrying_jobs'), __name__) logger.info(wording.get('retrying_jobs'), __name__)
if job_runner.retry_jobs(process_step, state_manager.get_item('halt_on_error')): if job_runner.retry_jobs(process_step):
logger.info(translator.get('processing_jobs_succeeded'), __name__) logger.info(wording.get('processing_jobs_succeed'), __name__)
return 0 return 0
logger.info(translator.get('processing_jobs_failed'), __name__) logger.info(wording.get('processing_jobs_failed'), __name__)
return 1 return 1
return 2 return 2
def process_step(job_id : str, step_index : int, step_args : Args) -> bool:
clear_reference_faces()
step_total = job_manager.count_step_total(job_id)
step_args.update(collect_job_args())
apply_args(step_args, state_manager.set_item)
logger.info(wording.get('processing_step').format(step_current = step_index + 1, step_total = step_total), __name__)
if common_pre_check() and processors_pre_check():
error_code = conditional_process()
return error_code == 0
return False
def process_headless(args : Args) -> ErrorCode: def process_headless(args : Args) -> ErrorCode:
job_id = job_helper.suggest_job_id('headless') job_id = job_helper.suggest_job_id('headless')
step_args = reduce_step_args(args) step_args = reduce_step_args(args)
@@ -282,69 +301,145 @@ def process_headless(args : Args) -> ErrorCode:
return 1 return 1
def process_batch(args : Args) -> ErrorCode: def process_image(start_time : float) -> ErrorCode:
job_id = job_helper.suggest_job_id('batch') if analyse_image(state_manager.get_item('target_path')):
step_args = reduce_step_args(args) return 3
job_args = reduce_job_args(args) # clear temp
source_paths = resolve_file_pattern(job_args.get('source_pattern')) logger.debug(wording.get('clearing_temp'), __name__)
target_paths = resolve_file_pattern(job_args.get('target_pattern')) clear_temp_directory(state_manager.get_item('target_path'))
# create temp
if job_manager.create_job(job_id): logger.debug(wording.get('creating_temp'), __name__)
if source_paths and target_paths: create_temp_directory(state_manager.get_item('target_path'))
for index, (source_path, target_path) in enumerate(itertools.product(source_paths, target_paths)): # copy image
step_args['source_paths'] = [ source_path ] process_manager.start()
step_args['target_path'] = target_path temp_image_resolution = pack_resolution(restrict_image_resolution(state_manager.get_item('target_path'), unpack_resolution(state_manager.get_item('output_image_resolution'))))
logger.info(wording.get('copying_image').format(resolution = temp_image_resolution), __name__)
try: if copy_image(state_manager.get_item('target_path'), temp_image_resolution):
step_args['output_path'] = job_args.get('output_pattern').format(index = index, source_name = get_file_name(source_path), target_name = get_file_name(target_path), target_extension = get_file_extension(target_path)) logger.debug(wording.get('copying_image_succeed'), __name__)
except KeyError: else:
logger.error(wording.get('copying_image_failed'), __name__)
process_manager.end()
return 1 return 1
# process image
if not job_manager.add_step(job_id, step_args): temp_file_path = get_temp_file_path(state_manager.get_item('target_path'))
return 1
if job_manager.submit_job(job_id) and job_runner.run_job(job_id, process_step):
return 0
if not source_paths and target_paths:
for index, target_path in enumerate(target_paths):
step_args['target_path'] = target_path
try:
step_args['output_path'] = job_args.get('output_pattern').format(index = index, target_name = get_file_name(target_path), target_extension = get_file_extension(target_path))
except KeyError:
return 1
if not job_manager.add_step(job_id, step_args):
return 1
if job_manager.submit_job(job_id) and job_runner.run_job(job_id, process_step):
return 0
return 1
def process_step(job_id : str, step_index : int, step_args : Args) -> bool:
step_total = job_manager.count_step_total(job_id)
step_args.update(collect_job_args())
apply_args(step_args, state_manager.set_item)
logger.info(translator.get('processing_step').format(step_current = step_index + 1, step_total = step_total), __name__)
if common_pre_check() and processors_pre_check():
error_code = conditional_process()
return error_code == 0
return False
def conditional_process() -> ErrorCode:
start_time = time()
for processor_module in get_processors_modules(state_manager.get_item('processors')): for processor_module in get_processors_modules(state_manager.get_item('processors')):
if not processor_module.pre_process('output'): logger.info(wording.get('processing'), processor_module.__name__)
return 2 processor_module.process_image(state_manager.get_item('source_paths'), temp_file_path, temp_file_path)
processor_module.post_process()
if is_image(state_manager.get_item('target_path')): if is_process_stopping():
return image_to_image.process(start_time) process_manager.end()
if is_video(state_manager.get_item('target_path')): return 4
return image_to_video.process(start_time) # finalize image
logger.info(wording.get('finalizing_image').format(resolution = state_manager.get_item('output_image_resolution')), __name__)
if finalize_image(state_manager.get_item('target_path'), state_manager.get_item('output_path'), state_manager.get_item('output_image_resolution')):
logger.debug(wording.get('finalizing_image_succeed'), __name__)
else:
logger.warn(wording.get('finalizing_image_skipped'), __name__)
# clear temp
logger.debug(wording.get('clearing_temp'), __name__)
clear_temp_directory(state_manager.get_item('target_path'))
# validate image
if is_image(state_manager.get_item('output_path')):
seconds = '{:.2f}'.format((time() - start_time) % 60)
logger.info(wording.get('processing_image_succeed').format(seconds = seconds), __name__)
conditional_log_statistics()
else:
logger.error(wording.get('processing_image_failed'), __name__)
process_manager.end()
return 1
process_manager.end()
return 0 return 0
def process_video(start_time : float) -> ErrorCode:
if analyse_video(state_manager.get_item('target_path'), state_manager.get_item('trim_frame_start'), state_manager.get_item('trim_frame_end')):
return 3
# clear temp
logger.debug(wording.get('clearing_temp'), __name__)
clear_temp_directory(state_manager.get_item('target_path'))
# create temp
logger.debug(wording.get('creating_temp'), __name__)
create_temp_directory(state_manager.get_item('target_path'))
# extract frames
process_manager.start()
temp_video_resolution = pack_resolution(restrict_video_resolution(state_manager.get_item('target_path'), unpack_resolution(state_manager.get_item('output_video_resolution'))))
temp_video_fps = restrict_video_fps(state_manager.get_item('target_path'), state_manager.get_item('output_video_fps'))
logger.info(wording.get('extracting_frames').format(resolution = temp_video_resolution, fps = temp_video_fps), __name__)
if extract_frames(state_manager.get_item('target_path'), temp_video_resolution, temp_video_fps):
logger.debug(wording.get('extracting_frames_succeed'), __name__)
else:
if is_process_stopping():
process_manager.end()
return 4
logger.error(wording.get('extracting_frames_failed'), __name__)
process_manager.end()
return 1
# process frames
temp_frame_paths = get_temp_frame_paths(state_manager.get_item('target_path'))
if temp_frame_paths:
for processor_module in get_processors_modules(state_manager.get_item('processors')):
logger.info(wording.get('processing'), processor_module.__name__)
processor_module.process_video(state_manager.get_item('source_paths'), temp_frame_paths)
processor_module.post_process()
if is_process_stopping():
return 4
else:
logger.error(wording.get('temp_frames_not_found'), __name__)
process_manager.end()
return 1
# merge video
logger.info(wording.get('merging_video').format(resolution = state_manager.get_item('output_video_resolution'), fps = state_manager.get_item('output_video_fps')), __name__)
if merge_video(state_manager.get_item('target_path'), state_manager.get_item('output_video_resolution'), state_manager.get_item('output_video_fps')):
logger.debug(wording.get('merging_video_succeed'), __name__)
else:
if is_process_stopping():
process_manager.end()
return 4
logger.error(wording.get('merging_video_failed'), __name__)
process_manager.end()
return 1
# handle audio
if state_manager.get_item('skip_audio'):
logger.info(wording.get('skipping_audio'), __name__)
move_temp_file(state_manager.get_item('target_path'), state_manager.get_item('output_path'))
else:
source_audio_path = get_first(filter_audio_paths(state_manager.get_item('source_paths')))
if source_audio_path:
if replace_audio(state_manager.get_item('target_path'), source_audio_path, state_manager.get_item('output_path')):
logger.debug(wording.get('replacing_audio_succeed'), __name__)
else:
if is_process_stopping():
process_manager.end()
return 4
logger.warn(wording.get('replacing_audio_skipped'), __name__)
move_temp_file(state_manager.get_item('target_path'), state_manager.get_item('output_path'))
else:
if restore_audio(state_manager.get_item('target_path'), state_manager.get_item('output_path'), state_manager.get_item('output_video_fps')):
logger.debug(wording.get('restoring_audio_succeed'), __name__)
else:
if is_process_stopping():
process_manager.end()
return 4
logger.warn(wording.get('restoring_audio_skipped'), __name__)
move_temp_file(state_manager.get_item('target_path'), state_manager.get_item('output_path'))
# clear temp
logger.debug(wording.get('clearing_temp'), __name__)
clear_temp_directory(state_manager.get_item('target_path'))
# validate video
if is_video(state_manager.get_item('output_path')):
seconds = '{:.2f}'.format((time() - start_time))
logger.info(wording.get('processing_video_succeed').format(seconds = seconds), __name__)
conditional_log_statistics()
else:
logger.error(wording.get('processing_video_failed'), __name__)
process_manager.end()
return 1
process_manager.end()
return 0
def is_process_stopping() -> bool:
if process_manager.is_stopping():
process_manager.end()
logger.info(wording.get('processing_stopped'), __name__)
return process_manager.is_pending()

View File

@@ -1,28 +0,0 @@
import itertools
import shutil
from typing import List
from facefusion import metadata
from facefusion.types import Command
def run(commands : List[Command]) -> List[Command]:
user_agent = metadata.get('name') + '/' + metadata.get('version')
return [ shutil.which('curl'), '--user-agent', user_agent, '--insecure', '--location', '--silent' ] + commands
def chain(*commands : List[Command]) -> List[Command]:
return list(itertools.chain(*commands))
def head(url : str) -> List[Command]:
return [ '-I', url ]
def download(url : str, download_file_path : str) -> List[Command]:
return [ '--create-dirs', '--continue-at', '-', '--output', download_file_path, url ]
def set_timeout(timeout : int) -> List[Command]:
return [ '--connect-timeout', str(timeout) ]

View File

@@ -1,18 +1,13 @@
from datetime import datetime, timedelta from datetime import datetime, timedelta
from time import time
from typing import Optional, Tuple from typing import Optional, Tuple
from facefusion import translator from facefusion import wording
def get_current_date_time() -> datetime: def get_current_date_time() -> datetime:
return datetime.now().astimezone() return datetime.now().astimezone()
def calculate_end_time(start_time : float) -> float:
return round(time() - start_time, 2)
def split_time_delta(time_delta : timedelta) -> Tuple[int, int, int, int]: def split_time_delta(time_delta : timedelta) -> Tuple[int, int, int, int]:
days, hours = divmod(time_delta.total_seconds(), 86400) days, hours = divmod(time_delta.total_seconds(), 86400)
hours, minutes = divmod(hours, 3600) hours, minutes = divmod(hours, 3600)
@@ -25,9 +20,9 @@ def describe_time_ago(date_time : datetime) -> Optional[str]:
days, hours, minutes, _ = split_time_delta(time_ago) days, hours, minutes, _ = split_time_delta(time_ago)
if timedelta(days = 1) < time_ago: if timedelta(days = 1) < time_ago:
return translator.get('time_ago_days').format(days = days, hours = hours, minutes = minutes) return wording.get('time_ago_days').format(days = days, hours = hours, minutes = minutes)
if timedelta(hours = 1) < time_ago: if timedelta(hours = 1) < time_ago:
return translator.get('time_ago_hours').format(hours = hours, minutes = minutes) return wording.get('time_ago_hours').format(hours = hours, minutes = minutes)
if timedelta(minutes = 1) < time_ago: if timedelta(minutes = 1) < time_ago:
return translator.get('time_ago_minutes').format(minutes = minutes) return wording.get('time_ago_minutes').format(minutes = minutes)
return translator.get('time_ago_now') return wording.get('time_ago_now')

View File

@@ -1,21 +1,22 @@
import os import os
import shutil
import ssl
import subprocess import subprocess
import urllib.request
from functools import lru_cache from functools import lru_cache
from typing import List, Optional, Tuple from typing import List, Tuple
from urllib.parse import urlparse from urllib.parse import urlparse
from tqdm import tqdm from tqdm import tqdm
import facefusion.choices from facefusion import logger, process_manager, state_manager, wording
from facefusion import curl_builder, logger, process_manager, state_manager, translator from facefusion.common_helper import is_macos
from facefusion.filesystem import get_file_name, get_file_size, is_file, remove_file from facefusion.filesystem import get_file_size, is_file, remove_file
from facefusion.hash_helper import validate_hash from facefusion.hash_helper import validate_hash
from facefusion.types import Command, DownloadProvider, DownloadSet from facefusion.typing import DownloadSet
if is_macos():
def open_curl(commands : List[Command]) -> subprocess.Popen[bytes]: ssl._create_default_https_context = ssl._create_unverified_context
commands = curl_builder.run(commands)
return subprocess.Popen(commands, stdin = subprocess.PIPE, stdout = subprocess.PIPE)
def conditional_download(download_directory_path : str, urls : List[str]) -> None: def conditional_download(download_directory_path : str, urls : List[str]) -> None:
@@ -23,104 +24,83 @@ def conditional_download(download_directory_path : str, urls : List[str]) -> Non
download_file_name = os.path.basename(urlparse(url).path) download_file_name = os.path.basename(urlparse(url).path)
download_file_path = os.path.join(download_directory_path, download_file_name) download_file_path = os.path.join(download_directory_path, download_file_name)
initial_size = get_file_size(download_file_path) initial_size = get_file_size(download_file_path)
download_size = get_static_download_size(url) download_size = get_download_size(url)
if initial_size < download_size: if initial_size < download_size:
with tqdm(total = download_size, initial = initial_size, desc = translator.get('downloading'), unit = 'B', unit_scale = True, unit_divisor = 1024, ascii = ' =', disable = state_manager.get_item('log_level') in [ 'warn', 'error' ]) as progress: with tqdm(total = download_size, initial = initial_size, desc = wording.get('downloading'), unit = 'B', unit_scale = True, unit_divisor = 1024, ascii = ' =', disable = state_manager.get_item('log_level') in [ 'warn', 'error' ]) as progress:
commands = curl_builder.chain( subprocess.Popen([ shutil.which('curl'), '--create-dirs', '--silent', '--insecure', '--location', '--continue-at', '-', '--output', download_file_path, url ])
curl_builder.download(url, download_file_path),
curl_builder.set_timeout(5)
)
open_curl(commands)
current_size = initial_size current_size = initial_size
progress.set_postfix(download_providers = state_manager.get_item('download_providers'), file_name = download_file_name)
progress.set_postfix(file = download_file_name)
while current_size < download_size: while current_size < download_size:
if is_file(download_file_path): if is_file(download_file_path):
current_size = get_file_size(download_file_path) current_size = get_file_size(download_file_path)
progress.update(current_size - progress.n) progress.update(current_size - progress.n)
@lru_cache(maxsize = 64) @lru_cache(maxsize = None)
def get_static_download_size(url : str) -> int: def get_download_size(url : str) -> int:
commands = curl_builder.chain( try:
curl_builder.head(url), response = urllib.request.urlopen(url, timeout = 10)
curl_builder.set_timeout(5) content_length = response.headers.get('Content-Length')
)
process = open_curl(commands)
lines = reversed(process.stdout.readlines())
for line in lines:
__line__ = line.decode().lower()
if 'content-length:' in __line__:
_, content_length = __line__.split('content-length:')
return int(content_length) return int(content_length)
except (OSError, TypeError, ValueError):
return 0 return 0
@lru_cache(maxsize = 64) def is_download_done(url : str, file_path : str) -> bool:
def ping_static_url(url : str) -> bool: if is_file(file_path):
commands = curl_builder.chain( return get_download_size(url) == get_file_size(file_path)
curl_builder.head(url), return False
curl_builder.set_timeout(5)
)
process = open_curl(commands)
process.communicate()
return process.returncode == 0
def conditional_download_hashes(hash_set : DownloadSet) -> bool: def conditional_download_hashes(download_directory_path : str, hashes : DownloadSet) -> bool:
hash_paths = [ hash_set.get(hash_key).get('path') for hash_key in hash_set.keys() ] hash_paths = [ hashes.get(hash_key).get('path') for hash_key in hashes.keys() ]
process_manager.check() process_manager.check()
if not state_manager.get_item('skip_download'):
_, invalid_hash_paths = validate_hash_paths(hash_paths) _, invalid_hash_paths = validate_hash_paths(hash_paths)
if invalid_hash_paths: if invalid_hash_paths:
for index in hash_set: for index in hashes:
if hash_set.get(index).get('path') in invalid_hash_paths: if hashes.get(index).get('path') in invalid_hash_paths:
invalid_hash_url = hash_set.get(index).get('url') invalid_hash_url = hashes.get(index).get('url')
if invalid_hash_url:
download_directory_path = os.path.dirname(hash_set.get(index).get('path'))
conditional_download(download_directory_path, [ invalid_hash_url ]) conditional_download(download_directory_path, [ invalid_hash_url ])
valid_hash_paths, invalid_hash_paths = validate_hash_paths(hash_paths) valid_hash_paths, invalid_hash_paths = validate_hash_paths(hash_paths)
for valid_hash_path in valid_hash_paths: for valid_hash_path in valid_hash_paths:
valid_hash_file_name = get_file_name(valid_hash_path) valid_hash_file_name, _ = os.path.splitext(os.path.basename(valid_hash_path))
logger.debug(translator.get('validating_hash_succeeded').format(hash_file_name = valid_hash_file_name), __name__) logger.debug(wording.get('validating_hash_succeed').format(hash_file_name = valid_hash_file_name), __name__)
for invalid_hash_path in invalid_hash_paths: for invalid_hash_path in invalid_hash_paths:
invalid_hash_file_name = get_file_name(invalid_hash_path) invalid_hash_file_name, _ = os.path.splitext(os.path.basename(invalid_hash_path))
logger.error(translator.get('validating_hash_failed').format(hash_file_name = invalid_hash_file_name), __name__) logger.error(wording.get('validating_hash_failed').format(hash_file_name = invalid_hash_file_name), __name__)
if not invalid_hash_paths: if not invalid_hash_paths:
process_manager.end() process_manager.end()
return not invalid_hash_paths return not invalid_hash_paths
def conditional_download_sources(source_set : DownloadSet) -> bool: def conditional_download_sources(download_directory_path : str, sources : DownloadSet) -> bool:
source_paths = [ source_set.get(source_key).get('path') for source_key in source_set.keys() ] source_paths = [ sources.get(source_key).get('path') for source_key in sources.keys() ]
process_manager.check() process_manager.check()
if not state_manager.get_item('skip_download'):
_, invalid_source_paths = validate_source_paths(source_paths) _, invalid_source_paths = validate_source_paths(source_paths)
if invalid_source_paths: if invalid_source_paths:
for index in source_set: for index in sources:
if source_set.get(index).get('path') in invalid_source_paths: if sources.get(index).get('path') in invalid_source_paths:
invalid_source_url = source_set.get(index).get('url') invalid_source_url = sources.get(index).get('url')
if invalid_source_url:
download_directory_path = os.path.dirname(source_set.get(index).get('path'))
conditional_download(download_directory_path, [ invalid_source_url ]) conditional_download(download_directory_path, [ invalid_source_url ])
valid_source_paths, invalid_source_paths = validate_source_paths(source_paths) valid_source_paths, invalid_source_paths = validate_source_paths(source_paths)
for valid_source_path in valid_source_paths: for valid_source_path in valid_source_paths:
valid_source_file_name = get_file_name(valid_source_path) valid_source_file_name, _ = os.path.splitext(os.path.basename(valid_source_path))
logger.debug(translator.get('validating_source_succeeded').format(source_file_name = valid_source_file_name), __name__) logger.debug(wording.get('validating_source_succeed').format(source_file_name = valid_source_file_name), __name__)
for invalid_source_path in invalid_source_paths: for invalid_source_path in invalid_source_paths:
invalid_source_file_name = get_file_name(invalid_source_path) invalid_source_file_name, _ = os.path.splitext(os.path.basename(invalid_source_path))
logger.error(translator.get('validating_source_failed').format(source_file_name = invalid_source_file_name), __name__) logger.error(wording.get('validating_source_failed').format(source_file_name = invalid_source_file_name), __name__)
if remove_file(invalid_source_path): if remove_file(invalid_source_path):
logger.error(translator.get('deleting_corrupt_source').format(source_file_name = invalid_source_file_name), __name__) logger.error(wording.get('deleting_corrupt_source').format(source_file_name = invalid_source_file_name), __name__)
if not invalid_source_paths: if not invalid_source_paths:
process_manager.end() process_manager.end()
@@ -136,7 +116,6 @@ def validate_hash_paths(hash_paths : List[str]) -> Tuple[List[str], List[str]]:
valid_hash_paths.append(hash_path) valid_hash_paths.append(hash_path)
else: else:
invalid_hash_paths.append(hash_path) invalid_hash_paths.append(hash_path)
return valid_hash_paths, invalid_hash_paths return valid_hash_paths, invalid_hash_paths
@@ -149,26 +128,4 @@ def validate_source_paths(source_paths : List[str]) -> Tuple[List[str], List[str
valid_source_paths.append(source_path) valid_source_paths.append(source_path)
else: else:
invalid_source_paths.append(source_path) invalid_source_paths.append(source_path)
return valid_source_paths, invalid_source_paths return valid_source_paths, invalid_source_paths
def resolve_download_url(base_name : str, file_name : str) -> Optional[str]:
download_providers = state_manager.get_item('download_providers')
for download_provider in download_providers:
download_url = resolve_download_url_by_provider(download_provider, base_name, file_name)
if download_url:
return download_url
return None
def resolve_download_url_by_provider(download_provider : DownloadProvider, base_name : str, file_name : str) -> Optional[str]:
download_provider_value = facefusion.choices.download_provider_set.get(download_provider)
for download_provider_url in download_provider_value.get('urls'):
if ping_static_url(download_provider_url):
return download_provider_url + download_provider_value.get('path').format(base_name = base_name, file_name = file_name)
return None

View File

@@ -1,45 +1,46 @@
import shutil
import subprocess import subprocess
import xml.etree.ElementTree as ElementTree import xml.etree.ElementTree as ElementTree
from functools import lru_cache from functools import lru_cache
from typing import List, Optional from typing import Any, List
from onnxruntime import get_available_providers, set_default_logger_severity from onnxruntime import get_available_providers, set_default_logger_severity
import facefusion.choices from facefusion.choices import execution_provider_set
from facefusion.types import ExecutionDevice, ExecutionProvider, InferenceSessionProvider, ValueAndUnit from facefusion.typing import ExecutionDevice, ExecutionProviderKey, ExecutionProviderSet, ValueAndUnit
set_default_logger_severity(3) set_default_logger_severity(3)
def has_execution_provider(execution_provider : ExecutionProvider) -> bool: def get_execution_provider_choices() -> List[ExecutionProviderKey]:
return execution_provider in get_available_execution_providers() return list(get_available_execution_provider_set().keys())
def get_available_execution_providers() -> List[ExecutionProvider]: def has_execution_provider(execution_provider_key : ExecutionProviderKey) -> bool:
inference_session_providers = get_available_providers() return execution_provider_key in get_execution_provider_choices()
available_execution_providers : List[ExecutionProvider] = []
for execution_provider, execution_provider_value in facefusion.choices.execution_provider_set.items():
if execution_provider_value in inference_session_providers:
index = facefusion.choices.execution_providers.index(execution_provider)
available_execution_providers.insert(index, execution_provider)
return available_execution_providers
def create_inference_session_providers(execution_device_id : int, execution_providers : List[ExecutionProvider]) -> List[InferenceSessionProvider]: def get_available_execution_provider_set() -> ExecutionProviderSet:
inference_session_providers : List[InferenceSessionProvider] = [] available_execution_providers = get_available_providers()
available_execution_provider_set : ExecutionProviderSet = {}
for execution_provider in execution_providers: for execution_provider_key, execution_provider_value in execution_provider_set.items():
if execution_provider == 'cuda': if execution_provider_value in available_execution_providers:
inference_session_providers.append((facefusion.choices.execution_provider_set.get(execution_provider), available_execution_provider_set[execution_provider_key] = execution_provider_value
return available_execution_provider_set
def create_execution_providers(execution_device_id : str, execution_provider_keys : List[ExecutionProviderKey]) -> List[Any]:
execution_providers : List[Any] = []
for execution_provider_key in execution_provider_keys:
if execution_provider_key == 'cuda':
execution_providers.append((execution_provider_set.get(execution_provider_key),
{ {
'device_id': execution_device_id, 'device_id': execution_device_id,
'cudnn_conv_algo_search': resolve_cudnn_conv_algo_search() 'cudnn_conv_algo_search': 'EXHAUSTIVE' if use_exhaustive() else 'DEFAULT'
})) }))
if execution_provider == 'tensorrt': if execution_provider_key == 'tensorrt':
inference_session_providers.append((facefusion.choices.execution_provider_set.get(execution_provider), execution_providers.append((execution_provider_set.get(execution_provider_key),
{ {
'device_id': execution_device_id, 'device_id': execution_device_id,
'trt_engine_cache_enable': True, 'trt_engine_cache_enable': True,
@@ -48,59 +49,39 @@ def create_inference_session_providers(execution_device_id : int, execution_prov
'trt_timing_cache_path': '.caches', 'trt_timing_cache_path': '.caches',
'trt_builder_optimization_level': 5 'trt_builder_optimization_level': 5
})) }))
if execution_provider in [ 'directml', 'rocm' ]: if execution_provider_key == 'openvino':
inference_session_providers.append((facefusion.choices.execution_provider_set.get(execution_provider), execution_providers.append((execution_provider_set.get(execution_provider_key),
{
'device_type': 'GPU.' + execution_device_id,
'precision': 'FP32'
}))
if execution_provider_key in [ 'directml', 'rocm' ]:
execution_providers.append((execution_provider_set.get(execution_provider_key),
{ {
'device_id': execution_device_id 'device_id': execution_device_id
})) }))
if execution_provider == 'migraphx': if execution_provider_key == 'coreml':
inference_session_providers.append((facefusion.choices.execution_provider_set.get(execution_provider), execution_providers.append(execution_provider_set.get(execution_provider_key))
{
'device_id': execution_device_id,
'migraphx_model_cache_dir': '.caches'
}))
if execution_provider == 'openvino':
inference_session_providers.append((facefusion.choices.execution_provider_set.get(execution_provider),
{
'device_type': resolve_openvino_device_type(execution_device_id),
'precision': 'FP32'
}))
if execution_provider == 'coreml':
inference_session_providers.append((facefusion.choices.execution_provider_set.get(execution_provider),
{
'SpecializationStrategy': 'FastPrediction',
'ModelCacheDirectory': '.caches'
}))
if 'cpu' in execution_providers: if 'cpu' in execution_provider_keys:
inference_session_providers.append(facefusion.choices.execution_provider_set.get('cpu')) execution_providers.append(execution_provider_set.get('cpu'))
return inference_session_providers return execution_providers
def resolve_cudnn_conv_algo_search() -> str: def use_exhaustive() -> bool:
execution_devices = detect_static_execution_devices() execution_devices = detect_static_execution_devices()
product_names = ('GeForce GTX 1630', 'GeForce GTX 1650', 'GeForce GTX 1660') product_names = ('GeForce GTX 1630', 'GeForce GTX 1650', 'GeForce GTX 1660')
for execution_device in execution_devices: return any(execution_device.get('product').get('name').startswith(product_names) for execution_device in execution_devices)
if execution_device.get('product').get('name').startswith(product_names):
return 'DEFAULT'
return 'EXHAUSTIVE'
def resolve_openvino_device_type(execution_device_id : int) -> str:
if execution_device_id == 0:
return 'GPU'
return 'GPU.' + str(execution_device_id)
def run_nvidia_smi() -> subprocess.Popen[bytes]: def run_nvidia_smi() -> subprocess.Popen[bytes]:
commands = [ shutil.which('nvidia-smi'), '--query', '--xml-format' ] commands = [ 'nvidia-smi', '--query', '--xml-format' ]
return subprocess.Popen(commands, stdout = subprocess.PIPE) return subprocess.Popen(commands, stdout = subprocess.PIPE)
@lru_cache() @lru_cache(maxsize = None)
def detect_static_execution_devices() -> List[ExecutionDevice]: def detect_static_execution_devices() -> List[ExecutionDevice]:
return detect_execution_devices() return detect_execution_devices()
@@ -117,44 +98,37 @@ def detect_execution_devices() -> List[ExecutionDevice]:
for gpu_element in root_element.findall('gpu'): for gpu_element in root_element.findall('gpu'):
execution_devices.append( execution_devices.append(
{ {
'driver_version': root_element.findtext('driver_version'), 'driver_version': root_element.find('driver_version').text,
'framework': 'framework':
{ {
'name': 'CUDA', 'name': 'CUDA',
'version': root_element.findtext('cuda_version') 'version': root_element.find('cuda_version').text
}, },
'product': 'product':
{ {
'vendor': 'NVIDIA', 'vendor': 'NVIDIA',
'name': gpu_element.findtext('product_name').replace('NVIDIA', '').strip() 'name': gpu_element.find('product_name').text.replace('NVIDIA ', '')
}, },
'video_memory': 'video_memory':
{ {
'total': create_value_and_unit(gpu_element.findtext('fb_memory_usage/total')), 'total': create_value_and_unit(gpu_element.find('fb_memory_usage/total').text),
'free': create_value_and_unit(gpu_element.findtext('fb_memory_usage/free')) 'free': create_value_and_unit(gpu_element.find('fb_memory_usage/free').text)
},
'temperature':
{
'gpu': create_value_and_unit(gpu_element.findtext('temperature/gpu_temp')),
'memory': create_value_and_unit(gpu_element.findtext('temperature/memory_temp'))
}, },
'utilization': 'utilization':
{ {
'gpu': create_value_and_unit(gpu_element.findtext('utilization/gpu_util')), 'gpu': create_value_and_unit(gpu_element.find('utilization/gpu_util').text),
'memory': create_value_and_unit(gpu_element.findtext('utilization/memory_util')) 'memory': create_value_and_unit(gpu_element.find('utilization/memory_util').text)
} }
}) })
return execution_devices return execution_devices
def create_value_and_unit(text : str) -> Optional[ValueAndUnit]: def create_value_and_unit(text : str) -> ValueAndUnit:
if ' ' in text:
value, unit = text.split() value, unit = text.split()
value_and_unit : ValueAndUnit =\
return\
{ {
'value': int(value), 'value': int(value),
'unit': str(unit) 'unit': str(unit)
} }
return None
return value_and_unit

View File

@@ -1,34 +1,24 @@
import os
import signal
import sys import sys
from time import sleep from time import sleep
from types import FrameType
from facefusion import process_manager, state_manager from facefusion import process_manager, state_manager
from facefusion.temp_helper import clear_temp_directory from facefusion.temp_helper import clear_temp_directory
from facefusion.types import ErrorCode from facefusion.typing import ErrorCode
def fatal_exit(error_code : ErrorCode) -> None:
os._exit(error_code)
def hard_exit(error_code : ErrorCode) -> None: def hard_exit(error_code : ErrorCode) -> None:
sys.exit(error_code) sys.exit(error_code)
def signal_exit(signum : int, frame : FrameType) -> None: def conditional_exit(error_code : ErrorCode) -> None:
graceful_exit(0) if state_manager.get_item('command') == 'headless-run':
hard_exit(error_code)
def graceful_exit(error_code : ErrorCode) -> None: def graceful_exit(error_code : ErrorCode) -> None:
signal.signal(signal.SIGINT, signal.SIG_IGN)
process_manager.stop() process_manager.stop()
while process_manager.is_processing(): while process_manager.is_processing():
sleep(0.5) sleep(0.5)
if state_manager.get_item('target_path'): if state_manager.get_item('target_path'):
clear_temp_directory(state_manager.get_item('target_path')) clear_temp_directory(state_manager.get_item('target_path'))
hard_exit(error_code) hard_exit(error_code)

View File

@@ -5,12 +5,12 @@ import numpy
from facefusion import state_manager from facefusion import state_manager
from facefusion.common_helper import get_first from facefusion.common_helper import get_first
from facefusion.face_classifier import classify_face from facefusion.face_classifier import classify_face
from facefusion.face_detector import detect_faces, detect_faces_by_angle from facefusion.face_detector import detect_faces, detect_rotated_faces
from facefusion.face_helper import apply_nms, convert_to_face_landmark_5, estimate_face_angle, get_nms_threshold from facefusion.face_helper import apply_nms, convert_to_face_landmark_5, estimate_face_angle, get_nms_threshold
from facefusion.face_landmarker import detect_face_landmark, estimate_face_landmark_68_5 from facefusion.face_landmarker import detect_face_landmarks, estimate_face_landmark_68_5
from facefusion.face_recognizer import calculate_face_embedding from facefusion.face_recognizer import calc_embedding
from facefusion.face_store import get_static_faces, set_static_faces from facefusion.face_store import get_static_faces, set_static_faces
from facefusion.types import BoundingBox, Face, FaceLandmark5, FaceLandmarkSet, FaceScoreSet, Score, VisionFrame from facefusion.typing import BoundingBox, Face, FaceLandmark5, FaceLandmarkSet, FaceScoreSet, Score, VisionFrame
def create_faces(vision_frame : VisionFrame, bounding_boxes : List[BoundingBox], face_scores : List[Score], face_landmarks_5 : List[FaceLandmark5]) -> List[Face]: def create_faces(vision_frame : VisionFrame, bounding_boxes : List[BoundingBox], face_scores : List[Score], face_landmarks_5 : List[FaceLandmark5]) -> List[Face]:
@@ -29,7 +29,7 @@ def create_faces(vision_frame : VisionFrame, bounding_boxes : List[BoundingBox],
face_angle = estimate_face_angle(face_landmark_68_5) face_angle = estimate_face_angle(face_landmark_68_5)
if state_manager.get_item('face_landmarker_score') > 0: if state_manager.get_item('face_landmarker_score') > 0:
face_landmark_68, face_landmark_score_68 = detect_face_landmark(vision_frame, bounding_box, face_angle) face_landmark_68, face_landmark_score_68 = detect_face_landmarks(vision_frame, bounding_box, face_angle)
if face_landmark_score_68 > state_manager.get_item('face_landmarker_score'): if face_landmark_score_68 > state_manager.get_item('face_landmarker_score'):
face_landmark_5_68 = convert_to_face_landmark_5(face_landmark_68) face_landmark_5_68 = convert_to_face_landmark_5(face_landmark_68)
@@ -45,15 +45,15 @@ def create_faces(vision_frame : VisionFrame, bounding_boxes : List[BoundingBox],
'detector': face_score, 'detector': face_score,
'landmarker': face_landmark_score_68 'landmarker': face_landmark_score_68
} }
face_embedding, face_embedding_norm = calculate_face_embedding(vision_frame, face_landmark_set.get('5/68')) embedding, normed_embedding = calc_embedding(vision_frame, face_landmark_set.get('5/68'))
gender, age, race = classify_face(vision_frame, face_landmark_set.get('5/68')) gender, age, race = classify_face(vision_frame, face_landmark_set.get('5/68'))
faces.append(Face( faces.append(Face(
bounding_box = bounding_box, bounding_box = bounding_box,
score_set = face_score_set, score_set = face_score_set,
landmark_set = face_landmark_set, landmark_set = face_landmark_set,
angle = face_angle, angle = face_angle,
embedding = face_embedding, embedding = embedding,
embedding_norm = face_embedding_norm, normed_embedding = normed_embedding,
gender = gender, gender = gender,
age = age, age = age,
race = race race = race
@@ -69,23 +69,23 @@ def get_one_face(faces : List[Face], position : int = 0) -> Optional[Face]:
def get_average_face(faces : List[Face]) -> Optional[Face]: def get_average_face(faces : List[Face]) -> Optional[Face]:
face_embeddings = [] embeddings = []
face_embeddings_norm = [] normed_embeddings = []
if faces: if faces:
first_face = get_first(faces) first_face = get_first(faces)
for face in faces: for face in faces:
face_embeddings.append(face.embedding) embeddings.append(face.embedding)
face_embeddings_norm.append(face.embedding_norm) normed_embeddings.append(face.normed_embedding)
return Face( return Face(
bounding_box = first_face.bounding_box, bounding_box = first_face.bounding_box,
score_set = first_face.score_set, score_set = first_face.score_set,
landmark_set = first_face.landmark_set, landmark_set = first_face.landmark_set,
angle = first_face.angle, angle = first_face.angle,
embedding = numpy.mean(face_embeddings, axis = 0), embedding = numpy.mean(embeddings, axis = 0),
embedding_norm = numpy.mean(face_embeddings_norm, axis = 0), normed_embedding = numpy.mean(normed_embeddings, axis = 0),
gender = first_face.gender, gender = first_face.gender,
age = first_face.age, age = first_face.age,
race = first_face.race race = first_face.race
@@ -110,7 +110,7 @@ def get_many_faces(vision_frames : List[VisionFrame]) -> List[Face]:
if face_detector_angle == 0: if face_detector_angle == 0:
bounding_boxes, face_scores, face_landmarks_5 = detect_faces(vision_frame) bounding_boxes, face_scores, face_landmarks_5 = detect_faces(vision_frame)
else: else:
bounding_boxes, face_scores, face_landmarks_5 = detect_faces_by_angle(vision_frame, face_detector_angle) bounding_boxes, face_scores, face_landmarks_5 = detect_rotated_faces(vision_frame, face_detector_angle)
all_bounding_boxes.extend(bounding_boxes) all_bounding_boxes.extend(bounding_boxes)
all_face_scores.extend(face_scores) all_face_scores.extend(face_scores)
all_face_landmarks_5.extend(face_landmarks_5) all_face_landmarks_5.extend(face_landmarks_5)
@@ -122,22 +122,3 @@ def get_many_faces(vision_frames : List[VisionFrame]) -> List[Face]:
many_faces.extend(faces) many_faces.extend(faces)
set_static_faces(vision_frame, faces) set_static_faces(vision_frame, faces)
return many_faces return many_faces
def scale_face(target_face : Face, target_vision_frame : VisionFrame, temp_vision_frame : VisionFrame) -> Face:
scale_x = temp_vision_frame.shape[1] / target_vision_frame.shape[1]
scale_y = temp_vision_frame.shape[0] / target_vision_frame.shape[0]
bounding_box = target_face.bounding_box * [ scale_x, scale_y, scale_x, scale_y ]
landmark_set =\
{
'5': target_face.landmark_set.get('5') * numpy.array([ scale_x, scale_y ]),
'5/68': target_face.landmark_set.get('5/68') * numpy.array([ scale_x, scale_y ]),
'68': target_face.landmark_set.get('68') * numpy.array([ scale_x, scale_y ]),
'68/5': target_face.landmark_set.get('68/5') * numpy.array([ scale_x, scale_y ])
}
return target_face._replace(
bounding_box = bounding_box,
landmark_set = landmark_set
)

View File

@@ -1,33 +1,23 @@
from functools import lru_cache
from typing import List, Tuple from typing import List, Tuple
import numpy import numpy
from facefusion import inference_manager from facefusion import inference_manager
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url from facefusion.download import conditional_download_hashes, conditional_download_sources
from facefusion.face_helper import warp_face_by_face_landmark_5 from facefusion.face_helper import warp_face_by_face_landmark_5
from facefusion.filesystem import resolve_relative_path from facefusion.filesystem import resolve_relative_path
from facefusion.thread_helper import conditional_thread_semaphore from facefusion.thread_helper import conditional_thread_semaphore
from facefusion.types import Age, DownloadScope, FaceLandmark5, Gender, InferencePool, ModelOptions, ModelSet, Race, VisionFrame from facefusion.typing import Age, FaceLandmark5, Gender, InferencePool, ModelOptions, ModelSet, Race, VisionFrame
MODEL_SET : ModelSet =\
@lru_cache() {
def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
return\
{
'fairface': 'fairface':
{ {
'__metadata__':
{
'vendor': 'dchen236',
'license': 'Non-Commercial',
'year': 2021
},
'hashes': 'hashes':
{ {
'face_classifier': 'face_classifier':
{ {
'url': resolve_download_url('models-3.0.0', 'fairface.hash'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/fairface.hash',
'path': resolve_relative_path('../.assets/models/fairface.hash') 'path': resolve_relative_path('../.assets/models/fairface.hash')
} }
}, },
@@ -35,7 +25,7 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
{ {
'face_classifier': 'face_classifier':
{ {
'url': resolve_download_url('models-3.0.0', 'fairface.onnx'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/fairface.onnx',
'path': resolve_relative_path('../.assets/models/fairface.onnx') 'path': resolve_relative_path('../.assets/models/fairface.onnx')
} }
}, },
@@ -44,30 +34,28 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
'mean': [ 0.485, 0.456, 0.406 ], 'mean': [ 0.485, 0.456, 0.406 ],
'standard_deviation': [ 0.229, 0.224, 0.225 ] 'standard_deviation': [ 0.229, 0.224, 0.225 ]
} }
} }
def get_inference_pool() -> InferencePool: def get_inference_pool() -> InferencePool:
model_names = [ 'fairface' ] model_sources = get_model_options().get('sources')
model_source_set = get_model_options().get('sources') return inference_manager.get_inference_pool(__name__, model_sources)
return inference_manager.get_inference_pool(__name__, model_names, model_source_set)
def clear_inference_pool() -> None: def clear_inference_pool() -> None:
model_names = [ 'fairface' ] inference_manager.clear_inference_pool(__name__)
inference_manager.clear_inference_pool(__name__, model_names)
def get_model_options() -> ModelOptions: def get_model_options() -> ModelOptions:
return create_static_model_set('full').get('fairface') return MODEL_SET.get('fairface')
def pre_check() -> bool: def pre_check() -> bool:
model_hash_set = get_model_options().get('hashes') download_directory_path = resolve_relative_path('../.assets/models')
model_source_set = get_model_options().get('sources') model_hashes = get_model_options().get('hashes')
model_sources = get_model_options().get('sources')
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set) return conditional_download_hashes(download_directory_path, model_hashes) and conditional_download_sources(download_directory_path, model_sources)
def classify_face(temp_vision_frame : VisionFrame, face_landmark_5 : FaceLandmark5) -> Tuple[Gender, Age, Race]: def classify_face(temp_vision_frame : VisionFrame, face_landmark_5 : FaceLandmark5) -> Tuple[Gender, Age, Race]:
@@ -76,7 +64,7 @@ def classify_face(temp_vision_frame : VisionFrame, face_landmark_5 : FaceLandmar
model_mean = get_model_options().get('mean') model_mean = get_model_options().get('mean')
model_standard_deviation = get_model_options().get('standard_deviation') model_standard_deviation = get_model_options().get('standard_deviation')
crop_vision_frame, _ = warp_face_by_face_landmark_5(temp_vision_frame, face_landmark_5, model_template, model_size) crop_vision_frame, _ = warp_face_by_face_landmark_5(temp_vision_frame, face_landmark_5, model_template, model_size)
crop_vision_frame = crop_vision_frame.astype(numpy.float32)[:, :, ::-1] / 255.0 crop_vision_frame = crop_vision_frame.astype(numpy.float32)[:, :, ::-1] / 255
crop_vision_frame -= model_mean crop_vision_frame -= model_mean
crop_vision_frame /= model_standard_deviation crop_vision_frame /= model_standard_deviation
crop_vision_frame = crop_vision_frame.transpose(2, 0, 1) crop_vision_frame = crop_vision_frame.transpose(2, 0, 1)

View File

@@ -1,35 +1,25 @@
from functools import lru_cache from typing import List, Tuple
from typing import List, Sequence, Tuple
import cv2 import cv2
import numpy import numpy
from facefusion import inference_manager, state_manager from facefusion import inference_manager, state_manager
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url from facefusion.download import conditional_download_hashes, conditional_download_sources
from facefusion.face_helper import create_rotation_matrix_and_size, create_static_anchors, distance_to_bounding_box, distance_to_face_landmark_5, normalize_bounding_box, transform_bounding_box, transform_points from facefusion.face_helper import create_rotated_matrix_and_size, create_static_anchors, distance_to_bounding_box, distance_to_face_landmark_5, normalize_bounding_box, transform_bounding_box, transform_points
from facefusion.filesystem import resolve_relative_path from facefusion.filesystem import resolve_relative_path
from facefusion.thread_helper import thread_semaphore from facefusion.thread_helper import thread_semaphore
from facefusion.types import Angle, BoundingBox, Detection, DownloadScope, DownloadSet, FaceLandmark5, InferencePool, Margin, ModelSet, Score, VisionFrame from facefusion.typing import Angle, BoundingBox, Detection, DownloadSet, FaceLandmark5, InferencePool, ModelSet, Score, VisionFrame
from facefusion.vision import restrict_frame, unpack_resolution from facefusion.vision import resize_frame_resolution, unpack_resolution
MODEL_SET : ModelSet =\
@lru_cache() {
def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
return\
{
'retinaface': 'retinaface':
{ {
'__metadata__':
{
'vendor': 'InsightFace',
'license': 'Non-Commercial',
'year': 2020
},
'hashes': 'hashes':
{ {
'retinaface': 'retinaface':
{ {
'url': resolve_download_url('models-3.0.0', 'retinaface_10g.hash'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/retinaface_10g.hash',
'path': resolve_relative_path('../.assets/models/retinaface_10g.hash') 'path': resolve_relative_path('../.assets/models/retinaface_10g.hash')
} }
}, },
@@ -37,24 +27,18 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
{ {
'retinaface': 'retinaface':
{ {
'url': resolve_download_url('models-3.0.0', 'retinaface_10g.onnx'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/retinaface_10g.onnx',
'path': resolve_relative_path('../.assets/models/retinaface_10g.onnx') 'path': resolve_relative_path('../.assets/models/retinaface_10g.onnx')
} }
} }
}, },
'scrfd': 'scrfd':
{ {
'__metadata__':
{
'vendor': 'InsightFace',
'license': 'Non-Commercial',
'year': 2021
},
'hashes': 'hashes':
{ {
'scrfd': 'scrfd':
{ {
'url': resolve_download_url('models-3.0.0', 'scrfd_2.5g.hash'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/scrfd_2.5g.hash',
'path': resolve_relative_path('../.assets/models/scrfd_2.5g.hash') 'path': resolve_relative_path('../.assets/models/scrfd_2.5g.hash')
} }
}, },
@@ -62,146 +46,101 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
{ {
'scrfd': 'scrfd':
{ {
'url': resolve_download_url('models-3.0.0', 'scrfd_2.5g.onnx'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/scrfd_2.5g.onnx',
'path': resolve_relative_path('../.assets/models/scrfd_2.5g.onnx') 'path': resolve_relative_path('../.assets/models/scrfd_2.5g.onnx')
} }
} }
}, },
'yolo_face': 'yoloface':
{ {
'__metadata__':
{
'vendor': 'derronqi',
'license': 'GPL-3.0',
'year': 2022
},
'hashes': 'hashes':
{ {
'yolo_face': 'yoloface':
{ {
'url': resolve_download_url('models-3.0.0', 'yoloface_8n.hash'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/yoloface_8n.hash',
'path': resolve_relative_path('../.assets/models/yoloface_8n.hash') 'path': resolve_relative_path('../.assets/models/yoloface_8n.hash')
} }
}, },
'sources': 'sources':
{ {
'yolo_face': 'yoloface':
{ {
'url': resolve_download_url('models-3.0.0', 'yoloface_8n.onnx'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/yoloface_8n.onnx',
'path': resolve_relative_path('../.assets/models/yoloface_8n.onnx') 'path': resolve_relative_path('../.assets/models/yoloface_8n.onnx')
} }
} }
},
'yunet':
{
'__metadata__':
{
'vendor': 'OpenCV',
'license': 'MIT',
'year': 2023
},
'hashes':
{
'yunet':
{
'url': resolve_download_url('models-3.4.0', 'yunet_2023_mar.hash'),
'path': resolve_relative_path('../.assets/models/yunet_2023_mar.hash')
}
},
'sources':
{
'yunet':
{
'url': resolve_download_url('models-3.4.0', 'yunet_2023_mar.onnx'),
'path': resolve_relative_path('../.assets/models/yunet_2023_mar.onnx')
}
}
}
} }
}
def get_inference_pool() -> InferencePool: def get_inference_pool() -> InferencePool:
model_names = [ state_manager.get_item('face_detector_model') ] _, model_sources = collect_model_downloads()
_, model_source_set = collect_model_downloads() model_context = __name__ + '.' + state_manager.get_item('face_detector_model')
return inference_manager.get_inference_pool(model_context, model_sources)
return inference_manager.get_inference_pool(__name__, model_names, model_source_set)
def clear_inference_pool() -> None: def clear_inference_pool() -> None:
model_names = [ state_manager.get_item('face_detector_model') ] model_context = __name__ + '.' + state_manager.get_item('face_detector_model')
inference_manager.clear_inference_pool(__name__, model_names) inference_manager.clear_inference_pool(model_context)
def collect_model_downloads() -> Tuple[DownloadSet, DownloadSet]: def collect_model_downloads() -> Tuple[DownloadSet, DownloadSet]:
model_set = create_static_model_set('full') model_hashes = {}
model_hash_set = {} model_sources = {}
model_source_set = {}
for face_detector_model in [ 'retinaface', 'scrfd', 'yolo_face', 'yunet' ]: if state_manager.get_item('face_detector_model') in [ 'many', 'retinaface' ]:
if state_manager.get_item('face_detector_model') in [ 'many', face_detector_model ]: model_hashes['retinaface'] = MODEL_SET.get('retinaface').get('hashes').get('retinaface')
model_hash_set[face_detector_model] = model_set.get(face_detector_model).get('hashes').get(face_detector_model) model_sources['retinaface'] = MODEL_SET.get('retinaface').get('sources').get('retinaface')
model_source_set[face_detector_model] = model_set.get(face_detector_model).get('sources').get(face_detector_model) if state_manager.get_item('face_detector_model') in [ 'many', 'scrfd' ]:
model_hashes['scrfd'] = MODEL_SET.get('scrfd').get('hashes').get('scrfd')
return model_hash_set, model_source_set model_sources['scrfd'] = MODEL_SET.get('scrfd').get('sources').get('scrfd')
if state_manager.get_item('face_detector_model') in [ 'many', 'yoloface' ]:
model_hashes['yoloface'] = MODEL_SET.get('yoloface').get('hashes').get('yoloface')
model_sources['yoloface'] = MODEL_SET.get('yoloface').get('sources').get('yoloface')
return model_hashes, model_sources
def pre_check() -> bool: def pre_check() -> bool:
model_hash_set, model_source_set = collect_model_downloads() download_directory_path = resolve_relative_path('../.assets/models')
model_hashes, model_sources = collect_model_downloads()
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set) return conditional_download_hashes(download_directory_path, model_hashes) and conditional_download_sources(download_directory_path, model_sources)
def detect_faces(vision_frame : VisionFrame) -> Tuple[List[BoundingBox], List[Score], List[FaceLandmark5]]: def detect_faces(vision_frame : VisionFrame) -> Tuple[List[BoundingBox], List[Score], List[FaceLandmark5]]:
margin_top, margin_right, margin_bottom, margin_left = prepare_margin(vision_frame)
margin_vision_frame = numpy.pad(vision_frame, ((margin_top, margin_bottom), (margin_left, margin_right), (0, 0)))
all_bounding_boxes : List[BoundingBox] = [] all_bounding_boxes : List[BoundingBox] = []
all_face_scores : List[Score] = [] all_face_scores : List[Score] = []
all_face_landmarks_5 : List[FaceLandmark5] = [] all_face_landmarks_5 : List[FaceLandmark5] = []
if state_manager.get_item('face_detector_model') in [ 'many', 'retinaface' ]: if state_manager.get_item('face_detector_model') in [ 'many', 'retinaface' ]:
bounding_boxes, face_scores, face_landmarks_5 = detect_with_retinaface(margin_vision_frame, state_manager.get_item('face_detector_size')) bounding_boxes, face_scores, face_landmarks_5 = detect_with_retinaface(vision_frame, state_manager.get_item('face_detector_size'))
all_bounding_boxes.extend(bounding_boxes) all_bounding_boxes.extend(bounding_boxes)
all_face_scores.extend(face_scores) all_face_scores.extend(face_scores)
all_face_landmarks_5.extend(face_landmarks_5) all_face_landmarks_5.extend(face_landmarks_5)
if state_manager.get_item('face_detector_model') in [ 'many', 'scrfd' ]: if state_manager.get_item('face_detector_model') in [ 'many', 'scrfd' ]:
bounding_boxes, face_scores, face_landmarks_5 = detect_with_scrfd(margin_vision_frame, state_manager.get_item('face_detector_size')) bounding_boxes, face_scores, face_landmarks_5 = detect_with_scrfd(vision_frame, state_manager.get_item('face_detector_size'))
all_bounding_boxes.extend(bounding_boxes) all_bounding_boxes.extend(bounding_boxes)
all_face_scores.extend(face_scores) all_face_scores.extend(face_scores)
all_face_landmarks_5.extend(face_landmarks_5) all_face_landmarks_5.extend(face_landmarks_5)
if state_manager.get_item('face_detector_model') in [ 'many', 'yolo_face' ]: if state_manager.get_item('face_detector_model') in [ 'many', 'yoloface' ]:
bounding_boxes, face_scores, face_landmarks_5 = detect_with_yolo_face(margin_vision_frame, state_manager.get_item('face_detector_size')) bounding_boxes, face_scores, face_landmarks_5 = detect_with_yoloface(vision_frame, state_manager.get_item('face_detector_size'))
all_bounding_boxes.extend(bounding_boxes) all_bounding_boxes.extend(bounding_boxes)
all_face_scores.extend(face_scores) all_face_scores.extend(face_scores)
all_face_landmarks_5.extend(face_landmarks_5) all_face_landmarks_5.extend(face_landmarks_5)
if state_manager.get_item('face_detector_model') == 'yunet': all_bounding_boxes = [ normalize_bounding_box(all_bounding_box) for all_bounding_box in all_bounding_boxes ]
bounding_boxes, face_scores, face_landmarks_5 = detect_with_yunet(margin_vision_frame, state_manager.get_item('face_detector_size'))
all_bounding_boxes.extend(bounding_boxes)
all_face_scores.extend(face_scores)
all_face_landmarks_5.extend(face_landmarks_5)
all_bounding_boxes = [ normalize_bounding_box(all_bounding_box) - numpy.array([ margin_left, margin_top, margin_left, margin_top ]) for all_bounding_box in all_bounding_boxes ]
all_face_landmarks_5 = [ all_face_landmark_5 - numpy.array([ margin_left, margin_top ]) for all_face_landmark_5 in all_face_landmarks_5 ]
return all_bounding_boxes, all_face_scores, all_face_landmarks_5 return all_bounding_boxes, all_face_scores, all_face_landmarks_5
def prepare_margin(vision_frame : VisionFrame) -> Margin: def detect_rotated_faces(vision_frame : VisionFrame, angle : Angle) -> Tuple[List[BoundingBox], List[Score], List[FaceLandmark5]]:
margin_top = int(vision_frame.shape[0] * numpy.interp(state_manager.get_item('face_detector_margin')[0], [ 0, 100 ], [ 0, 0.5 ])) rotated_matrix, rotated_size = create_rotated_matrix_and_size(angle, vision_frame.shape[:2][::-1])
margin_right = int(vision_frame.shape[1] * numpy.interp(state_manager.get_item('face_detector_margin')[1], [ 0, 100 ], [ 0, 0.5 ])) rotated_vision_frame = cv2.warpAffine(vision_frame, rotated_matrix, rotated_size)
margin_bottom = int(vision_frame.shape[0] * numpy.interp(state_manager.get_item('face_detector_margin')[2], [ 0, 100 ], [ 0, 0.5 ])) rotated_inverse_matrix = cv2.invertAffineTransform(rotated_matrix)
margin_left = int(vision_frame.shape[1] * numpy.interp(state_manager.get_item('face_detector_margin')[3], [ 0, 100 ], [ 0, 0.5 ])) bounding_boxes, face_scores, face_landmarks_5 = detect_faces(rotated_vision_frame)
return margin_top, margin_right, margin_bottom, margin_left bounding_boxes = [ transform_bounding_box(bounding_box, rotated_inverse_matrix) for bounding_box in bounding_boxes ]
face_landmarks_5 = [ transform_points(face_landmark_5, rotated_inverse_matrix) for face_landmark_5 in face_landmarks_5 ]
def detect_faces_by_angle(vision_frame : VisionFrame, face_angle : Angle) -> Tuple[List[BoundingBox], List[Score], List[FaceLandmark5]]:
rotation_matrix, rotation_size = create_rotation_matrix_and_size(face_angle, vision_frame.shape[:2][::-1])
rotation_vision_frame = cv2.warpAffine(vision_frame, rotation_matrix, rotation_size)
rotation_inverse_matrix = cv2.invertAffineTransform(rotation_matrix)
bounding_boxes, face_scores, face_landmarks_5 = detect_faces(rotation_vision_frame)
bounding_boxes = [ transform_bounding_box(bounding_box, rotation_inverse_matrix) for bounding_box in bounding_boxes ]
face_landmarks_5 = [ transform_points(face_landmark_5, rotation_inverse_matrix) for face_landmark_5 in face_landmarks_5 ]
return bounding_boxes, face_scores, face_landmarks_5 return bounding_boxes, face_scores, face_landmarks_5
@@ -212,40 +151,37 @@ def detect_with_retinaface(vision_frame : VisionFrame, face_detector_size : str)
feature_strides = [ 8, 16, 32 ] feature_strides = [ 8, 16, 32 ]
feature_map_channel = 3 feature_map_channel = 3
anchor_total = 2 anchor_total = 2
face_detector_score = state_manager.get_item('face_detector_score')
face_detector_width, face_detector_height = unpack_resolution(face_detector_size) face_detector_width, face_detector_height = unpack_resolution(face_detector_size)
temp_vision_frame = restrict_frame(vision_frame, (face_detector_width, face_detector_height)) temp_vision_frame = resize_frame_resolution(vision_frame, (face_detector_width, face_detector_height))
ratio_height = vision_frame.shape[0] / temp_vision_frame.shape[0] ratio_height = vision_frame.shape[0] / temp_vision_frame.shape[0]
ratio_width = vision_frame.shape[1] / temp_vision_frame.shape[1] ratio_width = vision_frame.shape[1] / temp_vision_frame.shape[1]
detect_vision_frame = prepare_detect_frame(temp_vision_frame, face_detector_size) detect_vision_frame = prepare_detect_frame(temp_vision_frame, face_detector_size)
detect_vision_frame = normalize_detect_frame(detect_vision_frame, [ -1, 1 ])
detection = forward_with_retinaface(detect_vision_frame) detection = forward_with_retinaface(detect_vision_frame)
for index, feature_stride in enumerate(feature_strides): for index, feature_stride in enumerate(feature_strides):
face_scores_raw = detection[index] keep_indices = numpy.where(detection[index] >= state_manager.get_item('face_detector_score'))[0]
keep_indices = numpy.where(face_scores_raw >= face_detector_score)[0]
if numpy.any(keep_indices): if numpy.any(keep_indices):
stride_height = face_detector_height // feature_stride stride_height = face_detector_height // feature_stride
stride_width = face_detector_width // feature_stride stride_width = face_detector_width // feature_stride
anchors = create_static_anchors(feature_stride, anchor_total, stride_height, stride_width) anchors = create_static_anchors(feature_stride, anchor_total, stride_height, stride_width)
bounding_boxes_raw = detection[index + feature_map_channel] * feature_stride bounding_box_raw = detection[index + feature_map_channel] * feature_stride
face_landmarks_5_raw = detection[index + feature_map_channel * 2] * feature_stride face_landmark_5_raw = detection[index + feature_map_channel * 2] * feature_stride
for bounding_box_raw in distance_to_bounding_box(anchors, bounding_boxes_raw)[keep_indices]: for bounding_box in distance_to_bounding_box(anchors, bounding_box_raw)[keep_indices]:
bounding_boxes.append(numpy.array( bounding_boxes.append(numpy.array(
[ [
bounding_box_raw[0] * ratio_width, bounding_box[0] * ratio_width,
bounding_box_raw[1] * ratio_height, bounding_box[1] * ratio_height,
bounding_box_raw[2] * ratio_width, bounding_box[2] * ratio_width,
bounding_box_raw[3] * ratio_height bounding_box[3] * ratio_height,
])) ]))
for face_score_raw in face_scores_raw[keep_indices]: for score in detection[index][keep_indices]:
face_scores.append(face_score_raw[0]) face_scores.append(score[0])
for face_landmark_raw_5 in distance_to_face_landmark_5(anchors, face_landmarks_5_raw)[keep_indices]: for face_landmark_5 in distance_to_face_landmark_5(anchors, face_landmark_5_raw)[keep_indices]:
face_landmarks_5.append(face_landmark_raw_5 * [ ratio_width, ratio_height ]) face_landmarks_5.append(face_landmark_5 * [ ratio_width, ratio_height ])
return bounding_boxes, face_scores, face_landmarks_5 return bounding_boxes, face_scores, face_landmarks_5
@@ -257,139 +193,73 @@ def detect_with_scrfd(vision_frame : VisionFrame, face_detector_size : str) -> T
feature_strides = [ 8, 16, 32 ] feature_strides = [ 8, 16, 32 ]
feature_map_channel = 3 feature_map_channel = 3
anchor_total = 2 anchor_total = 2
face_detector_score = state_manager.get_item('face_detector_score')
face_detector_width, face_detector_height = unpack_resolution(face_detector_size) face_detector_width, face_detector_height = unpack_resolution(face_detector_size)
temp_vision_frame = restrict_frame(vision_frame, (face_detector_width, face_detector_height)) temp_vision_frame = resize_frame_resolution(vision_frame, (face_detector_width, face_detector_height))
ratio_height = vision_frame.shape[0] / temp_vision_frame.shape[0] ratio_height = vision_frame.shape[0] / temp_vision_frame.shape[0]
ratio_width = vision_frame.shape[1] / temp_vision_frame.shape[1] ratio_width = vision_frame.shape[1] / temp_vision_frame.shape[1]
detect_vision_frame = prepare_detect_frame(temp_vision_frame, face_detector_size) detect_vision_frame = prepare_detect_frame(temp_vision_frame, face_detector_size)
detect_vision_frame = normalize_detect_frame(detect_vision_frame, [ -1, 1 ])
detection = forward_with_scrfd(detect_vision_frame) detection = forward_with_scrfd(detect_vision_frame)
for index, feature_stride in enumerate(feature_strides): for index, feature_stride in enumerate(feature_strides):
face_scores_raw = detection[index] keep_indices = numpy.where(detection[index] >= state_manager.get_item('face_detector_score'))[0]
keep_indices = numpy.where(face_scores_raw >= face_detector_score)[0]
if numpy.any(keep_indices): if numpy.any(keep_indices):
stride_height = face_detector_height // feature_stride stride_height = face_detector_height // feature_stride
stride_width = face_detector_width // feature_stride stride_width = face_detector_width // feature_stride
anchors = create_static_anchors(feature_stride, anchor_total, stride_height, stride_width) anchors = create_static_anchors(feature_stride, anchor_total, stride_height, stride_width)
bounding_boxes_raw = detection[index + feature_map_channel] * feature_stride bounding_box_raw = detection[index + feature_map_channel] * feature_stride
face_landmarks_5_raw = detection[index + feature_map_channel * 2] * feature_stride face_landmark_5_raw = detection[index + feature_map_channel * 2] * feature_stride
for bounding_box_raw in distance_to_bounding_box(anchors, bounding_boxes_raw)[keep_indices]: for bounding_box in distance_to_bounding_box(anchors, bounding_box_raw)[keep_indices]:
bounding_boxes.append(numpy.array( bounding_boxes.append(numpy.array(
[ [
bounding_box_raw[0] * ratio_width, bounding_box[0] * ratio_width,
bounding_box_raw[1] * ratio_height, bounding_box[1] * ratio_height,
bounding_box_raw[2] * ratio_width, bounding_box[2] * ratio_width,
bounding_box_raw[3] * ratio_height bounding_box[3] * ratio_height,
])) ]))
for face_score_raw in face_scores_raw[keep_indices]: for score in detection[index][keep_indices]:
face_scores.append(face_score_raw[0]) face_scores.append(score[0])
for face_landmark_raw_5 in distance_to_face_landmark_5(anchors, face_landmarks_5_raw)[keep_indices]: for face_landmark_5 in distance_to_face_landmark_5(anchors, face_landmark_5_raw)[keep_indices]:
face_landmarks_5.append(face_landmark_raw_5 * [ ratio_width, ratio_height ]) face_landmarks_5.append(face_landmark_5 * [ ratio_width, ratio_height ])
return bounding_boxes, face_scores, face_landmarks_5 return bounding_boxes, face_scores, face_landmarks_5
def detect_with_yolo_face(vision_frame : VisionFrame, face_detector_size : str) -> Tuple[List[BoundingBox], List[Score], List[FaceLandmark5]]: def detect_with_yoloface(vision_frame : VisionFrame, face_detector_size : str) -> Tuple[List[BoundingBox], List[Score], List[FaceLandmark5]]:
bounding_boxes = [] bounding_boxes = []
face_scores = [] face_scores = []
face_landmarks_5 = [] face_landmarks_5 = []
face_detector_score = state_manager.get_item('face_detector_score')
face_detector_width, face_detector_height = unpack_resolution(face_detector_size) face_detector_width, face_detector_height = unpack_resolution(face_detector_size)
temp_vision_frame = restrict_frame(vision_frame, (face_detector_width, face_detector_height)) temp_vision_frame = resize_frame_resolution(vision_frame, (face_detector_width, face_detector_height))
ratio_height = vision_frame.shape[0] / temp_vision_frame.shape[0] ratio_height = vision_frame.shape[0] / temp_vision_frame.shape[0]
ratio_width = vision_frame.shape[1] / temp_vision_frame.shape[1] ratio_width = vision_frame.shape[1] / temp_vision_frame.shape[1]
detect_vision_frame = prepare_detect_frame(temp_vision_frame, face_detector_size) detect_vision_frame = prepare_detect_frame(temp_vision_frame, face_detector_size)
detect_vision_frame = normalize_detect_frame(detect_vision_frame, [ 0, 1 ]) detection = forward_with_yoloface(detect_vision_frame)
detection = forward_with_yolo_face(detect_vision_frame)
detection = numpy.squeeze(detection).T detection = numpy.squeeze(detection).T
bounding_boxes_raw, face_scores_raw, face_landmarks_5_raw = numpy.split(detection, [ 4, 5 ], axis = 1) bounding_box_raw, score_raw, face_landmark_5_raw = numpy.split(detection, [ 4, 5 ], axis = 1)
keep_indices = numpy.where(face_scores_raw > face_detector_score)[0] keep_indices = numpy.where(score_raw > state_manager.get_item('face_detector_score'))[0]
if numpy.any(keep_indices): if numpy.any(keep_indices):
bounding_boxes_raw, face_scores_raw, face_landmarks_5_raw = bounding_boxes_raw[keep_indices], face_scores_raw[keep_indices], face_landmarks_5_raw[keep_indices] bounding_box_raw, face_landmark_5_raw, score_raw = bounding_box_raw[keep_indices], face_landmark_5_raw[keep_indices], score_raw[keep_indices]
for bounding_box_raw in bounding_boxes_raw: for bounding_box in bounding_box_raw:
bounding_boxes.append(numpy.array( bounding_boxes.append(numpy.array(
[ [
(bounding_box_raw[0] - bounding_box_raw[2] / 2) * ratio_width, (bounding_box[0] - bounding_box[2] / 2) * ratio_width,
(bounding_box_raw[1] - bounding_box_raw[3] / 2) * ratio_height, (bounding_box[1] - bounding_box[3] / 2) * ratio_height,
(bounding_box_raw[0] + bounding_box_raw[2] / 2) * ratio_width, (bounding_box[0] + bounding_box[2] / 2) * ratio_width,
(bounding_box_raw[1] + bounding_box_raw[3] / 2) * ratio_height (bounding_box[1] + bounding_box[3] / 2) * ratio_height,
])) ]))
face_scores = face_scores_raw.ravel().tolist() face_scores = score_raw.ravel().tolist()
face_landmarks_5_raw[:, 0::3] = (face_landmarks_5_raw[:, 0::3]) * ratio_width face_landmark_5_raw[:, 0::3] = (face_landmark_5_raw[:, 0::3]) * ratio_width
face_landmarks_5_raw[:, 1::3] = (face_landmarks_5_raw[:, 1::3]) * ratio_height face_landmark_5_raw[:, 1::3] = (face_landmark_5_raw[:, 1::3]) * ratio_height
for face_landmark_raw_5 in face_landmarks_5_raw: for face_landmark_5 in face_landmark_5_raw:
face_landmarks_5.append(numpy.array(face_landmark_raw_5.reshape(-1, 3)[:, :2])) face_landmarks_5.append(numpy.array(face_landmark_5.reshape(-1, 3)[:, :2]))
return bounding_boxes, face_scores, face_landmarks_5
def detect_with_yunet(vision_frame : VisionFrame, face_detector_size : str) -> Tuple[List[BoundingBox], List[Score], List[FaceLandmark5]]:
bounding_boxes = []
face_scores = []
face_landmarks_5 = []
feature_strides = [ 8, 16, 32 ]
feature_map_channel = 3
anchor_total = 1
face_detector_score = state_manager.get_item('face_detector_score')
face_detector_width, face_detector_height = unpack_resolution(face_detector_size)
temp_vision_frame = restrict_frame(vision_frame, (face_detector_width, face_detector_height))
ratio_height = vision_frame.shape[0] / temp_vision_frame.shape[0]
ratio_width = vision_frame.shape[1] / temp_vision_frame.shape[1]
detect_vision_frame = prepare_detect_frame(temp_vision_frame, face_detector_size)
detect_vision_frame = normalize_detect_frame(detect_vision_frame, [ 0, 255 ])
detection = forward_with_yunet(detect_vision_frame)
for index, feature_stride in enumerate(feature_strides):
face_scores_raw = (detection[index] * detection[index + feature_map_channel]).reshape(-1)
keep_indices = numpy.where(face_scores_raw >= face_detector_score)[0]
if numpy.any(keep_indices):
stride_height = face_detector_height // feature_stride
stride_width = face_detector_width // feature_stride
anchors = create_static_anchors(feature_stride, anchor_total, stride_height, stride_width)
bounding_boxes_center = detection[index + feature_map_channel * 2].squeeze(0)[:, :2] * feature_stride + anchors
bounding_boxes_size = numpy.exp(detection[index + feature_map_channel * 2].squeeze(0)[:, 2:4]) * feature_stride
face_landmarks_5_raw = detection[index + feature_map_channel * 3].squeeze(0)
bounding_boxes_raw = numpy.stack(
[
bounding_boxes_center[:, 0] - bounding_boxes_size[:, 0] / 2,
bounding_boxes_center[:, 1] - bounding_boxes_size[:, 1] / 2,
bounding_boxes_center[:, 0] + bounding_boxes_size[:, 0] / 2,
bounding_boxes_center[:, 1] + bounding_boxes_size[:, 1] / 2
], axis = -1)
for bounding_box_raw in bounding_boxes_raw[keep_indices]:
bounding_boxes.append(numpy.array(
[
bounding_box_raw[0] * ratio_width,
bounding_box_raw[1] * ratio_height,
bounding_box_raw[2] * ratio_width,
bounding_box_raw[3] * ratio_height
]))
face_scores.extend(face_scores_raw[keep_indices])
face_landmarks_5_raw = numpy.concatenate(
[
face_landmarks_5_raw[:, [0, 1]] * feature_stride + anchors,
face_landmarks_5_raw[:, [2, 3]] * feature_stride + anchors,
face_landmarks_5_raw[:, [4, 5]] * feature_stride + anchors,
face_landmarks_5_raw[:, [6, 7]] * feature_stride + anchors,
face_landmarks_5_raw[:, [8, 9]] * feature_stride + anchors
], axis = -1).reshape(-1, 5, 2)
for face_landmark_raw_5 in face_landmarks_5_raw[keep_indices]:
face_landmarks_5.append(face_landmark_raw_5 * [ ratio_width, ratio_height ])
return bounding_boxes, face_scores, face_landmarks_5 return bounding_boxes, face_scores, face_landmarks_5
@@ -418,20 +288,8 @@ def forward_with_scrfd(detect_vision_frame : VisionFrame) -> Detection:
return detection return detection
def forward_with_yolo_face(detect_vision_frame : VisionFrame) -> Detection: def forward_with_yoloface(detect_vision_frame : VisionFrame) -> Detection:
face_detector = get_inference_pool().get('yolo_face') face_detector = get_inference_pool().get('yoloface')
with thread_semaphore():
detection = face_detector.run(None,
{
'input': detect_vision_frame
})
return detection
def forward_with_yunet(detect_vision_frame : VisionFrame) -> Detection:
face_detector = get_inference_pool().get('yunet')
with thread_semaphore(): with thread_semaphore():
detection = face_detector.run(None, detection = face_detector.run(None,
@@ -446,13 +304,6 @@ def prepare_detect_frame(temp_vision_frame : VisionFrame, face_detector_size : s
face_detector_width, face_detector_height = unpack_resolution(face_detector_size) face_detector_width, face_detector_height = unpack_resolution(face_detector_size)
detect_vision_frame = numpy.zeros((face_detector_height, face_detector_width, 3)) detect_vision_frame = numpy.zeros((face_detector_height, face_detector_width, 3))
detect_vision_frame[:temp_vision_frame.shape[0], :temp_vision_frame.shape[1], :] = temp_vision_frame detect_vision_frame[:temp_vision_frame.shape[0], :temp_vision_frame.shape[1], :] = temp_vision_frame
detect_vision_frame = (detect_vision_frame - 127.5) / 128.0
detect_vision_frame = numpy.expand_dims(detect_vision_frame.transpose(2, 0, 1), axis = 0).astype(numpy.float32) detect_vision_frame = numpy.expand_dims(detect_vision_frame.transpose(2, 0, 1), axis = 0).astype(numpy.float32)
return detect_vision_frame return detect_vision_frame
def normalize_detect_frame(detect_vision_frame : VisionFrame, normalize_range : Sequence[int]) -> VisionFrame:
if normalize_range == [ -1, 1 ]:
return (detect_vision_frame - 127.5) / 128.0
if normalize_range == [ 0, 1 ]:
return detect_vision_frame / 255.0
return detect_vision_frame

View File

@@ -5,9 +5,9 @@ import cv2
import numpy import numpy
from cv2.typing import Size from cv2.typing import Size
from facefusion.types import Anchors, Angle, BoundingBox, Distance, FaceDetectorModel, FaceLandmark5, FaceLandmark68, Mask, Matrix, Points, Scale, Score, Translation, VisionFrame, WarpTemplate, WarpTemplateSet from facefusion.typing import Anchors, Angle, BoundingBox, Distance, FaceDetectorModel, FaceLandmark5, FaceLandmark68, Mask, Matrix, Points, Scale, Score, Translation, VisionFrame, WarpTemplate, WarpTemplateSet
WARP_TEMPLATE_SET : WarpTemplateSet =\ WARP_TEMPLATES : WarpTemplateSet =\
{ {
'arcface_112_v1': numpy.array( 'arcface_112_v1': numpy.array(
[ [
@@ -25,7 +25,7 @@ WARP_TEMPLATE_SET : WarpTemplateSet =\
[ 0.37097589, 0.82469196 ], [ 0.37097589, 0.82469196 ],
[ 0.63151696, 0.82325089 ] [ 0.63151696, 0.82325089 ]
]), ]),
'arcface_128': numpy.array( 'arcface_128_v2': numpy.array(
[ [
[ 0.36167656, 0.40387734 ], [ 0.36167656, 0.40387734 ],
[ 0.63696719, 0.40235469 ], [ 0.63696719, 0.40235469 ],
@@ -33,14 +33,6 @@ WARP_TEMPLATE_SET : WarpTemplateSet =\
[ 0.38710391, 0.72160547 ], [ 0.38710391, 0.72160547 ],
[ 0.61507734, 0.72034453 ] [ 0.61507734, 0.72034453 ]
]), ]),
'dfl_whole_face': numpy.array(
[
[ 0.35342266, 0.39285716 ],
[ 0.62797622, 0.39285716 ],
[ 0.48660713, 0.54017860 ],
[ 0.38839287, 0.68750011 ],
[ 0.59821427, 0.68750011 ]
]),
'ffhq_512': numpy.array( 'ffhq_512': numpy.array(
[ [
[ 0.37691676, 0.46864664 ], [ 0.37691676, 0.46864664 ],
@@ -48,29 +40,13 @@ WARP_TEMPLATE_SET : WarpTemplateSet =\
[ 0.50123859, 0.61331904 ], [ 0.50123859, 0.61331904 ],
[ 0.39308822, 0.72541100 ], [ 0.39308822, 0.72541100 ],
[ 0.61150205, 0.72490465 ] [ 0.61150205, 0.72490465 ]
]),
'mtcnn_512': numpy.array(
[
[ 0.36562865, 0.46733799 ],
[ 0.63305391, 0.46585885 ],
[ 0.50019127, 0.61942959 ],
[ 0.39032951, 0.77598822 ],
[ 0.61178945, 0.77476328 ]
]),
'styleganex_384': numpy.array(
[
[ 0.42353745, 0.52289879 ],
[ 0.57725008, 0.52319972 ],
[ 0.50123859, 0.61331904 ],
[ 0.43364461, 0.68337652 ],
[ 0.57015325, 0.68306005 ]
]) ])
} }
def estimate_matrix_by_face_landmark_5(face_landmark_5 : FaceLandmark5, warp_template : WarpTemplate, crop_size : Size) -> Matrix: def estimate_matrix_by_face_landmark_5(face_landmark_5 : FaceLandmark5, warp_template : WarpTemplate, crop_size : Size) -> Matrix:
warp_template_norm = WARP_TEMPLATE_SET.get(warp_template) * crop_size normed_warp_template = WARP_TEMPLATES.get(warp_template) * crop_size
affine_matrix = cv2.estimateAffinePartial2D(face_landmark_5, warp_template_norm, method = cv2.RANSAC, ransacReprojThreshold = 100)[0] affine_matrix = cv2.estimateAffinePartial2D(face_landmark_5, normed_warp_template, method = cv2.RANSAC, ransacReprojThreshold = 100)[0]
return affine_matrix return affine_matrix
@@ -98,59 +74,39 @@ def warp_face_by_translation(temp_vision_frame : VisionFrame, translation : Tran
return crop_vision_frame, affine_matrix return crop_vision_frame, affine_matrix
def paste_back(temp_vision_frame : VisionFrame, crop_vision_frame : VisionFrame, crop_vision_mask : Mask, affine_matrix : Matrix) -> VisionFrame: def paste_back(temp_vision_frame : VisionFrame, crop_vision_frame : VisionFrame, crop_mask : Mask, affine_matrix : Matrix) -> VisionFrame:
paste_bounding_box, paste_matrix = calculate_paste_area(temp_vision_frame, crop_vision_frame, affine_matrix)
x1, y1, x2, y2 = paste_bounding_box
paste_width = x2 - x1
paste_height = y2 - y1
inverse_vision_mask = cv2.warpAffine(crop_vision_mask, paste_matrix, (paste_width, paste_height)).clip(0, 1)
inverse_vision_mask = numpy.expand_dims(inverse_vision_mask, axis = -1)
inverse_vision_frame = cv2.warpAffine(crop_vision_frame, paste_matrix, (paste_width, paste_height), borderMode = cv2.BORDER_REPLICATE)
temp_vision_frame = temp_vision_frame.copy()
paste_vision_frame = temp_vision_frame[y1:y2, x1:x2]
paste_vision_frame = paste_vision_frame * (1 - inverse_vision_mask) + inverse_vision_frame * inverse_vision_mask
temp_vision_frame[y1:y2, x1:x2] = paste_vision_frame.astype(temp_vision_frame.dtype)
return temp_vision_frame
def calculate_paste_area(temp_vision_frame : VisionFrame, crop_vision_frame : VisionFrame, affine_matrix : Matrix) -> Tuple[BoundingBox, Matrix]:
temp_height, temp_width = temp_vision_frame.shape[:2]
crop_height, crop_width = crop_vision_frame.shape[:2]
inverse_matrix = cv2.invertAffineTransform(affine_matrix) inverse_matrix = cv2.invertAffineTransform(affine_matrix)
crop_points = numpy.array([ [ 0, 0 ], [ crop_width, 0 ], [ crop_width, crop_height ], [ 0, crop_height ] ]) temp_size = temp_vision_frame.shape[:2][::-1]
paste_region_points = transform_points(crop_points, inverse_matrix) inverse_mask = cv2.warpAffine(crop_mask, inverse_matrix, temp_size).clip(0, 1)
paste_region_point_min = numpy.floor(paste_region_points.min(axis = 0)).astype(int) inverse_vision_frame = cv2.warpAffine(crop_vision_frame, inverse_matrix, temp_size, borderMode = cv2.BORDER_REPLICATE)
paste_region_point_max = numpy.ceil(paste_region_points.max(axis = 0)).astype(int) paste_vision_frame = temp_vision_frame.copy()
x1, y1 = numpy.clip(paste_region_point_min, 0, [ temp_width, temp_height ]) paste_vision_frame[:, :, 0] = inverse_mask * inverse_vision_frame[:, :, 0] + (1 - inverse_mask) * temp_vision_frame[:, :, 0]
x2, y2 = numpy.clip(paste_region_point_max, 0, [ temp_width, temp_height ]) paste_vision_frame[:, :, 1] = inverse_mask * inverse_vision_frame[:, :, 1] + (1 - inverse_mask) * temp_vision_frame[:, :, 1]
paste_bounding_box = numpy.array([ x1, y1, x2, y2 ]) paste_vision_frame[:, :, 2] = inverse_mask * inverse_vision_frame[:, :, 2] + (1 - inverse_mask) * temp_vision_frame[:, :, 2]
paste_matrix = inverse_matrix.copy() return paste_vision_frame
paste_matrix[0, 2] -= x1
paste_matrix[1, 2] -= y1
return paste_bounding_box, paste_matrix
@lru_cache() @lru_cache(maxsize = None)
def create_static_anchors(feature_stride : int, anchor_total : int, stride_height : int, stride_width : int) -> Anchors: def create_static_anchors(feature_stride : int, anchor_total : int, stride_height : int, stride_width : int) -> Anchors:
x, y = numpy.mgrid[:stride_width, :stride_height] y, x = numpy.mgrid[:stride_height, :stride_width][::-1]
anchors = numpy.stack((y, x), axis = -1) anchors = numpy.stack((y, x), axis = -1)
anchors = (anchors * feature_stride).reshape((-1, 2)) anchors = (anchors * feature_stride).reshape((-1, 2))
anchors = numpy.stack([ anchors ] * anchor_total, axis = 1).reshape((-1, 2)) anchors = numpy.stack([ anchors ] * anchor_total, axis = 1).reshape((-1, 2))
return anchors return anchors
def create_rotation_matrix_and_size(angle : Angle, size : Size) -> Tuple[Matrix, Size]: def create_rotated_matrix_and_size(angle : Angle, size : Size) -> Tuple[Matrix, Size]:
rotation_matrix = cv2.getRotationMatrix2D((size[0] / 2, size[1] / 2), angle, 1) rotated_matrix = cv2.getRotationMatrix2D((size[0] / 2, size[1] / 2), angle, 1)
rotation_size = numpy.dot(numpy.abs(rotation_matrix[:, :2]), size) rotated_size = numpy.dot(numpy.abs(rotated_matrix[:, :2]), size)
rotation_matrix[:, -1] += (rotation_size - size) * 0.5 #type:ignore[misc] rotated_matrix[:, -1] += (rotated_size - size) * 0.5 #type:ignore[misc]
rotation_size = int(rotation_size[0]), int(rotation_size[1]) rotated_size = int(rotated_size[0]), int(rotated_size[1])
return rotation_matrix, rotation_size return rotated_matrix, rotated_size
def create_bounding_box(face_landmark_68 : FaceLandmark68) -> BoundingBox: def create_bounding_box(face_landmark_68 : FaceLandmark68) -> BoundingBox:
x1, y1 = numpy.min(face_landmark_68, axis = 0) min_x, min_y = numpy.min(face_landmark_68, axis = 0)
x2, y2 = numpy.max(face_landmark_68, axis = 0) max_x, max_y = numpy.max(face_landmark_68, axis = 0)
bounding_box = normalize_bounding_box(numpy.array([ x1, y1, x2, y2 ])) bounding_box = normalize_bounding_box(numpy.array([ min_x, min_y, max_x, max_y ]))
return bounding_box return bounding_box
@@ -228,9 +184,9 @@ def estimate_face_angle(face_landmark_68 : FaceLandmark68) -> Angle:
return face_angle return face_angle
def apply_nms(bounding_boxes : List[BoundingBox], scores : List[Score], score_threshold : float, nms_threshold : float) -> Sequence[int]: def apply_nms(bounding_boxes : List[BoundingBox], face_scores : List[Score], score_threshold : float, nms_threshold : float) -> Sequence[int]:
bounding_boxes_norm = [ (x1, y1, x2 - x1, y2 - y1) for (x1, y1, x2, y2) in bounding_boxes ] normed_bounding_boxes = [ (x1, y1, x2 - x1, y2 - y1) for (x1, y1, x2, y2) in bounding_boxes ]
keep_indices = cv2.dnn.NMSBoxes(bounding_boxes_norm, scores, score_threshold = score_threshold, nms_threshold = nms_threshold) keep_indices = cv2.dnn.NMSBoxes(normed_bounding_boxes, face_scores, score_threshold = score_threshold, nms_threshold = nms_threshold)
return keep_indices return keep_indices
@@ -246,11 +202,9 @@ def get_nms_threshold(face_detector_model : FaceDetectorModel, face_detector_ang
return 0.4 return 0.4
def merge_matrix(temp_matrices : List[Matrix]) -> Matrix: def merge_matrix(matrices : List[Matrix]) -> Matrix:
matrix = numpy.vstack([temp_matrices[0], [0, 0, 1]]) merged_matrix = numpy.vstack([ matrices[0], [ 0, 0, 1 ] ])
for matrix in matrices[1:]:
for temp_matrix in temp_matrices[1:]: matrix = numpy.vstack([ matrix, [ 0, 0, 1 ] ])
temp_matrix = numpy.vstack([ temp_matrix, [ 0, 0, 1 ] ]) merged_matrix = numpy.dot(merged_matrix, matrix)
matrix = numpy.dot(temp_matrix, matrix) return merged_matrix[:2, :]
return matrix[:2, :]

View File

@@ -1,34 +1,24 @@
from functools import lru_cache
from typing import Tuple from typing import Tuple
import cv2 import cv2
import numpy import numpy
from facefusion import inference_manager, state_manager from facefusion import inference_manager, state_manager
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url from facefusion.download import conditional_download_hashes, conditional_download_sources
from facefusion.face_helper import create_rotation_matrix_and_size, estimate_matrix_by_face_landmark_5, transform_points, warp_face_by_translation from facefusion.face_helper import create_rotated_matrix_and_size, estimate_matrix_by_face_landmark_5, transform_points, warp_face_by_translation
from facefusion.filesystem import resolve_relative_path from facefusion.filesystem import resolve_relative_path
from facefusion.thread_helper import conditional_thread_semaphore from facefusion.thread_helper import conditional_thread_semaphore
from facefusion.types import Angle, BoundingBox, DownloadScope, DownloadSet, FaceLandmark5, FaceLandmark68, InferencePool, ModelSet, Prediction, Score, VisionFrame from facefusion.typing import Angle, BoundingBox, DownloadSet, FaceLandmark5, FaceLandmark68, InferencePool, ModelSet, Prediction, Score, VisionFrame
MODEL_SET : ModelSet =\
@lru_cache() {
def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
return\
{
'2dfan4': '2dfan4':
{ {
'__metadata__':
{
'vendor': 'breadbread1984',
'license': 'MIT',
'year': 2018
},
'hashes': 'hashes':
{ {
'2dfan4': '2dfan4':
{ {
'url': resolve_download_url('models-3.0.0', '2dfan4.hash'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/2dfan4.hash',
'path': resolve_relative_path('../.assets/models/2dfan4.hash') 'path': resolve_relative_path('../.assets/models/2dfan4.hash')
} }
}, },
@@ -36,7 +26,7 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
{ {
'2dfan4': '2dfan4':
{ {
'url': resolve_download_url('models-3.0.0', '2dfan4.onnx'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/2dfan4.onnx',
'path': resolve_relative_path('../.assets/models/2dfan4.onnx') 'path': resolve_relative_path('../.assets/models/2dfan4.onnx')
} }
}, },
@@ -44,17 +34,11 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
}, },
'peppa_wutz': 'peppa_wutz':
{ {
'__metadata__':
{
'vendor': 'Unknown',
'license': 'Apache-2.0',
'year': 2023
},
'hashes': 'hashes':
{ {
'peppa_wutz': 'peppa_wutz':
{ {
'url': resolve_download_url('models-3.0.0', 'peppa_wutz.hash'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/peppa_wutz.hash',
'path': resolve_relative_path('../.assets/models/peppa_wutz.hash') 'path': resolve_relative_path('../.assets/models/peppa_wutz.hash')
} }
}, },
@@ -62,7 +46,7 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
{ {
'peppa_wutz': 'peppa_wutz':
{ {
'url': resolve_download_url('models-3.0.0', 'peppa_wutz.onnx'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/peppa_wutz.onnx',
'path': resolve_relative_path('../.assets/models/peppa_wutz.onnx') 'path': resolve_relative_path('../.assets/models/peppa_wutz.onnx')
} }
}, },
@@ -70,17 +54,11 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
}, },
'fan_68_5': 'fan_68_5':
{ {
'__metadata__':
{
'vendor': 'FaceFusion',
'license': 'OpenRAIL-M',
'year': 2024
},
'hashes': 'hashes':
{ {
'fan_68_5': 'fan_68_5':
{ {
'url': resolve_download_url('models-3.0.0', 'fan_68_5.hash'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/fan_68_5.hash',
'path': resolve_relative_path('../.assets/models/fan_68_5.hash') 'path': resolve_relative_path('../.assets/models/fan_68_5.hash')
} }
}, },
@@ -88,52 +66,52 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
{ {
'fan_68_5': 'fan_68_5':
{ {
'url': resolve_download_url('models-3.0.0', 'fan_68_5.onnx'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/fan_68_5.onnx',
'path': resolve_relative_path('../.assets/models/fan_68_5.onnx') 'path': resolve_relative_path('../.assets/models/fan_68_5.onnx')
} }
} }
} }
} }
def get_inference_pool() -> InferencePool: def get_inference_pool() -> InferencePool:
model_names = [ state_manager.get_item('face_landmarker_model'), 'fan_68_5' ] _, model_sources = collect_model_downloads()
_, model_source_set = collect_model_downloads() model_context = __name__ + '.' + state_manager.get_item('face_landmarker_model')
return inference_manager.get_inference_pool(model_context, model_sources)
return inference_manager.get_inference_pool(__name__, model_names, model_source_set)
def clear_inference_pool() -> None: def clear_inference_pool() -> None:
model_names = [ state_manager.get_item('face_landmarker_model'), 'fan_68_5' ] model_context = __name__ + '.' + state_manager.get_item('face_landmarker_model')
inference_manager.clear_inference_pool(__name__, model_names) inference_manager.clear_inference_pool(model_context)
def collect_model_downloads() -> Tuple[DownloadSet, DownloadSet]: def collect_model_downloads() -> Tuple[DownloadSet, DownloadSet]:
model_set = create_static_model_set('full') model_hashes =\
model_hash_set =\
{ {
'fan_68_5': model_set.get('fan_68_5').get('hashes').get('fan_68_5') 'fan_68_5': MODEL_SET.get('fan_68_5').get('hashes').get('fan_68_5')
} }
model_source_set =\ model_sources =\
{ {
'fan_68_5': model_set.get('fan_68_5').get('sources').get('fan_68_5') 'fan_68_5': MODEL_SET.get('fan_68_5').get('sources').get('fan_68_5')
} }
for face_landmarker_model in [ '2dfan4', 'peppa_wutz' ]: if state_manager.get_item('face_landmarker_model') in [ 'many', '2dfan4' ]:
if state_manager.get_item('face_landmarker_model') in [ 'many', face_landmarker_model ]: model_hashes['2dfan4'] = MODEL_SET.get('2dfan4').get('hashes').get('2dfan4')
model_hash_set[face_landmarker_model] = model_set.get(face_landmarker_model).get('hashes').get(face_landmarker_model) model_sources['2dfan4'] = MODEL_SET.get('2dfan4').get('sources').get('2dfan4')
model_source_set[face_landmarker_model] = model_set.get(face_landmarker_model).get('sources').get(face_landmarker_model) if state_manager.get_item('face_landmarker_model') in [ 'many', 'peppa_wutz' ]:
model_hashes['peppa_wutz'] = MODEL_SET.get('peppa_wutz').get('hashes').get('peppa_wutz')
return model_hash_set, model_source_set model_sources['peppa_wutz'] = MODEL_SET.get('peppa_wutz').get('sources').get('peppa_wutz')
return model_hashes, model_sources
def pre_check() -> bool: def pre_check() -> bool:
model_hash_set, model_source_set = collect_model_downloads() download_directory_path = resolve_relative_path('../.assets/models')
model_hashes, model_sources = collect_model_downloads()
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set) return conditional_download_hashes(download_directory_path, model_hashes) and conditional_download_sources(download_directory_path, model_sources)
def detect_face_landmark(vision_frame : VisionFrame, bounding_box : BoundingBox, face_angle : Angle) -> Tuple[FaceLandmark68, Score]: def detect_face_landmarks(vision_frame : VisionFrame, bounding_box : BoundingBox, face_angle : Angle) -> Tuple[FaceLandmark68, Score]:
face_landmark_2dfan4 = None face_landmark_2dfan4 = None
face_landmark_peppa_wutz = None face_landmark_peppa_wutz = None
face_landmark_score_2dfan4 = 0.0 face_landmark_score_2dfan4 = 0.0
@@ -141,7 +119,6 @@ def detect_face_landmark(vision_frame : VisionFrame, bounding_box : BoundingBox,
if state_manager.get_item('face_landmarker_model') in [ 'many', '2dfan4' ]: if state_manager.get_item('face_landmarker_model') in [ 'many', '2dfan4' ]:
face_landmark_2dfan4, face_landmark_score_2dfan4 = detect_with_2dfan4(vision_frame, bounding_box, face_angle) face_landmark_2dfan4, face_landmark_score_2dfan4 = detect_with_2dfan4(vision_frame, bounding_box, face_angle)
if state_manager.get_item('face_landmarker_model') in [ 'many', 'peppa_wutz' ]: if state_manager.get_item('face_landmarker_model') in [ 'many', 'peppa_wutz' ]:
face_landmark_peppa_wutz, face_landmark_score_peppa_wutz = detect_with_peppa_wutz(vision_frame, bounding_box, face_angle) face_landmark_peppa_wutz, face_landmark_score_peppa_wutz = detect_with_peppa_wutz(vision_frame, bounding_box, face_angle)
@@ -151,17 +128,17 @@ def detect_face_landmark(vision_frame : VisionFrame, bounding_box : BoundingBox,
def detect_with_2dfan4(temp_vision_frame: VisionFrame, bounding_box: BoundingBox, face_angle: Angle) -> Tuple[FaceLandmark68, Score]: def detect_with_2dfan4(temp_vision_frame: VisionFrame, bounding_box: BoundingBox, face_angle: Angle) -> Tuple[FaceLandmark68, Score]:
model_size = create_static_model_set('full').get('2dfan4').get('size') model_size = MODEL_SET.get('2dfan4').get('size')
scale = 195 / numpy.subtract(bounding_box[2:], bounding_box[:2]).max().clip(1, None) scale = 195 / numpy.subtract(bounding_box[2:], bounding_box[:2]).max().clip(1, None)
translation = (model_size[0] - numpy.add(bounding_box[2:], bounding_box[:2]) * scale) * 0.5 translation = (model_size[0] - numpy.add(bounding_box[2:], bounding_box[:2]) * scale) * 0.5
rotation_matrix, rotation_size = create_rotation_matrix_and_size(face_angle, model_size) rotated_matrix, rotated_size = create_rotated_matrix_and_size(face_angle, model_size)
crop_vision_frame, affine_matrix = warp_face_by_translation(temp_vision_frame, translation, scale, model_size) crop_vision_frame, affine_matrix = warp_face_by_translation(temp_vision_frame, translation, scale, model_size)
crop_vision_frame = cv2.warpAffine(crop_vision_frame, rotation_matrix, rotation_size) crop_vision_frame = cv2.warpAffine(crop_vision_frame, rotated_matrix, rotated_size)
crop_vision_frame = conditional_optimize_contrast(crop_vision_frame) crop_vision_frame = conditional_optimize_contrast(crop_vision_frame)
crop_vision_frame = crop_vision_frame.transpose(2, 0, 1).astype(numpy.float32) / 255.0 crop_vision_frame = crop_vision_frame.transpose(2, 0, 1).astype(numpy.float32) / 255.0
face_landmark_68, face_heatmap = forward_with_2dfan4(crop_vision_frame) face_landmark_68, face_heatmap = forward_with_2dfan4(crop_vision_frame)
face_landmark_68 = face_landmark_68[:, :, :2][0] / 64 * 256 face_landmark_68 = face_landmark_68[:, :, :2][0] / 64 * 256
face_landmark_68 = transform_points(face_landmark_68, cv2.invertAffineTransform(rotation_matrix)) face_landmark_68 = transform_points(face_landmark_68, cv2.invertAffineTransform(rotated_matrix))
face_landmark_68 = transform_points(face_landmark_68, cv2.invertAffineTransform(affine_matrix)) face_landmark_68 = transform_points(face_landmark_68, cv2.invertAffineTransform(affine_matrix))
face_landmark_score_68 = numpy.amax(face_heatmap, axis = (2, 3)) face_landmark_score_68 = numpy.amax(face_heatmap, axis = (2, 3))
face_landmark_score_68 = numpy.mean(face_landmark_score_68) face_landmark_score_68 = numpy.mean(face_landmark_score_68)
@@ -170,18 +147,18 @@ def detect_with_2dfan4(temp_vision_frame: VisionFrame, bounding_box: BoundingBox
def detect_with_peppa_wutz(temp_vision_frame : VisionFrame, bounding_box : BoundingBox, face_angle : Angle) -> Tuple[FaceLandmark68, Score]: def detect_with_peppa_wutz(temp_vision_frame : VisionFrame, bounding_box : BoundingBox, face_angle : Angle) -> Tuple[FaceLandmark68, Score]:
model_size = create_static_model_set('full').get('peppa_wutz').get('size') model_size = MODEL_SET.get('peppa_wutz').get('size')
scale = 195 / numpy.subtract(bounding_box[2:], bounding_box[:2]).max().clip(1, None) scale = 195 / numpy.subtract(bounding_box[2:], bounding_box[:2]).max().clip(1, None)
translation = (model_size[0] - numpy.add(bounding_box[2:], bounding_box[:2]) * scale) * 0.5 translation = (model_size[0] - numpy.add(bounding_box[2:], bounding_box[:2]) * scale) * 0.5
rotation_matrix, rotation_size = create_rotation_matrix_and_size(face_angle, model_size) rotated_matrix, rotated_size = create_rotated_matrix_and_size(face_angle, model_size)
crop_vision_frame, affine_matrix = warp_face_by_translation(temp_vision_frame, translation, scale, model_size) crop_vision_frame, affine_matrix = warp_face_by_translation(temp_vision_frame, translation, scale, model_size)
crop_vision_frame = cv2.warpAffine(crop_vision_frame, rotation_matrix, rotation_size) crop_vision_frame = cv2.warpAffine(crop_vision_frame, rotated_matrix, rotated_size)
crop_vision_frame = conditional_optimize_contrast(crop_vision_frame) crop_vision_frame = conditional_optimize_contrast(crop_vision_frame)
crop_vision_frame = crop_vision_frame.transpose(2, 0, 1).astype(numpy.float32) / 255.0 crop_vision_frame = crop_vision_frame.transpose(2, 0, 1).astype(numpy.float32) / 255.0
crop_vision_frame = numpy.expand_dims(crop_vision_frame, axis = 0) crop_vision_frame = numpy.expand_dims(crop_vision_frame, axis = 0)
prediction = forward_with_peppa_wutz(crop_vision_frame) prediction = forward_with_peppa_wutz(crop_vision_frame)
face_landmark_68 = prediction.reshape(-1, 3)[:, :2] / 64 * model_size[0] face_landmark_68 = prediction.reshape(-1, 3)[:, :2] / 64 * model_size[0]
face_landmark_68 = transform_points(face_landmark_68, cv2.invertAffineTransform(rotation_matrix)) face_landmark_68 = transform_points(face_landmark_68, cv2.invertAffineTransform(rotated_matrix))
face_landmark_68 = transform_points(face_landmark_68, cv2.invertAffineTransform(affine_matrix)) face_landmark_68 = transform_points(face_landmark_68, cv2.invertAffineTransform(affine_matrix))
face_landmark_score_68 = prediction.reshape(-1, 3)[:, 2].mean() face_landmark_score_68 = prediction.reshape(-1, 3)[:, 2].mean()
face_landmark_score_68 = numpy.interp(face_landmark_score_68, [ 0, 0.95 ], [ 0, 1 ]) face_landmark_score_68 = numpy.interp(face_landmark_score_68, [ 0, 0.95 ], [ 0, 1 ])
@@ -190,7 +167,7 @@ def detect_with_peppa_wutz(temp_vision_frame : VisionFrame, bounding_box : Bound
def conditional_optimize_contrast(crop_vision_frame : VisionFrame) -> VisionFrame: def conditional_optimize_contrast(crop_vision_frame : VisionFrame) -> VisionFrame:
crop_vision_frame = cv2.cvtColor(crop_vision_frame, cv2.COLOR_RGB2Lab) crop_vision_frame = cv2.cvtColor(crop_vision_frame, cv2.COLOR_RGB2Lab)
if numpy.mean(crop_vision_frame[:, :, 0]) < 30: #type:ignore[arg-type] if numpy.mean(crop_vision_frame[:, :, 0]) < 30: # type:ignore[arg-type]
crop_vision_frame[:, :, 0] = cv2.createCLAHE(clipLimit = 2).apply(crop_vision_frame[:, :, 0]) crop_vision_frame[:, :, 0] = cv2.createCLAHE(clipLimit = 2).apply(crop_vision_frame[:, :, 0])
crop_vision_frame = cv2.cvtColor(crop_vision_frame, cv2.COLOR_Lab2RGB) crop_vision_frame = cv2.cvtColor(crop_vision_frame, cv2.COLOR_Lab2RGB)
return crop_vision_frame return crop_vision_frame

View File

@@ -1,138 +1,45 @@
from functools import lru_cache from functools import lru_cache
from typing import List, Tuple from typing import Dict, List, Tuple
import cv2 import cv2
import numpy import numpy
from cv2.typing import Size
import facefusion.choices from facefusion import inference_manager
from facefusion import inference_manager, state_manager from facefusion.download import conditional_download_hashes, conditional_download_sources
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
from facefusion.filesystem import resolve_relative_path from facefusion.filesystem import resolve_relative_path
from facefusion.thread_helper import conditional_thread_semaphore from facefusion.thread_helper import conditional_thread_semaphore
from facefusion.types import DownloadScope, DownloadSet, FaceLandmark68, FaceMaskArea, FaceMaskRegion, InferencePool, Mask, ModelSet, Padding, VisionFrame from facefusion.typing import DownloadSet, FaceLandmark68, FaceMaskRegion, InferencePool, Mask, ModelSet, Padding, VisionFrame
MODEL_SET : ModelSet =\
@lru_cache() {
def create_static_model_set(download_scope : DownloadScope) -> ModelSet: 'face_occluder':
return\
{ {
'xseg_1':
{
'__metadata__':
{
'vendor': 'DeepFaceLab',
'license': 'GPL-3.0',
'year': 2021
},
'hashes': 'hashes':
{ {
'face_occluder': 'face_occluder':
{ {
'url': resolve_download_url('models-3.1.0', 'xseg_1.hash'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/dfl_xseg.hash',
'path': resolve_relative_path('../.assets/models/xseg_1.hash') 'path': resolve_relative_path('../.assets/models/dfl_xseg.hash')
} }
}, },
'sources': 'sources':
{ {
'face_occluder': 'face_occluder':
{ {
'url': resolve_download_url('models-3.1.0', 'xseg_1.onnx'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/dfl_xseg.onnx',
'path': resolve_relative_path('../.assets/models/xseg_1.onnx') 'path': resolve_relative_path('../.assets/models/dfl_xseg.onnx')
} }
}, },
'size': (256, 256) 'size': (256, 256)
}, },
'xseg_2': 'face_parser':
{ {
'__metadata__':
{
'vendor': 'DeepFaceLab',
'license': 'GPL-3.0',
'year': 2021
},
'hashes':
{
'face_occluder':
{
'url': resolve_download_url('models-3.1.0', 'xseg_2.hash'),
'path': resolve_relative_path('../.assets/models/xseg_2.hash')
}
},
'sources':
{
'face_occluder':
{
'url': resolve_download_url('models-3.1.0', 'xseg_2.onnx'),
'path': resolve_relative_path('../.assets/models/xseg_2.onnx')
}
},
'size': (256, 256)
},
'xseg_3':
{
'__metadata__':
{
'vendor': 'DeepFaceLab',
'license': 'GPL-3.0',
'year': 2021
},
'hashes':
{
'face_occluder':
{
'url': resolve_download_url('models-3.2.0', 'xseg_3.hash'),
'path': resolve_relative_path('../.assets/models/xseg_3.hash')
}
},
'sources':
{
'face_occluder':
{
'url': resolve_download_url('models-3.2.0', 'xseg_3.onnx'),
'path': resolve_relative_path('../.assets/models/xseg_3.onnx')
}
},
'size': (256, 256)
},
'bisenet_resnet_18':
{
'__metadata__':
{
'vendor': 'yakhyo',
'license': 'MIT',
'year': 2024
},
'hashes': 'hashes':
{ {
'face_parser': 'face_parser':
{ {
'url': resolve_download_url('models-3.1.0', 'bisenet_resnet_18.hash'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/bisenet_resnet_34.hash',
'path': resolve_relative_path('../.assets/models/bisenet_resnet_18.hash')
}
},
'sources':
{
'face_parser':
{
'url': resolve_download_url('models-3.1.0', 'bisenet_resnet_18.onnx'),
'path': resolve_relative_path('../.assets/models/bisenet_resnet_18.onnx')
}
},
'size': (512, 512)
},
'bisenet_resnet_34':
{
'__metadata__':
{
'vendor': 'yakhyo',
'license': 'MIT',
'year': 2024
},
'hashes':
{
'face_parser':
{
'url': resolve_download_url('models-3.0.0', 'bisenet_resnet_34.hash'),
'path': resolve_relative_path('../.assets/models/bisenet_resnet_34.hash') 'path': resolve_relative_path('../.assets/models/bisenet_resnet_34.hash')
} }
}, },
@@ -140,53 +47,60 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
{ {
'face_parser': 'face_parser':
{ {
'url': resolve_download_url('models-3.0.0', 'bisenet_resnet_34.onnx'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/bisenet_resnet_34.onnx',
'path': resolve_relative_path('../.assets/models/bisenet_resnet_34.onnx') 'path': resolve_relative_path('../.assets/models/bisenet_resnet_34.onnx')
} }
}, },
'size': (512, 512) 'size': (512, 512)
} }
} }
FACE_MASK_REGIONS : Dict[FaceMaskRegion, int] =\
{
'skin': 1,
'left-eyebrow': 2,
'right-eyebrow': 3,
'left-eye': 4,
'right-eye': 5,
'glasses': 6,
'nose': 10,
'mouth': 11,
'upper-lip': 12,
'lower-lip': 13
}
def get_inference_pool() -> InferencePool: def get_inference_pool() -> InferencePool:
model_names = [ state_manager.get_item('face_occluder_model'), state_manager.get_item('face_parser_model') ] _, model_sources = collect_model_downloads()
_, model_source_set = collect_model_downloads() return inference_manager.get_inference_pool(__name__, model_sources)
return inference_manager.get_inference_pool(__name__, model_names, model_source_set)
def clear_inference_pool() -> None: def clear_inference_pool() -> None:
model_names = [ state_manager.get_item('face_occluder_model'), state_manager.get_item('face_parser_model') ] inference_manager.clear_inference_pool(__name__)
inference_manager.clear_inference_pool(__name__, model_names)
def collect_model_downloads() -> Tuple[DownloadSet, DownloadSet]: def collect_model_downloads() -> Tuple[DownloadSet, DownloadSet]:
model_set = create_static_model_set('full') model_hashes =\
model_hash_set = {} {
model_source_set = {} 'face_occluder': MODEL_SET.get('face_occluder').get('hashes').get('face_occluder'),
'face_parser': MODEL_SET.get('face_parser').get('hashes').get('face_parser')
for face_occluder_model in [ 'xseg_1', 'xseg_2', 'xseg_3' ]: }
if state_manager.get_item('face_occluder_model') in [ 'many', face_occluder_model ]: model_sources =\
model_hash_set[face_occluder_model] = model_set.get(face_occluder_model).get('hashes').get('face_occluder') {
model_source_set[face_occluder_model] = model_set.get(face_occluder_model).get('sources').get('face_occluder') 'face_occluder': MODEL_SET.get('face_occluder').get('sources').get('face_occluder'),
'face_parser': MODEL_SET.get('face_parser').get('sources').get('face_parser')
for face_parser_model in [ 'bisenet_resnet_18', 'bisenet_resnet_34' ]: }
if state_manager.get_item('face_parser_model') == face_parser_model: return model_hashes, model_sources
model_hash_set[face_parser_model] = model_set.get(face_parser_model).get('hashes').get('face_parser')
model_source_set[face_parser_model] = model_set.get(face_parser_model).get('sources').get('face_parser')
return model_hash_set, model_source_set
def pre_check() -> bool: def pre_check() -> bool:
model_hash_set, model_source_set = collect_model_downloads() download_directory_path = resolve_relative_path('../.assets/models')
model_hashes, model_sources = collect_model_downloads()
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set) return conditional_download_hashes(download_directory_path, model_hashes) and conditional_download_sources(download_directory_path, model_sources)
def create_box_mask(crop_vision_frame : VisionFrame, face_mask_blur : float, face_mask_padding : Padding) -> Mask: @lru_cache(maxsize = None)
crop_size = crop_vision_frame.shape[:2][::-1] def create_static_box_mask(crop_size : Size, face_mask_blur : float, face_mask_padding : Padding) -> Mask:
blur_amount = int(crop_size[0] * 0.5 * face_mask_blur) blur_amount = int(crop_size[0] * 0.5 * face_mask_blur)
blur_area = max(blur_amount // 2, 1) blur_area = max(blur_amount // 2, 1)
box_mask : Mask = numpy.ones(crop_size).astype(numpy.float32) box_mask : Mask = numpy.ones(crop_size).astype(numpy.float32)
@@ -194,68 +108,49 @@ def create_box_mask(crop_vision_frame : VisionFrame, face_mask_blur : float, fac
box_mask[-max(blur_area, int(crop_size[1] * face_mask_padding[2] / 100)):, :] = 0 box_mask[-max(blur_area, int(crop_size[1] * face_mask_padding[2] / 100)):, :] = 0
box_mask[:, :max(blur_area, int(crop_size[0] * face_mask_padding[3] / 100))] = 0 box_mask[:, :max(blur_area, int(crop_size[0] * face_mask_padding[3] / 100))] = 0
box_mask[:, -max(blur_area, int(crop_size[0] * face_mask_padding[1] / 100)):] = 0 box_mask[:, -max(blur_area, int(crop_size[0] * face_mask_padding[1] / 100)):] = 0
if blur_amount > 0: if blur_amount > 0:
box_mask = cv2.GaussianBlur(box_mask, (0, 0), blur_amount * 0.25) box_mask = cv2.GaussianBlur(box_mask, (0, 0), blur_amount * 0.25)
return box_mask return box_mask
def create_occlusion_mask(crop_vision_frame : VisionFrame) -> Mask: def create_occlusion_mask(crop_vision_frame : VisionFrame) -> Mask:
temp_masks = [] model_size = MODEL_SET.get('face_occluder').get('size')
if state_manager.get_item('face_occluder_model') == 'many':
model_names = [ 'xseg_1', 'xseg_2', 'xseg_3' ]
else:
model_names = [ state_manager.get_item('face_occluder_model') ]
for model_name in model_names:
model_size = create_static_model_set('full').get(model_name).get('size')
prepare_vision_frame = cv2.resize(crop_vision_frame, model_size) prepare_vision_frame = cv2.resize(crop_vision_frame, model_size)
prepare_vision_frame = numpy.expand_dims(prepare_vision_frame, axis = 0).astype(numpy.float32) / 255.0 prepare_vision_frame = numpy.expand_dims(prepare_vision_frame, axis = 0).astype(numpy.float32) / 255
prepare_vision_frame = prepare_vision_frame.transpose(0, 1, 2, 3) prepare_vision_frame = prepare_vision_frame.transpose(0, 1, 2, 3)
temp_mask = forward_occlude_face(prepare_vision_frame, model_name) occlusion_mask = forward_occlude_face(prepare_vision_frame)
temp_mask = temp_mask.transpose(0, 1, 2).clip(0, 1).astype(numpy.float32) occlusion_mask = occlusion_mask.transpose(0, 1, 2).clip(0, 1).astype(numpy.float32)
temp_mask = cv2.resize(temp_mask, crop_vision_frame.shape[:2][::-1]) occlusion_mask = cv2.resize(occlusion_mask, crop_vision_frame.shape[:2][::-1])
temp_masks.append(temp_mask)
occlusion_mask = numpy.minimum.reduce(temp_masks)
occlusion_mask = (cv2.GaussianBlur(occlusion_mask.clip(0, 1), (0, 0), 5).clip(0.5, 1) - 0.5) * 2 occlusion_mask = (cv2.GaussianBlur(occlusion_mask.clip(0, 1), (0, 0), 5).clip(0.5, 1) - 0.5) * 2
return occlusion_mask return occlusion_mask
def create_area_mask(crop_vision_frame : VisionFrame, face_landmark_68 : FaceLandmark68, face_mask_areas : List[FaceMaskArea]) -> Mask:
crop_size = crop_vision_frame.shape[:2][::-1]
landmark_points = []
for face_mask_area in face_mask_areas:
if face_mask_area in facefusion.choices.face_mask_area_set:
landmark_points.extend(facefusion.choices.face_mask_area_set.get(face_mask_area))
convex_hull = cv2.convexHull(face_landmark_68[landmark_points].astype(numpy.int32))
area_mask = numpy.zeros(crop_size).astype(numpy.float32)
cv2.fillConvexPoly(area_mask, convex_hull, 1.0) # type: ignore[call-overload]
area_mask = (cv2.GaussianBlur(area_mask.clip(0, 1), (0, 0), 5).clip(0.5, 1) - 0.5) * 2
return area_mask
def create_region_mask(crop_vision_frame : VisionFrame, face_mask_regions : List[FaceMaskRegion]) -> Mask: def create_region_mask(crop_vision_frame : VisionFrame, face_mask_regions : List[FaceMaskRegion]) -> Mask:
model_name = state_manager.get_item('face_parser_model') model_size = MODEL_SET.get('face_parser').get('size')
model_size = create_static_model_set('full').get(model_name).get('size')
prepare_vision_frame = cv2.resize(crop_vision_frame, model_size) prepare_vision_frame = cv2.resize(crop_vision_frame, model_size)
prepare_vision_frame = prepare_vision_frame[:, :, ::-1].astype(numpy.float32) / 255.0 prepare_vision_frame = prepare_vision_frame[:, :, ::-1].astype(numpy.float32) / 255
prepare_vision_frame = numpy.subtract(prepare_vision_frame, numpy.array([ 0.485, 0.456, 0.406 ]).astype(numpy.float32)) prepare_vision_frame = numpy.subtract(prepare_vision_frame, numpy.array([ 0.485, 0.456, 0.406 ]).astype(numpy.float32))
prepare_vision_frame = numpy.divide(prepare_vision_frame, numpy.array([ 0.229, 0.224, 0.225 ]).astype(numpy.float32)) prepare_vision_frame = numpy.divide(prepare_vision_frame, numpy.array([ 0.229, 0.224, 0.225 ]).astype(numpy.float32))
prepare_vision_frame = numpy.expand_dims(prepare_vision_frame, axis = 0) prepare_vision_frame = numpy.expand_dims(prepare_vision_frame, axis = 0)
prepare_vision_frame = prepare_vision_frame.transpose(0, 3, 1, 2) prepare_vision_frame = prepare_vision_frame.transpose(0, 3, 1, 2)
region_mask = forward_parse_face(prepare_vision_frame) region_mask = forward_parse_face(prepare_vision_frame)
region_mask = numpy.isin(region_mask.argmax(0), [ facefusion.choices.face_mask_region_set.get(face_mask_region) for face_mask_region in face_mask_regions ]) region_mask = numpy.isin(region_mask.argmax(0), [ FACE_MASK_REGIONS[region] for region in face_mask_regions ])
region_mask = cv2.resize(region_mask.astype(numpy.float32), crop_vision_frame.shape[:2][::-1]) region_mask = cv2.resize(region_mask.astype(numpy.float32), crop_vision_frame.shape[:2][::-1])
region_mask = (cv2.GaussianBlur(region_mask.clip(0, 1), (0, 0), 5).clip(0.5, 1) - 0.5) * 2 region_mask = (cv2.GaussianBlur(region_mask.clip(0, 1), (0, 0), 5).clip(0.5, 1) - 0.5) * 2
return region_mask return region_mask
def forward_occlude_face(prepare_vision_frame : VisionFrame, model_name : str) -> Mask: def create_mouth_mask(face_landmark_68 : FaceLandmark68) -> Mask:
face_occluder = get_inference_pool().get(model_name) convex_hull = cv2.convexHull(face_landmark_68[numpy.r_[3:14, 31:36]].astype(numpy.int32))
mouth_mask : Mask = numpy.zeros((512, 512)).astype(numpy.float32)
mouth_mask = cv2.fillConvexPoly(mouth_mask, convex_hull, 1.0) #type:ignore[call-overload]
mouth_mask = cv2.erode(mouth_mask.clip(0, 1), numpy.ones((21, 3)))
mouth_mask = cv2.GaussianBlur(mouth_mask, (0, 0), sigmaX = 1, sigmaY = 15)
return mouth_mask
def forward_occlude_face(prepare_vision_frame : VisionFrame) -> Mask:
face_occluder = get_inference_pool().get('face_occluder')
with conditional_thread_semaphore(): with conditional_thread_semaphore():
occlusion_mask : Mask = face_occluder.run(None, occlusion_mask : Mask = face_occluder.run(None,
@@ -267,8 +162,7 @@ def forward_occlude_face(prepare_vision_frame : VisionFrame, model_name : str) -
def forward_parse_face(prepare_vision_frame : VisionFrame) -> Mask: def forward_parse_face(prepare_vision_frame : VisionFrame) -> Mask:
model_name = state_manager.get_item('face_parser_model') face_parser = get_inference_pool().get('face_parser')
face_parser = get_inference_pool().get(model_name)
with conditional_thread_semaphore(): with conditional_thread_semaphore():
region_mask : Mask = face_parser.run(None, region_mask : Mask = face_parser.run(None,

View File

@@ -1,33 +1,23 @@
from functools import lru_cache
from typing import Tuple from typing import Tuple
import numpy import numpy
from facefusion import inference_manager from facefusion import inference_manager
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url from facefusion.download import conditional_download_hashes, conditional_download_sources
from facefusion.face_helper import warp_face_by_face_landmark_5 from facefusion.face_helper import warp_face_by_face_landmark_5
from facefusion.filesystem import resolve_relative_path from facefusion.filesystem import resolve_relative_path
from facefusion.thread_helper import conditional_thread_semaphore from facefusion.thread_helper import conditional_thread_semaphore
from facefusion.types import DownloadScope, Embedding, FaceLandmark5, InferencePool, ModelOptions, ModelSet, VisionFrame from facefusion.typing import Embedding, FaceLandmark5, InferencePool, ModelOptions, ModelSet, VisionFrame
MODEL_SET : ModelSet =\
@lru_cache() {
def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
return\
{
'arcface': 'arcface':
{ {
'__metadata__':
{
'vendor': 'InsightFace',
'license': 'Non-Commercial',
'year': 2018
},
'hashes': 'hashes':
{ {
'face_recognizer': 'face_recognizer':
{ {
'url': resolve_download_url('models-3.0.0', 'arcface_w600k_r50.hash'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/arcface_w600k_r50.hash',
'path': resolve_relative_path('../.assets/models/arcface_w600k_r50.hash') 'path': resolve_relative_path('../.assets/models/arcface_w600k_r50.hash')
} }
}, },
@@ -35,59 +25,57 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
{ {
'face_recognizer': 'face_recognizer':
{ {
'url': resolve_download_url('models-3.0.0', 'arcface_w600k_r50.onnx'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/arcface_w600k_r50.onnx',
'path': resolve_relative_path('../.assets/models/arcface_w600k_r50.onnx') 'path': resolve_relative_path('../.assets/models/arcface_w600k_r50.onnx')
} }
}, },
'template': 'arcface_112_v2', 'template': 'arcface_112_v2',
'size': (112, 112) 'size': (112, 112)
} }
} }
def get_inference_pool() -> InferencePool: def get_inference_pool() -> InferencePool:
model_names = [ 'arcface' ] model_sources = get_model_options().get('sources')
model_source_set = get_model_options().get('sources') return inference_manager.get_inference_pool(__name__, model_sources)
return inference_manager.get_inference_pool(__name__, model_names, model_source_set)
def clear_inference_pool() -> None: def clear_inference_pool() -> None:
model_names = [ 'arcface' ] inference_manager.clear_inference_pool(__name__)
inference_manager.clear_inference_pool(__name__, model_names)
def get_model_options() -> ModelOptions: def get_model_options() -> ModelOptions:
return create_static_model_set('full').get('arcface') return MODEL_SET.get('arcface')
def pre_check() -> bool: def pre_check() -> bool:
model_hash_set = get_model_options().get('hashes') download_directory_path = resolve_relative_path('../.assets/models')
model_source_set = get_model_options().get('sources') model_hashes = get_model_options().get('hashes')
model_sources = get_model_options().get('sources')
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set) return conditional_download_hashes(download_directory_path, model_hashes) and conditional_download_sources(download_directory_path, model_sources)
def calculate_face_embedding(temp_vision_frame : VisionFrame, face_landmark_5 : FaceLandmark5) -> Tuple[Embedding, Embedding]: def calc_embedding(temp_vision_frame : VisionFrame, face_landmark_5 : FaceLandmark5) -> Tuple[Embedding, Embedding]:
model_template = get_model_options().get('template') model_template = get_model_options().get('template')
model_size = get_model_options().get('size') model_size = get_model_options().get('size')
crop_vision_frame, matrix = warp_face_by_face_landmark_5(temp_vision_frame, face_landmark_5, model_template, model_size) crop_vision_frame, matrix = warp_face_by_face_landmark_5(temp_vision_frame, face_landmark_5, model_template, model_size)
crop_vision_frame = crop_vision_frame / 127.5 - 1 crop_vision_frame = crop_vision_frame / 127.5 - 1
crop_vision_frame = crop_vision_frame[:, :, ::-1].transpose(2, 0, 1).astype(numpy.float32) crop_vision_frame = crop_vision_frame[:, :, ::-1].transpose(2, 0, 1).astype(numpy.float32)
crop_vision_frame = numpy.expand_dims(crop_vision_frame, axis = 0) crop_vision_frame = numpy.expand_dims(crop_vision_frame, axis = 0)
face_embedding = forward(crop_vision_frame) embedding = forward(crop_vision_frame)
face_embedding = face_embedding.ravel() embedding = embedding.ravel()
face_embedding_norm = face_embedding / numpy.linalg.norm(face_embedding) normed_embedding = embedding / numpy.linalg.norm(embedding)
return face_embedding, face_embedding_norm return embedding, normed_embedding
def forward(crop_vision_frame : VisionFrame) -> Embedding: def forward(crop_vision_frame : VisionFrame) -> Embedding:
face_recognizer = get_inference_pool().get('face_recognizer') face_recognizer = get_inference_pool().get('face_recognizer')
with conditional_thread_semaphore(): with conditional_thread_semaphore():
face_embedding = face_recognizer.run(None, embedding = face_recognizer.run(None,
{ {
'input': crop_vision_frame 'input': crop_vision_frame
})[0] })[0]
return face_embedding return embedding

View File

@@ -3,106 +3,67 @@ from typing import List
import numpy import numpy
from facefusion import state_manager from facefusion import state_manager
from facefusion.face_analyser import get_many_faces, get_one_face from facefusion.typing import Face, FaceSelectorOrder, FaceSet, Gender, Race
from facefusion.types import Face, FaceSelectorOrder, Gender, Race, Score, VisionFrame
def select_faces(reference_vision_frame : VisionFrame, target_vision_frame : VisionFrame) -> List[Face]: def find_similar_faces(faces : List[Face], reference_faces : FaceSet, face_distance : float) -> List[Face]:
target_faces = get_many_faces([ target_vision_frame ]) similar_faces : List[Face] = []
if state_manager.get_item('face_selector_mode') == 'many': if faces and reference_faces:
return sort_and_filter_faces(target_faces) for reference_set in reference_faces:
if not similar_faces:
if state_manager.get_item('face_selector_mode') == 'one': for reference_face in reference_faces[reference_set]:
target_face = get_one_face(sort_and_filter_faces(target_faces)) for face in faces:
if target_face: if compare_faces(face, reference_face, face_distance):
return [ target_face ] similar_faces.append(face)
return similar_faces
if state_manager.get_item('face_selector_mode') == 'reference':
reference_faces = get_many_faces([ reference_vision_frame ])
reference_faces = sort_and_filter_faces(reference_faces)
reference_face = get_one_face(reference_faces, state_manager.get_item('reference_face_position'))
if reference_face:
match_faces = find_match_faces([ reference_face ], target_faces, state_manager.get_item('reference_face_distance'))
return match_faces
return []
def find_match_faces(reference_faces : List[Face], target_faces : List[Face], face_distance : float) -> List[Face]:
match_faces : List[Face] = []
for reference_face in reference_faces:
if reference_face:
for index, target_face in enumerate(target_faces):
if compare_faces(target_face, reference_face, face_distance):
match_faces.append(target_faces[index])
return match_faces
def compare_faces(face : Face, reference_face : Face, face_distance : float) -> bool: def compare_faces(face : Face, reference_face : Face, face_distance : float) -> bool:
current_face_distance = calculate_face_distance(face, reference_face) current_face_distance = calc_face_distance(face, reference_face)
current_face_distance = float(numpy.interp(current_face_distance, [ 0, 2 ], [ 0, 1 ]))
return current_face_distance < face_distance return current_face_distance < face_distance
def calculate_face_distance(face : Face, reference_face : Face) -> float: def calc_face_distance(face : Face, reference_face : Face) -> float:
if hasattr(face, 'embedding_norm') and hasattr(reference_face, 'embedding_norm'): if hasattr(face, 'normed_embedding') and hasattr(reference_face, 'normed_embedding'):
return 1 - numpy.dot(face.embedding_norm, reference_face.embedding_norm) return 1 - numpy.dot(face.normed_embedding, reference_face.normed_embedding)
return 0 return 0
def sort_and_filter_faces(faces : List[Face]) -> List[Face]: def sort_and_filter_faces(faces : List[Face]) -> List[Face]:
if faces: if faces:
if state_manager.get_item('face_selector_order'): if state_manager.get_item('face_selector_order'):
faces = sort_faces_by_order(faces, state_manager.get_item('face_selector_order')) faces = sort_by_order(faces, state_manager.get_item('face_selector_order'))
if state_manager.get_item('face_selector_gender'): if state_manager.get_item('face_selector_gender'):
faces = filter_faces_by_gender(faces, state_manager.get_item('face_selector_gender')) faces = filter_by_gender(faces, state_manager.get_item('face_selector_gender'))
if state_manager.get_item('face_selector_race'): if state_manager.get_item('face_selector_race'):
faces = filter_faces_by_race(faces, state_manager.get_item('face_selector_race')) faces = filter_by_race(faces, state_manager.get_item('face_selector_race'))
if state_manager.get_item('face_selector_age_start') or state_manager.get_item('face_selector_age_end'): if state_manager.get_item('face_selector_age_start') or state_manager.get_item('face_selector_age_end'):
faces = filter_faces_by_age(faces, state_manager.get_item('face_selector_age_start'), state_manager.get_item('face_selector_age_end')) faces = filter_by_age(faces, state_manager.get_item('face_selector_age_start'), state_manager.get_item('face_selector_age_end'))
return faces return faces
def sort_faces_by_order(faces : List[Face], order : FaceSelectorOrder) -> List[Face]: def sort_by_order(faces : List[Face], order : FaceSelectorOrder) -> List[Face]:
if order == 'left-right': if order == 'left-right':
return sorted(faces, key = get_bounding_box_left) return sorted(faces, key = lambda face: face.bounding_box[0])
if order == 'right-left': if order == 'right-left':
return sorted(faces, key = get_bounding_box_left, reverse = True) return sorted(faces, key = lambda face: face.bounding_box[0], reverse = True)
if order == 'top-bottom': if order == 'top-bottom':
return sorted(faces, key = get_bounding_box_top) return sorted(faces, key = lambda face: face.bounding_box[1])
if order == 'bottom-top': if order == 'bottom-top':
return sorted(faces, key = get_bounding_box_top, reverse = True) return sorted(faces, key = lambda face: face.bounding_box[1], reverse = True)
if order == 'small-large': if order == 'small-large':
return sorted(faces, key = get_bounding_box_area) return sorted(faces, key = lambda face: (face.bounding_box[2] - face.bounding_box[0]) * (face.bounding_box[3] - face.bounding_box[1]))
if order == 'large-small': if order == 'large-small':
return sorted(faces, key = get_bounding_box_area, reverse = True) return sorted(faces, key = lambda face: (face.bounding_box[2] - face.bounding_box[0]) * (face.bounding_box[3] - face.bounding_box[1]), reverse = True)
if order == 'best-worst': if order == 'best-worst':
return sorted(faces, key = get_face_detector_score, reverse = True) return sorted(faces, key = lambda face: face.score_set.get('detector'), reverse = True)
if order == 'worst-best': if order == 'worst-best':
return sorted(faces, key = get_face_detector_score) return sorted(faces, key = lambda face: face.score_set.get('detector'))
return faces return faces
def get_bounding_box_left(face : Face) -> float: def filter_by_gender(faces : List[Face], gender : Gender) -> List[Face]:
return face.bounding_box[0]
def get_bounding_box_top(face : Face) -> float:
return face.bounding_box[1]
def get_bounding_box_area(face : Face) -> float:
return (face.bounding_box[2] - face.bounding_box[0]) * (face.bounding_box[3] - face.bounding_box[1])
def get_face_detector_score(face : Face) -> Score:
return face.score_set.get('detector')
def filter_faces_by_gender(faces : List[Face], gender : Gender) -> List[Face]:
filter_faces = [] filter_faces = []
for face in faces: for face in faces:
@@ -111,7 +72,7 @@ def filter_faces_by_gender(faces : List[Face], gender : Gender) -> List[Face]:
return filter_faces return filter_faces
def filter_faces_by_age(faces : List[Face], face_selector_age_start : int, face_selector_age_end : int) -> List[Face]: def filter_by_age(faces : List[Face], face_selector_age_start : int, face_selector_age_end : int) -> List[Face]:
filter_faces = [] filter_faces = []
age = range(face_selector_age_start, face_selector_age_end) age = range(face_selector_age_start, face_selector_age_end)
@@ -121,7 +82,7 @@ def filter_faces_by_age(faces : List[Face], face_selector_age_start : int, face_
return filter_faces return filter_faces
def filter_faces_by_race(faces : List[Face], race : Race) -> List[Face]: def filter_by_race(faces : List[Face], race : Race) -> List[Face]:
filter_faces = [] filter_faces = []
for face in faces: for face in faces:

View File

@@ -1,11 +1,14 @@
import hashlib
from typing import List, Optional from typing import List, Optional
from facefusion.hash_helper import create_hash import numpy
from facefusion.types import Face, FaceStore, VisionFrame
from facefusion.typing import Face, FaceSet, FaceStore, VisionFrame
FACE_STORE : FaceStore =\ FACE_STORE : FaceStore =\
{ {
'static_faces': {} 'static_faces': {},
'reference_faces': {}
} }
@@ -14,15 +17,37 @@ def get_face_store() -> FaceStore:
def get_static_faces(vision_frame : VisionFrame) -> Optional[List[Face]]: def get_static_faces(vision_frame : VisionFrame) -> Optional[List[Face]]:
vision_hash = create_hash(vision_frame.tobytes()) frame_hash = create_frame_hash(vision_frame)
return FACE_STORE.get('static_faces').get(vision_hash) if frame_hash in FACE_STORE['static_faces']:
return FACE_STORE['static_faces'][frame_hash]
return None
def set_static_faces(vision_frame : VisionFrame, faces : List[Face]) -> None: def set_static_faces(vision_frame : VisionFrame, faces : List[Face]) -> None:
vision_hash = create_hash(vision_frame.tobytes()) frame_hash = create_frame_hash(vision_frame)
if vision_hash: if frame_hash:
FACE_STORE['static_faces'][vision_hash] = faces FACE_STORE['static_faces'][frame_hash] = faces
def clear_static_faces() -> None: def clear_static_faces() -> None:
FACE_STORE['static_faces'].clear() FACE_STORE['static_faces'] = {}
def create_frame_hash(vision_frame : VisionFrame) -> Optional[str]:
return hashlib.sha1(vision_frame.tobytes()).hexdigest() if numpy.any(vision_frame) else None
def get_reference_faces() -> Optional[FaceSet]:
if FACE_STORE['reference_faces']:
return FACE_STORE['reference_faces']
return None
def append_reference_face(name : str, face : Face) -> None:
if name not in FACE_STORE['reference_faces']:
FACE_STORE['reference_faces'][name] = []
FACE_STORE['reference_faces'][name].append(face)
def clear_reference_faces() -> None:
FACE_STORE['reference_faces'] = {}

View File

@@ -1,58 +1,26 @@
import os import os
import shutil
import subprocess import subprocess
import tempfile import tempfile
from functools import partial from typing import List, Optional
from typing import List, Optional, cast
from tqdm import tqdm import filetype
import facefusion.choices from facefusion import logger, process_manager, state_manager
from facefusion import ffmpeg_builder, logger, process_manager, state_manager, translator from facefusion.filesystem import remove_file
from facefusion.filesystem import get_file_format, remove_file
from facefusion.temp_helper import get_temp_file_path, get_temp_frames_pattern from facefusion.temp_helper import get_temp_file_path, get_temp_frames_pattern
from facefusion.types import AudioBuffer, AudioEncoder, Command, EncoderSet, Fps, Resolution, UpdateProgress, VideoEncoder, VideoFormat from facefusion.typing import AudioBuffer, Fps, OutputVideoPreset
from facefusion.vision import detect_video_duration, detect_video_fps, pack_resolution, predict_video_frame_total from facefusion.vision import restrict_video_fps
def run_ffmpeg_with_progress(commands : List[Command], update_progress : UpdateProgress) -> subprocess.Popen[bytes]: def run_ffmpeg(args : List[str]) -> subprocess.Popen[bytes]:
log_level = state_manager.get_item('log_level') commands = [ shutil.which('ffmpeg'), '-hide_banner', '-loglevel', 'error' ]
commands.extend(ffmpeg_builder.set_progress()) commands.extend(args)
commands.extend(ffmpeg_builder.cast_stream())
commands = ffmpeg_builder.run(commands)
process = subprocess.Popen(commands, stderr = subprocess.PIPE, stdout = subprocess.PIPE) process = subprocess.Popen(commands, stderr = subprocess.PIPE, stdout = subprocess.PIPE)
while process_manager.is_processing(): while process_manager.is_processing():
try: try:
while __line__ := process.stdout.readline().decode().lower(): if state_manager.get_item('log_level') == 'debug':
if process_manager.is_stopping():
process.terminate()
if 'frame=' in __line__:
_, frame_number = __line__.split('frame=')
update_progress(int(frame_number))
if log_level == 'debug':
log_debug(process)
process.wait(timeout = 0.5)
except subprocess.TimeoutExpired:
continue
return process
return process
def update_progress(progress : tqdm, frame_number : int) -> None:
progress.update(frame_number - progress.n)
def run_ffmpeg(commands : List[Command]) -> subprocess.Popen[bytes]:
log_level = state_manager.get_item('log_level')
commands = ffmpeg_builder.run(commands)
process = subprocess.Popen(commands, stderr = subprocess.PIPE, stdout = subprocess.PIPE)
while process_manager.is_processing():
try:
if log_level == 'debug':
log_debug(process) log_debug(process)
process.wait(timeout = 0.5) process.wait(timeout = 0.5)
except subprocess.TimeoutExpired: except subprocess.TimeoutExpired:
@@ -61,12 +29,12 @@ def run_ffmpeg(commands : List[Command]) -> subprocess.Popen[bytes]:
if process_manager.is_stopping(): if process_manager.is_stopping():
process.terminate() process.terminate()
return process return process
def open_ffmpeg(commands : List[Command]) -> subprocess.Popen[bytes]: def open_ffmpeg(args : List[str]) -> subprocess.Popen[bytes]:
commands = ffmpeg_builder.run(commands) commands = [ shutil.which('ffmpeg'), '-hide_banner', '-loglevel', 'quiet' ]
commands.extend(args)
return subprocess.Popen(commands, stdin = subprocess.PIPE, stdout = subprocess.PIPE) return subprocess.Popen(commands, stdin = subprocess.PIPE, stdout = subprocess.PIPE)
@@ -79,167 +47,46 @@ def log_debug(process : subprocess.Popen[bytes]) -> None:
logger.debug(error.strip(), __name__) logger.debug(error.strip(), __name__)
def get_available_encoder_set() -> EncoderSet: def extract_frames(target_path : str, temp_video_resolution : str, temp_video_fps : Fps) -> bool:
available_encoder_set : EncoderSet =\ trim_frame_start = state_manager.get_item('trim_frame_start')
{ trim_frame_end = state_manager.get_item('trim_frame_end')
'audio': [],
'video': []
}
commands = ffmpeg_builder.chain(
ffmpeg_builder.get_encoders()
)
process = run_ffmpeg(commands)
while line := process.stdout.readline().decode().lower():
if line.startswith(' a'):
audio_encoder = line.split()[1]
if audio_encoder in facefusion.choices.output_audio_encoders:
index = facefusion.choices.output_audio_encoders.index(audio_encoder) #type:ignore[arg-type]
available_encoder_set['audio'].insert(index, audio_encoder) #type:ignore[arg-type]
if line.startswith(' v'):
video_encoder = line.split()[1]
if video_encoder in facefusion.choices.output_video_encoders:
index = facefusion.choices.output_video_encoders.index(video_encoder) #type:ignore[arg-type]
available_encoder_set['video'].insert(index, video_encoder) #type:ignore[arg-type]
return available_encoder_set
def extract_frames(target_path : str, temp_video_resolution : Resolution, temp_video_fps : Fps, trim_frame_start : int, trim_frame_end : int) -> bool:
extract_frame_total = predict_video_frame_total(target_path, temp_video_fps, trim_frame_start, trim_frame_end)
temp_frames_pattern = get_temp_frames_pattern(target_path, '%08d') temp_frames_pattern = get_temp_frames_pattern(target_path, '%08d')
commands = ffmpeg_builder.chain( commands = [ '-i', target_path, '-s', str(temp_video_resolution), '-q:v', '0' ]
ffmpeg_builder.set_input(target_path),
ffmpeg_builder.set_media_resolution(pack_resolution(temp_video_resolution)),
ffmpeg_builder.set_frame_quality(0),
ffmpeg_builder.select_frame_range(trim_frame_start, trim_frame_end, temp_video_fps),
ffmpeg_builder.prevent_frame_drop(),
ffmpeg_builder.set_output(temp_frames_pattern)
)
with tqdm(total = extract_frame_total, desc = translator.get('extracting'), unit = 'frame', ascii = ' =', disable = state_manager.get_item('log_level') in [ 'warn', 'error' ]) as progress: if isinstance(trim_frame_start, int) and isinstance(trim_frame_end, int):
process = run_ffmpeg_with_progress(commands, partial(update_progress, progress)) commands.extend([ '-vf', 'trim=start_frame=' + str(trim_frame_start) + ':end_frame=' + str(trim_frame_end) + ',fps=' + str(temp_video_fps) ])
return process.returncode == 0 elif isinstance(trim_frame_start, int):
commands.extend([ '-vf', 'trim=start_frame=' + str(trim_frame_start) + ',fps=' + str(temp_video_fps) ])
elif isinstance(trim_frame_end, int):
def copy_image(target_path : str, temp_image_resolution : Resolution) -> bool: commands.extend([ '-vf', 'trim=end_frame=' + str(trim_frame_end) + ',fps=' + str(temp_video_fps) ])
temp_image_path = get_temp_file_path(target_path) else:
commands = ffmpeg_builder.chain( commands.extend([ '-vf', 'fps=' + str(temp_video_fps) ])
ffmpeg_builder.set_input(target_path), commands.extend([ '-vsync', '0', temp_frames_pattern ])
ffmpeg_builder.set_media_resolution(pack_resolution(temp_image_resolution)),
ffmpeg_builder.set_image_quality(target_path, 100),
ffmpeg_builder.force_output(temp_image_path)
)
return run_ffmpeg(commands).returncode == 0 return run_ffmpeg(commands).returncode == 0
def finalize_image(target_path : str, output_path : str, output_image_resolution : Resolution) -> bool: def merge_video(target_path : str, output_video_resolution : str, output_video_fps : Fps) -> bool:
output_image_quality = state_manager.get_item('output_image_quality') temp_video_fps = restrict_video_fps(target_path, output_video_fps)
temp_image_path = get_temp_file_path(target_path) temp_file_path = get_temp_file_path(target_path)
commands = ffmpeg_builder.chain(
ffmpeg_builder.set_input(temp_image_path),
ffmpeg_builder.set_media_resolution(pack_resolution(output_image_resolution)),
ffmpeg_builder.set_image_quality(target_path, output_image_quality),
ffmpeg_builder.force_output(output_path)
)
return run_ffmpeg(commands).returncode == 0
def read_audio_buffer(target_path : str, audio_sample_rate : int, audio_sample_size : int, audio_channel_total : int) -> Optional[AudioBuffer]:
commands = ffmpeg_builder.chain(
ffmpeg_builder.set_input(target_path),
ffmpeg_builder.ignore_video_stream(),
ffmpeg_builder.set_audio_sample_rate(audio_sample_rate),
ffmpeg_builder.set_audio_sample_size(audio_sample_size),
ffmpeg_builder.set_audio_channel_total(audio_channel_total),
ffmpeg_builder.cast_stream()
)
process = open_ffmpeg(commands)
audio_buffer, _ = process.communicate()
if process.returncode == 0:
return audio_buffer
return None
def restore_audio(target_path : str, output_path : str, trim_frame_start : int, trim_frame_end : int) -> bool:
output_audio_encoder = state_manager.get_item('output_audio_encoder')
output_audio_quality = state_manager.get_item('output_audio_quality')
output_audio_volume = state_manager.get_item('output_audio_volume')
target_video_fps = detect_video_fps(target_path)
temp_video_path = get_temp_file_path(target_path)
temp_video_format = cast(VideoFormat, get_file_format(temp_video_path))
temp_video_duration = detect_video_duration(temp_video_path)
output_audio_encoder = fix_audio_encoder(temp_video_format, output_audio_encoder)
commands = ffmpeg_builder.chain(
ffmpeg_builder.set_input(temp_video_path),
ffmpeg_builder.select_media_range(trim_frame_start, trim_frame_end, target_video_fps),
ffmpeg_builder.set_input(target_path),
ffmpeg_builder.copy_video_encoder(),
ffmpeg_builder.set_audio_encoder(output_audio_encoder),
ffmpeg_builder.set_audio_quality(output_audio_encoder, output_audio_quality),
ffmpeg_builder.set_audio_volume(output_audio_volume),
ffmpeg_builder.select_media_stream('0:v:0'),
ffmpeg_builder.select_media_stream('1:a:0'),
ffmpeg_builder.set_video_duration(temp_video_duration),
ffmpeg_builder.force_output(output_path)
)
return run_ffmpeg(commands).returncode == 0
def replace_audio(target_path : str, audio_path : str, output_path : str) -> bool:
output_audio_encoder = state_manager.get_item('output_audio_encoder')
output_audio_quality = state_manager.get_item('output_audio_quality')
output_audio_volume = state_manager.get_item('output_audio_volume')
temp_video_path = get_temp_file_path(target_path)
temp_video_format = cast(VideoFormat, get_file_format(temp_video_path))
temp_video_duration = detect_video_duration(temp_video_path)
output_audio_encoder = fix_audio_encoder(temp_video_format, output_audio_encoder)
commands = ffmpeg_builder.chain(
ffmpeg_builder.set_input(temp_video_path),
ffmpeg_builder.set_input(audio_path),
ffmpeg_builder.copy_video_encoder(),
ffmpeg_builder.set_audio_encoder(output_audio_encoder),
ffmpeg_builder.set_audio_quality(output_audio_encoder, output_audio_quality),
ffmpeg_builder.set_audio_volume(output_audio_volume),
ffmpeg_builder.set_video_duration(temp_video_duration),
ffmpeg_builder.force_output(output_path)
)
return run_ffmpeg(commands).returncode == 0
def merge_video(target_path : str, temp_video_fps : Fps, output_video_resolution : Resolution, output_video_fps : Fps, trim_frame_start : int, trim_frame_end : int) -> bool:
output_video_encoder = state_manager.get_item('output_video_encoder')
output_video_quality = state_manager.get_item('output_video_quality')
output_video_preset = state_manager.get_item('output_video_preset')
merge_frame_total = predict_video_frame_total(target_path, output_video_fps, trim_frame_start, trim_frame_end)
temp_video_path = get_temp_file_path(target_path)
temp_video_format = cast(VideoFormat, get_file_format(temp_video_path))
temp_frames_pattern = get_temp_frames_pattern(target_path, '%08d') temp_frames_pattern = get_temp_frames_pattern(target_path, '%08d')
commands = [ '-r', str(temp_video_fps), '-i', temp_frames_pattern, '-s', str(output_video_resolution), '-c:v', state_manager.get_item('output_video_encoder') ]
output_video_encoder = fix_video_encoder(temp_video_format, output_video_encoder) if state_manager.get_item('output_video_encoder') in [ 'libx264', 'libx265' ]:
commands = ffmpeg_builder.chain( output_video_compression = round(51 - (state_manager.get_item('output_video_quality') * 0.51))
ffmpeg_builder.set_input_fps(temp_video_fps), commands.extend([ '-crf', str(output_video_compression), '-preset', state_manager.get_item('output_video_preset') ])
ffmpeg_builder.set_input(temp_frames_pattern), if state_manager.get_item('output_video_encoder') in [ 'libvpx-vp9' ]:
ffmpeg_builder.set_media_resolution(pack_resolution(output_video_resolution)), output_video_compression = round(63 - (state_manager.get_item('output_video_quality') * 0.63))
ffmpeg_builder.set_video_encoder(output_video_encoder), commands.extend([ '-crf', str(output_video_compression) ])
ffmpeg_builder.set_video_quality(output_video_encoder, output_video_quality), if state_manager.get_item('output_video_encoder') in [ 'h264_nvenc', 'hevc_nvenc' ]:
ffmpeg_builder.set_video_preset(output_video_encoder, output_video_preset), output_video_compression = round(51 - (state_manager.get_item('output_video_quality') * 0.51))
ffmpeg_builder.concat( commands.extend([ '-cq', str(output_video_compression), '-preset', map_nvenc_preset(state_manager.get_item('output_video_preset')) ])
ffmpeg_builder.set_video_fps(output_video_fps), if state_manager.get_item('output_video_encoder') in [ 'h264_amf', 'hevc_amf' ]:
ffmpeg_builder.keep_video_alpha(output_video_encoder) output_video_compression = round(51 - (state_manager.get_item('output_video_quality') * 0.51))
), commands.extend([ '-qp_i', str(output_video_compression), '-qp_p', str(output_video_compression), '-quality', map_amf_preset(state_manager.get_item('output_video_preset')) ])
ffmpeg_builder.set_pixel_format(output_video_encoder), if state_manager.get_item('output_video_encoder') in [ 'h264_videotoolbox', 'hevc_videotoolbox' ]:
ffmpeg_builder.force_output(temp_video_path) commands.extend([ '-q:v', str(state_manager.get_item('output_video_quality')) ])
) commands.extend([ '-vf', 'framerate=fps=' + str(output_video_fps), '-pix_fmt', 'yuv420p', '-colorspace', 'bt709', '-y', temp_file_path ])
return run_ffmpeg(commands).returncode == 0
with tqdm(total = merge_frame_total, desc = translator.get('merging'), unit = 'frame', ascii = ' =', disable = state_manager.get_item('log_level') in [ 'warn', 'error' ]) as progress:
process = run_ffmpeg_with_progress(commands, partial(update_progress, progress))
return process.returncode == 0
def concat_video(output_path : str, temp_output_paths : List[str]) -> bool: def concat_video(output_path : str, temp_output_paths : List[str]) -> bool:
@@ -250,42 +97,80 @@ def concat_video(output_path : str, temp_output_paths : List[str]) -> bool:
concat_video_file.write('file \'' + os.path.abspath(temp_output_path) + '\'' + os.linesep) concat_video_file.write('file \'' + os.path.abspath(temp_output_path) + '\'' + os.linesep)
concat_video_file.flush() concat_video_file.flush()
concat_video_file.close() concat_video_file.close()
commands = [ '-f', 'concat', '-safe', '0', '-i', concat_video_file.name, '-c:v', 'copy', '-c:a', state_manager.get_item('output_audio_encoder'), '-y', os.path.abspath(output_path) ]
output_path = os.path.abspath(output_path)
commands = ffmpeg_builder.chain(
ffmpeg_builder.unsafe_concat(),
ffmpeg_builder.set_input(concat_video_file.name),
ffmpeg_builder.copy_video_encoder(),
ffmpeg_builder.copy_audio_encoder(),
ffmpeg_builder.force_output(output_path)
)
process = run_ffmpeg(commands) process = run_ffmpeg(commands)
process.communicate() process.communicate()
remove_file(concat_video_path) remove_file(concat_video_path)
return process.returncode == 0 return process.returncode == 0
def fix_audio_encoder(video_format : VideoFormat, audio_encoder : AudioEncoder) -> AudioEncoder: def copy_image(target_path : str, temp_image_resolution : str) -> bool:
if video_format == 'avi' and audio_encoder == 'libopus': temp_file_path = get_temp_file_path(target_path)
return 'aac' temp_image_compression = calc_image_compression(target_path, 100)
if video_format in [ 'm4v', 'mpeg', 'wmv' ]: commands = [ '-i', target_path, '-s', str(temp_image_resolution), '-q:v', str(temp_image_compression), '-y', temp_file_path ]
return 'aac' return run_ffmpeg(commands).returncode == 0
if video_format == 'mov' and audio_encoder in [ 'flac', 'libopus' ]:
return 'aac'
if video_format == 'mxf':
return 'pcm_s16le'
if video_format == 'webm':
return 'libopus'
return audio_encoder
def fix_video_encoder(video_format : VideoFormat, video_encoder : VideoEncoder) -> VideoEncoder: def finalize_image(target_path : str, output_path : str, output_image_resolution : str) -> bool:
if video_format in [ 'm4v', 'mpeg', 'mxf', 'wmv' ]: temp_file_path = get_temp_file_path(target_path)
return 'libx264' output_image_compression = calc_image_compression(target_path, state_manager.get_item('output_image_quality'))
if video_format in [ 'mkv', 'mp4' ] and video_encoder == 'rawvideo': commands = [ '-i', temp_file_path, '-s', str(output_image_resolution), '-q:v', str(output_image_compression), '-y', output_path ]
return 'libx264' return run_ffmpeg(commands).returncode == 0
if video_format == 'mov' and video_encoder == 'libvpx-vp9':
return 'libx264'
if video_format == 'webm': def calc_image_compression(image_path : str, image_quality : int) -> int:
return 'libvpx-vp9' is_webp = filetype.guess_mime(image_path) == 'image/webp'
return video_encoder if is_webp:
image_quality = 100 - image_quality
return round(31 - (image_quality * 0.31))
def read_audio_buffer(target_path : str, sample_rate : int, channel_total : int) -> Optional[AudioBuffer]:
commands = [ '-i', target_path, '-vn', '-f', 's16le', '-acodec', 'pcm_s16le', '-ar', str(sample_rate), '-ac', str(channel_total), '-' ]
process = open_ffmpeg(commands)
audio_buffer, _ = process.communicate()
if process.returncode == 0:
return audio_buffer
return None
def restore_audio(target_path : str, output_path : str, output_video_fps : Fps) -> bool:
trim_frame_start = state_manager.get_item('trim_frame_start')
trim_frame_end = state_manager.get_item('trim_frame_end')
temp_file_path = get_temp_file_path(target_path)
commands = [ '-i', temp_file_path ]
if isinstance(trim_frame_start, int):
start_time = trim_frame_start / output_video_fps
commands.extend([ '-ss', str(start_time) ])
if isinstance(trim_frame_end, int):
end_time = trim_frame_end / output_video_fps
commands.extend([ '-to', str(end_time) ])
commands.extend([ '-i', target_path, '-c:v', 'copy', '-c:a', state_manager.get_item('output_audio_encoder'), '-map', '0:v:0', '-map', '1:a:0', '-shortest', '-y', output_path ])
return run_ffmpeg(commands).returncode == 0
def replace_audio(target_path : str, audio_path : str, output_path : str) -> bool:
temp_file_path = get_temp_file_path(target_path)
commands = [ '-i', temp_file_path, '-i', audio_path, '-c:a', state_manager.get_item('output_audio_encoder'), '-af', 'apad', '-shortest', '-y', output_path ]
return run_ffmpeg(commands).returncode == 0
def map_nvenc_preset(output_video_preset : OutputVideoPreset) -> Optional[str]:
if output_video_preset in [ 'ultrafast', 'superfast', 'veryfast', 'faster', 'fast' ]:
return 'fast'
if output_video_preset == 'medium':
return 'medium'
if output_video_preset in [ 'slow', 'slower', 'veryslow' ]:
return 'slow'
return None
def map_amf_preset(output_video_preset : OutputVideoPreset) -> Optional[str]:
if output_video_preset in [ 'ultrafast', 'superfast', 'veryfast' ]:
return 'speed'
if output_video_preset in [ 'faster', 'fast', 'medium' ]:
return 'balanced'
if output_video_preset in [ 'slow', 'slower', 'veryslow' ]:
return 'quality'
return None

View File

@@ -1,267 +0,0 @@
import itertools
import shutil
from typing import List, Optional
import numpy
from facefusion.filesystem import get_file_format
from facefusion.types import AudioEncoder, Command, CommandSet, Duration, Fps, StreamMode, VideoEncoder, VideoPreset
def run(commands : List[Command]) -> List[Command]:
return [ shutil.which('ffmpeg'), '-loglevel', 'error' ] + commands
def chain(*commands : List[Command]) -> List[Command]:
return list(itertools.chain(*commands))
def concat(*__commands__ : List[Command]) -> List[Command]:
commands = []
command_set : CommandSet = {}
for command in __commands__:
for argument, value in zip(command[::2], command[1::2]):
command_set.setdefault(argument, []).append(value)
for argument, values in command_set.items():
commands.append(argument)
commands.append(','.join(values))
return commands
def get_encoders() -> List[Command]:
return [ '-encoders' ]
def set_hardware_accelerator(value : str) -> List[Command]:
return [ '-hwaccel', value ]
def set_progress() -> List[Command]:
return [ '-progress' ]
def set_input(input_path : str) -> List[Command]:
return [ '-i', input_path ]
def set_input_fps(input_fps : Fps) -> List[Command]:
return [ '-r', str(input_fps)]
def set_output(output_path : str) -> List[Command]:
return [ output_path ]
def force_output(output_path : str) -> List[Command]:
return [ '-y', output_path ]
def cast_stream() -> List[Command]:
return [ '-' ]
def set_stream_mode(stream_mode : StreamMode) -> List[Command]:
if stream_mode == 'udp':
return [ '-f', 'mpegts' ]
if stream_mode == 'v4l2':
return [ '-f', 'v4l2' ]
return []
def set_stream_quality(stream_quality : int) -> List[Command]:
return [ '-b:v', str(stream_quality) + 'k' ]
def unsafe_concat() -> List[Command]:
return [ '-f', 'concat', '-safe', '0' ]
def set_pixel_format(video_encoder : VideoEncoder) -> List[Command]:
if video_encoder == 'rawvideo':
return [ '-pix_fmt', 'rgb24' ]
if video_encoder == 'libvpx-vp9':
return [ '-pix_fmt', 'yuva420p' ]
return [ '-pix_fmt', 'yuv420p' ]
def set_frame_quality(frame_quality : int) -> List[Command]:
return [ '-q:v', str(frame_quality) ]
def select_frame_range(frame_start : int, frame_end : int, video_fps : Fps) -> List[Command]:
if isinstance(frame_start, int) and isinstance(frame_end, int):
return [ '-vf', 'trim=start_frame=' + str(frame_start) + ':end_frame=' + str(frame_end) + ',fps=' + str(video_fps) ]
if isinstance(frame_start, int):
return [ '-vf', 'trim=start_frame=' + str(frame_start) + ',fps=' + str(video_fps) ]
if isinstance(frame_end, int):
return [ '-vf', 'trim=end_frame=' + str(frame_end) + ',fps=' + str(video_fps) ]
return [ '-vf', 'fps=' + str(video_fps) ]
def prevent_frame_drop() -> List[Command]:
return [ '-vsync', '0' ]
def select_media_range(frame_start : int, frame_end : int, media_fps : Fps) -> List[Command]:
commands = []
if isinstance(frame_start, int):
commands.extend([ '-ss', str(frame_start / media_fps) ])
if isinstance(frame_end, int):
commands.extend([ '-to', str(frame_end / media_fps) ])
return commands
def select_media_stream(media_stream : str) -> List[Command]:
return [ '-map', media_stream ]
def set_media_resolution(video_resolution : str) -> List[Command]:
return [ '-s', video_resolution ]
def set_image_quality(image_path : str, image_quality : int) -> List[Command]:
if get_file_format(image_path) == 'webp':
return [ '-q:v', str(image_quality) ]
image_compression = round(31 - (image_quality * 0.31))
return [ '-q:v', str(image_compression) ]
def set_audio_encoder(audio_codec : str) -> List[Command]:
return [ '-c:a', audio_codec ]
def copy_audio_encoder() -> List[Command]:
return set_audio_encoder('copy')
def set_audio_sample_rate(audio_sample_rate : int) -> List[Command]:
return [ '-ar', str(audio_sample_rate) ]
def set_audio_sample_size(audio_sample_size : int) -> List[Command]:
if audio_sample_size == 16:
return [ '-f', 's16le' ]
if audio_sample_size == 32:
return [ '-f', 's32le' ]
return []
def set_audio_channel_total(audio_channel_total : int) -> List[Command]:
return [ '-ac', str(audio_channel_total) ]
def set_audio_quality(audio_encoder : AudioEncoder, audio_quality : int) -> List[Command]:
if audio_encoder == 'aac':
audio_compression = numpy.round(numpy.interp(audio_quality, [ 0, 100 ], [ 0.1, 2.0 ]), 1).astype(float).item()
return [ '-q:a', str(audio_compression) ]
if audio_encoder == 'libmp3lame':
audio_compression = numpy.round(numpy.interp(audio_quality, [ 0, 100 ], [ 9, 0 ])).astype(int).item()
return [ '-q:a', str(audio_compression) ]
if audio_encoder == 'libopus':
audio_bit_rate = numpy.round(numpy.interp(audio_quality, [ 0, 100 ], [ 64, 256 ])).astype(int).item()
return [ '-b:a', str(audio_bit_rate) + 'k' ]
if audio_encoder == 'libvorbis':
audio_compression = numpy.round(numpy.interp(audio_quality, [ 0, 100 ], [ -1, 10 ]), 1).astype(float).item()
return [ '-q:a', str(audio_compression) ]
return []
def set_audio_volume(audio_volume : int) -> List[Command]:
return [ '-filter:a', 'volume=' + str(audio_volume / 100) ]
def set_video_encoder(video_encoder : str) -> List[Command]:
return [ '-c:v', video_encoder ]
def copy_video_encoder() -> List[Command]:
return set_video_encoder('copy')
def set_video_quality(video_encoder : VideoEncoder, video_quality : int) -> List[Command]:
if video_encoder in [ 'libx264', 'libx264rgb', 'libx265' ]:
video_compression = numpy.round(numpy.interp(video_quality, [ 0, 100 ], [ 51, 0 ])).astype(int).item()
return [ '-crf', str(video_compression) ]
if video_encoder == 'libvpx-vp9':
video_compression = numpy.round(numpy.interp(video_quality, [ 0, 100 ], [ 63, 0 ])).astype(int).item()
return [ '-crf', str(video_compression) ]
if video_encoder in [ 'h264_nvenc', 'hevc_nvenc' ]:
video_compression = numpy.round(numpy.interp(video_quality, [ 0, 100 ], [ 51, 0 ])).astype(int).item()
return [ '-cq', str(video_compression) ]
if video_encoder in [ 'h264_amf', 'hevc_amf' ]:
video_compression = numpy.round(numpy.interp(video_quality, [ 0, 100 ], [ 51, 0 ])).astype(int).item()
return [ '-qp_i', str(video_compression), '-qp_p', str(video_compression), '-qp_b', str(video_compression) ]
if video_encoder in [ 'h264_qsv', 'hevc_qsv' ]:
video_compression = numpy.round(numpy.interp(video_quality, [ 0, 100 ], [ 51, 0 ])).astype(int).item()
return [ '-qp', str(video_compression) ]
if video_encoder in [ 'h264_videotoolbox', 'hevc_videotoolbox' ]:
video_bit_rate = numpy.round(numpy.interp(video_quality, [ 0, 100 ], [ 1024, 50512 ])).astype(int).item()
return [ '-b:v', str(video_bit_rate) + 'k' ]
return []
def set_video_preset(video_encoder : VideoEncoder, video_preset : VideoPreset) -> List[Command]:
if video_encoder in [ 'libx264', 'libx264rgb', 'libx265' ]:
return [ '-preset', video_preset ]
if video_encoder in [ 'h264_nvenc', 'hevc_nvenc' ]:
return [ '-preset', map_nvenc_preset(video_preset) ]
if video_encoder in [ 'h264_amf', 'hevc_amf' ]:
return [ '-quality', map_amf_preset(video_preset) ]
if video_encoder in [ 'h264_qsv', 'hevc_qsv' ]:
return [ '-preset', map_qsv_preset(video_preset) ]
return []
def set_video_fps(video_fps : Fps) -> List[Command]:
return [ '-vf', 'fps=' + str(video_fps) ]
def set_video_duration(video_duration : Duration) -> List[Command]:
return [ '-t', str(video_duration) ]
def keep_video_alpha(video_encoder : VideoEncoder) -> List[Command]:
if video_encoder == 'libvpx-vp9':
return [ '-vf', 'format=yuva420p' ]
return []
def capture_video() -> List[Command]:
return [ '-f', 'rawvideo', '-pix_fmt', 'rgb24' ]
def ignore_video_stream() -> List[Command]:
return [ '-vn' ]
def map_nvenc_preset(video_preset : VideoPreset) -> Optional[str]:
if video_preset in [ 'ultrafast', 'superfast', 'veryfast', 'faster', 'fast' ]:
return 'fast'
if video_preset == 'medium':
return 'medium'
if video_preset in [ 'slow', 'slower', 'veryslow' ]:
return 'slow'
return None
def map_amf_preset(video_preset : VideoPreset) -> Optional[str]:
if video_preset in [ 'ultrafast', 'superfast', 'veryfast' ]:
return 'speed'
if video_preset in [ 'faster', 'fast', 'medium' ]:
return 'balanced'
if video_preset in [ 'slow', 'slower', 'veryslow' ]:
return 'quality'
return None
def map_qsv_preset(video_preset : VideoPreset) -> Optional[str]:
if video_preset in [ 'ultrafast', 'superfast', 'veryfast' ]:
return 'veryfast'
if video_preset in [ 'faster', 'fast', 'medium', 'slow', 'slower', 'veryslow' ]:
return video_preset
return None

View File

@@ -1,9 +1,14 @@
import glob
import os import os
import shutil import shutil
from pathlib import Path
from typing import List, Optional from typing import List, Optional
import facefusion.choices import filetype
from facefusion.common_helper import is_windows
if is_windows():
import ctypes
def get_file_size(file_path : str) -> int: def get_file_size(file_path : str) -> int:
@@ -12,97 +17,54 @@ def get_file_size(file_path : str) -> int:
return 0 return 0
def get_file_name(file_path : str) -> Optional[str]: def same_file_extension(file_paths : List[str]) -> bool:
file_name, _ = os.path.splitext(os.path.basename(file_path)) file_extensions : List[str] = []
if file_name: for file_path in file_paths:
return file_name _, file_extension = os.path.splitext(file_path.lower())
return None
if file_extensions and file_extension not in file_extensions:
def get_file_extension(file_path : str) -> Optional[str]:
_, file_extension = os.path.splitext(file_path)
if file_extension:
return file_extension.lower()
return None
def get_file_format(file_path : str) -> Optional[str]:
file_extension = get_file_extension(file_path)
if file_extension:
if file_extension == '.jpg':
return 'jpeg'
if file_extension == '.tif':
return 'tiff'
if file_extension == '.mpg':
return 'mpeg'
return file_extension.lstrip('.')
return None
def same_file_extension(first_file_path : str, second_file_path : str) -> bool:
first_file_extension = get_file_extension(first_file_path)
second_file_extension = get_file_extension(second_file_path)
if first_file_extension and second_file_extension:
return get_file_extension(first_file_path) == get_file_extension(second_file_path)
return False return False
file_extensions.append(file_extension)
return True
def is_file(file_path : str) -> bool: def is_file(file_path : str) -> bool:
if file_path: return bool(file_path and os.path.isfile(file_path))
return os.path.isfile(file_path)
def is_directory(directory_path : str) -> bool:
return bool(directory_path and os.path.isdir(directory_path))
def in_directory(file_path : str) -> bool:
if file_path and not is_directory(file_path):
return is_directory(os.path.dirname(file_path))
return False return False
def is_audio(audio_path : str) -> bool: def is_audio(audio_path : str) -> bool:
return is_file(audio_path) and get_file_format(audio_path) in facefusion.choices.audio_formats return is_file(audio_path) and filetype.helpers.is_audio(audio_path)
def has_audio(audio_paths : List[str]) -> bool: def has_audio(audio_paths : List[str]) -> bool:
if audio_paths: if audio_paths:
return any(map(is_audio, audio_paths)) return any(is_audio(audio_path) for audio_path in audio_paths)
return False
def are_audios(audio_paths : List[str]) -> bool:
if audio_paths:
return all(map(is_audio, audio_paths))
return False return False
def is_image(image_path : str) -> bool: def is_image(image_path : str) -> bool:
return is_file(image_path) and get_file_format(image_path) in facefusion.choices.image_formats return is_file(image_path) and filetype.helpers.is_image(image_path)
def has_image(image_paths : List[str]) -> bool: def has_image(image_paths: List[str]) -> bool:
if image_paths: if image_paths:
return any(is_image(image_path) for image_path in image_paths) return any(is_image(image_path) for image_path in image_paths)
return False return False
def are_images(image_paths : List[str]) -> bool:
if image_paths:
return all(map(is_image, image_paths))
return False
def is_video(video_path : str) -> bool: def is_video(video_path : str) -> bool:
return is_file(video_path) and get_file_format(video_path) in facefusion.choices.video_formats return is_file(video_path) and filetype.helpers.is_video(video_path)
def has_video(video_paths : List[str]) -> bool:
if video_paths:
return any(map(is_video, video_paths))
return False
def are_videos(video_paths : List[str]) -> bool:
if video_paths:
return all(map(is_video, video_paths))
return False
def filter_audio_paths(paths : List[str]) -> List[str]: def filter_audio_paths(paths : List[str]) -> List[str]:
@@ -117,6 +79,24 @@ def filter_image_paths(paths : List[str]) -> List[str]:
return [] return []
def resolve_relative_path(path : str) -> str:
return os.path.abspath(os.path.join(os.path.dirname(__file__), path))
def sanitize_path_for_windows(full_path : str) -> Optional[str]:
buffer_size = 0
while True:
unicode_buffer = ctypes.create_unicode_buffer(buffer_size)
buffer_limit = ctypes.windll.kernel32.GetShortPathNameW(full_path, unicode_buffer, buffer_size) #type:ignore[attr-defined]
if buffer_size > buffer_limit:
return unicode_buffer.value
if buffer_limit == 0:
return None
buffer_size = buffer_limit
def copy_file(file_path : str, move_path : str) -> bool: def copy_file(file_path : str, move_path : str) -> bool:
if is_file(file_path): if is_file(file_path):
shutil.copy(file_path, move_path) shutil.copy(file_path, move_path)
@@ -138,53 +118,23 @@ def remove_file(file_path : str) -> bool:
return False return False
def resolve_file_paths(directory_path : str) -> List[str]:
file_paths : List[str] = []
if is_directory(directory_path):
file_names_and_extensions = sorted(os.listdir(directory_path))
for file_name_and_extension in file_names_and_extensions:
if not file_name_and_extension.startswith(('.', '__')):
file_path = os.path.join(directory_path, file_name_and_extension)
file_paths.append(file_path)
return file_paths
def resolve_file_pattern(file_pattern : str) -> List[str]:
if in_directory(file_pattern):
return sorted(glob.glob(file_pattern))
return []
def is_directory(directory_path : str) -> bool:
if directory_path:
return os.path.isdir(directory_path)
return False
def in_directory(file_path : str) -> bool:
if file_path:
directory_path = os.path.dirname(file_path)
if directory_path:
return not is_directory(file_path) and is_directory(directory_path)
return False
def create_directory(directory_path : str) -> bool: def create_directory(directory_path : str) -> bool:
if directory_path and not is_file(directory_path): if directory_path and not is_file(directory_path):
os.makedirs(directory_path, exist_ok = True) Path(directory_path).mkdir(parents = True, exist_ok = True)
return is_directory(directory_path) return is_directory(directory_path)
return False return False
def list_directory(directory_path : str) -> Optional[List[str]]:
if is_directory(directory_path):
files = os.listdir(directory_path)
files = [ Path(file).stem for file in files if not Path(file).stem.startswith(('.', '__')) ]
return sorted(files)
return None
def remove_directory(directory_path : str) -> bool: def remove_directory(directory_path : str) -> bool:
if is_directory(directory_path): if is_directory(directory_path):
shutil.rmtree(directory_path, ignore_errors = True) shutil.rmtree(directory_path, ignore_errors = True)
return not is_directory(directory_path) return not is_directory(directory_path)
return False return False
def resolve_relative_path(path : str) -> str:
return os.path.abspath(os.path.join(os.path.dirname(__file__), path))

View File

@@ -2,7 +2,7 @@ import os
import zlib import zlib
from typing import Optional from typing import Optional
from facefusion.filesystem import get_file_name, is_file from facefusion.filesystem import is_file
def create_hash(content : bytes) -> str: def create_hash(content : bytes) -> str:
@@ -13,8 +13,8 @@ def validate_hash(validate_path : str) -> bool:
hash_path = get_hash_path(validate_path) hash_path = get_hash_path(validate_path)
if is_file(hash_path): if is_file(hash_path):
with open(hash_path) as hash_file: with open(hash_path, 'r') as hash_file:
hash_content = hash_file.read() hash_content = hash_file.read().strip()
with open(validate_path, 'rb') as validate_file: with open(validate_path, 'rb') as validate_file:
validate_content = validate_file.read() validate_content = validate_file.read()
@@ -25,8 +25,8 @@ def validate_hash(validate_path : str) -> bool:
def get_hash_path(validate_path : str) -> Optional[str]: def get_hash_path(validate_path : str) -> Optional[str]:
if is_file(validate_path): if is_file(validate_path):
validate_directory_path, file_name_and_extension = os.path.split(validate_path) validate_directory_path, _ = os.path.split(validate_path)
validate_file_name = get_file_name(file_name_and_extension) validate_file_name, _ = os.path.splitext(_)
return os.path.join(validate_directory_path, validate_file_name + '.hash') return os.path.join(validate_directory_path, validate_file_name + '.hash')
return None return None

View File

@@ -1,95 +1,79 @@
import importlib from functools import lru_cache
import random from time import sleep
from time import sleep, time
from typing import List from typing import List
import onnx
from onnxruntime import InferenceSession from onnxruntime import InferenceSession
from facefusion import logger, process_manager, state_manager, translator from facefusion import process_manager, state_manager
from facefusion.app_context import detect_app_context from facefusion.app_context import detect_app_context
from facefusion.common_helper import is_windows from facefusion.execution import create_execution_providers, has_execution_provider
from facefusion.execution import create_inference_session_providers, has_execution_provider from facefusion.thread_helper import thread_lock
from facefusion.exit_helper import fatal_exit from facefusion.typing import DownloadSet, ExecutionProviderKey, InferencePool, InferencePoolSet, ModelInitializer
from facefusion.filesystem import get_file_name, is_file
from facefusion.time_helper import calculate_end_time
from facefusion.types import DownloadSet, ExecutionProvider, InferencePool, InferencePoolSet
INFERENCE_POOL_SET : InferencePoolSet =\ INFERENCE_POOLS : InferencePoolSet =\
{ {
'cli': {}, 'cli': {}, # type:ignore[typeddict-item]
'ui': {} 'ui': {} # type:ignore[typeddict-item]
} }
def get_inference_pool(module_name : str, model_names : List[str], model_source_set : DownloadSet) -> InferencePool: def get_inference_pool(model_context : str, model_sources : DownloadSet) -> InferencePool:
global INFERENCE_POOLS
with thread_lock():
while process_manager.is_checking(): while process_manager.is_checking():
sleep(0.5) sleep(0.5)
execution_device_ids = state_manager.get_item('execution_device_ids')
execution_providers = resolve_execution_providers(module_name)
app_context = detect_app_context() app_context = detect_app_context()
inference_context = get_inference_context(model_context)
for execution_device_id in execution_device_ids: if app_context == 'cli' and INFERENCE_POOLS.get('ui').get(inference_context):
inference_context = get_inference_context(module_name, model_names, execution_device_id, execution_providers) INFERENCE_POOLS['cli'][inference_context] = INFERENCE_POOLS.get('ui').get(inference_context)
if app_context == 'ui' and INFERENCE_POOLS.get('cli').get(inference_context):
INFERENCE_POOLS['ui'][inference_context] = INFERENCE_POOLS.get('cli').get(inference_context)
if not INFERENCE_POOLS.get(app_context).get(inference_context):
execution_provider_keys = resolve_execution_provider_keys(model_context)
INFERENCE_POOLS[app_context][inference_context] = create_inference_pool(model_sources, state_manager.get_item('execution_device_id'), execution_provider_keys)
if app_context == 'cli' and INFERENCE_POOL_SET.get('ui').get(inference_context): return INFERENCE_POOLS.get(app_context).get(inference_context)
INFERENCE_POOL_SET['cli'][inference_context] = INFERENCE_POOL_SET.get('ui').get(inference_context)
if app_context == 'ui' and INFERENCE_POOL_SET.get('cli').get(inference_context):
INFERENCE_POOL_SET['ui'][inference_context] = INFERENCE_POOL_SET.get('cli').get(inference_context)
if not INFERENCE_POOL_SET.get(app_context).get(inference_context):
INFERENCE_POOL_SET[app_context][inference_context] = create_inference_pool(model_source_set, execution_device_id, execution_providers)
current_inference_context = get_inference_context(module_name, model_names, random.choice(execution_device_ids), execution_providers)
return INFERENCE_POOL_SET.get(app_context).get(current_inference_context)
def create_inference_pool(model_source_set : DownloadSet, execution_device_id : int, execution_providers : List[ExecutionProvider]) -> InferencePool: def create_inference_pool(model_sources : DownloadSet, execution_device_id : str, execution_provider_keys : List[ExecutionProviderKey]) -> InferencePool:
inference_pool : InferencePool = {} inference_pool : InferencePool = {}
for model_name in model_source_set.keys(): for model_name in model_sources.keys():
model_path = model_source_set.get(model_name).get('path') inference_pool[model_name] = create_inference_session(model_sources.get(model_name).get('path'), execution_device_id, execution_provider_keys)
if is_file(model_path):
inference_pool[model_name] = create_inference_session(model_path, execution_device_id, execution_providers)
return inference_pool return inference_pool
def clear_inference_pool(module_name : str, model_names : List[str]) -> None: def clear_inference_pool(model_context : str) -> None:
execution_device_ids = state_manager.get_item('execution_device_ids') global INFERENCE_POOLS
execution_providers = resolve_execution_providers(module_name)
app_context = detect_app_context() app_context = detect_app_context()
inference_context = get_inference_context(model_context)
if is_windows() and has_execution_provider('directml'): if INFERENCE_POOLS.get(app_context).get(inference_context):
INFERENCE_POOL_SET[app_context].clear() del INFERENCE_POOLS[app_context][inference_context]
for execution_device_id in execution_device_ids:
inference_context = get_inference_context(module_name, model_names, execution_device_id, execution_providers)
if INFERENCE_POOL_SET.get(app_context).get(inference_context):
del INFERENCE_POOL_SET[app_context][inference_context]
def create_inference_session(model_path : str, execution_device_id : int, execution_providers : List[ExecutionProvider]) -> InferenceSession: def create_inference_session(model_path : str, execution_device_id : str, execution_provider_keys : List[ExecutionProviderKey]) -> InferenceSession:
model_file_name = get_file_name(model_path) execution_providers = create_execution_providers(execution_device_id, execution_provider_keys)
start_time = time() return InferenceSession(model_path, providers = execution_providers)
try:
inference_session_providers = create_inference_session_providers(execution_device_id, execution_providers)
inference_session = InferenceSession(model_path, providers = inference_session_providers)
logger.debug(translator.get('loading_model_succeeded').format(model_name = model_file_name, seconds = calculate_end_time(start_time)), __name__)
return inference_session
except Exception:
logger.error(translator.get('loading_model_failed').format(model_name = model_file_name), __name__)
fatal_exit(1)
def get_inference_context(module_name : str, model_names : List[str], execution_device_id : int, execution_providers : List[ExecutionProvider]) -> str: @lru_cache(maxsize = None)
inference_context = '.'.join([ module_name ] + model_names + [ str(execution_device_id) ] + list(execution_providers)) def get_static_model_initializer(model_path : str) -> ModelInitializer:
return inference_context model = onnx.load(model_path)
return onnx.numpy_helper.to_array(model.graph.initializer[-1])
def resolve_execution_providers(module_name : str) -> List[ExecutionProvider]: def resolve_execution_provider_keys(model_context : str) -> List[ExecutionProviderKey]:
module = importlib.import_module(module_name) if has_execution_provider('coreml') and (model_context.startswith('facefusion.processors.modules.age_modifier') or model_context.startswith('facefusion.processors.modules.frame_colorizer')):
return [ 'cpu' ]
if hasattr(module, 'resolve_execution_providers'):
return getattr(module, 'resolve_execution_providers')()
return state_manager.get_item('execution_providers') return state_manager.get_item('execution_providers')
def get_inference_context(model_context : str) -> str:
execution_provider_keys = resolve_execution_provider_keys(model_context)
inference_context = model_context + '.' + '_'.join(execution_provider_keys)
return inference_context

View File

@@ -3,69 +3,60 @@ import shutil
import signal import signal
import subprocess import subprocess
import sys import sys
import tempfile
from argparse import ArgumentParser, HelpFormatter from argparse import ArgumentParser, HelpFormatter
from functools import partial from typing import Dict, Tuple
from types import FrameType
from facefusion import metadata from facefusion import metadata, wording
from facefusion.common_helper import is_linux, is_windows from facefusion.common_helper import is_linux, is_macos, is_windows
LOCALS =\ ONNXRUNTIMES : Dict[str, Tuple[str, str]] = {}
{
'install_dependency': 'install the {dependency} package', if is_macos():
'skip_conda': 'skip the conda environment check', ONNXRUNTIMES['default'] = ('onnxruntime', '1.19.2')
'conda_not_activated': 'conda is not activated' else:
} ONNXRUNTIMES['default'] = ('onnxruntime', '1.19.2')
ONNXRUNTIME_SET =\ ONNXRUNTIMES['cuda'] = ('onnxruntime-gpu', '1.19.2')
{ ONNXRUNTIMES['openvino'] = ('onnxruntime-openvino', '1.19.0')
'default': ('onnxruntime', '1.23.2')
}
if is_windows() or is_linux():
ONNXRUNTIME_SET['cuda'] = ('onnxruntime-gpu', '1.23.2')
ONNXRUNTIME_SET['openvino'] = ('onnxruntime-openvino', '1.23.0')
if is_windows():
ONNXRUNTIME_SET['directml'] = ('onnxruntime-directml', '1.23.0')
if is_linux(): if is_linux():
ONNXRUNTIME_SET['rocm'] = ('onnxruntime-rocm', '1.21.0') ONNXRUNTIMES['rocm'] = ('onnxruntime-rocm', '1.18.0')
if is_windows():
ONNXRUNTIMES['directml'] = ('onnxruntime-directml', '1.17.3')
def cli() -> None: def cli() -> None:
signal.signal(signal.SIGINT, signal_exit) signal.signal(signal.SIGINT, lambda signal_number, frame: sys.exit(0))
program = ArgumentParser(formatter_class = partial(HelpFormatter, max_help_position = 50)) program = ArgumentParser(formatter_class = lambda prog: HelpFormatter(prog, max_help_position = 50))
program.add_argument('--onnxruntime', help = LOCALS.get('install_dependency').format(dependency = 'onnxruntime'), choices = ONNXRUNTIME_SET.keys(), required = True) program.add_argument('--onnxruntime', help = wording.get('help.install_dependency').format(dependency = 'onnxruntime'), choices = ONNXRUNTIMES.keys(), required = True)
program.add_argument('--skip-conda', help = LOCALS.get('skip_conda'), action = 'store_true') program.add_argument('--skip-conda', help = wording.get('help.skip_conda'), action = 'store_true')
program.add_argument('-v', '--version', version = metadata.get('name') + ' ' + metadata.get('version'), action = 'version') program.add_argument('-v', '--version', version = metadata.get('name') + ' ' + metadata.get('version'), action = 'version')
run(program) run(program)
def signal_exit(signum : int, frame : FrameType) -> None:
sys.exit(0)
def run(program : ArgumentParser) -> None: def run(program : ArgumentParser) -> None:
args = program.parse_args() args = program.parse_args()
has_conda = 'CONDA_PREFIX' in os.environ has_conda = 'CONDA_PREFIX' in os.environ
onnxruntime_name, onnxruntime_version = ONNXRUNTIME_SET.get(args.onnxruntime) onnxruntime_name, onnxruntime_version = ONNXRUNTIMES.get(args.onnxruntime)
if not args.skip_conda and not has_conda: if not args.skip_conda and not has_conda:
sys.stdout.write(LOCALS.get('conda_not_activated') + os.linesep) sys.stdout.write(wording.get('conda_not_activated') + os.linesep)
sys.exit(1) sys.exit(1)
with open('requirements.txt') as file: subprocess.call([ shutil.which('pip'), 'install', '-r', 'requirements.txt', '--force-reinstall' ])
for line in file.readlines():
__line__ = line.strip()
if not __line__.startswith('onnxruntime'):
subprocess.call([ shutil.which('pip'), 'install', line, '--force-reinstall' ])
if args.onnxruntime == 'rocm': if args.onnxruntime == 'rocm':
python_id = 'cp' + str(sys.version_info.major) + str(sys.version_info.minor) python_id = 'cp' + str(sys.version_info.major) + str(sys.version_info.minor)
if python_id in [ 'cp310', 'cp312' ]: if python_id == 'cp310':
wheel_name = 'onnxruntime_rocm-' + onnxruntime_version + '-' + python_id + '-' + python_id + '-linux_x86_64.whl' wheel_name = 'onnxruntime_rocm-' + onnxruntime_version +'-' + python_id + '-' + python_id + '-linux_x86_64.whl'
wheel_url = 'https://repo.radeon.com/rocm/manylinux/rocm-rel-6.4/' + wheel_name wheel_path = os.path.join(tempfile.gettempdir(), wheel_name)
subprocess.call([ shutil.which('pip'), 'install', wheel_url, '--force-reinstall' ]) wheel_url = 'https://repo.radeon.com/rocm/manylinux/rocm-rel-6.2/' + wheel_name
subprocess.call([ shutil.which('curl'), '--silent', '--location', '--continue-at', '-', '--output', wheel_path, wheel_url ])
subprocess.call([ shutil.which('pip'), 'uninstall', 'onnxruntime', wheel_path, '-y', '-q' ])
subprocess.call([ shutil.which('pip'), 'install', wheel_path, '--force-reinstall' ])
os.remove(wheel_path)
else: else:
subprocess.call([ shutil.which('pip'), 'uninstall', 'onnxruntime', onnxruntime_name, '-y', '-q' ])
subprocess.call([ shutil.which('pip'), 'install', onnxruntime_name + '==' + onnxruntime_version, '--force-reinstall' ]) subprocess.call([ shutil.which('pip'), 'install', onnxruntime_name + '==' + onnxruntime_version, '--force-reinstall' ])
if args.onnxruntime == 'cuda' and has_conda: if args.onnxruntime == 'cuda' and has_conda:
@@ -81,7 +72,7 @@ def run(program : ArgumentParser) -> None:
os.path.join(os.getenv('CONDA_PREFIX'), 'lib'), os.path.join(os.getenv('CONDA_PREFIX'), 'lib'),
os.path.join(os.getenv('CONDA_PREFIX'), 'lib', python_id, 'site-packages', 'tensorrt_libs') os.path.join(os.getenv('CONDA_PREFIX'), 'lib', python_id, 'site-packages', 'tensorrt_libs')
]) ])
library_paths = list(dict.fromkeys([ library_path for library_path in library_paths if os.path.exists(library_path) ])) library_paths = [ library_path for library_path in library_paths if os.path.exists(library_path) ]
subprocess.call([ shutil.which('conda'), 'env', 'config', 'vars', 'set', 'LD_LIBRARY_PATH=' + os.pathsep.join(library_paths) ]) subprocess.call([ shutil.which('conda'), 'env', 'config', 'vars', 'set', 'LD_LIBRARY_PATH=' + os.pathsep.join(library_paths) ])
@@ -94,7 +85,10 @@ def run(program : ArgumentParser) -> None:
os.path.join(os.getenv('CONDA_PREFIX'), 'Lib'), os.path.join(os.getenv('CONDA_PREFIX'), 'Lib'),
os.path.join(os.getenv('CONDA_PREFIX'), 'Lib', 'site-packages', 'tensorrt_libs') os.path.join(os.getenv('CONDA_PREFIX'), 'Lib', 'site-packages', 'tensorrt_libs')
]) ])
library_paths = list(dict.fromkeys([ library_path for library_path in library_paths if os.path.exists(library_path) ])) library_paths = [ library_path for library_path in library_paths if os.path.exists(library_path) ]
subprocess.call([ shutil.which('conda'), 'env', 'config', 'vars', 'set', 'PATH=' + os.pathsep.join(library_paths) ]) subprocess.call([ shutil.which('conda'), 'env', 'config', 'vars', 'set', 'PATH=' + os.pathsep.join(library_paths) ])
if onnxruntime_version < '1.19.0':
subprocess.call([ shutil.which('pip'), 'install', 'numpy==1.26.4', '--force-reinstall' ])
subprocess.call([ shutil.which('pip'), 'install', 'python-multipart==0.0.12', '--force-reinstall' ])

View File

@@ -2,16 +2,11 @@ import os
from datetime import datetime from datetime import datetime
from typing import Optional from typing import Optional
from facefusion.filesystem import get_file_extension, get_file_name
def get_step_output_path(job_id : str, step_index : int, output_path : str) -> Optional[str]: def get_step_output_path(job_id : str, step_index : int, output_path : str) -> Optional[str]:
if output_path: if output_path:
output_directory_path, output_file_path = os.path.split(output_path) output_directory_path, _ = os.path.split(output_path)
output_file_name = get_file_name(output_file_path) output_file_name, output_file_extension = os.path.splitext(_)
output_file_extension = get_file_extension(output_file_path)
if output_file_name and output_file_extension:
return os.path.join(output_directory_path, output_file_name + '-' + job_id + '-' + str(step_index) + output_file_extension) return os.path.join(output_directory_path, output_file_name + '-' + job_id + '-' + str(step_index) + output_file_extension)
return None return None

View File

@@ -1,15 +1,15 @@
from datetime import datetime from datetime import datetime
from typing import List, Optional, Tuple from typing import Optional, Tuple
from facefusion.date_helper import describe_time_ago
from facefusion.jobs import job_manager from facefusion.jobs import job_manager
from facefusion.time_helper import describe_time_ago from facefusion.typing import JobStatus, TableContents, TableHeaders
from facefusion.types import JobStatus, TableContent, TableHeader
def compose_job_list(job_status : JobStatus) -> Tuple[List[TableHeader], List[List[TableContent]]]: def compose_job_list(job_status : JobStatus) -> Tuple[TableHeaders, TableContents]:
jobs = job_manager.find_jobs(job_status) jobs = job_manager.find_jobs(job_status)
job_headers : List[TableHeader] = [ 'job id', 'steps', 'date created', 'date updated', 'job status' ] job_headers : TableHeaders = [ 'job id', 'steps', 'date created', 'date updated', 'job status' ]
job_contents : List[List[TableContent]] = [] job_contents : TableContents = []
for index, job_id in enumerate(jobs): for index, job_id in enumerate(jobs):
if job_manager.validate_job(job_id): if job_manager.validate_job(job_id):

View File

@@ -1,13 +1,15 @@
import glob
import os import os
from copy import copy from copy import copy
from typing import List, Optional from typing import List, Optional
import facefusion.choices from facefusion.choices import job_statuses
from facefusion.filesystem import create_directory, get_file_name, is_directory, is_file, move_file, remove_directory, remove_file, resolve_file_pattern from facefusion.date_helper import get_current_date_time
from facefusion.filesystem import create_directory, is_directory, is_file, move_file, remove_directory, remove_file
from facefusion.jobs.job_helper import get_step_output_path from facefusion.jobs.job_helper import get_step_output_path
from facefusion.json import read_json, write_json from facefusion.json import read_json, write_json
from facefusion.time_helper import get_current_date_time from facefusion.temp_helper import create_base_directory
from facefusion.types import Args, Job, JobSet, JobStatus, JobStep, JobStepStatus from facefusion.typing import Args, Job, JobSet, JobStatus, JobStep, JobStepStatus
JOBS_PATH : Optional[str] = None JOBS_PATH : Optional[str] = None
@@ -16,8 +18,9 @@ def init_jobs(jobs_path : str) -> bool:
global JOBS_PATH global JOBS_PATH
JOBS_PATH = jobs_path JOBS_PATH = jobs_path
job_status_paths = [ os.path.join(JOBS_PATH, job_status) for job_status in facefusion.choices.job_statuses ] job_status_paths = [ os.path.join(JOBS_PATH, job_status) for job_status in job_statuses ]
create_base_directory()
for job_status_path in job_status_paths: for job_status_path in job_status_paths:
create_directory(job_status_path) create_directory(job_status_path)
return all(is_directory(status_path) for status_path in job_status_paths) return all(is_directory(status_path) for status_path in job_status_paths)
@@ -48,17 +51,14 @@ def submit_job(job_id : str) -> bool:
return False return False
def submit_jobs(halt_on_error : bool) -> bool: def submit_jobs() -> bool:
drafted_job_ids = find_job_ids('drafted') drafted_job_ids = find_job_ids('drafted')
has_error = False
if drafted_job_ids: if drafted_job_ids:
for job_id in drafted_job_ids: for job_id in drafted_job_ids:
if not submit_job(job_id): if not submit_job(job_id):
has_error = True
if halt_on_error:
return False return False
return not has_error return True
return False return False
@@ -66,37 +66,34 @@ def delete_job(job_id : str) -> bool:
return delete_job_file(job_id) return delete_job_file(job_id)
def delete_jobs(halt_on_error : bool) -> bool: def delete_jobs() -> bool:
job_ids = find_job_ids('drafted') + find_job_ids('queued') + find_job_ids('failed') + find_job_ids('completed') job_ids = find_job_ids('drafted') + find_job_ids('queued') + find_job_ids('failed') + find_job_ids('completed')
has_error = False
if job_ids: if job_ids:
for job_id in job_ids: for job_id in job_ids:
if not delete_job(job_id): if not delete_job(job_id):
has_error = True
if halt_on_error:
return False return False
return not has_error return True
return False return False
def find_jobs(job_status : JobStatus) -> JobSet: def find_jobs(job_status : JobStatus) -> JobSet:
job_ids = find_job_ids(job_status) job_ids = find_job_ids(job_status)
job_set : JobSet = {} jobs : JobSet = {}
for job_id in job_ids: for job_id in job_ids:
job_set[job_id] = read_job_file(job_id) jobs[job_id] = read_job_file(job_id)
return job_set return jobs
def find_job_ids(job_status : JobStatus) -> List[str]: def find_job_ids(job_status : JobStatus) -> List[str]:
job_pattern = os.path.join(JOBS_PATH, job_status, '*.json') job_pattern = os.path.join(JOBS_PATH, job_status, '*.json')
job_paths = resolve_file_pattern(job_pattern) job_files = glob.glob(job_pattern)
job_paths.sort(key = os.path.getmtime) job_files.sort(key = os.path.getmtime)
job_ids = [] job_ids = []
for job_path in job_paths: for job_file in job_files:
job_id = get_file_name(job_path) job_id, _ = os.path.splitext(os.path.basename(job_file))
job_ids.append(job_id) job_ids.append(job_id)
return job_ids return job_ids
@@ -188,6 +185,7 @@ def set_step_status(job_id : str, step_index : int, step_status : JobStepStatus)
if job: if job:
steps = job.get('steps') steps = job.get('steps')
if has_step(job_id, step_index): if has_step(job_id, step_index):
steps[step_index]['status'] = step_status steps[step_index]['status'] = step_status
return update_job_file(job_id, job) return update_job_file(job_id, job)
@@ -250,9 +248,9 @@ def find_job_path(job_id : str) -> Optional[str]:
job_file_name = get_job_file_name(job_id) job_file_name = get_job_file_name(job_id)
if job_file_name: if job_file_name:
for job_status in facefusion.choices.job_statuses: for job_status in job_statuses:
job_pattern = os.path.join(JOBS_PATH, job_status, job_file_name) job_pattern = os.path.join(JOBS_PATH, job_status, job_file_name)
job_paths = resolve_file_pattern(job_pattern) job_paths = glob.glob(job_pattern)
for job_path in job_paths: for job_path in job_paths:
return job_path return job_path

View File

@@ -1,7 +1,7 @@
from facefusion.ffmpeg import concat_video from facefusion.ffmpeg import concat_video
from facefusion.filesystem import are_images, are_videos, move_file, remove_file from facefusion.filesystem import is_image, is_video, move_file, remove_file
from facefusion.jobs import job_helper, job_manager from facefusion.jobs import job_helper, job_manager
from facefusion.types import JobOutputSet, JobStep, ProcessStep from facefusion.typing import JobOutputSet, JobStep, ProcessStep
def run_job(job_id : str, process_step : ProcessStep) -> bool: def run_job(job_id : str, process_step : ProcessStep) -> bool:
@@ -16,17 +16,14 @@ def run_job(job_id : str, process_step : ProcessStep) -> bool:
return False return False
def run_jobs(process_step : ProcessStep, halt_on_error : bool) -> bool: def run_jobs(process_step : ProcessStep) -> bool:
queued_job_ids = job_manager.find_job_ids('queued') queued_job_ids = job_manager.find_job_ids('queued')
has_error = False
if queued_job_ids: if queued_job_ids:
for job_id in queued_job_ids: for job_id in queued_job_ids:
if not run_job(job_id, process_step): if not run_job(job_id, process_step):
has_error = True
if halt_on_error:
return False return False
return not has_error return True
return False return False
@@ -38,17 +35,14 @@ def retry_job(job_id : str, process_step : ProcessStep) -> bool:
return False return False
def retry_jobs(process_step : ProcessStep, halt_on_error : bool) -> bool: def retry_jobs(process_step : ProcessStep) -> bool:
failed_job_ids = job_manager.find_job_ids('failed') failed_job_ids = job_manager.find_job_ids('failed')
has_error = False
if failed_job_ids: if failed_job_ids:
for job_id in failed_job_ids: for job_id in failed_job_ids:
if not retry_job(job_id, process_step): if not retry_job(job_id, process_step):
has_error = True
if halt_on_error:
return False return False
return not has_error return True
return False return False
@@ -79,10 +73,10 @@ def finalize_steps(job_id : str) -> bool:
output_set = collect_output_set(job_id) output_set = collect_output_set(job_id)
for output_path, temp_output_paths in output_set.items(): for output_path, temp_output_paths in output_set.items():
if are_videos(temp_output_paths): if all(map(is_video, temp_output_paths)):
if not concat_video(output_path, temp_output_paths): if not concat_video(output_path, temp_output_paths):
return False return False
if are_images(temp_output_paths): if any(map(is_image, temp_output_paths)):
for temp_output_path in temp_output_paths: for temp_output_path in temp_output_paths:
if not move_file(temp_output_path, output_path): if not move_file(temp_output_path, output_path):
return False return False
@@ -101,12 +95,12 @@ def clean_steps(job_id: str) -> bool:
def collect_output_set(job_id : str) -> JobOutputSet: def collect_output_set(job_id : str) -> JobOutputSet:
steps = job_manager.get_steps(job_id) steps = job_manager.get_steps(job_id)
job_output_set : JobOutputSet = {} output_set : JobOutputSet = {}
for index, step in enumerate(steps): for index, step in enumerate(steps):
output_path = step.get('args').get('output_path') output_path = step.get('args').get('output_path')
if output_path: if output_path:
step_output_path = job_manager.get_step_output_path(job_id, index, output_path) step_output_path = job_manager.get_step_output_path(job_id, index, output_path)
job_output_set.setdefault(output_path, []).append(step_output_path) output_set.setdefault(output_path, []).append(step_output_path)
return job_output_set return output_set

View File

@@ -1,6 +1,6 @@
from typing import List from typing import List
from facefusion.types import JobStore from facefusion.typing import JobStore
JOB_STORE : JobStore =\ JOB_STORE : JobStore =\
{ {
@@ -17,11 +17,11 @@ def get_step_keys() -> List[str]:
return JOB_STORE.get('step_keys') return JOB_STORE.get('step_keys')
def register_job_keys(job_keys : List[str]) -> None: def register_job_keys(step_keys : List[str]) -> None:
for job_key in job_keys:
JOB_STORE['job_keys'].append(job_key)
def register_step_keys(step_keys : List[str]) -> None:
for step_key in step_keys: for step_key in step_keys:
JOB_STORE['step_keys'].append(step_key) JOB_STORE['job_keys'].append(step_key)
def register_step_keys(job_keys : List[str]) -> None:
for job_key in job_keys:
JOB_STORE['step_keys'].append(job_key)

View File

@@ -3,13 +3,13 @@ from json import JSONDecodeError
from typing import Optional from typing import Optional
from facefusion.filesystem import is_file from facefusion.filesystem import is_file
from facefusion.types import Content from facefusion.typing import Content
def read_json(json_path : str) -> Optional[Content]: def read_json(json_path : str) -> Optional[Content]:
if is_file(json_path): if is_file(json_path):
try: try:
with open(json_path) as json_file: with open(json_path, 'r') as json_file:
return json.load(json_file) return json.load(json_file)
except JSONDecodeError: except JSONDecodeError:
pass pass

View File

@@ -1,274 +0,0 @@
from facefusion.types import Locals
LOCALS : Locals =\
{
'en':
{
'conda_not_activated': 'conda is not activated',
'python_not_supported': 'python version is not supported, upgrade to {version} or higher',
'curl_not_installed': 'curl is not installed',
'ffmpeg_not_installed': 'ffmpeg is not installed',
'creating_temp': 'creating temporary resources',
'extracting_frames': 'extracting frames with a resolution of {resolution} and {fps} frames per second',
'extracting_frames_succeeded': 'extracting frames succeeded',
'extracting_frames_failed': 'extracting frames failed',
'analysing': 'analysing',
'extracting': 'extracting',
'streaming': 'streaming',
'processing': 'processing',
'merging': 'merging',
'downloading': 'downloading',
'temp_frames_not_found': 'temporary frames not found',
'copying_image': 'copying image with a resolution of {resolution}',
'copying_image_succeeded': 'copying image succeeded',
'copying_image_failed': 'copying image failed',
'finalizing_image': 'finalizing image with a resolution of {resolution}',
'finalizing_image_succeeded': 'finalizing image succeeded',
'finalizing_image_skipped': 'finalizing image skipped',
'merging_video': 'merging video with a resolution of {resolution} and {fps} frames per second',
'merging_video_succeeded': 'merging video succeeded',
'merging_video_failed': 'merging video failed',
'skipping_audio': 'skipping audio',
'replacing_audio_succeeded': 'replacing audio succeeded',
'replacing_audio_skipped': 'replacing audio skipped',
'restoring_audio_succeeded': 'restoring audio succeeded',
'restoring_audio_skipped': 'restoring audio skipped',
'clearing_temp': 'clearing temporary resources',
'processing_stopped': 'processing stopped',
'processing_image_succeeded': 'processing to image succeeded in {seconds} seconds',
'processing_image_failed': 'processing to image failed',
'processing_video_succeeded': 'processing to video succeeded in {seconds} seconds',
'processing_video_failed': 'processing to video failed',
'choose_image_source': 'choose an image for the source',
'choose_audio_source': 'choose an audio for the source',
'choose_video_target': 'choose a video for the target',
'choose_image_or_video_target': 'choose an image or video for the target',
'specify_image_or_video_output': 'specify the output image or video within a directory',
'match_target_and_output_extension': 'match the target and output extension',
'no_source_face_detected': 'no source face detected',
'processor_not_loaded': 'processor {processor} could not be loaded',
'processor_not_implemented': 'processor {processor} not implemented correctly',
'ui_layout_not_loaded': 'ui layout {ui_layout} could not be loaded',
'ui_layout_not_implemented': 'ui layout {ui_layout} not implemented correctly',
'stream_not_loaded': 'stream {stream_mode} could not be loaded',
'stream_not_supported': 'stream not supported',
'job_created': 'job {job_id} created',
'job_not_created': 'job {job_id} not created',
'job_submitted': 'job {job_id} submitted',
'job_not_submitted': 'job {job_id} not submitted',
'job_all_submitted': 'jobs submitted',
'job_all_not_submitted': 'jobs not submitted',
'job_deleted': 'job {job_id} deleted',
'job_not_deleted': 'job {job_id} not deleted',
'job_all_deleted': 'jobs deleted',
'job_all_not_deleted': 'jobs not deleted',
'job_step_added': 'step added to job {job_id}',
'job_step_not_added': 'step not added to job {job_id}',
'job_remix_step_added': 'step {step_index} remixed from job {job_id}',
'job_remix_step_not_added': 'step {step_index} not remixed from job {job_id}',
'job_step_inserted': 'step {step_index} inserted to job {job_id}',
'job_step_not_inserted': 'step {step_index} not inserted to job {job_id}',
'job_step_removed': 'step {step_index} removed from job {job_id}',
'job_step_not_removed': 'step {step_index} not removed from job {job_id}',
'running_job': 'running queued job {job_id}',
'running_jobs': 'running all queued jobs',
'retrying_job': 'retrying failed job {job_id}',
'retrying_jobs': 'retrying all failed jobs',
'processing_job_succeeded': 'processing of job {job_id} succeeded',
'processing_jobs_succeeded': 'processing of all jobs succeeded',
'processing_job_failed': 'processing of job {job_id} failed',
'processing_jobs_failed': 'processing of all jobs failed',
'processing_step': 'processing step {step_current} of {step_total}',
'validating_hash_succeeded': 'validating hash for {hash_file_name} succeeded',
'validating_hash_failed': 'validating hash for {hash_file_name} failed',
'validating_source_succeeded': 'validating source for {source_file_name} succeeded',
'validating_source_failed': 'validating source for {source_file_name} failed',
'deleting_corrupt_source': 'deleting corrupt source for {source_file_name}',
'loading_model_succeeded': 'loading model {model_name} succeeded in {seconds} seconds',
'loading_model_failed': 'loading model {model_name} failed',
'time_ago_now': 'just now',
'time_ago_minutes': '{minutes} minutes ago',
'time_ago_hours': '{hours} hours and {minutes} minutes ago',
'time_ago_days': '{days} days, {hours} hours and {minutes} minutes ago',
'point': '.',
'comma': ',',
'colon': ':',
'question_mark': '?',
'exclamation_mark': '!',
'help':
{
'install_dependency': 'choose the variant of {dependency} to install',
'skip_conda': 'skip the conda environment check',
'config_path': 'choose the config file to override defaults',
'temp_path': 'specify the directory for the temporary resources',
'jobs_path': 'specify the directory to store jobs',
'source_paths': 'choose the image or audio paths',
'target_path': 'choose the image or video path',
'output_path': 'specify the image or video within a directory',
'source_pattern': 'choose the image or audio pattern',
'target_pattern': 'choose the image or video pattern',
'output_pattern': 'specify the image or video pattern',
'face_detector_model': 'choose the model responsible for detecting the faces',
'face_detector_size': 'specify the frame size provided to the face detector',
'face_detector_margin': 'apply top, right, bottom and left margin to the frame',
'face_detector_angles': 'specify the angles to rotate the frame before detecting faces',
'face_detector_score': 'filter the detected faces based on the confidence score',
'face_landmarker_model': 'choose the model responsible for detecting the face landmarks',
'face_landmarker_score': 'filter the detected face landmarks based on the confidence score',
'face_selector_mode': 'use reference based tracking or simple matching',
'face_selector_order': 'specify the order of the detected faces',
'face_selector_age_start': 'filter the detected faces based on the starting age',
'face_selector_age_end': 'filter the detected faces based on the ending age',
'face_selector_gender': 'filter the detected faces based on their gender',
'face_selector_race': 'filter the detected faces based on their race',
'reference_face_position': 'specify the position used to create the reference face',
'reference_face_distance': 'specify the similarity between the reference face and target face',
'reference_frame_number': 'specify the frame used to create the reference face',
'face_occluder_model': 'choose the model responsible for the occlusion mask',
'face_parser_model': 'choose the model responsible for the region mask',
'face_mask_types': 'mix and match different face mask types (choices: {choices})',
'face_mask_areas': 'choose the items used for the area mask (choices: {choices})',
'face_mask_regions': 'choose the items used for the region mask (choices: {choices})',
'face_mask_blur': 'specify the degree of blur applied to the box mask',
'face_mask_padding': 'apply top, right, bottom and left padding to the box mask',
'voice_extractor_model': 'choose the model responsible for extracting the voices',
'trim_frame_start': 'specify the starting frame of the target video',
'trim_frame_end': 'specify the ending frame of the target video',
'temp_frame_format': 'specify the temporary resources format',
'keep_temp': 'keep the temporary resources after processing',
'output_image_quality': 'specify the image quality which translates to the image compression',
'output_image_scale': 'specify the image scale based on the target image',
'output_audio_encoder': 'specify the encoder used for the audio',
'output_audio_quality': 'specify the audio quality which translates to the audio compression',
'output_audio_volume': 'specify the audio volume based on the target video',
'output_video_encoder': 'specify the encoder used for the video',
'output_video_preset': 'balance fast video processing and video file size',
'output_video_quality': 'specify the video quality which translates to the video compression',
'output_video_scale': 'specify the video scale based on the target video',
'output_video_fps': 'specify the video fps based on the target video',
'processors': 'load a single or multiple processors (choices: {choices}, ...)',
'background-remover-model': 'choose the model responsible for removing the background',
'background-remover-color': 'apply red, green blue and alpha values of the background',
'open_browser': 'open the browser once the program is ready',
'ui_layouts': 'launch a single or multiple UI layouts (choices: {choices}, ...)',
'ui_workflow': 'choose the ui workflow',
'download_providers': 'download using different providers (choices: {choices}, ...)',
'download_scope': 'specify the download scope',
'benchmark_mode': 'choose the benchmark mode',
'benchmark_resolutions': 'choose the resolutions for the benchmarks (choices: {choices}, ...)',
'benchmark_cycle_count': 'specify the amount of cycles per benchmark',
'execution_device_ids': 'specify the devices used for processing',
'execution_providers': 'inference using different providers (choices: {choices}, ...)',
'execution_thread_count': 'specify the amount of parallel threads while processing',
'video_memory_strategy': 'balance fast processing and low VRAM usage',
'system_memory_limit': 'limit the available RAM that can be used while processing',
'log_level': 'adjust the message severity displayed in the terminal',
'halt_on_error': 'halt the program once an error occurred',
'run': 'run the program',
'headless_run': 'run the program in headless mode',
'batch_run': 'run the program in batch mode',
'force_download': 'force automate downloads and exit',
'benchmark': 'benchmark the program',
'job_id': 'specify the job id',
'job_status': 'specify the job status',
'step_index': 'specify the step index',
'job_list': 'list jobs by status',
'job_create': 'create a drafted job',
'job_submit': 'submit a drafted job to become a queued job',
'job_submit_all': 'submit all drafted jobs to become a queued jobs',
'job_delete': 'delete a drafted, queued, failed or completed job',
'job_delete_all': 'delete all drafted, queued, failed and completed jobs',
'job_add_step': 'add a step to a drafted job',
'job_remix_step': 'remix a previous step from a drafted job',
'job_insert_step': 'insert a step to a drafted job',
'job_remove_step': 'remove a step from a drafted job',
'job_run': 'run a queued job',
'job_run_all': 'run all queued jobs',
'job_retry': 'retry a failed job',
'job_retry_all': 'retry all failed jobs'
},
'about':
{
'fund': 'fund training server',
'subscribe': 'become a member',
'join': 'join our community'
},
'uis':
{
'apply_button': 'APPLY',
'benchmark_mode_dropdown': 'BENCHMARK MODE',
'benchmark_cycle_count_slider': 'BENCHMARK CYCLE COUNT',
'benchmark_resolutions_checkbox_group': 'BENCHMARK RESOLUTIONS',
'clear_button': 'CLEAR',
'common_options_checkbox_group': 'OPTIONS',
'download_providers_checkbox_group': 'DOWNLOAD PROVIDERS',
'execution_providers_checkbox_group': 'EXECUTION PROVIDERS',
'execution_thread_count_slider': 'EXECUTION THREAD COUNT',
'face_detector_angles_checkbox_group': 'FACE DETECTOR ANGLES',
'face_detector_model_dropdown': 'FACE DETECTOR MODEL',
'face_detector_margin_slider': 'FACE DETECTOR MARGIN',
'face_detector_score_slider': 'FACE DETECTOR SCORE',
'face_detector_size_dropdown': 'FACE DETECTOR SIZE',
'face_landmarker_model_dropdown': 'FACE LANDMARKER MODEL',
'face_landmarker_score_slider': 'FACE LANDMARKER SCORE',
'face_mask_blur_slider': 'FACE MASK BLUR',
'face_mask_padding_bottom_slider': 'FACE MASK PADDING BOTTOM',
'face_mask_padding_left_slider': 'FACE MASK PADDING LEFT',
'face_mask_padding_right_slider': 'FACE MASK PADDING RIGHT',
'face_mask_padding_top_slider': 'FACE MASK PADDING TOP',
'face_mask_areas_checkbox_group': 'FACE MASK AREAS',
'face_mask_regions_checkbox_group': 'FACE MASK REGIONS',
'face_mask_types_checkbox_group': 'FACE MASK TYPES',
'face_selector_age_range_slider': 'FACE SELECTOR AGE',
'face_selector_gender_dropdown': 'FACE SELECTOR GENDER',
'face_selector_mode_dropdown': 'FACE SELECTOR MODE',
'face_selector_order_dropdown': 'FACE SELECTOR ORDER',
'face_selector_race_dropdown': 'FACE SELECTOR RACE',
'face_occluder_model_dropdown': 'FACE OCCLUDER MODEL',
'face_parser_model_dropdown': 'FACE PARSER MODEL',
'voice_extractor_model_dropdown': 'VOICE EXTRACTOR MODEL',
'job_list_status_checkbox_group': 'JOB STATUS',
'job_manager_job_action_dropdown': 'JOB_ACTION',
'job_manager_job_id_dropdown': 'JOB ID',
'job_manager_step_index_dropdown': 'STEP INDEX',
'job_runner_job_action_dropdown': 'JOB ACTION',
'job_runner_job_id_dropdown': 'JOB ID',
'log_level_dropdown': 'LOG LEVEL',
'output_audio_encoder_dropdown': 'OUTPUT AUDIO ENCODER',
'output_audio_quality_slider': 'OUTPUT AUDIO QUALITY',
'output_audio_volume_slider': 'OUTPUT AUDIO VOLUME',
'output_image_or_video': 'OUTPUT',
'output_image_quality_slider': 'OUTPUT IMAGE QUALITY',
'output_image_scale_slider': 'OUTPUT IMAGE SCALE',
'output_path_textbox': 'OUTPUT PATH',
'output_video_encoder_dropdown': 'OUTPUT VIDEO ENCODER',
'output_video_fps_slider': 'OUTPUT VIDEO FPS',
'output_video_preset_dropdown': 'OUTPUT VIDEO PRESET',
'output_video_quality_slider': 'OUTPUT VIDEO QUALITY',
'output_video_scale_slider': 'OUTPUT VIDEO SCALE',
'preview_frame_slider': 'PREVIEW FRAME',
'preview_image': 'PREVIEW',
'preview_mode_dropdown': 'PREVIEW MODE',
'preview_resolution_dropdown': 'PREVIEW RESOLUTION',
'processors_checkbox_group': 'PROCESSORS',
'reference_face_distance_slider': 'REFERENCE FACE DISTANCE',
'reference_face_gallery': 'REFERENCE FACE',
'refresh_button': 'REFRESH',
'source_file': 'SOURCE',
'start_button': 'START',
'stop_button': 'STOP',
'system_memory_limit_slider': 'SYSTEM MEMORY LIMIT',
'target_file': 'TARGET',
'temp_frame_format_dropdown': 'TEMP FRAME FORMAT',
'terminal_textbox': 'TERMINAL',
'trim_frame_slider': 'TRIM FRAME',
'ui_workflow': 'UI WORKFLOW',
'video_memory_strategy_dropdown': 'VIDEO MEMORY STRATEGY',
'webcam_fps_slider': 'WEBCAM FPS',
'webcam_image': 'WEBCAM',
'webcam_device_id_dropdown': 'WEBCAM DEVICE ID',
'webcam_mode_radio': 'WEBCAM MODE',
'webcam_resolution_dropdown': 'WEBCAM RESOLUTION'
}
}
}

View File

@@ -1,13 +1,14 @@
from logging import Logger, basicConfig, getLogger from logging import Logger, basicConfig, getLogger
from typing import Tuple
import facefusion.choices from facefusion.choices import log_level_set
from facefusion.common_helper import get_first, get_last from facefusion.common_helper import get_first, get_last
from facefusion.types import LogLevel from facefusion.typing import LogLevel, TableContents, TableHeaders
def init(log_level : LogLevel) -> None: def init(log_level : LogLevel) -> None:
basicConfig(format = '%(message)s') basicConfig(format = '%(message)s')
get_package_logger().setLevel(facefusion.choices.log_level_set.get(log_level)) get_package_logger().setLevel(log_level_set.get(log_level))
def get_package_logger() -> Logger: def get_package_logger() -> Logger:
@@ -31,15 +32,46 @@ def error(message : str, module_name : str) -> None:
def create_message(message : str, module_name : str) -> str: def create_message(message : str, module_name : str) -> str:
module_names = module_name.split('.') scopes = module_name.split('.')
first_module_name = get_first(module_names) first_scope = get_first(scopes)
last_module_name = get_last(module_names) last_scope = get_last(scopes)
if first_module_name and last_module_name: if first_scope and last_scope:
return '[' + first_module_name.upper() + '.' + last_module_name.upper() + '] ' + message return '[' + first_scope.upper() + '.' + last_scope.upper() + '] ' + message
return message return message
def table(headers : TableHeaders, contents : TableContents) -> None:
package_logger = get_package_logger()
table_column, table_separator = create_table_parts(headers, contents)
package_logger.info(table_separator)
package_logger.info(table_column.format(*headers))
package_logger.info(table_separator)
for content in contents:
content = [ value if value else '' for value in content ]
package_logger.info(table_column.format(*content))
package_logger.info(table_separator)
def create_table_parts(headers : TableHeaders, contents : TableContents) -> Tuple[str, str]:
column_parts = []
separator_parts = []
widths = [ len(header) for header in headers ]
for content in contents:
for index, value in enumerate(content):
widths[index] = max(widths[index], len(str(value)))
for width in widths:
column_parts.append('{:<' + str(width) + '}')
separator_parts.append('-' * width)
return '| ' + ' | '.join(column_parts) + ' |', '+-' + '-+-'.join(separator_parts) + '-+'
def enable() -> None: def enable() -> None:
get_package_logger().disabled = False get_package_logger().disabled = False

View File

@@ -4,12 +4,14 @@ METADATA =\
{ {
'name': 'FaceFusion', 'name': 'FaceFusion',
'description': 'Industry leading face manipulation platform', 'description': 'Industry leading face manipulation platform',
'version': '3.5.1', 'version': '3.0.1',
'license': 'OpenRAIL-AS', 'license': 'MIT',
'author': 'Henry Ruhs', 'author': 'Henry Ruhs',
'url': 'https://facefusion.io' 'url': 'https://facefusion.io'
} }
def get(key : str) -> Optional[str]: def get(key : str) -> Optional[str]:
if key in METADATA:
return METADATA.get(key) return METADATA.get(key)
return None

View File

@@ -1,11 +0,0 @@
from functools import lru_cache
import onnx
from facefusion.types import ModelInitializer
@lru_cache()
def get_static_model_initializer(model_path : str) -> ModelInitializer:
model = onnx.load(model_path)
return onnx.numpy_helper.to_array(model.graph.initializer[-1])

View File

@@ -1,29 +1,17 @@
from typing import List, Optional from typing import List, Optional
from facefusion.types import Color, Fps, Padding from facefusion.typing import Fps, Padding
def normalize_color(channels : Optional[List[int]]) -> Optional[Color]: def normalize_padding(padding : Optional[List[int]]) -> Optional[Padding]:
if channels and len(channels) == 1: if padding and len(padding) == 1:
return tuple([ channels[0], channels[0], channels[0], 255 ]) #type:ignore[return-value] return tuple([ padding[0] ] * 4) #type:ignore[return-value]
if channels and len(channels) == 2: if padding and len(padding) == 2:
return tuple([ channels[0], channels[1], channels[0], 255 ]) #type:ignore[return-value] return tuple([ padding[0], padding[1], padding[0], padding[1] ]) #type:ignore[return-value]
if channels and len(channels) == 3: if padding and len(padding) == 3:
return tuple([ channels[0], channels[1], channels[2], 255 ]) #type:ignore[return-value] return tuple([ padding[0], padding[1], padding[2], padding[1] ]) #type:ignore[return-value]
if channels and len(channels) == 4: if padding and len(padding) == 4:
return tuple(channels) #type:ignore[return-value] return tuple(padding) #type:ignore[return-value]
return None
def normalize_space(spaces : Optional[List[int]]) -> Optional[Padding]:
if spaces and len(spaces) == 1:
return tuple([spaces[0]] * 4) #type:ignore[return-value]
if spaces and len(spaces) == 2:
return tuple([spaces[0], spaces[1], spaces[0], spaces[1]]) #type:ignore[return-value]
if spaces and len(spaces) == 3:
return tuple([spaces[0], spaces[1], spaces[2], spaces[1]]) #type:ignore[return-value]
if spaces and len(spaces) == 4:
return tuple(spaces) #type:ignore[return-value]
return None return None

View File

@@ -1,4 +1,6 @@
from facefusion.types import ProcessState from typing import Generator, List
from facefusion.typing import ProcessState, QueuePayload
PROCESS_STATE : ProcessState = 'pending' PROCESS_STATE : ProcessState = 'pending'
@@ -43,3 +45,9 @@ def stop() -> None:
def end() -> None: def end() -> None:
set_process_state('pending') set_process_state('pending')
def manage(queue_payloads : List[QueuePayload]) -> Generator[QueuePayload, None, None]:
for query_payload in queue_payloads:
if is_processing():
yield query_payload

View File

@@ -1,27 +1,46 @@
from facefusion.processors.modules.age_modifier.choices import age_modifier_direction_range, age_modifier_models # noqa: F401 from typing import List, Sequence
from facefusion.processors.modules.background_remover.choices import background_remover_color_range, background_remover_models # noqa: F401
from facefusion.processors.modules.deep_swapper.choices import deep_swapper_models, deep_swapper_morph_range # noqa: F401 from facefusion.common_helper import create_float_range, create_int_range
from facefusion.processors.modules.expression_restorer.choices import expression_restorer_areas, expression_restorer_factor_range, expression_restorer_models # noqa: F401 from facefusion.processors.typing import AgeModifierModel, ExpressionRestorerModel, FaceDebuggerItem, FaceEditorModel, FaceEnhancerModel, FaceSwapperSet, FrameColorizerModel, FrameEnhancerModel, LipSyncerModel
from facefusion.processors.modules.face_debugger.choices import face_debugger_items # noqa: F401
from facefusion.processors.modules.face_editor.choices import ( # noqa: F401 age_modifier_models : List[AgeModifierModel] = [ 'styleganex_age' ]
face_editor_eye_gaze_horizontal_range, expression_restorer_models : List[ExpressionRestorerModel] = [ 'live_portrait' ]
face_editor_eye_gaze_vertical_range, face_debugger_items : List[FaceDebuggerItem] = [ 'bounding-box', 'face-landmark-5', 'face-landmark-5/68', 'face-landmark-68', 'face-landmark-68/5', 'face-mask', 'face-detector-score', 'face-landmarker-score', 'age', 'gender', 'race' ]
face_editor_eye_open_ratio_range, face_editor_models : List[FaceEditorModel] = [ 'live_portrait' ]
face_editor_eyebrow_direction_range, face_enhancer_models : List[FaceEnhancerModel] = [ 'codeformer', 'gfpgan_1.2', 'gfpgan_1.3', 'gfpgan_1.4', 'gpen_bfr_256', 'gpen_bfr_512', 'gpen_bfr_1024', 'gpen_bfr_2048', 'restoreformer_plus_plus' ]
face_editor_head_pitch_range, face_swapper_set : FaceSwapperSet =\
face_editor_head_roll_range, {
face_editor_head_yaw_range, 'blendswap_256': [ '256x256', '384x384', '512x512', '768x768', '1024x1024' ],
face_editor_lip_open_ratio_range, 'ghost_1_256': [ '256x256', '512x512', '768x768', '1024x1024' ],
face_editor_models, 'ghost_2_256': [ '256x256', '512x512', '768x768', '1024x1024' ],
face_editor_mouth_grim_range, 'ghost_3_256': [ '256x256', '512x512', '768x768', '1024x1024' ],
face_editor_mouth_position_horizontal_range, 'inswapper_128': [ '128x128', '256x256', '384x384', '512x512', '768x768', '1024x1024' ],
face_editor_mouth_position_vertical_range, 'inswapper_128_fp16': [ '128x128', '256x256', '384x384', '512x512', '768x768', '1024x1024' ],
face_editor_mouth_pout_range, 'simswap_256': [ '256x256', '512x512', '768x768', '1024x1024' ],
face_editor_mouth_purse_range, 'simswap_unofficial_512': [ '512x512', '768x768', '1024x1024' ],
face_editor_mouth_smile_range, 'uniface_256': [ '256x256', '512x512', '768x768', '1024x1024' ]
) }
from facefusion.processors.modules.face_enhancer.choices import face_enhancer_blend_range, face_enhancer_models, face_enhancer_weight_range # noqa: F401 frame_colorizer_models : List[FrameColorizerModel] = [ 'ddcolor', 'ddcolor_artistic', 'deoldify', 'deoldify_artistic', 'deoldify_stable' ]
from facefusion.processors.modules.face_swapper.choices import face_swapper_models, face_swapper_set, face_swapper_weight_range # noqa: F401 frame_colorizer_sizes : List[str] = [ '192x192', '256x256', '384x384', '512x512' ]
from facefusion.processors.modules.frame_colorizer.choices import frame_colorizer_blend_range, frame_colorizer_models, frame_colorizer_sizes # noqa: F401 frame_enhancer_models : List[FrameEnhancerModel] = [ 'clear_reality_x4', 'lsdir_x4', 'nomos8k_sc_x4', 'real_esrgan_x2', 'real_esrgan_x2_fp16', 'real_esrgan_x4', 'real_esrgan_x4_fp16', 'real_esrgan_x8', 'real_esrgan_x8_fp16', 'real_hatgan_x4', 'span_kendata_x4', 'ultra_sharp_x4' ]
from facefusion.processors.modules.frame_enhancer.choices import frame_enhancer_blend_range, frame_enhancer_models # noqa: F401 lip_syncer_models : List[LipSyncerModel] = [ 'wav2lip_96', 'wav2lip_gan_96' ]
from facefusion.processors.modules.lip_syncer.choices import lip_syncer_models, lip_syncer_weight_range # noqa: F401
age_modifier_direction_range : Sequence[int] = create_int_range(-100, 100, 1)
expression_restorer_factor_range : Sequence[int] = create_int_range(0, 100, 1)
face_editor_eyebrow_direction_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
face_editor_eye_gaze_horizontal_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
face_editor_eye_gaze_vertical_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
face_editor_eye_open_ratio_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
face_editor_lip_open_ratio_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
face_editor_mouth_grim_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
face_editor_mouth_pout_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
face_editor_mouth_purse_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
face_editor_mouth_smile_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
face_editor_mouth_position_horizontal_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
face_editor_mouth_position_vertical_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
face_editor_head_pitch_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
face_editor_head_yaw_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
face_editor_head_roll_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
face_enhancer_blend_range : Sequence[int] = create_int_range(0, 100, 1)
frame_colorizer_blend_range : Sequence[int] = create_int_range(0, 100, 1)
frame_enhancer_blend_range : Sequence[int] = create_int_range(0, 100, 1)

View File

@@ -1,10 +1,15 @@
import importlib import importlib
import os
from concurrent.futures import ThreadPoolExecutor, as_completed
from queue import Queue
from types import ModuleType from types import ModuleType
from typing import Any, List from typing import Any, List
from facefusion import logger, translator from tqdm import tqdm
from facefusion.exit_helper import hard_exit
from facefusion import logger, state_manager, wording
from facefusion.exit_helper import hard_exit
from facefusion.typing import ProcessFrames, QueuePayload
PROCESSORS_METHODS =\ PROCESSORS_METHODS =\
[ [
@@ -15,22 +20,26 @@ PROCESSORS_METHODS =\
'pre_check', 'pre_check',
'pre_process', 'pre_process',
'post_process', 'post_process',
'process_frame' 'get_reference_frame',
'process_frame',
'process_frames',
'process_image',
'process_video'
] ]
def load_processor_module(processor : str) -> Any: def load_processor_module(processor : str) -> Any:
try: try:
processor_module = importlib.import_module('facefusion.processors.modules.' + processor + '.core') processor_module = importlib.import_module('facefusion.processors.modules.' + processor)
for method_name in PROCESSORS_METHODS: for method_name in PROCESSORS_METHODS:
if not hasattr(processor_module, method_name): if not hasattr(processor_module, method_name):
raise NotImplementedError raise NotImplementedError
except ModuleNotFoundError as exception: except ModuleNotFoundError as exception:
logger.error(translator.get('processor_not_loaded').format(processor = processor), __name__) logger.error(wording.get('processor_not_loaded').format(processor = processor), __name__)
logger.debug(exception.msg, __name__) logger.debug(exception.msg, __name__)
hard_exit(1) hard_exit(1)
except NotImplementedError: except NotImplementedError:
logger.error(translator.get('processor_not_implemented').format(processor = processor), __name__) logger.error(wording.get('processor_not_implemented').format(processor = processor), __name__)
hard_exit(1) hard_exit(1)
return processor_module return processor_module
@@ -42,3 +51,60 @@ def get_processors_modules(processors : List[str]) -> List[ModuleType]:
processor_module = load_processor_module(processor) processor_module = load_processor_module(processor)
processor_modules.append(processor_module) processor_modules.append(processor_module)
return processor_modules return processor_modules
def clear_processors_modules(processors : List[str]) -> None:
for processor in processors:
processor_module = load_processor_module(processor)
processor_module.clear_inference_pool()
def multi_process_frames(source_paths : List[str], temp_frame_paths : List[str], process_frames : ProcessFrames) -> None:
queue_payloads = create_queue_payloads(temp_frame_paths)
with tqdm(total = len(queue_payloads), desc = wording.get('processing'), unit = 'frame', ascii = ' =', disable = state_manager.get_item('log_level') in [ 'warn', 'error' ]) as progress:
progress.set_postfix(
{
'execution_providers': state_manager.get_item('execution_providers'),
'execution_thread_count': state_manager.get_item('execution_thread_count'),
'execution_queue_count': state_manager.get_item('execution_queue_count')
})
with ThreadPoolExecutor(max_workers = state_manager.get_item('execution_thread_count')) as executor:
futures = []
queue : Queue[QueuePayload] = create_queue(queue_payloads)
queue_per_future = max(len(queue_payloads) // state_manager.get_item('execution_thread_count') * state_manager.get_item('execution_queue_count'), 1)
while not queue.empty():
future = executor.submit(process_frames, source_paths, pick_queue(queue, queue_per_future), progress.update)
futures.append(future)
for future_done in as_completed(futures):
future_done.result()
def create_queue(queue_payloads : List[QueuePayload]) -> Queue[QueuePayload]:
queue : Queue[QueuePayload] = Queue()
for queue_payload in queue_payloads:
queue.put(queue_payload)
return queue
def pick_queue(queue : Queue[QueuePayload], queue_per_future : int) -> List[QueuePayload]:
queues = []
for _ in range(queue_per_future):
if not queue.empty():
queues.append(queue.get())
return queues
def create_queue_payloads(temp_frame_paths : List[str]) -> List[QueuePayload]:
queue_payloads = []
temp_frame_paths = sorted(temp_frame_paths, key = os.path.basename)
for frame_number, frame_path in enumerate(temp_frame_paths):
frame_payload : QueuePayload =\
{
'frame_number': frame_number,
'frame_path': frame_path
}
queue_payloads.append(frame_payload)
return queue_payloads

View File

@@ -3,7 +3,7 @@ from typing import Tuple
import numpy import numpy
import scipy import scipy
from facefusion.processors.types import LivePortraitExpression, LivePortraitPitch, LivePortraitRoll, LivePortraitRotation, LivePortraitYaw from facefusion.processors.typing import LivePortraitExpression, LivePortraitPitch, LivePortraitRoll, LivePortraitRotation, LivePortraitYaw
EXPRESSION_MIN = numpy.array( EXPRESSION_MIN = numpy.array(
[ [
@@ -63,15 +63,15 @@ def limit_expression(expression : LivePortraitExpression) -> LivePortraitExpress
return numpy.clip(expression, EXPRESSION_MIN, EXPRESSION_MAX) return numpy.clip(expression, EXPRESSION_MIN, EXPRESSION_MAX)
def limit_angle(target_pitch : LivePortraitPitch, target_yaw : LivePortraitYaw, target_roll : LivePortraitRoll, output_pitch : LivePortraitPitch, output_yaw : LivePortraitYaw, output_roll : LivePortraitRoll) -> Tuple[LivePortraitPitch, LivePortraitYaw, LivePortraitRoll]: def limit_euler_angles(target_pitch : LivePortraitPitch, target_yaw : LivePortraitYaw, target_roll : LivePortraitRoll, output_pitch : LivePortraitPitch, output_yaw : LivePortraitYaw, output_roll : LivePortraitRoll) -> Tuple[LivePortraitPitch, LivePortraitYaw, LivePortraitRoll]:
pitch_min, pitch_max, yaw_min, yaw_max, roll_min, roll_max = calculate_euler_limits(target_pitch, target_yaw, target_roll) pitch_min, pitch_max, yaw_min, yaw_max, roll_min, roll_max = calc_euler_limits(target_pitch, target_yaw, target_roll)
output_pitch = numpy.clip(output_pitch, pitch_min, pitch_max) output_pitch = numpy.clip(output_pitch, pitch_min, pitch_max)
output_yaw = numpy.clip(output_yaw, yaw_min, yaw_max) output_yaw = numpy.clip(output_yaw, yaw_min, yaw_max)
output_roll = numpy.clip(output_roll, roll_min, roll_max) output_roll = numpy.clip(output_roll, roll_min, roll_max)
return output_pitch, output_yaw, output_roll return output_pitch, output_yaw, output_roll
def calculate_euler_limits(pitch : LivePortraitPitch, yaw : LivePortraitYaw, roll : LivePortraitRoll) -> Tuple[float, float, float, float, float, float]: def calc_euler_limits(pitch : LivePortraitPitch, yaw : LivePortraitYaw, roll : LivePortraitRoll) -> Tuple[float, float, float, float, float, float]:
pitch_min = -30.0 pitch_min = -30.0
pitch_max = 30.0 pitch_max = 30.0
yaw_min = -60.0 yaw_min = -60.0

View File

@@ -0,0 +1,268 @@
from argparse import ArgumentParser
from typing import Any, List
import cv2
import numpy
from cv2.typing import Size
from numpy.typing import NDArray
import facefusion.jobs.job_manager
import facefusion.jobs.job_store
import facefusion.processors.core as processors
from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, inference_manager, logger, process_manager, state_manager, wording
from facefusion.common_helper import create_int_metavar
from facefusion.download import conditional_download_hashes, conditional_download_sources
from facefusion.face_analyser import get_many_faces, get_one_face
from facefusion.face_helper import merge_matrix, paste_back, scale_face_landmark_5, warp_face_by_face_landmark_5
from facefusion.face_masker import create_occlusion_mask, create_static_box_mask
from facefusion.face_selector import find_similar_faces, sort_and_filter_faces
from facefusion.face_store import get_reference_faces
from facefusion.filesystem import in_directory, is_image, is_video, resolve_relative_path, same_file_extension
from facefusion.processors import choices as processors_choices
from facefusion.processors.typing import AgeModifierInputs
from facefusion.program_helper import find_argument_group
from facefusion.thread_helper import thread_semaphore
from facefusion.typing import ApplyStateItem, Args, Face, InferencePool, Mask, ModelOptions, ModelSet, ProcessMode, QueuePayload, UpdateProgress, VisionFrame
from facefusion.vision import read_image, read_static_image, write_image
MODEL_SET : ModelSet =\
{
'styleganex_age':
{
'hashes':
{
'age_modifier':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/styleganex_age.hash',
'path': resolve_relative_path('../.assets/models/styleganex_age.hash')
}
},
'sources':
{
'age_modifier':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/styleganex_age.onnx',
'path': resolve_relative_path('../.assets/models/styleganex_age.onnx')
}
},
'template': 'ffhq_512',
'size': (512, 512)
}
}
def get_inference_pool() -> InferencePool:
model_sources = get_model_options().get('sources')
model_context = __name__ + '.' + state_manager.get_item('age_modifier_model')
return inference_manager.get_inference_pool(model_context, model_sources)
def clear_inference_pool() -> None:
model_context = __name__ + '.' + state_manager.get_item('age_modifier_model')
inference_manager.clear_inference_pool(model_context)
def get_model_options() -> ModelOptions:
age_modifier_model = state_manager.get_item('age_modifier_model')
return MODEL_SET.get(age_modifier_model)
def register_args(program : ArgumentParser) -> None:
group_processors = find_argument_group(program, 'processors')
if group_processors:
group_processors.add_argument('--age-modifier-model', help = wording.get('help.age_modifier_model'), default = config.get_str_value('processors.age_modifier_model', 'styleganex_age'), choices = processors_choices.age_modifier_models)
group_processors.add_argument('--age-modifier-direction', help = wording.get('help.age_modifier_direction'), type = int, default = config.get_int_value('processors.age_modifier_direction', '0'), choices = processors_choices.age_modifier_direction_range, metavar = create_int_metavar(processors_choices.age_modifier_direction_range))
facefusion.jobs.job_store.register_step_keys([ 'age_modifier_model', 'age_modifier_direction' ])
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('age_modifier_model', args.get('age_modifier_model'))
apply_state_item('age_modifier_direction', args.get('age_modifier_direction'))
def pre_check() -> bool:
download_directory_path = resolve_relative_path('../.assets/models')
model_hashes = get_model_options().get('hashes')
model_sources = get_model_options().get('sources')
return conditional_download_hashes(download_directory_path, model_hashes) and conditional_download_sources(download_directory_path, model_sources)
def pre_process(mode : ProcessMode) -> bool:
if mode in [ 'output', 'preview' ] and not is_image(state_manager.get_item('target_path')) and not is_video(state_manager.get_item('target_path')):
logger.error(wording.get('choose_image_or_video_target') + wording.get('exclamation_mark'), __name__)
return False
if mode == 'output' and not in_directory(state_manager.get_item('output_path')):
logger.error(wording.get('specify_image_or_video_output') + wording.get('exclamation_mark'), __name__)
return False
if mode == 'output' and not same_file_extension([ state_manager.get_item('target_path'), state_manager.get_item('output_path') ]):
logger.error(wording.get('match_target_and_output_extension') + wording.get('exclamation_mark'), __name__)
return False
return True
def post_process() -> None:
read_static_image.cache_clear()
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
clear_inference_pool()
if state_manager.get_item('video_memory_strategy') == 'strict':
content_analyser.clear_inference_pool()
face_classifier.clear_inference_pool()
face_detector.clear_inference_pool()
face_landmarker.clear_inference_pool()
face_masker.clear_inference_pool()
face_recognizer.clear_inference_pool()
def modify_age(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
model_template = get_model_options().get('template')
model_size = get_model_options().get('size')
crop_size = (model_size[0] // 2, model_size[1] // 2)
face_landmark_5 = target_face.landmark_set.get('5/68').copy()
extend_face_landmark_5 = scale_face_landmark_5(face_landmark_5, 2.0)
crop_vision_frame, affine_matrix = warp_face_by_face_landmark_5(temp_vision_frame, face_landmark_5, model_template, crop_size)
extend_vision_frame, extend_affine_matrix = warp_face_by_face_landmark_5(temp_vision_frame, extend_face_landmark_5, model_template, model_size)
extend_vision_frame_raw = extend_vision_frame.copy()
box_mask = create_static_box_mask(model_size, state_manager.get_item('face_mask_blur'), (0, 0, 0, 0))
crop_masks =\
[
box_mask
]
if 'occlusion' in state_manager.get_item('face_mask_types'):
occlusion_mask = create_occlusion_mask(crop_vision_frame)
combined_matrix = merge_matrix([ extend_affine_matrix, cv2.invertAffineTransform(affine_matrix) ])
occlusion_mask = cv2.warpAffine(occlusion_mask, combined_matrix, model_size)
crop_masks.append(occlusion_mask)
crop_vision_frame = prepare_vision_frame(crop_vision_frame)
extend_vision_frame = prepare_vision_frame(extend_vision_frame)
extend_vision_frame = forward(crop_vision_frame, extend_vision_frame)
extend_vision_frame = normalize_extend_frame(extend_vision_frame)
extend_vision_frame = fix_color(extend_vision_frame_raw, extend_vision_frame)
extend_crop_mask = cv2.pyrUp(numpy.minimum.reduce(crop_masks).clip(0, 1))
extend_affine_matrix *= extend_vision_frame.shape[0] / 512
paste_vision_frame = paste_back(temp_vision_frame, extend_vision_frame, extend_crop_mask, extend_affine_matrix)
return paste_vision_frame
def forward(crop_vision_frame : VisionFrame, extend_vision_frame : VisionFrame) -> VisionFrame:
age_modifier = get_inference_pool().get('age_modifier')
age_modifier_inputs = {}
for age_modifier_input in age_modifier.get_inputs():
if age_modifier_input.name == 'target':
age_modifier_inputs[age_modifier_input.name] = crop_vision_frame
if age_modifier_input.name == 'target_with_background':
age_modifier_inputs[age_modifier_input.name] = extend_vision_frame
if age_modifier_input.name == 'direction':
age_modifier_inputs[age_modifier_input.name] = prepare_direction(state_manager.get_item('age_modifier_direction'))
with thread_semaphore():
crop_vision_frame = age_modifier.run(None, age_modifier_inputs)[0][0]
return crop_vision_frame
def fix_color(extend_vision_frame_raw : VisionFrame, extend_vision_frame : VisionFrame) -> VisionFrame:
color_difference = compute_color_difference(extend_vision_frame_raw, extend_vision_frame, (48, 48))
color_difference_mask = create_static_box_mask(extend_vision_frame.shape[:2][::-1], 1.0, (0, 0, 0, 0))
color_difference_mask = numpy.stack((color_difference_mask, ) * 3, axis = -1)
extend_vision_frame = normalize_color_difference(color_difference, color_difference_mask, extend_vision_frame)
return extend_vision_frame
def compute_color_difference(extend_vision_frame_raw : VisionFrame, extend_vision_frame : VisionFrame, size : Size) -> VisionFrame:
extend_vision_frame_raw = extend_vision_frame_raw.astype(numpy.float32) / 255
extend_vision_frame_raw = cv2.resize(extend_vision_frame_raw, size, interpolation = cv2.INTER_AREA)
extend_vision_frame = extend_vision_frame.astype(numpy.float32) / 255
extend_vision_frame = cv2.resize(extend_vision_frame, size, interpolation = cv2.INTER_AREA)
color_difference = extend_vision_frame_raw - extend_vision_frame
return color_difference
def normalize_color_difference(color_difference : VisionFrame, color_difference_mask : Mask, extend_vision_frame : VisionFrame) -> VisionFrame:
color_difference = cv2.resize(color_difference, extend_vision_frame.shape[:2][::-1], interpolation = cv2.INTER_CUBIC)
color_difference_mask = 1 - color_difference_mask.clip(0, 0.75)
extend_vision_frame = extend_vision_frame.astype(numpy.float32) / 255
extend_vision_frame += color_difference * color_difference_mask
extend_vision_frame = extend_vision_frame.clip(0, 1)
extend_vision_frame = numpy.multiply(extend_vision_frame, 255).astype(numpy.uint8)
return extend_vision_frame
def prepare_direction(direction : int) -> NDArray[Any]:
direction = numpy.interp(float(direction), [ -100, 100 ], [ 2.5, -2.5 ]) #type:ignore[assignment]
return numpy.array(direction).astype(numpy.float32)
def prepare_vision_frame(vision_frame : VisionFrame) -> VisionFrame:
vision_frame = vision_frame[:, :, ::-1] / 255.0
vision_frame = (vision_frame - 0.5) / 0.5
vision_frame = numpy.expand_dims(vision_frame.transpose(2, 0, 1), axis = 0).astype(numpy.float32)
return vision_frame
def normalize_extend_frame(extend_vision_frame : VisionFrame) -> VisionFrame:
extend_vision_frame = numpy.clip(extend_vision_frame, -1, 1)
extend_vision_frame = (extend_vision_frame + 1) / 2
extend_vision_frame = extend_vision_frame.transpose(1, 2, 0).clip(0, 255)
extend_vision_frame = (extend_vision_frame * 255.0)
extend_vision_frame = extend_vision_frame.astype(numpy.uint8)[:, :, ::-1]
extend_vision_frame = cv2.pyrDown(extend_vision_frame)
return extend_vision_frame
def get_reference_frame(source_face : Face, target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
return modify_age(target_face, temp_vision_frame)
def process_frame(inputs : AgeModifierInputs) -> VisionFrame:
reference_faces = inputs.get('reference_faces')
target_vision_frame = inputs.get('target_vision_frame')
many_faces = sort_and_filter_faces(get_many_faces([ target_vision_frame ]))
if state_manager.get_item('face_selector_mode') == 'many':
if many_faces:
for target_face in many_faces:
target_vision_frame = modify_age(target_face, target_vision_frame)
if state_manager.get_item('face_selector_mode') == 'one':
target_face = get_one_face(many_faces)
if target_face:
target_vision_frame = modify_age(target_face, target_vision_frame)
if state_manager.get_item('face_selector_mode') == 'reference':
similar_faces = find_similar_faces(many_faces, reference_faces, state_manager.get_item('reference_face_distance'))
if similar_faces:
for similar_face in similar_faces:
target_vision_frame = modify_age(similar_face, target_vision_frame)
return target_vision_frame
def process_frames(source_path : List[str], queue_payloads : List[QueuePayload], update_progress : UpdateProgress) -> None:
reference_faces = get_reference_faces() if 'reference' in state_manager.get_item('face_selector_mode') else None
for queue_payload in process_manager.manage(queue_payloads):
target_vision_path = queue_payload['frame_path']
target_vision_frame = read_image(target_vision_path)
output_vision_frame = process_frame(
{
'reference_faces': reference_faces,
'target_vision_frame': target_vision_frame
})
write_image(target_vision_path, output_vision_frame)
update_progress(1)
def process_image(source_path : str, target_path : str, output_path : str) -> None:
reference_faces = get_reference_faces() if 'reference' in state_manager.get_item('face_selector_mode') else None
target_vision_frame = read_static_image(target_path)
output_vision_frame = process_frame(
{
'reference_faces': reference_faces,
'target_vision_frame': target_vision_frame
})
write_image(output_path, output_vision_frame)
def process_video(source_paths : List[str], temp_frame_paths : List[str]) -> None:
processors.multi_process_frames(None, temp_frame_paths, process_frames)

View File

@@ -1,8 +0,0 @@
from typing import List, Sequence
from facefusion.common_helper import create_int_range
from facefusion.processors.modules.age_modifier.types import AgeModifierModel
age_modifier_models : List[AgeModifierModel] = [ 'styleganex_age' ]
age_modifier_direction_range : Sequence[int] = create_int_range(-100, 100, 1)

View File

@@ -1,219 +0,0 @@
from argparse import ArgumentParser
from functools import lru_cache
import cv2
import numpy
import facefusion.choices
import facefusion.jobs.job_manager
import facefusion.jobs.job_store
from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, inference_manager, logger, state_manager, translator, video_manager
from facefusion.common_helper import create_int_metavar, is_macos
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
from facefusion.execution import has_execution_provider
from facefusion.face_analyser import scale_face
from facefusion.face_helper import merge_matrix, paste_back, scale_face_landmark_5, warp_face_by_face_landmark_5
from facefusion.face_masker import create_box_mask, create_occlusion_mask
from facefusion.face_selector import select_faces
from facefusion.filesystem import in_directory, is_image, is_video, resolve_relative_path, same_file_extension
from facefusion.processors.modules.age_modifier import choices as age_modifier_choices
from facefusion.processors.modules.age_modifier.types import AgeModifierDirection, AgeModifierInputs
from facefusion.processors.types import ProcessorOutputs
from facefusion.program_helper import find_argument_group
from facefusion.thread_helper import thread_semaphore
from facefusion.types import ApplyStateItem, Args, DownloadScope, Face, InferencePool, ModelOptions, ModelSet, ProcessMode, VisionFrame
from facefusion.vision import match_frame_color, read_static_image, read_static_video_frame
@lru_cache()
def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
return\
{
'styleganex_age':
{
'__metadata__':
{
'vendor': 'williamyang1991',
'license': 'S-Lab-1.0',
'year': 2023
},
'hashes':
{
'age_modifier':
{
'url': resolve_download_url('models-3.1.0', 'styleganex_age.hash'),
'path': resolve_relative_path('../.assets/models/styleganex_age.hash')
}
},
'sources':
{
'age_modifier':
{
'url': resolve_download_url('models-3.1.0', 'styleganex_age.onnx'),
'path': resolve_relative_path('../.assets/models/styleganex_age.onnx')
}
},
'templates':
{
'target': 'ffhq_512',
'target_with_background': 'styleganex_384'
},
'sizes':
{
'target': (256, 256),
'target_with_background': (384, 384)
}
}
}
def get_inference_pool() -> InferencePool:
model_names = [ state_manager.get_item('age_modifier_model') ]
model_source_set = get_model_options().get('sources')
return inference_manager.get_inference_pool(__name__, model_names, model_source_set)
def clear_inference_pool() -> None:
model_names = [ state_manager.get_item('age_modifier_model') ]
inference_manager.clear_inference_pool(__name__, model_names)
def get_model_options() -> ModelOptions:
model_name = state_manager.get_item('age_modifier_model')
return create_static_model_set('full').get(model_name)
def register_args(program : ArgumentParser) -> None:
group_processors = find_argument_group(program, 'processors')
if group_processors:
group_processors.add_argument('--age-modifier-model', help = translator.get('help.model', __package__), default = config.get_str_value('processors', 'age_modifier_model', 'styleganex_age'), choices = age_modifier_choices.age_modifier_models)
group_processors.add_argument('--age-modifier-direction', help = translator.get('help.direction', __package__), type = int, default = config.get_int_value('processors', 'age_modifier_direction', '0'), choices = age_modifier_choices.age_modifier_direction_range, metavar = create_int_metavar(age_modifier_choices.age_modifier_direction_range))
facefusion.jobs.job_store.register_step_keys([ 'age_modifier_model', 'age_modifier_direction' ])
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('age_modifier_model', args.get('age_modifier_model'))
apply_state_item('age_modifier_direction', args.get('age_modifier_direction'))
def pre_check() -> bool:
model_hash_set = get_model_options().get('hashes')
model_source_set = get_model_options().get('sources')
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
def pre_process(mode : ProcessMode) -> bool:
if mode in [ 'output', 'preview' ] and not is_image(state_manager.get_item('target_path')) and not is_video(state_manager.get_item('target_path')):
logger.error(translator.get('choose_image_or_video_target') + translator.get('exclamation_mark'), __name__)
return False
if mode == 'output' and not in_directory(state_manager.get_item('output_path')):
logger.error(translator.get('specify_image_or_video_output') + translator.get('exclamation_mark'), __name__)
return False
if mode == 'output' and not same_file_extension(state_manager.get_item('target_path'), state_manager.get_item('output_path')):
logger.error(translator.get('match_target_and_output_extension') + translator.get('exclamation_mark'), __name__)
return False
return True
def post_process() -> None:
read_static_image.cache_clear()
read_static_video_frame.cache_clear()
video_manager.clear_video_pool()
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
clear_inference_pool()
if state_manager.get_item('video_memory_strategy') == 'strict':
content_analyser.clear_inference_pool()
face_classifier.clear_inference_pool()
face_detector.clear_inference_pool()
face_landmarker.clear_inference_pool()
face_masker.clear_inference_pool()
face_recognizer.clear_inference_pool()
def modify_age(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
model_templates = get_model_options().get('templates')
model_sizes = get_model_options().get('sizes')
face_landmark_5 = target_face.landmark_set.get('5/68').copy()
crop_vision_frame, affine_matrix = warp_face_by_face_landmark_5(temp_vision_frame, face_landmark_5, model_templates.get('target'), model_sizes.get('target'))
extend_face_landmark_5 = scale_face_landmark_5(face_landmark_5, 0.875)
extend_vision_frame, extend_affine_matrix = warp_face_by_face_landmark_5(temp_vision_frame, extend_face_landmark_5, model_templates.get('target_with_background'), model_sizes.get('target_with_background'))
extend_vision_frame_raw = extend_vision_frame.copy()
box_mask = create_box_mask(extend_vision_frame, state_manager.get_item('face_mask_blur'), (0, 0, 0, 0))
crop_masks =\
[
box_mask
]
if 'occlusion' in state_manager.get_item('face_mask_types'):
occlusion_mask = create_occlusion_mask(crop_vision_frame)
temp_matrix = merge_matrix([ extend_affine_matrix, cv2.invertAffineTransform(affine_matrix) ])
occlusion_mask = cv2.warpAffine(occlusion_mask, temp_matrix, model_sizes.get('target_with_background'))
crop_masks.append(occlusion_mask)
crop_vision_frame = prepare_vision_frame(crop_vision_frame)
extend_vision_frame = prepare_vision_frame(extend_vision_frame)
age_modifier_direction = numpy.array(numpy.interp(state_manager.get_item('age_modifier_direction'), [ -100, 100 ], [ 2.5, -2.5 ])).astype(numpy.float32)
extend_vision_frame = forward(crop_vision_frame, extend_vision_frame, age_modifier_direction)
extend_vision_frame = normalize_extend_frame(extend_vision_frame)
extend_vision_frame = match_frame_color(extend_vision_frame_raw, extend_vision_frame)
extend_affine_matrix *= (model_sizes.get('target')[0] * 4) / model_sizes.get('target_with_background')[0]
crop_mask = numpy.minimum.reduce(crop_masks).clip(0, 1)
crop_mask = cv2.resize(crop_mask, (model_sizes.get('target')[0] * 4, model_sizes.get('target')[1] * 4))
paste_vision_frame = paste_back(temp_vision_frame, extend_vision_frame, crop_mask, extend_affine_matrix)
return paste_vision_frame
def forward(crop_vision_frame : VisionFrame, extend_vision_frame : VisionFrame, age_modifier_direction : AgeModifierDirection) -> VisionFrame:
age_modifier = get_inference_pool().get('age_modifier')
age_modifier_inputs = {}
if is_macos() and has_execution_provider('coreml'):
age_modifier.set_providers([ facefusion.choices.execution_provider_set.get('cpu') ])
for age_modifier_input in age_modifier.get_inputs():
if age_modifier_input.name == 'target':
age_modifier_inputs[age_modifier_input.name] = crop_vision_frame
if age_modifier_input.name == 'target_with_background':
age_modifier_inputs[age_modifier_input.name] = extend_vision_frame
if age_modifier_input.name == 'direction':
age_modifier_inputs[age_modifier_input.name] = age_modifier_direction
with thread_semaphore():
crop_vision_frame = age_modifier.run(None, age_modifier_inputs)[0][0]
return crop_vision_frame
def prepare_vision_frame(vision_frame : VisionFrame) -> VisionFrame:
vision_frame = vision_frame[:, :, ::-1] / 255.0
vision_frame = (vision_frame - 0.5) / 0.5
vision_frame = numpy.expand_dims(vision_frame.transpose(2, 0, 1), axis = 0).astype(numpy.float32)
return vision_frame
def normalize_extend_frame(extend_vision_frame : VisionFrame) -> VisionFrame:
model_sizes = get_model_options().get('sizes')
extend_vision_frame = numpy.clip(extend_vision_frame, -1, 1)
extend_vision_frame = (extend_vision_frame + 1) / 2
extend_vision_frame = extend_vision_frame.transpose(1, 2, 0).clip(0, 255)
extend_vision_frame = (extend_vision_frame * 255.0)
extend_vision_frame = extend_vision_frame.astype(numpy.uint8)[:, :, ::-1]
extend_vision_frame = cv2.resize(extend_vision_frame, (model_sizes.get('target')[0] * 4, model_sizes.get('target')[1] * 4), interpolation = cv2.INTER_AREA)
return extend_vision_frame
def process_frame(inputs : AgeModifierInputs) -> ProcessorOutputs:
reference_vision_frame = inputs.get('reference_vision_frame')
target_vision_frame = inputs.get('target_vision_frame')
temp_vision_frame = inputs.get('temp_vision_frame')
temp_vision_mask = inputs.get('temp_vision_mask')
target_faces = select_faces(reference_vision_frame, target_vision_frame)
if target_faces:
for target_face in target_faces:
target_face = scale_face(target_face, target_vision_frame, temp_vision_frame)
temp_vision_frame = modify_age(target_face, temp_vision_frame)
return temp_vision_frame, temp_vision_mask

View File

@@ -1,18 +0,0 @@
from facefusion.types import Locals
LOCALS : Locals =\
{
'en':
{
'help':
{
'model': 'choose the model responsible for aging the face',
'direction': 'specify the direction in which the age should be modified'
},
'uis':
{
'direction_slider': 'AGE MODIFIER DIRECTION',
'model_dropdown': 'AGE MODIFIER MODEL'
}
}
}

View File

@@ -1,17 +0,0 @@
from typing import Any, Literal, TypeAlias, TypedDict
from numpy.typing import NDArray
from facefusion.types import Mask, VisionFrame
AgeModifierInputs = TypedDict('AgeModifierInputs',
{
'reference_vision_frame' : VisionFrame,
'target_vision_frame' : VisionFrame,
'temp_vision_frame' : VisionFrame,
'temp_vision_mask' : Mask
})
AgeModifierModel = Literal['styleganex_age']
AgeModifierDirection : TypeAlias = NDArray[Any]

View File

@@ -1,8 +0,0 @@
from typing import List, Sequence
from facefusion.common_helper import create_int_range
from facefusion.processors.modules.background_remover.types import BackgroundRemoverModel
background_remover_models : List[BackgroundRemoverModel] = [ 'ben_2', 'birefnet_general', 'birefnet_portrait', 'isnet_general', 'modnet', 'ormbg', 'rmbg_1.4', 'rmbg_2.0', 'silueta', 'u2net_cloth', 'u2net_general', 'u2net_human', 'u2netp' ]
background_remover_color_range : Sequence[int] = create_int_range(0, 255, 1)

View File

@@ -1,524 +0,0 @@
from argparse import ArgumentParser
from functools import lru_cache, partial
from typing import List, Tuple
import cv2
import numpy
import facefusion.jobs.job_manager
import facefusion.jobs.job_store
from facefusion import config, content_analyser, inference_manager, logger, state_manager, translator, video_manager
from facefusion.common_helper import is_macos
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
from facefusion.execution import has_execution_provider
from facefusion.filesystem import in_directory, is_image, is_video, resolve_relative_path, same_file_extension
from facefusion.normalizer import normalize_color
from facefusion.processors.modules.background_remover import choices as background_remover_choices
from facefusion.processors.modules.background_remover.types import BackgroundRemoverInputs
from facefusion.processors.types import ProcessorOutputs
from facefusion.program_helper import find_argument_group
from facefusion.sanitizer import sanitize_int_range
from facefusion.thread_helper import thread_semaphore
from facefusion.types import ApplyStateItem, Args, DownloadScope, ExecutionProvider, InferencePool, Mask, ModelOptions, ModelSet, ProcessMode, VisionFrame
from facefusion.vision import read_static_image, read_static_video_frame
@lru_cache()
def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
return\
{
'ben_2':
{
'__metadata__':
{
'vendor': 'PramaLLC',
'license': 'MIT',
'year': 2025
},
'hashes':
{
'background_remover':
{
'url': resolve_download_url('models-3.5.0', 'ben_2.hash'),
'path': resolve_relative_path('../.assets/models/ben_2.hash')
}
},
'sources':
{
'background_remover':
{
'url': resolve_download_url('models-3.5.0', 'ben_2.onnx'),
'path': resolve_relative_path('../.assets/models/ben_2.onnx')
}
},
'size': (1024, 1024),
'mean': [ 0.0, 0.0, 0.0 ],
'standard_deviation': [ 1.0, 1.0, 1.0 ]
},
'birefnet_general':
{
'__metadata__':
{
'vendor': 'ZhengPeng7',
'license': 'MIT',
'year': 2024
},
'hashes':
{
'background_remover':
{
'url': resolve_download_url('models-3.5.0', 'birefnet_general.hash'),
'path': resolve_relative_path('../.assets/models/birefnet_general.hash')
}
},
'sources':
{
'background_remover':
{
'url': resolve_download_url('models-3.5.0', 'birefnet_general.onnx'),
'path': resolve_relative_path('../.assets/models/birefnet_general.onnx')
}
},
'size': (1024, 1024),
'mean': [ 0.0, 0.0, 0.0 ],
'standard_deviation': [ 1.0, 1.0, 1.0 ]
},
'birefnet_portrait':
{
'__metadata__':
{
'vendor': 'ZhengPeng7',
'license': 'MIT',
'year': 2024
},
'hashes':
{
'background_remover':
{
'url': resolve_download_url('models-3.5.0', 'birefnet_portrait.hash'),
'path': resolve_relative_path('../.assets/models/birefnet_portrait.hash')
}
},
'sources':
{
'background_remover':
{
'url': resolve_download_url('models-3.5.0', 'birefnet_portrait.onnx'),
'path': resolve_relative_path('../.assets/models/birefnet_portrait.onnx')
}
},
'size': (1024, 1024),
'mean': [ 0.0, 0.0, 0.0 ],
'standard_deviation': [ 1.0, 1.0, 1.0 ]
},
'isnet_general':
{
'__metadata__':
{
'vendor': 'xuebinqin',
'license': 'Apache-2.0',
'year': 2022
},
'hashes':
{
'background_remover':
{
'url': resolve_download_url('models-3.5.0', 'isnet_general.hash'),
'path': resolve_relative_path('../.assets/models/isnet_general.hash')
}
},
'sources':
{
'background_remover':
{
'url': resolve_download_url('models-3.5.0', 'isnet_general.onnx'),
'path': resolve_relative_path('../.assets/models/isnet_general.onnx')
}
},
'size': (1024, 1024),
'mean': [ 0.5, 0.5, 0.5 ],
'standard_deviation': [ 1.0, 1.0, 1.0 ]
},
'modnet':
{
'__metadata__':
{
'vendor': 'ZHKKKe',
'license': 'Apache-2.0',
'year': 2020
},
'hashes':
{
'background_remover':
{
'url': resolve_download_url('models-3.5.0', 'modnet.hash'),
'path': resolve_relative_path('../.assets/models/modnet.hash')
}
},
'sources':
{
'background_remover':
{
'url': resolve_download_url('models-3.5.0', 'modnet.onnx'),
'path': resolve_relative_path('../.assets/models/modnet.onnx')
}
},
'size': (512, 512),
'mean': [ 0.5, 0.5, 0.5 ],
'standard_deviation': [ 0.5, 0.5, 0.5 ]
},
'ormbg':
{
'__metadata__':
{
'vendor': 'schirrmacher',
'license': 'Apache-2.0',
'year': 2024
},
'hashes':
{
'background_remover':
{
'url': resolve_download_url('models-3.5.0', 'ormbg.hash'),
'path': resolve_relative_path('../.assets/models/ormbg.hash')
}
},
'sources':
{
'background_remover':
{
'url': resolve_download_url('models-3.5.0', 'ormbg.onnx'),
'path': resolve_relative_path('../.assets/models/ormbg.onnx')
}
},
'size': (1024, 1024),
'mean': [ 0.0, 0.0, 0.0 ],
'standard_deviation': [ 1.0, 1.0, 1.0 ]
},
'rmbg_1.4':
{
'__metadata__':
{
'vendor': 'Bria',
'license': 'Non-Commercial',
'year': 2023
},
'hashes':
{
'background_remover':
{
'url': resolve_download_url('models-3.5.0', 'rmbg_1.4.hash'),
'path': resolve_relative_path('../.assets/models/rmbg_1.4.hash')
}
},
'sources':
{
'background_remover':
{
'url': resolve_download_url('models-3.5.0', 'rmbg_1.4.onnx'),
'path': resolve_relative_path('../.assets/models/rmbg_1.4.onnx')
}
},
'size': (1024, 1024),
'mean': [ 0.5, 0.5, 0.5 ],
'standard_deviation': [ 1.0, 1.0, 1.0 ]
},
'rmbg_2.0':
{
'__metadata__':
{
'vendor': 'Bria',
'license': 'Non-Commercial',
'year': 2024
},
'hashes':
{
'background_remover':
{
'url': resolve_download_url('models-3.5.0', 'rmbg_2.0.hash'),
'path': resolve_relative_path('../.assets/models/rmbg_2.0.hash')
}
},
'sources':
{
'background_remover':
{
'url': resolve_download_url('models-3.5.0', 'rmbg_2.0.onnx'),
'path': resolve_relative_path('../.assets/models/rmbg_2.0.onnx')
}
},
'size': (1024, 1024),
'mean': [ 0.485, 0.456, 0.406 ],
'standard_deviation': [ 0.229, 0.224, 0.225 ]
},
'silueta':
{
'__metadata__':
{
'vendor': 'Kikedao',
'license': 'Apache-2.0',
'year': 2022
},
'hashes':
{
'background_remover':
{
'url': resolve_download_url('models-3.5.0', 'silueta.hash'),
'path': resolve_relative_path('../.assets/models/silueta.hash')
}
},
'sources':
{
'background_remover':
{
'url': resolve_download_url('models-3.5.0', 'silueta.onnx'),
'path': resolve_relative_path('../.assets/models/silueta.onnx')
}
},
'size': (320, 320),
'mean': [ 0.485, 0.456, 0.406 ],
'standard_deviation': [ 0.229, 0.224, 0.225 ]
},
'u2net_cloth':
{
'__metadata__':
{
'vendor': 'levindabhi',
'license': 'MIT',
'year': 2021
},
'hashes':
{
'background_remover':
{
'url': resolve_download_url('models-3.5.0', 'u2net_cloth.hash'),
'path': resolve_relative_path('../.assets/models/u2net_cloth.hash')
}
},
'sources':
{
'background_remover':
{
'url': resolve_download_url('models-3.5.0', 'u2net_cloth.onnx'),
'path': resolve_relative_path('../.assets/models/u2net_cloth.onnx')
}
},
'size': (768, 768),
'mean': [ 0.485, 0.456, 0.406 ],
'standard_deviation': [ 0.229, 0.224, 0.225 ]
},
'u2net_general':
{
'__metadata__':
{
'vendor': 'xuebinqin',
'license': 'Apache-2.0',
'year': 2020
},
'hashes':
{
'background_remover':
{
'url': resolve_download_url('models-3.5.0', 'u2net_general.hash'),
'path': resolve_relative_path('../.assets/models/u2net_general.hash')
}
},
'sources':
{
'background_remover':
{
'url': resolve_download_url('models-3.5.0', 'u2net_general.onnx'),
'path': resolve_relative_path('../.assets/models/u2net_general.onnx')
}
},
'size': (320, 320),
'mean': [ 0.485, 0.456, 0.406 ],
'standard_deviation': [ 0.229, 0.224, 0.225 ]
},
'u2net_human':
{
'__metadata__':
{
'vendor': 'xuebinqin',
'license': 'Apache-2.0',
'year': 2021
},
'hashes':
{
'background_remover':
{
'url': resolve_download_url('models-3.5.0', 'u2net_human.hash'),
'path': resolve_relative_path('../.assets/models/u2net_human.hash')
}
},
'sources':
{
'background_remover':
{
'url': resolve_download_url('models-3.5.0', 'u2net_human.onnx'),
'path': resolve_relative_path('../.assets/models/u2net_human.onnx')
}
},
'size': (320, 320),
'mean': [ 0.485, 0.456, 0.406 ],
'standard_deviation': [ 0.229, 0.224, 0.225 ]
},
'u2netp':
{
'__metadata__':
{
'vendor': 'xuebinqin',
'license': 'Apache-2.0',
'year': 2021
},
'hashes':
{
'background_remover':
{
'url': resolve_download_url('models-3.5.0', 'u2netp.hash'),
'path': resolve_relative_path('../.assets/models/u2netp.hash')
}
},
'sources':
{
'background_remover':
{
'url': resolve_download_url('models-3.5.0', 'u2netp.onnx'),
'path': resolve_relative_path('../.assets/models/u2netp.onnx')
}
},
'size': (320, 320),
'mean': [ 0.485, 0.456, 0.406 ],
'standard_deviation': [ 0.229, 0.224, 0.225 ]
}
}
def get_inference_pool() -> InferencePool:
model_names = [ state_manager.get_item('background_remover_model') ]
model_source_set = get_model_options().get('sources')
return inference_manager.get_inference_pool(__name__, model_names, model_source_set)
def clear_inference_pool() -> None:
model_names = [ state_manager.get_item('background_remover_model') ]
inference_manager.clear_inference_pool(__name__, model_names)
def resolve_execution_providers() -> List[ExecutionProvider]:
if is_macos() and has_execution_provider('coreml'):
return [ 'cpu' ]
return state_manager.get_item('execution_providers')
def get_model_options() -> ModelOptions:
model_name = state_manager.get_item('background_remover_model')
return create_static_model_set('full').get(model_name)
def register_args(program : ArgumentParser) -> None:
group_processors = find_argument_group(program, 'processors')
if group_processors:
group_processors.add_argument('--background-remover-model', help = translator.get('help.model', __package__), default = config.get_str_value('processors', 'background_remover_model', 'rmbg_2.0'), choices = background_remover_choices.background_remover_models)
group_processors.add_argument('--background-remover-color', help = translator.get('help.color', __package__), type = partial(sanitize_int_range, int_range = background_remover_choices.background_remover_color_range), default = config.get_int_list('processors', 'background_remover_color', '0 0 0 0'), nargs ='+')
facefusion.jobs.job_store.register_step_keys([ 'background_remover_model', 'background_remover_color' ])
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('background_remover_model', args.get('background_remover_model'))
apply_state_item('background_remover_color', normalize_color(args.get('background_remover_color')))
def pre_check() -> bool:
model_hash_set = get_model_options().get('hashes')
model_source_set = get_model_options().get('sources')
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
def pre_process(mode : ProcessMode) -> bool:
if mode in [ 'output', 'preview' ] and not is_image(state_manager.get_item('target_path')) and not is_video(state_manager.get_item('target_path')):
logger.error(translator.get('choose_image_or_video_target') + translator.get('exclamation_mark'), __name__)
return False
if mode == 'output' and not in_directory(state_manager.get_item('output_path')):
logger.error(translator.get('specify_image_or_video_output') + translator.get('exclamation_mark'), __name__)
return False
if mode == 'output' and not same_file_extension(state_manager.get_item('target_path'), state_manager.get_item('output_path')):
logger.error(translator.get('match_target_and_output_extension') + translator.get('exclamation_mark'), __name__)
return False
return True
def post_process() -> None:
read_static_image.cache_clear()
read_static_video_frame.cache_clear()
video_manager.clear_video_pool()
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
clear_inference_pool()
if state_manager.get_item('video_memory_strategy') == 'strict':
content_analyser.clear_inference_pool()
def remove_background(temp_vision_frame : VisionFrame) -> Tuple[VisionFrame, Mask]:
temp_vision_mask = forward(prepare_temp_frame(temp_vision_frame))
temp_vision_mask = normalize_vision_mask(temp_vision_mask)
temp_vision_mask = cv2.resize(temp_vision_mask, temp_vision_frame.shape[:2][::-1])
temp_vision_frame = apply_background_color(temp_vision_frame, temp_vision_mask)
return temp_vision_frame, temp_vision_mask
def forward(temp_vision_frame : VisionFrame) -> VisionFrame:
background_remover = get_inference_pool().get('background_remover')
model_name = state_manager.get_item('background_remover_model')
with thread_semaphore():
remove_vision_frame = background_remover.run(None,
{
'input': temp_vision_frame
})[0]
if model_name == 'u2net_cloth':
remove_vision_frame = numpy.argmax(remove_vision_frame, axis = 1)
return remove_vision_frame
def prepare_temp_frame(temp_vision_frame : VisionFrame) -> VisionFrame:
model_size = get_model_options().get('size')
model_mean = get_model_options().get('mean')
model_standard_deviation = get_model_options().get('standard_deviation')
temp_vision_frame = cv2.resize(temp_vision_frame, model_size)
temp_vision_frame = temp_vision_frame[:, :, ::-1] / 255.0
temp_vision_frame = (temp_vision_frame - model_mean) / model_standard_deviation
temp_vision_frame = temp_vision_frame.transpose(2, 0, 1)
temp_vision_frame = numpy.expand_dims(temp_vision_frame, axis = 0).astype(numpy.float32)
return temp_vision_frame
def normalize_vision_mask(temp_vision_mask : Mask) -> Mask:
temp_vision_mask = numpy.squeeze(temp_vision_mask).clip(0, 1) * 255
temp_vision_mask = numpy.clip(temp_vision_mask, 0, 255).astype(numpy.uint8)
return temp_vision_mask
def apply_background_color(temp_vision_frame : VisionFrame, temp_vision_mask : Mask) -> VisionFrame:
background_remover_color = state_manager.get_item('background_remover_color')
temp_vision_mask = temp_vision_mask.astype(numpy.float32) / 255
temp_vision_mask = numpy.expand_dims(temp_vision_mask, axis = 2)
temp_vision_mask = (1 - temp_vision_mask) * background_remover_color[-1] / 255
color_frame = numpy.zeros_like(temp_vision_frame)
color_frame[:, :, 0] = background_remover_color[2]
color_frame[:, :, 1] = background_remover_color[1]
color_frame[:, :, 2] = background_remover_color[0]
temp_vision_frame = temp_vision_frame * (1 - temp_vision_mask) + color_frame * temp_vision_mask
temp_vision_frame = temp_vision_frame.astype(numpy.uint8)
return temp_vision_frame
def process_frame(inputs : BackgroundRemoverInputs) -> ProcessorOutputs:
temp_vision_frame = inputs.get('temp_vision_frame')
temp_vision_frame, temp_vision_mask = remove_background(temp_vision_frame)
temp_vision_mask = numpy.minimum.reduce([ temp_vision_mask, inputs.get('temp_vision_mask') ])
return temp_vision_frame, temp_vision_mask

View File

@@ -1,21 +0,0 @@
from facefusion.types import Locals
LOCALS : Locals =\
{
'en':
{
'help':
{
'model': 'choose the model responsible for removing the background',
'color': 'apply red, green blue and alpha values to the background'
},
'uis':
{
'model_dropdown': 'BACKGROUND REMOVER MODEL',
'color_red_number': 'BACKGROUND COLOR RED',
'color_green_number': 'BACKGROUND COLOR GREEN',
'color_blue_number': 'BACKGROUND COLOR BLUE',
'color_alpha_number': 'BACKGROUND COLOR ALPHA'
}
}
}

View File

@@ -1,12 +0,0 @@
from typing import Literal, TypedDict
from facefusion.types import Mask, VisionFrame
BackgroundRemoverInputs = TypedDict('BackgroundRemoverInputs',
{
'target_vision_frame' : VisionFrame,
'temp_vision_frame' : VisionFrame,
'temp_vision_mask' : Mask
})
BackgroundRemoverModel = Literal['ben_2', 'birefnet_general', 'birefnet_portrait', 'isnet_general', 'modnet', 'ormbg', 'rmbg_1.4', 'rmbg_2.0', 'silueta', 'u2net_cloth', 'u2net_general', 'u2net_human', 'u2netp']

View File

@@ -1,176 +0,0 @@
from typing import List, Sequence
from facefusion.common_helper import create_int_range
from facefusion.filesystem import get_file_name, resolve_file_paths, resolve_relative_path
from facefusion.processors.modules.deep_swapper.types import DeepSwapperModel
deep_swapper_models : List[DeepSwapperModel] =\
[
'druuzil/adam_levine_320',
'druuzil/adrianne_palicki_384',
'druuzil/agnetha_falskog_224',
'druuzil/alan_ritchson_320',
'druuzil/alicia_vikander_320',
'druuzil/amber_midthunder_320',
'druuzil/andras_arato_384',
'druuzil/andrew_tate_320',
'druuzil/angelina_jolie_384',
'druuzil/anne_hathaway_320',
'druuzil/anya_chalotra_320',
'druuzil/arnold_schwarzenegger_320',
'druuzil/benjamin_affleck_320',
'druuzil/benjamin_stiller_384',
'druuzil/bradley_pitt_224',
'druuzil/brie_larson_384',
'druuzil/bruce_campbell_384',
'druuzil/bryan_cranston_320',
'druuzil/catherine_blanchett_352',
'druuzil/christian_bale_320',
'druuzil/christopher_hemsworth_320',
'druuzil/christoph_waltz_384',
'druuzil/cillian_murphy_320',
'druuzil/cobie_smulders_256',
'druuzil/dwayne_johnson_384',
'druuzil/edward_norton_320',
'druuzil/elisabeth_shue_320',
'druuzil/elizabeth_olsen_384',
'druuzil/elon_musk_320',
'druuzil/emily_blunt_320',
'druuzil/emma_stone_384',
'druuzil/emma_watson_320',
'druuzil/erin_moriarty_384',
'druuzil/eva_green_320',
'druuzil/ewan_mcgregor_320',
'druuzil/florence_pugh_320',
'druuzil/freya_allan_320',
'druuzil/gary_cole_224',
'druuzil/gigi_hadid_224',
'druuzil/harrison_ford_384',
'druuzil/hayden_christensen_320',
'druuzil/heath_ledger_320',
'druuzil/henry_cavill_448',
'druuzil/hugh_jackman_384',
'druuzil/idris_elba_320',
'druuzil/jack_nicholson_320',
'druuzil/james_carrey_384',
'druuzil/james_mcavoy_320',
'druuzil/james_varney_320',
'druuzil/jason_momoa_320',
'druuzil/jason_statham_320',
'druuzil/jennifer_connelly_384',
'druuzil/jimmy_donaldson_320',
'druuzil/jordan_peterson_384',
'druuzil/karl_urban_224',
'druuzil/kate_beckinsale_384',
'druuzil/laurence_fishburne_384',
'druuzil/lili_reinhart_320',
'druuzil/luke_evans_384',
'druuzil/mads_mikkelsen_384',
'druuzil/mary_winstead_320',
'druuzil/margaret_qualley_384',
'druuzil/melina_juergens_320',
'druuzil/michael_fassbender_320',
'druuzil/michael_fox_320',
'druuzil/millie_bobby_brown_320',
'druuzil/morgan_freeman_320',
'druuzil/patrick_stewart_224',
'druuzil/rachel_weisz_384',
'druuzil/rebecca_ferguson_320',
'druuzil/scarlett_johansson_320',
'druuzil/shannen_doherty_384',
'druuzil/seth_macfarlane_384',
'druuzil/thomas_cruise_320',
'druuzil/thomas_hanks_384',
'druuzil/william_murray_384',
'druuzil/zoe_saldana_384',
'edel/emma_roberts_224',
'edel/ivanka_trump_224',
'edel/lize_dzjabrailova_224',
'edel/sidney_sweeney_224',
'edel/winona_ryder_224',
'iperov/alexandra_daddario_224',
'iperov/alexei_navalny_224',
'iperov/amber_heard_224',
'iperov/dilraba_dilmurat_224',
'iperov/elon_musk_224',
'iperov/emilia_clarke_224',
'iperov/emma_watson_224',
'iperov/erin_moriarty_224',
'iperov/jackie_chan_224',
'iperov/james_carrey_224',
'iperov/jason_statham_320',
'iperov/keanu_reeves_320',
'iperov/margot_robbie_224',
'iperov/natalie_dormer_224',
'iperov/nicolas_coppola_224',
'iperov/robert_downey_224',
'iperov/rowan_atkinson_224',
'iperov/ryan_reynolds_224',
'iperov/scarlett_johansson_224',
'iperov/sylvester_stallone_224',
'iperov/thomas_cruise_224',
'iperov/thomas_holland_224',
'iperov/vin_diesel_224',
'iperov/vladimir_putin_224',
'jen/angelica_trae_288',
'jen/ella_freya_224',
'jen/emma_myers_320',
'jen/evie_pickerill_224',
'jen/kang_hyewon_320',
'jen/maddie_mead_224',
'jen/nicole_turnbull_288',
'mats/alica_schmidt_320',
'mats/ashley_alexiss_224',
'mats/billie_eilish_224',
'mats/brie_larson_224',
'mats/cara_delevingne_224',
'mats/carolin_kebekus_224',
'mats/chelsea_clinton_224',
'mats/claire_boucher_224',
'mats/corinna_kopf_224',
'mats/florence_pugh_224',
'mats/hillary_clinton_224',
'mats/jenna_fischer_224',
'mats/kim_jisoo_320',
'mats/mica_suarez_320',
'mats/shailene_woodley_224',
'mats/shraddha_kapoor_320',
'mats/yu_jimin_352',
'rumateus/alison_brie_224',
'rumateus/amber_heard_224',
'rumateus/angelina_jolie_224',
'rumateus/aubrey_plaza_224',
'rumateus/bridget_regan_224',
'rumateus/cobie_smulders_224',
'rumateus/deborah_woll_224',
'rumateus/dua_lipa_224',
'rumateus/emma_stone_224',
'rumateus/hailee_steinfeld_224',
'rumateus/hilary_duff_224',
'rumateus/jessica_alba_224',
'rumateus/jessica_biel_224',
'rumateus/john_cena_224',
'rumateus/kim_kardashian_224',
'rumateus/kristen_bell_224',
'rumateus/lucy_liu_224',
'rumateus/margot_robbie_224',
'rumateus/megan_fox_224',
'rumateus/meghan_markle_224',
'rumateus/millie_bobby_brown_224',
'rumateus/natalie_portman_224',
'rumateus/nicki_minaj_224',
'rumateus/olivia_wilde_224',
'rumateus/shay_mitchell_224',
'rumateus/sophie_turner_224',
'rumateus/taylor_swift_224'
]
custom_model_file_paths = resolve_file_paths(resolve_relative_path('../.assets/models/custom'))
if custom_model_file_paths:
for model_file_path in custom_model_file_paths:
model_id = '/'.join([ 'custom', get_file_name(model_file_path) ])
deep_swapper_models.append(model_id)
deep_swapper_morph_range : Sequence[int] = create_int_range(0, 100, 1)

View File

@@ -1,424 +0,0 @@
from argparse import ArgumentParser
from functools import lru_cache
from typing import Tuple
import cv2
import numpy
from cv2.typing import Size
import facefusion.jobs.job_manager
import facefusion.jobs.job_store
from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, inference_manager, logger, state_manager, translator, video_manager
from facefusion.common_helper import create_int_metavar
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url_by_provider
from facefusion.face_analyser import scale_face
from facefusion.face_helper import paste_back, warp_face_by_face_landmark_5
from facefusion.face_masker import create_area_mask, create_box_mask, create_occlusion_mask, create_region_mask
from facefusion.face_selector import select_faces
from facefusion.filesystem import get_file_name, in_directory, is_image, is_video, resolve_file_paths, resolve_relative_path, same_file_extension
from facefusion.processors.modules.deep_swapper import choices as deep_swapper_choices
from facefusion.processors.modules.deep_swapper.types import DeepSwapperInputs, DeepSwapperMorph
from facefusion.processors.types import ProcessorOutputs
from facefusion.program_helper import find_argument_group
from facefusion.thread_helper import thread_semaphore
from facefusion.types import ApplyStateItem, Args, DownloadScope, Face, InferencePool, Mask, ModelOptions, ModelSet, ProcessMode, VisionFrame
from facefusion.vision import conditional_match_frame_color, read_static_image, read_static_video_frame
@lru_cache()
def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
model_config = []
if download_scope == 'full':
model_config.extend(
[
('druuzil', 'adam_levine_320'),
('druuzil', 'adrianne_palicki_384'),
('druuzil', 'agnetha_falskog_224'),
('druuzil', 'alan_ritchson_320'),
('druuzil', 'alicia_vikander_320'),
('druuzil', 'amber_midthunder_320'),
('druuzil', 'andras_arato_384'),
('druuzil', 'andrew_tate_320'),
('druuzil', 'angelina_jolie_384'),
('druuzil', 'anne_hathaway_320'),
('druuzil', 'anya_chalotra_320'),
('druuzil', 'arnold_schwarzenegger_320'),
('druuzil', 'benjamin_affleck_320'),
('druuzil', 'benjamin_stiller_384'),
('druuzil', 'bradley_pitt_224'),
('druuzil', 'brie_larson_384'),
('druuzil', 'bruce_campbell_384'),
('druuzil', 'bryan_cranston_320'),
('druuzil', 'catherine_blanchett_352'),
('druuzil', 'christian_bale_320'),
('druuzil', 'christopher_hemsworth_320'),
('druuzil', 'christoph_waltz_384'),
('druuzil', 'cillian_murphy_320'),
('druuzil', 'cobie_smulders_256'),
('druuzil', 'dwayne_johnson_384'),
('druuzil', 'edward_norton_320'),
('druuzil', 'elisabeth_shue_320'),
('druuzil', 'elizabeth_olsen_384'),
('druuzil', 'elon_musk_320'),
('druuzil', 'emily_blunt_320'),
('druuzil', 'emma_stone_384'),
('druuzil', 'emma_watson_320'),
('druuzil', 'erin_moriarty_384'),
('druuzil', 'eva_green_320'),
('druuzil', 'ewan_mcgregor_320'),
('druuzil', 'florence_pugh_320'),
('druuzil', 'freya_allan_320'),
('druuzil', 'gary_cole_224'),
('druuzil', 'gigi_hadid_224'),
('druuzil', 'harrison_ford_384'),
('druuzil', 'hayden_christensen_320'),
('druuzil', 'heath_ledger_320'),
('druuzil', 'henry_cavill_448'),
('druuzil', 'hugh_jackman_384'),
('druuzil', 'idris_elba_320'),
('druuzil', 'jack_nicholson_320'),
('druuzil', 'james_carrey_384'),
('druuzil', 'james_mcavoy_320'),
('druuzil', 'james_varney_320'),
('druuzil', 'jason_momoa_320'),
('druuzil', 'jason_statham_320'),
('druuzil', 'jennifer_connelly_384'),
('druuzil', 'jimmy_donaldson_320'),
('druuzil', 'jordan_peterson_384'),
('druuzil', 'karl_urban_224'),
('druuzil', 'kate_beckinsale_384'),
('druuzil', 'laurence_fishburne_384'),
('druuzil', 'lili_reinhart_320'),
('druuzil', 'luke_evans_384'),
('druuzil', 'mads_mikkelsen_384'),
('druuzil', 'mary_winstead_320'),
('druuzil', 'margaret_qualley_384'),
('druuzil', 'melina_juergens_320'),
('druuzil', 'michael_fassbender_320'),
('druuzil', 'michael_fox_320'),
('druuzil', 'millie_bobby_brown_320'),
('druuzil', 'morgan_freeman_320'),
('druuzil', 'patrick_stewart_224'),
('druuzil', 'rachel_weisz_384'),
('druuzil', 'rebecca_ferguson_320'),
('druuzil', 'scarlett_johansson_320'),
('druuzil', 'shannen_doherty_384'),
('druuzil', 'seth_macfarlane_384'),
('druuzil', 'thomas_cruise_320'),
('druuzil', 'thomas_hanks_384'),
('druuzil', 'william_murray_384'),
('druuzil', 'zoe_saldana_384'),
('edel', 'emma_roberts_224'),
('edel', 'ivanka_trump_224'),
('edel', 'lize_dzjabrailova_224'),
('edel', 'sidney_sweeney_224'),
('edel', 'winona_ryder_224')
])
if download_scope in [ 'lite', 'full' ]:
model_config.extend(
[
('iperov', 'alexandra_daddario_224'),
('iperov', 'alexei_navalny_224'),
('iperov', 'amber_heard_224'),
('iperov', 'dilraba_dilmurat_224'),
('iperov', 'elon_musk_224'),
('iperov', 'emilia_clarke_224'),
('iperov', 'emma_watson_224'),
('iperov', 'erin_moriarty_224'),
('iperov', 'jackie_chan_224'),
('iperov', 'james_carrey_224'),
('iperov', 'jason_statham_320'),
('iperov', 'keanu_reeves_320'),
('iperov', 'margot_robbie_224'),
('iperov', 'natalie_dormer_224'),
('iperov', 'nicolas_coppola_224'),
('iperov', 'robert_downey_224'),
('iperov', 'rowan_atkinson_224'),
('iperov', 'ryan_reynolds_224'),
('iperov', 'scarlett_johansson_224'),
('iperov', 'sylvester_stallone_224'),
('iperov', 'thomas_cruise_224'),
('iperov', 'thomas_holland_224'),
('iperov', 'vin_diesel_224'),
('iperov', 'vladimir_putin_224')
])
if download_scope == 'full':
model_config.extend(
[
('jen', 'angelica_trae_288'),
('jen', 'ella_freya_224'),
('jen', 'emma_myers_320'),
('jen', 'evie_pickerill_224'),
('jen', 'kang_hyewon_320'),
('jen', 'maddie_mead_224'),
('jen', 'nicole_turnbull_288'),
('mats', 'alica_schmidt_320'),
('mats', 'ashley_alexiss_224'),
('mats', 'billie_eilish_224'),
('mats', 'brie_larson_224'),
('mats', 'cara_delevingne_224'),
('mats', 'carolin_kebekus_224'),
('mats', 'chelsea_clinton_224'),
('mats', 'claire_boucher_224'),
('mats', 'corinna_kopf_224'),
('mats', 'florence_pugh_224'),
('mats', 'hillary_clinton_224'),
('mats', 'jenna_fischer_224'),
('mats', 'kim_jisoo_320'),
('mats', 'mica_suarez_320'),
('mats', 'shailene_woodley_224'),
('mats', 'shraddha_kapoor_320'),
('mats', 'yu_jimin_352'),
('rumateus', 'alison_brie_224'),
('rumateus', 'amber_heard_224'),
('rumateus', 'angelina_jolie_224'),
('rumateus', 'aubrey_plaza_224'),
('rumateus', 'bridget_regan_224'),
('rumateus', 'cobie_smulders_224'),
('rumateus', 'deborah_woll_224'),
('rumateus', 'dua_lipa_224'),
('rumateus', 'emma_stone_224'),
('rumateus', 'hailee_steinfeld_224'),
('rumateus', 'hilary_duff_224'),
('rumateus', 'jessica_alba_224'),
('rumateus', 'jessica_biel_224'),
('rumateus', 'john_cena_224'),
('rumateus', 'kim_kardashian_224'),
('rumateus', 'kristen_bell_224'),
('rumateus', 'lucy_liu_224'),
('rumateus', 'margot_robbie_224'),
('rumateus', 'megan_fox_224'),
('rumateus', 'meghan_markle_224'),
('rumateus', 'millie_bobby_brown_224'),
('rumateus', 'natalie_portman_224'),
('rumateus', 'nicki_minaj_224'),
('rumateus', 'olivia_wilde_224'),
('rumateus', 'shay_mitchell_224'),
('rumateus', 'sophie_turner_224'),
('rumateus', 'taylor_swift_224')
])
model_set : ModelSet = {}
for model_scope, model_name in model_config:
model_id = '/'.join([ model_scope, model_name ])
model_set[model_id] =\
{
'hashes':
{
'deep_swapper':
{
'url': resolve_download_url_by_provider('huggingface', 'deepfacelive-models-' + model_scope, model_name + '.hash'),
'path': resolve_relative_path('../.assets/models/' + model_scope + '/' + model_name + '.hash')
}
},
'sources':
{
'deep_swapper':
{
'url': resolve_download_url_by_provider('huggingface', 'deepfacelive-models-' + model_scope, model_name + '.dfm'),
'path': resolve_relative_path('../.assets/models/' + model_scope + '/' + model_name + '.dfm')
}
},
'template': 'dfl_whole_face'
}
custom_model_file_paths = resolve_file_paths(resolve_relative_path('../.assets/models/custom'))
if custom_model_file_paths:
for model_file_path in custom_model_file_paths:
model_id = '/'.join([ 'custom', get_file_name(model_file_path) ])
model_set[model_id] =\
{
'sources':
{
'deep_swapper':
{
'path': resolve_relative_path(model_file_path)
}
},
'template': 'dfl_whole_face'
}
return model_set
def get_inference_pool() -> InferencePool:
model_names = [ state_manager.get_item('deep_swapper_model') ]
model_source_set = get_model_options().get('sources')
return inference_manager.get_inference_pool(__name__, model_names, model_source_set)
def clear_inference_pool() -> None:
model_names = [ state_manager.get_item('deep_swapper_model') ]
inference_manager.clear_inference_pool(__name__, model_names)
def get_model_options() -> ModelOptions:
model_name = state_manager.get_item('deep_swapper_model')
return create_static_model_set('full').get(model_name)
def get_model_size() -> Size:
deep_swapper = get_inference_pool().get('deep_swapper')
for deep_swapper_input in deep_swapper.get_inputs():
if deep_swapper_input.name == 'in_face:0':
return deep_swapper_input.shape[1:3]
return 0, 0
def register_args(program : ArgumentParser) -> None:
group_processors = find_argument_group(program, 'processors')
if group_processors:
group_processors.add_argument('--deep-swapper-model', help = translator.get('help.model', __package__), default = config.get_str_value('processors', 'deep_swapper_model', 'iperov/elon_musk_224'), choices = deep_swapper_choices.deep_swapper_models)
group_processors.add_argument('--deep-swapper-morph', help = translator.get('help.morph', __package__), type = int, default = config.get_int_value('processors', 'deep_swapper_morph', '100'), choices = deep_swapper_choices.deep_swapper_morph_range, metavar = create_int_metavar(deep_swapper_choices.deep_swapper_morph_range))
facefusion.jobs.job_store.register_step_keys([ 'deep_swapper_model', 'deep_swapper_morph' ])
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('deep_swapper_model', args.get('deep_swapper_model'))
apply_state_item('deep_swapper_morph', args.get('deep_swapper_morph'))
def pre_check() -> bool:
model_hash_set = get_model_options().get('hashes')
model_source_set = get_model_options().get('sources')
if model_hash_set and model_source_set:
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
return True
def pre_process(mode : ProcessMode) -> bool:
if mode in [ 'output', 'preview' ] and not is_image(state_manager.get_item('target_path')) and not is_video(state_manager.get_item('target_path')):
logger.error(translator.get('choose_image_or_video_target') + translator.get('exclamation_mark'), __name__)
return False
if mode == 'output' and not in_directory(state_manager.get_item('output_path')):
logger.error(translator.get('specify_image_or_video_output') + translator.get('exclamation_mark'), __name__)
return False
if mode == 'output' and not same_file_extension(state_manager.get_item('target_path'), state_manager.get_item('output_path')):
logger.error(translator.get('match_target_and_output_extension') + translator.get('exclamation_mark'), __name__)
return False
return True
def post_process() -> None:
read_static_image.cache_clear()
read_static_video_frame.cache_clear()
video_manager.clear_video_pool()
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
clear_inference_pool()
if state_manager.get_item('video_memory_strategy') == 'strict':
content_analyser.clear_inference_pool()
face_classifier.clear_inference_pool()
face_detector.clear_inference_pool()
face_landmarker.clear_inference_pool()
face_masker.clear_inference_pool()
face_recognizer.clear_inference_pool()
def swap_face(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
model_template = get_model_options().get('template')
model_size = get_model_size()
crop_vision_frame, affine_matrix = warp_face_by_face_landmark_5(temp_vision_frame, target_face.landmark_set.get('5/68'), model_template, model_size)
crop_vision_frame_raw = crop_vision_frame.copy()
box_mask = create_box_mask(crop_vision_frame, state_manager.get_item('face_mask_blur'), state_manager.get_item('face_mask_padding'))
crop_masks =\
[
box_mask
]
if 'occlusion' in state_manager.get_item('face_mask_types'):
occlusion_mask = create_occlusion_mask(crop_vision_frame)
crop_masks.append(occlusion_mask)
crop_vision_frame = prepare_crop_frame(crop_vision_frame)
deep_swapper_morph = numpy.array([ numpy.interp(state_manager.get_item('deep_swapper_morph'), [ 0, 100 ], [ 0, 1 ]) ]).astype(numpy.float32)
crop_vision_frame, crop_source_mask, crop_target_mask = forward(crop_vision_frame, deep_swapper_morph)
crop_vision_frame = normalize_crop_frame(crop_vision_frame)
crop_vision_frame = conditional_match_frame_color(crop_vision_frame_raw, crop_vision_frame)
crop_masks.append(prepare_crop_mask(crop_source_mask, crop_target_mask))
if 'area' in state_manager.get_item('face_mask_types'):
face_landmark_68 = cv2.transform(target_face.landmark_set.get('68').reshape(1, -1, 2), affine_matrix).reshape(-1, 2)
area_mask = create_area_mask(crop_vision_frame, face_landmark_68, state_manager.get_item('face_mask_areas'))
crop_masks.append(area_mask)
if 'region' in state_manager.get_item('face_mask_types'):
region_mask = create_region_mask(crop_vision_frame, state_manager.get_item('face_mask_regions'))
crop_masks.append(region_mask)
crop_mask = numpy.minimum.reduce(crop_masks).clip(0, 1)
paste_vision_frame = paste_back(temp_vision_frame, crop_vision_frame, crop_mask, affine_matrix)
return paste_vision_frame
def forward(crop_vision_frame : VisionFrame, deep_swapper_morph : DeepSwapperMorph) -> Tuple[VisionFrame, Mask, Mask]:
deep_swapper = get_inference_pool().get('deep_swapper')
deep_swapper_inputs = {}
for deep_swapper_input in deep_swapper.get_inputs():
if deep_swapper_input.name == 'in_face:0':
deep_swapper_inputs[deep_swapper_input.name] = crop_vision_frame
if deep_swapper_input.name == 'morph_value:0':
deep_swapper_inputs[deep_swapper_input.name] = deep_swapper_morph
with thread_semaphore():
crop_target_mask, crop_vision_frame, crop_source_mask = deep_swapper.run(None, deep_swapper_inputs)
return crop_vision_frame[0], crop_source_mask[0], crop_target_mask[0]
def has_morph_input() -> bool:
deep_swapper = get_inference_pool().get('deep_swapper')
for deep_swapper_input in deep_swapper.get_inputs():
if deep_swapper_input.name == 'morph_value:0':
return True
return False
def prepare_crop_frame(crop_vision_frame : VisionFrame) -> VisionFrame:
crop_vision_frame = cv2.addWeighted(crop_vision_frame, 1.75, cv2.GaussianBlur(crop_vision_frame, (0, 0), 2), -0.75, 0)
crop_vision_frame = crop_vision_frame / 255.0
crop_vision_frame = numpy.expand_dims(crop_vision_frame, axis = 0).astype(numpy.float32)
return crop_vision_frame
def normalize_crop_frame(crop_vision_frame : VisionFrame) -> VisionFrame:
crop_vision_frame = (crop_vision_frame * 255.0).clip(0, 255)
crop_vision_frame = crop_vision_frame.astype(numpy.uint8)
return crop_vision_frame
def prepare_crop_mask(crop_source_mask : Mask, crop_target_mask : Mask) -> Mask:
model_size = get_model_size()
blur_size = 6.25
kernel_size = 3
crop_mask = numpy.minimum.reduce([ crop_source_mask, crop_target_mask ])
crop_mask = crop_mask.reshape(model_size).clip(0, 1)
crop_mask = cv2.erode(crop_mask, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (kernel_size, kernel_size)), iterations = 2)
crop_mask = cv2.GaussianBlur(crop_mask, (0, 0), blur_size)
return crop_mask
def process_frame(inputs : DeepSwapperInputs) -> ProcessorOutputs:
reference_vision_frame = inputs.get('reference_vision_frame')
target_vision_frame = inputs.get('target_vision_frame')
temp_vision_frame = inputs.get('temp_vision_frame')
temp_vision_mask = inputs.get('temp_vision_mask')
target_faces = select_faces(reference_vision_frame, target_vision_frame)
if target_faces:
for target_face in target_faces:
target_face = scale_face(target_face, target_vision_frame, temp_vision_frame)
temp_vision_frame = swap_face(target_face, temp_vision_frame)
return temp_vision_frame, temp_vision_mask

View File

@@ -1,18 +0,0 @@
from facefusion.types import Locals
LOCALS : Locals =\
{
'en':
{
'help':
{
'model': 'choose the model responsible for swapping the face',
'morph': 'morph between source face and target faces'
},
'uis':
{
'model_dropdown': 'DEEP SWAPPER MODEL',
'morph_slider': 'DEEP SWAPPER MORPH'
}
}
}

View File

@@ -1,17 +0,0 @@
from typing import Any, TypeAlias, TypedDict
from numpy.typing import NDArray
from facefusion.types import Mask, VisionFrame
DeepSwapperInputs = TypedDict('DeepSwapperInputs',
{
'reference_vision_frame' : VisionFrame,
'target_vision_frame' : VisionFrame,
'temp_vision_frame' : VisionFrame,
'temp_vision_mask' : Mask
})
DeepSwapperModel : TypeAlias = str
DeepSwapperMorph : TypeAlias = NDArray[Any]

View File

@@ -0,0 +1,290 @@
from argparse import ArgumentParser
from typing import List, Tuple
import cv2
import numpy
import facefusion.jobs.job_manager
import facefusion.jobs.job_store
import facefusion.processors.core as processors
from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, inference_manager, logger, process_manager, state_manager, wording
from facefusion.common_helper import create_int_metavar
from facefusion.download import conditional_download_hashes, conditional_download_sources
from facefusion.face_analyser import get_many_faces, get_one_face
from facefusion.face_helper import paste_back, warp_face_by_face_landmark_5
from facefusion.face_masker import create_occlusion_mask, create_static_box_mask
from facefusion.face_selector import find_similar_faces, sort_and_filter_faces
from facefusion.face_store import get_reference_faces
from facefusion.filesystem import in_directory, is_image, is_video, resolve_relative_path, same_file_extension
from facefusion.processors import choices as processors_choices
from facefusion.processors.live_portrait import create_rotation, limit_expression
from facefusion.processors.typing import ExpressionRestorerInputs
from facefusion.processors.typing import LivePortraitExpression, LivePortraitFeatureVolume, LivePortraitMotionPoints, LivePortraitPitch, LivePortraitRoll, LivePortraitScale, LivePortraitTranslation, LivePortraitYaw
from facefusion.program_helper import find_argument_group
from facefusion.thread_helper import conditional_thread_semaphore, thread_semaphore
from facefusion.typing import ApplyStateItem, Args, Face, InferencePool, ModelOptions, ModelSet, ProcessMode, QueuePayload, UpdateProgress, VisionFrame
from facefusion.vision import get_video_frame, read_image, read_static_image, write_image
MODEL_SET : ModelSet =\
{
'live_portrait':
{
'hashes':
{
'feature_extractor':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/live_portrait_feature_extractor.hash',
'path': resolve_relative_path('../.assets/models/live_portrait_feature_extractor.hash')
},
'motion_extractor':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/live_portrait_motion_extractor.hash',
'path': resolve_relative_path('../.assets/models/live_portrait_motion_extractor.hash')
},
'generator':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/live_portrait_generator.hash',
'path': resolve_relative_path('../.assets/models/live_portrait_generator.hash')
}
},
'sources':
{
'feature_extractor':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/live_portrait_feature_extractor.onnx',
'path': resolve_relative_path('../.assets/models/live_portrait_feature_extractor.onnx')
},
'motion_extractor':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/live_portrait_motion_extractor.onnx',
'path': resolve_relative_path('../.assets/models/live_portrait_motion_extractor.onnx')
},
'generator':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/live_portrait_generator.onnx',
'path': resolve_relative_path('../.assets/models/live_portrait_generator.onnx')
}
},
'template': 'arcface_128_v2',
'size': (512, 512)
}
}
def get_inference_pool() -> InferencePool:
model_sources = get_model_options().get('sources')
model_context = __name__ + '.' + state_manager.get_item('expression_restorer_model')
return inference_manager.get_inference_pool(model_context, model_sources)
def clear_inference_pool() -> None:
inference_manager.clear_inference_pool(__name__)
def get_model_options() -> ModelOptions:
expression_restorer_model = state_manager.get_item('expression_restorer_model')
return MODEL_SET.get(expression_restorer_model)
def register_args(program : ArgumentParser) -> None:
group_processors = find_argument_group(program, 'processors')
if group_processors:
group_processors.add_argument('--expression-restorer-model', help = wording.get('help.expression_restorer_model'), default = config.get_str_value('processors.expression_restorer_model', 'live_portrait'), choices = processors_choices.expression_restorer_models)
group_processors.add_argument('--expression-restorer-factor', help = wording.get('help.expression_restorer_factor'), type = int, default = config.get_int_value('processors.expression_restorer_factor', '80'), choices = processors_choices.expression_restorer_factor_range, metavar = create_int_metavar(processors_choices.expression_restorer_factor_range))
facefusion.jobs.job_store.register_step_keys([ 'expression_restorer_model','expression_restorer_factor' ])
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('expression_restorer_model', args.get('expression_restorer_model'))
apply_state_item('expression_restorer_factor', args.get('expression_restorer_factor'))
def pre_check() -> bool:
download_directory_path = resolve_relative_path('../.assets/models')
model_hashes = get_model_options().get('hashes')
model_sources = get_model_options().get('sources')
return conditional_download_hashes(download_directory_path, model_hashes) and conditional_download_sources(download_directory_path, model_sources)
def pre_process(mode : ProcessMode) -> bool:
if mode in [ 'output', 'preview' ] and not is_image(state_manager.get_item('target_path')) and not is_video(state_manager.get_item('target_path')):
logger.error(wording.get('choose_image_or_video_target') + wording.get('exclamation_mark'), __name__)
return False
if mode == 'output' and not in_directory(state_manager.get_item('output_path')):
logger.error(wording.get('specify_image_or_video_output') + wording.get('exclamation_mark'), __name__)
return False
if mode == 'output' and not same_file_extension([ state_manager.get_item('target_path'), state_manager.get_item('output_path') ]):
logger.error(wording.get('match_target_and_output_extension') + wording.get('exclamation_mark'), __name__)
return False
return True
def post_process() -> None:
read_static_image.cache_clear()
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
clear_inference_pool()
if state_manager.get_item('video_memory_strategy') == 'strict':
content_analyser.clear_inference_pool()
face_classifier.clear_inference_pool()
face_detector.clear_inference_pool()
face_landmarker.clear_inference_pool()
face_masker.clear_inference_pool()
face_recognizer.clear_inference_pool()
def restore_expression(source_vision_frame : VisionFrame, target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
model_template = get_model_options().get('template')
model_size = get_model_options().get('size')
expression_restorer_factor = float(numpy.interp(float(state_manager.get_item('expression_restorer_factor')), [ 0, 100 ], [ 0, 1.2 ]))
source_vision_frame = cv2.resize(source_vision_frame, temp_vision_frame.shape[:2][::-1])
source_crop_vision_frame, _ = warp_face_by_face_landmark_5(source_vision_frame, target_face.landmark_set.get('5/68'), model_template, model_size)
target_crop_vision_frame, affine_matrix = warp_face_by_face_landmark_5(temp_vision_frame, target_face.landmark_set.get('5/68'), model_template, model_size)
box_mask = create_static_box_mask(target_crop_vision_frame.shape[:2][::-1], state_manager.get_item('face_mask_blur'), (0, 0, 0, 0))
crop_masks =\
[
box_mask
]
if 'occlusion' in state_manager.get_item('face_mask_types'):
occlusion_mask = create_occlusion_mask(target_crop_vision_frame)
crop_masks.append(occlusion_mask)
source_crop_vision_frame = prepare_crop_frame(source_crop_vision_frame)
target_crop_vision_frame = prepare_crop_frame(target_crop_vision_frame)
target_crop_vision_frame = apply_restore(source_crop_vision_frame, target_crop_vision_frame, expression_restorer_factor)
target_crop_vision_frame = normalize_crop_frame(target_crop_vision_frame)
crop_mask = numpy.minimum.reduce(crop_masks).clip(0, 1)
temp_vision_frame = paste_back(temp_vision_frame, target_crop_vision_frame, crop_mask, affine_matrix)
return temp_vision_frame
def apply_restore(source_crop_vision_frame : VisionFrame, target_crop_vision_frame : VisionFrame, expression_restorer_factor : float) -> VisionFrame:
feature_volume = forward_extract_feature(target_crop_vision_frame)
source_expression = forward_extract_motion(source_crop_vision_frame)[5]
pitch, yaw, roll, scale, translation, target_expression, motion_points = forward_extract_motion(target_crop_vision_frame)
rotation = create_rotation(pitch, yaw, roll)
source_expression[:, [ 0, 4, 5, 8, 9 ]] = target_expression[:, [ 0, 4, 5, 8, 9 ]]
source_expression = source_expression * expression_restorer_factor + target_expression * (1 - expression_restorer_factor)
source_expression = limit_expression(source_expression)
source_motion_points = scale * (motion_points @ rotation.T + source_expression) + translation
target_motion_points = scale * (motion_points @ rotation.T + target_expression) + translation
crop_vision_frame = forward_generate_frame(feature_volume, source_motion_points, target_motion_points)
return crop_vision_frame
def forward_extract_feature(crop_vision_frame : VisionFrame) -> LivePortraitFeatureVolume:
feature_extractor = get_inference_pool().get('feature_extractor')
with conditional_thread_semaphore():
feature_volume = feature_extractor.run(None,
{
'input': crop_vision_frame
})[0]
return feature_volume
def forward_extract_motion(crop_vision_frame : VisionFrame) -> Tuple[LivePortraitPitch, LivePortraitYaw, LivePortraitRoll, LivePortraitScale, LivePortraitTranslation, LivePortraitExpression, LivePortraitMotionPoints]:
motion_extractor = get_inference_pool().get('motion_extractor')
with conditional_thread_semaphore():
pitch, yaw, roll, scale, translation, expression, motion_points = motion_extractor.run(None,
{
'input': crop_vision_frame
})
return pitch, yaw, roll, scale, translation, expression, motion_points
def forward_generate_frame(feature_volume : LivePortraitFeatureVolume, source_motion_points : LivePortraitMotionPoints, target_motion_points : LivePortraitMotionPoints) -> VisionFrame:
generator = get_inference_pool().get('generator')
with thread_semaphore():
crop_vision_frame = generator.run(None,
{
'feature_volume': feature_volume,
'source': source_motion_points,
'target': target_motion_points
})[0][0]
return crop_vision_frame
def prepare_crop_frame(crop_vision_frame : VisionFrame) -> VisionFrame:
model_size = get_model_options().get('size')
prepare_size = (model_size[0] // 2, model_size[1] // 2)
crop_vision_frame = cv2.resize(crop_vision_frame, prepare_size, interpolation = cv2.INTER_AREA)
crop_vision_frame = crop_vision_frame[:, :, ::-1] / 255.0
crop_vision_frame = numpy.expand_dims(crop_vision_frame.transpose(2, 0, 1), axis = 0).astype(numpy.float32)
return crop_vision_frame
def normalize_crop_frame(crop_vision_frame : VisionFrame) -> VisionFrame:
crop_vision_frame = crop_vision_frame.transpose(1, 2, 0).clip(0, 1)
crop_vision_frame = (crop_vision_frame * 255.0)
crop_vision_frame = crop_vision_frame.astype(numpy.uint8)[:, :, ::-1]
return crop_vision_frame
def get_reference_frame(source_face : Face, target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
pass
def process_frame(inputs : ExpressionRestorerInputs) -> VisionFrame:
reference_faces = inputs.get('reference_faces')
source_vision_frame = inputs.get('source_vision_frame')
target_vision_frame = inputs.get('target_vision_frame')
many_faces = sort_and_filter_faces(get_many_faces([ target_vision_frame ]))
if state_manager.get_item('face_selector_mode') == 'many':
if many_faces:
for target_face in many_faces:
target_vision_frame = restore_expression(source_vision_frame, target_face, target_vision_frame)
if state_manager.get_item('face_selector_mode') == 'one':
target_face = get_one_face(many_faces)
if target_face:
target_vision_frame = restore_expression(source_vision_frame, target_face, target_vision_frame)
if state_manager.get_item('face_selector_mode') == 'reference':
similar_faces = find_similar_faces(many_faces, reference_faces, state_manager.get_item('reference_face_distance'))
if similar_faces:
for similar_face in similar_faces:
target_vision_frame = restore_expression(source_vision_frame, similar_face, target_vision_frame)
return target_vision_frame
def process_frames(source_path : List[str], queue_payloads : List[QueuePayload], update_progress : UpdateProgress) -> None:
reference_faces = get_reference_faces() if 'reference' in state_manager.get_item('face_selector_mode') else None
for queue_payload in process_manager.manage(queue_payloads):
frame_number = queue_payload.get('frame_number')
if state_manager.get_item('trim_frame_start'):
frame_number += state_manager.get_item('trim_frame_start')
source_vision_frame = get_video_frame(state_manager.get_item('target_path'), frame_number)
target_vision_path = queue_payload.get('frame_path')
target_vision_frame = read_image(target_vision_path)
output_vision_frame = process_frame(
{
'reference_faces': reference_faces,
'source_vision_frame': source_vision_frame,
'target_vision_frame': target_vision_frame
})
write_image(target_vision_path, output_vision_frame)
update_progress(1)
def process_image(source_path : str, target_path : str, output_path : str) -> None:
reference_faces = get_reference_faces() if 'reference' in state_manager.get_item('face_selector_mode') else None
source_vision_frame = read_static_image(state_manager.get_item('target_path'))
target_vision_frame = read_static_image(target_path)
output_vision_frame = process_frame(
{
'reference_faces': reference_faces,
'source_vision_frame': source_vision_frame,
'target_vision_frame': target_vision_frame
})
write_image(output_path, output_vision_frame)
def process_video(source_paths : List[str], temp_frame_paths : List[str]) -> None:
processors.multi_process_frames(None, temp_frame_paths, process_frames)

View File

@@ -1,10 +0,0 @@
from typing import List, Sequence
from facefusion.common_helper import create_int_range
from facefusion.processors.modules.expression_restorer.types import ExpressionRestorerArea, ExpressionRestorerModel
expression_restorer_models : List[ExpressionRestorerModel] = [ 'live_portrait' ]
expression_restorer_areas : List[ExpressionRestorerArea] = [ 'upper-face', 'lower-face' ]
expression_restorer_factor_range : Sequence[int] = create_int_range(0, 100, 1)

View File

@@ -1,270 +0,0 @@
from argparse import ArgumentParser
from functools import lru_cache
from typing import Tuple
import cv2
import numpy
import facefusion.jobs.job_manager
import facefusion.jobs.job_store
from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, inference_manager, logger, state_manager, translator, video_manager
from facefusion.common_helper import create_int_metavar
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
from facefusion.face_analyser import scale_face
from facefusion.face_helper import paste_back, warp_face_by_face_landmark_5
from facefusion.face_masker import create_box_mask, create_occlusion_mask
from facefusion.face_selector import select_faces
from facefusion.filesystem import in_directory, is_image, is_video, resolve_relative_path, same_file_extension
from facefusion.processors.live_portrait import create_rotation, limit_expression
from facefusion.processors.modules.expression_restorer import choices as expression_restorer_choices
from facefusion.processors.modules.expression_restorer.types import ExpressionRestorerInputs
from facefusion.processors.types import LivePortraitExpression, LivePortraitFeatureVolume, LivePortraitMotionPoints, LivePortraitPitch, LivePortraitRoll, LivePortraitScale, LivePortraitTranslation, LivePortraitYaw, ProcessorOutputs
from facefusion.program_helper import find_argument_group
from facefusion.thread_helper import conditional_thread_semaphore, thread_semaphore
from facefusion.types import ApplyStateItem, Args, DownloadScope, Face, InferencePool, ModelOptions, ModelSet, ProcessMode, VisionFrame
from facefusion.vision import read_static_image, read_static_video_frame
@lru_cache()
def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
return\
{
'live_portrait':
{
'__metadata__':
{
'vendor': 'KwaiVGI',
'license': 'MIT',
'year': 2024
},
'hashes':
{
'feature_extractor':
{
'url': resolve_download_url('models-3.0.0', 'live_portrait_feature_extractor.hash'),
'path': resolve_relative_path('../.assets/models/live_portrait_feature_extractor.hash')
},
'motion_extractor':
{
'url': resolve_download_url('models-3.0.0', 'live_portrait_motion_extractor.hash'),
'path': resolve_relative_path('../.assets/models/live_portrait_motion_extractor.hash')
},
'generator':
{
'url': resolve_download_url('models-3.0.0', 'live_portrait_generator.hash'),
'path': resolve_relative_path('../.assets/models/live_portrait_generator.hash')
}
},
'sources':
{
'feature_extractor':
{
'url': resolve_download_url('models-3.0.0', 'live_portrait_feature_extractor.onnx'),
'path': resolve_relative_path('../.assets/models/live_portrait_feature_extractor.onnx')
},
'motion_extractor':
{
'url': resolve_download_url('models-3.0.0', 'live_portrait_motion_extractor.onnx'),
'path': resolve_relative_path('../.assets/models/live_portrait_motion_extractor.onnx')
},
'generator':
{
'url': resolve_download_url('models-3.0.0', 'live_portrait_generator.onnx'),
'path': resolve_relative_path('../.assets/models/live_portrait_generator.onnx')
}
},
'template': 'arcface_128',
'size': (512, 512)
}
}
def get_inference_pool() -> InferencePool:
model_names = [ state_manager.get_item('expression_restorer_model') ]
model_source_set = get_model_options().get('sources')
return inference_manager.get_inference_pool(__name__, model_names, model_source_set)
def clear_inference_pool() -> None:
model_names = [ state_manager.get_item('expression_restorer_model') ]
inference_manager.clear_inference_pool(__name__, model_names)
def get_model_options() -> ModelOptions:
model_name = state_manager.get_item('expression_restorer_model')
return create_static_model_set('full').get(model_name)
def register_args(program : ArgumentParser) -> None:
group_processors = find_argument_group(program, 'processors')
if group_processors:
group_processors.add_argument('--expression-restorer-model', help = translator.get('help.model', __package__), default = config.get_str_value('processors', 'expression_restorer_model', 'live_portrait'), choices = expression_restorer_choices.expression_restorer_models)
group_processors.add_argument('--expression-restorer-factor', help = translator.get('help.factor', __package__), type = int, default = config.get_int_value('processors', 'expression_restorer_factor', '80'), choices = expression_restorer_choices.expression_restorer_factor_range, metavar = create_int_metavar(expression_restorer_choices.expression_restorer_factor_range))
group_processors.add_argument('--expression-restorer-areas', help = translator.get('help.areas', __package__).format(choices = ', '.join(expression_restorer_choices.expression_restorer_areas)), default = config.get_str_list('processors', 'expression_restorer_areas', ' '.join(expression_restorer_choices.expression_restorer_areas)), choices = expression_restorer_choices.expression_restorer_areas, nargs ='+', metavar ='EXPRESSION_RESTORER_AREAS')
facefusion.jobs.job_store.register_step_keys([ 'expression_restorer_model', 'expression_restorer_factor', 'expression_restorer_areas' ])
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('expression_restorer_model', args.get('expression_restorer_model'))
apply_state_item('expression_restorer_factor', args.get('expression_restorer_factor'))
apply_state_item('expression_restorer_areas', args.get('expression_restorer_areas'))
def pre_check() -> bool:
model_hash_set = get_model_options().get('hashes')
model_source_set = get_model_options().get('sources')
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
def pre_process(mode : ProcessMode) -> bool:
if mode == 'stream':
logger.error(translator.get('stream_not_supported') + translator.get('exclamation_mark'), __name__)
return False
if mode in [ 'output', 'preview' ] and not is_image(state_manager.get_item('target_path')) and not is_video(state_manager.get_item('target_path')):
logger.error(translator.get('choose_image_or_video_target') + translator.get('exclamation_mark'), __name__)
return False
if mode == 'output' and not in_directory(state_manager.get_item('output_path')):
logger.error(translator.get('specify_image_or_video_output') + translator.get('exclamation_mark'), __name__)
return False
if mode == 'output' and not same_file_extension(state_manager.get_item('target_path'), state_manager.get_item('output_path')):
logger.error(translator.get('match_target_and_output_extension') + translator.get('exclamation_mark'), __name__)
return False
return True
def post_process() -> None:
read_static_image.cache_clear()
read_static_video_frame.cache_clear()
video_manager.clear_video_pool()
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
clear_inference_pool()
if state_manager.get_item('video_memory_strategy') == 'strict':
content_analyser.clear_inference_pool()
face_classifier.clear_inference_pool()
face_detector.clear_inference_pool()
face_landmarker.clear_inference_pool()
face_masker.clear_inference_pool()
face_recognizer.clear_inference_pool()
def restore_expression(target_face : Face, target_vision_frame : VisionFrame, temp_vision_frame : VisionFrame) -> VisionFrame:
model_template = get_model_options().get('template')
model_size = get_model_options().get('size')
expression_restorer_factor = float(numpy.interp(float(state_manager.get_item('expression_restorer_factor')), [ 0, 100 ], [ 0, 1.2 ]))
target_crop_vision_frame, _ = warp_face_by_face_landmark_5(target_vision_frame, target_face.landmark_set.get('5/68'), model_template, model_size)
temp_crop_vision_frame, affine_matrix = warp_face_by_face_landmark_5(temp_vision_frame, target_face.landmark_set.get('5/68'), model_template, model_size)
box_mask = create_box_mask(temp_crop_vision_frame, state_manager.get_item('face_mask_blur'), (0, 0, 0, 0))
crop_masks =\
[
box_mask
]
if 'occlusion' in state_manager.get_item('face_mask_types'):
occlusion_mask = create_occlusion_mask(temp_crop_vision_frame)
crop_masks.append(occlusion_mask)
target_crop_vision_frame = prepare_crop_frame(target_crop_vision_frame)
temp_crop_vision_frame = prepare_crop_frame(temp_crop_vision_frame)
temp_crop_vision_frame = apply_restore(target_crop_vision_frame, temp_crop_vision_frame, expression_restorer_factor)
temp_crop_vision_frame = normalize_crop_frame(temp_crop_vision_frame)
crop_mask = numpy.minimum.reduce(crop_masks).clip(0, 1)
paste_vision_frame = paste_back(temp_vision_frame, temp_crop_vision_frame, crop_mask, affine_matrix)
return paste_vision_frame
def apply_restore(target_crop_vision_frame : VisionFrame, temp_crop_vision_frame : VisionFrame, expression_restorer_factor : float) -> VisionFrame:
feature_volume = forward_extract_feature(temp_crop_vision_frame)
target_expression = forward_extract_motion(target_crop_vision_frame)[5]
pitch, yaw, roll, scale, translation, temp_expression, motion_points = forward_extract_motion(temp_crop_vision_frame)
rotation = create_rotation(pitch, yaw, roll)
target_expression = restrict_expression_areas(temp_expression, target_expression)
target_expression = target_expression * expression_restorer_factor + temp_expression * (1 - expression_restorer_factor)
target_expression = limit_expression(target_expression)
target_motion_points = scale * (motion_points @ rotation.T + target_expression) + translation
temp_motion_points = scale * (motion_points @ rotation.T + temp_expression) + translation
crop_vision_frame = forward_generate_frame(feature_volume, target_motion_points, temp_motion_points)
return crop_vision_frame
def restrict_expression_areas(temp_expression : LivePortraitExpression, target_expression : LivePortraitExpression) -> LivePortraitExpression:
expression_restorer_areas = state_manager.get_item('expression_restorer_areas')
if 'upper-face' not in expression_restorer_areas:
target_expression[:, [1, 2, 6, 10, 11, 12, 13, 15, 16]] = temp_expression[:, [1, 2, 6, 10, 11, 12, 13, 15, 16]]
if 'lower-face' not in expression_restorer_areas:
target_expression[:, [3, 7, 14, 17, 18, 19, 20]] = temp_expression[:, [3, 7, 14, 17, 18, 19, 20]]
target_expression[:, [0, 4, 5, 8, 9]] = temp_expression[:, [0, 4, 5, 8, 9]]
return target_expression
def forward_extract_feature(crop_vision_frame : VisionFrame) -> LivePortraitFeatureVolume:
feature_extractor = get_inference_pool().get('feature_extractor')
with conditional_thread_semaphore():
feature_volume = feature_extractor.run(None,
{
'input': crop_vision_frame
})[0]
return feature_volume
def forward_extract_motion(crop_vision_frame : VisionFrame) -> Tuple[LivePortraitPitch, LivePortraitYaw, LivePortraitRoll, LivePortraitScale, LivePortraitTranslation, LivePortraitExpression, LivePortraitMotionPoints]:
motion_extractor = get_inference_pool().get('motion_extractor')
with conditional_thread_semaphore():
pitch, yaw, roll, scale, translation, expression, motion_points = motion_extractor.run(None,
{
'input': crop_vision_frame
})
return pitch, yaw, roll, scale, translation, expression, motion_points
def forward_generate_frame(feature_volume : LivePortraitFeatureVolume, target_motion_points : LivePortraitMotionPoints, temp_motion_points : LivePortraitMotionPoints) -> VisionFrame:
generator = get_inference_pool().get('generator')
with thread_semaphore():
crop_vision_frame = generator.run(None,
{
'feature_volume': feature_volume,
'source': target_motion_points,
'target': temp_motion_points
})[0][0]
return crop_vision_frame
def prepare_crop_frame(crop_vision_frame : VisionFrame) -> VisionFrame:
model_size = get_model_options().get('size')
prepare_size = (model_size[0] // 2, model_size[1] // 2)
crop_vision_frame = cv2.resize(crop_vision_frame, prepare_size, interpolation = cv2.INTER_AREA)
crop_vision_frame = crop_vision_frame[:, :, ::-1] / 255.0
crop_vision_frame = numpy.expand_dims(crop_vision_frame.transpose(2, 0, 1), axis = 0).astype(numpy.float32)
return crop_vision_frame
def normalize_crop_frame(crop_vision_frame : VisionFrame) -> VisionFrame:
crop_vision_frame = crop_vision_frame.transpose(1, 2, 0).clip(0, 1)
crop_vision_frame = crop_vision_frame * 255.0
crop_vision_frame = crop_vision_frame.astype(numpy.uint8)[:, :, ::-1]
return crop_vision_frame
def process_frame(inputs : ExpressionRestorerInputs) -> ProcessorOutputs:
reference_vision_frame = inputs.get('reference_vision_frame')
target_vision_frame = inputs.get('target_vision_frame')
temp_vision_frame = inputs.get('temp_vision_frame')
temp_vision_mask = inputs.get('temp_vision_mask')
target_faces = select_faces(reference_vision_frame, target_vision_frame)
if target_faces:
for target_face in target_faces:
target_face = scale_face(target_face, target_vision_frame, temp_vision_frame)
temp_vision_frame = restore_expression(target_face, target_vision_frame, temp_vision_frame)
return temp_vision_frame, temp_vision_mask

View File

@@ -1,20 +0,0 @@
from facefusion.types import Locals
LOCALS : Locals =\
{
'en':
{
'help':
{
'model': 'choose the model responsible for restoring the expression',
'factor': 'restore factor of expression from the target face',
'areas': 'choose the items used for the expression areas (choices: {choices})'
},
'uis':
{
'model_dropdown': 'EXPRESSION RESTORER MODEL',
'factor_slider': 'EXPRESSION RESTORER FACTOR',
'areas_checkbox_group': 'EXPRESSION RESTORER AREAS'
}
}
}

View File

@@ -1,16 +0,0 @@
from typing import List, Literal, TypedDict
from facefusion.types import Mask, VisionFrame
ExpressionRestorerInputs = TypedDict('ExpressionRestorerInputs',
{
'reference_vision_frame' : VisionFrame,
'source_vision_frames' : List[VisionFrame],
'target_vision_frame' : VisionFrame,
'temp_vision_frame' : VisionFrame,
'temp_vision_mask' : Mask
})
ExpressionRestorerModel = Literal['live_portrait']
ExpressionRestorerArea = Literal['upper-face', 'lower-face']

View File

@@ -0,0 +1,222 @@
from argparse import ArgumentParser
from typing import List
import cv2
import numpy
import facefusion.jobs.job_manager
import facefusion.jobs.job_store
import facefusion.processors.core as processors
from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, logger, process_manager, state_manager, wording
from facefusion.face_analyser import get_many_faces, get_one_face
from facefusion.face_helper import warp_face_by_face_landmark_5
from facefusion.face_masker import create_occlusion_mask, create_region_mask, create_static_box_mask
from facefusion.face_selector import find_similar_faces, sort_and_filter_faces
from facefusion.face_store import get_reference_faces
from facefusion.filesystem import in_directory, same_file_extension
from facefusion.processors import choices as processors_choices
from facefusion.processors.typing import FaceDebuggerInputs
from facefusion.program_helper import find_argument_group
from facefusion.typing import ApplyStateItem, Args, Face, ProcessMode, QueuePayload, UpdateProgress, VisionFrame
from facefusion.vision import read_image, read_static_image, write_image
def get_inference_pool() -> None:
pass
def clear_inference_pool() -> None:
pass
def register_args(program : ArgumentParser) -> None:
group_processors = find_argument_group(program, 'processors')
if group_processors:
group_processors.add_argument('--face-debugger-items', help = wording.get('help.face_debugger_items').format(choices = ', '.join(processors_choices.face_debugger_items)), default = config.get_str_list('processors.face_debugger_items', 'face-landmark-5/68 face-mask'), choices = processors_choices.face_debugger_items, nargs = '+', metavar = 'FACE_DEBUGGER_ITEMS')
facefusion.jobs.job_store.register_step_keys([ 'face_debugger_items' ])
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('face_debugger_items', args.get('face_debugger_items'))
def pre_check() -> bool:
return True
def pre_process(mode : ProcessMode) -> bool:
if mode == 'output' and not in_directory(state_manager.get_item('output_path')):
logger.error(wording.get('specify_image_or_video_output') + wording.get('exclamation_mark'), __name__)
return False
if mode == 'output' and not same_file_extension([ state_manager.get_item('target_path'), state_manager.get_item('output_path') ]):
logger.error(wording.get('match_target_and_output_extension') + wording.get('exclamation_mark'), __name__)
return False
return True
def post_process() -> None:
read_static_image.cache_clear()
if state_manager.get_item('video_memory_strategy') == 'strict':
content_analyser.clear_inference_pool()
face_classifier.clear_inference_pool()
face_detector.clear_inference_pool()
face_landmarker.clear_inference_pool()
face_masker.clear_inference_pool()
face_recognizer.clear_inference_pool()
def debug_face(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
primary_color = (0, 0, 255)
primary_light_color = (100, 100, 255)
secondary_color = (0, 255, 0)
tertiary_color = (255, 255, 0)
bounding_box = target_face.bounding_box.astype(numpy.int32)
temp_vision_frame = temp_vision_frame.copy()
has_face_landmark_5_fallback = numpy.array_equal(target_face.landmark_set.get('5'), target_face.landmark_set.get('5/68'))
has_face_landmark_68_fallback = numpy.array_equal(target_face.landmark_set.get('68'), target_face.landmark_set.get('68/5'))
face_debugger_items = state_manager.get_item('face_debugger_items')
if 'bounding-box' in face_debugger_items:
x1, y1, x2, y2 = bounding_box
cv2.rectangle(temp_vision_frame, (x1, y1), (x2, y2), primary_color, 2)
if target_face.angle == 0:
cv2.line(temp_vision_frame, (x1, y1), (x2, y1), primary_light_color, 3)
elif target_face.angle == 180:
cv2.line(temp_vision_frame, (x1, y2), (x2, y2), primary_light_color, 3)
elif target_face.angle == 90:
cv2.line(temp_vision_frame, (x2, y1), (x2, y2), primary_light_color, 3)
elif target_face.angle == 270:
cv2.line(temp_vision_frame, (x1, y1), (x1, y2), primary_light_color, 3)
if 'face-mask' in face_debugger_items:
crop_vision_frame, affine_matrix = warp_face_by_face_landmark_5(temp_vision_frame, target_face.landmark_set.get('5/68'), 'arcface_128_v2', (512, 512))
inverse_matrix = cv2.invertAffineTransform(affine_matrix)
temp_size = temp_vision_frame.shape[:2][::-1]
crop_masks = []
if 'box' in state_manager.get_item('face_mask_types'):
box_mask = create_static_box_mask(crop_vision_frame.shape[:2][::-1], 0, state_manager.get_item('face_mask_padding'))
crop_masks.append(box_mask)
if 'occlusion' in state_manager.get_item('face_mask_types'):
occlusion_mask = create_occlusion_mask(crop_vision_frame)
crop_masks.append(occlusion_mask)
if 'region' in state_manager.get_item('face_mask_types'):
region_mask = create_region_mask(crop_vision_frame, state_manager.get_item('face_mask_regions'))
crop_masks.append(region_mask)
crop_mask = numpy.minimum.reduce(crop_masks).clip(0, 1)
crop_mask = (crop_mask * 255).astype(numpy.uint8)
inverse_vision_frame = cv2.warpAffine(crop_mask, inverse_matrix, temp_size)
inverse_vision_frame = cv2.threshold(inverse_vision_frame, 100, 255, cv2.THRESH_BINARY)[1]
inverse_vision_frame[inverse_vision_frame > 0] = 255 #type:ignore[operator]
inverse_contours = cv2.findContours(inverse_vision_frame, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)[0]
cv2.drawContours(temp_vision_frame, inverse_contours, -1, tertiary_color if has_face_landmark_5_fallback else secondary_color, 2)
if 'face-landmark-5' in face_debugger_items and numpy.any(target_face.landmark_set.get('5')):
face_landmark_5 = target_face.landmark_set.get('5').astype(numpy.int32)
for index in range(face_landmark_5.shape[0]):
cv2.circle(temp_vision_frame, (face_landmark_5[index][0], face_landmark_5[index][1]), 3, primary_color, -1)
if 'face-landmark-5/68' in face_debugger_items and numpy.any(target_face.landmark_set.get('5/68')):
face_landmark_5_68 = target_face.landmark_set.get('5/68').astype(numpy.int32)
for index in range(face_landmark_5_68.shape[0]):
cv2.circle(temp_vision_frame, (face_landmark_5_68[index][0], face_landmark_5_68[index][1]), 3, tertiary_color if has_face_landmark_5_fallback else secondary_color, -1)
if 'face-landmark-68' in face_debugger_items and numpy.any(target_face.landmark_set.get('68')):
face_landmark_68 = target_face.landmark_set.get('68').astype(numpy.int32)
for index in range(face_landmark_68.shape[0]):
cv2.circle(temp_vision_frame, (face_landmark_68[index][0], face_landmark_68[index][1]), 3, tertiary_color if has_face_landmark_68_fallback else secondary_color, -1)
if 'face-landmark-68/5' in face_debugger_items and numpy.any(target_face.landmark_set.get('68')):
face_landmark_68 = target_face.landmark_set.get('68/5').astype(numpy.int32)
for index in range(face_landmark_68.shape[0]):
cv2.circle(temp_vision_frame, (face_landmark_68[index][0], face_landmark_68[index][1]), 3, tertiary_color, -1)
if bounding_box[3] - bounding_box[1] > 50 and bounding_box[2] - bounding_box[0] > 50:
top = bounding_box[1]
left = bounding_box[0] - 20
if 'face-detector-score' in face_debugger_items:
face_score_text = str(round(target_face.score_set.get('detector'), 2))
top = top + 20
cv2.putText(temp_vision_frame, face_score_text, (left, top), cv2.FONT_HERSHEY_SIMPLEX, 0.5, primary_color, 2)
if 'face-landmarker-score' in face_debugger_items:
face_score_text = str(round(target_face.score_set.get('landmarker'), 2))
top = top + 20
cv2.putText(temp_vision_frame, face_score_text, (left, top), cv2.FONT_HERSHEY_SIMPLEX, 0.5, tertiary_color if has_face_landmark_5_fallback else secondary_color, 2)
if 'age' in face_debugger_items:
face_age_text = str(target_face.age.start) + '-' + str(target_face.age.stop)
top = top + 20
cv2.putText(temp_vision_frame, face_age_text, (left, top), cv2.FONT_HERSHEY_SIMPLEX, 0.5, primary_color, 2)
if 'gender' in face_debugger_items:
face_gender_text = target_face.gender
top = top + 20
cv2.putText(temp_vision_frame, face_gender_text, (left, top), cv2.FONT_HERSHEY_SIMPLEX, 0.5, primary_color, 2)
if 'race' in face_debugger_items:
face_race_text = target_face.race
top = top + 20
cv2.putText(temp_vision_frame, face_race_text, (left, top), cv2.FONT_HERSHEY_SIMPLEX, 0.5, primary_color, 2)
return temp_vision_frame
def get_reference_frame(source_face : Face, target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
pass
def process_frame(inputs : FaceDebuggerInputs) -> VisionFrame:
reference_faces = inputs.get('reference_faces')
target_vision_frame = inputs.get('target_vision_frame')
many_faces = sort_and_filter_faces(get_many_faces([ target_vision_frame ]))
if state_manager.get_item('face_selector_mode') == 'many':
if many_faces:
for target_face in many_faces:
target_vision_frame = debug_face(target_face, target_vision_frame)
if state_manager.get_item('face_selector_mode') == 'one':
target_face = get_one_face(many_faces)
if target_face:
target_vision_frame = debug_face(target_face, target_vision_frame)
if state_manager.get_item('face_selector_mode') == 'reference':
similar_faces = find_similar_faces(many_faces, reference_faces, state_manager.get_item('reference_face_distance'))
if similar_faces:
for similar_face in similar_faces:
target_vision_frame = debug_face(similar_face, target_vision_frame)
return target_vision_frame
def process_frames(source_paths : List[str], queue_payloads : List[QueuePayload], update_progress : UpdateProgress) -> None:
reference_faces = get_reference_faces() if 'reference' in state_manager.get_item('face_selector_mode') else None
for queue_payload in process_manager.manage(queue_payloads):
target_vision_path = queue_payload['frame_path']
target_vision_frame = read_image(target_vision_path)
output_vision_frame = process_frame(
{
'reference_faces': reference_faces,
'target_vision_frame': target_vision_frame
})
write_image(target_vision_path, output_vision_frame)
update_progress(1)
def process_image(source_paths : List[str], target_path : str, output_path : str) -> None:
reference_faces = get_reference_faces() if 'reference' in state_manager.get_item('face_selector_mode') else None
target_vision_frame = read_static_image(target_path)
output_vision_frame = process_frame(
{
'reference_faces': reference_faces,
'target_vision_frame': target_vision_frame
})
write_image(output_path, output_vision_frame)
def process_video(source_paths : List[str], temp_frame_paths : List[str]) -> None:
processors.multi_process_frames(source_paths, temp_frame_paths, process_frames)

View File

@@ -1,5 +0,0 @@
from typing import List
from facefusion.processors.modules.face_debugger.types import FaceDebuggerItem
face_debugger_items : List[FaceDebuggerItem] = [ 'bounding-box', 'face-landmark-5', 'face-landmark-5/68', 'face-landmark-68', 'face-landmark-68/5', 'face-mask' ]

View File

@@ -1,235 +0,0 @@
from argparse import ArgumentParser
import cv2
import numpy
import facefusion.jobs.job_manager
import facefusion.jobs.job_store
from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, logger, state_manager, translator, video_manager
from facefusion.face_analyser import scale_face
from facefusion.face_helper import warp_face_by_face_landmark_5
from facefusion.face_masker import create_area_mask, create_box_mask, create_occlusion_mask, create_region_mask
from facefusion.face_selector import select_faces
from facefusion.filesystem import in_directory, is_image, is_video, same_file_extension
from facefusion.processors.modules.face_debugger import choices as face_debugger_choices
from facefusion.processors.modules.face_debugger.types import FaceDebuggerInputs
from facefusion.processors.types import ProcessorOutputs
from facefusion.program_helper import find_argument_group
from facefusion.types import ApplyStateItem, Args, Face, InferencePool, ProcessMode, VisionFrame
from facefusion.vision import read_static_image, read_static_video_frame
def get_inference_pool() -> InferencePool:
pass
def clear_inference_pool() -> None:
pass
def register_args(program : ArgumentParser) -> None:
group_processors = find_argument_group(program, 'processors')
if group_processors:
group_processors.add_argument('--face-debugger-items', help = translator.get('help.items', __package__).format(choices = ', '.join(face_debugger_choices.face_debugger_items)), default = config.get_str_list('processors', 'face_debugger_items', 'face-landmark-5/68 face-mask'), choices = face_debugger_choices.face_debugger_items, nargs = '+', metavar = 'FACE_DEBUGGER_ITEMS')
facefusion.jobs.job_store.register_step_keys([ 'face_debugger_items' ])
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('face_debugger_items', args.get('face_debugger_items'))
def pre_check() -> bool:
return True
def pre_process(mode : ProcessMode) -> bool:
if mode in [ 'output', 'preview' ] and not is_image(state_manager.get_item('target_path')) and not is_video(state_manager.get_item('target_path')):
logger.error(translator.get('choose_image_or_video_target') + translator.get('exclamation_mark'), __name__)
return False
if mode == 'output' and not in_directory(state_manager.get_item('output_path')):
logger.error(translator.get('specify_image_or_video_output') + translator.get('exclamation_mark'), __name__)
return False
if mode == 'output' and not same_file_extension(state_manager.get_item('target_path'), state_manager.get_item('output_path')):
logger.error(translator.get('match_target_and_output_extension') + translator.get('exclamation_mark'), __name__)
return False
return True
def post_process() -> None:
read_static_image.cache_clear()
read_static_video_frame.cache_clear()
video_manager.clear_video_pool()
if state_manager.get_item('video_memory_strategy') == 'strict':
content_analyser.clear_inference_pool()
face_classifier.clear_inference_pool()
face_detector.clear_inference_pool()
face_landmarker.clear_inference_pool()
face_masker.clear_inference_pool()
face_recognizer.clear_inference_pool()
def debug_face(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
face_debugger_items = state_manager.get_item('face_debugger_items')
if 'bounding-box' in face_debugger_items:
temp_vision_frame = draw_bounding_box(target_face, temp_vision_frame)
if 'face-mask' in face_debugger_items:
temp_vision_frame = draw_face_mask(target_face, temp_vision_frame)
if 'face-landmark-5' in face_debugger_items:
temp_vision_frame = draw_face_landmark_5(target_face, temp_vision_frame)
if 'face-landmark-5/68' in face_debugger_items:
temp_vision_frame = draw_face_landmark_5_68(target_face, temp_vision_frame)
if 'face-landmark-68' in face_debugger_items:
temp_vision_frame = draw_face_landmark_68(target_face, temp_vision_frame)
if 'face-landmark-68/5' in face_debugger_items:
temp_vision_frame = draw_face_landmark_68_5(target_face, temp_vision_frame)
return temp_vision_frame
def draw_bounding_box(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
temp_vision_frame = numpy.ascontiguousarray(temp_vision_frame)
box_color = 0, 0, 255
border_color = 100, 100, 255
bounding_box = target_face.bounding_box.astype(numpy.int32)
x1, y1, x2, y2 = bounding_box
cv2.rectangle(temp_vision_frame, (x1, y1), (x2, y2), box_color, 2)
if target_face.angle == 0:
cv2.line(temp_vision_frame, (x1, y1), (x2, y1), border_color, 3)
if target_face.angle == 180:
cv2.line(temp_vision_frame, (x1, y2), (x2, y2), border_color, 3)
if target_face.angle == 90:
cv2.line(temp_vision_frame, (x2, y1), (x2, y2), border_color, 3)
if target_face.angle == 270:
cv2.line(temp_vision_frame, (x1, y1), (x1, y2), border_color, 3)
return temp_vision_frame
def draw_face_mask(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
crop_masks = []
temp_vision_frame = numpy.ascontiguousarray(temp_vision_frame)
face_landmark_5 = target_face.landmark_set.get('5')
face_landmark_68 = target_face.landmark_set.get('68')
face_landmark_5_68 = target_face.landmark_set.get('5/68')
crop_vision_frame, affine_matrix = warp_face_by_face_landmark_5(temp_vision_frame, face_landmark_5_68, 'arcface_128', (512, 512))
inverse_matrix = cv2.invertAffineTransform(affine_matrix)
temp_size = temp_vision_frame.shape[:2][::-1]
mask_color = 0, 255, 0
if numpy.array_equal(face_landmark_5, face_landmark_5_68):
mask_color = 255, 255, 0
if 'box' in state_manager.get_item('face_mask_types'):
box_mask = create_box_mask(crop_vision_frame, 0, state_manager.get_item('face_mask_padding'))
crop_masks.append(box_mask)
if 'occlusion' in state_manager.get_item('face_mask_types'):
occlusion_mask = create_occlusion_mask(crop_vision_frame)
crop_masks.append(occlusion_mask)
if 'area' in state_manager.get_item('face_mask_types'):
face_landmark_68 = cv2.transform(face_landmark_68.reshape(1, -1, 2), affine_matrix).reshape(-1, 2)
area_mask = create_area_mask(crop_vision_frame, face_landmark_68, state_manager.get_item('face_mask_areas'))
crop_masks.append(area_mask)
if 'region' in state_manager.get_item('face_mask_types'):
region_mask = create_region_mask(crop_vision_frame, state_manager.get_item('face_mask_regions'))
crop_masks.append(region_mask)
crop_mask = numpy.minimum.reduce(crop_masks).clip(0, 1)
crop_mask = (crop_mask * 255).astype(numpy.uint8)
inverse_vision_frame = cv2.warpAffine(crop_mask, inverse_matrix, temp_size)
inverse_vision_frame = cv2.threshold(inverse_vision_frame, 100, 255, cv2.THRESH_BINARY)[1]
inverse_contours, _ = cv2.findContours(inverse_vision_frame, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(temp_vision_frame, inverse_contours, -1, mask_color, 2)
return temp_vision_frame
def draw_face_landmark_5(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
temp_vision_frame = numpy.ascontiguousarray(temp_vision_frame)
face_landmark_5 = target_face.landmark_set.get('5')
point_color = 0, 0, 255
if numpy.any(face_landmark_5):
face_landmark_5 = face_landmark_5.astype(numpy.int32)
for point in face_landmark_5:
cv2.circle(temp_vision_frame, tuple(point), 3, point_color, -1)
return temp_vision_frame
def draw_face_landmark_5_68(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
temp_vision_frame = numpy.ascontiguousarray(temp_vision_frame)
face_landmark_5 = target_face.landmark_set.get('5')
face_landmark_5_68 = target_face.landmark_set.get('5/68')
point_color = 0, 255, 0
if numpy.array_equal(face_landmark_5, face_landmark_5_68):
point_color = 255, 255, 0
if numpy.any(face_landmark_5_68):
face_landmark_5_68 = face_landmark_5_68.astype(numpy.int32)
for point in face_landmark_5_68:
cv2.circle(temp_vision_frame, tuple(point), 3, point_color, -1)
return temp_vision_frame
def draw_face_landmark_68(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
temp_vision_frame = numpy.ascontiguousarray(temp_vision_frame)
face_landmark_68 = target_face.landmark_set.get('68')
face_landmark_68_5 = target_face.landmark_set.get('68/5')
point_color = 0, 255, 0
if numpy.array_equal(face_landmark_68, face_landmark_68_5):
point_color = 255, 255, 0
if numpy.any(face_landmark_68):
face_landmark_68 = face_landmark_68.astype(numpy.int32)
for point in face_landmark_68:
cv2.circle(temp_vision_frame, tuple(point), 3, point_color, -1)
return temp_vision_frame
def draw_face_landmark_68_5(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
temp_vision_frame = numpy.ascontiguousarray(temp_vision_frame)
face_landmark_68_5 = target_face.landmark_set.get('68/5')
point_color = 255, 255, 0
if numpy.any(face_landmark_68_5):
face_landmark_68_5 = face_landmark_68_5.astype(numpy.int32)
for point in face_landmark_68_5:
cv2.circle(temp_vision_frame, tuple(point), 3, point_color, -1)
return temp_vision_frame
def process_frame(inputs : FaceDebuggerInputs) -> ProcessorOutputs:
reference_vision_frame = inputs.get('reference_vision_frame')
target_vision_frame = inputs.get('target_vision_frame')
temp_vision_frame = inputs.get('temp_vision_frame')
temp_vision_mask = inputs.get('temp_vision_mask')
target_faces = select_faces(reference_vision_frame, target_vision_frame)
if target_faces:
for target_face in target_faces:
target_face = scale_face(target_face, target_vision_frame, temp_vision_frame)
temp_vision_frame = debug_face(target_face, temp_vision_frame)
return temp_vision_frame, temp_vision_mask

View File

@@ -1,16 +0,0 @@
from facefusion.types import Locals
LOCALS : Locals =\
{
'en':
{
'help':
{
'items': 'load a single or multiple processors (choices: {choices})'
},
'uis':
{
'items_checkbox_group': 'FACE DEBUGGER ITEMS'
}
}
}

View File

@@ -1,13 +0,0 @@
from typing import Literal, TypedDict
from facefusion.types import Mask, VisionFrame
FaceDebuggerInputs = TypedDict('FaceDebuggerInputs',
{
'reference_vision_frame' : VisionFrame,
'target_vision_frame' : VisionFrame,
'temp_vision_frame' : VisionFrame,
'temp_vision_mask' : Mask
})
FaceDebuggerItem = Literal['bounding-box', 'face-landmark-5', 'face-landmark-5/68', 'face-landmark-68', 'face-landmark-68/5', 'face-mask']

View File

@@ -1,72 +1,63 @@
from argparse import ArgumentParser from argparse import ArgumentParser
from functools import lru_cache from typing import List, Tuple
from typing import Tuple
import cv2 import cv2
import numpy import numpy
import facefusion.jobs.job_manager import facefusion.jobs.job_manager
import facefusion.jobs.job_store import facefusion.jobs.job_store
from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, inference_manager, logger, state_manager, translator, video_manager import facefusion.processors.core as processors
from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, inference_manager, logger, process_manager, state_manager, wording
from facefusion.common_helper import create_float_metavar from facefusion.common_helper import create_float_metavar
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url from facefusion.download import conditional_download_hashes, conditional_download_sources
from facefusion.face_analyser import scale_face from facefusion.face_analyser import get_many_faces, get_one_face
from facefusion.face_helper import paste_back, scale_face_landmark_5, warp_face_by_face_landmark_5 from facefusion.face_helper import paste_back, scale_face_landmark_5, warp_face_by_face_landmark_5
from facefusion.face_masker import create_box_mask from facefusion.face_masker import create_static_box_mask
from facefusion.face_selector import select_faces from facefusion.face_selector import find_similar_faces, sort_and_filter_faces
from facefusion.face_store import get_reference_faces
from facefusion.filesystem import in_directory, is_image, is_video, resolve_relative_path, same_file_extension from facefusion.filesystem import in_directory, is_image, is_video, resolve_relative_path, same_file_extension
from facefusion.processors.live_portrait import create_rotation, limit_angle, limit_expression from facefusion.processors import choices as processors_choices
from facefusion.processors.modules.face_editor import choices as face_editor_choices from facefusion.processors.live_portrait import create_rotation, limit_euler_angles, limit_expression
from facefusion.processors.modules.face_editor.types import FaceEditorInputs from facefusion.processors.typing import FaceEditorInputs, LivePortraitExpression, LivePortraitFeatureVolume, LivePortraitMotionPoints, LivePortraitPitch, LivePortraitRoll, LivePortraitRotation, LivePortraitScale, LivePortraitTranslation, LivePortraitYaw
from facefusion.processors.types import LivePortraitExpression, LivePortraitFeatureVolume, LivePortraitMotionPoints, LivePortraitPitch, LivePortraitRoll, LivePortraitRotation, LivePortraitScale, LivePortraitTranslation, LivePortraitYaw, ProcessorOutputs
from facefusion.program_helper import find_argument_group from facefusion.program_helper import find_argument_group
from facefusion.thread_helper import conditional_thread_semaphore, thread_semaphore from facefusion.thread_helper import conditional_thread_semaphore, thread_semaphore
from facefusion.types import ApplyStateItem, Args, DownloadScope, Face, FaceLandmark68, InferencePool, ModelOptions, ModelSet, ProcessMode, VisionFrame from facefusion.typing import ApplyStateItem, Args, Face, FaceLandmark68, InferencePool, ModelOptions, ModelSet, ProcessMode, QueuePayload, UpdateProgress, VisionFrame
from facefusion.vision import read_static_image, read_static_video_frame from facefusion.vision import read_image, read_static_image, write_image
MODEL_SET : ModelSet =\
@lru_cache() {
def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
return\
{
'live_portrait': 'live_portrait':
{ {
'__metadata__':
{
'vendor': 'KwaiVGI',
'license': 'MIT',
'year': 2024
},
'hashes': 'hashes':
{ {
'feature_extractor': 'feature_extractor':
{ {
'url': resolve_download_url('models-3.0.0', 'live_portrait_feature_extractor.hash'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/live_portrait_feature_extractor.hash',
'path': resolve_relative_path('../.assets/models/live_portrait_feature_extractor.hash') 'path': resolve_relative_path('../.assets/models/live_portrait_feature_extractor.hash')
}, },
'motion_extractor': 'motion_extractor':
{ {
'url': resolve_download_url('models-3.0.0', 'live_portrait_motion_extractor.hash'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/live_portrait_motion_extractor.hash',
'path': resolve_relative_path('../.assets/models/live_portrait_motion_extractor.hash') 'path': resolve_relative_path('../.assets/models/live_portrait_motion_extractor.hash')
}, },
'eye_retargeter': 'eye_retargeter':
{ {
'url': resolve_download_url('models-3.0.0', 'live_portrait_eye_retargeter.hash'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/live_portrait_eye_retargeter.hash',
'path': resolve_relative_path('../.assets/models/live_portrait_eye_retargeter.hash') 'path': resolve_relative_path('../.assets/models/live_portrait_eye_retargeter.hash')
}, },
'lip_retargeter': 'lip_retargeter':
{ {
'url': resolve_download_url('models-3.0.0', 'live_portrait_lip_retargeter.hash'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/live_portrait_lip_retargeter.hash',
'path': resolve_relative_path('../.assets/models/live_portrait_lip_retargeter.hash') 'path': resolve_relative_path('../.assets/models/live_portrait_lip_retargeter.hash')
}, },
'stitcher': 'stitcher':
{ {
'url': resolve_download_url('models-3.0.0', 'live_portrait_stitcher.hash'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/live_portrait_stitcher.hash',
'path': resolve_relative_path('../.assets/models/live_portrait_stitcher.hash') 'path': resolve_relative_path('../.assets/models/live_portrait_stitcher.hash')
}, },
'generator': 'generator':
{ {
'url': resolve_download_url('models-3.0.0', 'live_portrait_generator.hash'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/live_portrait_generator.hash',
'path': resolve_relative_path('../.assets/models/live_portrait_generator.hash') 'path': resolve_relative_path('../.assets/models/live_portrait_generator.hash')
} }
}, },
@@ -74,76 +65,75 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
{ {
'feature_extractor': 'feature_extractor':
{ {
'url': resolve_download_url('models-3.0.0', 'live_portrait_feature_extractor.onnx'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/live_portrait_feature_extractor.onnx',
'path': resolve_relative_path('../.assets/models/live_portrait_feature_extractor.onnx') 'path': resolve_relative_path('../.assets/models/live_portrait_feature_extractor.onnx')
}, },
'motion_extractor': 'motion_extractor':
{ {
'url': resolve_download_url('models-3.0.0', 'live_portrait_motion_extractor.onnx'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/live_portrait_motion_extractor.onnx',
'path': resolve_relative_path('../.assets/models/live_portrait_motion_extractor.onnx') 'path': resolve_relative_path('../.assets/models/live_portrait_motion_extractor.onnx')
}, },
'eye_retargeter': 'eye_retargeter':
{ {
'url': resolve_download_url('models-3.0.0', 'live_portrait_eye_retargeter.onnx'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/live_portrait_eye_retargeter.onnx',
'path': resolve_relative_path('../.assets/models/live_portrait_eye_retargeter.onnx') 'path': resolve_relative_path('../.assets/models/live_portrait_eye_retargeter.onnx')
}, },
'lip_retargeter': 'lip_retargeter':
{ {
'url': resolve_download_url('models-3.0.0', 'live_portrait_lip_retargeter.onnx'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/live_portrait_lip_retargeter.onnx',
'path': resolve_relative_path('../.assets/models/live_portrait_lip_retargeter.onnx') 'path': resolve_relative_path('../.assets/models/live_portrait_lip_retargeter.onnx')
}, },
'stitcher': 'stitcher':
{ {
'url': resolve_download_url('models-3.0.0', 'live_portrait_stitcher.onnx'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/live_portrait_stitcher.onnx',
'path': resolve_relative_path('../.assets/models/live_portrait_stitcher.onnx') 'path': resolve_relative_path('../.assets/models/live_portrait_stitcher.onnx')
}, },
'generator': 'generator':
{ {
'url': resolve_download_url('models-3.0.0', 'live_portrait_generator.onnx'), 'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/live_portrait_generator.onnx',
'path': resolve_relative_path('../.assets/models/live_portrait_generator.onnx') 'path': resolve_relative_path('../.assets/models/live_portrait_generator.onnx')
} }
}, },
'template': 'ffhq_512', 'template': 'ffhq_512',
'size': (512, 512) 'size': (512, 512)
} }
} }
def get_inference_pool() -> InferencePool: def get_inference_pool() -> InferencePool:
model_names = [ state_manager.get_item('face_editor_model') ] model_sources = get_model_options().get('sources')
model_source_set = get_model_options().get('sources') model_context = __name__ + '.' + state_manager.get_item('face_editor_model')
return inference_manager.get_inference_pool(model_context, model_sources)
return inference_manager.get_inference_pool(__name__, model_names, model_source_set)
def clear_inference_pool() -> None: def clear_inference_pool() -> None:
model_names = [ state_manager.get_item('face_editor_model') ] model_context = __name__ + '.' + state_manager.get_item('face_editor_model')
inference_manager.clear_inference_pool(__name__, model_names) inference_manager.clear_inference_pool(model_context)
def get_model_options() -> ModelOptions: def get_model_options() -> ModelOptions:
model_name = state_manager.get_item('face_editor_model') face_editor_model = state_manager.get_item('face_editor_model')
return create_static_model_set('full').get(model_name) return MODEL_SET.get(face_editor_model)
def register_args(program : ArgumentParser) -> None: def register_args(program : ArgumentParser) -> None:
group_processors = find_argument_group(program, 'processors') group_processors = find_argument_group(program, 'processors')
if group_processors: if group_processors:
group_processors.add_argument('--face-editor-model', help = translator.get('help.model', __package__), default = config.get_str_value('processors', 'face_editor_model', 'live_portrait'), choices = face_editor_choices.face_editor_models) group_processors.add_argument('--face-editor-model', help = wording.get('help.face_editor_model'), default = config.get_str_value('processors.face_editor_model', 'live_portrait'), choices = processors_choices.face_editor_models)
group_processors.add_argument('--face-editor-eyebrow-direction', help = translator.get('help.eyebrow_direction', __package__), type = float, default = config.get_float_value('processors', 'face_editor_eyebrow_direction', '0'), choices = face_editor_choices.face_editor_eyebrow_direction_range, metavar = create_float_metavar(face_editor_choices.face_editor_eyebrow_direction_range)) group_processors.add_argument('--face-editor-eyebrow-direction', help = wording.get('help.face_editor_eyebrow_direction'), type = float, default = config.get_float_value('processors.face_editor_eyebrow_direction', '0'), choices = processors_choices.face_editor_eyebrow_direction_range, metavar = create_float_metavar(processors_choices.face_editor_eyebrow_direction_range))
group_processors.add_argument('--face-editor-eye-gaze-horizontal', help = translator.get('help.eye_gaze_horizontal', __package__), type = float, default = config.get_float_value('processors', 'face_editor_eye_gaze_horizontal', '0'), choices = face_editor_choices.face_editor_eye_gaze_horizontal_range, metavar = create_float_metavar(face_editor_choices.face_editor_eye_gaze_horizontal_range)) group_processors.add_argument('--face-editor-eye-gaze-horizontal', help = wording.get('help.face_editor_eye_gaze_horizontal'), type = float, default = config.get_float_value('processors.face_editor_eye_gaze_horizontal', '0'), choices = processors_choices.face_editor_eye_gaze_horizontal_range, metavar = create_float_metavar(processors_choices.face_editor_eye_gaze_horizontal_range))
group_processors.add_argument('--face-editor-eye-gaze-vertical', help = translator.get('help.eye_gaze_vertical', __package__), type = float, default = config.get_float_value('processors', 'face_editor_eye_gaze_vertical', '0'), choices = face_editor_choices.face_editor_eye_gaze_vertical_range, metavar = create_float_metavar(face_editor_choices.face_editor_eye_gaze_vertical_range)) group_processors.add_argument('--face-editor-eye-gaze-vertical', help = wording.get('help.face_editor_eye_gaze_vertical'), type = float, default = config.get_float_value('processors.face_editor_eye_gaze_vertical', '0'), choices = processors_choices.face_editor_eye_gaze_vertical_range, metavar = create_float_metavar(processors_choices.face_editor_eye_gaze_vertical_range))
group_processors.add_argument('--face-editor-eye-open-ratio', help = translator.get('help.eye_open_ratio', __package__), type = float, default = config.get_float_value('processors', 'face_editor_eye_open_ratio', '0'), choices = face_editor_choices.face_editor_eye_open_ratio_range, metavar = create_float_metavar(face_editor_choices.face_editor_eye_open_ratio_range)) group_processors.add_argument('--face-editor-eye-open-ratio', help = wording.get('help.face_editor_eye_open_ratio'), type = float, default = config.get_float_value('processors.face_editor_eye_open_ratio', '0'), choices = processors_choices.face_editor_eye_open_ratio_range, metavar = create_float_metavar(processors_choices.face_editor_eye_open_ratio_range))
group_processors.add_argument('--face-editor-lip-open-ratio', help = translator.get('help.lip_open_ratio', __package__), type = float, default = config.get_float_value('processors', 'face_editor_lip_open_ratio', '0'), choices = face_editor_choices.face_editor_lip_open_ratio_range, metavar = create_float_metavar(face_editor_choices.face_editor_lip_open_ratio_range)) group_processors.add_argument('--face-editor-lip-open-ratio', help = wording.get('help.face_editor_lip_open_ratio'), type = float, default = config.get_float_value('processors.face_editor_lip_open_ratio', '0'), choices = processors_choices.face_editor_lip_open_ratio_range, metavar = create_float_metavar(processors_choices.face_editor_lip_open_ratio_range))
group_processors.add_argument('--face-editor-mouth-grim', help = translator.get('help.mouth_grim', __package__), type = float, default = config.get_float_value('processors', 'face_editor_mouth_grim', '0'), choices = face_editor_choices.face_editor_mouth_grim_range, metavar = create_float_metavar(face_editor_choices.face_editor_mouth_grim_range)) group_processors.add_argument('--face-editor-mouth-grim', help = wording.get('help.face_editor_mouth_grim'), type = float, default = config.get_float_value('processors.face_editor_mouth_grim', '0'), choices = processors_choices.face_editor_mouth_grim_range, metavar = create_float_metavar(processors_choices.face_editor_mouth_grim_range))
group_processors.add_argument('--face-editor-mouth-pout', help = translator.get('help.mouth_pout', __package__), type = float, default = config.get_float_value('processors', 'face_editor_mouth_pout', '0'), choices = face_editor_choices.face_editor_mouth_pout_range, metavar = create_float_metavar(face_editor_choices.face_editor_mouth_pout_range)) group_processors.add_argument('--face-editor-mouth-pout', help = wording.get('help.face_editor_mouth_pout'), type = float, default = config.get_float_value('processors.face_editor_mouth_pout', '0'), choices = processors_choices.face_editor_mouth_pout_range, metavar = create_float_metavar(processors_choices.face_editor_mouth_pout_range))
group_processors.add_argument('--face-editor-mouth-purse', help = translator.get('help.mouth_purse', __package__), type = float, default = config.get_float_value('processors', 'face_editor_mouth_purse', '0'), choices = face_editor_choices.face_editor_mouth_purse_range, metavar = create_float_metavar(face_editor_choices.face_editor_mouth_purse_range)) group_processors.add_argument('--face-editor-mouth-purse', help = wording.get('help.face_editor_mouth_purse'), type = float, default = config.get_float_value('processors.face_editor_mouth_purse', '0'), choices = processors_choices.face_editor_mouth_purse_range, metavar = create_float_metavar(processors_choices.face_editor_mouth_purse_range))
group_processors.add_argument('--face-editor-mouth-smile', help = translator.get('help.mouth_smile', __package__), type = float, default = config.get_float_value('processors', 'face_editor_mouth_smile', '0'), choices = face_editor_choices.face_editor_mouth_smile_range, metavar = create_float_metavar(face_editor_choices.face_editor_mouth_smile_range)) group_processors.add_argument('--face-editor-mouth-smile', help = wording.get('help.face_editor_mouth_smile'), type = float, default = config.get_float_value('processors.face_editor_mouth_smile', '0'), choices = processors_choices.face_editor_mouth_smile_range, metavar = create_float_metavar(processors_choices.face_editor_mouth_smile_range))
group_processors.add_argument('--face-editor-mouth-position-horizontal', help = translator.get('help.mouth_position_horizontal', __package__), type = float, default = config.get_float_value('processors', 'face_editor_mouth_position_horizontal', '0'), choices = face_editor_choices.face_editor_mouth_position_horizontal_range, metavar = create_float_metavar(face_editor_choices.face_editor_mouth_position_horizontal_range)) group_processors.add_argument('--face-editor-mouth-position-horizontal', help = wording.get('help.face_editor_mouth_position_horizontal'), type = float, default = config.get_float_value('processors.face_editor_mouth_position_horizontal', '0'), choices = processors_choices.face_editor_mouth_position_horizontal_range, metavar = create_float_metavar(processors_choices.face_editor_mouth_position_horizontal_range))
group_processors.add_argument('--face-editor-mouth-position-vertical', help = translator.get('help.mouth_position_vertical', __package__), type = float, default = config.get_float_value('processors', 'face_editor_mouth_position_vertical', '0'), choices = face_editor_choices.face_editor_mouth_position_vertical_range, metavar = create_float_metavar(face_editor_choices.face_editor_mouth_position_vertical_range)) group_processors.add_argument('--face-editor-mouth-position-vertical', help = wording.get('help.face_editor_mouth_position_vertical'), type = float, default = config.get_float_value('processors.face_editor_mouth_position_vertical', '0'), choices = processors_choices.face_editor_mouth_position_vertical_range, metavar = create_float_metavar(processors_choices.face_editor_mouth_position_vertical_range))
group_processors.add_argument('--face-editor-head-pitch', help = translator.get('help.head_pitch', __package__), type = float, default = config.get_float_value('processors', 'face_editor_head_pitch', '0'), choices = face_editor_choices.face_editor_head_pitch_range, metavar = create_float_metavar(face_editor_choices.face_editor_head_pitch_range)) group_processors.add_argument('--face-editor-head-pitch', help = wording.get('help.face_editor_head_pitch'), type = float, default = config.get_float_value('processors.face_editor_head_pitch', '0'), choices = processors_choices.face_editor_head_pitch_range, metavar = create_float_metavar(processors_choices.face_editor_head_pitch_range))
group_processors.add_argument('--face-editor-head-yaw', help = translator.get('help.head_yaw', __package__), type = float, default = config.get_float_value('processors', 'face_editor_head_yaw', '0'), choices = face_editor_choices.face_editor_head_yaw_range, metavar = create_float_metavar(face_editor_choices.face_editor_head_yaw_range)) group_processors.add_argument('--face-editor-head-yaw', help = wording.get('help.face_editor_head_yaw'), type = float, default = config.get_float_value('processors.face_editor_head_yaw', '0'), choices = processors_choices.face_editor_head_yaw_range, metavar = create_float_metavar(processors_choices.face_editor_head_yaw_range))
group_processors.add_argument('--face-editor-head-roll', help = translator.get('help.head_roll', __package__), type = float, default = config.get_float_value('processors', 'face_editor_head_roll', '0'), choices = face_editor_choices.face_editor_head_roll_range, metavar = create_float_metavar(face_editor_choices.face_editor_head_roll_range)) group_processors.add_argument('--face-editor-head-roll', help = wording.get('help.face_editor_head_roll'), type = float, default = config.get_float_value('processors.face_editor_head_roll', '0'), choices = processors_choices.face_editor_head_roll_range, metavar = create_float_metavar(processors_choices.face_editor_head_roll_range))
facefusion.jobs.job_store.register_step_keys([ 'face_editor_model', 'face_editor_eyebrow_direction', 'face_editor_eye_gaze_horizontal', 'face_editor_eye_gaze_vertical', 'face_editor_eye_open_ratio', 'face_editor_lip_open_ratio', 'face_editor_mouth_grim', 'face_editor_mouth_pout', 'face_editor_mouth_purse', 'face_editor_mouth_smile', 'face_editor_mouth_position_horizontal', 'face_editor_mouth_position_vertical', 'face_editor_head_pitch', 'face_editor_head_yaw', 'face_editor_head_roll' ]) facefusion.jobs.job_store.register_step_keys([ 'face_editor_model', 'face_editor_eyebrow_direction', 'face_editor_eye_gaze_horizontal', 'face_editor_eye_gaze_vertical', 'face_editor_eye_open_ratio', 'face_editor_lip_open_ratio', 'face_editor_mouth_grim', 'face_editor_mouth_pout', 'face_editor_mouth_purse', 'face_editor_mouth_smile', 'face_editor_mouth_position_horizontal', 'face_editor_mouth_position_vertical', 'face_editor_head_pitch', 'face_editor_head_yaw', 'face_editor_head_roll' ])
@@ -166,29 +156,28 @@ def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
def pre_check() -> bool: def pre_check() -> bool:
model_hash_set = get_model_options().get('hashes') download_directory_path = resolve_relative_path('../.assets/models')
model_source_set = get_model_options().get('sources') model_hashes = get_model_options().get('hashes')
model_sources = get_model_options().get('sources')
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set) return conditional_download_hashes(download_directory_path, model_hashes) and conditional_download_sources(download_directory_path, model_sources)
def pre_process(mode : ProcessMode) -> bool: def pre_process(mode : ProcessMode) -> bool:
if mode in [ 'output', 'preview' ] and not is_image(state_manager.get_item('target_path')) and not is_video(state_manager.get_item('target_path')): if mode in [ 'output', 'preview' ] and not is_image(state_manager.get_item('target_path')) and not is_video(state_manager.get_item('target_path')):
logger.error(translator.get('choose_image_or_video_target') + translator.get('exclamation_mark'), __name__) logger.error(wording.get('choose_image_or_video_target') + wording.get('exclamation_mark'), __name__)
return False return False
if mode == 'output' and not in_directory(state_manager.get_item('output_path')): if mode == 'output' and not in_directory(state_manager.get_item('output_path')):
logger.error(translator.get('specify_image_or_video_output') + translator.get('exclamation_mark'), __name__) logger.error(wording.get('specify_image_or_video_output') + wording.get('exclamation_mark'), __name__)
return False return False
if mode == 'output' and not same_file_extension(state_manager.get_item('target_path'), state_manager.get_item('output_path')): if mode == 'output' and not same_file_extension([ state_manager.get_item('target_path'), state_manager.get_item('output_path') ]):
logger.error(translator.get('match_target_and_output_extension') + translator.get('exclamation_mark'), __name__) logger.error(wording.get('match_target_and_output_extension') + wording.get('exclamation_mark'), __name__)
return False return False
return True return True
def post_process() -> None: def post_process() -> None:
read_static_image.cache_clear() read_static_image.cache_clear()
read_static_video_frame.cache_clear()
video_manager.clear_video_pool()
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]: if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
clear_inference_pool() clear_inference_pool()
if state_manager.get_item('video_memory_strategy') == 'strict': if state_manager.get_item('video_memory_strategy') == 'strict':
@@ -205,12 +194,12 @@ def edit_face(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFram
model_size = get_model_options().get('size') model_size = get_model_options().get('size')
face_landmark_5 = scale_face_landmark_5(target_face.landmark_set.get('5/68'), 1.5) face_landmark_5 = scale_face_landmark_5(target_face.landmark_set.get('5/68'), 1.5)
crop_vision_frame, affine_matrix = warp_face_by_face_landmark_5(temp_vision_frame, face_landmark_5, model_template, model_size) crop_vision_frame, affine_matrix = warp_face_by_face_landmark_5(temp_vision_frame, face_landmark_5, model_template, model_size)
box_mask = create_box_mask(crop_vision_frame, state_manager.get_item('face_mask_blur'), (0, 0, 0, 0)) box_mask = create_static_box_mask(crop_vision_frame.shape[:2][::-1], state_manager.get_item('face_mask_blur'), (0, 0, 0, 0))
crop_vision_frame = prepare_crop_frame(crop_vision_frame) crop_vision_frame = prepare_crop_frame(crop_vision_frame)
crop_vision_frame = apply_edit(crop_vision_frame, target_face.landmark_set.get('68')) crop_vision_frame = apply_edit(crop_vision_frame, target_face.landmark_set.get('68'))
crop_vision_frame = normalize_crop_frame(crop_vision_frame) crop_vision_frame = normalize_crop_frame(crop_vision_frame)
paste_vision_frame = paste_back(temp_vision_frame, crop_vision_frame, box_mask, affine_matrix) temp_vision_frame = paste_back(temp_vision_frame, crop_vision_frame, box_mask, affine_matrix)
return paste_vision_frame return temp_vision_frame
def apply_edit(crop_vision_frame : VisionFrame, face_landmark_68 : FaceLandmark68) -> VisionFrame: def apply_edit(crop_vision_frame : VisionFrame, face_landmark_68 : FaceLandmark68) -> VisionFrame:
@@ -348,8 +337,8 @@ def edit_eye_gaze(expression : LivePortraitExpression) -> LivePortraitExpression
def edit_eye_open(motion_points : LivePortraitMotionPoints, face_landmark_68 : FaceLandmark68) -> LivePortraitMotionPoints: def edit_eye_open(motion_points : LivePortraitMotionPoints, face_landmark_68 : FaceLandmark68) -> LivePortraitMotionPoints:
face_editor_eye_open_ratio = state_manager.get_item('face_editor_eye_open_ratio') face_editor_eye_open_ratio = state_manager.get_item('face_editor_eye_open_ratio')
left_eye_ratio = calculate_distance_ratio(face_landmark_68, 37, 40, 39, 36) left_eye_ratio = calc_distance_ratio(face_landmark_68, 37, 40, 39, 36)
right_eye_ratio = calculate_distance_ratio(face_landmark_68, 43, 46, 45, 42) right_eye_ratio = calc_distance_ratio(face_landmark_68, 43, 46, 45, 42)
if face_editor_eye_open_ratio < 0: if face_editor_eye_open_ratio < 0:
eye_motion_points = numpy.concatenate([ motion_points.ravel(), [ left_eye_ratio, right_eye_ratio, 0.0 ] ]) eye_motion_points = numpy.concatenate([ motion_points.ravel(), [ left_eye_ratio, right_eye_ratio, 0.0 ] ])
@@ -363,7 +352,7 @@ def edit_eye_open(motion_points : LivePortraitMotionPoints, face_landmark_68 : F
def edit_lip_open(motion_points : LivePortraitMotionPoints, face_landmark_68 : FaceLandmark68) -> LivePortraitMotionPoints: def edit_lip_open(motion_points : LivePortraitMotionPoints, face_landmark_68 : FaceLandmark68) -> LivePortraitMotionPoints:
face_editor_lip_open_ratio = state_manager.get_item('face_editor_lip_open_ratio') face_editor_lip_open_ratio = state_manager.get_item('face_editor_lip_open_ratio')
lip_ratio = calculate_distance_ratio(face_landmark_68, 62, 66, 54, 48) lip_ratio = calc_distance_ratio(face_landmark_68, 62, 66, 54, 48)
if face_editor_lip_open_ratio < 0: if face_editor_lip_open_ratio < 0:
lip_motion_points = numpy.concatenate([ motion_points.ravel(), [ lip_ratio, 0.0 ] ]) lip_motion_points = numpy.concatenate([ motion_points.ravel(), [ lip_ratio, 0.0 ] ])
@@ -456,12 +445,12 @@ def edit_head_rotation(pitch : LivePortraitPitch, yaw : LivePortraitYaw, roll :
edit_pitch = pitch + float(numpy.interp(face_editor_head_pitch, [ -1, 1 ], [ 20, -20 ])) edit_pitch = pitch + float(numpy.interp(face_editor_head_pitch, [ -1, 1 ], [ 20, -20 ]))
edit_yaw = yaw + float(numpy.interp(face_editor_head_yaw, [ -1, 1 ], [ 60, -60 ])) edit_yaw = yaw + float(numpy.interp(face_editor_head_yaw, [ -1, 1 ], [ 60, -60 ]))
edit_roll = roll + float(numpy.interp(face_editor_head_roll, [ -1, 1 ], [ -15, 15 ])) edit_roll = roll + float(numpy.interp(face_editor_head_roll, [ -1, 1 ], [ -15, 15 ]))
edit_pitch, edit_yaw, edit_roll = limit_angle(pitch, yaw, roll, edit_pitch, edit_yaw, edit_roll) edit_pitch, edit_yaw, edit_roll = limit_euler_angles(pitch, yaw, roll, edit_pitch, edit_yaw, edit_roll)
rotation = create_rotation(edit_pitch, edit_yaw, edit_roll) rotation = create_rotation(edit_pitch, edit_yaw, edit_roll)
return rotation return rotation
def calculate_distance_ratio(face_landmark_68 : FaceLandmark68, top_index : int, bottom_index : int, left_index : int, right_index : int) -> float: def calc_distance_ratio(face_landmark_68 : FaceLandmark68, top_index : int, bottom_index : int, left_index : int, right_index : int) -> float:
vertical_direction = face_landmark_68[top_index] - face_landmark_68[bottom_index] vertical_direction = face_landmark_68[top_index] - face_landmark_68[bottom_index]
horizontal_direction = face_landmark_68[left_index] - face_landmark_68[right_index] horizontal_direction = face_landmark_68[left_index] - face_landmark_68[right_index]
distance_ratio = float(numpy.linalg.norm(vertical_direction) / (numpy.linalg.norm(horizontal_direction) + 1e-6)) distance_ratio = float(numpy.linalg.norm(vertical_direction) / (numpy.linalg.norm(horizontal_direction) + 1e-6))
@@ -484,16 +473,56 @@ def normalize_crop_frame(crop_vision_frame : VisionFrame) -> VisionFrame:
return crop_vision_frame return crop_vision_frame
def process_frame(inputs : FaceEditorInputs) -> ProcessorOutputs: def get_reference_frame(source_face : Face, target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
reference_vision_frame = inputs.get('reference_vision_frame') pass
def process_frame(inputs : FaceEditorInputs) -> VisionFrame:
reference_faces = inputs.get('reference_faces')
target_vision_frame = inputs.get('target_vision_frame') target_vision_frame = inputs.get('target_vision_frame')
temp_vision_frame = inputs.get('temp_vision_frame') many_faces = sort_and_filter_faces(get_many_faces([ target_vision_frame ]))
temp_vision_mask = inputs.get('temp_vision_mask')
target_faces = select_faces(reference_vision_frame, target_vision_frame)
if target_faces: if state_manager.get_item('face_selector_mode') == 'many':
for target_face in target_faces: if many_faces:
target_face = scale_face(target_face, target_vision_frame, temp_vision_frame) for target_face in many_faces:
temp_vision_frame = edit_face(target_face, temp_vision_frame) target_vision_frame = edit_face(target_face, target_vision_frame)
if state_manager.get_item('face_selector_mode') == 'one':
target_face = get_one_face(many_faces)
if target_face:
target_vision_frame = edit_face(target_face, target_vision_frame)
if state_manager.get_item('face_selector_mode') == 'reference':
similar_faces = find_similar_faces(many_faces, reference_faces, state_manager.get_item('reference_face_distance'))
if similar_faces:
for similar_face in similar_faces:
target_vision_frame = edit_face(similar_face, target_vision_frame)
return target_vision_frame
return temp_vision_frame, temp_vision_mask
def process_frames(source_path : List[str], queue_payloads : List[QueuePayload], update_progress : UpdateProgress) -> None:
reference_faces = get_reference_faces() if 'reference' in state_manager.get_item('face_selector_mode') else None
for queue_payload in process_manager.manage(queue_payloads):
target_vision_path = queue_payload['frame_path']
target_vision_frame = read_image(target_vision_path)
output_vision_frame = process_frame(
{
'reference_faces': reference_faces,
'target_vision_frame': target_vision_frame
})
write_image(target_vision_path, output_vision_frame)
update_progress(1)
def process_image(source_path : str, target_path : str, output_path : str) -> None:
reference_faces = get_reference_faces() if 'reference' in state_manager.get_item('face_selector_mode') else None
target_vision_frame = read_static_image(target_path)
output_vision_frame = process_frame(
{
'reference_faces': reference_faces,
'target_vision_frame': target_vision_frame
})
write_image(output_path, output_vision_frame)
def process_video(source_paths : List[str], temp_frame_paths : List[str]) -> None:
processors.multi_process_frames(None, temp_frame_paths, process_frames)

View File

@@ -1,21 +0,0 @@
from typing import List, Sequence
from facefusion.common_helper import create_float_range
from facefusion.processors.modules.face_editor.types import FaceEditorModel
face_editor_models : List[FaceEditorModel] = [ 'live_portrait' ]
face_editor_eyebrow_direction_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
face_editor_eye_gaze_horizontal_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
face_editor_eye_gaze_vertical_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
face_editor_eye_open_ratio_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
face_editor_lip_open_ratio_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
face_editor_mouth_grim_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
face_editor_mouth_pout_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
face_editor_mouth_purse_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
face_editor_mouth_smile_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
face_editor_mouth_position_horizontal_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
face_editor_mouth_position_vertical_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
face_editor_head_pitch_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
face_editor_head_yaw_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
face_editor_head_roll_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)

View File

@@ -1,44 +0,0 @@
from facefusion.types import Locals
LOCALS : Locals =\
{
'en':
{
'help':
{
'model': 'choose the model responsible for editing the face',
'eyebrow_direction': 'specify the eyebrow direction',
'eye_gaze_horizontal': 'specify the horizontal eye gaze',
'eye_gaze_vertical': 'specify the vertical eye gaze',
'eye_open_ratio': 'specify the ratio of eye opening',
'lip_open_ratio': 'specify the ratio of lip opening',
'mouth_grim': 'specify the mouth grim',
'mouth_pout': 'specify the mouth pout',
'mouth_purse': 'specify the mouth purse',
'mouth_smile': 'specify the mouth smile',
'mouth_position_horizontal': 'specify the horizontal mouth position',
'mouth_position_vertical': 'specify the vertical mouth position',
'head_pitch': 'specify the head pitch',
'head_yaw': 'specify the head yaw',
'head_roll': 'specify the head roll'
},
'uis':
{
'eyebrow_direction_slider': 'FACE EDITOR EYEBROW DIRECTION',
'eye_gaze_horizontal_slider': 'FACE EDITOR EYE GAZE HORIZONTAL',
'eye_gaze_vertical_slider': 'FACE EDITOR EYE GAZE VERTICAL',
'eye_open_ratio_slider': 'FACE EDITOR EYE OPEN RATIO',
'head_pitch_slider': 'FACE EDITOR HEAD PITCH',
'head_roll_slider': 'FACE EDITOR HEAD ROLL',
'head_yaw_slider': 'FACE EDITOR HEAD YAW',
'lip_open_ratio_slider': 'FACE EDITOR LIP OPEN RATIO',
'model_dropdown': 'FACE EDITOR MODEL',
'mouth_grim_slider': 'FACE EDITOR MOUTH GRIM',
'mouth_position_horizontal_slider': 'FACE EDITOR MOUTH POSITION HORIZONTAL',
'mouth_position_vertical_slider': 'FACE EDITOR MOUTH POSITION VERTICAL',
'mouth_pout_slider': 'FACE EDITOR MOUTH POUT',
'mouth_purse_slider': 'FACE EDITOR MOUTH PURSE',
'mouth_smile_slider': 'FACE EDITOR MOUTH SMILE'
}
}
}

View File

@@ -1,13 +0,0 @@
from typing import Literal, TypedDict
from facefusion.types import Mask, VisionFrame
FaceEditorInputs = TypedDict('FaceEditorInputs',
{
'reference_vision_frame' : VisionFrame,
'target_vision_frame' : VisionFrame,
'temp_vision_frame' : VisionFrame,
'temp_vision_mask' : Mask
})
FaceEditorModel = Literal['live_portrait']

View File

@@ -0,0 +1,397 @@
from argparse import ArgumentParser
from typing import List
import cv2
import numpy
import facefusion.jobs.job_manager
import facefusion.jobs.job_store
import facefusion.processors.core as processors
from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, inference_manager, logger, process_manager, state_manager, wording
from facefusion.common_helper import create_int_metavar
from facefusion.download import conditional_download_hashes, conditional_download_sources
from facefusion.face_analyser import get_many_faces, get_one_face
from facefusion.face_helper import paste_back, warp_face_by_face_landmark_5
from facefusion.face_masker import create_occlusion_mask, create_static_box_mask
from facefusion.face_selector import find_similar_faces, sort_and_filter_faces
from facefusion.face_store import get_reference_faces
from facefusion.filesystem import in_directory, is_image, is_video, resolve_relative_path, same_file_extension
from facefusion.processors import choices as processors_choices
from facefusion.processors.typing import FaceEnhancerInputs
from facefusion.program_helper import find_argument_group
from facefusion.thread_helper import thread_semaphore
from facefusion.typing import ApplyStateItem, Args, Face, InferencePool, ModelOptions, ModelSet, ProcessMode, QueuePayload, UpdateProgress, VisionFrame
from facefusion.vision import read_image, read_static_image, write_image
MODEL_SET : ModelSet =\
{
'codeformer':
{
'hashes':
{
'face_enhancer':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/codeformer.hash',
'path': resolve_relative_path('../.assets/models/codeformer.hash')
}
},
'sources':
{
'face_enhancer':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/codeformer.onnx',
'path': resolve_relative_path('../.assets/models/codeformer.onnx')
}
},
'template': 'ffhq_512',
'size': (512, 512)
},
'gfpgan_1.2':
{
'hashes':
{
'face_enhancer':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/gfpgan_1.2.hash',
'path': resolve_relative_path('../.assets/models/gfpgan_1.2.hash')
}
},
'sources':
{
'face_enhancer':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/gfpgan_1.2.onnx',
'path': resolve_relative_path('../.assets/models/gfpgan_1.2.onnx')
}
},
'template': 'ffhq_512',
'size': (512, 512)
},
'gfpgan_1.3':
{
'hashes':
{
'face_enhancer':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/gfpgan_1.3.hash',
'path': resolve_relative_path('../.assets/models/gfpgan_1.3.hash')
}
},
'sources':
{
'face_enhancer':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/gfpgan_1.3.onnx',
'path': resolve_relative_path('../.assets/models/gfpgan_1.3.onnx')
}
},
'template': 'ffhq_512',
'size': (512, 512)
},
'gfpgan_1.4':
{
'hashes':
{
'face_enhancer':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/gfpgan_1.4.hash',
'path': resolve_relative_path('../.assets/models/gfpgan_1.4.hash')
}
},
'sources':
{
'face_enhancer':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/gfpgan_1.4.onnx',
'path': resolve_relative_path('../.assets/models/gfpgan_1.4.onnx')
}
},
'template': 'ffhq_512',
'size': (512, 512)
},
'gpen_bfr_256':
{
'hashes':
{
'face_enhancer':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/gpen_bfr_256.hash',
'path': resolve_relative_path('../.assets/models/gpen_bfr_256.hash')
}
},
'sources':
{
'face_enhancer':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/gpen_bfr_256.onnx',
'path': resolve_relative_path('../.assets/models/gpen_bfr_256.onnx')
}
},
'template': 'arcface_128_v2',
'size': (256, 256)
},
'gpen_bfr_512':
{
'hashes':
{
'face_enhancer':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/gpen_bfr_512.hash',
'path': resolve_relative_path('../.assets/models/gpen_bfr_512.hash')
}
},
'sources':
{
'face_enhancer':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/gpen_bfr_512.onnx',
'path': resolve_relative_path('../.assets/models/gpen_bfr_512.onnx')
}
},
'template': 'ffhq_512',
'size': (512, 512)
},
'gpen_bfr_1024':
{
'hashes':
{
'face_enhancer':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/gpen_bfr_1024.hash',
'path': resolve_relative_path('../.assets/models/gpen_bfr_1024.hash')
}
},
'sources':
{
'face_enhancer':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/gpen_bfr_1024.onnx',
'path': resolve_relative_path('../.assets/models/gpen_bfr_1024.onnx')
}
},
'template': 'ffhq_512',
'size': (1024, 1024)
},
'gpen_bfr_2048':
{
'hashes':
{
'face_enhancer':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/gpen_bfr_2048.hash',
'path': resolve_relative_path('../.assets/models/gpen_bfr_2048.hash')
}
},
'sources':
{
'face_enhancer':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/gpen_bfr_2048.onnx',
'path': resolve_relative_path('../.assets/models/gpen_bfr_2048.onnx')
}
},
'template': 'ffhq_512',
'size': (2048, 2048)
},
'restoreformer_plus_plus':
{
'hashes':
{
'face_enhancer':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/restoreformer_plus_plus.hash',
'path': resolve_relative_path('../.assets/models/restoreformer_plus_plus.hash')
}
},
'sources':
{
'face_enhancer':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/restoreformer_plus_plus.onnx',
'path': resolve_relative_path('../.assets/models/restoreformer_plus_plus.onnx')
}
},
'template': 'ffhq_512',
'size': (512, 512)
}
}
def get_inference_pool() -> InferencePool:
model_sources = get_model_options().get('sources')
model_context = __name__ + '.' + state_manager.get_item('face_enhancer_model')
return inference_manager.get_inference_pool(model_context, model_sources)
def clear_inference_pool() -> None:
model_context = __name__ + '.' + state_manager.get_item('face_enhancer_model')
inference_manager.clear_inference_pool(model_context)
def get_model_options() -> ModelOptions:
face_enhancer_model = state_manager.get_item('face_enhancer_model')
return MODEL_SET.get(face_enhancer_model)
def register_args(program : ArgumentParser) -> None:
group_processors = find_argument_group(program, 'processors')
if group_processors:
group_processors.add_argument('--face-enhancer-model', help = wording.get('help.face_enhancer_model'), default = config.get_str_value('processors.face_enhancer_model', 'gfpgan_1.4'), choices = processors_choices.face_enhancer_models)
group_processors.add_argument('--face-enhancer-blend', help = wording.get('help.face_enhancer_blend'), type = int, default = config.get_int_value('processors.face_enhancer_blend', '80'), choices = processors_choices.face_enhancer_blend_range, metavar = create_int_metavar(processors_choices.face_enhancer_blend_range))
facefusion.jobs.job_store.register_step_keys([ 'face_enhancer_model', 'face_enhancer_blend' ])
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('face_enhancer_model', args.get('face_enhancer_model'))
apply_state_item('face_enhancer_blend', args.get('face_enhancer_blend'))
def pre_check() -> bool:
download_directory_path = resolve_relative_path('../.assets/models')
model_hashes = get_model_options().get('hashes')
model_sources = get_model_options().get('sources')
return conditional_download_hashes(download_directory_path, model_hashes) and conditional_download_sources(download_directory_path, model_sources)
def pre_process(mode : ProcessMode) -> bool:
if mode in [ 'output', 'preview' ] and not is_image(state_manager.get_item('target_path')) and not is_video(state_manager.get_item('target_path')):
logger.error(wording.get('choose_image_or_video_target') + wording.get('exclamation_mark'), __name__)
return False
if mode == 'output' and not in_directory(state_manager.get_item('output_path')):
logger.error(wording.get('specify_image_or_video_output') + wording.get('exclamation_mark'), __name__)
return False
if mode == 'output' and not same_file_extension([ state_manager.get_item('target_path'), state_manager.get_item('output_path') ]):
logger.error(wording.get('match_target_and_output_extension') + wording.get('exclamation_mark'), __name__)
return False
return True
def post_process() -> None:
read_static_image.cache_clear()
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
clear_inference_pool()
if state_manager.get_item('video_memory_strategy') == 'strict':
content_analyser.clear_inference_pool()
face_classifier.clear_inference_pool()
face_detector.clear_inference_pool()
face_landmarker.clear_inference_pool()
face_masker.clear_inference_pool()
face_recognizer.clear_inference_pool()
def enhance_face(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
model_template = get_model_options().get('template')
model_size = get_model_options().get('size')
crop_vision_frame, affine_matrix = warp_face_by_face_landmark_5(temp_vision_frame, target_face.landmark_set.get('5/68'), model_template, model_size)
box_mask = create_static_box_mask(crop_vision_frame.shape[:2][::-1], state_manager.get_item('face_mask_blur'), (0, 0, 0, 0))
crop_masks =\
[
box_mask
]
if 'occlusion' in state_manager.get_item('face_mask_types'):
occlusion_mask = create_occlusion_mask(crop_vision_frame)
crop_masks.append(occlusion_mask)
crop_vision_frame = prepare_crop_frame(crop_vision_frame)
crop_vision_frame = forward(crop_vision_frame)
crop_vision_frame = normalize_crop_frame(crop_vision_frame)
crop_mask = numpy.minimum.reduce(crop_masks).clip(0, 1)
paste_vision_frame = paste_back(temp_vision_frame, crop_vision_frame, crop_mask, affine_matrix)
temp_vision_frame = blend_frame(temp_vision_frame, paste_vision_frame)
return temp_vision_frame
def forward(crop_vision_frame : VisionFrame) -> VisionFrame:
face_enhancer = get_inference_pool().get('face_enhancer')
face_enhancer_inputs = {}
for face_enhancer_input in face_enhancer.get_inputs():
if face_enhancer_input.name == 'input':
face_enhancer_inputs[face_enhancer_input.name] = crop_vision_frame
if face_enhancer_input.name == 'weight':
weight = numpy.array([ 1 ]).astype(numpy.double)
face_enhancer_inputs[face_enhancer_input.name] = weight
with thread_semaphore():
crop_vision_frame = face_enhancer.run(None, face_enhancer_inputs)[0][0]
return crop_vision_frame
def prepare_crop_frame(crop_vision_frame : VisionFrame) -> VisionFrame:
crop_vision_frame = crop_vision_frame[:, :, ::-1] / 255.0
crop_vision_frame = (crop_vision_frame - 0.5) / 0.5
crop_vision_frame = numpy.expand_dims(crop_vision_frame.transpose(2, 0, 1), axis = 0).astype(numpy.float32)
return crop_vision_frame
def normalize_crop_frame(crop_vision_frame : VisionFrame) -> VisionFrame:
crop_vision_frame = numpy.clip(crop_vision_frame, -1, 1)
crop_vision_frame = (crop_vision_frame + 1) / 2
crop_vision_frame = crop_vision_frame.transpose(1, 2, 0)
crop_vision_frame = (crop_vision_frame * 255.0).round()
crop_vision_frame = crop_vision_frame.astype(numpy.uint8)[:, :, ::-1]
return crop_vision_frame
def blend_frame(temp_vision_frame : VisionFrame, paste_vision_frame : VisionFrame) -> VisionFrame:
face_enhancer_blend = 1 - (state_manager.get_item('face_enhancer_blend') / 100)
temp_vision_frame = cv2.addWeighted(temp_vision_frame, face_enhancer_blend, paste_vision_frame, 1 - face_enhancer_blend, 0)
return temp_vision_frame
def get_reference_frame(source_face : Face, target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
return enhance_face(target_face, temp_vision_frame)
def process_frame(inputs : FaceEnhancerInputs) -> VisionFrame:
reference_faces = inputs.get('reference_faces')
target_vision_frame = inputs.get('target_vision_frame')
many_faces = sort_and_filter_faces(get_many_faces([ target_vision_frame ]))
if state_manager.get_item('face_selector_mode') == 'many':
if many_faces:
for target_face in many_faces:
target_vision_frame = enhance_face(target_face, target_vision_frame)
if state_manager.get_item('face_selector_mode') == 'one':
target_face = get_one_face(many_faces)
if target_face:
target_vision_frame = enhance_face(target_face, target_vision_frame)
if state_manager.get_item('face_selector_mode') == 'reference':
similar_faces = find_similar_faces(many_faces, reference_faces, state_manager.get_item('reference_face_distance'))
if similar_faces:
for similar_face in similar_faces:
target_vision_frame = enhance_face(similar_face, target_vision_frame)
return target_vision_frame
def process_frames(source_path : List[str], queue_payloads : List[QueuePayload], update_progress : UpdateProgress) -> None:
reference_faces = get_reference_faces() if 'reference' in state_manager.get_item('face_selector_mode') else None
for queue_payload in process_manager.manage(queue_payloads):
target_vision_path = queue_payload['frame_path']
target_vision_frame = read_image(target_vision_path)
output_vision_frame = process_frame(
{
'reference_faces': reference_faces,
'target_vision_frame': target_vision_frame
})
write_image(target_vision_path, output_vision_frame)
update_progress(1)
def process_image(source_path : str, target_path : str, output_path : str) -> None:
reference_faces = get_reference_faces() if 'reference' in state_manager.get_item('face_selector_mode') else None
target_vision_frame = read_static_image(target_path)
output_vision_frame = process_frame(
{
'reference_faces': reference_faces,
'target_vision_frame': target_vision_frame
})
write_image(output_path, output_vision_frame)
def process_video(source_paths : List[str], temp_frame_paths : List[str]) -> None:
processors.multi_process_frames(None, temp_frame_paths, process_frames)

View File

@@ -1,10 +0,0 @@
from typing import List, Sequence
from facefusion.common_helper import create_float_range, create_int_range
from facefusion.processors.modules.face_enhancer.types import FaceEnhancerModel
face_enhancer_models : List[FaceEnhancerModel] = [ 'codeformer', 'gfpgan_1.2', 'gfpgan_1.3', 'gfpgan_1.4', 'gpen_bfr_256', 'gpen_bfr_512', 'gpen_bfr_1024', 'gpen_bfr_2048', 'restoreformer_plus_plus' ]
face_enhancer_blend_range : Sequence[int] = create_int_range(0, 100, 1)
face_enhancer_weight_range : Sequence[float] = create_float_range(0.0, 1.0, 0.05)

View File

@@ -1,426 +0,0 @@
from argparse import ArgumentParser
from functools import lru_cache
import numpy
import facefusion.jobs.job_manager
import facefusion.jobs.job_store
from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, inference_manager, logger, state_manager, translator, video_manager
from facefusion.common_helper import create_float_metavar, create_int_metavar
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
from facefusion.face_analyser import scale_face
from facefusion.face_helper import paste_back, warp_face_by_face_landmark_5
from facefusion.face_masker import create_box_mask, create_occlusion_mask
from facefusion.face_selector import select_faces
from facefusion.filesystem import in_directory, is_image, is_video, resolve_relative_path, same_file_extension
from facefusion.processors.modules.face_enhancer import choices as face_enhancer_choices
from facefusion.processors.modules.face_enhancer.types import FaceEnhancerInputs, FaceEnhancerWeight
from facefusion.processors.types import ProcessorOutputs
from facefusion.program_helper import find_argument_group
from facefusion.thread_helper import thread_semaphore
from facefusion.types import ApplyStateItem, Args, DownloadScope, Face, InferencePool, ModelOptions, ModelSet, ProcessMode, VisionFrame
from facefusion.vision import blend_frame, read_static_image, read_static_video_frame
@lru_cache()
def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
return\
{
'codeformer':
{
'__metadata__':
{
'vendor': 'sczhou',
'license': 'S-Lab-1.0',
'year': 2022
},
'hashes':
{
'face_enhancer':
{
'url': resolve_download_url('models-3.0.0', 'codeformer.hash'),
'path': resolve_relative_path('../.assets/models/codeformer.hash')
}
},
'sources':
{
'face_enhancer':
{
'url': resolve_download_url('models-3.0.0', 'codeformer.onnx'),
'path': resolve_relative_path('../.assets/models/codeformer.onnx')
}
},
'template': 'ffhq_512',
'size': (512, 512)
},
'gfpgan_1.2':
{
'__metadata__':
{
'vendor': 'TencentARC',
'license': 'Apache-2.0',
'year': 2022
},
'hashes':
{
'face_enhancer':
{
'url': resolve_download_url('models-3.0.0', 'gfpgan_1.2.hash'),
'path': resolve_relative_path('../.assets/models/gfpgan_1.2.hash')
}
},
'sources':
{
'face_enhancer':
{
'url': resolve_download_url('models-3.0.0', 'gfpgan_1.2.onnx'),
'path': resolve_relative_path('../.assets/models/gfpgan_1.2.onnx')
}
},
'template': 'ffhq_512',
'size': (512, 512)
},
'gfpgan_1.3':
{
'__metadata__':
{
'vendor': 'TencentARC',
'license': 'Apache-2.0',
'year': 2022
},
'hashes':
{
'face_enhancer':
{
'url': resolve_download_url('models-3.0.0', 'gfpgan_1.3.hash'),
'path': resolve_relative_path('../.assets/models/gfpgan_1.3.hash')
}
},
'sources':
{
'face_enhancer':
{
'url': resolve_download_url('models-3.0.0', 'gfpgan_1.3.onnx'),
'path': resolve_relative_path('../.assets/models/gfpgan_1.3.onnx')
}
},
'template': 'ffhq_512',
'size': (512, 512)
},
'gfpgan_1.4':
{
'__metadata__':
{
'vendor': 'TencentARC',
'license': 'Apache-2.0',
'year': 2022
},
'hashes':
{
'face_enhancer':
{
'url': resolve_download_url('models-3.0.0', 'gfpgan_1.4.hash'),
'path': resolve_relative_path('../.assets/models/gfpgan_1.4.hash')
}
},
'sources':
{
'face_enhancer':
{
'url': resolve_download_url('models-3.0.0', 'gfpgan_1.4.onnx'),
'path': resolve_relative_path('../.assets/models/gfpgan_1.4.onnx')
}
},
'template': 'ffhq_512',
'size': (512, 512)
},
'gpen_bfr_256':
{
'__metadata__':
{
'vendor': 'yangxy',
'license': 'Non-Commercial',
'year': 2021
},
'hashes':
{
'face_enhancer':
{
'url': resolve_download_url('models-3.0.0', 'gpen_bfr_256.hash'),
'path': resolve_relative_path('../.assets/models/gpen_bfr_256.hash')
}
},
'sources':
{
'face_enhancer':
{
'url': resolve_download_url('models-3.0.0', 'gpen_bfr_256.onnx'),
'path': resolve_relative_path('../.assets/models/gpen_bfr_256.onnx')
}
},
'template': 'arcface_128',
'size': (256, 256)
},
'gpen_bfr_512':
{
'__metadata__':
{
'vendor': 'yangxy',
'license': 'Non-Commercial',
'year': 2021
},
'hashes':
{
'face_enhancer':
{
'url': resolve_download_url('models-3.0.0', 'gpen_bfr_512.hash'),
'path': resolve_relative_path('../.assets/models/gpen_bfr_512.hash')
}
},
'sources':
{
'face_enhancer':
{
'url': resolve_download_url('models-3.0.0', 'gpen_bfr_512.onnx'),
'path': resolve_relative_path('../.assets/models/gpen_bfr_512.onnx')
}
},
'template': 'ffhq_512',
'size': (512, 512)
},
'gpen_bfr_1024':
{
'__metadata__':
{
'vendor': 'yangxy',
'license': 'Non-Commercial',
'year': 2021
},
'hashes':
{
'face_enhancer':
{
'url': resolve_download_url('models-3.0.0', 'gpen_bfr_1024.hash'),
'path': resolve_relative_path('../.assets/models/gpen_bfr_1024.hash')
}
},
'sources':
{
'face_enhancer':
{
'url': resolve_download_url('models-3.0.0', 'gpen_bfr_1024.onnx'),
'path': resolve_relative_path('../.assets/models/gpen_bfr_1024.onnx')
}
},
'template': 'ffhq_512',
'size': (1024, 1024)
},
'gpen_bfr_2048':
{
'__metadata__':
{
'vendor': 'yangxy',
'license': 'Non-Commercial',
'year': 2021
},
'hashes':
{
'face_enhancer':
{
'url': resolve_download_url('models-3.0.0', 'gpen_bfr_2048.hash'),
'path': resolve_relative_path('../.assets/models/gpen_bfr_2048.hash')
}
},
'sources':
{
'face_enhancer':
{
'url': resolve_download_url('models-3.0.0', 'gpen_bfr_2048.onnx'),
'path': resolve_relative_path('../.assets/models/gpen_bfr_2048.onnx')
}
},
'template': 'ffhq_512',
'size': (2048, 2048)
},
'restoreformer_plus_plus':
{
'__metadata__':
{
'vendor': 'wzhouxiff',
'license': 'Apache-2.0',
'year': 2022
},
'hashes':
{
'face_enhancer':
{
'url': resolve_download_url('models-3.0.0', 'restoreformer_plus_plus.hash'),
'path': resolve_relative_path('../.assets/models/restoreformer_plus_plus.hash')
}
},
'sources':
{
'face_enhancer':
{
'url': resolve_download_url('models-3.0.0', 'restoreformer_plus_plus.onnx'),
'path': resolve_relative_path('../.assets/models/restoreformer_plus_plus.onnx')
}
},
'template': 'ffhq_512',
'size': (512, 512)
}
}
def get_inference_pool() -> InferencePool:
model_names = [ state_manager.get_item('face_enhancer_model') ]
model_source_set = get_model_options().get('sources')
return inference_manager.get_inference_pool(__name__, model_names, model_source_set)
def clear_inference_pool() -> None:
model_names = [ state_manager.get_item('face_enhancer_model') ]
inference_manager.clear_inference_pool(__name__, model_names)
def get_model_options() -> ModelOptions:
model_name = state_manager.get_item('face_enhancer_model')
return create_static_model_set('full').get(model_name)
def register_args(program : ArgumentParser) -> None:
group_processors = find_argument_group(program, 'processors')
if group_processors:
group_processors.add_argument('--face-enhancer-model', help = translator.get('help.model', __package__), default = config.get_str_value('processors', 'face_enhancer_model', 'gfpgan_1.4'), choices = face_enhancer_choices.face_enhancer_models)
group_processors.add_argument('--face-enhancer-blend', help = translator.get('help.blend', __package__), type = int, default = config.get_int_value('processors', 'face_enhancer_blend', '80'), choices = face_enhancer_choices.face_enhancer_blend_range, metavar = create_int_metavar(face_enhancer_choices.face_enhancer_blend_range))
group_processors.add_argument('--face-enhancer-weight', help = translator.get('help.weight', __package__), type = float, default = config.get_float_value('processors', 'face_enhancer_weight', '0.5'), choices = face_enhancer_choices.face_enhancer_weight_range, metavar = create_float_metavar(face_enhancer_choices.face_enhancer_weight_range))
facefusion.jobs.job_store.register_step_keys([ 'face_enhancer_model', 'face_enhancer_blend', 'face_enhancer_weight' ])
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('face_enhancer_model', args.get('face_enhancer_model'))
apply_state_item('face_enhancer_blend', args.get('face_enhancer_blend'))
apply_state_item('face_enhancer_weight', args.get('face_enhancer_weight'))
def pre_check() -> bool:
model_hash_set = get_model_options().get('hashes')
model_source_set = get_model_options().get('sources')
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
def pre_process(mode : ProcessMode) -> bool:
if mode in [ 'output', 'preview' ] and not is_image(state_manager.get_item('target_path')) and not is_video(state_manager.get_item('target_path')):
logger.error(translator.get('choose_image_or_video_target') + translator.get('exclamation_mark'), __name__)
return False
if mode == 'output' and not in_directory(state_manager.get_item('output_path')):
logger.error(translator.get('specify_image_or_video_output') + translator.get('exclamation_mark'), __name__)
return False
if mode == 'output' and not same_file_extension(state_manager.get_item('target_path'), state_manager.get_item('output_path')):
logger.error(translator.get('match_target_and_output_extension') + translator.get('exclamation_mark'), __name__)
return False
return True
def post_process() -> None:
read_static_image.cache_clear()
read_static_video_frame.cache_clear()
video_manager.clear_video_pool()
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
clear_inference_pool()
if state_manager.get_item('video_memory_strategy') == 'strict':
content_analyser.clear_inference_pool()
face_classifier.clear_inference_pool()
face_detector.clear_inference_pool()
face_landmarker.clear_inference_pool()
face_masker.clear_inference_pool()
face_recognizer.clear_inference_pool()
def enhance_face(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
model_template = get_model_options().get('template')
model_size = get_model_options().get('size')
crop_vision_frame, affine_matrix = warp_face_by_face_landmark_5(temp_vision_frame, target_face.landmark_set.get('5/68'), model_template, model_size)
box_mask = create_box_mask(crop_vision_frame, state_manager.get_item('face_mask_blur'), (0, 0, 0, 0))
crop_masks =\
[
box_mask
]
if 'occlusion' in state_manager.get_item('face_mask_types'):
occlusion_mask = create_occlusion_mask(crop_vision_frame)
crop_masks.append(occlusion_mask)
crop_vision_frame = prepare_crop_frame(crop_vision_frame)
face_enhancer_weight = numpy.array([ state_manager.get_item('face_enhancer_weight') ]).astype(numpy.double)
crop_vision_frame = forward(crop_vision_frame, face_enhancer_weight)
crop_vision_frame = normalize_crop_frame(crop_vision_frame)
crop_mask = numpy.minimum.reduce(crop_masks).clip(0, 1)
paste_vision_frame = paste_back(temp_vision_frame, crop_vision_frame, crop_mask, affine_matrix)
temp_vision_frame = blend_paste_frame(temp_vision_frame, paste_vision_frame)
return temp_vision_frame
def forward(crop_vision_frame : VisionFrame, face_enhancer_weight : FaceEnhancerWeight) -> VisionFrame:
face_enhancer = get_inference_pool().get('face_enhancer')
face_enhancer_inputs = {}
for face_enhancer_input in face_enhancer.get_inputs():
if face_enhancer_input.name == 'input':
face_enhancer_inputs[face_enhancer_input.name] = crop_vision_frame
if face_enhancer_input.name == 'weight':
face_enhancer_inputs[face_enhancer_input.name] = face_enhancer_weight
with thread_semaphore():
crop_vision_frame = face_enhancer.run(None, face_enhancer_inputs)[0][0]
return crop_vision_frame
def has_weight_input() -> bool:
face_enhancer = get_inference_pool().get('face_enhancer')
for deep_swapper_input in face_enhancer.get_inputs():
if deep_swapper_input.name == 'weight':
return True
return False
def prepare_crop_frame(crop_vision_frame : VisionFrame) -> VisionFrame:
crop_vision_frame = crop_vision_frame[:, :, ::-1] / 255.0
crop_vision_frame = (crop_vision_frame - 0.5) / 0.5
crop_vision_frame = numpy.expand_dims(crop_vision_frame.transpose(2, 0, 1), axis = 0).astype(numpy.float32)
return crop_vision_frame
def normalize_crop_frame(crop_vision_frame : VisionFrame) -> VisionFrame:
crop_vision_frame = numpy.clip(crop_vision_frame, -1, 1)
crop_vision_frame = (crop_vision_frame + 1) / 2
crop_vision_frame = crop_vision_frame.transpose(1, 2, 0)
crop_vision_frame = (crop_vision_frame * 255.0).round()
crop_vision_frame = crop_vision_frame.astype(numpy.uint8)[:, :, ::-1]
return crop_vision_frame
def blend_paste_frame(temp_vision_frame : VisionFrame, paste_vision_frame : VisionFrame) -> VisionFrame:
face_enhancer_blend = 1 - (state_manager.get_item('face_enhancer_blend') / 100)
temp_vision_frame = blend_frame(temp_vision_frame, paste_vision_frame, 1 - face_enhancer_blend)
return temp_vision_frame
def process_frame(inputs : FaceEnhancerInputs) -> ProcessorOutputs:
reference_vision_frame = inputs.get('reference_vision_frame')
target_vision_frame = inputs.get('target_vision_frame')
temp_vision_frame = inputs.get('temp_vision_frame')
temp_vision_mask = inputs.get('temp_vision_mask')
target_faces = select_faces(reference_vision_frame, target_vision_frame)
if target_faces:
for target_face in target_faces:
target_face = scale_face(target_face, target_vision_frame, temp_vision_frame)
temp_vision_frame = enhance_face(target_face, temp_vision_frame)
return temp_vision_frame, temp_vision_mask

View File

@@ -1,20 +0,0 @@
from facefusion.types import Locals
LOCALS : Locals =\
{
'en':
{
'help':
{
'model': 'choose the model responsible for enhancing the face',
'blend': 'blend the enhanced into the previous face',
'weight': 'specify the degree of weight applied to the face'
},
'uis':
{
'blend_slider': 'FACE ENHANCER BLEND',
'model_dropdown': 'FACE ENHANCER MODEL',
'weight_slider': 'FACE ENHANCER WEIGHT'
}
}
}

View File

@@ -1,17 +0,0 @@
from typing import Any, Literal, TypeAlias, TypedDict
from numpy.typing import NDArray
from facefusion.types import Mask, VisionFrame
FaceEnhancerInputs = TypedDict('FaceEnhancerInputs',
{
'reference_vision_frame' : VisionFrame,
'target_vision_frame' : VisionFrame,
'temp_vision_frame' : VisionFrame,
'temp_vision_mask' : Mask
})
FaceEnhancerModel = Literal['codeformer', 'gfpgan_1.2', 'gfpgan_1.3', 'gfpgan_1.4', 'gpen_bfr_256', 'gpen_bfr_512', 'gpen_bfr_1024', 'gpen_bfr_2048', 'restoreformer_plus_plus']
FaceEnhancerWeight : TypeAlias = NDArray[Any]

View File

@@ -0,0 +1,564 @@
from argparse import ArgumentParser
from typing import List, Tuple
import numpy
import facefusion.jobs.job_manager
import facefusion.jobs.job_store
import facefusion.processors.core as processors
from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, inference_manager, logger, process_manager, state_manager, wording
from facefusion.common_helper import get_first
from facefusion.download import conditional_download_hashes, conditional_download_sources
from facefusion.execution import has_execution_provider
from facefusion.face_analyser import get_average_face, get_many_faces, get_one_face
from facefusion.face_helper import paste_back, warp_face_by_face_landmark_5
from facefusion.face_masker import create_occlusion_mask, create_region_mask, create_static_box_mask
from facefusion.face_selector import find_similar_faces, sort_and_filter_faces
from facefusion.face_store import get_reference_faces
from facefusion.filesystem import filter_image_paths, has_image, in_directory, is_image, is_video, resolve_relative_path, same_file_extension
from facefusion.inference_manager import get_static_model_initializer
from facefusion.processors import choices as processors_choices
from facefusion.processors.pixel_boost import explode_pixel_boost, implode_pixel_boost
from facefusion.processors.typing import FaceSwapperInputs
from facefusion.program_helper import find_argument_group, suggest_face_swapper_pixel_boost_choices
from facefusion.thread_helper import conditional_thread_semaphore
from facefusion.typing import ApplyStateItem, Args, Embedding, Face, InferencePool, ModelOptions, ModelSet, ProcessMode, QueuePayload, UpdateProgress, VisionFrame
from facefusion.vision import read_image, read_static_image, read_static_images, unpack_resolution, write_image
MODEL_SET : ModelSet =\
{
'blendswap_256':
{
'hashes':
{
'face_swapper':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/blendswap_256.hash',
'path': resolve_relative_path('../.assets/models/blendswap_256.hash')
}
},
'sources':
{
'face_swapper':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/blendswap_256.onnx',
'path': resolve_relative_path('../.assets/models/blendswap_256.onnx')
}
},
'type': 'blendswap',
'template': 'ffhq_512',
'size': (256, 256),
'mean': [ 0.0, 0.0, 0.0 ],
'standard_deviation': [ 1.0, 1.0, 1.0 ]
},
'ghost_1_256':
{
'hashes':
{
'face_swapper':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/ghost_1_256.hash',
'path': resolve_relative_path('../.assets/models/ghost_1_256.hash')
},
'embedding_converter':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/arcface_converter_ghost.hash',
'path': resolve_relative_path('../.assets/models/arcface_converter_ghost.hash')
}
},
'sources':
{
'face_swapper':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/ghost_1_256.onnx',
'path': resolve_relative_path('../.assets/models/ghost_1_256.onnx')
},
'embedding_converter':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/arcface_converter_ghost.onnx',
'path': resolve_relative_path('../.assets/models/arcface_converter_ghost.onnx')
}
},
'type': 'ghost',
'template': 'arcface_112_v1',
'size': (256, 256),
'mean': [ 0.5, 0.5, 0.5 ],
'standard_deviation': [ 0.5, 0.5, 0.5 ]
},
'ghost_2_256':
{
'hashes':
{
'face_swapper':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/ghost_2_256.hash',
'path': resolve_relative_path('../.assets/models/ghost_2_256.hash')
},
'embedding_converter':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/arcface_converter_ghost.hash',
'path': resolve_relative_path('../.assets/models/arcface_converter_ghost.hash')
}
},
'sources':
{
'face_swapper':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/ghost_2_256.onnx',
'path': resolve_relative_path('../.assets/models/ghost_2_256.onnx')
},
'embedding_converter':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/arcface_converter_ghost.onnx',
'path': resolve_relative_path('../.assets/models/arcface_converter_ghost.onnx')
}
},
'type': 'ghost',
'template': 'arcface_112_v1',
'size': (256, 256),
'mean': [ 0.5, 0.5, 0.5 ],
'standard_deviation': [ 0.5, 0.5, 0.5 ]
},
'ghost_3_256':
{
'hashes':
{
'face_swapper':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/ghost_3_256.hash',
'path': resolve_relative_path('../.assets/models/ghost_3_256.hash')
},
'embedding_converter':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/arcface_converter_ghost.hash',
'path': resolve_relative_path('../.assets/models/arcface_converter_ghost.hash')
}
},
'sources':
{
'face_swapper':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/ghost_3_256.onnx',
'path': resolve_relative_path('../.assets/models/ghost_3_256.onnx')
},
'embedding_converter':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/arcface_converter_ghost.onnx',
'path': resolve_relative_path('../.assets/models/arcface_converter_ghost.onnx')
}
},
'type': 'ghost',
'template': 'arcface_112_v1',
'size': (256, 256),
'mean': [ 0.5, 0.5, 0.5 ],
'standard_deviation': [ 0.5, 0.5, 0.5 ]
},
'inswapper_128':
{
'hashes':
{
'face_swapper':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/inswapper_128.hash',
'path': resolve_relative_path('../.assets/models/inswapper_128.hash')
}
},
'sources':
{
'face_swapper':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/inswapper_128.onnx',
'path': resolve_relative_path('../.assets/models/inswapper_128.onnx')
}
},
'type': 'inswapper',
'template': 'arcface_128_v2',
'size': (128, 128),
'mean': [ 0.0, 0.0, 0.0 ],
'standard_deviation': [ 1.0, 1.0, 1.0 ]
},
'inswapper_128_fp16':
{
'hashes':
{
'face_swapper':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/inswapper_128_fp16.hash',
'path': resolve_relative_path('../.assets/models/inswapper_128_fp16.hash')
}
},
'sources':
{
'face_swapper':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/inswapper_128_fp16.onnx',
'path': resolve_relative_path('../.assets/models/inswapper_128_fp16.onnx')
}
},
'type': 'inswapper',
'template': 'arcface_128_v2',
'size': (128, 128),
'mean': [ 0.0, 0.0, 0.0 ],
'standard_deviation': [ 1.0, 1.0, 1.0 ]
},
'simswap_256':
{
'hashes':
{
'face_swapper':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/simswap_256.hash',
'path': resolve_relative_path('../.assets/models/simswap_256.hash')
},
'embedding_converter':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/arcface_converter_simswap.hash',
'path': resolve_relative_path('../.assets/models/arcface_converter_simswap.hash')
}
},
'sources':
{
'face_swapper':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/simswap_256.onnx',
'path': resolve_relative_path('../.assets/models/simswap_256.onnx')
},
'embedding_converter':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/arcface_converter_simswap.onnx',
'path': resolve_relative_path('../.assets/models/arcface_converter_simswap.onnx')
}
},
'type': 'simswap',
'template': 'arcface_112_v1',
'size': (256, 256),
'mean': [ 0.485, 0.456, 0.406 ],
'standard_deviation': [ 0.229, 0.224, 0.225 ]
},
'simswap_unofficial_512':
{
'hashes':
{
'face_swapper':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/simswap_unofficial_512.hash',
'path': resolve_relative_path('../.assets/models/simswap_unofficial_512.hash')
},
'embedding_converter':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/arcface_converter_simswap.hash',
'path': resolve_relative_path('../.assets/models/arcface_converter_simswap.hash')
}
},
'sources':
{
'face_swapper':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/simswap_unofficial_512.onnx',
'path': resolve_relative_path('../.assets/models/simswap_unofficial_512.onnx')
},
'embedding_converter':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/arcface_converter_simswap.onnx',
'path': resolve_relative_path('../.assets/models/arcface_converter_simswap.onnx')
}
},
'type': 'simswap',
'template': 'arcface_112_v1',
'size': (512, 512),
'mean': [ 0.0, 0.0, 0.0 ],
'standard_deviation': [ 1.0, 1.0, 1.0 ]
},
'uniface_256':
{
'hashes':
{
'face_swapper':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/uniface_256.hash',
'path': resolve_relative_path('../.assets/models/uniface_256.hash')
}
},
'sources':
{
'face_swapper':
{
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models-3.0.0/uniface_256.onnx',
'path': resolve_relative_path('../.assets/models/uniface_256.onnx')
}
},
'type': 'uniface',
'template': 'ffhq_512',
'size': (256, 256),
'mean': [ 0.5, 0.5, 0.5 ],
'standard_deviation': [ 0.5, 0.5, 0.5 ]
}
}
def get_inference_pool() -> InferencePool:
model_sources = get_model_options().get('sources')
model_context = __name__ + '.' + state_manager.get_item('face_swapper_model')
return inference_manager.get_inference_pool(model_context, model_sources)
def clear_inference_pool() -> None:
model_context = __name__ + '.' + state_manager.get_item('face_swapper_model')
inference_manager.clear_inference_pool(model_context)
def get_model_options() -> ModelOptions:
face_swapper_model = state_manager.get_item('face_swapper_model')
face_swapper_model = 'inswapper_128' if has_execution_provider('coreml') and face_swapper_model == 'inswapper_128_fp16' else face_swapper_model
return MODEL_SET.get(face_swapper_model)
def register_args(program : ArgumentParser) -> None:
group_processors = find_argument_group(program, 'processors')
if group_processors:
group_processors.add_argument('--face-swapper-model', help = wording.get('help.face_swapper_model'), default = config.get_str_value('processors.face_swapper_model', 'inswapper_128_fp16'), choices = processors_choices.face_swapper_set.keys())
face_swapper_pixel_boost_choices = suggest_face_swapper_pixel_boost_choices(program)
group_processors.add_argument('--face-swapper-pixel-boost', help = wording.get('help.face_swapper_pixel_boost'), default = config.get_str_value('processors.face_swapper_pixel_boost', get_first(face_swapper_pixel_boost_choices)), choices = face_swapper_pixel_boost_choices)
facefusion.jobs.job_store.register_step_keys([ 'face_swapper_model', 'face_swapper_pixel_boost' ])
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('face_swapper_model', args.get('face_swapper_model'))
apply_state_item('face_swapper_pixel_boost', args.get('face_swapper_pixel_boost'))
def pre_check() -> bool:
download_directory_path = resolve_relative_path('../.assets/models')
model_hashes = get_model_options().get('hashes')
model_sources = get_model_options().get('sources')
return conditional_download_hashes(download_directory_path, model_hashes) and conditional_download_sources(download_directory_path, model_sources)
def pre_process(mode : ProcessMode) -> bool:
if not has_image(state_manager.get_item('source_paths')):
logger.error(wording.get('choose_image_source') + wording.get('exclamation_mark'), __name__)
return False
source_image_paths = filter_image_paths(state_manager.get_item('source_paths'))
source_frames = read_static_images(source_image_paths)
source_faces = get_many_faces(source_frames)
if not get_one_face(source_faces):
logger.error(wording.get('no_source_face_detected') + wording.get('exclamation_mark'), __name__)
return False
if mode in [ 'output', 'preview' ] and not is_image(state_manager.get_item('target_path')) and not is_video(state_manager.get_item('target_path')):
logger.error(wording.get('choose_image_or_video_target') + wording.get('exclamation_mark'), __name__)
return False
if mode == 'output' and not in_directory(state_manager.get_item('output_path')):
logger.error(wording.get('specify_image_or_video_output') + wording.get('exclamation_mark'), __name__)
return False
if mode == 'output' and not same_file_extension([ state_manager.get_item('target_path'), state_manager.get_item('output_path') ]):
logger.error(wording.get('match_target_and_output_extension') + wording.get('exclamation_mark'), __name__)
return False
return True
def post_process() -> None:
read_static_image.cache_clear()
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
clear_inference_pool()
get_static_model_initializer.cache_clear()
if state_manager.get_item('video_memory_strategy') == 'strict':
content_analyser.clear_inference_pool()
face_classifier.clear_inference_pool()
face_detector.clear_inference_pool()
face_landmarker.clear_inference_pool()
face_masker.clear_inference_pool()
face_recognizer.clear_inference_pool()
def swap_face(source_face : Face, target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
model_template = get_model_options().get('template')
model_size = get_model_options().get('size')
pixel_boost_size = unpack_resolution(state_manager.get_item('face_swapper_pixel_boost'))
pixel_boost_total = pixel_boost_size[0] // model_size[0]
crop_vision_frame, affine_matrix = warp_face_by_face_landmark_5(temp_vision_frame, target_face.landmark_set.get('5/68'), model_template, pixel_boost_size)
temp_vision_frames = []
crop_masks = []
if 'box' in state_manager.get_item('face_mask_types'):
box_mask = create_static_box_mask(crop_vision_frame.shape[:2][::-1], state_manager.get_item('face_mask_blur'), state_manager.get_item('face_mask_padding'))
crop_masks.append(box_mask)
if 'occlusion' in state_manager.get_item('face_mask_types'):
occlusion_mask = create_occlusion_mask(crop_vision_frame)
crop_masks.append(occlusion_mask)
pixel_boost_vision_frames = implode_pixel_boost(crop_vision_frame, pixel_boost_total, model_size)
for pixel_boost_vision_frame in pixel_boost_vision_frames:
pixel_boost_vision_frame = prepare_crop_frame(pixel_boost_vision_frame)
pixel_boost_vision_frame = forward_swap_face(source_face, pixel_boost_vision_frame)
pixel_boost_vision_frame = normalize_crop_frame(pixel_boost_vision_frame)
temp_vision_frames.append(pixel_boost_vision_frame)
crop_vision_frame = explode_pixel_boost(temp_vision_frames, pixel_boost_total, model_size, pixel_boost_size)
if 'region' in state_manager.get_item('face_mask_types'):
region_mask = create_region_mask(crop_vision_frame, state_manager.get_item('face_mask_regions'))
crop_masks.append(region_mask)
crop_mask = numpy.minimum.reduce(crop_masks).clip(0, 1)
temp_vision_frame = paste_back(temp_vision_frame, crop_vision_frame, crop_mask, affine_matrix)
return temp_vision_frame
def forward_swap_face(source_face : Face, crop_vision_frame : VisionFrame) -> VisionFrame:
face_swapper = get_inference_pool().get('face_swapper')
model_type = get_model_options().get('type')
face_swapper_inputs = {}
for face_swapper_input in face_swapper.get_inputs():
if face_swapper_input.name == 'source':
if model_type == 'blendswap' or model_type == 'uniface':
face_swapper_inputs[face_swapper_input.name] = prepare_source_frame(source_face)
else:
face_swapper_inputs[face_swapper_input.name] = prepare_source_embedding(source_face)
if face_swapper_input.name == 'target':
face_swapper_inputs[face_swapper_input.name] = crop_vision_frame
with conditional_thread_semaphore():
crop_vision_frame = face_swapper.run(None, face_swapper_inputs)[0][0]
return crop_vision_frame
def forward_convert_embedding(embedding : Embedding) -> Embedding:
embedding_converter = get_inference_pool().get('embedding_converter')
with conditional_thread_semaphore():
embedding = embedding_converter.run(None,
{
'input': embedding
})[0]
return embedding
def prepare_source_frame(source_face : Face) -> VisionFrame:
model_type = get_model_options().get('type')
source_vision_frame = read_static_image(get_first(state_manager.get_item('source_paths')))
if model_type == 'blendswap':
source_vision_frame, _ = warp_face_by_face_landmark_5(source_vision_frame, source_face.landmark_set.get('5/68'), 'arcface_112_v2', (112, 112))
if model_type == 'uniface':
source_vision_frame, _ = warp_face_by_face_landmark_5(source_vision_frame, source_face.landmark_set.get('5/68'), 'ffhq_512', (256, 256))
source_vision_frame = source_vision_frame[:, :, ::-1] / 255.0
source_vision_frame = source_vision_frame.transpose(2, 0, 1)
source_vision_frame = numpy.expand_dims(source_vision_frame, axis = 0).astype(numpy.float32)
return source_vision_frame
def prepare_source_embedding(source_face : Face) -> Embedding:
model_type = get_model_options().get('type')
if model_type == 'ghost':
source_embedding, _ = convert_embedding(source_face)
source_embedding = source_embedding.reshape(1, -1)
elif model_type == 'inswapper':
model_path = get_model_options().get('sources').get('face_swapper').get('path')
model_initializer = get_static_model_initializer(model_path)
source_embedding = source_face.embedding.reshape((1, -1))
source_embedding = numpy.dot(source_embedding, model_initializer) / numpy.linalg.norm(source_embedding)
else:
_, source_normed_embedding = convert_embedding(source_face)
source_embedding = source_normed_embedding.reshape(1, -1)
return source_embedding
def convert_embedding(source_face : Face) -> Tuple[Embedding, Embedding]:
embedding = source_face.embedding.reshape(-1, 512)
embedding = forward_convert_embedding(embedding)
embedding = embedding.ravel()
normed_embedding = embedding / numpy.linalg.norm(embedding)
return embedding, normed_embedding
def prepare_crop_frame(crop_vision_frame : VisionFrame) -> VisionFrame:
model_mean = get_model_options().get('mean')
model_standard_deviation = get_model_options().get('standard_deviation')
crop_vision_frame = crop_vision_frame[:, :, ::-1] / 255.0
crop_vision_frame = (crop_vision_frame - model_mean) / model_standard_deviation
crop_vision_frame = crop_vision_frame.transpose(2, 0, 1)
crop_vision_frame = numpy.expand_dims(crop_vision_frame, axis = 0).astype(numpy.float32)
return crop_vision_frame
def normalize_crop_frame(crop_vision_frame : VisionFrame) -> VisionFrame:
model_type = get_model_options().get('type')
model_mean = get_model_options().get('mean')
model_standard_deviation = get_model_options().get('standard_deviation')
crop_vision_frame = crop_vision_frame.transpose(1, 2, 0)
if model_type == 'ghost' or model_type == 'uniface':
crop_vision_frame = crop_vision_frame * model_standard_deviation + model_mean
crop_vision_frame = crop_vision_frame.clip(0, 1)
crop_vision_frame = crop_vision_frame[:, :, ::-1] * 255
return crop_vision_frame
def get_reference_frame(source_face : Face, target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
return swap_face(source_face, target_face, temp_vision_frame)
def process_frame(inputs : FaceSwapperInputs) -> VisionFrame:
reference_faces = inputs.get('reference_faces')
source_face = inputs.get('source_face')
target_vision_frame = inputs.get('target_vision_frame')
many_faces = sort_and_filter_faces(get_many_faces([ target_vision_frame ]))
if state_manager.get_item('face_selector_mode') == 'many':
if many_faces:
for target_face in many_faces:
target_vision_frame = swap_face(source_face, target_face, target_vision_frame)
if state_manager.get_item('face_selector_mode') == 'one':
target_face = get_one_face(many_faces)
if target_face:
target_vision_frame = swap_face(source_face, target_face, target_vision_frame)
if state_manager.get_item('face_selector_mode') == 'reference':
similar_faces = find_similar_faces(many_faces, reference_faces, state_manager.get_item('reference_face_distance'))
if similar_faces:
for similar_face in similar_faces:
target_vision_frame = swap_face(source_face, similar_face, target_vision_frame)
return target_vision_frame
def process_frames(source_paths : List[str], queue_payloads : List[QueuePayload], update_progress : UpdateProgress) -> None:
reference_faces = get_reference_faces() if 'reference' in state_manager.get_item('face_selector_mode') else None
source_frames = read_static_images(source_paths)
source_faces = get_many_faces(source_frames)
source_face = get_average_face(source_faces)
for queue_payload in process_manager.manage(queue_payloads):
target_vision_path = queue_payload['frame_path']
target_vision_frame = read_image(target_vision_path)
output_vision_frame = process_frame(
{
'reference_faces': reference_faces,
'source_face': source_face,
'target_vision_frame': target_vision_frame
})
write_image(target_vision_path, output_vision_frame)
update_progress(1)
def process_image(source_paths : List[str], target_path : str, output_path : str) -> None:
reference_faces = get_reference_faces() if 'reference' in state_manager.get_item('face_selector_mode') else None
source_frames = read_static_images(source_paths)
source_faces = get_many_faces(source_frames)
source_face = get_average_face(source_faces)
target_vision_frame = read_static_image(target_path)
output_vision_frame = process_frame(
{
'reference_faces': reference_faces,
'source_face': source_face,
'target_vision_frame': target_vision_frame
})
write_image(output_path, output_vision_frame)
def process_video(source_paths : List[str], temp_frame_paths : List[str]) -> None:
processors.multi_process_frames(source_paths, temp_frame_paths, process_frames)

View File

@@ -1,25 +0,0 @@
from typing import List, Sequence
from facefusion.common_helper import create_float_range
from facefusion.processors.modules.face_swapper.types import FaceSwapperModel, FaceSwapperSet, FaceSwapperWeight
face_swapper_set : FaceSwapperSet =\
{
'blendswap_256': [ '256x256', '384x384', '512x512', '768x768', '1024x1024' ],
'ghost_1_256': [ '256x256', '512x512', '768x768', '1024x1024' ],
'ghost_2_256': [ '256x256', '512x512', '768x768', '1024x1024' ],
'ghost_3_256': [ '256x256', '512x512', '768x768', '1024x1024' ],
'hififace_unofficial_256': [ '256x256', '512x512', '768x768', '1024x1024' ],
'hyperswap_1a_256': [ '256x256', '512x512', '768x768', '1024x1024' ],
'hyperswap_1b_256': [ '256x256', '512x512', '768x768', '1024x1024' ],
'hyperswap_1c_256': [ '256x256', '512x512', '768x768', '1024x1024' ],
'inswapper_128': [ '128x128', '256x256', '384x384', '512x512', '768x768', '1024x1024' ],
'inswapper_128_fp16': [ '128x128', '256x256', '384x384', '512x512', '768x768', '1024x1024' ],
'simswap_256': [ '256x256', '512x512', '768x768', '1024x1024' ],
'simswap_unofficial_512': [ '512x512', '768x768', '1024x1024' ],
'uniface_256': [ '256x256', '512x512', '768x768', '1024x1024' ]
}
face_swapper_models : List[FaceSwapperModel] = list(face_swapper_set.keys())
face_swapper_weight_range : Sequence[FaceSwapperWeight] = create_float_range(0.0, 1.0, 0.05)

View File

@@ -1,774 +0,0 @@
from argparse import ArgumentParser
from functools import lru_cache
from typing import List, Optional, Tuple
import cv2
import numpy
import facefusion.choices
import facefusion.jobs.job_manager
import facefusion.jobs.job_store
from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, inference_manager, logger, state_manager, translator, video_manager
from facefusion.common_helper import get_first, is_macos
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
from facefusion.execution import has_execution_provider
from facefusion.face_analyser import get_average_face, get_many_faces, get_one_face, scale_face
from facefusion.face_helper import paste_back, warp_face_by_face_landmark_5
from facefusion.face_masker import create_area_mask, create_box_mask, create_occlusion_mask, create_region_mask
from facefusion.face_selector import select_faces, sort_faces_by_order
from facefusion.filesystem import filter_image_paths, has_image, in_directory, is_image, is_video, resolve_relative_path, same_file_extension
from facefusion.model_helper import get_static_model_initializer
from facefusion.processors.modules.face_swapper import choices as face_swapper_choices
from facefusion.processors.modules.face_swapper.types import FaceSwapperInputs
from facefusion.processors.pixel_boost import explode_pixel_boost, implode_pixel_boost
from facefusion.processors.types import ProcessorOutputs
from facefusion.program_helper import find_argument_group
from facefusion.thread_helper import conditional_thread_semaphore
from facefusion.types import ApplyStateItem, Args, DownloadScope, Embedding, Face, InferencePool, ModelOptions, ModelSet, ProcessMode, VisionFrame
from facefusion.vision import read_static_image, read_static_images, read_static_video_frame, unpack_resolution
@lru_cache()
def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
return\
{
'blendswap_256':
{
'__metadata__':
{
'vendor': 'mapooon',
'license': 'Non-Commercial',
'year': 2023
},
'hashes':
{
'face_swapper':
{
'url': resolve_download_url('models-3.0.0', 'blendswap_256.hash'),
'path': resolve_relative_path('../.assets/models/blendswap_256.hash')
}
},
'sources':
{
'face_swapper':
{
'url': resolve_download_url('models-3.0.0', 'blendswap_256.onnx'),
'path': resolve_relative_path('../.assets/models/blendswap_256.onnx')
}
},
'type': 'blendswap',
'template': 'ffhq_512',
'size': (256, 256),
'mean': [ 0.0, 0.0, 0.0 ],
'standard_deviation': [ 1.0, 1.0, 1.0 ]
},
'ghost_1_256':
{
'__metadata__':
{
'vendor': 'ai-forever',
'license': 'Apache-2.0',
'year': 2022
},
'hashes':
{
'face_swapper':
{
'url': resolve_download_url('models-3.0.0', 'ghost_1_256.hash'),
'path': resolve_relative_path('../.assets/models/ghost_1_256.hash')
},
'embedding_converter':
{
'url': resolve_download_url('models-3.4.0', 'crossface_ghost.hash'),
'path': resolve_relative_path('../.assets/models/crossface_ghost.hash')
}
},
'sources':
{
'face_swapper':
{
'url': resolve_download_url('models-3.0.0', 'ghost_1_256.onnx'),
'path': resolve_relative_path('../.assets/models/ghost_1_256.onnx')
},
'embedding_converter':
{
'url': resolve_download_url('models-3.4.0', 'crossface_ghost.onnx'),
'path': resolve_relative_path('../.assets/models/crossface_ghost.onnx')
}
},
'type': 'ghost',
'template': 'arcface_112_v1',
'size': (256, 256),
'mean': [ 0.5, 0.5, 0.5 ],
'standard_deviation': [ 0.5, 0.5, 0.5 ]
},
'ghost_2_256':
{
'__metadata__':
{
'vendor': 'ai-forever',
'license': 'Apache-2.0',
'year': 2022
},
'hashes':
{
'face_swapper':
{
'url': resolve_download_url('models-3.0.0', 'ghost_2_256.hash'),
'path': resolve_relative_path('../.assets/models/ghost_2_256.hash')
},
'embedding_converter':
{
'url': resolve_download_url('models-3.4.0', 'crossface_ghost.hash'),
'path': resolve_relative_path('../.assets/models/crossface_ghost.hash')
}
},
'sources':
{
'face_swapper':
{
'url': resolve_download_url('models-3.0.0', 'ghost_2_256.onnx'),
'path': resolve_relative_path('../.assets/models/ghost_2_256.onnx')
},
'embedding_converter':
{
'url': resolve_download_url('models-3.4.0', 'crossface_ghost.onnx'),
'path': resolve_relative_path('../.assets/models/crossface_ghost.onnx')
}
},
'type': 'ghost',
'template': 'arcface_112_v1',
'size': (256, 256),
'mean': [ 0.5, 0.5, 0.5 ],
'standard_deviation': [ 0.5, 0.5, 0.5 ]
},
'ghost_3_256':
{
'__metadata__':
{
'vendor': 'ai-forever',
'license': 'Apache-2.0',
'year': 2022
},
'hashes':
{
'face_swapper':
{
'url': resolve_download_url('models-3.0.0', 'ghost_3_256.hash'),
'path': resolve_relative_path('../.assets/models/ghost_3_256.hash')
},
'embedding_converter':
{
'url': resolve_download_url('models-3.4.0', 'crossface_ghost.hash'),
'path': resolve_relative_path('../.assets/models/crossface_ghost.hash')
}
},
'sources':
{
'face_swapper':
{
'url': resolve_download_url('models-3.0.0', 'ghost_3_256.onnx'),
'path': resolve_relative_path('../.assets/models/ghost_3_256.onnx')
},
'embedding_converter':
{
'url': resolve_download_url('models-3.4.0', 'crossface_ghost.onnx'),
'path': resolve_relative_path('../.assets/models/crossface_ghost.onnx')
}
},
'type': 'ghost',
'template': 'arcface_112_v1',
'size': (256, 256),
'mean': [ 0.5, 0.5, 0.5 ],
'standard_deviation': [ 0.5, 0.5, 0.5 ]
},
'hififace_unofficial_256':
{
'__metadata__':
{
'vendor': 'GuijiAI',
'license': 'Unknown',
'year': 2021
},
'hashes':
{
'face_swapper':
{
'url': resolve_download_url('models-3.1.0', 'hififace_unofficial_256.hash'),
'path': resolve_relative_path('../.assets/models/hififace_unofficial_256.hash')
},
'embedding_converter':
{
'url': resolve_download_url('models-3.4.0', 'crossface_hififace.hash'),
'path': resolve_relative_path('../.assets/models/crossface_hififace.hash')
}
},
'sources':
{
'face_swapper':
{
'url': resolve_download_url('models-3.1.0', 'hififace_unofficial_256.onnx'),
'path': resolve_relative_path('../.assets/models/hififace_unofficial_256.onnx')
},
'embedding_converter':
{
'url': resolve_download_url('models-3.4.0', 'crossface_hififace.onnx'),
'path': resolve_relative_path('../.assets/models/crossface_hififace.onnx')
}
},
'type': 'hififace',
'template': 'mtcnn_512',
'size': (256, 256),
'mean': [ 0.5, 0.5, 0.5 ],
'standard_deviation': [ 0.5, 0.5, 0.5 ]
},
'hyperswap_1a_256':
{
'__metadata__':
{
'vendor': 'FaceFusion',
'license': 'ResearchRAIL',
'year': 2025
},
'hashes':
{
'face_swapper':
{
'url': resolve_download_url('models-3.3.0', 'hyperswap_1a_256.hash'),
'path': resolve_relative_path('../.assets/models/hyperswap_1a_256.hash')
}
},
'sources':
{
'face_swapper':
{
'url': resolve_download_url('models-3.3.0', 'hyperswap_1a_256.onnx'),
'path': resolve_relative_path('../.assets/models/hyperswap_1a_256.onnx')
}
},
'type': 'hyperswap',
'template': 'arcface_128',
'size': (256, 256),
'mean': [ 0.5, 0.5, 0.5 ],
'standard_deviation': [ 0.5, 0.5, 0.5 ]
},
'hyperswap_1b_256':
{
'__metadata__':
{
'vendor': 'FaceFusion',
'license': 'ResearchRAIL',
'year': 2025
},
'hashes':
{
'face_swapper':
{
'url': resolve_download_url('models-3.3.0', 'hyperswap_1b_256.hash'),
'path': resolve_relative_path('../.assets/models/hyperswap_1b_256.hash')
}
},
'sources':
{
'face_swapper':
{
'url': resolve_download_url('models-3.3.0', 'hyperswap_1b_256.onnx'),
'path': resolve_relative_path('../.assets/models/hyperswap_1b_256.onnx')
}
},
'type': 'hyperswap',
'template': 'arcface_128',
'size': (256, 256),
'mean': [ 0.5, 0.5, 0.5 ],
'standard_deviation': [ 0.5, 0.5, 0.5 ]
},
'hyperswap_1c_256':
{
'__metadata__':
{
'vendor': 'FaceFusion',
'license': 'ResearchRAIL',
'year': 2025
},
'hashes':
{
'face_swapper':
{
'url': resolve_download_url('models-3.3.0', 'hyperswap_1c_256.hash'),
'path': resolve_relative_path('../.assets/models/hyperswap_1c_256.hash')
}
},
'sources':
{
'face_swapper':
{
'url': resolve_download_url('models-3.3.0', 'hyperswap_1c_256.onnx'),
'path': resolve_relative_path('../.assets/models/hyperswap_1c_256.onnx')
}
},
'type': 'hyperswap',
'template': 'arcface_128',
'size': (256, 256),
'mean': [ 0.5, 0.5, 0.5 ],
'standard_deviation': [ 0.5, 0.5, 0.5 ]
},
'inswapper_128':
{
'__metadata__':
{
'vendor': 'InsightFace',
'license': 'Non-Commercial',
'year': 2023
},
'hashes':
{
'face_swapper':
{
'url': resolve_download_url('models-3.0.0', 'inswapper_128.hash'),
'path': resolve_relative_path('../.assets/models/inswapper_128.hash')
}
},
'sources':
{
'face_swapper':
{
'url': resolve_download_url('models-3.0.0', 'inswapper_128.onnx'),
'path': resolve_relative_path('../.assets/models/inswapper_128.onnx')
}
},
'type': 'inswapper',
'template': 'arcface_128',
'size': (128, 128),
'mean': [ 0.0, 0.0, 0.0 ],
'standard_deviation': [ 1.0, 1.0, 1.0 ]
},
'inswapper_128_fp16':
{
'__metadata__':
{
'vendor': 'InsightFace',
'license': 'Non-Commercial',
'year': 2023
},
'hashes':
{
'face_swapper':
{
'url': resolve_download_url('models-3.0.0', 'inswapper_128_fp16.hash'),
'path': resolve_relative_path('../.assets/models/inswapper_128_fp16.hash')
}
},
'sources':
{
'face_swapper':
{
'url': resolve_download_url('models-3.0.0', 'inswapper_128_fp16.onnx'),
'path': resolve_relative_path('../.assets/models/inswapper_128_fp16.onnx')
}
},
'type': 'inswapper',
'template': 'arcface_128',
'size': (128, 128),
'mean': [ 0.0, 0.0, 0.0 ],
'standard_deviation': [ 1.0, 1.0, 1.0 ]
},
'simswap_256':
{
'__metadata__':
{
'vendor': 'neuralchen',
'license': 'Non-Commercial',
'year': 2020
},
'hashes':
{
'face_swapper':
{
'url': resolve_download_url('models-3.0.0', 'simswap_256.hash'),
'path': resolve_relative_path('../.assets/models/simswap_256.hash')
},
'embedding_converter':
{
'url': resolve_download_url('models-3.4.0', 'crossface_simswap.hash'),
'path': resolve_relative_path('../.assets/models/crossface_simswap.hash')
}
},
'sources':
{
'face_swapper':
{
'url': resolve_download_url('models-3.0.0', 'simswap_256.onnx'),
'path': resolve_relative_path('../.assets/models/simswap_256.onnx')
},
'embedding_converter':
{
'url': resolve_download_url('models-3.4.0', 'crossface_simswap.onnx'),
'path': resolve_relative_path('../.assets/models/crossface_simswap.onnx')
}
},
'type': 'simswap',
'template': 'arcface_112_v1',
'size': (256, 256),
'mean': [ 0.485, 0.456, 0.406 ],
'standard_deviation': [ 0.229, 0.224, 0.225 ]
},
'simswap_unofficial_512':
{
'__metadata__':
{
'vendor': 'neuralchen',
'license': 'Non-Commercial',
'year': 2020
},
'hashes':
{
'face_swapper':
{
'url': resolve_download_url('models-3.0.0', 'simswap_unofficial_512.hash'),
'path': resolve_relative_path('../.assets/models/simswap_unofficial_512.hash')
},
'embedding_converter':
{
'url': resolve_download_url('models-3.4.0', 'crossface_simswap.hash'),
'path': resolve_relative_path('../.assets/models/crossface_simswap.hash')
}
},
'sources':
{
'face_swapper':
{
'url': resolve_download_url('models-3.0.0', 'simswap_unofficial_512.onnx'),
'path': resolve_relative_path('../.assets/models/simswap_unofficial_512.onnx')
},
'embedding_converter':
{
'url': resolve_download_url('models-3.4.0', 'crossface_simswap.onnx'),
'path': resolve_relative_path('../.assets/models/crossface_simswap.onnx')
}
},
'type': 'simswap',
'template': 'arcface_112_v1',
'size': (512, 512),
'mean': [ 0.0, 0.0, 0.0 ],
'standard_deviation': [ 1.0, 1.0, 1.0 ]
},
'uniface_256':
{
'__metadata__':
{
'vendor': 'xc-csc101',
'license': 'Unknown',
'year': 2022
},
'hashes':
{
'face_swapper':
{
'url': resolve_download_url('models-3.0.0', 'uniface_256.hash'),
'path': resolve_relative_path('../.assets/models/uniface_256.hash')
}
},
'sources':
{
'face_swapper':
{
'url': resolve_download_url('models-3.0.0', 'uniface_256.onnx'),
'path': resolve_relative_path('../.assets/models/uniface_256.onnx')
}
},
'type': 'uniface',
'template': 'ffhq_512',
'size': (256, 256),
'mean': [ 0.5, 0.5, 0.5 ],
'standard_deviation': [ 0.5, 0.5, 0.5 ]
}
}
def get_inference_pool() -> InferencePool:
model_names = [ get_model_name() ]
model_source_set = get_model_options().get('sources')
return inference_manager.get_inference_pool(__name__, model_names, model_source_set)
def clear_inference_pool() -> None:
model_names = [ get_model_name() ]
inference_manager.clear_inference_pool(__name__, model_names)
def get_model_options() -> ModelOptions:
model_name = get_model_name()
return create_static_model_set('full').get(model_name)
def get_model_name() -> str:
model_name = state_manager.get_item('face_swapper_model')
if is_macos() and has_execution_provider('coreml') and model_name == 'inswapper_128_fp16':
return 'inswapper_128'
return model_name
def register_args(program : ArgumentParser) -> None:
group_processors = find_argument_group(program, 'processors')
if group_processors:
group_processors.add_argument('--face-swapper-model', help = translator.get('help.model', __package__), default = config.get_str_value('processors', 'face_swapper_model', 'hyperswap_1a_256'), choices = face_swapper_choices.face_swapper_models)
known_args, _ = program.parse_known_args()
face_swapper_pixel_boost_choices = face_swapper_choices.face_swapper_set.get(known_args.face_swapper_model)
group_processors.add_argument('--face-swapper-pixel-boost', help = translator.get('help.pixel_boost', __package__), default = config.get_str_value('processors', 'face_swapper_pixel_boost', get_first(face_swapper_pixel_boost_choices)), choices = face_swapper_pixel_boost_choices)
group_processors.add_argument('--face-swapper-weight', help = translator.get('help.weight', __package__), type = float, default = config.get_float_value('processors', 'face_swapper_weight', '0.5'), choices = face_swapper_choices.face_swapper_weight_range)
facefusion.jobs.job_store.register_step_keys([ 'face_swapper_model', 'face_swapper_pixel_boost', 'face_swapper_weight' ])
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('face_swapper_model', args.get('face_swapper_model'))
apply_state_item('face_swapper_pixel_boost', args.get('face_swapper_pixel_boost'))
apply_state_item('face_swapper_weight', args.get('face_swapper_weight'))
def pre_check() -> bool:
model_hash_set = get_model_options().get('hashes')
model_source_set = get_model_options().get('sources')
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
def pre_process(mode : ProcessMode) -> bool:
if not has_image(state_manager.get_item('source_paths')):
logger.error(translator.get('choose_image_source') + translator.get('exclamation_mark'), __name__)
return False
source_image_paths = filter_image_paths(state_manager.get_item('source_paths'))
source_frames = read_static_images(source_image_paths)
source_faces = get_many_faces(source_frames)
if not get_one_face(source_faces):
logger.error(translator.get('no_source_face_detected') + translator.get('exclamation_mark'), __name__)
return False
if mode in [ 'output', 'preview' ] and not is_image(state_manager.get_item('target_path')) and not is_video(state_manager.get_item('target_path')):
logger.error(translator.get('choose_image_or_video_target') + translator.get('exclamation_mark'), __name__)
return False
if mode == 'output' and not in_directory(state_manager.get_item('output_path')):
logger.error(translator.get('specify_image_or_video_output') + translator.get('exclamation_mark'), __name__)
return False
if mode == 'output' and not same_file_extension(state_manager.get_item('target_path'), state_manager.get_item('output_path')):
logger.error(translator.get('match_target_and_output_extension') + translator.get('exclamation_mark'), __name__)
return False
return True
def post_process() -> None:
read_static_image.cache_clear()
read_static_video_frame.cache_clear()
video_manager.clear_video_pool()
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
get_static_model_initializer.cache_clear()
clear_inference_pool()
if state_manager.get_item('video_memory_strategy') == 'strict':
content_analyser.clear_inference_pool()
face_classifier.clear_inference_pool()
face_detector.clear_inference_pool()
face_landmarker.clear_inference_pool()
face_masker.clear_inference_pool()
face_recognizer.clear_inference_pool()
def swap_face(source_face : Face, target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
model_template = get_model_options().get('template')
model_size = get_model_options().get('size')
pixel_boost_size = unpack_resolution(state_manager.get_item('face_swapper_pixel_boost'))
pixel_boost_total = pixel_boost_size[0] // model_size[0]
crop_vision_frame, affine_matrix = warp_face_by_face_landmark_5(temp_vision_frame, target_face.landmark_set.get('5/68'), model_template, pixel_boost_size)
temp_vision_frames = []
crop_masks = []
if 'box' in state_manager.get_item('face_mask_types'):
box_mask = create_box_mask(crop_vision_frame, state_manager.get_item('face_mask_blur'), state_manager.get_item('face_mask_padding'))
crop_masks.append(box_mask)
if 'occlusion' in state_manager.get_item('face_mask_types'):
occlusion_mask = create_occlusion_mask(crop_vision_frame)
crop_masks.append(occlusion_mask)
pixel_boost_vision_frames = implode_pixel_boost(crop_vision_frame, pixel_boost_total, model_size)
for pixel_boost_vision_frame in pixel_boost_vision_frames:
pixel_boost_vision_frame = prepare_crop_frame(pixel_boost_vision_frame)
pixel_boost_vision_frame = forward_swap_face(source_face, target_face, pixel_boost_vision_frame)
pixel_boost_vision_frame = normalize_crop_frame(pixel_boost_vision_frame)
temp_vision_frames.append(pixel_boost_vision_frame)
crop_vision_frame = explode_pixel_boost(temp_vision_frames, pixel_boost_total, model_size, pixel_boost_size)
if 'area' in state_manager.get_item('face_mask_types'):
face_landmark_68 = cv2.transform(target_face.landmark_set.get('68').reshape(1, -1, 2), affine_matrix).reshape(-1, 2)
area_mask = create_area_mask(crop_vision_frame, face_landmark_68, state_manager.get_item('face_mask_areas'))
crop_masks.append(area_mask)
if 'region' in state_manager.get_item('face_mask_types'):
region_mask = create_region_mask(crop_vision_frame, state_manager.get_item('face_mask_regions'))
crop_masks.append(region_mask)
crop_mask = numpy.minimum.reduce(crop_masks).clip(0, 1)
paste_vision_frame = paste_back(temp_vision_frame, crop_vision_frame, crop_mask, affine_matrix)
return paste_vision_frame
def forward_swap_face(source_face : Face, target_face : Face, crop_vision_frame : VisionFrame) -> VisionFrame:
face_swapper = get_inference_pool().get('face_swapper')
model_type = get_model_options().get('type')
face_swapper_inputs = {}
if is_macos() and has_execution_provider('coreml') and model_type in [ 'ghost', 'uniface' ]:
face_swapper.set_providers([ facefusion.choices.execution_provider_set.get('cpu') ])
for face_swapper_input in face_swapper.get_inputs():
if face_swapper_input.name == 'source':
if model_type in [ 'blendswap', 'uniface' ]:
face_swapper_inputs[face_swapper_input.name] = prepare_source_frame(source_face)
else:
source_embedding = prepare_source_embedding(source_face)
source_embedding = balance_source_embedding(source_embedding, target_face.embedding)
face_swapper_inputs[face_swapper_input.name] = source_embedding
if face_swapper_input.name == 'target':
face_swapper_inputs[face_swapper_input.name] = crop_vision_frame
with conditional_thread_semaphore():
crop_vision_frame = face_swapper.run(None, face_swapper_inputs)[0][0]
return crop_vision_frame
def forward_convert_embedding(face_embedding : Embedding) -> Embedding:
embedding_converter = get_inference_pool().get('embedding_converter')
with conditional_thread_semaphore():
face_embedding = embedding_converter.run(None,
{
'input': face_embedding
})[0]
return face_embedding
def prepare_source_frame(source_face : Face) -> VisionFrame:
model_type = get_model_options().get('type')
source_vision_frame = read_static_image(get_first(state_manager.get_item('source_paths')))
if model_type == 'blendswap':
source_vision_frame, _ = warp_face_by_face_landmark_5(source_vision_frame, source_face.landmark_set.get('5/68'), 'arcface_112_v2', (112, 112))
if model_type == 'uniface':
source_vision_frame, _ = warp_face_by_face_landmark_5(source_vision_frame, source_face.landmark_set.get('5/68'), 'ffhq_512', (256, 256))
source_vision_frame = source_vision_frame[:, :, ::-1] / 255.0
source_vision_frame = source_vision_frame.transpose(2, 0, 1)
source_vision_frame = numpy.expand_dims(source_vision_frame, axis = 0).astype(numpy.float32)
return source_vision_frame
def prepare_source_embedding(source_face : Face) -> Embedding:
model_type = get_model_options().get('type')
if model_type == 'ghost':
source_embedding = source_face.embedding.reshape(-1, 512)
source_embedding, _ = convert_source_embedding(source_embedding)
source_embedding = source_embedding.reshape(1, -1)
return source_embedding
if model_type == 'hyperswap':
source_embedding = source_face.embedding_norm.reshape((1, -1))
return source_embedding
if model_type == 'inswapper':
model_path = get_model_options().get('sources').get('face_swapper').get('path')
model_initializer = get_static_model_initializer(model_path)
source_embedding = source_face.embedding.reshape((1, -1))
source_embedding = numpy.dot(source_embedding, model_initializer) / numpy.linalg.norm(source_embedding)
return source_embedding
source_embedding = source_face.embedding.reshape(-1, 512)
_, source_embedding_norm = convert_source_embedding(source_embedding)
source_embedding = source_embedding_norm.reshape(1, -1)
return source_embedding
def balance_source_embedding(source_embedding : Embedding, target_embedding : Embedding) -> Embedding:
model_type = get_model_options().get('type')
face_swapper_weight = state_manager.get_item('face_swapper_weight')
face_swapper_weight = numpy.interp(face_swapper_weight, [ 0, 1 ], [ 0.35, -0.35 ]).astype(numpy.float32)
if model_type in [ 'hififace', 'hyperswap', 'inswapper', 'simswap' ]:
target_embedding = target_embedding / numpy.linalg.norm(target_embedding)
source_embedding = source_embedding.reshape(1, -1)
target_embedding = target_embedding.reshape(1, -1)
source_embedding = source_embedding * (1 - face_swapper_weight) + target_embedding * face_swapper_weight
return source_embedding
def convert_source_embedding(source_embedding : Embedding) -> Tuple[Embedding, Embedding]:
source_embedding = forward_convert_embedding(source_embedding)
source_embedding = source_embedding.ravel()
source_embedding_norm = source_embedding / numpy.linalg.norm(source_embedding)
return source_embedding, source_embedding_norm
def prepare_crop_frame(crop_vision_frame : VisionFrame) -> VisionFrame:
model_mean = get_model_options().get('mean')
model_standard_deviation = get_model_options().get('standard_deviation')
crop_vision_frame = crop_vision_frame[:, :, ::-1] / 255.0
crop_vision_frame = (crop_vision_frame - model_mean) / model_standard_deviation
crop_vision_frame = crop_vision_frame.transpose(2, 0, 1)
crop_vision_frame = numpy.expand_dims(crop_vision_frame, axis = 0).astype(numpy.float32)
return crop_vision_frame
def normalize_crop_frame(crop_vision_frame : VisionFrame) -> VisionFrame:
model_type = get_model_options().get('type')
model_mean = get_model_options().get('mean')
model_standard_deviation = get_model_options().get('standard_deviation')
crop_vision_frame = crop_vision_frame.transpose(1, 2, 0)
if model_type in [ 'ghost', 'hififace', 'hyperswap', 'uniface' ]:
crop_vision_frame = crop_vision_frame * model_standard_deviation + model_mean
crop_vision_frame = crop_vision_frame.clip(0, 1)
crop_vision_frame = crop_vision_frame[:, :, ::-1] * 255
return crop_vision_frame
def extract_source_face(source_vision_frames : List[VisionFrame]) -> Optional[Face]:
source_faces = []
if source_vision_frames:
for source_vision_frame in source_vision_frames:
temp_faces = get_many_faces([source_vision_frame])
temp_faces = sort_faces_by_order(temp_faces, 'large-small')
if temp_faces:
source_faces.append(get_first(temp_faces))
return get_average_face(source_faces)
def process_frame(inputs : FaceSwapperInputs) -> ProcessorOutputs:
reference_vision_frame = inputs.get('reference_vision_frame')
source_vision_frames = inputs.get('source_vision_frames')
target_vision_frame = inputs.get('target_vision_frame')
temp_vision_frame = inputs.get('temp_vision_frame')
temp_vision_mask = inputs.get('temp_vision_mask')
source_face = extract_source_face(source_vision_frames)
target_faces = select_faces(reference_vision_frame, target_vision_frame)
if source_face and target_faces:
for target_face in target_faces:
target_face = scale_face(target_face, target_vision_frame, temp_vision_frame)
temp_vision_frame = swap_face(source_face, target_face, temp_vision_frame)
return temp_vision_frame, temp_vision_mask

View File

@@ -1,20 +0,0 @@
from facefusion.types import Locals
LOCALS : Locals =\
{
'en':
{
'help':
{
'model': 'choose the model responsible for swapping the face',
'pixel_boost': 'choose the pixel boost resolution for the face swapper',
'weight': 'specify the degree of weight applied to the face'
},
'uis':
{
'model_dropdown': 'FACE SWAPPER MODEL',
'pixel_boost_dropdown': 'FACE SWAPPER PIXEL BOOST',
'weight_slider': 'FACE SWAPPER WEIGHT'
}
}
}

Some files were not shown because too many files have changed in this diff Show More