From a6929d6cb4f10aac02ca6ab41ac56b8d879721aa Mon Sep 17 00:00:00 2001 From: henryruhs Date: Tue, 12 Nov 2024 18:57:18 +0100 Subject: [PATCH] Allow bulk runner with target pattern only --- facefusion/core.py | 26 +++++++++++++++++--------- tests/test_cli_bulk_runner.py | 26 ++++++++++++++------------ tests/test_vision.py | 10 +++++----- 3 files changed, 36 insertions(+), 26 deletions(-) diff --git a/facefusion/core.py b/facefusion/core.py index 8b28fef0..d0e90103 100755 --- a/facefusion/core.py +++ b/facefusion/core.py @@ -318,16 +318,24 @@ def process_bulk(args : Args) -> ErrorCode: 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 + step_args['output_path'] = job_args.get('output_pattern').format(index = index) + 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 - 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 - step_args['output_path'] = job_args.get('output_pattern').format(index = index) - 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 + step_args['output_path'] = job_args.get('output_pattern').format(index = index) + 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 diff --git a/tests/test_cli_bulk_runner.py b/tests/test_cli_bulk_runner.py index bcae2d57..a82b0980 100644 --- a/tests/test_cli_bulk_runner.py +++ b/tests/test_cli_bulk_runner.py @@ -14,8 +14,8 @@ def before_all() -> None: [ 'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/target-240p.mp4' ]) - subprocess.run([ 'ffmpeg', '-i', get_test_example_file('target-240p.mp4'), '-vframes', '1', get_test_example_file('target-240p-a.jpg') ]) - subprocess.run([ 'ffmpeg', '-i', get_test_example_file('target-240p.mp4'), '-vframes', '2', get_test_example_file('target-240p-b.jpg') ]) + subprocess.run([ 'ffmpeg', '-i', get_test_example_file('target-240p.mp4'), '-vframes', '1', get_test_example_file('target-240p-bulk-1.jpg') ]) + subprocess.run([ 'ffmpeg', '-i', get_test_example_file('target-240p.mp4'), '-vframes', '2', get_test_example_file('target-240p-bulk-2.jpg') ]) @pytest.fixture(scope = 'function', autouse = True) @@ -25,19 +25,21 @@ def before_each() -> None: prepare_test_output_directory() -def test_bulk_run_image_to_image() -> None: - commands = [ sys.executable, 'facefusion.py', 'bulk-run', '--jobs-path', get_test_jobs_directory(), '--processors', 'face_debugger', '-s', get_test_example_file('target-240p-*.jpg'), '-t', get_test_example_file('target-240p-*.jpg'), '-o', get_test_output_file('test-bulk-run-image-to-image-{index}.jpg') ] +def test_bulk_run_targets() -> None: + commands = [ sys.executable, 'facefusion.py', 'bulk-run', '--jobs-path', get_test_jobs_directory(), '--processors', 'face_debugger', '-t', get_test_example_file('target-240p-bulk-*.jpg'), '-o', get_test_output_file('test-bulk-run-targets-{index}.jpg') ] assert subprocess.run(commands).returncode == 0 - assert is_test_output_file('test-bulk-run-image-to-image-0.jpg') is True - assert is_test_output_file('test-bulk-run-image-to-image-1.jpg') is True - assert is_test_output_file('test-bulk-run-image-to-image-2.jpg') is True - assert is_test_output_file('test-bulk-run-image-to-image-3.jpg') is True + assert is_test_output_file('test-bulk-run-targets-0.jpg') is True + assert is_test_output_file('test-bulk-run-targets-1.jpg') is True + assert is_test_output_file('test-bulk-run-targets-2.jpg') is False -def test_bulk_run_image_to_video() -> None: - commands = [ sys.executable, 'facefusion.py', 'bulk-run', '--jobs-path', get_test_jobs_directory(), '--processors', 'face_debugger', '-s', get_test_example_file('target-240p-*.jpg'), '-t', get_test_example_file('target-240p.mp4'), '-o', get_test_output_file('test-bulk-run-image-to-video-{index}.mp4') ] +def test_bulk_run_sources_to_targets() -> None: + commands = [ sys.executable, 'facefusion.py', 'bulk-run', '--jobs-path', get_test_jobs_directory(), '-s', get_test_example_file('target-240p-bulk-*.jpg'), '-t', get_test_example_file('target-240p-bulk-*.jpg'), '-o', get_test_output_file('test-bulk-run-sources-to-targets-{index}.jpg') ] assert subprocess.run(commands).returncode == 0 - assert is_test_output_file('test-bulk-run-image-to-video-0.mp4') is True - assert is_test_output_file('test-bulk-run-image-to-video-1.mp4') is True + assert is_test_output_file('test-bulk-run-sources-to-targets-0.jpg') is True + assert is_test_output_file('test-bulk-run-sources-to-targets-1.jpg') is True + assert is_test_output_file('test-bulk-run-sources-to-targets-2.jpg') is True + assert is_test_output_file('test-bulk-run-sources-to-targets-3.jpg') is True + assert is_test_output_file('test-bulk-run-sources-to-targets-4.jpg') is False diff --git a/tests/test_vision.py b/tests/test_vision.py index 8922fe21..f5d2f5b4 100644 --- a/tests/test_vision.py +++ b/tests/test_vision.py @@ -1,6 +1,5 @@ import subprocess -import cv2 import pytest from facefusion.download import conditional_download @@ -18,6 +17,7 @@ def before_all() -> None: ]) subprocess.run([ 'ffmpeg', '-i', get_test_example_file('target-240p.mp4'), '-vframes', '1', get_test_example_file('target-240p.jpg') ]) subprocess.run([ 'ffmpeg', '-i', get_test_example_file('target-1080p.mp4'), '-vframes', '1', get_test_example_file('target-1080p.jpg') ]) + subprocess.run([ 'ffmpeg', '-i', get_test_example_file('target-240p.mp4'), '-vframes', '1', '-vf', 'hue=s=0', get_test_example_file('target-240p-0sat.jpg') ]) subprocess.run([ 'ffmpeg', '-i', get_test_example_file('target-240p.mp4'), '-vframes', '1', '-vf', 'transpose=0', get_test_example_file('target-240p-90deg.jpg') ]) subprocess.run([ 'ffmpeg', '-i', get_test_example_file('target-1080p.mp4'), '-vframes', '1', '-vf', 'transpose=0', get_test_example_file('target-1080p-90deg.jpg') ]) subprocess.run([ 'ffmpeg', '-i', get_test_example_file('target-240p.mp4'), '-vf', 'fps=25', get_test_example_file('target-240p-25fps.mp4') ]) @@ -118,16 +118,16 @@ def test_unpack_resolution() -> None: def test_calc_histogram_difference() -> None: - source_vision_frame = read_image(get_test_example_file('target-1080p.jpg')) - target_vision_frame = cv2.cvtColor(cv2.cvtColor(source_vision_frame, cv2.COLOR_BGR2GRAY), cv2.COLOR_GRAY2BGR) + source_vision_frame = read_image(get_test_example_file('target-240p.jpg')) + target_vision_frame = read_image(get_test_example_file('target-240p-0sat.jpg')) assert calc_histogram_difference(source_vision_frame, source_vision_frame) == 1.0 assert calc_histogram_difference(source_vision_frame, target_vision_frame) < 0.5 def test_match_frame_color() -> None: - source_vision_frame = read_image(get_test_example_file('target-1080p.jpg')) - target_vision_frame = cv2.cvtColor(cv2.cvtColor(source_vision_frame, cv2.COLOR_BGR2GRAY), cv2.COLOR_GRAY2BGR) + source_vision_frame = read_image(get_test_example_file('target-240p.jpg')) + target_vision_frame = read_image(get_test_example_file('target-240p-0sat.jpg')) output_vision_frame = match_frame_color(source_vision_frame, target_vision_frame) assert calc_histogram_difference(source_vision_frame, output_vision_frame) > 0.5