mirror of
https://github.com/facefusion/facefusion.git
synced 2026-11-21 17:31:00 +01:00
unify Margin and Padding to Space, handle Space value on /state
This commit is contained in:
1 parent
e2c5f4cdf7
commit
f0fc5a9e05
6 files changed
+42
-28
No files matched your search
@@ -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
|
||||
@@ -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 ]))
|
||||
|
||||
@@ -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)
|
||||
|
||||
+20
-18
@@ -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
|
||||
|
||||
|
||||
|
||||
+3
-4
@@ -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,
|
||||
|
||||
@@ -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
|
||||
Reference in new issue
Block a user