From f0fc5a9e05aa8a1150c010ecdaa0496b149c1175 Mon Sep 17 00:00:00 2001 From: henryruhs Date: Thu, 24 Sep 2026 11:55:13 +0200 Subject: [PATCH] unify Margin and Padding to Space, handle Space value on /state --- facefusion/apis/state_helper.py | 14 ++++++++++-- facefusion/face_detector.py | 4 ++-- facefusion/face_masker.py | 4 ++-- facefusion/normalizer.py | 38 +++++++++++++++++---------------- facefusion/types.py | 7 +++--- tests/test_normalizer.py | 3 +++ 6 files changed, 42 insertions(+), 28 deletions(-) diff --git a/facefusion/apis/state_helper.py b/facefusion/apis/state_helper.py index 109ddceb..f57eed26 100644 --- a/facefusion/apis/state_helper.py +++ b/facefusion/apis/state_helper.py @@ -1,7 +1,8 @@ -from typing import Literal, get_args, get_origin +from typing import Literal, get_origin from facefusion import capability_store from facefusion.common_helper import cast_bool, cast_float, cast_int +from facefusion.normalizer import normalize_color, normalize_space from facefusion.processors.types import ProcessorState from facefusion.types import State, StateKey, StateValue @@ -22,10 +23,13 @@ def validate_argument_value(key : StateKey, value : StateValue) -> bool: if get_origin(argument_type) is list: if choices: - return isinstance(value, list) and value in choices + return isinstance(value, list) and all(choice in choices for choice in value) return isinstance(value, list) + if get_origin(argument_type) is tuple: + return isinstance(value, tuple) + if get_origin(argument_type) is Literal: if choices: return isinstance(value, str) and value in choices @@ -47,4 +51,10 @@ def cast_argument_value(key : StateKey, value : StateValue) -> StateValue: if argument_type is bool: return cast_bool(value) + if key in [ 'face_detector_margin', 'face_mask_padding' ]: + return normalize_space(value) + + if key in [ 'background_remover_fill_color', 'background_remover_despill_color' ]: + return normalize_color(value) + return value diff --git a/facefusion/face_detector.py b/facefusion/face_detector.py index 1ee9dd3c..2a0371ea 100644 --- a/facefusion/face_detector.py +++ b/facefusion/face_detector.py @@ -9,7 +9,7 @@ from facefusion.download import conditional_download_hashes, conditional_downloa 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.types import Angle, BoundingBox, Detection, DownloadScope, DownloadSet, FaceLandmark5, InferencePool, ModelSet, Score, Space, VisionFrame from facefusion.vision import restrict_frame, unpack_resolution @@ -187,7 +187,7 @@ def detect_faces(vision_frame : VisionFrame) -> Tuple[List[BoundingBox], List[Sc return all_bounding_boxes, all_face_scores, all_face_landmarks_5 -def prepare_margin(vision_frame : VisionFrame) -> Margin: +def prepare_margin(vision_frame : VisionFrame) -> Space: 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 ])) diff --git a/facefusion/face_masker.py b/facefusion/face_masker.py index 20f02484..a5de6b0d 100755 --- a/facefusion/face_masker.py +++ b/facefusion/face_masker.py @@ -9,7 +9,7 @@ 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 +from facefusion.types import DownloadScope, DownloadSet, FaceLandmark68, FaceMaskArea, FaceMaskRegion, InferencePool, Mask, ModelSet, Space, VisionFrame @lru_cache() @@ -185,7 +185,7 @@ def pre_check() -> bool: 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: +def create_box_mask(crop_vision_frame : VisionFrame, face_mask_blur : float, face_mask_padding : Space) -> 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) diff --git a/facefusion/normalizer.py b/facefusion/normalizer.py index 1f7ae093..15ca5f16 100644 --- a/facefusion/normalizer.py +++ b/facefusion/normalizer.py @@ -1,29 +1,31 @@ from typing import List, Optional -from facefusion.types import Color, Fps, Padding +from facefusion.types import Color, Fps, Space 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] + if isinstance(channels, list): + 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] +def normalize_space(spaces : Optional[List[int]]) -> Optional[Space]: + if isinstance(spaces, list): + 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 diff --git a/facefusion/types.py b/facefusion/types.py index fadf8ba4..4c2326f5 100755 --- a/facefusion/types.py +++ b/facefusion/types.py @@ -102,8 +102,7 @@ Fps : TypeAlias = float Duration : TypeAlias = float Time : TypeAlias = float Color : TypeAlias = Tuple[int, int, int, int] -Padding : TypeAlias = Tuple[int, int, int, int] -Margin : TypeAlias = Tuple[int, int, int, int] +Space : TypeAlias = Tuple[int, int, int, int] Orientation = Literal['landscape', 'portrait'] Resolution : TypeAlias = Tuple[int, int] @@ -621,7 +620,7 @@ State = TypedDict('State', 'benchmark_cycle_count' : int, 'face_detector_model' : FaceDetectorModel, 'face_detector_size' : str, - 'face_detector_margin' : Margin, + 'face_detector_margin' : Space, 'face_detector_angles' : List[Angle], 'face_detector_score' : Score, 'face_aligner_model' : FaceAlignerModel, @@ -642,7 +641,7 @@ State = TypedDict('State', 'face_mask_areas' : List[FaceMaskArea], 'face_mask_regions' : List[FaceMaskRegion], 'face_mask_blur' : float, - 'face_mask_padding' : Padding, + 'face_mask_padding' : Space, 'voice_extractor_model' : VoiceExtractorModel, 'trim_frame_start' : int, 'trim_frame_end' : int, diff --git a/tests/test_normalizer.py b/tests/test_normalizer.py index 789957c0..07511898 100644 --- a/tests/test_normalizer.py +++ b/tests/test_normalizer.py @@ -6,6 +6,7 @@ def test_normalize_color() -> None: assert normalize_color([ 0, 128 ]) == (0, 128, 0, 255) assert normalize_color([ 0, 128, 255 ]) == (0, 128, 255, 255) assert normalize_color([ 0, 128, 255, 0 ]) == (0, 128, 255, 0) + assert normalize_color('invalid') is None assert normalize_color(None) is None @@ -14,6 +15,7 @@ def test_normalize_space() -> None: assert normalize_space([ 1 ]) == (1, 1, 1, 1) assert normalize_space([ 1, 2 ]) == (1, 2, 1, 2) assert normalize_space([ 1, 2, 3 ]) == (1, 2, 3, 2) + assert normalize_space('invalid') is None assert normalize_space(None) is None @@ -21,4 +23,5 @@ def test_normalize_fps() -> None: assert normalize_fps(0.0) == 1.0 assert normalize_fps(25.0) == 25.0 assert normalize_fps(61.0) == 60.0 + assert normalize_fps('invalid') is None assert normalize_fps(None) is None