Spaces:
Runtime error
Runtime error
Delete facefusion
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
Files changed (50)
- facefusion/__init__.pyDeleted
- facefusion/app_context.pyDeleted-16
- facefusion/args.pyDeleted-131
- facefusion/audio.pyDeleted-143
- facefusion/benchmarker.pyDeleted-111
- facefusion/camera_manager.pyDeleted-53
- facefusion/choices.pyDeleted-169
- facefusion/cli_helper.pyDeleted-35
- facefusion/common_helper.pyDeleted-84
- facefusion/config.pyDeleted-74
- facefusion/content_analyser.pyDeleted-247
- facefusion/core.pyDeleted-350
- facefusion/curl_builder.pyDeleted-32
- facefusion/download.pyDeleted-175
- facefusion/execution.pyDeleted-160
- facefusion/exit_helper.pyDeleted-34
- facefusion/face_analyser.pyDeleted-143
- facefusion/face_classifier.pyDeleted-140
- facefusion/face_detector.pyDeleted-458
- facefusion/face_helper.pyDeleted-256
- facefusion/face_landmarker.pyDeleted-242
- facefusion/face_masker.pyDeleted-279
- facefusion/face_recognizer.pyDeleted-93
- facefusion/face_selector.pyDeleted-130
- facefusion/face_store.pyDeleted-28
- facefusion/ffmpeg.pyDeleted-291
- facefusion/ffmpeg_builder.pyDeleted-267
- facefusion/filesystem.pyDeleted-190
- facefusion/hash_helper.pyDeleted-32
- facefusion/inference_manager.pyDeleted-96
- facefusion/installer.pyDeleted-109
- facefusion/jobs/__init__.pyDeleted
- facefusion/jobs/job_helper.pyDeleted-20
- facefusion/jobs/job_list.pyDeleted-34
- facefusion/jobs/job_manager.pyDeleted-265
- facefusion/jobs/job_runner.pyDeleted-112
- facefusion/jobs/job_store.pyDeleted-27
- facefusion/json.pyDeleted-22
- facefusion/locales.pyDeleted-274
- facefusion/logger.pyDeleted-48
- facefusion/memory.pyDeleted-21
- facefusion/metadata.pyDeleted-15
- facefusion/model_helper.pyDeleted-11
- facefusion/normalizer.pyDeleted-33
- facefusion/process_manager.pyDeleted-45
- facefusion/processors/__init__.pyDeleted
- facefusion/processors/core.pyDeleted-44
- facefusion/processors/live_portrait.pyDeleted-101
- facefusion/processors/modules/__init__.pyDeleted
- facefusion/processors/modules/age_modifier/__init__.pyDeleted
12345678910111213141516import os
import sys
from facefusion.types import AppContext
def detect_app_context() -> AppContext:
frame = sys._getframe(1)
while frame:
if os.path.join('facefusion', 'jobs') in frame.f_code.co_filename:
return 'cli'
if os.path.join('facefusion', 'uis') in frame.f_code.co_filename:
return 'ui'
frame = frame.f_back
return 'cli'123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131from facefusion import state_manager
from facefusion.filesystem import get_file_name, is_video, resolve_file_paths
from facefusion.jobs import job_store
from facefusion.normalizer import normalize_fps, normalize_space
from facefusion.processors.core import get_processors_modules
from facefusion.types import ApplyStateItem, Args
from facefusion.vision import detect_video_fps
def reduce_step_args(args : Args) -> Args:
step_args =\
{
key: args[key] for key in args if key in job_store.get_step_keys()
}
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:
step_args =\
{
key: state_manager.get_item(key) for key in job_store.get_step_keys() #type:ignore[arg-type]
}
return step_args
def collect_job_args() -> Args:
job_args =\
{
key: state_manager.get_item(key) for key in job_store.get_job_keys() #type:ignore[arg-type]
}
return job_args
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
# general
apply_state_item('command', args.get('command'))
# paths
apply_state_item('temp_path', args.get('temp_path'))
apply_state_item('jobs_path', args.get('jobs_path'))
apply_state_item('source_paths', args.get('source_paths'))
apply_state_item('target_path', args.get('target_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
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_margin', normalize_space(args.get('face_detector_margin')))
apply_state_item('face_detector_angles', args.get('face_detector_angles'))
apply_state_item('face_detector_score', args.get('face_detector_score'))
# face landmarker
apply_state_item('face_landmarker_model', args.get('face_landmarker_model'))
apply_state_item('face_landmarker_score', args.get('face_landmarker_score'))
# face selector
apply_state_item('face_selector_mode', args.get('face_selector_mode'))
apply_state_item('face_selector_order', args.get('face_selector_order'))
apply_state_item('face_selector_age_start', args.get('face_selector_age_start'))
apply_state_item('face_selector_age_end', args.get('face_selector_age_end'))
apply_state_item('face_selector_gender', args.get('face_selector_gender'))
apply_state_item('face_selector_race', args.get('face_selector_race'))
apply_state_item('reference_face_position', args.get('reference_face_position'))
apply_state_item('reference_face_distance', args.get('reference_face_distance'))
apply_state_item('reference_frame_number', args.get('reference_frame_number'))
# 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_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_padding', normalize_space(args.get('face_mask_padding')))
# voice extractor
apply_state_item('voice_extractor_model', args.get('voice_extractor_model'))
# frame extraction
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('temp_frame_format', args.get('temp_frame_format'))
apply_state_item('keep_temp', args.get('keep_temp'))
# output creation
apply_state_item('output_image_quality', args.get('output_image_quality'))
apply_state_item('output_image_scale', args.get('output_image_scale'))
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_preset', args.get('output_video_preset'))
apply_state_item('output_video_quality', args.get('output_video_quality'))
apply_state_item('output_video_scale', args.get('output_video_scale'))
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'))
apply_state_item('output_video_fps', output_video_fps)
# processors
available_processors = [ get_file_name(file_path) for file_path in resolve_file_paths('facefusion/processors/modules') ]
apply_state_item('processors', args.get('processors'))
for processor_module in get_processors_modules(available_processors):
processor_module.apply_args(args, apply_state_item)
# uis
apply_state_item('open_browser', args.get('open_browser'))
apply_state_item('ui_layouts', args.get('ui_layouts'))
apply_state_item('ui_workflow', args.get('ui_workflow'))
# execution
apply_state_item('execution_device_ids', args.get('execution_device_ids'))
apply_state_item('execution_providers', args.get('execution_providers'))
apply_state_item('execution_thread_count', args.get('execution_thread_count'))
# download
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
apply_state_item('video_memory_strategy', args.get('video_memory_strategy'))
apply_state_item('system_memory_limit', args.get('system_memory_limit'))
# misc
apply_state_item('log_level', args.get('log_level'))
apply_state_item('halt_on_error', args.get('halt_on_error'))
# jobs
apply_state_item('job_id', args.get('job_id'))
apply_state_item('job_status', args.get('job_status'))
apply_state_item('step_index', args.get('step_index'))123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143from functools import lru_cache
from typing import Any, List, Optional
import numpy
import scipy
from numpy.typing import NDArray
from facefusion.ffmpeg import read_audio_buffer
from facefusion.filesystem import is_audio
from facefusion.types import Audio, AudioFrame, Fps, Mel, MelFilterBank, Spectrogram
from facefusion.voice_extractor import batch_extract_voice
@lru_cache(maxsize = 64)
def read_static_audio(audio_path : str, fps : Fps) -> Optional[List[AudioFrame]]:
return read_audio(audio_path, fps)
def read_audio(audio_path : str, fps : Fps) -> Optional[List[AudioFrame]]:
audio_sample_rate = 48000
audio_sample_size = 16
audio_channel_total = 2
if is_audio(audio_path):
audio_buffer = read_audio_buffer(audio_path, audio_sample_rate, audio_sample_size, audio_channel_total)
audio = numpy.frombuffer(audio_buffer, dtype = numpy.int16).reshape(-1, 2)
audio = prepare_audio(audio)
spectrogram = create_spectrogram(audio)
audio_frames = extract_audio_frames(spectrogram, fps)
return audio_frames
return None
@lru_cache(maxsize = 64)
def read_static_voice(audio_path : str, fps : Fps) -> Optional[List[AudioFrame]]:
return read_voice(audio_path, fps)
def read_voice(audio_path : str, fps : Fps) -> Optional[List[AudioFrame]]:
voice_sample_rate = 48000
voice_sample_size = 16
voice_channel_total = 2
voice_chunk_size = 240 * 1024
voice_step_size = 180 * 1024
if is_audio(audio_path):
audio_buffer = read_audio_buffer(audio_path, voice_sample_rate, voice_sample_size, voice_channel_total)
audio = numpy.frombuffer(audio_buffer, dtype = numpy.int16).reshape(-1, 2)
audio = batch_extract_voice(audio, voice_chunk_size, voice_step_size)
audio = prepare_voice(audio)
spectrogram = create_spectrogram(audio)
audio_frames = extract_audio_frames(spectrogram, fps)
return audio_frames
return None
def get_audio_frame(audio_path : str, fps : Fps, frame_number : int = 0) -> Optional[AudioFrame]:
if is_audio(audio_path):
audio_frames = read_static_audio(audio_path, fps)
if frame_number in range(len(audio_frames)):
return audio_frames[frame_number]
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]:
if is_audio(audio_path):
voice_frames = read_static_voice(audio_path, fps)
if frame_number in range(len(voice_frames)):
return voice_frames[frame_number]
return None
def create_empty_audio_frame() -> AudioFrame:
mel_filter_total = 80
audio_step_size = 16
audio_frame = numpy.zeros((mel_filter_total, audio_step_size)).astype(numpy.int16)
return audio_frame
def prepare_audio(audio : Audio) -> Audio:
if audio.ndim > 1:
audio = numpy.mean(audio, axis = 1)
audio = audio / numpy.max(numpy.abs(audio), axis = 0)
audio = scipy.signal.lfilter([ 1.0, -0.97 ], [ 1.0 ], audio)
return audio
def prepare_voice(audio : Audio) -> Audio:
audio_sample_rate = 48000
audio_resample_rate = 16000
audio_resample_factor = round(len(audio) * audio_resample_rate / audio_sample_rate)
audio = scipy.signal.resample(audio, audio_resample_factor)
audio = prepare_audio(audio)
return audio
def convert_hertz_to_mel(hertz : float) -> float:
return 2595 * numpy.log10(1 + hertz / 700)
def convert_mel_to_hertz(mel : Mel) -> NDArray[Any]:
return 700 * (10 ** (mel / 2595) - 1)
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_bin_total = 800
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)
indices = numpy.floor((mel_bin_total + 1) * convert_mel_to_hertz(mel_frequency_range) / audio_sample_rate).astype(numpy.int16)
for index in range(mel_filter_total):
start = indices[index]
end = indices[index + 1]
mel_filter_bank[index, start:end] = scipy.signal.windows.triang(end - start)
return mel_filter_bank
def create_spectrogram(audio : Audio) -> Spectrogram:
mel_bin_total = 800
mel_bin_overlap = 600
mel_filter_bank = create_mel_filter_bank()
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))
return spectrogram123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111import 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)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253from 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_ids123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169import logging
from typing import List, Sequence
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
face_detector_set : FaceDetectorSet =\
{
'many': [ '640x640' ],
'retinaface': [ '160x160', '320x320', '480x480', '512x512', '640x640' ],
'scrfd': [ '160x160', '320x320', '480x480', '512x512', '640x640' ],
'yolo_face': [ '640x640' ],
'yunet': [ '640x640' ]
}
face_detector_models : List[FaceDetectorModel] = list(face_detector_set.keys())
face_landmarker_models : List[FaceLandmarkerModel] = [ 'many', '2dfan4', 'peppa_wutz' ]
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_genders : List[Gender] = [ 'female', 'male' ]
face_selector_races : List[Race] = [ 'white', 'black', 'latino', 'asian', 'indian', 'arabic' ]
face_occluder_models : List[FaceOccluderModel] = [ 'many', 'xseg_1', 'xseg_2', 'xseg_3' ]
face_parser_models : List[FaceParserModel] = [ 'bisenet_resnet_18', 'bisenet_resnet_34' ]
face_mask_types : List[FaceMaskType] = [ 'box', 'occlusion', 'area', 'region' ]
face_mask_area_set : FaceMaskAreaSet =\
{
'upper-face': [ 0, 1, 2, 31, 32, 33, 34, 35, 14, 15, 16, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17 ],
'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' ]
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 =\
{
'error': logging.ERROR,
'warn': logging.WARNING,
'info': logging.INFO,
'debug': logging.DEBUG
}
log_levels : List[LogLevel] = list(log_level_set.keys())
ui_workflows : List[UiWorkflow] = [ 'instant_runner', 'job_runner', 'job_manager' ]
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)
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_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_padding_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)
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_scale_range : Sequence[float] = create_float_range(0.25, 8.0, 0.25)1234567891011121314151617181920212223242526272829303132333435from 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) + '-+'123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384import platform
from typing import Any, Iterable, Optional, Reversible, Sequence
def is_linux() -> bool:
return platform.system().lower() == 'linux'
def is_macos() -> bool:
return platform.system().lower() == 'darwin'
def is_windows() -> bool:
return platform.system().lower() == 'windows'
def create_int_metavar(int_range : Sequence[int]) -> str:
return '[' + str(int_range[0]) + '..' + str(int_range[-1]) + ':' + str(calculate_int_step(int_range)) + ']'
def create_float_metavar(float_range : Sequence[float]) -> str:
return '[' + str(float_range[0]) + '..' + str(float_range[-1]) + ':' + str(calculate_float_step(float_range)) + ']'
def create_int_range(start : int, end : int, step : int) -> Sequence[int]:
int_range = []
current = start
while current <= end:
int_range.append(current)
current += step
return int_range
def create_float_range(start : float, end : float, step : float) -> Sequence[float]:
float_range = []
current = start
while current <= end:
float_range.append(round(current, 2))
current = round(current + step, 2)
return float_range
def calculate_int_step(int_range : Sequence[int]) -> int:
return int_range[1] - int_range[0]
def calculate_float_step(float_range : Sequence[float]) -> float:
return round(float_range[1] - float_range[0], 2)
def cast_int(value : Any) -> Optional[int]:
try:
return int(value)
except (ValueError, TypeError):
return None
def cast_float(value : Any) -> Optional[float]:
try:
return float(value)
except (ValueError, TypeError):
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:
if isinstance(__list__, Iterable):
return next(iter(__list__), None)
return None
def get_last(__list__ : Any) -> Any:
if isinstance(__list__, Reversible):
return next(reversed(__list__), None)
return None1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374from configparser import ConfigParser
from typing import List, Optional
from facefusion import state_manager
from facefusion.common_helper import cast_bool, cast_float, cast_int
CONFIG_PARSER = None
def get_config_parser() -> ConfigParser:
global CONFIG_PARSER
if CONFIG_PARSER is None:
CONFIG_PARSER = ConfigParser()
CONFIG_PARSER.read(state_manager.get_item('config_path'), encoding = 'utf-8')
return CONFIG_PARSER
def clear_config_parser() -> None:
global CONFIG_PARSER
CONFIG_PARSER = None
def get_str_value(section : str, option : str, fallback : Optional[str] = None) -> Optional[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)
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
def get_int_list(section : str, option : str, fallback : Optional[str] = None) -> Optional[List[int]]:
config_parser = get_config_parser()
if config_parser.has_option(section, option) and config_parser.get(section, option).strip():
return list(map(int, config_parser.get(section, option).split()))
if fallback:
return list(map(int, fallback.split()))
return None123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247from functools import lru_cache
from typing import List, Tuple
import numpy
from tqdm import tqdm
from facefusion import inference_manager, state_manager, translator
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 resolve_relative_path
from facefusion.thread_helper import conditional_thread_semaphore
from facefusion.types import Detection, DownloadScope, DownloadSet, ExecutionProvider, Fps, InferencePool, ModelSet, VisionFrame
from facefusion.vision import detect_video_fps, fit_contain_frame, read_image, read_video_frame
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:
model_names = [ 'nsfw_1', 'nsfw_2', 'nsfw_3' ]
_, model_source_set = collect_model_downloads()
return inference_manager.get_inference_pool(__name__, model_names, model_source_set)
def clear_inference_pool() -> None:
model_names = [ 'nsfw_1', 'nsfw_2', 'nsfw_3' ]
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 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:
model_hash_set, model_source_set = collect_model_downloads()
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
def analyse_stream(vision_frame : VisionFrame, video_fps : Fps) -> bool:
global STREAM_COUNTER
STREAM_COUNTER = STREAM_COUNTER + 1
if STREAM_COUNTER % int(video_fps) == 0:
return analyse_frame(vision_frame)
return False
def analyse_frame(vision_frame : VisionFrame) -> bool:
return detect_nsfw(vision_frame)
@lru_cache()
def analyse_image(image_path : str) -> bool:
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 is_nsfw_1 and is_nsfw_2 or is_nsfw_1 and is_nsfw_3 or is_nsfw_2 and is_nsfw_3
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():
detection = content_analyser.run(None,
{
'input': vision_frame
})[0]
if model_name in [ 'nsfw_2', 'nsfw_3' ]:
return detection[0]
return detection
def prepare_detect_frame(temp_vision_frame : VisionFrame, model_name : str) -> VisionFrame:
model_set = create_static_model_set('full').get(model_name)
model_size = model_set.get('size')
model_mean = model_set.get('mean')
model_standard_deviation = model_set.get('standard_deviation')
detect_vision_frame = fit_contain_frame(temp_vision_frame, model_size)
detect_vision_frame = detect_vision_frame[:, :, ::-1] / 255.0
detect_vision_frame -= model_mean
detect_vision_frame /= model_standard_deviation
detect_vision_frame = numpy.expand_dims(detect_vision_frame.transpose(2, 0, 1), axis = 0).astype(numpy.float32)
return detect_vision_frame123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350import inspect
import itertools
import shutil
import signal
import sys
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
from facefusion.args import apply_args, collect_job_args, reduce_job_args, reduce_step_args
from facefusion.download import conditional_download_hashes, conditional_download_sources
from facefusion.exit_helper import hard_exit, signal_exit
from facefusion.filesystem import get_file_extension, get_file_name, is_image, is_video, resolve_file_paths, resolve_file_pattern
from facefusion.jobs import job_helper, job_manager, job_runner
from facefusion.jobs.job_list import compose_job_list
from facefusion.memory import limit_system_memory
from facefusion.processors.core import get_processors_modules
from facefusion.program import create_program
from facefusion.program_helper import validate_args
from facefusion.types import Args, ErrorCode
from facefusion.workflows import image_to_image, image_to_video
def cli() -> None:
if pre_check():
signal.signal(signal.SIGINT, signal_exit)
program = create_program()
if validate_args(program):
args = vars(program.parse_args())
apply_args(args, state_manager.init_item)
if state_manager.get_item('command'):
logger.init(state_manager.get_item('log_level'))
route(args)
else:
program.print_help()
else:
hard_exit(2)
else:
hard_exit(2)
def route(args : Args) -> None:
system_memory_limit = state_manager.get_item('system_memory_limit')
if system_memory_limit and system_memory_limit > 0:
limit_system_memory(system_memory_limit)
if state_manager.get_item('command') == 'force-download':
error_code = force_download()
hard_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 not job_manager.init_jobs(state_manager.get_item('jobs_path')):
hard_exit(1)
error_code = route_job_manager(args)
hard_exit(error_code)
if state_manager.get_item('command') == 'run':
import facefusion.uis.core as ui
if not common_pre_check() or not processors_pre_check():
hard_exit(2)
for ui_layout in ui.get_ui_layouts_modules(state_manager.get_item('ui_layouts')):
if not ui_layout.pre_check():
hard_exit(2)
ui.init()
ui.launch()
if state_manager.get_item('command') == 'headless-run':
if not job_manager.init_jobs(state_manager.get_item('jobs_path')):
hard_exit(1)
error_code = process_headless(args)
hard_exit(error_code)
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 not job_manager.init_jobs(state_manager.get_item('jobs_path')):
hard_exit(1)
error_code = route_job_runner()
hard_exit(error_code)
def pre_check() -> bool:
if sys.version_info < (3, 10):
logger.error(translator.get('python_not_supported').format(version = '3.10'), __name__)
return False
if not shutil.which('curl'):
logger.error(translator.get('curl_not_installed'), __name__)
return False
if not shutil.which('ffmpeg'):
logger.error(translator.get('ffmpeg_not_installed'), __name__)
return False
return True
def common_pre_check() -> bool:
common_modules =\
[
content_analyser,
face_classifier,
face_detector,
face_landmarker,
face_masker,
face_recognizer,
voice_extractor
]
content_analyser_content = inspect.getsource(content_analyser).encode()
content_analyser_hash = hash_helper.create_hash(content_analyser_content)
return all(module.pre_check() for module in common_modules) and content_analyser_hash == 'b14e7b92'
def processors_pre_check() -> bool:
for processor_module in get_processors_modules(state_manager.get_item('processors')):
if not processor_module.pre_check():
return False
return True
def force_download() -> ErrorCode:
common_modules =\
[
content_analyser,
face_classifier,
face_detector,
face_landmarker,
face_masker,
face_recognizer,
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)
for module in common_modules + processor_modules:
if hasattr(module, 'create_static_model_set'):
for model in module.create_static_model_set(state_manager.get_item('download_scope')).values():
model_hash_set = model.get('hashes')
model_source_set = model.get('sources')
if model_hash_set and model_source_set:
if not conditional_download_hashes(model_hash_set) or not conditional_download_sources(model_source_set):
return 1
return 0
def route_job_manager(args : Args) -> ErrorCode:
if state_manager.get_item('command') == 'job-list':
job_headers, job_contents = compose_job_list(state_manager.get_item('job_status'))
if job_contents:
cli_helper.render_table(job_headers, job_contents)
return 0
return 1
if state_manager.get_item('command') == 'job-create':
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__)
return 0
logger.error(translator.get('job_not_created').format(job_id = state_manager.get_item('job_id')), __name__)
return 1
if state_manager.get_item('command') == 'job-submit':
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__)
return 0
logger.error(translator.get('job_not_submitted').format(job_id = state_manager.get_item('job_id')), __name__)
return 1
if state_manager.get_item('command') == 'job-submit-all':
if job_manager.submit_jobs(state_manager.get_item('halt_on_error')):
logger.info(translator.get('job_all_submitted'), __name__)
return 0
logger.error(translator.get('job_all_not_submitted'), __name__)
return 1
if state_manager.get_item('command') == 'job-delete':
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__)
return 0
logger.error(translator.get('job_not_deleted').format(job_id = state_manager.get_item('job_id')), __name__)
return 1
if state_manager.get_item('command') == 'job-delete-all':
if job_manager.delete_jobs(state_manager.get_item('halt_on_error')):
logger.info(translator.get('job_all_deleted'), __name__)
return 0
logger.error(translator.get('job_all_not_deleted'), __name__)
return 1
if state_manager.get_item('command') == 'job-add-step':
step_args = reduce_step_args(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__)
return 0
logger.error(translator.get('job_step_not_added').format(job_id = state_manager.get_item('job_id')), __name__)
return 1
if state_manager.get_item('command') == 'job-remix-step':
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):
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__)
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__)
return 1
if state_manager.get_item('command') == 'job-insert-step':
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):
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__)
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__)
return 1
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')):
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__)
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__)
return 1
return 1
def route_job_runner() -> ErrorCode:
if state_manager.get_item('command') == 'job-run':
logger.info(translator.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):
logger.info(translator.get('processing_job_succeeded').format(job_id = state_manager.get_item('job_id')), __name__)
return 0
logger.info(translator.get('processing_job_failed').format(job_id = state_manager.get_item('job_id')), __name__)
return 1
if state_manager.get_item('command') == 'job-run-all':
logger.info(translator.get('running_jobs'), __name__)
if job_runner.run_jobs(process_step, state_manager.get_item('halt_on_error')):
logger.info(translator.get('processing_jobs_succeeded'), __name__)
return 0
logger.info(translator.get('processing_jobs_failed'), __name__)
return 1
if state_manager.get_item('command') == 'job-retry':
logger.info(translator.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):
logger.info(translator.get('processing_job_succeeded').format(job_id = state_manager.get_item('job_id')), __name__)
return 0
logger.info(translator.get('processing_job_failed').format(job_id = state_manager.get_item('job_id')), __name__)
return 1
if state_manager.get_item('command') == 'job-retry-all':
logger.info(translator.get('retrying_jobs'), __name__)
if job_runner.retry_jobs(process_step, state_manager.get_item('halt_on_error')):
logger.info(translator.get('processing_jobs_succeeded'), __name__)
return 0
logger.info(translator.get('processing_jobs_failed'), __name__)
return 1
return 2
def process_headless(args : Args) -> ErrorCode:
job_id = job_helper.suggest_job_id('headless')
step_args = reduce_step_args(args)
if job_manager.create_job(job_id) and job_manager.add_step(job_id, step_args) and job_manager.submit_job(job_id) and job_runner.run_job(job_id, process_step):
return 0
return 1
def process_batch(args : Args) -> ErrorCode:
job_id = job_helper.suggest_job_id('batch')
step_args = reduce_step_args(args)
job_args = reduce_job_args(args)
source_paths = resolve_file_pattern(job_args.get('source_pattern'))
target_paths = resolve_file_pattern(job_args.get('target_pattern'))
if job_manager.create_job(job_id):
if source_paths and target_paths:
for index, (source_path, target_path) in enumerate(itertools.product(source_paths, target_paths)):
step_args['source_paths'] = [ source_path ]
step_args['target_path'] = target_path
try:
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))
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
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')):
if not processor_module.pre_process('output'):
return 2
if is_image(state_manager.get_item('target_path')):
return image_to_image.process(start_time)
if is_video(state_manager.get_item('target_path')):
return image_to_video.process(start_time)
return 0
1234567891011121314151617181920212223242526272829303132import 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 ping(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) ]
def set_retry(retry : int) -> List[Command]:
return [ '--retry', str(retry) ]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175import os
import subprocess
from functools import lru_cache
from typing import List, Optional, Tuple
from urllib.parse import urlparse
from tqdm import tqdm
import facefusion.choices
from facefusion import curl_builder, logger, process_manager, state_manager, translator
from facefusion.filesystem import get_file_name, get_file_size, is_file, remove_file
from facefusion.hash_helper import validate_hash
from facefusion.types import Command, DownloadProvider, DownloadSet
def open_curl(commands : List[Command]) -> subprocess.Popen[bytes]:
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:
for url in urls:
download_file_name = os.path.basename(urlparse(url).path)
download_file_path = os.path.join(download_directory_path, download_file_name)
initial_size = get_file_size(download_file_path)
download_size = get_static_download_size(url)
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:
commands = curl_builder.chain(
curl_builder.download(url, download_file_path),
curl_builder.set_timeout(5),
curl_builder.set_retry(5)
)
open_curl(commands)
current_size = initial_size
progress.set_postfix(download_providers = state_manager.get_item('download_providers'), file_name = download_file_name)
while current_size < download_size:
if is_file(download_file_path):
current_size = get_file_size(download_file_path)
progress.update(current_size - progress.n)
@lru_cache(maxsize = 64)
def get_static_download_size(url : str) -> int:
commands = curl_builder.chain(
curl_builder.ping(url),
curl_builder.set_timeout(5)
)
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 0
@lru_cache(maxsize = 64)
def ping_static_url(url : str) -> bool:
commands = curl_builder.chain(
curl_builder.ping(url),
curl_builder.set_timeout(5)
)
process = open_curl(commands)
process.communicate()
return process.returncode == 0
def conditional_download_hashes(hash_set : DownloadSet) -> bool:
hash_paths = [ hash_set.get(hash_key).get('path') for hash_key in hash_set.keys() ]
process_manager.check()
_, invalid_hash_paths = validate_hash_paths(hash_paths)
if invalid_hash_paths:
for index in hash_set:
if hash_set.get(index).get('path') in invalid_hash_paths:
invalid_hash_url = hash_set.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 ])
valid_hash_paths, invalid_hash_paths = validate_hash_paths(hash_paths)
for valid_hash_path in valid_hash_paths:
valid_hash_file_name = get_file_name(valid_hash_path)
logger.debug(translator.get('validating_hash_succeeded').format(hash_file_name = valid_hash_file_name), __name__)
for invalid_hash_path in invalid_hash_paths:
invalid_hash_file_name = get_file_name(invalid_hash_path)
logger.error(translator.get('validating_hash_failed').format(hash_file_name = invalid_hash_file_name), __name__)
if not invalid_hash_paths:
process_manager.end()
return not invalid_hash_paths
def conditional_download_sources(source_set : DownloadSet) -> bool:
source_paths = [ source_set.get(source_key).get('path') for source_key in source_set.keys() ]
process_manager.check()
_, invalid_source_paths = validate_source_paths(source_paths)
if invalid_source_paths:
for index in source_set:
if source_set.get(index).get('path') in invalid_source_paths:
invalid_source_url = source_set.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 ])
valid_source_paths, invalid_source_paths = validate_source_paths(source_paths)
for valid_source_path in valid_source_paths:
valid_source_file_name = get_file_name(valid_source_path)
logger.debug(translator.get('validating_source_succeeded').format(source_file_name = valid_source_file_name), __name__)
for invalid_source_path in invalid_source_paths:
invalid_source_file_name = get_file_name(invalid_source_path)
logger.error(translator.get('validating_source_failed').format(source_file_name = invalid_source_file_name), __name__)
if remove_file(invalid_source_path):
logger.error(translator.get('deleting_corrupt_source').format(source_file_name = invalid_source_file_name), __name__)
if not invalid_source_paths:
process_manager.end()
return not invalid_source_paths
def validate_hash_paths(hash_paths : List[str]) -> Tuple[List[str], List[str]]:
valid_hash_paths = []
invalid_hash_paths = []
for hash_path in hash_paths:
if is_file(hash_path):
valid_hash_paths.append(hash_path)
else:
invalid_hash_paths.append(hash_path)
return valid_hash_paths, invalid_hash_paths
def validate_source_paths(source_paths : List[str]) -> Tuple[List[str], List[str]]:
valid_source_paths = []
invalid_source_paths = []
for source_path in source_paths:
if validate_hash(source_path):
valid_source_paths.append(source_path)
else:
invalid_source_paths.append(source_path)
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 None123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160import shutil
import subprocess
import xml.etree.ElementTree as ElementTree
from functools import lru_cache
from typing import List, Optional
from onnxruntime import get_available_providers, set_default_logger_severity
import facefusion.choices
from facefusion.types import ExecutionDevice, ExecutionProvider, InferenceSessionProvider, ValueAndUnit
set_default_logger_severity(3)
def has_execution_provider(execution_provider : ExecutionProvider) -> bool:
return execution_provider in get_available_execution_providers()
def get_available_execution_providers() -> List[ExecutionProvider]:
inference_session_providers = get_available_providers()
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]:
inference_session_providers : List[InferenceSessionProvider] = []
for execution_provider in execution_providers:
if execution_provider == 'cuda':
inference_session_providers.append((facefusion.choices.execution_provider_set.get(execution_provider),
{
'device_id': execution_device_id,
'cudnn_conv_algo_search': resolve_cudnn_conv_algo_search()
}))
if execution_provider == 'tensorrt':
inference_session_providers.append((facefusion.choices.execution_provider_set.get(execution_provider),
{
'device_id': execution_device_id,
'trt_engine_cache_enable': True,
'trt_engine_cache_path': '.caches',
'trt_timing_cache_enable': True,
'trt_timing_cache_path': '.caches',
'trt_builder_optimization_level': 5
}))
if execution_provider in [ 'directml', 'rocm' ]:
inference_session_providers.append((facefusion.choices.execution_provider_set.get(execution_provider),
{
'device_id': execution_device_id
}))
if execution_provider == 'migraphx':
inference_session_providers.append((facefusion.choices.execution_provider_set.get(execution_provider),
{
'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:
inference_session_providers.append(facefusion.choices.execution_provider_set.get('cpu'))
return inference_session_providers
def resolve_cudnn_conv_algo_search() -> str:
execution_devices = detect_static_execution_devices()
product_names = ('GeForce GTX 1630', 'GeForce GTX 1650', 'GeForce GTX 1660')
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]:
commands = [ shutil.which('nvidia-smi'), '--query', '--xml-format' ]
return subprocess.Popen(commands, stdout = subprocess.PIPE)
@lru_cache()
def detect_static_execution_devices() -> List[ExecutionDevice]:
return detect_execution_devices()
def detect_execution_devices() -> List[ExecutionDevice]:
execution_devices : List[ExecutionDevice] = []
try:
output, _ = run_nvidia_smi().communicate()
root_element = ElementTree.fromstring(output)
except Exception:
root_element = ElementTree.Element('xml')
for gpu_element in root_element.findall('gpu'):
execution_devices.append(
{
'driver_version': root_element.findtext('driver_version'),
'framework':
{
'name': 'CUDA',
'version': root_element.findtext('cuda_version')
},
'product':
{
'vendor': 'NVIDIA',
'name': gpu_element.findtext('product_name').replace('NVIDIA', '').strip()
},
'video_memory':
{
'total': create_value_and_unit(gpu_element.findtext('fb_memory_usage/total')),
'free': create_value_and_unit(gpu_element.findtext('fb_memory_usage/free'))
},
'temperature':
{
'gpu': create_value_and_unit(gpu_element.findtext('temperature/gpu_temp')),
'memory': create_value_and_unit(gpu_element.findtext('temperature/memory_temp'))
},
'utilization':
{
'gpu': create_value_and_unit(gpu_element.findtext('utilization/gpu_util')),
'memory': create_value_and_unit(gpu_element.findtext('utilization/memory_util'))
}
})
return execution_devices
def create_value_and_unit(text : str) -> Optional[ValueAndUnit]:
if ' ' in text:
value, unit = text.split()
return\
{
'value': int(value),
'unit': str(unit)
}
return None12345678910111213141516171819202122232425262728293031323334import os
import signal
import sys
from time import sleep
from types import FrameType
from facefusion import process_manager, state_manager
from facefusion.temp_helper import clear_temp_directory
from facefusion.types import ErrorCode
def fatal_exit(error_code : ErrorCode) -> None:
os._exit(error_code)
def hard_exit(error_code : ErrorCode) -> None:
sys.exit(error_code)
def signal_exit(signum : int, frame : FrameType) -> None:
graceful_exit(0)
def graceful_exit(error_code : ErrorCode) -> None:
signal.signal(signal.SIGINT, signal.SIG_IGN)
process_manager.stop()
while process_manager.is_processing():
sleep(0.5)
if state_manager.get_item('target_path'):
clear_temp_directory(state_manager.get_item('target_path'))
hard_exit(error_code)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143from typing import List, Optional
import numpy
from facefusion import state_manager
from facefusion.common_helper import get_first
from facefusion.face_classifier import classify_face
from facefusion.face_detector import detect_faces, detect_faces_by_angle
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_recognizer import calculate_face_embedding
from facefusion.face_store import get_static_faces, set_static_faces
from facefusion.types 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]:
faces = []
nms_threshold = get_nms_threshold(state_manager.get_item('face_detector_model'), state_manager.get_item('face_detector_angles'))
keep_indices = apply_nms(bounding_boxes, face_scores, state_manager.get_item('face_detector_score'), nms_threshold)
for index in keep_indices:
bounding_box = bounding_boxes[index]
face_score = face_scores[index]
face_landmark_5 = face_landmarks_5[index]
face_landmark_5_68 = face_landmark_5
face_landmark_68_5 = estimate_face_landmark_68_5(face_landmark_5_68)
face_landmark_68 = face_landmark_68_5
face_landmark_score_68 = 0.0
face_angle = estimate_face_angle(face_landmark_68_5)
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)
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_set : FaceLandmarkSet =\
{
'5': face_landmark_5,
'5/68': face_landmark_5_68,
'68': face_landmark_68,
'68/5': face_landmark_68_5
}
face_score_set : FaceScoreSet =\
{
'detector': face_score,
'landmarker': face_landmark_score_68
}
face_embedding, face_embedding_norm = calculate_face_embedding(vision_frame, face_landmark_set.get('5/68'))
gender, age, race = classify_face(vision_frame, face_landmark_set.get('5/68'))
faces.append(Face(
bounding_box = bounding_box,
score_set = face_score_set,
landmark_set = face_landmark_set,
angle = face_angle,
embedding = face_embedding,
embedding_norm = face_embedding_norm,
gender = gender,
age = age,
race = race
))
return faces
def get_one_face(faces : List[Face], position : int = 0) -> Optional[Face]:
if faces:
position = min(position, len(faces) - 1)
return faces[position]
return None
def get_average_face(faces : List[Face]) -> Optional[Face]:
face_embeddings = []
face_embeddings_norm = []
if faces:
first_face = get_first(faces)
for face in faces:
face_embeddings.append(face.embedding)
face_embeddings_norm.append(face.embedding_norm)
return Face(
bounding_box = first_face.bounding_box,
score_set = first_face.score_set,
landmark_set = first_face.landmark_set,
angle = first_face.angle,
embedding = numpy.mean(face_embeddings, axis = 0),
embedding_norm = numpy.mean(face_embeddings_norm, axis = 0),
gender = first_face.gender,
age = first_face.age,
race = first_face.race
)
return None
def get_many_faces(vision_frames : List[VisionFrame]) -> List[Face]:
many_faces : List[Face] = []
for vision_frame in vision_frames:
if numpy.any(vision_frame):
static_faces = get_static_faces(vision_frame)
if static_faces:
many_faces.extend(static_faces)
else:
all_bounding_boxes = []
all_face_scores = []
all_face_landmarks_5 = []
for face_detector_angle in state_manager.get_item('face_detector_angles'):
if face_detector_angle == 0:
bounding_boxes, face_scores, face_landmarks_5 = detect_faces(vision_frame)
else:
bounding_boxes, face_scores, face_landmarks_5 = detect_faces_by_angle(vision_frame, face_detector_angle)
all_bounding_boxes.extend(bounding_boxes)
all_face_scores.extend(face_scores)
all_face_landmarks_5.extend(face_landmarks_5)
if all_bounding_boxes and all_face_scores and all_face_landmarks_5 and state_manager.get_item('face_detector_score') > 0:
faces = create_faces(vision_frame, all_bounding_boxes, all_face_scores, all_face_landmarks_5)
if faces:
many_faces.extend(faces)
set_static_faces(vision_frame, 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
)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140from functools import lru_cache
from typing import List, Tuple
import numpy
from facefusion import inference_manager
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
from facefusion.face_helper import warp_face_by_face_landmark_5
from facefusion.filesystem import resolve_relative_path
from facefusion.thread_helper import conditional_thread_semaphore
from facefusion.types import Age, DownloadScope, FaceLandmark5, Gender, InferencePool, ModelOptions, ModelSet, Race, VisionFrame
@lru_cache()
def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
return\
{
'fairface':
{
'__metadata__':
{
'vendor': 'dchen236',
'license': 'Non-Commercial',
'year': 2021
},
'hashes':
{
'face_classifier':
{
'url': resolve_download_url('models-3.0.0', 'fairface.hash'),
'path': resolve_relative_path('../.assets/models/fairface.hash')
}
},
'sources':
{
'face_classifier':
{
'url': resolve_download_url('models-3.0.0', 'fairface.onnx'),
'path': resolve_relative_path('../.assets/models/fairface.onnx')
}
},
'template': 'arcface_112_v2',
'size': (224, 224),
'mean': [ 0.485, 0.456, 0.406 ],
'standard_deviation': [ 0.229, 0.224, 0.225 ]
}
}
def get_inference_pool() -> InferencePool:
model_names = [ 'fairface' ]
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 = [ 'fairface' ]
inference_manager.clear_inference_pool(__name__, model_names)
def get_model_options() -> ModelOptions:
return create_static_model_set('full').get('fairface')
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 classify_face(temp_vision_frame : VisionFrame, face_landmark_5 : FaceLandmark5) -> Tuple[Gender, Age, Race]:
model_template = get_model_options().get('template')
model_size = get_model_options().get('size')
model_mean = get_model_options().get('mean')
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 = crop_vision_frame.astype(numpy.float32)[:, :, ::-1] / 255.0
crop_vision_frame -= model_mean
crop_vision_frame /= model_standard_deviation
crop_vision_frame = crop_vision_frame.transpose(2, 0, 1)
crop_vision_frame = numpy.expand_dims(crop_vision_frame, axis = 0)
gender_id, age_id, race_id = forward(crop_vision_frame)
gender = categorize_gender(gender_id[0])
age = categorize_age(age_id[0])
race = categorize_race(race_id[0])
return gender, age, race
def forward(crop_vision_frame : VisionFrame) -> Tuple[List[int], List[int], List[int]]:
face_classifier = get_inference_pool().get('face_classifier')
with conditional_thread_semaphore():
race_id, gender_id, age_id = face_classifier.run(None,
{
'input': crop_vision_frame
})
return gender_id, age_id, race_id
def categorize_gender(gender_id : int) -> Gender:
if gender_id == 1:
return 'female'
return 'male'
def categorize_age(age_id : int) -> Age:
if age_id == 0:
return range(0, 2)
if age_id == 1:
return range(3, 9)
if age_id == 2:
return range(10, 19)
if age_id == 3:
return range(20, 29)
if age_id == 4:
return range(30, 39)
if age_id == 5:
return range(40, 49)
if age_id == 6:
return range(50, 59)
if age_id == 7:
return range(60, 69)
return range(70, 100)
def categorize_race(race_id : int) -> Race:
if race_id == 1:
return 'black'
if race_id == 2:
return 'latino'
if race_id == 3 or race_id == 4:
return 'asian'
if race_id == 5:
return 'indian'
if race_id == 6:
return 'arabic'
return 'white'123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458from functools import lru_cache
from typing import List, Sequence, Tuple
import cv2
import numpy
from facefusion import inference_manager, state_manager
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
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.filesystem import resolve_relative_path
from facefusion.thread_helper import thread_semaphore
from facefusion.types import Angle, BoundingBox, Detection, DownloadScope, DownloadSet, FaceLandmark5, InferencePool, Margin, ModelSet, Score, VisionFrame
from facefusion.vision import restrict_frame, unpack_resolution
@lru_cache()
def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
return\
{
'retinaface':
{
'__metadata__':
{
'vendor': 'InsightFace',
'license': 'Non-Commercial',
'year': 2020
},
'hashes':
{
'retinaface':
{
'url': resolve_download_url('models-3.0.0', 'retinaface_10g.hash'),
'path': resolve_relative_path('../.assets/models/retinaface_10g.hash')
}
},
'sources':
{
'retinaface':
{
'url': resolve_download_url('models-3.0.0', 'retinaface_10g.onnx'),
'path': resolve_relative_path('../.assets/models/retinaface_10g.onnx')
}
}
},
'scrfd':
{
'__metadata__':
{
'vendor': 'InsightFace',
'license': 'Non-Commercial',
'year': 2021
},
'hashes':
{
'scrfd':
{
'url': resolve_download_url('models-3.0.0', 'scrfd_2.5g.hash'),
'path': resolve_relative_path('../.assets/models/scrfd_2.5g.hash')
}
},
'sources':
{
'scrfd':
{
'url': resolve_download_url('models-3.0.0', 'scrfd_2.5g.onnx'),
'path': resolve_relative_path('../.assets/models/scrfd_2.5g.onnx')
}
}
},
'yolo_face':
{
'__metadata__':
{
'vendor': 'derronqi',
'license': 'GPL-3.0',
'year': 2022
},
'hashes':
{
'yolo_face':
{
'url': resolve_download_url('models-3.0.0', 'yoloface_8n.hash'),
'path': resolve_relative_path('../.assets/models/yoloface_8n.hash')
}
},
'sources':
{
'yolo_face':
{
'url': resolve_download_url('models-3.0.0', '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:
model_names = [ state_manager.get_item('face_detector_model') ]
_, model_source_set = collect_model_downloads()
return inference_manager.get_inference_pool(__name__, model_names, model_source_set)
def clear_inference_pool() -> None:
model_names = [ state_manager.get_item('face_detector_model') ]
inference_manager.clear_inference_pool(__name__, model_names)
def collect_model_downloads() -> Tuple[DownloadSet, DownloadSet]:
model_set = create_static_model_set('full')
model_hash_set = {}
model_source_set = {}
for face_detector_model in [ 'retinaface', 'scrfd', 'yolo_face', 'yunet' ]:
if state_manager.get_item('face_detector_model') in [ 'many', face_detector_model ]:
model_hash_set[face_detector_model] = model_set.get(face_detector_model).get('hashes').get(face_detector_model)
model_source_set[face_detector_model] = model_set.get(face_detector_model).get('sources').get(face_detector_model)
return model_hash_set, model_source_set
def pre_check() -> bool:
model_hash_set, model_source_set = collect_model_downloads()
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
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_face_scores : List[Score] = []
all_face_landmarks_5 : List[FaceLandmark5] = []
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'))
all_bounding_boxes.extend(bounding_boxes)
all_face_scores.extend(face_scores)
all_face_landmarks_5.extend(face_landmarks_5)
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'))
all_bounding_boxes.extend(bounding_boxes)
all_face_scores.extend(face_scores)
all_face_landmarks_5.extend(face_landmarks_5)
if state_manager.get_item('face_detector_model') in [ 'many', 'yolo_face' ]:
bounding_boxes, face_scores, face_landmarks_5 = detect_with_yolo_face(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)
if state_manager.get_item('face_detector_model') == 'yunet':
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
def prepare_margin(vision_frame : VisionFrame) -> Margin:
margin_top = int(vision_frame.shape[0] * numpy.interp(state_manager.get_item('face_detector_margin')[0], [ 0, 100 ], [ 0, 0.5 ]))
margin_right = int(vision_frame.shape[1] * numpy.interp(state_manager.get_item('face_detector_margin')[1], [ 0, 100 ], [ 0, 0.5 ]))
margin_bottom = int(vision_frame.shape[0] * numpy.interp(state_manager.get_item('face_detector_margin')[2], [ 0, 100 ], [ 0, 0.5 ]))
margin_left = int(vision_frame.shape[1] * numpy.interp(state_manager.get_item('face_detector_margin')[3], [ 0, 100 ], [ 0, 0.5 ]))
return margin_top, margin_right, margin_bottom, margin_left
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
def detect_with_retinaface(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 = 2
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, [ -1, 1 ])
detection = forward_with_retinaface(detect_vision_frame)
for index, feature_stride in enumerate(feature_strides):
face_scores_raw = detection[index]
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_raw = detection[index + feature_map_channel] * feature_stride
face_landmarks_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]:
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
]))
for face_score_raw in face_scores_raw[keep_indices]:
face_scores.append(face_score_raw[0])
for face_landmark_raw_5 in distance_to_face_landmark_5(anchors, 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
def detect_with_scrfd(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 = 2
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, [ -1, 1 ])
detection = forward_with_scrfd(detect_vision_frame)
for index, feature_stride in enumerate(feature_strides):
face_scores_raw = detection[index]
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_raw = detection[index + feature_map_channel] * feature_stride
face_landmarks_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]:
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
]))
for face_score_raw in face_scores_raw[keep_indices]:
face_scores.append(face_score_raw[0])
for face_landmark_raw_5 in distance_to_face_landmark_5(anchors, 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
def detect_with_yolo_face(vision_frame : VisionFrame, face_detector_size : str) -> Tuple[List[BoundingBox], List[Score], List[FaceLandmark5]]:
bounding_boxes = []
face_scores = []
face_landmarks_5 = []
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, 1 ])
detection = forward_with_yolo_face(detect_vision_frame)
detection = numpy.squeeze(detection).T
bounding_boxes_raw, face_scores_raw, face_landmarks_5_raw = numpy.split(detection, [ 4, 5 ], axis = 1)
keep_indices = numpy.where(face_scores_raw > face_detector_score)[0]
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]
for bounding_box_raw in bounding_boxes_raw:
bounding_boxes.append(numpy.array(
[
(bounding_box_raw[0] - bounding_box_raw[2] / 2) * ratio_width,
(bounding_box_raw[1] - bounding_box_raw[3] / 2) * ratio_height,
(bounding_box_raw[0] + bounding_box_raw[2] / 2) * ratio_width,
(bounding_box_raw[1] + bounding_box_raw[3] / 2) * ratio_height
]))
face_scores = face_scores_raw.ravel().tolist()
face_landmarks_5_raw[:, 0::3] = (face_landmarks_5_raw[:, 0::3]) * ratio_width
face_landmarks_5_raw[:, 1::3] = (face_landmarks_5_raw[:, 1::3]) * ratio_height
for face_landmark_raw_5 in face_landmarks_5_raw:
face_landmarks_5.append(numpy.array(face_landmark_raw_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
def forward_with_retinaface(detect_vision_frame : VisionFrame) -> Detection:
face_detector = get_inference_pool().get('retinaface')
with thread_semaphore():
detection = face_detector.run(None,
{
'input': detect_vision_frame
})
return detection
def forward_with_scrfd(detect_vision_frame : VisionFrame) -> Detection:
face_detector = get_inference_pool().get('scrfd')
with thread_semaphore():
detection = face_detector.run(None,
{
'input': detect_vision_frame
})
return detection
def forward_with_yolo_face(detect_vision_frame : VisionFrame) -> Detection:
face_detector = get_inference_pool().get('yolo_face')
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():
detection = face_detector.run(None,
{
'input': detect_vision_frame
})
return detection
def prepare_detect_frame(temp_vision_frame : VisionFrame, face_detector_size : str) -> VisionFrame:
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[:temp_vision_frame.shape[0], :temp_vision_frame.shape[1], :] = temp_vision_frame
detect_vision_frame = numpy.expand_dims(detect_vision_frame.transpose(2, 0, 1), axis = 0).astype(numpy.float32)
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_frame123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256from functools import lru_cache
from typing import List, Sequence, Tuple
import cv2
import numpy
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
WARP_TEMPLATE_SET : WarpTemplateSet =\
{
'arcface_112_v1': numpy.array(
[
[ 0.35473214, 0.45658929 ],
[ 0.64526786, 0.45658929 ],
[ 0.50000000, 0.61154464 ],
[ 0.37913393, 0.77687500 ],
[ 0.62086607, 0.77687500 ]
]),
'arcface_112_v2': numpy.array(
[
[ 0.34191607, 0.46157411 ],
[ 0.65653393, 0.45983393 ],
[ 0.50022500, 0.64050536 ],
[ 0.37097589, 0.82469196 ],
[ 0.63151696, 0.82325089 ]
]),
'arcface_128': numpy.array(
[
[ 0.36167656, 0.40387734 ],
[ 0.63696719, 0.40235469 ],
[ 0.50019687, 0.56044219 ],
[ 0.38710391, 0.72160547 ],
[ 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(
[
[ 0.37691676, 0.46864664 ],
[ 0.62285697, 0.46912813 ],
[ 0.50123859, 0.61331904 ],
[ 0.39308822, 0.72541100 ],
[ 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:
warp_template_norm = WARP_TEMPLATE_SET.get(warp_template) * crop_size
affine_matrix = cv2.estimateAffinePartial2D(face_landmark_5, warp_template_norm, method = cv2.RANSAC, ransacReprojThreshold = 100)[0]
return affine_matrix
def warp_face_by_face_landmark_5(temp_vision_frame : VisionFrame, face_landmark_5 : FaceLandmark5, warp_template : WarpTemplate, crop_size : Size) -> Tuple[VisionFrame, Matrix]:
affine_matrix = estimate_matrix_by_face_landmark_5(face_landmark_5, warp_template, crop_size)
crop_vision_frame = cv2.warpAffine(temp_vision_frame, affine_matrix, crop_size, borderMode = cv2.BORDER_REPLICATE, flags = cv2.INTER_AREA)
return crop_vision_frame, affine_matrix
def warp_face_by_bounding_box(temp_vision_frame : VisionFrame, bounding_box : BoundingBox, crop_size : Size) -> Tuple[VisionFrame, Matrix]:
source_points = numpy.array([ [ bounding_box[0], bounding_box[1] ], [bounding_box[2], bounding_box[1] ], [ bounding_box[0], bounding_box[3] ] ]).astype(numpy.float32)
target_points = numpy.array([ [ 0, 0 ], [ crop_size[0], 0 ], [ 0, crop_size[1] ] ]).astype(numpy.float32)
affine_matrix = cv2.getAffineTransform(source_points, target_points)
if bounding_box[2] - bounding_box[0] > crop_size[0] or bounding_box[3] - bounding_box[1] > crop_size[1]:
interpolation_method = cv2.INTER_AREA
else:
interpolation_method = cv2.INTER_LINEAR
crop_vision_frame = cv2.warpAffine(temp_vision_frame, affine_matrix, crop_size, flags = interpolation_method)
return crop_vision_frame, affine_matrix
def warp_face_by_translation(temp_vision_frame : VisionFrame, translation : Translation, scale : float, crop_size : Size) -> Tuple[VisionFrame, Matrix]:
affine_matrix = numpy.array([ [ scale, 0, translation[0] ], [ 0, scale, translation[1] ] ])
crop_vision_frame = cv2.warpAffine(temp_vision_frame, affine_matrix, crop_size)
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:
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)
crop_points = numpy.array([ [ 0, 0 ], [ crop_width, 0 ], [ crop_width, crop_height ], [ 0, crop_height ] ])
paste_region_points = transform_points(crop_points, inverse_matrix)
paste_region_point_min = numpy.floor(paste_region_points.min(axis = 0)).astype(int)
paste_region_point_max = numpy.ceil(paste_region_points.max(axis = 0)).astype(int)
x1, y1 = numpy.clip(paste_region_point_min, 0, [ temp_width, temp_height ])
x2, y2 = numpy.clip(paste_region_point_max, 0, [ temp_width, temp_height ])
paste_bounding_box = numpy.array([ x1, y1, x2, y2 ])
paste_matrix = inverse_matrix.copy()
paste_matrix[0, 2] -= x1
paste_matrix[1, 2] -= y1
return paste_bounding_box, paste_matrix
@lru_cache()
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]
anchors = numpy.stack((y, x), axis = -1)
anchors = (anchors * feature_stride).reshape((-1, 2))
anchors = numpy.stack([ anchors ] * anchor_total, axis = 1).reshape((-1, 2))
return anchors
def create_rotation_matrix_and_size(angle : Angle, size : Size) -> Tuple[Matrix, Size]:
rotation_matrix = cv2.getRotationMatrix2D((size[0] / 2, size[1] / 2), angle, 1)
rotation_size = numpy.dot(numpy.abs(rotation_matrix[:, :2]), size)
rotation_matrix[:, -1] += (rotation_size - size) * 0.5 #type:ignore[misc]
rotation_size = int(rotation_size[0]), int(rotation_size[1])
return rotation_matrix, rotation_size
def create_bounding_box(face_landmark_68 : FaceLandmark68) -> BoundingBox:
x1, y1 = numpy.min(face_landmark_68, axis = 0)
x2, y2 = numpy.max(face_landmark_68, axis = 0)
bounding_box = normalize_bounding_box(numpy.array([ x1, y1, x2, y2 ]))
return bounding_box
def normalize_bounding_box(bounding_box : BoundingBox) -> BoundingBox:
x1, y1, x2, y2 = bounding_box
x1, x2 = sorted([ x1, x2 ])
y1, y2 = sorted([ y1, y2 ])
return numpy.array([ x1, y1, x2, y2 ])
def transform_points(points : Points, matrix : Matrix) -> Points:
points = points.reshape(-1, 1, 2)
points = cv2.transform(points, matrix) #type:ignore[assignment]
points = points.reshape(-1, 2)
return points
def transform_bounding_box(bounding_box : BoundingBox, matrix : Matrix) -> BoundingBox:
points = numpy.array(
[
[ bounding_box[0], bounding_box[1] ],
[ bounding_box[2], bounding_box[1] ],
[ bounding_box[2], bounding_box[3] ],
[ bounding_box[0], bounding_box[3] ]
])
points = transform_points(points, matrix)
x1, y1 = numpy.min(points, axis = 0)
x2, y2 = numpy.max(points, axis = 0)
return normalize_bounding_box(numpy.array([ x1, y1, x2, y2 ]))
def distance_to_bounding_box(points : Points, distance : Distance) -> BoundingBox:
x1 = points[:, 0] - distance[:, 0]
y1 = points[:, 1] - distance[:, 1]
x2 = points[:, 0] + distance[:, 2]
y2 = points[:, 1] + distance[:, 3]
bounding_box = numpy.column_stack([ x1, y1, x2, y2 ])
return bounding_box
def distance_to_face_landmark_5(points : Points, distance : Distance) -> FaceLandmark5:
x = points[:, 0::2] + distance[:, 0::2]
y = points[:, 1::2] + distance[:, 1::2]
face_landmark_5 = numpy.stack((x, y), axis = -1)
return face_landmark_5
def scale_face_landmark_5(face_landmark_5 : FaceLandmark5, scale : Scale) -> FaceLandmark5:
face_landmark_5_scale = face_landmark_5 - face_landmark_5[2]
face_landmark_5_scale *= scale
face_landmark_5_scale += face_landmark_5[2]
return face_landmark_5_scale
def convert_to_face_landmark_5(face_landmark_68 : FaceLandmark68) -> FaceLandmark5:
face_landmark_5 = numpy.array(
[
numpy.mean(face_landmark_68[36:42], axis = 0),
numpy.mean(face_landmark_68[42:48], axis = 0),
face_landmark_68[30],
face_landmark_68[48],
face_landmark_68[54]
])
return face_landmark_5
def estimate_face_angle(face_landmark_68 : FaceLandmark68) -> Angle:
x1, y1 = face_landmark_68[0]
x2, y2 = face_landmark_68[16]
theta = numpy.arctan2(y2 - y1, x2 - x1)
theta = numpy.degrees(theta) % 360
angles = numpy.linspace(0, 360, 5)
index = numpy.argmin(numpy.abs(angles - theta))
face_angle = int(angles[index] % 360)
return face_angle
def apply_nms(bounding_boxes : List[BoundingBox], 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 ]
keep_indices = cv2.dnn.NMSBoxes(bounding_boxes_norm, scores, score_threshold = score_threshold, nms_threshold = nms_threshold)
return keep_indices
def get_nms_threshold(face_detector_model : FaceDetectorModel, face_detector_angles : List[Angle]) -> float:
if face_detector_model == 'many':
return 0.1
if len(face_detector_angles) == 2:
return 0.3
if len(face_detector_angles) == 3:
return 0.2
if len(face_detector_angles) == 4:
return 0.1
return 0.4
def merge_matrix(temp_matrices : List[Matrix]) -> Matrix:
matrix = numpy.vstack([temp_matrices[0], [0, 0, 1]])
for temp_matrix in temp_matrices[1:]:
temp_matrix = numpy.vstack([ temp_matrix, [ 0, 0, 1 ] ])
matrix = numpy.dot(temp_matrix, matrix)
return matrix[:2, :]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242from functools import lru_cachefrom typing import Tuple
import cv2import numpy
from facefusion import inference_manager, state_managerfrom facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_urlfrom facefusion.face_helper import create_rotation_matrix_and_size, estimate_matrix_by_face_landmark_5, transform_points, warp_face_by_translationfrom facefusion.filesystem import resolve_relative_pathfrom facefusion.thread_helper import conditional_thread_semaphorefrom facefusion.types import Angle, BoundingBox, DownloadScope, DownloadSet, FaceLandmark5, FaceLandmark68, InferencePool, ModelSet, Prediction, Score, VisionFrame
@lru_cache()def create_static_model_set(download_scope : DownloadScope) -> ModelSet: return\ { '2dfan4': { '__metadata__': { 'vendor': 'breadbread1984', 'license': 'MIT', 'year': 2018 }, 'hashes': { '2dfan4': { 'url': resolve_download_url('models-3.0.0', '2dfan4.hash'), 'path': resolve_relative_path('../.assets/models/2dfan4.hash') } }, 'sources': { '2dfan4': { 'url': resolve_download_url('models-3.0.0', '2dfan4.onnx'), 'path': resolve_relative_path('../.assets/models/2dfan4.onnx') } }, 'size': (256, 256) }, 'peppa_wutz': { '__metadata__': { 'vendor': 'Unknown', 'license': 'Apache-2.0', 'year': 2023 }, 'hashes': { 'peppa_wutz': { 'url': resolve_download_url('models-3.0.0', 'peppa_wutz.hash'), 'path': resolve_relative_path('../.assets/models/peppa_wutz.hash') } }, 'sources': { 'peppa_wutz': { 'url': resolve_download_url('models-3.0.0', 'peppa_wutz.onnx'), 'path': resolve_relative_path('../.assets/models/peppa_wutz.onnx') } }, 'size': (256, 256) }, 'fan_68_5': { '__metadata__': { 'vendor': 'FaceFusion', 'license': 'OpenRAIL-M', 'year': 2024 }, 'hashes': { 'fan_68_5': { 'url': resolve_download_url('models-3.0.0', 'fan_68_5.hash'), 'path': resolve_relative_path('../.assets/models/fan_68_5.hash') } }, 'sources': { 'fan_68_5': { 'url': resolve_download_url('models-3.0.0', 'fan_68_5.onnx'), 'path': resolve_relative_path('../.assets/models/fan_68_5.onnx') } } } }
def get_inference_pool() -> InferencePool: model_names = [ state_manager.get_item('face_landmarker_model'), 'fan_68_5' ] _, model_source_set = collect_model_downloads()
return inference_manager.get_inference_pool(__name__, model_names, model_source_set)
def clear_inference_pool() -> None: model_names = [ state_manager.get_item('face_landmarker_model'), 'fan_68_5' ] inference_manager.clear_inference_pool(__name__, model_names)
def collect_model_downloads() -> Tuple[DownloadSet, DownloadSet]: model_set = create_static_model_set('full') model_hash_set =\ { 'fan_68_5': model_set.get('fan_68_5').get('hashes').get('fan_68_5') } model_source_set =\ { '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', face_landmarker_model ]: model_hash_set[face_landmarker_model] = model_set.get(face_landmarker_model).get('hashes').get(face_landmarker_model) model_source_set[face_landmarker_model] = model_set.get(face_landmarker_model).get('sources').get(face_landmarker_model)
return model_hash_set, model_source_set
def pre_check() -> bool: model_hash_set, model_source_set = collect_model_downloads()
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
def detect_face_landmark(vision_frame : VisionFrame, bounding_box : BoundingBox, face_angle : Angle) -> Tuple[FaceLandmark68, Score]: face_landmark_2dfan4 = None face_landmark_peppa_wutz = None face_landmark_score_2dfan4 = 0.0 face_landmark_score_peppa_wutz = 0.0
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)
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)
if face_landmark_score_2dfan4 > face_landmark_score_peppa_wutz - 0.2: return face_landmark_2dfan4, face_landmark_score_2dfan4 return face_landmark_peppa_wutz, face_landmark_score_peppa_wutz
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') 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 rotation_matrix, rotation_size = create_rotation_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 = cv2.warpAffine(crop_vision_frame, rotation_matrix, rotation_size) crop_vision_frame = conditional_optimize_contrast(crop_vision_frame) 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_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(affine_matrix)) 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.interp(face_landmark_score_68, [ 0, 0.9 ], [ 0, 1 ]) return face_landmark_68, face_landmark_score_68
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') 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 rotation_matrix, rotation_size = create_rotation_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 = cv2.warpAffine(crop_vision_frame, rotation_matrix, rotation_size) 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 = numpy.expand_dims(crop_vision_frame, axis = 0) prediction = forward_with_peppa_wutz(crop_vision_frame) 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(affine_matrix)) 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 ]) return face_landmark_68, face_landmark_score_68
def conditional_optimize_contrast(crop_vision_frame : VisionFrame) -> VisionFrame: if crop_vision_frame.dtype == numpy.uint16: crop_vision_frame = (crop_vision_frame / 256).astype(numpy.uint8) crop_vision_frame = cv2.cvtColor(crop_vision_frame, cv2.COLOR_RGB2Lab) 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 = cv2.cvtColor(crop_vision_frame, cv2.COLOR_Lab2RGB) return crop_vision_frame
def estimate_face_landmark_68_5(face_landmark_5 : FaceLandmark5) -> FaceLandmark68: affine_matrix = estimate_matrix_by_face_landmark_5(face_landmark_5, 'ffhq_512', (1, 1)) face_landmark_5 = cv2.transform(face_landmark_5.reshape(1, -1, 2), affine_matrix).reshape(-1, 2) face_landmark_68_5 = forward_fan_68_5(face_landmark_5) face_landmark_68_5 = cv2.transform(face_landmark_68_5.reshape(1, -1, 2), cv2.invertAffineTransform(affine_matrix)).reshape(-1, 2) return face_landmark_68_5
def forward_with_2dfan4(crop_vision_frame : VisionFrame) -> Tuple[Prediction, Prediction]: face_landmarker = get_inference_pool().get('2dfan4')
with conditional_thread_semaphore(): prediction = face_landmarker.run(None, { 'input': [ crop_vision_frame ] })
return prediction
def forward_with_peppa_wutz(crop_vision_frame : VisionFrame) -> Prediction: face_landmarker = get_inference_pool().get('peppa_wutz')
with conditional_thread_semaphore(): prediction = face_landmarker.run(None, { 'input': crop_vision_frame })[0]
return prediction
def forward_fan_68_5(face_landmark_5 : FaceLandmark5) -> FaceLandmark68: face_landmarker = get_inference_pool().get('fan_68_5')
with conditional_thread_semaphore(): face_landmark_68_5 = face_landmarker.run(None, { 'input': [ face_landmark_5 ] })[0][0]
return face_landmark_68_5123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279from functools import lru_cache
from typing import List, Tuple
import cv2
import numpy
import facefusion.choices
from facefusion import inference_manager, state_manager
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
from facefusion.filesystem import resolve_relative_path
from facefusion.thread_helper import conditional_thread_semaphore
from facefusion.types import DownloadScope, DownloadSet, FaceLandmark68, FaceMaskArea, FaceMaskRegion, InferencePool, Mask, ModelSet, Padding, VisionFrame
@lru_cache()
def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
return\
{
'xseg_1':
{
'__metadata__':
{
'vendor': 'DeepFaceLab',
'license': 'GPL-3.0',
'year': 2021
},
'hashes':
{
'face_occluder':
{
'url': resolve_download_url('models-3.1.0', 'xseg_1.hash'),
'path': resolve_relative_path('../.assets/models/xseg_1.hash')
}
},
'sources':
{
'face_occluder':
{
'url': resolve_download_url('models-3.1.0', 'xseg_1.onnx'),
'path': resolve_relative_path('../.assets/models/xseg_1.onnx')
}
},
'size': (256, 256)
},
'xseg_2':
{
'__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':
{
'face_parser':
{
'url': resolve_download_url('models-3.1.0', 'bisenet_resnet_18.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')
}
},
'sources':
{
'face_parser':
{
'url': resolve_download_url('models-3.0.0', 'bisenet_resnet_34.onnx'),
'path': resolve_relative_path('../.assets/models/bisenet_resnet_34.onnx')
}
},
'size': (512, 512)
}
}
def get_inference_pool() -> InferencePool:
model_names = [ state_manager.get_item('face_occluder_model'), state_manager.get_item('face_parser_model') ]
_, model_source_set = collect_model_downloads()
return inference_manager.get_inference_pool(__name__, model_names, model_source_set)
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__, model_names)
def collect_model_downloads() -> Tuple[DownloadSet, DownloadSet]:
model_set = create_static_model_set('full')
model_hash_set = {}
model_source_set = {}
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_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')
for face_parser_model in [ 'bisenet_resnet_18', 'bisenet_resnet_34' ]:
if state_manager.get_item('face_parser_model') == face_parser_model:
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:
model_hash_set, model_source_set = collect_model_downloads()
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
def create_box_mask(crop_vision_frame : VisionFrame, face_mask_blur : float, face_mask_padding : Padding) -> Mask:
crop_size = crop_vision_frame.shape[:2][::-1]
blur_amount = int(crop_size[0] * 0.5 * face_mask_blur)
blur_area = max(blur_amount // 2, 1)
box_mask : Mask = numpy.ones(crop_size).astype(numpy.float32)
box_mask[:max(blur_area, int(crop_size[1] * face_mask_padding[0] / 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[1] / 100)):] = 0
if blur_amount > 0:
box_mask = cv2.GaussianBlur(box_mask, (0, 0), blur_amount * 0.25)
return box_mask
def create_occlusion_mask(crop_vision_frame : VisionFrame) -> Mask:
temp_masks = []
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 = numpy.expand_dims(prepare_vision_frame, axis = 0).astype(numpy.float32) / 255.0
prepare_vision_frame = prepare_vision_frame.transpose(0, 1, 2, 3)
temp_mask = forward_occlude_face(prepare_vision_frame, model_name)
temp_mask = temp_mask.transpose(0, 1, 2).clip(0, 1).astype(numpy.float32)
temp_mask = cv2.resize(temp_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
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:
model_name = state_manager.get_item('face_parser_model')
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 = prepare_vision_frame[:, :, ::-1].astype(numpy.float32) / 255.0
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.expand_dims(prepare_vision_frame, axis = 0)
prepare_vision_frame = prepare_vision_frame.transpose(0, 3, 1, 2)
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 = 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
return region_mask
def forward_occlude_face(prepare_vision_frame : VisionFrame, model_name : str) -> Mask:
face_occluder = get_inference_pool().get(model_name)
with conditional_thread_semaphore():
occlusion_mask : Mask = face_occluder.run(None,
{
'input': prepare_vision_frame
})[0][0]
return occlusion_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(model_name)
with conditional_thread_semaphore():
region_mask : Mask = face_parser.run(None,
{
'input': prepare_vision_frame
})[0][0]
return region_mask123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293from functools import lru_cache
from typing import Tuple
import numpy
from facefusion import inference_manager
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
from facefusion.face_helper import warp_face_by_face_landmark_5
from facefusion.filesystem import resolve_relative_path
from facefusion.thread_helper import conditional_thread_semaphore
from facefusion.types import DownloadScope, Embedding, FaceLandmark5, InferencePool, ModelOptions, ModelSet, VisionFrame
@lru_cache()
def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
return\
{
'arcface':
{
'__metadata__':
{
'vendor': 'InsightFace',
'license': 'Non-Commercial',
'year': 2018
},
'hashes':
{
'face_recognizer':
{
'url': resolve_download_url('models-3.0.0', 'arcface_w600k_r50.hash'),
'path': resolve_relative_path('../.assets/models/arcface_w600k_r50.hash')
}
},
'sources':
{
'face_recognizer':
{
'url': resolve_download_url('models-3.0.0', 'arcface_w600k_r50.onnx'),
'path': resolve_relative_path('../.assets/models/arcface_w600k_r50.onnx')
}
},
'template': 'arcface_112_v2',
'size': (112, 112)
}
}
def get_inference_pool() -> InferencePool:
model_names = [ 'arcface' ]
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 = [ 'arcface' ]
inference_manager.clear_inference_pool(__name__, model_names)
def get_model_options() -> ModelOptions:
return create_static_model_set('full').get('arcface')
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 calculate_face_embedding(temp_vision_frame : VisionFrame, face_landmark_5 : FaceLandmark5) -> Tuple[Embedding, Embedding]:
model_template = get_model_options().get('template')
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 = crop_vision_frame / 127.5 - 1
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)
face_embedding = forward(crop_vision_frame)
face_embedding = face_embedding.ravel()
face_embedding_norm = face_embedding / numpy.linalg.norm(face_embedding)
return face_embedding, face_embedding_norm
def forward(crop_vision_frame : VisionFrame) -> Embedding:
face_recognizer = get_inference_pool().get('face_recognizer')
with conditional_thread_semaphore():
face_embedding = face_recognizer.run(None,
{
'input': crop_vision_frame
})[0]
return face_embedding123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130from typing import List
import numpy
from facefusion import state_manager
from facefusion.face_analyser import get_many_faces, get_one_face
from facefusion.types import Face, FaceSelectorOrder, Gender, Race, Score, VisionFrame
def select_faces(reference_vision_frame : VisionFrame, target_vision_frame : VisionFrame) -> List[Face]:
target_faces = get_many_faces([ target_vision_frame ])
if state_manager.get_item('face_selector_mode') == 'many':
return sort_and_filter_faces(target_faces)
if state_manager.get_item('face_selector_mode') == 'one':
target_face = get_one_face(sort_and_filter_faces(target_faces))
if target_face:
return [ target_face ]
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:
current_face_distance = calculate_face_distance(face, reference_face)
current_face_distance = float(numpy.interp(current_face_distance, [ 0, 2 ], [ 0, 1 ]))
return current_face_distance < face_distance
def calculate_face_distance(face : Face, reference_face : Face) -> float:
if hasattr(face, 'embedding_norm') and hasattr(reference_face, 'embedding_norm'):
return 1 - numpy.dot(face.embedding_norm, reference_face.embedding_norm)
return 0
def sort_and_filter_faces(faces : List[Face]) -> List[Face]:
if faces:
if state_manager.get_item('face_selector_order'):
faces = sort_faces_by_order(faces, state_manager.get_item('face_selector_order'))
if state_manager.get_item('face_selector_gender'):
faces = filter_faces_by_gender(faces, state_manager.get_item('face_selector_gender'))
if state_manager.get_item('face_selector_race'):
faces = filter_faces_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'):
faces = filter_faces_by_age(faces, state_manager.get_item('face_selector_age_start'), state_manager.get_item('face_selector_age_end'))
return faces
def sort_faces_by_order(faces : List[Face], order : FaceSelectorOrder) -> List[Face]:
if order == 'left-right':
return sorted(faces, key = get_bounding_box_left)
if order == 'right-left':
return sorted(faces, key = get_bounding_box_left, reverse = True)
if order == 'top-bottom':
return sorted(faces, key = get_bounding_box_top)
if order == 'bottom-top':
return sorted(faces, key = get_bounding_box_top, reverse = True)
if order == 'small-large':
return sorted(faces, key = get_bounding_box_area)
if order == 'large-small':
return sorted(faces, key = get_bounding_box_area, reverse = True)
if order == 'best-worst':
return sorted(faces, key = get_face_detector_score, reverse = True)
if order == 'worst-best':
return sorted(faces, key = get_face_detector_score)
return faces
def get_bounding_box_left(face : Face) -> float:
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 = []
for face in faces:
if face.gender == gender:
filter_faces.append(face)
return filter_faces
def filter_faces_by_age(faces : List[Face], face_selector_age_start : int, face_selector_age_end : int) -> List[Face]:
filter_faces = []
age = range(face_selector_age_start, face_selector_age_end)
for face in faces:
if set(face.age) & set(age):
filter_faces.append(face)
return filter_faces
def filter_faces_by_race(faces : List[Face], race : Race) -> List[Face]:
filter_faces = []
for face in faces:
if face.race == race:
filter_faces.append(face)
return filter_faces12345678910111213141516171819202122232425262728from typing import List, Optional
from facefusion.hash_helper import create_hash
from facefusion.types import Face, FaceStore, VisionFrame
FACE_STORE : FaceStore =\
{
'static_faces': {}
}
def get_face_store() -> FaceStore:
return FACE_STORE
def get_static_faces(vision_frame : VisionFrame) -> Optional[List[Face]]:
vision_hash = create_hash(vision_frame.tobytes())
return FACE_STORE.get('static_faces').get(vision_hash)
def set_static_faces(vision_frame : VisionFrame, faces : List[Face]) -> None:
vision_hash = create_hash(vision_frame.tobytes())
if vision_hash:
FACE_STORE['static_faces'][vision_hash] = faces
def clear_static_faces() -> None:
FACE_STORE['static_faces'].clear()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291import os
import subprocess
import tempfile
from functools import partial
from typing import List, Optional, cast
from tqdm import tqdm
import facefusion.choices
from facefusion import ffmpeg_builder, logger, process_manager, state_manager, translator
from facefusion.filesystem import get_file_format, remove_file
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.vision import detect_video_duration, detect_video_fps, pack_resolution, predict_video_frame_total
def run_ffmpeg_with_progress(commands : List[Command], update_progress : UpdateProgress) -> subprocess.Popen[bytes]:
log_level = state_manager.get_item('log_level')
commands.extend(ffmpeg_builder.set_progress())
commands.extend(ffmpeg_builder.cast_stream())
commands = ffmpeg_builder.run(commands)
process = subprocess.Popen(commands, stderr = subprocess.PIPE, stdout = subprocess.PIPE)
while process_manager.is_processing():
try:
while __line__ := process.stdout.readline().decode().lower():
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)
process.wait(timeout = 0.5)
except subprocess.TimeoutExpired:
continue
return process
if process_manager.is_stopping():
process.terminate()
return process
def open_ffmpeg(commands : List[Command]) -> subprocess.Popen[bytes]:
commands = ffmpeg_builder.run(commands)
return subprocess.Popen(commands, stdin = subprocess.PIPE, stdout = subprocess.PIPE)
def log_debug(process : subprocess.Popen[bytes]) -> None:
_, stderr = process.communicate()
errors = stderr.decode().split(os.linesep)
for error in errors:
if error.strip():
logger.debug(error.strip(), __name__)
def get_available_encoder_set() -> EncoderSet:
available_encoder_set : EncoderSet =\
{
'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')
commands = ffmpeg_builder.chain(
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:
process = run_ffmpeg_with_progress(commands, partial(update_progress, progress))
return process.returncode == 0
def copy_image(target_path : str, temp_image_resolution : Resolution) -> bool:
temp_image_path = get_temp_file_path(target_path)
commands = ffmpeg_builder.chain(
ffmpeg_builder.set_input(target_path),
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
def finalize_image(target_path : str, output_path : str, output_image_resolution : Resolution) -> bool:
output_image_quality = state_manager.get_item('output_image_quality')
temp_image_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')
output_video_encoder = fix_video_encoder(temp_video_format, output_video_encoder)
commands = ffmpeg_builder.chain(
ffmpeg_builder.set_input_fps(temp_video_fps),
ffmpeg_builder.set_input(temp_frames_pattern),
ffmpeg_builder.set_media_resolution(pack_resolution(output_video_resolution)),
ffmpeg_builder.set_video_encoder(output_video_encoder),
ffmpeg_builder.set_video_quality(output_video_encoder, output_video_quality),
ffmpeg_builder.set_video_preset(output_video_encoder, output_video_preset),
ffmpeg_builder.concat(
ffmpeg_builder.set_video_fps(output_video_fps),
ffmpeg_builder.keep_video_alpha(output_video_encoder)
),
ffmpeg_builder.set_pixel_format(output_video_encoder),
ffmpeg_builder.force_output(temp_video_path)
)
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:
concat_video_path = tempfile.mktemp()
with open(concat_video_path, 'w') as concat_video_file:
for temp_output_path in temp_output_paths:
concat_video_file.write('file \'' + os.path.abspath(temp_output_path) + '\'' + os.linesep)
concat_video_file.flush()
concat_video_file.close()
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.communicate()
remove_file(concat_video_path)
return process.returncode == 0
def fix_audio_encoder(video_format : VideoFormat, audio_encoder : AudioEncoder) -> AudioEncoder:
if video_format == 'avi' and audio_encoder == 'libopus':
return 'aac'
if video_format in [ 'm4v', 'mpeg', 'wmv' ]:
return 'aac'
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:
if video_format in [ 'm4v', 'mpeg', 'mxf', 'wmv' ]:
return 'libx264'
if video_format in [ 'mkv', 'mp4' ] and video_encoder == 'rawvideo':
return 'libx264'
if video_format == 'mov' and video_encoder == 'libvpx-vp9':
return 'libx264'
if video_format == 'webm':
return 'libvpx-vp9'
return video_encoder123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267import 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 None123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190import glob
import os
import shutil
from typing import List, Optional
import facefusion.choices
def get_file_size(file_path : str) -> int:
if is_file(file_path):
return os.path.getsize(file_path)
return 0
def get_file_name(file_path : str) -> Optional[str]:
file_name, _ = os.path.splitext(os.path.basename(file_path))
if file_name:
return file_name
return None
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
def is_file(file_path : str) -> bool:
if file_path:
return os.path.isfile(file_path)
return False
def is_audio(audio_path : str) -> bool:
return is_file(audio_path) and get_file_format(audio_path) in facefusion.choices.audio_formats
def has_audio(audio_paths : List[str]) -> bool:
if audio_paths:
return any(map(is_audio, audio_paths))
return False
def are_audios(audio_paths : List[str]) -> bool:
if audio_paths:
return all(map(is_audio, audio_paths))
return False
def is_image(image_path : str) -> bool:
return is_file(image_path) and get_file_format(image_path) in facefusion.choices.image_formats
def has_image(image_paths : List[str]) -> bool:
if image_paths:
return any(is_image(image_path) for image_path in image_paths)
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:
return is_file(video_path) and get_file_format(video_path) in facefusion.choices.video_formats
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]:
if paths:
return [ path for path in paths if is_audio(path) ]
return []
def filter_image_paths(paths : List[str]) -> List[str]:
if paths:
return [ path for path in paths if is_image(path) ]
return []
def copy_file(file_path : str, move_path : str) -> bool:
if is_file(file_path):
shutil.copy(file_path, move_path)
return is_file(move_path)
return False
def move_file(file_path : str, move_path : str) -> bool:
if is_file(file_path):
shutil.move(file_path, move_path)
return not is_file(file_path) and is_file(move_path)
return False
def remove_file(file_path : str) -> bool:
if is_file(file_path):
os.remove(file_path)
return not is_file(file_path)
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:
if directory_path and not is_file(directory_path):
os.makedirs(directory_path, exist_ok = True)
return is_directory(directory_path)
return False
def remove_directory(directory_path : str) -> bool:
if is_directory(directory_path):
shutil.rmtree(directory_path, ignore_errors = True)
return not is_directory(directory_path)
return False
def resolve_relative_path(path : str) -> str:
return os.path.abspath(os.path.join(os.path.dirname(__file__), path))1234567891011121314151617181920212223242526272829303132import os
import zlib
from typing import Optional
from facefusion.filesystem import get_file_name, is_file
def create_hash(content : bytes) -> str:
return format(zlib.crc32(content), '08x')
def validate_hash(validate_path : str) -> bool:
hash_path = get_hash_path(validate_path)
if is_file(hash_path):
with open(hash_path) as hash_file:
hash_content = hash_file.read()
with open(validate_path, 'rb') as validate_file:
validate_content = validate_file.read()
return create_hash(validate_content) == hash_content
return False
def get_hash_path(validate_path : str) -> Optional[str]:
if is_file(validate_path):
validate_directory_path, file_name_and_extension = os.path.split(validate_path)
validate_file_name = get_file_name(file_name_and_extension)
return os.path.join(validate_directory_path, validate_file_name + '.hash')
return None123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596import importlibimport randomfrom time import sleep, timefrom typing import List
from onnxruntime import InferenceSession
from facefusion import logger, process_manager, state_manager, translatorfrom facefusion.app_context import detect_app_contextfrom facefusion.common_helper import is_windowsfrom facefusion.execution import create_inference_session_providers, has_execution_providerfrom facefusion.exit_helper import fatal_exitfrom facefusion.filesystem import get_file_name, is_filefrom facefusion.time_helper import calculate_end_timefrom facefusion.types import DownloadSet, ExecutionProvider, InferencePool, InferencePoolSet
INFERENCE_POOL_SET : InferencePoolSet =\{ 'cli': {}, 'ui': {}}
def get_inference_pool(module_name : str, model_names : List[str], model_source_set : DownloadSet) -> InferencePool: while process_manager.is_checking(): 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()
for execution_device_id in execution_device_ids: inference_context = get_inference_context(module_name, model_names, execution_device_id, execution_providers)
if app_context == 'cli' and INFERENCE_POOL_SET.get('ui').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: inference_pool : InferencePool = {}
for model_name in model_source_set.keys(): model_path = model_source_set.get(model_name).get('path') if is_file(model_path): inference_pool[model_name] = create_inference_session(model_path, execution_device_id, execution_providers)
return inference_pool
def clear_inference_pool(module_name : str, model_names : List[str]) -> None: execution_device_ids = state_manager.get_item('execution_device_ids') execution_providers = resolve_execution_providers(module_name) app_context = detect_app_context()
if is_windows() and has_execution_provider('directml'): INFERENCE_POOL_SET[app_context].clear()
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: model_file_name = get_file_name(model_path) start_time = time()
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: valid_model_names = [name for name in model_names if name] inference_context = '.'.join([ module_name ] + valid_model_names + [ str(execution_device_id) ] + list(execution_providers)) return inference_context
def resolve_execution_providers(module_name : str) -> List[ExecutionProvider]: module = importlib.import_module(module_name)
if hasattr(module, 'resolve_execution_providers'): return getattr(module, 'resolve_execution_providers')() return state_manager.get_item('execution_providers')123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109import os
import shutil
import signal
import subprocess
import sys
from argparse import ArgumentParser, HelpFormatter
from functools import partial
from types import FrameType
from facefusion import metadata
from facefusion.common_helper import is_linux, is_windows
LOCALES =\
{
'install_dependency': 'install the {dependency} package',
'force_reinstall': 'force reinstall of packages',
'skip_conda': 'skip the conda environment check',
'conda_not_activated': 'conda is not activated'
}
ONNXRUNTIME_SET =\
{
'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():
ONNXRUNTIME_SET['migraphx'] = ('onnxruntime-migraphx', '1.23.0')
ONNXRUNTIME_SET['rocm'] = ('onnxruntime_rocm', '1.22.1', '7.0.2') #type:ignore[assignment]
def cli() -> None:
signal.signal(signal.SIGINT, signal_exit)
program = ArgumentParser(formatter_class = partial(HelpFormatter, max_help_position = 50))
program.add_argument('--onnxruntime', help = LOCALES.get('install_dependency').format(dependency = 'onnxruntime'), choices = ONNXRUNTIME_SET.keys(), required = True)
program.add_argument('--force-reinstall', help = LOCALES.get('force_reinstall'), action = 'store_true')
program.add_argument('--skip-conda', help = LOCALES.get('skip_conda'), action = 'store_true')
program.add_argument('-v', '--version', version = metadata.get('name') + ' ' + metadata.get('version'), action = 'version')
run(program)
def signal_exit(signum : int, frame : FrameType) -> None:
sys.exit(0)
def run(program : ArgumentParser) -> None:
args = program.parse_args()
has_conda = 'CONDA_PREFIX' in os.environ
commands = [ shutil.which('pip'), 'install' ]
if args.force_reinstall:
commands.append('--force-reinstall')
if not args.skip_conda and not has_conda:
sys.stdout.write(LOCALES.get('conda_not_activated') + os.linesep)
sys.exit(1)
with open('requirements.txt') as file:
for line in file.readlines():
__line__ = line.strip()
if not __line__.startswith('onnxruntime'):
commands.append(__line__)
if args.onnxruntime == 'rocm':
onnxruntime_name, onnxruntime_version, rocm_version = ONNXRUNTIME_SET.get(args.onnxruntime) #type:ignore[misc]
python_id = 'cp' + str(sys.version_info.major) + str(sys.version_info.minor)
if python_id in [ 'cp310', 'cp312' ]:
wheel_name = onnxruntime_name + '-' + onnxruntime_version + '-' + python_id + '-' + python_id + '-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl'
wheel_url = 'https://repo.radeon.com/rocm/manylinux/rocm-rel-' + rocm_version + '/' + wheel_name
commands.append(wheel_url)
else:
onnxruntime_name, onnxruntime_version = ONNXRUNTIME_SET.get(args.onnxruntime)
commands.append(onnxruntime_name + '==' + onnxruntime_version)
subprocess.call(commands)
if args.onnxruntime == 'cuda' and has_conda:
library_paths = []
if is_linux():
if os.getenv('LD_LIBRARY_PATH'):
library_paths = os.getenv('LD_LIBRARY_PATH').split(os.pathsep)
python_id = 'python' + str(sys.version_info.major) + '.' + str(sys.version_info.minor)
library_paths.extend(
[
os.path.join(os.getenv('CONDA_PREFIX'), 'lib'),
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) ]))
subprocess.call([ shutil.which('conda'), 'env', 'config', 'vars', 'set', 'LD_LIBRARY_PATH=' + os.pathsep.join(library_paths) ])
if is_windows():
if os.getenv('PATH'):
library_paths = os.getenv('PATH').split(os.pathsep)
library_paths.extend(
[
os.path.join(os.getenv('CONDA_PREFIX'), 'Lib'),
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) ]))
subprocess.call([ shutil.which('conda'), 'env', 'config', 'vars', 'set', 'PATH=' + os.pathsep.join(library_paths) ])1234567891011121314151617181920import os
from datetime import datetime
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]:
if output_path:
output_directory_path, output_file_path = os.path.split(output_path)
output_file_name = get_file_name(output_file_path)
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 None
def suggest_job_id(job_prefix : str = 'job') -> str:
return job_prefix + '-' + datetime.now().strftime('%Y-%m-%d-%H-%M-%S')12345678910111213141516171819202122232425262728293031323334from datetime import datetime
from typing import List, Optional, Tuple
from facefusion.jobs import job_manager
from facefusion.time_helper import describe_time_ago
from facefusion.types import JobStatus, TableContent, TableHeader
def compose_job_list(job_status : JobStatus) -> Tuple[List[TableHeader], List[List[TableContent]]]:
jobs = job_manager.find_jobs(job_status)
job_headers : List[TableHeader] = [ 'job id', 'steps', 'date created', 'date updated', 'job status' ]
job_contents : List[List[TableContent]] = []
for index, job_id in enumerate(jobs):
if job_manager.validate_job(job_id):
job = jobs[job_id]
step_total = job_manager.count_step_total(job_id)
date_created = prepare_describe_datetime(job.get('date_created'))
date_updated = prepare_describe_datetime(job.get('date_updated'))
job_contents.append(
[
job_id,
step_total,
date_created,
date_updated,
job_status
])
return job_headers, job_contents
def prepare_describe_datetime(date_time : Optional[str]) -> Optional[str]:
if date_time:
return describe_time_ago(datetime.fromisoformat(date_time))
return None123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265import os
from copy import copy
from typing import List, Optional
import facefusion.choices
from facefusion.filesystem import create_directory, get_file_name, is_directory, is_file, move_file, remove_directory, remove_file, resolve_file_pattern
from facefusion.jobs.job_helper import get_step_output_path
from facefusion.json import read_json, write_json
from facefusion.time_helper import get_current_date_time
from facefusion.types import Args, Job, JobSet, JobStatus, JobStep, JobStepStatus
JOBS_PATH : Optional[str] = None
def init_jobs(jobs_path : str) -> bool:
global JOBS_PATH
JOBS_PATH = jobs_path
job_status_paths = [ os.path.join(JOBS_PATH, job_status) for job_status in facefusion.choices.job_statuses ]
for job_status_path in job_status_paths:
create_directory(job_status_path)
return all(is_directory(status_path) for status_path in job_status_paths)
def clear_jobs(jobs_path : str) -> bool:
return remove_directory(jobs_path)
def create_job(job_id : str) -> bool:
job : Job =\
{
'version': '1',
'date_created': get_current_date_time().isoformat(),
'date_updated': None,
'steps': []
}
return create_job_file(job_id, job)
def submit_job(job_id : str) -> bool:
drafted_job_ids = find_job_ids('drafted')
steps = get_steps(job_id)
if job_id in drafted_job_ids and steps:
return set_steps_status(job_id, 'queued') and move_job_file(job_id, 'queued')
return False
def submit_jobs(halt_on_error : bool) -> bool:
drafted_job_ids = find_job_ids('drafted')
has_error = False
if drafted_job_ids:
for job_id in drafted_job_ids:
if not submit_job(job_id):
has_error = True
if halt_on_error:
return False
return not has_error
return False
def delete_job(job_id : str) -> bool:
return delete_job_file(job_id)
def delete_jobs(halt_on_error : bool) -> bool:
job_ids = find_job_ids('drafted') + find_job_ids('queued') + find_job_ids('failed') + find_job_ids('completed')
has_error = False
if job_ids:
for job_id in job_ids:
if not delete_job(job_id):
has_error = True
if halt_on_error:
return False
return not has_error
return False
def find_jobs(job_status : JobStatus) -> JobSet:
job_ids = find_job_ids(job_status)
job_set : JobSet = {}
for job_id in job_ids:
job_set[job_id] = read_job_file(job_id)
return job_set
def find_job_ids(job_status : JobStatus) -> List[str]:
job_pattern = os.path.join(JOBS_PATH, job_status, '*.json')
job_paths = resolve_file_pattern(job_pattern)
job_paths.sort(key = os.path.getmtime)
job_ids = []
for job_path in job_paths:
job_id = get_file_name(job_path)
job_ids.append(job_id)
return job_ids
def validate_job(job_id : str) -> bool:
job = read_job_file(job_id)
return bool(job and 'version' in job and 'date_created' in job and 'date_updated' in job and 'steps' in job)
def has_step(job_id : str, step_index : int) -> bool:
step_total = count_step_total(job_id)
return step_index in range(step_total)
def add_step(job_id : str, step_args : Args) -> bool:
job = read_job_file(job_id)
if job:
job.get('steps').append(
{
'args': step_args,
'status': 'drafted'
})
return update_job_file(job_id, job)
return False
def remix_step(job_id : str, step_index : int, step_args : Args) -> bool:
steps = get_steps(job_id)
step_args = copy(step_args)
if step_index and step_index < 0:
step_index = count_step_total(job_id) - 1
if has_step(job_id, step_index):
output_path = steps[step_index].get('args').get('output_path')
step_args['target_path'] = get_step_output_path(job_id, step_index, output_path)
return add_step(job_id, step_args)
return False
def insert_step(job_id : str, step_index : int, step_args : Args) -> bool:
job = read_job_file(job_id)
step_args = copy(step_args)
if step_index and step_index < 0:
step_index = count_step_total(job_id) - 1
if job and has_step(job_id, step_index):
job.get('steps').insert(step_index,
{
'args': step_args,
'status': 'drafted'
})
return update_job_file(job_id, job)
return False
def remove_step(job_id : str, step_index : int) -> bool:
job = read_job_file(job_id)
if step_index and step_index < 0:
step_index = count_step_total(job_id) - 1
if job and has_step(job_id, step_index):
job.get('steps').pop(step_index)
return update_job_file(job_id, job)
return False
def get_steps(job_id : str) -> List[JobStep]:
job = read_job_file(job_id)
if job:
return job.get('steps')
return []
def count_step_total(job_id : str) -> int:
steps = get_steps(job_id)
if steps:
return len(steps)
return 0
def set_step_status(job_id : str, step_index : int, step_status : JobStepStatus) -> bool:
job = read_job_file(job_id)
if job:
steps = job.get('steps')
if has_step(job_id, step_index):
steps[step_index]['status'] = step_status
return update_job_file(job_id, job)
return False
def set_steps_status(job_id : str, step_status : JobStepStatus) -> bool:
job = read_job_file(job_id)
if job:
for step in job.get('steps'):
step['status'] = step_status
return update_job_file(job_id, job)
return False
def read_job_file(job_id : str) -> Optional[Job]:
job_path = find_job_path(job_id)
return read_json(job_path) #type:ignore[return-value]
def create_job_file(job_id : str, job : Job) -> bool:
job_path = find_job_path(job_id)
if not is_file(job_path):
job_create_path = suggest_job_path(job_id, 'drafted')
return write_json(job_create_path, job) #type:ignore[arg-type]
return False
def update_job_file(job_id : str, job : Job) -> bool:
job_path = find_job_path(job_id)
if is_file(job_path):
job['date_updated'] = get_current_date_time().isoformat()
return write_json(job_path, job) #type:ignore[arg-type]
return False
def move_job_file(job_id : str, job_status : JobStatus) -> bool:
job_path = find_job_path(job_id)
job_move_path = suggest_job_path(job_id, job_status)
return move_file(job_path, job_move_path)
def delete_job_file(job_id : str) -> bool:
job_path = find_job_path(job_id)
return remove_file(job_path)
def suggest_job_path(job_id : str, job_status : JobStatus) -> Optional[str]:
job_file_name = get_job_file_name(job_id)
if job_file_name:
return os.path.join(JOBS_PATH, job_status, job_file_name)
return None
def find_job_path(job_id : str) -> Optional[str]:
job_file_name = get_job_file_name(job_id)
if job_file_name:
for job_status in facefusion.choices.job_statuses:
job_pattern = os.path.join(JOBS_PATH, job_status, job_file_name)
job_paths = resolve_file_pattern(job_pattern)
for job_path in job_paths:
return job_path
return None
def get_job_file_name(job_id : str) -> Optional[str]:
if job_id:
return job_id + '.json'
return None123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112from facefusion.ffmpeg import concat_video
from facefusion.filesystem import are_images, are_videos, move_file, remove_file
from facefusion.jobs import job_helper, job_manager
from facefusion.types import JobOutputSet, JobStep, ProcessStep
def run_job(job_id : str, process_step : ProcessStep) -> bool:
queued_job_ids = job_manager.find_job_ids('queued')
if job_id in queued_job_ids:
if run_steps(job_id, process_step) and finalize_steps(job_id):
clean_steps(job_id)
return job_manager.move_job_file(job_id, 'completed')
clean_steps(job_id)
job_manager.move_job_file(job_id, 'failed')
return False
def run_jobs(process_step : ProcessStep, halt_on_error : bool) -> bool:
queued_job_ids = job_manager.find_job_ids('queued')
has_error = False
if queued_job_ids:
for job_id in queued_job_ids:
if not run_job(job_id, process_step):
has_error = True
if halt_on_error:
return False
return not has_error
return False
def retry_job(job_id : str, process_step : ProcessStep) -> bool:
failed_job_ids = job_manager.find_job_ids('failed')
if job_id in failed_job_ids:
return job_manager.set_steps_status(job_id, 'queued') and job_manager.move_job_file(job_id, 'queued') and run_job(job_id, process_step)
return False
def retry_jobs(process_step : ProcessStep, halt_on_error : bool) -> bool:
failed_job_ids = job_manager.find_job_ids('failed')
has_error = False
if failed_job_ids:
for job_id in failed_job_ids:
if not retry_job(job_id, process_step):
has_error = True
if halt_on_error:
return False
return not has_error
return False
def run_step(job_id : str, step_index : int, step : JobStep, process_step : ProcessStep) -> bool:
step_args = step.get('args')
if job_manager.set_step_status(job_id, step_index, 'started') and process_step(job_id, step_index, step_args):
output_path = step_args.get('output_path')
step_output_path = job_helper.get_step_output_path(job_id, step_index, output_path)
return move_file(output_path, step_output_path) and job_manager.set_step_status(job_id, step_index, 'completed')
job_manager.set_step_status(job_id, step_index, 'failed')
return False
def run_steps(job_id : str, process_step : ProcessStep) -> bool:
steps = job_manager.get_steps(job_id)
if steps:
for index, step in enumerate(steps):
if not run_step(job_id, index, step, process_step):
return False
return True
return False
def finalize_steps(job_id : str) -> bool:
output_set = collect_output_set(job_id)
for output_path, temp_output_paths in output_set.items():
if are_videos(temp_output_paths):
if not concat_video(output_path, temp_output_paths):
return False
if are_images(temp_output_paths):
for temp_output_path in temp_output_paths:
if not move_file(temp_output_path, output_path):
return False
return True
def clean_steps(job_id: str) -> bool:
output_set = collect_output_set(job_id)
for temp_output_paths in output_set.values():
for temp_output_path in temp_output_paths:
if not remove_file(temp_output_path):
return False
return True
def collect_output_set(job_id : str) -> JobOutputSet:
steps = job_manager.get_steps(job_id)
job_output_set : JobOutputSet = {}
for index, step in enumerate(steps):
output_path = step.get('args').get('output_path')
if 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)
return job_output_set123456789101112131415161718192021222324252627from typing import List
from facefusion.types import JobStore
JOB_STORE : JobStore =\
{
'job_keys': [],
'step_keys': []
}
def get_job_keys() -> List[str]:
return JOB_STORE.get('job_keys')
def get_step_keys() -> List[str]:
return JOB_STORE.get('step_keys')
def register_job_keys(job_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:
JOB_STORE['step_keys'].append(step_key)12345678910111213141516171819202122import json
from json import JSONDecodeError
from typing import Optional
from facefusion.filesystem import is_file
from facefusion.types import Content
def read_json(json_path : str) -> Optional[Content]:
if is_file(json_path):
try:
with open(json_path) as json_file:
return json.load(json_file)
except JSONDecodeError:
pass
return None
def write_json(json_path : str, content : Content) -> bool:
with open(json_path, 'w') as json_file:
json.dump(content, json_file, indent = 4)
return is_file(json_path)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274from facefusion.types import Locales
LOCALES : Locales =\
{
'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'
}
}
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748from logging import Logger, basicConfig, getLogger
import facefusion.choices
from facefusion.common_helper import get_first, get_last
from facefusion.types import LogLevel
def init(log_level : LogLevel) -> None:
basicConfig(format = '%(message)s')
get_package_logger().setLevel(facefusion.choices.log_level_set.get(log_level))
def get_package_logger() -> Logger:
return getLogger('facefusion')
def debug(message : str, module_name : str) -> None:
get_package_logger().debug(create_message(message, module_name))
def info(message : str, module_name : str) -> None:
get_package_logger().info(create_message(message, module_name))
def warn(message : str, module_name : str) -> None:
get_package_logger().warning(create_message(message, module_name))
def error(message : str, module_name : str) -> None:
get_package_logger().error(create_message(message, module_name))
def create_message(message : str, module_name : str) -> str:
module_names = module_name.split('.')
first_module_name = get_first(module_names)
last_module_name = get_last(module_names)
if first_module_name and last_module_name:
return '[' + first_module_name.upper() + '.' + last_module_name.upper() + '] ' + message
return message
def enable() -> None:
get_package_logger().disabled = False
def disable() -> None:
get_package_logger().disabled = True123456789101112131415161718192021from facefusion.common_helper import is_macos, is_windows
if is_windows():
import ctypes
else:
import resource
def limit_system_memory(system_memory_limit : int = 1) -> bool:
if is_macos():
system_memory_limit = system_memory_limit * (1024 ** 6)
else:
system_memory_limit = system_memory_limit * (1024 ** 3)
try:
if is_windows():
ctypes.windll.kernel32.SetProcessWorkingSetSize(-1, ctypes.c_size_t(system_memory_limit), ctypes.c_size_t(system_memory_limit)) #type:ignore[attr-defined]
else:
resource.setrlimit(resource.RLIMIT_DATA, (system_memory_limit, system_memory_limit))
return True
except Exception:
return False123456789101112131415from typing import Optional
METADATA =\
{
'name': 'FaceFusion',
'description': 'Industry leading face manipulation platform',
'version': '3.5.2',
'license': 'OpenRAIL-AS',
'author': 'Henry Ruhs',
'url': 'https://facefusion.io'
}
def get(key : str) -> Optional[str]:
return METADATA.get(key)1234567891011from 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])123456789101112131415161718192021222324252627282930313233from typing import List, Optional
from facefusion.types import Color, Fps, Padding
def normalize_color(channels : Optional[List[int]]) -> Optional[Color]:
if channels and len(channels) == 1:
return tuple([ channels[0], channels[0], channels[0], 255 ]) #type:ignore[return-value]
if channels and len(channels) == 2:
return tuple([ channels[0], channels[1], channels[0], 255 ]) #type:ignore[return-value]
if channels and len(channels) == 3:
return tuple([ channels[0], channels[1], channels[2], 255 ]) #type:ignore[return-value]
if channels and len(channels) == 4:
return tuple(channels) #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
def normalize_fps(fps : Optional[float]) -> Optional[Fps]:
if isinstance(fps, (int, float)):
return max(1.0, min(fps, 60.0))
return None123456789101112131415161718192021222324252627282930313233343536373839404142434445from facefusion.types import ProcessState
PROCESS_STATE : ProcessState = 'pending'
def get_process_state() -> ProcessState:
return PROCESS_STATE
def set_process_state(process_state : ProcessState) -> None:
global PROCESS_STATE
PROCESS_STATE = process_state
def is_checking() -> bool:
return get_process_state() == 'checking'
def is_processing() -> bool:
return get_process_state() == 'processing'
def is_stopping() -> bool:
return get_process_state() == 'stopping'
def is_pending() -> bool:
return get_process_state() == 'pending'
def check() -> None:
set_process_state('checking')
def start() -> None:
set_process_state('processing')
def stop() -> None:
set_process_state('stopping')
def end() -> None:
set_process_state('pending')1234567891011121314151617181920212223242526272829303132333435363738394041424344import importlib
from types import ModuleType
from typing import Any, List
from facefusion import logger, translator
from facefusion.exit_helper import hard_exit
PROCESSORS_METHODS =\
[
'get_inference_pool',
'clear_inference_pool',
'register_args',
'apply_args',
'pre_check',
'pre_process',
'post_process',
'process_frame'
]
def load_processor_module(processor : str) -> Any:
try:
processor_module = importlib.import_module('facefusion.processors.modules.' + processor + '.core')
for method_name in PROCESSORS_METHODS:
if not hasattr(processor_module, method_name):
raise NotImplementedError
except ModuleNotFoundError as exception:
logger.error(translator.get('processor_not_loaded').format(processor = processor), __name__)
logger.debug(exception.msg, __name__)
hard_exit(1)
except NotImplementedError:
logger.error(translator.get('processor_not_implemented').format(processor = processor), __name__)
hard_exit(1)
return processor_module
def get_processors_modules(processors : List[str]) -> List[ModuleType]:
processor_modules = []
for processor in processors:
processor_module = load_processor_module(processor)
processor_modules.append(processor_module)
return processor_modules123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101from typing import Tuple
import numpy
import scipy
from facefusion.processors.types import LivePortraitExpression, LivePortraitPitch, LivePortraitRoll, LivePortraitRotation, LivePortraitYaw
EXPRESSION_MIN = numpy.array(
[
[
[ -2.88067125e-02, -8.12731311e-02, -1.70541159e-03 ],
[ -4.88598682e-02, -3.32196616e-02, -1.67431499e-04 ],
[ -6.75425082e-02, -4.28681746e-02, -1.98950816e-04 ],
[ -7.23103955e-02, -3.28503326e-02, -7.31324719e-04 ],
[ -3.87073644e-02, -6.01546466e-02, -5.50269964e-04 ],
[ -6.38048723e-02, -2.23840728e-01, -7.13261834e-04 ],
[ -3.02710701e-02, -3.93195450e-02, -8.24086510e-06 ],
[ -2.95799859e-02, -5.39318882e-02, -1.74219604e-04 ],
[ -2.92359516e-02, -1.53050944e-02, -6.30460854e-05 ],
[ -5.56493877e-03, -2.34344602e-02, -1.26858242e-04 ],
[ -4.37593013e-02, -2.77768299e-02, -2.70503685e-02 ],
[ -1.76926646e-02, -1.91676542e-02, -1.15090821e-04 ],
[ -8.34268332e-03, -3.99775570e-03, -3.27481248e-05 ],
[ -3.40162888e-02, -2.81868968e-02, -1.96679524e-04 ],
[ -2.91855410e-02, -3.97511162e-02, -2.81230678e-05 ],
[ -1.50395725e-02, -2.49494594e-02, -9.42573533e-05 ],
[ -1.67938769e-02, -2.00953931e-02, -4.00750607e-04 ],
[ -1.86435618e-02, -2.48535164e-02, -2.74416432e-02 ],
[ -4.61211195e-03, -1.21660791e-02, -2.93173041e-04 ],
[ -4.10017073e-02, -7.43824020e-02, -4.42762971e-02 ],
[ -1.90370996e-02, -3.74363363e-02, -1.34740388e-02 ]
]
]).astype(numpy.float32)
EXPRESSION_MAX = numpy.array(
[
[
[ 4.46682945e-02, 7.08772913e-02, 4.08344204e-04 ],
[ 2.14308221e-02, 6.15894832e-02, 4.85319615e-05 ],
[ 3.02363783e-02, 4.45043296e-02, 1.28298725e-05 ],
[ 3.05869691e-02, 3.79812494e-02, 6.57040102e-04 ],
[ 4.45670523e-02, 3.97259220e-02, 7.10966764e-04 ],
[ 9.43699256e-02, 9.85926315e-02, 2.02551950e-04 ],
[ 1.61131397e-02, 2.92906128e-02, 3.44733417e-06 ],
[ 5.23825921e-02, 1.07065082e-01, 6.61510974e-04 ],
[ 2.85718683e-03, 8.32320191e-03, 2.39314613e-04 ],
[ 2.57947259e-02, 1.60935968e-02, 2.41853559e-05 ],
[ 4.90833223e-02, 3.43903080e-02, 3.22353356e-02 ],
[ 1.44766076e-02, 3.39248963e-02, 1.42291479e-04 ],
[ 8.75749043e-04, 6.82212645e-03, 2.76097053e-05 ],
[ 1.86958015e-02, 3.84016186e-02, 7.33085908e-05 ],
[ 2.01714113e-02, 4.90544215e-02, 2.34028921e-05 ],
[ 2.46518422e-02, 3.29151377e-02, 3.48571630e-05 ],
[ 2.22457591e-02, 1.21796541e-02, 1.56396593e-04 ],
[ 1.72109623e-02, 3.01626958e-02, 1.36556877e-02 ],
[ 1.83460284e-02, 1.61141958e-02, 2.87440169e-04 ],
[ 3.57594155e-02, 1.80554688e-01, 2.75554154e-02 ],
[ 2.17450950e-02, 8.66811201e-02, 3.34241726e-02 ]
]
]).astype(numpy.float32)
def limit_expression(expression : LivePortraitExpression) -> LivePortraitExpression:
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]:
pitch_min, pitch_max, yaw_min, yaw_max, roll_min, roll_max = calculate_euler_limits(target_pitch, target_yaw, target_roll)
output_pitch = numpy.clip(output_pitch, pitch_min, pitch_max)
output_yaw = numpy.clip(output_yaw, yaw_min, yaw_max)
output_roll = numpy.clip(output_roll, roll_min, roll_max)
return output_pitch, output_yaw, output_roll
def calculate_euler_limits(pitch : LivePortraitPitch, yaw : LivePortraitYaw, roll : LivePortraitRoll) -> Tuple[float, float, float, float, float, float]:
pitch_min = -30.0
pitch_max = 30.0
yaw_min = -60.0
yaw_max = 60.0
roll_min = -20.0
roll_max = 20.0
if pitch < 0:
pitch_min = min(pitch, pitch_min)
else:
pitch_max = max(pitch, pitch_max)
if yaw < 0:
yaw_min = min(yaw, yaw_min)
else:
yaw_max = max(yaw, yaw_max)
if roll < 0:
roll_min = min(roll, roll_min)
else:
roll_max = max(roll, roll_max)
return pitch_min, pitch_max, yaw_min, yaw_max, roll_min, roll_max
def create_rotation(pitch : LivePortraitPitch, yaw : LivePortraitYaw, roll : LivePortraitRoll) -> LivePortraitRotation:
rotation = scipy.spatial.transform.Rotation.from_euler('xyz', [ pitch, yaw, roll ], degrees = True).as_matrix()
rotation = rotation.astype(numpy.float32)
return rotationfacefusion/__init__.pyDeleted
facefusion/app_context.pyDeleted-16
facefusion/args.pyDeleted-131
facefusion/audio.pyDeleted-143
facefusion/benchmarker.pyDeleted-111
facefusion/camera_manager.pyDeleted-53
facefusion/choices.pyDeleted-169
facefusion/cli_helper.pyDeleted-35
facefusion/common_helper.pyDeleted-84
facefusion/config.pyDeleted-74
facefusion/content_analyser.pyDeleted-247
facefusion/core.pyDeleted-350
facefusion/curl_builder.pyDeleted-32
facefusion/download.pyDeleted-175
facefusion/execution.pyDeleted-160
facefusion/exit_helper.pyDeleted-34
facefusion/face_analyser.pyDeleted-143
facefusion/face_classifier.pyDeleted-140
facefusion/face_detector.pyDeleted-458
facefusion/face_helper.pyDeleted-256
facefusion/face_landmarker.pyDeleted-242
facefusion/face_masker.pyDeleted-279
facefusion/face_recognizer.pyDeleted-93
facefusion/face_selector.pyDeleted-130
facefusion/face_store.pyDeleted-28
facefusion/ffmpeg.pyDeleted-291
facefusion/ffmpeg_builder.pyDeleted-267
facefusion/filesystem.pyDeleted-190
facefusion/hash_helper.pyDeleted-32
facefusion/inference_manager.pyDeleted-96
facefusion/installer.pyDeleted-109
facefusion/jobs/__init__.pyDeleted
facefusion/jobs/job_helper.pyDeleted-20
facefusion/jobs/job_list.pyDeleted-34
facefusion/jobs/job_manager.pyDeleted-265
facefusion/jobs/job_runner.pyDeleted-112
facefusion/jobs/job_store.pyDeleted-27
facefusion/json.pyDeleted-22
facefusion/locales.pyDeleted-274
facefusion/logger.pyDeleted-48
facefusion/memory.pyDeleted-21
facefusion/metadata.pyDeleted-15
facefusion/model_helper.pyDeleted-11
facefusion/normalizer.pyDeleted-33
facefusion/process_manager.pyDeleted-45
facefusion/processors/__init__.pyDeleted
facefusion/processors/core.pyDeleted-44
facefusion/processors/live_portrait.pyDeleted-101
facefusion/processors/modules/__init__.pyDeleted
facefusion/processors/modules/age_modifier/__init__.pyDeleted