mirror of
https://github.com/facefusion/facefusion.git
synced 2026-04-23 01:46:09 +02:00
Follow the config parser way part3
This commit is contained in:
+16
-32
@@ -1,5 +1,5 @@
|
||||
from configparser import ConfigParser
|
||||
from typing import Any, List, Optional
|
||||
from typing import List, Optional
|
||||
|
||||
from facefusion import state_manager
|
||||
from facefusion.common_helper import cast_bool, cast_float, cast_int
|
||||
@@ -54,37 +54,21 @@ def get_bool_value(section : str, option : str, fallback : Optional[str] = None)
|
||||
return cast_bool(fallback)
|
||||
|
||||
|
||||
def get_str_list(key : str, fallback : Optional[str] = None) -> Optional[List[str]]:
|
||||
value = get_value_by_notation(key)
|
||||
|
||||
if value or fallback:
|
||||
return [ str(value) for value in (value or fallback).split(' ') ]
|
||||
return None
|
||||
|
||||
|
||||
def get_int_list(key : str, fallback : Optional[str] = None) -> Optional[List[int]]:
|
||||
value = get_value_by_notation(key)
|
||||
|
||||
if value or fallback:
|
||||
return [ cast_int(value) for value in (value or fallback).split(' ') ]
|
||||
return None
|
||||
|
||||
|
||||
def get_float_list(key : str, fallback : Optional[str] = None) -> Optional[List[float]]:
|
||||
value = get_value_by_notation(key)
|
||||
|
||||
if value or fallback:
|
||||
return [ cast_float(value) for value in (value or fallback).split(' ') ]
|
||||
return None
|
||||
|
||||
|
||||
def get_value_by_notation(key : str) -> Optional[Any]:
|
||||
def get_str_list(section : str, option : str, fallback : Optional[str] = None) -> Optional[List[str]]:
|
||||
config_parser = get_config_parser()
|
||||
|
||||
if '.' in key:
|
||||
section, name = key.split('.')
|
||||
if section in config_parser and name in config_parser[section]:
|
||||
return config_parser[section][name]
|
||||
if key in config_parser:
|
||||
return config_parser[key]
|
||||
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 [ cast_int(value) for value in config_parser.get(section, option).split() ]
|
||||
if fallback:
|
||||
return [ cast_int(value) for value in fallback.split() ]
|
||||
return None
|
||||
|
||||
@@ -142,7 +142,7 @@ def detect_execution_devices() -> List[ExecutionDevice]:
|
||||
|
||||
def create_value_and_unit(text : str) -> Optional[ValueAndUnit]:
|
||||
if ' ' in text:
|
||||
value, unit = text.split(' ')
|
||||
value, unit = text.split()
|
||||
|
||||
return\
|
||||
{
|
||||
|
||||
@@ -32,7 +32,7 @@ def clear_inference_pool() -> None:
|
||||
def register_args(program : ArgumentParser) -> None:
|
||||
group_processors = find_argument_group(program, 'processors')
|
||||
if group_processors:
|
||||
group_processors.add_argument('--face-debugger-items', help = wording.get('help.face_debugger_items').format(choices = ', '.join(processors_choices.face_debugger_items)), default = config.get_str_list('processors.face_debugger_items', 'face-landmark-5/68 face-mask'), choices = processors_choices.face_debugger_items, nargs = '+', metavar = 'FACE_DEBUGGER_ITEMS')
|
||||
group_processors.add_argument('--face-debugger-items', help = wording.get('help.face_debugger_items').format(choices = ', '.join(processors_choices.face_debugger_items)), default = config.get_str_list('processors', 'face_debugger_items', 'face-landmark-5/68 face-mask'), choices = processors_choices.face_debugger_items, nargs = '+', metavar = 'FACE_DEBUGGER_ITEMS')
|
||||
facefusion.jobs.job_store.register_step_keys([ 'face_debugger_items' ])
|
||||
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ def create_jobs_path_program() -> ArgumentParser:
|
||||
def create_source_paths_program() -> ArgumentParser:
|
||||
program = ArgumentParser(add_help = False)
|
||||
group_paths = program.add_argument_group('paths')
|
||||
group_paths.add_argument('-s', '--source-paths', help = wording.get('help.source_paths'), default = config.get_str_list('paths.source_paths'), nargs = '+')
|
||||
group_paths.add_argument('-s', '--source-paths', help = wording.get('help.source_paths'), default = config.get_str_list('paths', 'source_paths'), nargs = '+')
|
||||
job_store.register_step_keys([ 'source_paths' ])
|
||||
return program
|
||||
|
||||
@@ -99,7 +99,7 @@ def create_face_detector_program() -> ArgumentParser:
|
||||
known_args, _ = program.parse_known_args()
|
||||
face_detector_size_choices = facefusion.choices.face_detector_set.get(known_args.face_detector_model)
|
||||
group_face_detector.add_argument('--face-detector-size', help = wording.get('help.face_detector_size'), default = config.get_str_value('face_detector', 'face_detector_size', get_last(face_detector_size_choices)), choices = face_detector_size_choices)
|
||||
group_face_detector.add_argument('--face-detector-angles', help = wording.get('help.face_detector_angles'), type = int, default = config.get_int_list('face_detector.face_detector_angles', '0'), choices = facefusion.choices.face_detector_angles, nargs = '+', metavar = 'FACE_DETECTOR_ANGLES')
|
||||
group_face_detector.add_argument('--face-detector-angles', help = wording.get('help.face_detector_angles'), type = int, default = config.get_int_list('face_detector', 'face_detector_angles', '0'), choices = facefusion.choices.face_detector_angles, nargs = '+', metavar = 'FACE_DETECTOR_ANGLES')
|
||||
group_face_detector.add_argument('--face-detector-score', help = wording.get('help.face_detector_score'), type = float, default = config.get_float_value('face_detector', 'face_detector_score', '0.5'), choices = facefusion.choices.face_detector_score_range, metavar = create_float_metavar(facefusion.choices.face_detector_score_range))
|
||||
job_store.register_step_keys([ 'face_detector_model', 'face_detector_angles', 'face_detector_size', 'face_detector_score' ])
|
||||
return program
|
||||
@@ -135,10 +135,10 @@ def create_face_masker_program() -> ArgumentParser:
|
||||
group_face_masker = program.add_argument_group('face masker')
|
||||
group_face_masker.add_argument('--face-occluder-model', help = wording.get('help.face_occluder_model'), default = config.get_str_value('face_detector', 'face_occluder_model', 'xseg_1'), choices = facefusion.choices.face_occluder_models)
|
||||
group_face_masker.add_argument('--face-parser-model', help = wording.get('help.face_parser_model'), default = config.get_str_value('face_detector', 'face_parser_model', 'bisenet_resnet_34'), choices = facefusion.choices.face_parser_models)
|
||||
group_face_masker.add_argument('--face-mask-types', help = wording.get('help.face_mask_types').format(choices = ', '.join(facefusion.choices.face_mask_types)), default = config.get_str_list('face_masker.face_mask_types', 'box'), choices = facefusion.choices.face_mask_types, nargs = '+', metavar = 'FACE_MASK_TYPES')
|
||||
group_face_masker.add_argument('--face-mask-types', help = wording.get('help.face_mask_types').format(choices = ', '.join(facefusion.choices.face_mask_types)), default = config.get_str_list('face_masker', 'face_mask_types', 'box'), choices = facefusion.choices.face_mask_types, nargs = '+', metavar = 'FACE_MASK_TYPES')
|
||||
group_face_masker.add_argument('--face-mask-blur', help = wording.get('help.face_mask_blur'), type = float, default = config.get_float_value('face_masker', 'face_mask_blur', '0.3'), choices = facefusion.choices.face_mask_blur_range, metavar = create_float_metavar(facefusion.choices.face_mask_blur_range))
|
||||
group_face_masker.add_argument('--face-mask-padding', help = wording.get('help.face_mask_padding'), type = int, default = config.get_int_list('face_masker.face_mask_padding', '0 0 0 0'), nargs = '+')
|
||||
group_face_masker.add_argument('--face-mask-regions', help = wording.get('help.face_mask_regions').format(choices = ', '.join(facefusion.choices.face_mask_regions)), default = config.get_str_list('face_masker.face_mask_regions', ' '.join(facefusion.choices.face_mask_regions)), choices = facefusion.choices.face_mask_regions, nargs = '+', metavar = 'FACE_MASK_REGIONS')
|
||||
group_face_masker.add_argument('--face-mask-padding', help = wording.get('help.face_mask_padding'), type = int, default = config.get_int_list('face_masker', 'face_mask_padding', '0 0 0 0'), nargs = '+')
|
||||
group_face_masker.add_argument('--face-mask-regions', help = wording.get('help.face_mask_regions').format(choices = ', '.join(facefusion.choices.face_mask_regions)), default = config.get_str_list('face_masker', 'face_mask_regions', ' '.join(facefusion.choices.face_mask_regions)), choices = facefusion.choices.face_mask_regions, nargs = '+', metavar = 'FACE_MASK_REGIONS')
|
||||
job_store.register_step_keys([ 'face_occluder_model', 'face_parser_model', 'face_mask_types', 'face_mask_blur', 'face_mask_padding', 'face_mask_regions' ])
|
||||
return program
|
||||
|
||||
@@ -176,7 +176,7 @@ def create_processors_program() -> ArgumentParser:
|
||||
program = ArgumentParser(add_help = False)
|
||||
available_processors = [ get_file_name(file_path) for file_path in resolve_file_paths('facefusion/processors/modules') ]
|
||||
group_processors = program.add_argument_group('processors')
|
||||
group_processors.add_argument('--processors', help = wording.get('help.processors').format(choices = ', '.join(available_processors)), default = config.get_str_list('processors.processors', 'face_swapper'), nargs = '+')
|
||||
group_processors.add_argument('--processors', help = wording.get('help.processors').format(choices = ', '.join(available_processors)), default = config.get_str_list('processors', 'processors', 'face_swapper'), nargs = '+')
|
||||
job_store.register_step_keys([ 'processors' ])
|
||||
for processor_module in get_processors_modules(available_processors):
|
||||
processor_module.register_args(program)
|
||||
@@ -188,7 +188,7 @@ def create_uis_program() -> ArgumentParser:
|
||||
available_ui_layouts = [ get_file_name(file_path) for file_path in resolve_file_paths('facefusion/uis/layouts') ]
|
||||
group_uis = program.add_argument_group('uis')
|
||||
group_uis.add_argument('--open-browser', help = wording.get('help.open_browser'), action = 'store_true', default = config.get_bool_value('uis', 'open_browser'))
|
||||
group_uis.add_argument('--ui-layouts', help = wording.get('help.ui_layouts').format(choices = ', '.join(available_ui_layouts)), default = config.get_str_list('uis.ui_layouts', 'default'), nargs = '+')
|
||||
group_uis.add_argument('--ui-layouts', help = wording.get('help.ui_layouts').format(choices = ', '.join(available_ui_layouts)), default = config.get_str_list('uis', 'ui_layouts', 'default'), nargs = '+')
|
||||
group_uis.add_argument('--ui-workflow', help = wording.get('help.ui_workflow'), default = config.get_str_value('uis', 'ui_workflow', 'instant_runner'), choices = facefusion.choices.ui_workflows)
|
||||
return program
|
||||
|
||||
@@ -198,7 +198,7 @@ def create_execution_program() -> ArgumentParser:
|
||||
available_execution_providers = get_available_execution_providers()
|
||||
group_execution = program.add_argument_group('execution')
|
||||
group_execution.add_argument('--execution-device-id', help = wording.get('help.execution_device_id'), default = config.get_str_value('execution', 'execution_device_id', '0'))
|
||||
group_execution.add_argument('--execution-providers', help = wording.get('help.execution_providers').format(choices = ', '.join(available_execution_providers)), default = config.get_str_list('execution.execution_providers', get_first(available_execution_providers)), choices = available_execution_providers, nargs = '+', metavar = 'EXECUTION_PROVIDERS')
|
||||
group_execution.add_argument('--execution-providers', help = wording.get('help.execution_providers').format(choices = ', '.join(available_execution_providers)), default = config.get_str_list('execution', 'execution_providers', get_first(available_execution_providers)), choices = available_execution_providers, nargs = '+', metavar = 'EXECUTION_PROVIDERS')
|
||||
group_execution.add_argument('--execution-thread-count', help = wording.get('help.execution_thread_count'), type = int, default = config.get_int_value('execution', 'execution_thread_count', '4'), choices = facefusion.choices.execution_thread_count_range, metavar = create_int_metavar(facefusion.choices.execution_thread_count_range))
|
||||
group_execution.add_argument('--execution-queue-count', help = wording.get('help.execution_queue_count'), type = int, default = config.get_int_value('execution', 'execution_queue_count', '1'), choices = facefusion.choices.execution_queue_count_range, metavar = create_int_metavar(facefusion.choices.execution_queue_count_range))
|
||||
job_store.register_job_keys([ 'execution_device_id', 'execution_providers', 'execution_thread_count', 'execution_queue_count' ])
|
||||
@@ -208,7 +208,7 @@ def create_execution_program() -> ArgumentParser:
|
||||
def create_download_providers_program() -> ArgumentParser:
|
||||
program = ArgumentParser(add_help = False)
|
||||
group_download = program.add_argument_group('download')
|
||||
group_download.add_argument('--download-providers', help = wording.get('help.download_providers').format(choices = ', '.join(facefusion.choices.download_providers)), default = config.get_str_list('download.download_providers', ' '.join(facefusion.choices.download_providers)), choices = facefusion.choices.download_providers, nargs = '+', metavar = 'DOWNLOAD_PROVIDERS')
|
||||
group_download.add_argument('--download-providers', help = wording.get('help.download_providers').format(choices = ', '.join(facefusion.choices.download_providers)), default = config.get_str_list('download', 'download_providers', ' '.join(facefusion.choices.download_providers)), choices = facefusion.choices.download_providers, nargs = '+', metavar = 'DOWNLOAD_PROVIDERS')
|
||||
job_store.register_job_keys([ 'download_providers' ])
|
||||
return program
|
||||
|
||||
|
||||
+8
-15
@@ -77,21 +77,14 @@ def test_get_bool_value() -> None:
|
||||
|
||||
|
||||
def test_get_str_list() -> None:
|
||||
assert config.get_str_list('str_list.valid') == [ 'a', 'b', 'c' ]
|
||||
assert config.get_str_list('str_list.unset', 'c b a') == [ 'c', 'b', 'a' ]
|
||||
assert config.get_str_list('str_list.unset') is None
|
||||
assert config.get_str_list('str_list.invalid') is None
|
||||
assert config.get_str_list('str_list', 'valid') == [ 'a', 'b', 'c' ]
|
||||
assert config.get_str_list('str_list', 'unset', 'c b a') == [ 'c', 'b', 'a' ]
|
||||
assert config.get_str_list('str_list', 'unset') is None
|
||||
assert config.get_str_list('str_list', 'invalid') is None
|
||||
|
||||
|
||||
def test_get_int_list() -> None:
|
||||
assert config.get_int_list('int_list.valid') == [ 1, 2, 3 ]
|
||||
assert config.get_int_list('int_list.unset', '3 2 1') == [ 3, 2, 1 ]
|
||||
assert config.get_int_list('int_list.unset') is None
|
||||
assert config.get_int_list('int_list.invalid') is None
|
||||
|
||||
|
||||
def test_get_float_list() -> None:
|
||||
assert config.get_float_list('float_list.valid') == [ 1.0, 2.0, 3.0 ]
|
||||
assert config.get_float_list('float_list.unset', '3.0 2.0 1.0') == [ 3.0, 2.0, 1.0 ]
|
||||
assert config.get_float_list('float_list.unset') is None
|
||||
assert config.get_float_list('float_list.invalid') is None
|
||||
assert config.get_int_list('int_list', 'valid') == [ 1, 2, 3 ]
|
||||
assert config.get_int_list('int_list', 'unset', '3 2 1') == [ 3, 2, 1 ]
|
||||
assert config.get_int_list('int_list', 'unset') is None
|
||||
assert config.get_int_list('int_list', 'invalid') is None
|
||||
|
||||
Reference in New Issue
Block a user