From 3298ed144bbba606611e8c299107619451b36c7a Mon Sep 17 00:00:00 2001 From: henryruhs Date: Thu, 12 Jun 2025 23:01:15 +0200 Subject: [PATCH] Ban lambda usage --- facefusion/core.py | 3 ++- facefusion/face_selector.py | 34 ++++++++++++++++++------- facefusion/ffmpeg.py | 9 +++++-- facefusion/installer.py | 7 ++--- facefusion/uis/components/processors.py | 8 +++++- 5 files changed, 45 insertions(+), 16 deletions(-) diff --git a/facefusion/core.py b/facefusion/core.py index c24d416..c948c25 100755 --- a/facefusion/core.py +++ b/facefusion/core.py @@ -2,6 +2,7 @@ import itertools import shutil import signal import sys +from functools import partial from time import time import numpy @@ -31,7 +32,7 @@ from facefusion.vision import pack_resolution, read_image, read_static_images, r def cli() -> None: if pre_check(): - signal.signal(signal.SIGINT, lambda signal_number, frame: graceful_exit(0)) + signal.signal(signal.SIGINT, partial(graceful_exit, 0)) program = create_program() if validate_args(program): diff --git a/facefusion/face_selector.py b/facefusion/face_selector.py index e87711d..0c0341e 100644 --- a/facefusion/face_selector.py +++ b/facefusion/face_selector.py @@ -3,7 +3,7 @@ from typing import List import numpy from facefusion import state_manager -from facefusion.types import Face, FaceSelectorOrder, FaceSet, Gender, Race +from facefusion.types import Face, FaceSelectorOrder, FaceSet, Gender, Race, Score def find_similar_faces(faces : List[Face], reference_faces : FaceSet, face_distance : float) -> List[Face]: @@ -46,24 +46,40 @@ def sort_and_filter_faces(faces : List[Face]) -> List[Face]: def sort_faces_by_order(faces : List[Face], order : FaceSelectorOrder) -> List[Face]: if order == 'left-right': - return sorted(faces, key = lambda face: face.bounding_box[0]) + return sorted(faces, key = get_bounding_box_left) if order == 'right-left': - return sorted(faces, key = lambda face: face.bounding_box[0], reverse = True) + return sorted(faces, key = get_bounding_box_left, reverse = True) if order == 'top-bottom': - return sorted(faces, key = lambda face: face.bounding_box[1]) + return sorted(faces, key = get_bounding_box_top) if order == 'bottom-top': - return sorted(faces, key = lambda face: face.bounding_box[1], reverse = True) + return sorted(faces, key = get_bounding_box_top, reverse = True) if order == 'small-large': - return sorted(faces, key = lambda face: (face.bounding_box[2] - face.bounding_box[0]) * (face.bounding_box[3] - face.bounding_box[1])) + return sorted(faces, key = get_bounding_box_area) if order == 'large-small': - return sorted(faces, key = lambda face: (face.bounding_box[2] - face.bounding_box[0]) * (face.bounding_box[3] - face.bounding_box[1]), reverse = True) + return sorted(faces, key = get_bounding_box_area, reverse = True) if order == 'best-worst': - return sorted(faces, key = lambda face: face.score_set.get('detector'), reverse = True) + return sorted(faces, key = get_face_detector_score, reverse = True) if order == 'worst-best': - return sorted(faces, key = lambda face: face.score_set.get('detector')) + 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 = [] diff --git a/facefusion/ffmpeg.py b/facefusion/ffmpeg.py index 95e2a42..6e83fe1 100644 --- a/facefusion/ffmpeg.py +++ b/facefusion/ffmpeg.py @@ -1,6 +1,7 @@ import os import subprocess import tempfile +from functools import partial from typing import List, Optional, cast from tqdm import tqdm @@ -40,6 +41,10 @@ def run_ffmpeg_with_progress(commands : Commands, update_progress : UpdateProgre return process +def update_progress(progress : tqdm, frame_number : int): + progress.update(frame_number - progress.n) + + def run_ffmpeg(commands : Commands) -> subprocess.Popen[bytes]: log_level = state_manager.get_item('log_level') commands = ffmpeg_builder.run(commands) @@ -114,7 +119,7 @@ def extract_frames(target_path : str, temp_video_resolution : str, temp_video_fp ) with tqdm(total = extract_frame_total, desc = wording.get('extracting'), unit = 'frame', ascii = ' =', disable = state_manager.get_item('log_level') in [ 'warn', 'error' ]) as progress: - process = run_ffmpeg_with_progress(commands, lambda frame_number: progress.update(frame_number - progress.n)) + process = run_ffmpeg_with_progress(commands, partial(update_progress, progress)) return process.returncode == 0 @@ -230,7 +235,7 @@ def merge_video(target_path : str, temp_video_fps : Fps, output_video_resolution ) with tqdm(total = merge_frame_total, desc = wording.get('merging'), unit = 'frame', ascii = ' =', disable = state_manager.get_item('log_level') in [ 'warn', 'error' ]) as progress: - process = run_ffmpeg_with_progress(commands, lambda frame_number: progress.update(frame_number - progress.n)) + process = run_ffmpeg_with_progress(commands, partial(update_progress, progress)) return process.returncode == 0 diff --git a/facefusion/installer.py b/facefusion/installer.py index 2dfa86c..0c35d7c 100644 --- a/facefusion/installer.py +++ b/facefusion/installer.py @@ -4,6 +4,7 @@ import signal import subprocess import sys from argparse import ArgumentParser, HelpFormatter +from functools import partial from facefusion import metadata, wording from facefusion.common_helper import is_linux, is_windows @@ -14,7 +15,7 @@ ONNXRUNTIME_SET =\ } if is_windows() or is_linux(): ONNXRUNTIME_SET['cuda'] = ('onnxruntime-gpu', '1.22.0') - ONNXRUNTIME_SET['openvino'] = ('onnxruntime-openvino', '1.21.0') + ONNXRUNTIME_SET['openvino'] = ('onnxruntime-openvino', '1.22.0') if is_windows(): ONNXRUNTIME_SET['directml'] = ('onnxruntime-directml', '1.17.3') if is_linux(): @@ -22,8 +23,8 @@ if is_linux(): def cli() -> None: - signal.signal(signal.SIGINT, lambda signal_number, frame: sys.exit(0)) - program = ArgumentParser(formatter_class = lambda prog: HelpFormatter(prog, max_help_position = 50)) + signal.signal(signal.SIGINT, partial(sys.exit, 0)) + program = ArgumentParser(formatter_class = partial(HelpFormatter, max_help_position = 50)) program.add_argument('--onnxruntime', help = wording.get('help.install_dependency').format(dependency = 'onnxruntime'), choices = ONNXRUNTIME_SET.keys(), required = True) program.add_argument('--skip-conda', help = wording.get('help.skip_conda'), action = 'store_true') program.add_argument('-v', '--version', version = metadata.get('name') + ' ' + metadata.get('version'), action = 'version') diff --git a/facefusion/uis/components/processors.py b/facefusion/uis/components/processors.py index 85ebc9f..f734a0d 100644 --- a/facefusion/uis/components/processors.py +++ b/facefusion/uis/components/processors.py @@ -40,4 +40,10 @@ def update_processors(processors : List[str]) -> gradio.CheckboxGroup: def sort_processors(processors : List[str]) -> List[str]: available_processors = [ get_file_name(file_path) for file_path in resolve_file_paths('facefusion/processors/modules') ] - return sorted(available_processors, key = lambda processor : processors.index(processor) if processor in processors else len(processors)) + current_processors = [] + + for processor in processors + available_processors: + if processor in available_processors and processor not in current_processors: + current_processors.append(processor) + + return current_processors