Compare commits

...
39 Commits
Author SHA1 Message Date
harisreedhar 844f22db91 fix rounding bug 2026-07-27 18:33:14 +05:30
harisreedhar 45b8ad93ee fix index bug 2026-07-27 17:35:58 +05:30
HarisreedharandGitHub 66d4cad568 fix update preview bug (#1205)
* fix update preview bug

* fix update preview bug

* remove guard

* add is_vision_frame
2026-07-27 17:34:17 +05:30
7990faa38c Revamp execution provider overrides/adjustments (#1206)
* Split provider hooks into override/adjust with cached CoreML base

Replace the single resolve_inference_providers processor hook with two:
override_inference_providers (full replacement) and adjust_inference_providers
(merge options onto the base providers built by create_inference_providers).
This lets CoreML processors inherit ModelCacheDirectory + SpecializationStrategy
from the base while layering ModelFormat/MLComputeUnits on top.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HTQCZiYjJyUX11bDpbRSiB

* fix caching for execution provider by having override and adjust ways

* fix caching for execution provider by having override and adjust ways

* fix lint

* use proper pytest fixtures

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-27 12:16:33 +02:00
Henry RuhsandGitHub aba04180ed Cleanup/part 3 (#1203)
* remove todo

* sort out workflow, to match upcoming v4

* remove look ahead

* remove core namespace again
2026-07-26 20:01:22 +02:00
henryruhs 3e7645e65c make stream the default 2026-07-26 14:04:47 +02:00
henryruhs 0fc547a975 seek video reader only when trim frame start is larger 0 2026-07-26 10:20:53 +02:00
henryruhs ec4cba0947 move variables 2026-07-26 10:10:16 +02:00
Henry RuhsandGitHub 33ed68679d Cleanup Part 2 (#1202)
* tons of renaming

* tons of renaming

* multi reader approach

* bring tests to an okay-ish state

* bring drain back

* improve read_video_frame speed

* rename method
2026-07-26 10:08:47 +02:00
Henry RuhsandGitHub 585a7d75da Cleanup Part1 (#1201)
* remove some todos, improve video manager, simplify ffmpeg commands and more

* do more

* remove thread count for filters
2026-07-25 11:47:36 +02:00
HarisreedharandGitHub 4a8bfec297 fix windows CI fail (#1200) 2026-07-24 18:32:12 +02:00
HarisreedharandGitHub e25efafef0 margin decouple (#1199) 2026-07-24 21:19:35 +05:30
HarisreedharandGitHub c5f892e76c route chunk read through frame_store (#1198)
* vision integration

* Deleted read_video_chunk + read_static_video_chunk
2026-07-24 16:36:48 +02:00
Henry RuhsandGitHub d249733feb go v4 style for workflow (#1197)
* go v4 style for workflow

* remove some todos
2026-07-24 19:49:23 +05:30
HarisreedharandGitHub a14d688869 route window read through frame_store (#1196)
* route window read through frame_store

* update proper id

* restore todos

* restore todos
2026-07-24 18:22:40 +05:30
HarisreedharandGitHub 8c92608569 Add frame_store module (#1194)
* add frame_store module

* rename and change tests

* rename and update tests
2026-07-24 16:23:54 +05:30
henryruhs 4ee27234f8 fix for ffmpeg7 2026-07-24 09:13:57 +02:00
henryruhs 3602f69207 remove todos that were never needed 2026-07-24 07:53:00 +02:00
henryruhs be77bd6572 switch to ffmpeg.style for audio.py 2026-07-24 07:50:05 +02:00
76c1f79f29 New Video Manager (#1191)
* tiny adjustment for tests

* address the review on the video manager

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tbcd6VWCiU4BQP1gywPr2a

* introduce the stream strategy for the video workflow (#1192)

* introduce the stream strategy for the video workflow

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tbcd6VWCiU4BQP1gywPr2a

* address the review on the stream strategy

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tbcd6VWCiU4BQP1gywPr2a

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>

* annotate the changes for review

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tbcd6VWCiU4BQP1gywPr2a

* annotate the new tests for review

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tbcd6VWCiU4BQP1gywPr2a

* match the temp pixel format help to the locale style

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tbcd6VWCiU4BQP1gywPr2a

* question the set_input_seek naming

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tbcd6VWCiU4BQP1gywPr2a

* question the reader and writer keys

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tbcd6VWCiU4BQP1gywPr2a

* drop the review annotations from the encoder mapping tests

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tbcd6VWCiU4BQP1gywPr2a

* drop the review annotations from the thread count tests

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tbcd6VWCiU4BQP1gywPr2a

* drop the review annotations from the ui files

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tbcd6VWCiU4BQP1gywPr2a

* capture the open review questions as annotations

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tbcd6VWCiU4BQP1gywPr2a

* drop the settled annotations from the types

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tbcd6VWCiU4BQP1gywPr2a

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-23 18:06:40 +02:00
1b151c5c3e compose every test fixture via the builder and run_ffmpeg (#1189)
* compose every test fixture via the builder and run_ffmpeg

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tbcd6VWCiU4BQP1gywPr2a

* use loops in tests for ffmpeg stuff

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-23 14:54:50 +02:00
a515d7fc3f restrict hdr color transfer and tag the merge output as bt709 (#1188)
* restrict hdr color transfer and tag the merge output as bt709

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tbcd6VWCiU4BQP1gywPr2a

* restrict hdr color transfer and tag the merge output as bt709

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tbcd6VWCiU4BQP1gywPr2a

* full video migration

* compose the hdr fixture via the builder chain

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tbcd6VWCiU4BQP1gywPr2a

* compose the test fixtures via the builder and run_ffmpeg

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tbcd6VWCiU4BQP1gywPr2a

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-23 14:38:22 +02:00
4d92e8b5b7 introduce workflow-mode and workflow-strategy like next major (#1187)
Claude-Session: https://claude.ai/code/session_01Tbcd6VWCiU4BQP1gywPr2a

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-23 13:25:13 +02:00
74bfb8c297 adopt the workflow task vocabulary from next major (#1185)
Claude-Session: https://claude.ai/code/session_01Tbcd6VWCiU4BQP1gywPr2a

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-23 12:56:50 +02:00
a982ed9f61 probe video metadata via ffprobe in vision (#1184)
* probe video metadata via ffprobe in vision

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tbcd6VWCiU4BQP1gywPr2a

* probe video metadata via ffprobe in vision

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tbcd6VWCiU4BQP1gywPr2a

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-23 12:34:32 +02:00
fe4aedec4d introduce ffprobe and ffprobe_builder (#1182)
* introduce ffprobe and ffprobe_builder

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tbcd6VWCiU4BQP1gywPr2a

* introduce ffprobe and ffprobe_builder

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tbcd6VWCiU4BQP1gywPr2a

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-23 11:37:54 +02:00
a1e9a4180b drop keep_temp and the common options component (#1180)
Claude-Session: https://claude.ai/code/session_01Tbcd6VWCiU4BQP1gywPr2a

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-23 11:16:49 +02:00
Henry RuhsandGitHub 3702f2ec99 unify the dependency checks in pre_check and add ffprobe (#1181) 2026-07-23 11:16:00 +02:00
henryruhs fb4c11983d mark as next 2026-07-23 11:06:47 +02:00
Henry RuhsandGitHub 3f81a8a784 patch-3.7.1 (#1178)
* patch 3.7.1

* fix bug for 2 processors on image to image pipeline (#1177)

* reduce default value of target_frame_amount

* restore old performance

* restore old performance

* restore old performance

* restore old performance
2026-07-05 16:43:36 +02:00
henryruhs b8f80460cf last minute change to frame distribution 2026-06-30 16:23:09 +02:00
henryruhs 5305968de7 fix wording 2026-06-30 16:09:22 +02:00
henryruhs b6d740e64a fix wording 2026-06-30 16:03:42 +02:00
henryruhs 75bd742858 update preview 2026-06-30 15:12:26 +02:00
a2cbfd73b1 3.7.0 (#1175)
* mark as next, introduce dynamic scale for face debugger

* use latest onnxruntime

* update within Gradio 5

* Remove system memory limit (#986)

* remove system memory limit from ui

* remove system memory limit from args.py

* flatten the face store

* prevent countless importlib.import_module calls

* remove --onnxruntime from install.py

* remove --onnxruntime from install.py

* resolve static inference providers to fix macos (#1127)

* resolve static inference providers to fix macos

* fix lint

* restore old behaviour

* restore old behaviour

* handle ghost and uniface as well

* adjust condition for ghost and uniface

* fix Gradio gallery styles

* remove face store (#1132)

* fix dataflow in streamer

* Face selector auto mode (#1137)

* introduce face selector auto mode

* introduce face selector auto mode

* introduce face selector auto mode

* correct way is to pass source_vision_frames

* make the world a better place

* fix dataflow in faceswapper, no read of files withing inner methods (#1148)

* fix dataflow in faceswapper, no read of files withing inner methods

* fix lint

* adjust code more

* adjust code more

* bring back the face store but for source and reference only (#1149)

* bring back the face store but for source and reference only

* fix ci

* minor improvement

* guard for tobytes()

* drop condition in select_faces()

* Replace CONFIG_PARSER global with @lru_cache (#1147)

* remove global config_parser

* fix import order

* remove lambda

* remove unused block

* optimize app context detection

* decouple common modules from core (#1152)

* decouple common modules from core

* remove that nonsense

* remove that nonsense

* minor adjustment to workflows

* Tag HEVC output as hvc1 and move moov atom to the front (#1153)

* Tag HEVC output as hvc1 and move moov atom to the front

ffmpeg defaults HEVC in MP4 to the 'hev1' sample entry and leaves the moov
atom at the tail. Apple players (QuickTime, Finder QuickLook) refuse to decode
'hev1' and stall reading a tail-placed moov on large files, so hevc_nvenc /
libx265 renders cannot be previewed on macOS.

- add ffmpeg_builder.set_video_tag(): emit `-tag:v hvc1` for every HEVC
  encoder (libx265, hevc_nvenc, hevc_amf, hevc_qsv, hevc_videotoolbox).
  Applied in merge_video where the encoder is known; `-c:v copy` in the audio
  mux / concat steps preserves the tag.
- add ffmpeg_builder.set_faststart(): emit `-movflags +faststart`, applied in
  restore_audio / replace_audio / concat_video which write the final output.

H.264 and other codecs are left untouched. Verified on a real hevc_nvenc
render: hev1 hung QuickLook (no thumbnail); after the patch the file is hvc1
with a front-placed moov and QuickLook generates a thumbnail.

* Restrict hvc1 tag and faststart to quicktime containers

Gate set_video_tag / set_faststart on the output container format
(m4v, mov, mp4) via get_file_format(), so non-quicktime muxers no longer
receive -tag:v hvc1 / -movflags +faststart. Trim test_set_video_tag to a
single positive and negative assertion.

Addresses review on #1153.

* Move hvc1 tag and faststart gates into ffmpeg_builder

Rename set_video_tag / set_faststart to conditional_* and push the
container-format gate (m4v, mov, mp4) inside the builders, keeping
ffmpeg.py free of inline conditionals. Matches the set_image_quality
pattern. Addresses review on #1153.

* post cleanup after merge

* Pack target frames (#1158)

* pack target frames

* add todos

* add todos, resolve todos

* resolve todos

* change names

* revert to single target frame for select faces

* fix lint

* return empty frame

* get() have no default

* Fix trim (#1162)

* fix trim

* fix trim

* rename ffmpeg builder method

* rename to temp_frame_set and temp_frame_pattern

---------

Co-authored-by: harisreedhar <h4harisreedhar.s.s@gmail.com>
Co-authored-by: Harisreedhar <46858047+harisreedhar@users.noreply.github.com>

* Implement face tracker (#1163)

* add face tracker

* change get_nearest_track_face -> get_nearest_track_index

* create face_creator.py and move methods around

* add type FaceTrack

* naming

* remove iou test, don't belong there

* fix spaces

* rename to interpolate_points

* rename to find_best_face_track

* just track_faces

* cleanp

* previous next naming

* remove >= and >=

* rename

* remove helper from test and use face from source.jpg

* make get_anchor_indices more readable

* track_faces() call before and is forwarded to select_faces

* change to interpolate_faces

* rename methods

* rename methods

* rename variables

* remove dtype

* move face_anlyser -> face_creator

* claenup face_creator.py

* move tests to dedicated test face detector

* move tracking inside select_faces

* simplify face_tracker (#1165)

* minor renaming

* improve face_tracker test (#1166)

* improve face_tracker test

* cleanup

* Add target frame amount (#1167)

* introduce --target-frame-amount

* add ui

* make track_faces conditional

* update choices.py

* fix []

* rename component file to frame_process.py

* fix track preview (#1168)

* introduce face origin (#1169)

* add guard to prevent failure

* show and hide voice extractor according to lip syncer

* rename average_face_coordinates to average_face_geometry

* use static faces for select_faces()

* face store with lock (#1171)

* face store with lock

* face store with lock

* remove refill color from bbox

* adjust tests and handle frame_position proper way

* enforce similar naming

* introduce face tracker score

* introduce face tracker score

* fix/audio-trim-alignment (#1173)

* fix audio offset

* fix audio offset

* remove reference_frame_number check

---------

Co-authored-by: harisreedhar <h4harisreedhar.s.s@gmail.com>

* reduce face tracker score from 0 to 0.5

* mark as 3.7.0

* make face tracker stateless

---------

Co-authored-by: Harisreedhar <46858047+harisreedhar@users.noreply.github.com>
Co-authored-by: kazuki nakai <kazuki.nakai@agiletec.net>
Co-authored-by: harisreedhar <h4harisreedhar.s.s@gmail.com>
2026-06-30 15:00:02 +02:00
Henry RuhsandGitHub 5b7d145aa7 Patch 3.6.1 (#1078)
* Avast intercepts ssl cert and breaks curl

* modernize dependencies

* fix sanitizer for int range

* comment out tests

* disable testing for CI

* disable testing for CI
2026-04-19 21:17:39 +02:00
Henry RuhsandGitHub 519360bcd6 Update LICENSE.md 2026-04-01 09:10:08 +02:00
57fcb86b82 3.6.0 (#1062)
* mark as next

* add fran model

* add support for corridor key (#1060)

* introduce despill color

* simplify the apply dispill color

* finalize naming for both fill and despill

* follow vision_frame convension

* patch fran model

* adjust fran urls

* Feat/dynamic env setup (#1061)

* dynamic environment setup

* dynamic environment setup

* fix fran model

* prevent directml using incompatible corridor_key model

* fix environment setup for windows

* switch to corridor_key_1024 and corridor_key_2048

* switch to corridor_key_1024 and corridor_key_2048

* mark it as 3.6.0

* rename environment to conda

* rename environment to conda

* fix testing for face analyser

* some background remove cosmetics

* some background remove cosmetics

* some background remove cosmetics

* update preview

---------

Co-authored-by: harisreedhar <h4harisreedhar.s.s@gmail.com>
2026-03-16 15:08:43 +01:00
henryruhs 2cc05d4fba update licenses 2026-03-09 21:24:53 +01:00
121 changed files with 3587 additions and 1265 deletions
Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 MiB

After

Width:  |  Height:  |  Size: 1.1 MiB

+2 -2
View File
@@ -33,7 +33,7 @@ jobs:
uses: actions/setup-python@v5 uses: actions/setup-python@v5
with: with:
python-version: '3.12' python-version: '3.12'
- run: python install.py --onnxruntime default --skip-conda - run: python install.py default --skip-conda
- run: pip install pytest - run: pip install pytest
- run: pytest - run: pytest
report: report:
@@ -48,7 +48,7 @@ jobs:
uses: actions/setup-python@v5 uses: actions/setup-python@v5
with: with:
python-version: '3.12' python-version: '3.12'
- run: python install.py --onnxruntime default --skip-conda - run: python install.py default --skip-conda
- run: pip install coveralls - run: pip install coveralls
- run: pip install pytest - run: pip install pytest
- run: pip install pytest-cov - run: pip install pytest-cov
+1 -1
View File
@@ -1,3 +1,3 @@
OpenRAIL-AS license OpenRAIL-AS license
Copyright (c) 2025 Henry Ruhs Copyright (c) 2026 Henry Ruhs
+13 -3
View File
@@ -1,3 +1,7 @@
[workflow]
workflow_mode =
workflow_strategy =
[paths] [paths]
temp_path = temp_path =
jobs_path = jobs_path =
@@ -32,6 +36,9 @@ reference_face_position =
reference_face_distance = reference_face_distance =
reference_frame_number = reference_frame_number =
[face_tracker]
face_tracker_score =
[face_masker] [face_masker]
face_occluder_model = face_occluder_model =
face_parser_model = face_parser_model =
@@ -48,7 +55,10 @@ voice_extractor_model =
trim_frame_start = trim_frame_start =
trim_frame_end = trim_frame_end =
temp_frame_format = temp_frame_format =
keep_temp = temp_pixel_format =
[frame_distribution]
target_frame_amount =
[output_creation] [output_creation]
output_image_quality = output_image_quality =
@@ -67,7 +77,8 @@ processors =
age_modifier_model = age_modifier_model =
age_modifier_direction = age_modifier_direction =
background_remover_model = background_remover_model =
background_remover_color = background_remover_fill_color =
background_remover_despill_color =
deep_swapper_model = deep_swapper_model =
deep_swapper_morph = deep_swapper_morph =
expression_restorer_model = expression_restorer_model =
@@ -124,7 +135,6 @@ execution_thread_count =
[memory] [memory]
video_memory_strategy = video_memory_strategy =
system_memory_limit =
[misc] [misc]
log_level = log_level =
+2 -1
View File
@@ -4,7 +4,8 @@ import os
os.environ['OMP_NUM_THREADS'] = '1' os.environ['OMP_NUM_THREADS'] = '1'
from facefusion import core from facefusion import conda, core
if __name__ == '__main__': if __name__ == '__main__':
conda.setup()
core.cli() core.cli()
+4 -2
View File
@@ -5,12 +5,14 @@ from facefusion.types import AppContext
def detect_app_context() -> AppContext: def detect_app_context() -> AppContext:
jobs_path = os.path.join('facefusion', 'jobs')
uis_path = os.path.join('facefusion', 'uis')
frame = sys._getframe(1) frame = sys._getframe(1)
while frame: while frame:
if os.path.join('facefusion', 'jobs') in frame.f_code.co_filename: if jobs_path in frame.f_code.co_filename:
return 'cli' return 'cli'
if os.path.join('facefusion', 'uis') in frame.f_code.co_filename: if uis_path in frame.f_code.co_filename:
return 'ui' return 'ui'
frame = frame.f_back frame = frame.f_back
return 'cli' return 'cli'
+5 -2
View File
@@ -9,6 +9,8 @@ from facefusion.vision import detect_video_fps
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None: def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('command', args.get('command')) apply_state_item('command', args.get('command'))
apply_state_item('workflow_mode', args.get('workflow_mode'))
apply_state_item('workflow_strategy', args.get('workflow_strategy'))
apply_state_item('temp_path', args.get('temp_path')) apply_state_item('temp_path', args.get('temp_path'))
apply_state_item('jobs_path', args.get('jobs_path')) apply_state_item('jobs_path', args.get('jobs_path'))
apply_state_item('source_paths', args.get('source_paths')) apply_state_item('source_paths', args.get('source_paths'))
@@ -33,6 +35,7 @@ def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('reference_face_position', args.get('reference_face_position')) apply_state_item('reference_face_position', args.get('reference_face_position'))
apply_state_item('reference_face_distance', args.get('reference_face_distance')) apply_state_item('reference_face_distance', args.get('reference_face_distance'))
apply_state_item('reference_frame_number', args.get('reference_frame_number')) apply_state_item('reference_frame_number', args.get('reference_frame_number'))
apply_state_item('face_tracker_score', args.get('face_tracker_score'))
apply_state_item('face_occluder_model', args.get('face_occluder_model')) apply_state_item('face_occluder_model', args.get('face_occluder_model'))
apply_state_item('face_parser_model', args.get('face_parser_model')) apply_state_item('face_parser_model', args.get('face_parser_model'))
apply_state_item('face_mask_types', args.get('face_mask_types')) apply_state_item('face_mask_types', args.get('face_mask_types'))
@@ -44,7 +47,8 @@ def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('trim_frame_start', args.get('trim_frame_start')) apply_state_item('trim_frame_start', args.get('trim_frame_start'))
apply_state_item('trim_frame_end', args.get('trim_frame_end')) apply_state_item('trim_frame_end', args.get('trim_frame_end'))
apply_state_item('temp_frame_format', args.get('temp_frame_format')) apply_state_item('temp_frame_format', args.get('temp_frame_format'))
apply_state_item('keep_temp', args.get('keep_temp')) apply_state_item('temp_pixel_format', args.get('temp_pixel_format'))
apply_state_item('target_frame_amount', args.get('target_frame_amount'))
apply_state_item('output_image_quality', args.get('output_image_quality')) apply_state_item('output_image_quality', args.get('output_image_quality'))
apply_state_item('output_image_scale', args.get('output_image_scale')) apply_state_item('output_image_scale', args.get('output_image_scale'))
apply_state_item('output_audio_encoder', args.get('output_audio_encoder')) apply_state_item('output_audio_encoder', args.get('output_audio_encoder'))
@@ -77,7 +81,6 @@ def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('benchmark_resolutions', args.get('benchmark_resolutions')) apply_state_item('benchmark_resolutions', args.get('benchmark_resolutions'))
apply_state_item('benchmark_cycle_count', args.get('benchmark_cycle_count')) apply_state_item('benchmark_cycle_count', args.get('benchmark_cycle_count'))
apply_state_item('video_memory_strategy', args.get('video_memory_strategy')) apply_state_item('video_memory_strategy', args.get('video_memory_strategy'))
apply_state_item('system_memory_limit', args.get('system_memory_limit'))
apply_state_item('log_level', args.get('log_level')) apply_state_item('log_level', args.get('log_level'))
apply_state_item('halt_on_error', args.get('halt_on_error')) apply_state_item('halt_on_error', args.get('halt_on_error'))
apply_state_item('job_id', args.get('job_id')) apply_state_item('job_id', args.get('job_id'))
+3 -3
View File
@@ -5,7 +5,7 @@ import numpy
import scipy import scipy
from numpy.typing import NDArray from numpy.typing import NDArray
from facefusion.ffmpeg import read_audio_buffer from facefusion import ffmpeg
from facefusion.filesystem import is_audio from facefusion.filesystem import is_audio
from facefusion.types import Audio, AudioFrame, Fps, Mel, MelFilterBank, Spectrogram from facefusion.types import Audio, AudioFrame, Fps, Mel, MelFilterBank, Spectrogram
from facefusion.voice_extractor import batch_extract_voice from facefusion.voice_extractor import batch_extract_voice
@@ -22,7 +22,7 @@ def read_audio(audio_path : str, fps : Fps) -> Optional[List[AudioFrame]]:
audio_channel_total = 2 audio_channel_total = 2
if is_audio(audio_path): if is_audio(audio_path):
audio_buffer = read_audio_buffer(audio_path, audio_sample_rate, audio_sample_size, audio_channel_total) audio_buffer = ffmpeg.read_audio_buffer(audio_path, audio_sample_rate, audio_sample_size, audio_channel_total)
audio = numpy.frombuffer(audio_buffer, dtype = numpy.int16).reshape(-1, 2) audio = numpy.frombuffer(audio_buffer, dtype = numpy.int16).reshape(-1, 2)
audio = prepare_audio(audio) audio = prepare_audio(audio)
spectrogram = create_spectrogram(audio) spectrogram = create_spectrogram(audio)
@@ -44,7 +44,7 @@ def read_voice(audio_path : str, fps : Fps) -> Optional[List[AudioFrame]]:
voice_step_size = 180 * 1024 voice_step_size = 180 * 1024
if is_audio(audio_path): if is_audio(audio_path):
audio_buffer = read_audio_buffer(audio_path, voice_sample_rate, voice_sample_size, voice_channel_total) audio_buffer = ffmpeg.read_audio_buffer(audio_path, voice_sample_rate, voice_sample_size, voice_channel_total)
audio = numpy.frombuffer(audio_buffer, dtype = numpy.int16).reshape(-1, 2) audio = numpy.frombuffer(audio_buffer, dtype = numpy.int16).reshape(-1, 2)
audio = batch_extract_voice(audio, voice_chunk_size, voice_step_size) audio = batch_extract_voice(audio, voice_chunk_size, voice_step_size)
audio = prepare_voice(audio) audio = prepare_voice(audio)
+2 -2
View File
@@ -9,7 +9,7 @@ import facefusion.choices
from facefusion import content_analyser, core, state_manager from facefusion import content_analyser, core, state_manager
from facefusion.cli_helper import render_table from facefusion.cli_helper import render_table
from facefusion.download import conditional_download, resolve_download_url from facefusion.download import conditional_download, resolve_download_url
from facefusion.face_store import clear_static_faces from facefusion.face_store import clear_faces
from facefusion.filesystem import get_file_extension from facefusion.filesystem import get_file_extension
from facefusion.types import BenchmarkCycleSet from facefusion.types import BenchmarkCycleSet
from facefusion.vision import count_video_frame_total, detect_video_fps from facefusion.vision import count_video_frame_total, detect_video_fps
@@ -64,7 +64,7 @@ def cycle(cycle_count : int) -> BenchmarkCycleSet:
if state_manager.get_item('benchmark_mode') == 'cold': if state_manager.get_item('benchmark_mode') == 'cold':
content_analyser.analyse_image.cache_clear() content_analyser.analyse_image.cache_clear()
content_analyser.analyse_video.cache_clear() content_analyser.analyse_video.cache_clear()
clear_static_faces() clear_faces()
start_time = perf_counter() start_time = perf_counter()
core.conditional_process() core.conditional_process()
+11 -4
View File
@@ -2,7 +2,7 @@ import logging
from typing import List, Sequence, get_args from typing import List, Sequence, get_args
from facefusion.common_helper import create_float_range, create_int_range from facefusion.common_helper import create_float_range, create_int_range
from facefusion.types import Angle, AudioEncoder, AudioFormat, AudioTypeSet, BenchmarkMode, BenchmarkResolution, BenchmarkSet, DownloadProvider, DownloadProviderSet, DownloadScope, EncoderSet, ExecutionProvider, ExecutionProviderSet, FaceDetectorModel, FaceDetectorSet, FaceLandmarkerModel, FaceMaskArea, FaceMaskAreaSet, FaceMaskRegion, FaceMaskRegionSet, FaceMaskType, FaceOccluderModel, FaceParserModel, FaceSelectorMode, FaceSelectorOrder, Gender, ImageFormat, ImageTypeSet, JobStatus, LogLevel, LogLevelSet, Race, Score, TempFrameFormat, UiWorkflow, VideoEncoder, VideoFormat, VideoMemoryStrategy, VideoPreset, VideoTypeSet, VoiceExtractorModel from facefusion.types import Angle, AudioEncoder, AudioFormat, AudioTypeSet, BenchmarkMode, BenchmarkResolution, BenchmarkSet, DownloadProvider, DownloadProviderSet, DownloadScope, EncoderSet, ExecutionProvider, ExecutionProviderSet, FaceDetectorModel, FaceDetectorSet, FaceLandmarkerModel, FaceMaskArea, FaceMaskAreaSet, FaceMaskRegion, FaceMaskRegionSet, FaceMaskType, FaceOccluderModel, FaceParserModel, FaceSelectorGender, FaceSelectorMode, FaceSelectorOrder, FaceSelectorRace, Gender, ImageFormat, ImageTypeSet, JobStatus, LogLevel, LogLevelSet, Race, Score, TempFrameFormat, TempPixelFormat, UiWorkflow, VideoEncoder, VideoFormat, VideoMemoryStrategy, VideoPreset, VideoTypeSet, VoiceExtractorModel, WorkflowMode, WorkflowStrategy
face_detector_set : FaceDetectorSet =\ face_detector_set : FaceDetectorSet =\
{ {
@@ -16,8 +16,10 @@ face_detector_models : List[FaceDetectorModel] = list(get_args(FaceDetectorModel
face_landmarker_models : List[FaceLandmarkerModel] = list(get_args(FaceLandmarkerModel)) face_landmarker_models : List[FaceLandmarkerModel] = list(get_args(FaceLandmarkerModel))
face_selector_modes : List[FaceSelectorMode] = list(get_args(FaceSelectorMode)) face_selector_modes : List[FaceSelectorMode] = list(get_args(FaceSelectorMode))
face_selector_orders : List[FaceSelectorOrder] = list(get_args(FaceSelectorOrder)) face_selector_orders : List[FaceSelectorOrder] = list(get_args(FaceSelectorOrder))
face_selector_genders : List[Gender] = list(get_args(Gender)) genders : List[Gender] = list(get_args(Gender))
face_selector_races : List[Race] = list(get_args(Race)) races : List[Race] = list(get_args(Race))
face_selector_genders : List[FaceSelectorGender] = list(get_args(FaceSelectorGender))
face_selector_races : List[FaceSelectorRace] = list(get_args(FaceSelectorRace))
face_occluder_models : List[FaceOccluderModel] = list(get_args(FaceOccluderModel)) face_occluder_models : List[FaceOccluderModel] = list(get_args(FaceOccluderModel))
face_parser_models : List[FaceParserModel] = list(get_args(FaceParserModel)) face_parser_models : List[FaceParserModel] = list(get_args(FaceParserModel))
face_mask_types : List[FaceMaskType] = list(get_args(FaceMaskType)) face_mask_types : List[FaceMaskType] = list(get_args(FaceMaskType))
@@ -74,10 +76,14 @@ video_type_set : VideoTypeSet =\
'webm': 'video/webm', 'webm': 'video/webm',
'wmv': 'video/x-ms-wmv' 'wmv': 'video/x-ms-wmv'
} }
workflow_modes : List[WorkflowMode] = list(get_args(WorkflowMode))
workflow_strategies : List[WorkflowStrategy] = list(get_args(WorkflowStrategy))
audio_formats : List[AudioFormat] = list(get_args(AudioFormat)) audio_formats : List[AudioFormat] = list(get_args(AudioFormat))
image_formats : List[ImageFormat] = list(get_args(ImageFormat)) image_formats : List[ImageFormat] = list(get_args(ImageFormat))
video_formats : List[VideoFormat] = list(get_args(VideoFormat)) video_formats : List[VideoFormat] = list(get_args(VideoFormat))
temp_frame_formats : List[TempFrameFormat] = list(get_args(TempFrameFormat)) temp_frame_formats : List[TempFrameFormat] = list(get_args(TempFrameFormat))
temp_pixel_formats : List[TempPixelFormat] = list(get_args(TempPixelFormat))
output_audio_encoders : List[AudioEncoder] = list(get_args(AudioEncoder)) output_audio_encoders : List[AudioEncoder] = list(get_args(AudioEncoder))
output_video_encoders : List[VideoEncoder] = list(get_args(VideoEncoder)) output_video_encoders : List[VideoEncoder] = list(get_args(VideoEncoder))
@@ -153,7 +159,6 @@ job_statuses : List[JobStatus] = list(get_args(JobStatus))
benchmark_cycle_count_range : Sequence[int] = create_int_range(1, 10, 1) benchmark_cycle_count_range : Sequence[int] = create_int_range(1, 10, 1)
execution_thread_count_range : Sequence[int] = create_int_range(1, 32, 1) execution_thread_count_range : Sequence[int] = create_int_range(1, 32, 1)
system_memory_limit_range : Sequence[int] = create_int_range(0, 128, 4)
face_detector_margin_range : Sequence[int] = create_int_range(0, 100, 1) face_detector_margin_range : Sequence[int] = create_int_range(0, 100, 1)
face_detector_angles : Sequence[Angle] = create_int_range(0, 270, 90) face_detector_angles : Sequence[Angle] = create_int_range(0, 270, 90)
face_detector_score_range : Sequence[Score] = create_float_range(0.0, 1.0, 0.05) face_detector_score_range : Sequence[Score] = create_float_range(0.0, 1.0, 0.05)
@@ -162,6 +167,8 @@ face_mask_blur_range : Sequence[float] = create_float_range(0.0, 1.0, 0.05)
face_mask_padding_range : Sequence[int] = create_int_range(0, 100, 1) face_mask_padding_range : Sequence[int] = create_int_range(0, 100, 1)
face_selector_age_range : Sequence[int] = create_int_range(0, 100, 1) face_selector_age_range : Sequence[int] = create_int_range(0, 100, 1)
reference_face_distance_range : Sequence[float] = create_float_range(0.0, 1.0, 0.05) reference_face_distance_range : Sequence[float] = create_float_range(0.0, 1.0, 0.05)
face_tracker_score_range : Sequence[Score] = create_float_range(0.0, 0.5, 0.05)
target_frame_amount_range : Sequence[int] = create_int_range(0, 10, 1)
output_image_quality_range : Sequence[int] = create_int_range(0, 100, 1) output_image_quality_range : Sequence[int] = create_int_range(0, 100, 1)
output_image_scale_range : Sequence[float] = create_float_range(0.25, 8.0, 0.25) output_image_scale_range : Sequence[float] = create_float_range(0.25, 8.0, 0.25)
output_audio_quality_range : Sequence[int] = create_int_range(0, 100, 1) output_audio_quality_range : Sequence[int] = create_int_range(0, 100, 1)
+6
View File
@@ -78,6 +78,12 @@ def get_first(__list__ : Any) -> Any:
return None return None
def get_middle(__list__ : Any) -> Any:
if isinstance(__list__, Sequence) and __list__:
return __list__[len(__list__) // 2]
return None
def get_last(__list__ : Any) -> Any: def get_last(__list__ : Any) -> Any:
if isinstance(__list__, Reversible): if isinstance(__list__, Reversible):
return next(reversed(__list__), None) return next(reversed(__list__), None)
+41
View File
@@ -0,0 +1,41 @@
import os
import sys
from typing import List
from facefusion.common_helper import is_linux, is_windows
def setup() -> None:
conda_prefix = os.getenv('CONDA_PREFIX')
conda_ready = os.getenv('CONDA_READY')
if conda_prefix and not conda_ready:
if is_linux():
python_id = 'python' + str(sys.version_info.major) + '.' + str(sys.version_info.minor)
library_paths : List[str] =\
[
os.path.join(conda_prefix, 'lib'),
os.path.join(conda_prefix, 'lib', python_id, 'site-packages', 'tensorrt_libs')
]
library_paths = list(filter(os.path.exists, library_paths))
if library_paths:
if os.getenv('LD_LIBRARY_PATH'):
library_paths.append(os.getenv('LD_LIBRARY_PATH'))
os.environ['LD_LIBRARY_PATH'] = os.pathsep.join(library_paths)
os.environ['CONDA_READY'] = '1'
os.execv(sys.executable, [ sys.executable ] + sys.argv)
if is_windows():
library_paths =\
[
os.path.join(conda_prefix, 'Lib'),
os.path.join(conda_prefix, 'Lib', 'site-packages', 'tensorrt_libs')
]
library_paths = list(filter(os.path.exists, library_paths))
if library_paths:
if os.getenv('PATH'):
library_paths.append(os.getenv('PATH'))
os.environ['PATH'] = os.pathsep.join(library_paths)
os.environ['CONDA_READY'] = '1'
+12 -21
View File
@@ -1,29 +1,20 @@
from configparser import ConfigParser from configparser import ConfigParser
from functools import lru_cache
from typing import List, Optional from typing import List, Optional
from facefusion import state_manager from facefusion import state_manager
from facefusion.common_helper import cast_bool, cast_float, cast_int from facefusion.common_helper import cast_bool, cast_float, cast_int
CONFIG_PARSER = None
@lru_cache
def get_config_parser() -> ConfigParser: def get_static_config_parser() -> ConfigParser:
global CONFIG_PARSER config_parser = ConfigParser()
config_parser.read(state_manager.get_item('config_path'), encoding = 'utf-8')
if CONFIG_PARSER is None: return config_parser
CONFIG_PARSER = ConfigParser()
CONFIG_PARSER.read(state_manager.get_item('config_path'), encoding = 'utf-8')
return CONFIG_PARSER
def clear_config_parser() -> None:
global CONFIG_PARSER
CONFIG_PARSER = None
def get_str_value(section : str, option : str, fallback : Optional[str] = None) -> Optional[str]: def get_str_value(section : str, option : str, fallback : Optional[str] = None) -> Optional[str]:
config_parser = get_config_parser() config_parser = get_static_config_parser()
if config_parser.has_option(section, option) and config_parser.get(section, option).strip(): if config_parser.has_option(section, option) and config_parser.get(section, option).strip():
return config_parser.get(section, option) return config_parser.get(section, option)
@@ -31,7 +22,7 @@ def get_str_value(section : str, option : str, fallback : Optional[str] = None)
def get_int_value(section : str, option : str, fallback : Optional[str] = None) -> Optional[int]: def get_int_value(section : str, option : str, fallback : Optional[str] = None) -> Optional[int]:
config_parser = get_config_parser() config_parser = get_static_config_parser()
if config_parser.has_option(section, option) and config_parser.get(section, option).strip(): if config_parser.has_option(section, option) and config_parser.get(section, option).strip():
return config_parser.getint(section, option) return config_parser.getint(section, option)
@@ -39,7 +30,7 @@ def get_int_value(section : str, option : str, fallback : Optional[str] = None)
def get_float_value(section : str, option : str, fallback : Optional[str] = None) -> Optional[float]: def get_float_value(section : str, option : str, fallback : Optional[str] = None) -> Optional[float]:
config_parser = get_config_parser() config_parser = get_static_config_parser()
if config_parser.has_option(section, option) and config_parser.get(section, option).strip(): if config_parser.has_option(section, option) and config_parser.get(section, option).strip():
return config_parser.getfloat(section, option) return config_parser.getfloat(section, option)
@@ -47,7 +38,7 @@ def get_float_value(section : str, option : str, fallback : Optional[str] = None
def get_bool_value(section : str, option : str, fallback : Optional[str] = None) -> Optional[bool]: def get_bool_value(section : str, option : str, fallback : Optional[str] = None) -> Optional[bool]:
config_parser = get_config_parser() config_parser = get_static_config_parser()
if config_parser.has_option(section, option) and config_parser.get(section, option).strip(): if config_parser.has_option(section, option) and config_parser.get(section, option).strip():
return config_parser.getboolean(section, option) return config_parser.getboolean(section, option)
@@ -55,7 +46,7 @@ def get_bool_value(section : str, option : str, fallback : Optional[str] = None)
def get_str_list(section : str, option : str, fallback : Optional[str] = None) -> Optional[List[str]]: def get_str_list(section : str, option : str, fallback : Optional[str] = None) -> Optional[List[str]]:
config_parser = get_config_parser() config_parser = get_static_config_parser()
if config_parser.has_option(section, option) and config_parser.get(section, option).strip(): if config_parser.has_option(section, option) and config_parser.get(section, option).strip():
return config_parser.get(section, option).split() return config_parser.get(section, option).split()
@@ -65,7 +56,7 @@ def get_str_list(section : str, option : str, fallback : Optional[str] = None) -
def get_int_list(section : str, option : str, fallback : Optional[str] = None) -> Optional[List[int]]: def get_int_list(section : str, option : str, fallback : Optional[str] = None) -> Optional[List[int]]:
config_parser = get_config_parser() config_parser = get_static_config_parser()
if config_parser.has_option(section, option) and config_parser.get(section, option).strip(): if config_parser.has_option(section, option) and config_parser.get(section, option).strip():
return list(map(int, config_parser.get(section, option).split())) return list(map(int, config_parser.get(section, option).split()))
+15 -17
View File
@@ -1,17 +1,15 @@
from functools import lru_cache from functools import lru_cache
from typing import List, Tuple from typing import Tuple
import numpy import numpy
from tqdm import tqdm from tqdm import tqdm
from facefusion import inference_manager, state_manager, translator from facefusion import inference_manager, state_manager, translator, video_manager
from facefusion.common_helper import is_macos
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
from facefusion.execution import has_execution_provider
from facefusion.filesystem import resolve_relative_path from facefusion.filesystem import resolve_relative_path
from facefusion.thread_helper import conditional_thread_semaphore from facefusion.thread_helper import conditional_thread_semaphore
from facefusion.types import Detection, DownloadScope, DownloadSet, ExecutionProvider, Fps, InferencePool, ModelSet, VisionFrame from facefusion.types import Detection, DownloadScope, DownloadSet, Fps, InferencePool, ModelSet, VisionFrame
from facefusion.vision import detect_video_fps, fit_contain_frame, read_image, read_video_frame from facefusion.vision import detect_video_fps, fit_contain_frame, read_image
STREAM_COUNTER = 0 STREAM_COUNTER = 0
@@ -119,12 +117,6 @@ def clear_inference_pool() -> None:
inference_manager.clear_inference_pool(__name__, model_names) inference_manager.clear_inference_pool(__name__, model_names)
def resolve_execution_providers() -> List[ExecutionProvider]:
if is_macos() and has_execution_provider('coreml'):
return [ 'cpu' ]
return state_manager.get_item('execution_providers')
def collect_model_downloads() -> Tuple[DownloadSet, DownloadSet]: def collect_model_downloads() -> Tuple[DownloadSet, DownloadSet]:
model_set = create_static_model_set('full') model_set = create_static_model_set('full')
model_hash_set = {} model_hash_set = {}
@@ -166,19 +158,25 @@ def analyse_image(image_path : str) -> bool:
def analyse_video(video_path : str, trim_frame_start : int, trim_frame_end : int) -> bool: def analyse_video(video_path : str, trim_frame_start : int, trim_frame_end : int) -> bool:
video_fps = detect_video_fps(video_path) video_fps = detect_video_fps(video_path)
frame_range = range(trim_frame_start, trim_frame_end) frame_range = range(trim_frame_start, trim_frame_end)
video_reader = video_manager.get_reader(video_path, 'analyse_video')
rate = 0.0 rate = 0.0
total = 0 total = 0
counter = 0 counter = 0
if trim_frame_start > 0:
video_manager.seek_video_reader(video_reader, trim_frame_start)
with tqdm(total = len(frame_range), desc = translator.get('analysing'), unit = 'frame', ascii = ' =', disable = state_manager.get_item('log_level') in [ 'warn', 'error' ]) as progress: with tqdm(total = len(frame_range), desc = translator.get('analysing'), unit = 'frame', ascii = ' =', disable = state_manager.get_item('log_level') in [ 'warn', 'error' ]) as progress:
for frame_number in frame_range: for frame_number in frame_range:
if frame_number % int(video_fps) == 0: vision_frame = video_manager.read_video_frame(video_reader)
vision_frame = read_video_frame(video_path, frame_number)
total += 1
if analyse_frame(vision_frame): if frame_number % int(video_fps) == 0:
counter += 1 if numpy.any(vision_frame):
total += 1
if analyse_frame(vision_frame):
counter += 1
if counter > 0 and total > 0: if counter > 0 and total > 0:
rate = counter / total * 100 rate = counter / total * 100
+28 -42
View File
@@ -5,18 +5,17 @@ import signal
import sys import sys
from time import time from time import time
from facefusion import benchmarker, cli_helper, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, hash_helper, logger, state_manager, translator, voice_extractor from facefusion import benchmarker, cli_helper, content_analyser, hash_helper, logger, state_manager, translator
from facefusion.args import apply_args, collect_job_args, reduce_job_args, reduce_step_args from facefusion.args import apply_args, collect_job_args, reduce_job_args, reduce_step_args
from facefusion.download import conditional_download_hashes, conditional_download_sources from facefusion.download import conditional_download_hashes, conditional_download_sources
from facefusion.exit_helper import hard_exit, signal_exit from facefusion.exit_helper import hard_exit, signal_exit
from facefusion.filesystem import get_file_extension, get_file_name, is_image, is_video, resolve_file_paths, resolve_file_pattern from facefusion.filesystem import get_file_extension, get_file_name, is_video, resolve_file_paths, resolve_file_pattern
from facefusion.jobs import job_helper, job_manager, job_runner from facefusion.jobs import job_helper, job_manager, job_runner
from facefusion.jobs.job_list import compose_job_list from facefusion.jobs.job_list import compose_job_list
from facefusion.memory import limit_system_memory
from facefusion.processors.core import get_processors_modules from facefusion.processors.core import get_processors_modules
from facefusion.program import create_program from facefusion.program import create_program
from facefusion.program_helper import validate_args from facefusion.program_helper import validate_args
from facefusion.types import Args, ErrorCode from facefusion.types import Args, ErrorCode, WorkflowMode
from facefusion.workflows import image_to_image, image_to_video from facefusion.workflows import image_to_image, image_to_video
@@ -41,11 +40,6 @@ def cli() -> None:
def route(args : Args) -> None: def route(args : Args) -> None:
system_memory_limit = state_manager.get_item('system_memory_limit')
if system_memory_limit and system_memory_limit > 0:
limit_system_memory(system_memory_limit)
if state_manager.get_item('command') == 'force-download': if state_manager.get_item('command') == 'force-download':
error_code = force_download() error_code = force_download()
hard_exit(error_code) hard_exit(error_code)
@@ -96,32 +90,17 @@ def pre_check() -> bool:
logger.error(translator.get('python_not_supported').format(version = '3.10'), __name__) logger.error(translator.get('python_not_supported').format(version = '3.10'), __name__)
return False return False
if not shutil.which('curl'): for dependency in [ 'curl', 'ffmpeg', 'ffprobe' ]:
logger.error(translator.get('curl_not_installed'), __name__) if not shutil.which(dependency):
return False logger.error(translator.get('dependency_not_installed').format(dependency = dependency), __name__)
return False
if not shutil.which('ffmpeg'):
logger.error(translator.get('ffmpeg_not_installed'), __name__)
return False
return True return True
def common_pre_check() -> bool: def common_pre_check() -> bool:
common_modules =\
[
content_analyser,
face_classifier,
face_detector,
face_landmarker,
face_masker,
face_recognizer,
voice_extractor
]
content_analyser_content = inspect.getsource(content_analyser).encode() content_analyser_content = inspect.getsource(content_analyser).encode()
content_analyser_hash = hash_helper.create_hash(content_analyser_content)
return all(module.pre_check() for module in common_modules) and content_analyser_hash == 'b14e7b92' return hash_helper.create_hash(content_analyser_content) == '0922c180'
def processors_pre_check() -> bool: def processors_pre_check() -> bool:
@@ -132,22 +111,19 @@ def processors_pre_check() -> bool:
def force_download() -> ErrorCode: def force_download() -> ErrorCode:
common_modules =\ download_scope = state_manager.get_item('download_scope')
[
content_analyser,
face_classifier,
face_detector,
face_landmarker,
face_masker,
face_recognizer,
voice_extractor
]
available_processors = [ get_file_name(file_path) for file_path in resolve_file_paths('facefusion/processors/modules') ] available_processors = [ get_file_name(file_path) for file_path in resolve_file_paths('facefusion/processors/modules') ]
processor_modules = get_processors_modules(available_processors) processor_modules = get_processors_modules(available_processors)
common_modules = []
for processor_module in processor_modules:
for common_module in processor_module.get_common_modules():
if common_module not in common_modules:
common_modules.append(common_module)
for module in common_modules + processor_modules: for module in common_modules + processor_modules:
if hasattr(module, 'create_static_model_set'): if hasattr(module, 'create_static_model_set'):
for model in module.create_static_model_set(state_manager.get_item('download_scope')).values(): for model in module.create_static_model_set(download_scope).values():
model_hash_set = model.get('hashes') model_hash_set = model.get('hashes')
model_source_set = model.get('sources') model_source_set = model.get('sources')
@@ -336,15 +312,25 @@ def process_step(job_id : str, step_index : int, step_args : Args) -> bool:
def conditional_process() -> ErrorCode: def conditional_process() -> ErrorCode:
start_time = time() start_time = time()
if state_manager.get_item('workflow_mode') == 'auto':
state_manager.set_item('workflow_mode', detect_workflow_mode())
for processor_module in get_processors_modules(state_manager.get_item('processors')): for processor_module in get_processors_modules(state_manager.get_item('processors')):
if not processor_module.pre_process('output'): if not processor_module.pre_process('output'):
return 2 return 2
if is_image(state_manager.get_item('target_path')): if state_manager.get_item('workflow_mode') == 'image-to-image':
return image_to_image.process(start_time) return image_to_image.process(start_time)
if is_video(state_manager.get_item('target_path')): if state_manager.get_item('workflow_mode') == 'image-to-video':
return image_to_video.process(start_time) return image_to_video.process(start_time)
return 0 return 0
def detect_workflow_mode() -> WorkflowMode:
if is_video(state_manager.get_item('target_path')):
return 'image-to-video'
return 'image-to-image'
+1 -1
View File
@@ -9,7 +9,7 @@ from facefusion.types import Command
def run(commands : List[Command]) -> List[Command]: def run(commands : List[Command]) -> List[Command]:
user_agent = metadata.get('name') + '/' + metadata.get('version') user_agent = metadata.get('name') + '/' + metadata.get('version')
return [ shutil.which('curl'), '--user-agent', user_agent, '--location', '--silent' ] + commands return [ shutil.which('curl'), '--user-agent', user_agent, '--location', '--silent', '--ssl-no-revoke' ] + commands
def chain(*commands : List[Command]) -> List[Command]: def chain(*commands : List[Command]) -> List[Command]:
+3
View File
@@ -32,6 +32,7 @@ def conditional_download(download_directory_path : str, urls : List[str]) -> Non
curl_builder.set_timeout(5), curl_builder.set_timeout(5),
curl_builder.set_retry(5) curl_builder.set_retry(5)
) )
open_curl(commands) open_curl(commands)
current_size = initial_size current_size = initial_size
progress.set_postfix(download_providers = state_manager.get_item('download_providers'), file_name = download_file_name) progress.set_postfix(download_providers = state_manager.get_item('download_providers'), file_name = download_file_name)
@@ -48,6 +49,7 @@ def get_static_download_size(url : str) -> int:
curl_builder.ping(url), curl_builder.ping(url),
curl_builder.set_timeout(5) curl_builder.set_timeout(5)
) )
process = open_curl(commands) process = open_curl(commands)
lines = reversed(process.stdout.readlines()) lines = reversed(process.stdout.readlines())
@@ -66,6 +68,7 @@ def ping_static_url(url : str) -> bool:
curl_builder.ping(url), curl_builder.ping(url),
curl_builder.set_timeout(5) curl_builder.set_timeout(5)
) )
process = open_curl(commands) process = open_curl(commands)
process.communicate() process.communicate()
return process.returncode == 0 return process.returncode == 0
+1 -1
View File
@@ -20,7 +20,7 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
'__metadata__': '__metadata__':
{ {
'vendor': 'dchen236', 'vendor': 'dchen236',
'license': 'Non-Commercial', 'license': 'CC-BY-4.0',
'year': 2021 'year': 2021
}, },
'hashes': 'hashes':
@@ -2,14 +2,13 @@ from typing import List, Optional
import numpy import numpy
from facefusion import state_manager from facefusion import face_store, state_manager
from facefusion.common_helper import get_first from facefusion.common_helper import get_first, get_middle
from facefusion.face_classifier import classify_face from facefusion.face_classifier import classify_face
from facefusion.face_detector import detect_faces, detect_faces_by_angle from facefusion.face_detector import detect_faces, detect_faces_by_angle
from facefusion.face_helper import apply_nms, convert_to_face_landmark_5, estimate_face_angle, get_nms_threshold from facefusion.face_helper import apply_nms, average_points, convert_to_face_landmark_5, estimate_face_angle, get_nms_threshold
from facefusion.face_landmarker import detect_face_landmark, estimate_face_landmark_68_5 from facefusion.face_landmarker import detect_face_landmark, estimate_face_landmark_68_5
from facefusion.face_recognizer import calculate_face_embedding from facefusion.face_recognizer import calculate_face_embedding
from facefusion.face_store import get_static_faces, set_static_faces
from facefusion.types import BoundingBox, Face, FaceLandmark5, FaceLandmarkSet, FaceScoreSet, Score, VisionFrame from facefusion.types import BoundingBox, Face, FaceLandmark5, FaceLandmarkSet, FaceScoreSet, Score, VisionFrame
@@ -47,7 +46,9 @@ def create_faces(vision_frame : VisionFrame, bounding_boxes : List[BoundingBox],
} }
face_embedding, face_embedding_norm = calculate_face_embedding(vision_frame, face_landmark_set.get('5/68')) face_embedding, face_embedding_norm = calculate_face_embedding(vision_frame, face_landmark_set.get('5/68'))
gender, age, race = classify_face(vision_frame, face_landmark_set.get('5/68')) gender, age, race = classify_face(vision_frame, face_landmark_set.get('5/68'))
faces.append(Face( faces.append(Face(
origin = 'detect',
bounding_box = bounding_box, bounding_box = bounding_box,
score_set = face_score_set, score_set = face_score_set,
landmark_set = face_landmark_set, landmark_set = face_landmark_set,
@@ -68,7 +69,102 @@ def get_one_face(faces : List[Face], position : int = 0) -> Optional[Face]:
return None return None
def get_average_face(faces : List[Face]) -> Optional[Face]: def get_many_faces(vision_frames : List[VisionFrame]) -> List[Face]:
many_faces : List[Face] = []
for vision_frame in vision_frames:
if numpy.any(vision_frame):
all_bounding_boxes = []
all_face_scores = []
all_face_landmarks_5 = []
for face_detector_angle in state_manager.get_item('face_detector_angles'):
if face_detector_angle == 0:
bounding_boxes, face_scores, face_landmarks_5 = detect_faces(vision_frame)
else:
bounding_boxes, face_scores, face_landmarks_5 = detect_faces_by_angle(vision_frame, face_detector_angle)
all_bounding_boxes.extend(bounding_boxes)
all_face_scores.extend(face_scores)
all_face_landmarks_5.extend(face_landmarks_5)
if all_bounding_boxes and all_face_scores and all_face_landmarks_5 and state_manager.get_item('face_detector_score') > 0:
faces = create_faces(vision_frame, all_bounding_boxes, all_face_scores, all_face_landmarks_5)
if faces:
many_faces.extend(faces)
return many_faces
def get_static_faces(vision_frames : List[VisionFrame]) -> List[Face]:
many_faces : List[Face] = []
for vision_frame in vision_frames:
faces = face_store.get_faces(vision_frame)
if not faces:
with face_store.resolve_lock(vision_frame):
faces = face_store.get_faces(vision_frame)
if not faces:
faces = get_many_faces([ vision_frame ])
if faces:
face_store.set_faces(vision_frame, faces)
many_faces.extend(faces)
return many_faces
def refill_faces(faces : List[Optional[Face]]) -> List[Face]:
fill_faces = []
anchor_index_previous = -1
for index, face in enumerate(faces):
if face:
for gap_index in range(anchor_index_previous + 1, index):
average_factor = (gap_index - anchor_index_previous) / (index - anchor_index_previous)
average_face = average_face_geometry([faces[anchor_index_previous], face], average_factor)
fill_faces.append(average_face)
fill_faces.append(face)
anchor_index_previous = index
return fill_faces
def average_face_geometry(faces : List[Face], average_factor : float) -> Face:
face_first = get_first(faces)
face_middle = get_middle(faces)
face_anchor = face_middle
if average_factor < 0.5:
face_anchor = face_first
landmark_set : FaceLandmarkSet =\
{
'5': average_points(face_first.landmark_set.get('5'), face_middle.landmark_set.get('5'), average_factor),
'5/68': average_points(face_first.landmark_set.get('5/68'), face_middle.landmark_set.get('5/68'), average_factor),
'68': average_points(face_first.landmark_set.get('68'), face_middle.landmark_set.get('68'), average_factor),
'68/5': average_points(face_first.landmark_set.get('68/5'), face_middle.landmark_set.get('68/5'), average_factor)
}
return Face(
origin = 'refill',
bounding_box = average_points(face_first.bounding_box, face_middle.bounding_box, average_factor),
score_set = face_anchor.score_set,
landmark_set = landmark_set,
angle = estimate_face_angle(landmark_set.get('68/5')),
embedding = face_anchor.embedding,
embedding_norm = face_anchor.embedding_norm,
gender = face_anchor.gender,
age = face_anchor.age,
race = face_anchor.race
)
def average_face_identity(faces : List[Face]) -> Optional[Face]:
face_embeddings = [] face_embeddings = []
face_embeddings_norm = [] face_embeddings_norm = []
@@ -80,6 +176,7 @@ def get_average_face(faces : List[Face]) -> Optional[Face]:
face_embeddings_norm.append(face.embedding_norm) face_embeddings_norm.append(face.embedding_norm)
return Face( return Face(
origin = first_face.origin,
bounding_box = first_face.bounding_box, bounding_box = first_face.bounding_box,
score_set = first_face.score_set, score_set = first_face.score_set,
landmark_set = first_face.landmark_set, landmark_set = first_face.landmark_set,
@@ -93,37 +190,6 @@ def get_average_face(faces : List[Face]) -> Optional[Face]:
return None return None
def get_many_faces(vision_frames : List[VisionFrame]) -> List[Face]:
many_faces : List[Face] = []
for vision_frame in vision_frames:
if numpy.any(vision_frame):
static_faces = get_static_faces(vision_frame)
if static_faces:
many_faces.extend(static_faces)
else:
all_bounding_boxes = []
all_face_scores = []
all_face_landmarks_5 = []
for face_detector_angle in state_manager.get_item('face_detector_angles'):
if face_detector_angle == 0:
bounding_boxes, face_scores, face_landmarks_5 = detect_faces(vision_frame)
else:
bounding_boxes, face_scores, face_landmarks_5 = detect_faces_by_angle(vision_frame, face_detector_angle)
all_bounding_boxes.extend(bounding_boxes)
all_face_scores.extend(face_scores)
all_face_landmarks_5.extend(face_landmarks_5)
if all_bounding_boxes and all_face_scores and all_face_landmarks_5 and state_manager.get_item('face_detector_score') > 0:
faces = create_faces(vision_frame, all_bounding_boxes, all_face_scores, all_face_landmarks_5)
if faces:
many_faces.extend(faces)
set_static_faces(vision_frame, faces)
return many_faces
def scale_face(target_face : Face, target_vision_frame : VisionFrame, temp_vision_frame : VisionFrame) -> Face: def scale_face(target_face : Face, target_vision_frame : VisionFrame, temp_vision_frame : VisionFrame) -> Face:
scale_x = temp_vision_frame.shape[1] / target_vision_frame.shape[1] scale_x = temp_vision_frame.shape[1] / target_vision_frame.shape[1]
scale_y = temp_vision_frame.shape[0] / target_vision_frame.shape[0] scale_y = temp_vision_frame.shape[0] / target_vision_frame.shape[0]
+20
View File
@@ -254,3 +254,23 @@ def merge_matrix(temp_matrices : List[Matrix]) -> Matrix:
matrix = numpy.dot(temp_matrix, matrix) matrix = numpy.dot(temp_matrix, matrix)
return matrix[:2, :] return matrix[:2, :]
def calculate_bounding_box_overlap(bounding_box_a : BoundingBox, bounding_box_b : BoundingBox) -> float:
intersection_x1 = max(bounding_box_a[0], bounding_box_b[0])
intersection_y1 = max(bounding_box_a[1], bounding_box_b[1])
intersection_x2 = min(bounding_box_a[2], bounding_box_b[2])
intersection_y2 = min(bounding_box_a[3], bounding_box_b[3])
intersection = max(0, intersection_x2 - intersection_x1) * max(0, intersection_y2 - intersection_y1)
bounding_box_area = (bounding_box_a[2] - bounding_box_a[0]) * (bounding_box_a[3] - bounding_box_a[1])
reference_bounding_box_area = (bounding_box_b[2] - bounding_box_b[0]) * (bounding_box_b[3] - bounding_box_b[1])
union = bounding_box_area + reference_bounding_box_area - intersection
if union > 0:
return intersection / union
return 0.0
def average_points(points_previous : Points, points_next : Points, average_factor : float) -> Points:
return points_previous * (1 - average_factor) + points_next * average_factor
+41 -16
View File
@@ -2,26 +2,35 @@ from typing import List
import numpy import numpy
import facefusion.choices
from facefusion import state_manager from facefusion import state_manager
from facefusion.face_analyser import get_many_faces, get_one_face from facefusion.common_helper import get_first, get_middle
from facefusion.face_creator import get_one_face, get_static_faces
from facefusion.face_tracker import track_faces
from facefusion.types import Face, FaceSelectorOrder, Gender, Race, Score, VisionFrame from facefusion.types import Face, FaceSelectorOrder, Gender, Race, Score, VisionFrame
def select_faces(reference_vision_frame : VisionFrame, target_vision_frame : VisionFrame) -> List[Face]: def select_faces(reference_vision_frame : VisionFrame, source_vision_frames : List[VisionFrame], target_vision_frames : List[VisionFrame]) -> List[Face]:
target_faces = get_many_faces([ target_vision_frame ]) source_faces = get_static_faces(source_vision_frames)
if state_manager.get_item('face_tracker_score') > 0:
target_faces = track_faces(target_vision_frames, state_manager.get_item('face_tracker_score'))
else:
target_faces = get_static_faces([ get_middle(target_vision_frames) ])
if state_manager.get_item('face_selector_mode') == 'many': if state_manager.get_item('face_selector_mode') == 'many':
return sort_and_filter_faces(target_faces) return sort_and_filter_faces(source_faces, target_faces)
if state_manager.get_item('face_selector_mode') == 'one': if state_manager.get_item('face_selector_mode') == 'one':
target_face = get_one_face(sort_and_filter_faces(target_faces)) target_face = get_one_face(sort_and_filter_faces(source_faces, target_faces))
if target_face: if target_face:
return [ target_face ] return [ target_face ]
if state_manager.get_item('face_selector_mode') == 'reference': if state_manager.get_item('face_selector_mode') == 'reference':
reference_faces = get_many_faces([ reference_vision_frame ]) reference_faces = get_static_faces([ reference_vision_frame ])
reference_faces = sort_and_filter_faces(reference_faces) reference_faces = sort_and_filter_faces(source_faces, reference_faces)
reference_face = get_one_face(reference_faces, state_manager.get_item('reference_face_position')) reference_face = get_one_face(reference_faces, state_manager.get_item('reference_face_position'))
if reference_face: if reference_face:
match_faces = find_match_faces([ reference_face ], target_faces, state_manager.get_item('reference_face_distance')) match_faces = find_match_faces([ reference_face ], target_faces, state_manager.get_item('reference_face_distance'))
return match_faces return match_faces
@@ -53,17 +62,33 @@ def calculate_face_distance(face : Face, reference_face : Face) -> float:
return 0 return 0
def sort_and_filter_faces(faces : List[Face]) -> List[Face]: def sort_and_filter_faces(source_faces : List[Face], target_faces : List[Face]) -> List[Face]:
if faces: if target_faces:
if state_manager.get_item('face_selector_order'): if state_manager.get_item('face_selector_order'):
faces = sort_faces_by_order(faces, state_manager.get_item('face_selector_order')) target_faces = sort_faces_by_order(target_faces, state_manager.get_item('face_selector_order'))
if state_manager.get_item('face_selector_gender'):
faces = filter_faces_by_gender(faces, state_manager.get_item('face_selector_gender')) face_selector_gender = state_manager.get_item('face_selector_gender')
if state_manager.get_item('face_selector_race'): face_selector_race = state_manager.get_item('face_selector_race')
faces = filter_faces_by_race(faces, state_manager.get_item('face_selector_race'))
if source_faces and face_selector_gender == 'auto' or face_selector_race == 'auto':
source_face = get_first(sort_faces_by_order(source_faces, 'large-small'))
if source_face:
if face_selector_gender == 'auto':
face_selector_gender = source_face.gender
if face_selector_race == 'auto':
face_selector_race = source_face.race
if face_selector_gender in facefusion.choices.genders:
target_faces = filter_faces_by_gender(target_faces, face_selector_gender)
if face_selector_race in facefusion.choices.races:
target_faces = filter_faces_by_race(target_faces, face_selector_race)
if state_manager.get_item('face_selector_age_start') or state_manager.get_item('face_selector_age_end'): if state_manager.get_item('face_selector_age_start') or state_manager.get_item('face_selector_age_end'):
faces = filter_faces_by_age(faces, state_manager.get_item('face_selector_age_start'), state_manager.get_item('face_selector_age_end')) target_faces = filter_faces_by_age(target_faces, state_manager.get_item('face_selector_age_start'), state_manager.get_item('face_selector_age_end'))
return faces
return target_faces
def sort_faces_by_order(faces : List[Face], order : FaceSelectorOrder) -> List[Face]: def sort_faces_by_order(faces : List[Face], order : FaceSelectorOrder) -> List[Face]:
+29 -15
View File
@@ -1,28 +1,42 @@
import threading
from typing import List, Optional from typing import List, Optional
import numpy
from facefusion.hash_helper import create_hash from facefusion.hash_helper import create_hash
from facefusion.types import Face, FaceStore, VisionFrame from facefusion.types import Face, FaceStore, VisionFrame
FACE_STORE : FaceStore =\ FACE_STORE : FaceStore = {}
{
'static_faces': {}
}
def get_face_store() -> FaceStore: def get_faces(vision_frame : VisionFrame) -> Optional[List[Face]]:
return FACE_STORE if numpy.any(vision_frame):
vision_hash = create_hash(vision_frame.tobytes())
if FACE_STORE.get(vision_hash):
return FACE_STORE.get(vision_hash).get('faces')
return None
def get_static_faces(vision_frame : VisionFrame) -> Optional[List[Face]]: def set_faces(vision_frame : VisionFrame, faces : List[Face]) -> None:
vision_hash = create_hash(vision_frame.tobytes()) if numpy.any(vision_frame):
return FACE_STORE.get('static_faces').get(vision_hash) vision_hash = create_hash(vision_frame.tobytes())
FACE_STORE.setdefault(vision_hash,
{
'lock': threading.Lock()
})['faces'] = faces
def set_static_faces(vision_frame : VisionFrame, faces : List[Face]) -> None: def resolve_lock(vision_frame : VisionFrame) -> threading.Lock:
vision_hash = create_hash(vision_frame.tobytes()) if numpy.any(vision_frame):
if vision_hash: vision_hash = create_hash(vision_frame.tobytes())
FACE_STORE['static_faces'][vision_hash] = faces return FACE_STORE.setdefault(vision_hash,
{
'lock': threading.Lock()
}).get('lock')
return threading.Lock()
def clear_static_faces() -> None: def clear_faces() -> None:
FACE_STORE['static_faces'].clear() FACE_STORE.clear()
+61
View File
@@ -0,0 +1,61 @@
from typing import List
from facefusion.common_helper import get_first, get_last
from facefusion.face_creator import get_static_faces, refill_faces
from facefusion.face_helper import calculate_bounding_box_overlap
from facefusion.types import Face, FaceTrack, Score, VisionFrame
def track_faces(vision_frames : List[VisionFrame], score : Score) -> List[Face]:
target_index = len(vision_frames) // 2
face_tracks = create_face_tracks(vision_frames, score)
temp_faces = []
for face_track in face_tracks:
track_indices = sorted(face_track)
track_index_first = get_first(track_indices)
track_index_last = get_last(track_indices)
track_range = range(track_index_first, track_index_last + 1)
if target_index in track_range:
fill_faces = []
for index in track_range:
fill_faces.append(face_track.get(index))
temp_faces.append(refill_faces(fill_faces)[target_index - track_index_first])
return temp_faces
def create_face_tracks(vision_frames : List[VisionFrame], score : Score) -> List[FaceTrack]:
face_tracks : List[FaceTrack] = []
for frame_index, vision_frame in enumerate(vision_frames):
for face in get_static_faces([ vision_frame ]):
face_track = select_face_track(face_tracks, face, score)
if face_track:
face_track[frame_index] = face
else:
face_tracks.append(
{
frame_index : face
})
return face_tracks
def select_face_track(face_tracks : List[FaceTrack], face : Face, score : Score) -> FaceTrack:
select_track : FaceTrack = {}
select_score = score
for face_track in face_tracks:
track_face = face_track.get(get_last(face_track))
track_score = calculate_bounding_box_overlap(face.bounding_box, track_face.bounding_box)
if track_score > select_score:
select_score = track_score
select_track = face_track
return select_track
+89 -22
View File
@@ -7,11 +7,10 @@ from typing import List, Optional, cast
from tqdm import tqdm from tqdm import tqdm
import facefusion.choices import facefusion.choices
from facefusion import ffmpeg_builder, logger, process_manager, state_manager, translator from facefusion import ffmpeg_builder, ffprobe, logger, process_manager, state_manager, translator, vision
from facefusion.filesystem import get_file_format, remove_file from facefusion.filesystem import get_file_format, remove_file
from facefusion.temp_helper import get_temp_file_path, get_temp_frames_pattern from facefusion.temp_helper import get_temp_file_path, get_temp_frame_pattern
from facefusion.types import AudioBuffer, AudioEncoder, Command, EncoderSet, Fps, Resolution, UpdateProgress, VideoEncoder, VideoFormat from facefusion.types import AudioBuffer, AudioEncoder, Command, EncoderSet, Fps, Resolution, UpdateProgress, VideoEncoder, VideoFormat, VideoReaderMetadata
from facefusion.vision import detect_video_duration, detect_video_fps, pack_resolution, predict_video_frame_total
def run_ffmpeg_with_progress(commands : List[Command], update_progress : UpdateProgress) -> subprocess.Popen[bytes]: def run_ffmpeg_with_progress(commands : List[Command], update_progress : UpdateProgress) -> subprocess.Popen[bytes]:
@@ -70,6 +69,51 @@ def open_ffmpeg(commands : List[Command]) -> subprocess.Popen[bytes]:
return subprocess.Popen(commands, stdin = subprocess.PIPE, stdout = subprocess.PIPE) return subprocess.Popen(commands, stdin = subprocess.PIPE, stdout = subprocess.PIPE)
def create_video_reader(video_path : str, frame_number : int, video_metadata : VideoReaderMetadata) -> subprocess.Popen[bytes]:
commands = ffmpeg_builder.chain(
ffmpeg_builder.seek_to(frame_number / video_metadata.get('fps')),
ffmpeg_builder.set_input(video_path),
ffmpeg_builder.restrict_color_transfer(video_metadata.get('color_transfer')),
ffmpeg_builder.prevent_frame_drop(),
ffmpeg_builder.enforce_pixel_format('bgr24'),
ffmpeg_builder.set_output_format('rawvideo'),
ffmpeg_builder.cast_stream()
)
return open_ffmpeg(commands)
def create_video_writer(target_path : str, temp_video_fps : Fps, temp_video_resolution : Resolution, output_video_resolution : Resolution, output_video_fps : Fps) -> subprocess.Popen[bytes]:
output_video_encoder = state_manager.get_item('output_video_encoder')
output_video_quality = state_manager.get_item('output_video_quality')
output_video_preset = state_manager.get_item('output_video_preset')
temp_video_path = get_temp_file_path(target_path)
temp_video_format = cast(VideoFormat, get_file_format(temp_video_path))
output_video_encoder = fix_video_encoder(temp_video_format, output_video_encoder)
commands = ffmpeg_builder.chain(
ffmpeg_builder.set_output_format('rawvideo'),
ffmpeg_builder.enforce_pixel_format(state_manager.get_item('temp_pixel_format')),
ffmpeg_builder.set_media_resolution(vision.pack_resolution(temp_video_resolution)),
ffmpeg_builder.set_input_fps(temp_video_fps),
ffmpeg_builder.set_input('pipe:0'),
ffmpeg_builder.set_media_resolution(vision.pack_resolution(output_video_resolution)),
ffmpeg_builder.set_video_encoder(output_video_encoder),
ffmpeg_builder.set_thread_count(16),
ffmpeg_builder.set_video_tag(output_video_encoder, temp_video_format),
ffmpeg_builder.set_video_quality(output_video_encoder, output_video_quality),
ffmpeg_builder.set_video_preset(output_video_encoder, output_video_preset),
ffmpeg_builder.concat(
ffmpeg_builder.set_video_fps(output_video_fps),
ffmpeg_builder.convert_color_space('bt709')
),
ffmpeg_builder.set_pixel_format(output_video_encoder),
ffmpeg_builder.force_output(temp_video_path)
)
return open_ffmpeg(commands)
def log_debug(process : subprocess.Popen[bytes]) -> None: def log_debug(process : subprocess.Popen[bytes]) -> None:
_, stderr = process.communicate() _, stderr = process.communicate()
errors = stderr.decode().split(os.linesep) errors = stderr.decode().split(os.linesep)
@@ -108,16 +152,22 @@ def get_available_encoder_set() -> EncoderSet:
def extract_frames(target_path : str, temp_video_resolution : Resolution, temp_video_fps : Fps, trim_frame_start : int, trim_frame_end : int) -> bool: def extract_frames(target_path : str, temp_video_resolution : Resolution, temp_video_fps : Fps, trim_frame_start : int, trim_frame_end : int) -> bool:
extract_frame_total = predict_video_frame_total(target_path, temp_video_fps, trim_frame_start, trim_frame_end) color_transfer = ffprobe.extract_static_video_metadata(target_path).get('color_transfer')
temp_frames_pattern = get_temp_frames_pattern(target_path, '%08d') extract_frame_total = vision.predict_video_frame_total(target_path, temp_video_fps, trim_frame_start, trim_frame_end)
temp_frame_pattern = get_temp_frame_pattern(target_path, '%08d')
commands = ffmpeg_builder.chain( commands = ffmpeg_builder.chain(
ffmpeg_builder.set_input(target_path), ffmpeg_builder.set_input(target_path),
ffmpeg_builder.set_media_resolution(pack_resolution(temp_video_resolution)), ffmpeg_builder.set_media_resolution(vision.pack_resolution(temp_video_resolution)),
ffmpeg_builder.set_frame_quality(0), ffmpeg_builder.set_frame_quality(0),
ffmpeg_builder.enforce_pixel_format('rgb24'), ffmpeg_builder.enforce_pixel_format('rgb24'),
ffmpeg_builder.select_frame_range(trim_frame_start, trim_frame_end, temp_video_fps), ffmpeg_builder.concat(
ffmpeg_builder.select_frame_range(trim_frame_start, trim_frame_end, temp_video_fps),
ffmpeg_builder.restrict_color_transfer(color_transfer)
),
ffmpeg_builder.prevent_frame_drop(), ffmpeg_builder.prevent_frame_drop(),
ffmpeg_builder.set_output(temp_frames_pattern) ffmpeg_builder.set_start_number(trim_frame_start),
ffmpeg_builder.set_output(temp_frame_pattern)
) )
with tqdm(total = extract_frame_total, desc = translator.get('extracting'), unit = 'frame', ascii = ' =', disable = state_manager.get_item('log_level') in [ 'warn', 'error' ]) as progress: with tqdm(total = extract_frame_total, desc = translator.get('extracting'), unit = 'frame', ascii = ' =', disable = state_manager.get_item('log_level') in [ 'warn', 'error' ]) as progress:
@@ -127,24 +177,28 @@ def extract_frames(target_path : str, temp_video_resolution : Resolution, temp_v
def copy_image(target_path : str, temp_image_resolution : Resolution) -> bool: def copy_image(target_path : str, temp_image_resolution : Resolution) -> bool:
temp_image_path = get_temp_file_path(target_path) temp_image_path = get_temp_file_path(target_path)
commands = ffmpeg_builder.chain( commands = ffmpeg_builder.chain(
ffmpeg_builder.set_input(target_path), ffmpeg_builder.set_input(target_path),
ffmpeg_builder.set_media_resolution(pack_resolution(temp_image_resolution)), ffmpeg_builder.set_media_resolution(vision.pack_resolution(temp_image_resolution)),
ffmpeg_builder.set_image_quality(target_path, 100), ffmpeg_builder.set_image_quality(target_path, 100),
ffmpeg_builder.force_output(temp_image_path) ffmpeg_builder.force_output(temp_image_path)
) )
return run_ffmpeg(commands).returncode == 0 return run_ffmpeg(commands).returncode == 0
def finalize_image(target_path : str, output_path : str, output_image_resolution : Resolution) -> bool: def finalize_image(target_path : str, output_path : str, output_image_resolution : Resolution) -> bool:
output_image_quality = state_manager.get_item('output_image_quality') output_image_quality = state_manager.get_item('output_image_quality')
temp_image_path = get_temp_file_path(target_path) temp_image_path = get_temp_file_path(target_path)
commands = ffmpeg_builder.chain( commands = ffmpeg_builder.chain(
ffmpeg_builder.set_input(temp_image_path), ffmpeg_builder.set_input(temp_image_path),
ffmpeg_builder.set_media_resolution(pack_resolution(output_image_resolution)), ffmpeg_builder.set_media_resolution(vision.pack_resolution(output_image_resolution)),
ffmpeg_builder.set_image_quality(target_path, output_image_quality), ffmpeg_builder.set_image_quality(target_path, output_image_quality),
ffmpeg_builder.force_output(output_path) ffmpeg_builder.force_output(output_path)
) )
return run_ffmpeg(commands).returncode == 0 return run_ffmpeg(commands).returncode == 0
@@ -169,12 +223,13 @@ def restore_audio(target_path : str, output_path : str, trim_frame_start : int,
output_audio_encoder = state_manager.get_item('output_audio_encoder') output_audio_encoder = state_manager.get_item('output_audio_encoder')
output_audio_quality = state_manager.get_item('output_audio_quality') output_audio_quality = state_manager.get_item('output_audio_quality')
output_audio_volume = state_manager.get_item('output_audio_volume') output_audio_volume = state_manager.get_item('output_audio_volume')
target_video_fps = detect_video_fps(target_path) target_video_fps = vision.detect_video_fps(target_path)
temp_video_path = get_temp_file_path(target_path) temp_video_path = get_temp_file_path(target_path)
temp_video_format = cast(VideoFormat, get_file_format(temp_video_path)) temp_video_format = cast(VideoFormat, get_file_format(temp_video_path))
temp_video_duration = detect_video_duration(temp_video_path) temp_video_duration = vision.detect_video_duration(temp_video_path)
output_video_format = cast(VideoFormat, get_file_format(output_path))
output_audio_encoder = fix_audio_encoder(temp_video_format, output_audio_encoder) output_audio_encoder = fix_audio_encoder(temp_video_format, output_audio_encoder)
commands = ffmpeg_builder.chain( commands = ffmpeg_builder.chain(
ffmpeg_builder.set_input(temp_video_path), ffmpeg_builder.set_input(temp_video_path),
ffmpeg_builder.select_media_range(trim_frame_start, trim_frame_end, target_video_fps), ffmpeg_builder.select_media_range(trim_frame_start, trim_frame_end, target_video_fps),
@@ -186,8 +241,10 @@ def restore_audio(target_path : str, output_path : str, trim_frame_start : int,
ffmpeg_builder.select_media_stream('0:v:0'), ffmpeg_builder.select_media_stream('0:v:0'),
ffmpeg_builder.select_media_stream('1:a:0'), ffmpeg_builder.select_media_stream('1:a:0'),
ffmpeg_builder.set_video_duration(temp_video_duration), ffmpeg_builder.set_video_duration(temp_video_duration),
ffmpeg_builder.set_faststart(output_video_format),
ffmpeg_builder.force_output(output_path) ffmpeg_builder.force_output(output_path)
) )
return run_ffmpeg(commands).returncode == 0 return run_ffmpeg(commands).returncode == 0
@@ -197,9 +254,10 @@ def replace_audio(target_path : str, audio_path : str, output_path : str) -> boo
output_audio_volume = state_manager.get_item('output_audio_volume') output_audio_volume = state_manager.get_item('output_audio_volume')
temp_video_path = get_temp_file_path(target_path) temp_video_path = get_temp_file_path(target_path)
temp_video_format = cast(VideoFormat, get_file_format(temp_video_path)) temp_video_format = cast(VideoFormat, get_file_format(temp_video_path))
temp_video_duration = detect_video_duration(temp_video_path) temp_video_duration = vision.detect_video_duration(temp_video_path)
output_video_format = cast(VideoFormat, get_file_format(output_path))
output_audio_encoder = fix_audio_encoder(temp_video_format, output_audio_encoder) output_audio_encoder = fix_audio_encoder(temp_video_format, output_audio_encoder)
commands = ffmpeg_builder.chain( commands = ffmpeg_builder.chain(
ffmpeg_builder.set_input(temp_video_path), ffmpeg_builder.set_input(temp_video_path),
ffmpeg_builder.set_input(audio_path), ffmpeg_builder.set_input(audio_path),
@@ -208,8 +266,10 @@ def replace_audio(target_path : str, audio_path : str, output_path : str) -> boo
ffmpeg_builder.set_audio_quality(output_audio_encoder, output_audio_quality), ffmpeg_builder.set_audio_quality(output_audio_encoder, output_audio_quality),
ffmpeg_builder.set_audio_volume(output_audio_volume), ffmpeg_builder.set_audio_volume(output_audio_volume),
ffmpeg_builder.set_video_duration(temp_video_duration), ffmpeg_builder.set_video_duration(temp_video_duration),
ffmpeg_builder.set_faststart(output_video_format),
ffmpeg_builder.force_output(output_path) ffmpeg_builder.force_output(output_path)
) )
return run_ffmpeg(commands).returncode == 0 return run_ffmpeg(commands).returncode == 0
@@ -217,22 +277,25 @@ def merge_video(target_path : str, temp_video_fps : Fps, output_video_resolution
output_video_encoder = state_manager.get_item('output_video_encoder') output_video_encoder = state_manager.get_item('output_video_encoder')
output_video_quality = state_manager.get_item('output_video_quality') output_video_quality = state_manager.get_item('output_video_quality')
output_video_preset = state_manager.get_item('output_video_preset') output_video_preset = state_manager.get_item('output_video_preset')
merge_frame_total = predict_video_frame_total(target_path, output_video_fps, trim_frame_start, trim_frame_end) merge_frame_total = vision.predict_video_frame_total(target_path, output_video_fps, trim_frame_start, trim_frame_end)
temp_video_path = get_temp_file_path(target_path) temp_video_path = get_temp_file_path(target_path)
temp_video_format = cast(VideoFormat, get_file_format(temp_video_path)) temp_video_format = cast(VideoFormat, get_file_format(temp_video_path))
temp_frames_pattern = get_temp_frames_pattern(target_path, '%08d') temp_frame_pattern = get_temp_frame_pattern(target_path, '%08d')
output_video_encoder = fix_video_encoder(temp_video_format, output_video_encoder) output_video_encoder = fix_video_encoder(temp_video_format, output_video_encoder)
commands = ffmpeg_builder.chain( commands = ffmpeg_builder.chain(
ffmpeg_builder.set_input_fps(temp_video_fps), ffmpeg_builder.set_input_fps(temp_video_fps),
ffmpeg_builder.set_input(temp_frames_pattern), ffmpeg_builder.set_start_number(trim_frame_start),
ffmpeg_builder.set_media_resolution(pack_resolution(output_video_resolution)), ffmpeg_builder.set_input(temp_frame_pattern),
ffmpeg_builder.set_media_resolution(vision.pack_resolution(output_video_resolution)),
ffmpeg_builder.set_video_encoder(output_video_encoder), ffmpeg_builder.set_video_encoder(output_video_encoder),
ffmpeg_builder.set_video_tag(output_video_encoder, temp_video_format),
ffmpeg_builder.set_video_quality(output_video_encoder, output_video_quality), ffmpeg_builder.set_video_quality(output_video_encoder, output_video_quality),
ffmpeg_builder.set_video_preset(output_video_encoder, output_video_preset), ffmpeg_builder.set_video_preset(output_video_encoder, output_video_preset),
ffmpeg_builder.concat( ffmpeg_builder.concat(
ffmpeg_builder.set_video_fps(output_video_fps), ffmpeg_builder.set_video_fps(output_video_fps),
ffmpeg_builder.keep_video_alpha(output_video_encoder) ffmpeg_builder.keep_video_alpha(output_video_encoder),
ffmpeg_builder.convert_color_space('bt709')
), ),
ffmpeg_builder.set_pixel_format(output_video_encoder), ffmpeg_builder.set_pixel_format(output_video_encoder),
ffmpeg_builder.force_output(temp_video_path) ffmpeg_builder.force_output(temp_video_path)
@@ -254,13 +317,17 @@ def concat_video(output_path : str, temp_output_paths : List[str]) -> bool:
concat_video_file.close() concat_video_file.close()
output_path = os.path.abspath(output_path) output_path = os.path.abspath(output_path)
output_video_format = cast(VideoFormat, get_file_format(output_path))
commands = ffmpeg_builder.chain( commands = ffmpeg_builder.chain(
ffmpeg_builder.unsafe_concat(), ffmpeg_builder.unsafe_concat(),
ffmpeg_builder.set_input(concat_video_file.name), ffmpeg_builder.set_input(concat_video_file.name),
ffmpeg_builder.copy_video_encoder(), ffmpeg_builder.copy_video_encoder(),
ffmpeg_builder.copy_audio_encoder(), ffmpeg_builder.copy_audio_encoder(),
ffmpeg_builder.set_faststart(output_video_format),
ffmpeg_builder.force_output(output_path) ffmpeg_builder.force_output(output_path)
) )
process = run_ffmpeg(commands) process = run_ffmpeg(commands)
process.communicate() process.communicate()
remove_file(concat_video_path) remove_file(concat_video_path)
+39 -1
View File
@@ -5,7 +5,7 @@ from typing import List, Optional
import numpy import numpy
from facefusion.filesystem import get_file_format from facefusion.filesystem import get_file_format
from facefusion.types import AudioEncoder, Command, CommandSet, Duration, Fps, StreamMode, VideoEncoder, VideoPreset from facefusion.types import AudioEncoder, ColorSpace, ColorTransfer, Command, CommandSet, Duration, Fps, StreamMode, VideoEncoder, VideoFormat, VideoPreset
def run(commands : List[Command]) -> List[Command]: def run(commands : List[Command]) -> List[Command]:
@@ -51,6 +51,10 @@ def set_input_fps(input_fps : Fps) -> List[Command]:
return [ '-r', str(input_fps) ] return [ '-r', str(input_fps) ]
def set_start_number(frame_number : int) -> List[Command]:
return [ '-start_number', str(frame_number) ]
def set_output(output_path : str) -> List[Command]: def set_output(output_path : str) -> List[Command]:
return [ output_path ] return [ output_path ]
@@ -79,6 +83,14 @@ def unsafe_concat() -> List[Command]:
return [ '-f', 'concat', '-safe', '0' ] return [ '-f', 'concat', '-safe', '0' ]
def seek_to(time : float) -> List[Command]:
return [ '-ss', str(time)]
def set_output_format(output_format : str) -> List[Command]:
return [ '-f', output_format ]
def enforce_pixel_format(pixel_format : str) -> List[Command]: def enforce_pixel_format(pixel_format : str) -> List[Command]:
return [ '-pix_fmt', pixel_format ] return [ '-pix_fmt', pixel_format ]
@@ -109,6 +121,16 @@ def prevent_frame_drop() -> List[Command]:
return [ '-vsync', '0' ] return [ '-vsync', '0' ]
def restrict_color_transfer(color_transfer : ColorTransfer) -> List[Command]:
if color_transfer in [ 'smpte2084', 'arib-std-b67' ]:
return [ '-vf', 'scale=out_primaries=bt709:out_transfer=bt709:intent=perceptual' ]
return []
def convert_color_space(color_space : ColorSpace) -> List[Command]:
return [ '-vf', 'scale=out_color_matrix=' + color_space + ':out_range=tv,setparams=colorspace=' + color_space + ':color_primaries=' + color_space + ':color_trc=' + color_space ]
def select_media_range(frame_start : int, frame_end : int, media_fps : Fps) -> List[Command]: def select_media_range(frame_start : int, frame_end : int, media_fps : Fps) -> List[Command]:
commands = [] commands = []
@@ -179,6 +201,10 @@ def set_audio_volume(audio_volume : int) -> List[Command]:
return [ '-filter:a', 'volume=' + str(audio_volume / 100) ] return [ '-filter:a', 'volume=' + str(audio_volume / 100) ]
def set_thread_count(thread_count : int) -> List[Command]:
return [ '-threads', str(thread_count) ]
def set_video_encoder(video_encoder : str) -> List[Command]: def set_video_encoder(video_encoder : str) -> List[Command]:
return [ '-c:v', video_encoder ] return [ '-c:v', video_encoder ]
@@ -187,6 +213,18 @@ def copy_video_encoder() -> List[Command]:
return set_video_encoder('copy') return set_video_encoder('copy')
def set_faststart(video_format : VideoFormat) -> List[Command]:
if video_format in [ 'm4v', 'mov', 'mp4' ]:
return [ '-movflags', '+faststart' ]
return []
def set_video_tag(video_encoder : VideoEncoder, video_format : VideoFormat) -> List[Command]:
if video_format in [ 'm4v', 'mov', 'mp4' ] and video_encoder in [ 'libx265', 'hevc_nvenc', 'hevc_amf', 'hevc_qsv', 'hevc_videotoolbox' ]:
return [ '-tag:v', 'hvc1' ]
return []
def set_video_quality(video_encoder : VideoEncoder, video_quality : int) -> List[Command]: def set_video_quality(video_encoder : VideoEncoder, video_quality : int) -> List[Command]:
if video_encoder in [ 'libx264', 'libx264rgb', 'libx265' ]: if video_encoder in [ 'libx264', 'libx264rgb', 'libx265' ]:
video_compression = numpy.round(numpy.interp(video_quality, [ 0, 100 ], [ 51, 0 ])).astype(int).item() video_compression = numpy.round(numpy.interp(video_quality, [ 0, 100 ], [ 51, 0 ])).astype(int).item()
+130
View File
@@ -0,0 +1,130 @@
import subprocess
from functools import lru_cache
from typing import Dict, List
from facefusion import ffprobe_builder
from facefusion.types import AudioMetadata, Buffer, Command, Fps, VideoMetadata
def run_ffprobe(commands : List[Command]) -> subprocess.Popen[Buffer]:
commands = ffprobe_builder.run(commands)
return subprocess.Popen(commands, stderr = subprocess.PIPE, stdout = subprocess.PIPE)
def parse_entries(output : Buffer) -> Dict[str, str]:
media_entries = {}
if output:
lines = output.decode().strip().splitlines()
for line in lines:
if '=' in line:
key, value = line.split('=', 1)
media_entries[key] = value
return media_entries
def probe_audio_entries(audio_path : str, entries : List[str]) -> Dict[str, str]:
commands = ffprobe_builder.chain(
ffprobe_builder.select_stream('a:0'),
ffprobe_builder.show_stream_entries(entries),
ffprobe_builder.format_to_key_value(),
ffprobe_builder.set_input(audio_path)
)
output, _ = run_ffprobe(commands).communicate()
return parse_entries(output)
def probe_video_entries(video_path : str, entries : List[str]) -> Dict[str, str]:
commands = ffprobe_builder.chain(
ffprobe_builder.select_stream('v:0'),
ffprobe_builder.show_stream_entries(entries),
ffprobe_builder.format_to_key_value(),
ffprobe_builder.set_input(video_path)
)
output, _ = run_ffprobe(commands).communicate()
return parse_entries(output)
def probe_format_entries(media_path : str, entries : List[str]) -> Dict[str, str]:
commands = ffprobe_builder.chain(
ffprobe_builder.show_format_entries(entries),
ffprobe_builder.format_to_key_value(),
ffprobe_builder.set_input(media_path)
)
output, _ = run_ffprobe(commands).communicate()
return parse_entries(output)
@lru_cache(maxsize = 128)
def extract_static_audio_metadata(audio_path : str) -> AudioMetadata:
return extract_audio_metadata(audio_path)
def extract_audio_metadata(audio_path : str) -> AudioMetadata:
audio_entries = probe_audio_entries(audio_path, [ 'sample_rate', 'channels' ])
format_entries = probe_format_entries(audio_path, [ 'duration', 'bit_rate' ])
duration = float(format_entries.get('duration'))
sample_rate = int(audio_entries.get('sample_rate'))
frame_total = round(duration * sample_rate)
channel_total = int(audio_entries.get('channels'))
bit_rate = int(format_entries.get('bit_rate'))
audio_metadata : AudioMetadata =\
{
'duration' : duration,
'frame_total' : frame_total,
'channel_total' : channel_total,
'sample_rate' : sample_rate,
'bit_rate' : bit_rate
}
return audio_metadata
@lru_cache(maxsize = 128)
def extract_static_video_metadata(video_path : str) -> VideoMetadata:
return extract_video_metadata(video_path)
def extract_video_metadata(video_path : str) -> VideoMetadata:
video_entries = probe_video_entries(video_path, [ 'width', 'height', 'r_frame_rate', 'color_transfer' ])
format_entries = probe_format_entries(video_path, [ 'duration', 'bit_rate' ])
duration = float(format_entries.get('duration'))
fps = extract_video_fps(video_entries.get('r_frame_rate'))
frame_total = round(duration * fps)
width = int(video_entries.get('width'))
height = int(video_entries.get('height'))
bit_rate = int(format_entries.get('bit_rate'))
color_transfer = video_entries.get('color_transfer', 'unknown')
video_metadata : VideoMetadata =\
{
'duration' : duration,
'frame_total' : frame_total,
'fps' : fps,
'resolution' : (width, height),
'bit_rate' : bit_rate,
'color_transfer' : color_transfer
}
return video_metadata
def extract_video_fps(frame_rate : str) -> Fps:
if frame_rate and '/' in frame_rate:
numerator, denominator = frame_rate.split('/')
if int(numerator) and int(denominator):
return int(numerator) / int(denominator)
return 0.0
+33
View File
@@ -0,0 +1,33 @@
import itertools
import shutil
from typing import List
from facefusion.types import Command
def run(commands : List[Command]) -> List[Command]:
return [ shutil.which('ffprobe'), '-loglevel', 'error' ] + commands
def chain(*commands : List[Command]) -> List[Command]:
return list(itertools.chain(*commands))
def select_stream(stream : str) -> List[Command]:
return [ '-select_streams', stream ]
def show_stream_entries(entries : List[str]) -> List[Command]:
return [ '-show_entries', 'stream=' + ','.join(entries) ]
def show_format_entries(entries : List[str]) -> List[Command]:
return [ '-show_entries', 'format=' + ','.join(entries) ]
def format_to_key_value() -> List[Command]:
return [ '-of', 'default=noprint_wrappers=1' ]
def set_input(input_path : str) -> List[Command]:
return [ '-i', input_path ]
+35
View File
@@ -0,0 +1,35 @@
from facefusion.types import FrameStoreSet, VisionFrame, VisionFrameSet
FRAME_STORE_SET : FrameStoreSet = {}
def get_frame_store(id : str) -> VisionFrameSet:
if id not in FRAME_STORE_SET:
FRAME_STORE_SET[id] = {}
return FRAME_STORE_SET.get(id)
def set_frame(id : str, frame_number : int, vision_frame : VisionFrame) -> None:
frame_store = get_frame_store(id)
frame_store[frame_number] = vision_frame
def select_frame_set(id : str, frame_start : int, frame_end : int) -> VisionFrameSet:
frame_store = get_frame_store(id)
frame_set = {}
for frame_number in range(frame_start, frame_end + 1):
if frame_number in frame_store:
frame_set[frame_number] = frame_store.get(frame_number)
return frame_set
def reduce_frames(id : str, frame_min : int, frame_max : int) -> None:
FRAME_STORE_SET[id] = select_frame_set(id, frame_min, frame_max)
def clear_frames(id : str) -> None:
if id in FRAME_STORE_SET:
del FRAME_STORE_SET[id]
+32 -12
View File
@@ -1,5 +1,6 @@
import importlib import importlib
import random import random
from functools import lru_cache
from time import sleep, time from time import sleep, time
from typing import List from typing import List
@@ -12,7 +13,7 @@ from facefusion.execution import create_inference_providers, has_execution_provi
from facefusion.exit_helper import fatal_exit from facefusion.exit_helper import fatal_exit
from facefusion.filesystem import get_file_name, is_file from facefusion.filesystem import get_file_name, is_file
from facefusion.time_helper import calculate_end_time from facefusion.time_helper import calculate_end_time
from facefusion.types import DownloadSet, ExecutionProvider, InferencePool, InferencePoolSet from facefusion.types import DownloadSet, ExecutionProvider, InferencePool, InferencePoolSet, InferenceProvider
INFERENCE_POOL_SET : InferencePoolSet =\ INFERENCE_POOL_SET : InferencePoolSet =\
{ {
@@ -25,7 +26,7 @@ def get_inference_pool(module_name : str, model_names : List[str], model_source_
while process_manager.is_checking(): while process_manager.is_checking():
sleep(0.5) sleep(0.5)
execution_device_ids = state_manager.get_item('execution_device_ids') execution_device_ids = state_manager.get_item('execution_device_ids')
execution_providers = resolve_execution_providers(module_name) execution_providers = state_manager.get_item('execution_providers')
app_context = detect_app_context() app_context = detect_app_context()
for execution_device_id in execution_device_ids: for execution_device_id in execution_device_ids:
@@ -36,26 +37,27 @@ def get_inference_pool(module_name : str, model_names : List[str], model_source_
if app_context == 'ui' and INFERENCE_POOL_SET.get('cli').get(inference_context): if app_context == 'ui' and INFERENCE_POOL_SET.get('cli').get(inference_context):
INFERENCE_POOL_SET['ui'][inference_context] = INFERENCE_POOL_SET.get('cli').get(inference_context) INFERENCE_POOL_SET['ui'][inference_context] = INFERENCE_POOL_SET.get('cli').get(inference_context)
if not INFERENCE_POOL_SET.get(app_context).get(inference_context): if not INFERENCE_POOL_SET.get(app_context).get(inference_context):
INFERENCE_POOL_SET[app_context][inference_context] = create_inference_pool(model_source_set, execution_device_id, execution_providers) inference_providers = resolve_static_inference_providers(module_name, execution_device_id)
INFERENCE_POOL_SET[app_context][inference_context] = create_inference_pool(model_source_set, inference_providers)
current_inference_context = get_inference_context(module_name, model_names, random.choice(execution_device_ids), execution_providers) current_inference_context = get_inference_context(module_name, model_names, random.choice(execution_device_ids), execution_providers)
return INFERENCE_POOL_SET.get(app_context).get(current_inference_context) return INFERENCE_POOL_SET.get(app_context).get(current_inference_context)
def create_inference_pool(model_source_set : DownloadSet, execution_device_id : int, execution_providers : List[ExecutionProvider]) -> InferencePool: def create_inference_pool(model_source_set : DownloadSet, inference_providers : List[InferenceProvider]) -> InferencePool:
inference_pool : InferencePool = {} inference_pool : InferencePool = {}
for model_name in model_source_set.keys(): for model_name in model_source_set.keys():
model_path = model_source_set.get(model_name).get('path') model_path = model_source_set.get(model_name).get('path')
if is_file(model_path): if is_file(model_path):
inference_pool[model_name] = create_inference_session(model_path, execution_device_id, execution_providers) inference_pool[model_name] = create_inference_session(model_path, inference_providers)
return inference_pool return inference_pool
def clear_inference_pool(module_name : str, model_names : List[str]) -> None: def clear_inference_pool(module_name : str, model_names : List[str]) -> None:
execution_device_ids = state_manager.get_item('execution_device_ids') execution_device_ids = state_manager.get_item('execution_device_ids')
execution_providers = resolve_execution_providers(module_name) execution_providers = state_manager.get_item('execution_providers')
app_context = detect_app_context() app_context = detect_app_context()
if is_windows() and has_execution_provider('directml'): if is_windows() and has_execution_provider('directml'):
@@ -67,12 +69,11 @@ def clear_inference_pool(module_name : str, model_names : List[str]) -> None:
del INFERENCE_POOL_SET[app_context][inference_context] del INFERENCE_POOL_SET[app_context][inference_context]
def create_inference_session(model_path : str, execution_device_id : int, execution_providers : List[ExecutionProvider]) -> InferenceSession: def create_inference_session(model_path : str, inference_providers : List[InferenceProvider]) -> InferenceSession:
model_file_name = get_file_name(model_path) model_file_name = get_file_name(model_path)
start_time = time() start_time = time()
try: try:
inference_providers = create_inference_providers(execution_device_id, execution_providers)
inference_session = InferenceSession(model_path, providers = inference_providers) inference_session = InferenceSession(model_path, providers = inference_providers)
logger.debug(translator.get('loading_model_succeeded').format(model_name = model_file_name, seconds = calculate_end_time(start_time)), __name__) logger.debug(translator.get('loading_model_succeeded').format(model_name = model_file_name, seconds = calculate_end_time(start_time)), __name__)
return inference_session return inference_session
@@ -87,9 +88,28 @@ def get_inference_context(module_name : str, model_names : List[str], execution_
return inference_context return inference_context
def resolve_execution_providers(module_name : str) -> List[ExecutionProvider]: @lru_cache()
def resolve_static_inference_providers(module_name : str, execution_device_id : int) -> List[InferenceProvider]:
module = importlib.import_module(module_name) module = importlib.import_module(module_name)
execution_providers = state_manager.get_item('execution_providers')
if hasattr(module, 'resolve_execution_providers'): if hasattr(module, 'override_inference_providers'):
return getattr(module, 'resolve_execution_providers')() override_inference_providers = getattr(module, 'override_inference_providers')()
return state_manager.get_item('execution_providers')
if override_inference_providers:
return override_inference_providers
if hasattr(module, 'adjust_inference_providers'):
adjust_inference_providers = getattr(module, 'adjust_inference_providers')()
if adjust_inference_providers:
inference_providers = create_inference_providers(execution_device_id, execution_providers)
for adjust_inference_provider in adjust_inference_providers:
for inference_provider in inference_providers:
if inference_provider[0] == adjust_inference_provider[0] and inference_provider[1]:
inference_provider[1].update(adjust_inference_provider[1])
return inference_providers
return create_inference_providers(execution_device_id, execution_providers)
+6 -38
View File
@@ -19,23 +19,23 @@ LOCALES =\
} }
ONNXRUNTIME_SET =\ ONNXRUNTIME_SET =\
{ {
'default': ('onnxruntime', '1.24.1') 'default': ('onnxruntime', '1.26.0')
} }
if is_windows() or is_linux(): if is_windows() or is_linux():
ONNXRUNTIME_SET['cuda'] = ('onnxruntime-gpu', '1.24.3') ONNXRUNTIME_SET['cuda'] = ('onnxruntime-gpu', '1.26.0')
ONNXRUNTIME_SET['openvino'] = ('onnxruntime-openvino', '1.24.1') ONNXRUNTIME_SET['openvino'] = ('onnxruntime-openvino', '1.24.1')
if is_windows(): if is_windows():
ONNXRUNTIME_SET['directml'] = ('onnxruntime-directml', '1.24.3') ONNXRUNTIME_SET['directml'] = ('onnxruntime-directml', '1.24.4')
ONNXRUNTIME_SET['qnn'] = ('onnxruntime-qnn', '1.24.3') ONNXRUNTIME_SET['qnn'] = ('onnxruntime-qnn', '1.24.4')
if is_linux(): if is_linux():
ONNXRUNTIME_SET['migraphx'] = ('onnxruntime-migraphx', '1.24.2') ONNXRUNTIME_SET['migraphx'] = ('onnxruntime-migraphx', '1.25.0')
ONNXRUNTIME_SET['rocm'] = ('onnxruntime-rocm', '1.22.2.post1') ONNXRUNTIME_SET['rocm'] = ('onnxruntime-rocm', '1.22.2.post1')
def cli() -> None: def cli() -> None:
signal.signal(signal.SIGINT, signal_exit) signal.signal(signal.SIGINT, signal_exit)
program = ArgumentParser(formatter_class = partial(HelpFormatter, max_help_position = 50)) program = ArgumentParser(formatter_class = partial(HelpFormatter, max_help_position = 50))
program.add_argument('--onnxruntime', help = LOCALES.get('install_dependency').format(dependency = 'onnxruntime'), choices = ONNXRUNTIME_SET.keys(), required = True) program.add_argument('onnxruntime', help = LOCALES.get('install_dependency').format(dependency = 'onnxruntime'), choices = ONNXRUNTIME_SET.keys())
program.add_argument('--force-reinstall', help = LOCALES.get('force_reinstall'), action = 'store_true') program.add_argument('--force-reinstall', help = LOCALES.get('force_reinstall'), action = 'store_true')
program.add_argument('--skip-conda', help = LOCALES.get('skip_conda'), action = 'store_true') program.add_argument('--skip-conda', help = LOCALES.get('skip_conda'), action = 'store_true')
program.add_argument('-v', '--version', version = metadata.get('name') + ' ' + metadata.get('version'), action = 'version') program.add_argument('-v', '--version', version = metadata.get('name') + ' ' + metadata.get('version'), action = 'version')
@@ -72,35 +72,3 @@ def run(program : ArgumentParser) -> None:
subprocess.call([ shutil.which('pip'), 'uninstall', 'onnxruntime', onnxruntime_name, '-y', '-q' ]) subprocess.call([ shutil.which('pip'), 'uninstall', 'onnxruntime', onnxruntime_name, '-y', '-q' ])
subprocess.call(commands) subprocess.call(commands)
if args.onnxruntime == 'cuda' and has_conda:
library_paths = []
if is_linux():
python_id = 'python' + str(sys.version_info.major) + '.' + str(sys.version_info.minor)
library_paths.extend(
[
os.path.join(os.getenv('CONDA_PREFIX'), 'lib'),
os.path.join(os.getenv('CONDA_PREFIX'), 'lib', python_id, 'site-packages', 'tensorrt_libs')
])
if os.getenv('LD_LIBRARY_PATH'):
library_paths.extend(os.getenv('LD_LIBRARY_PATH').split(os.pathsep))
library_paths = list(dict.fromkeys(filter(os.path.exists, library_paths)))
subprocess.call([ shutil.which('conda'), 'env', 'config', 'vars', 'set', 'LD_LIBRARY_PATH=' + os.pathsep.join(library_paths) ])
if is_windows():
library_paths.extend(
[
os.path.join(os.getenv('CONDA_PREFIX'), 'Lib'),
os.path.join(os.getenv('CONDA_PREFIX'), 'Lib', 'site-packages', 'tensorrt_libs')
])
if os.getenv('PATH'):
library_paths.extend(os.getenv('PATH').split(os.pathsep))
library_paths = list(dict.fromkeys(filter(os.path.exists, library_paths)))
subprocess.call([ shutil.which('conda'), 'env', 'config', 'vars', 'set', 'PATH=' + os.pathsep.join(library_paths) ])
+2
View File
@@ -6,6 +6,7 @@ import facefusion.choices
from facefusion.filesystem import create_directory, get_file_name, is_directory, is_file, move_file, remove_directory, remove_file, resolve_file_pattern from facefusion.filesystem import create_directory, get_file_name, is_directory, is_file, move_file, remove_directory, remove_file, resolve_file_pattern
from facefusion.jobs.job_helper import get_step_output_path from facefusion.jobs.job_helper import get_step_output_path
from facefusion.json import read_json, write_json from facefusion.json import read_json, write_json
from facefusion.sanitizer import sanitize_job_id
from facefusion.time_helper import get_current_date_time from facefusion.time_helper import get_current_date_time
from facefusion.types import Args, Job, JobSet, JobStatus, JobStep, JobStepStatus from facefusion.types import Args, Job, JobSet, JobStatus, JobStep, JobStepStatus
@@ -261,5 +262,6 @@ def find_job_path(job_id : str) -> Optional[str]:
def get_job_file_name(job_id : str) -> Optional[str]: def get_job_file_name(job_id : str) -> Optional[str]:
if job_id: if job_id:
job_id = sanitize_job_id(job_id)
return job_id + '.json' return job_id + '.json'
return None return None
+9 -6
View File
@@ -6,8 +6,7 @@ LOCALES : Locales =\
{ {
'conda_not_activated': 'conda is not activated', 'conda_not_activated': 'conda is not activated',
'python_not_supported': 'python version is not supported, upgrade to {version} or higher', 'python_not_supported': 'python version is not supported, upgrade to {version} or higher',
'curl_not_installed': 'curl is not installed', 'dependency_not_installed': '{dependency} is not installed',
'ffmpeg_not_installed': 'ffmpeg is not installed',
'creating_temp': 'creating temporary resources', 'creating_temp': 'creating temporary resources',
'extracting_frames': 'extracting frames with a resolution of {resolution} and {fps} frames per second', 'extracting_frames': 'extracting frames with a resolution of {resolution} and {fps} frames per second',
'extracting_frames_succeeded': 'extracting frames succeeded', 'extracting_frames_succeeded': 'extracting frames succeeded',
@@ -99,6 +98,8 @@ LOCALES : Locales =\
{ {
'install_dependency': 'choose the variant of {dependency} to install', 'install_dependency': 'choose the variant of {dependency} to install',
'skip_conda': 'skip the conda environment check', 'skip_conda': 'skip the conda environment check',
'workflow_mode': 'choose the workflow mode',
'workflow_strategy': 'choose the workflow strategy',
'config_path': 'choose the config file to override defaults', 'config_path': 'choose the config file to override defaults',
'temp_path': 'specify the directory for the temporary resources', 'temp_path': 'specify the directory for the temporary resources',
'jobs_path': 'specify the directory to store jobs', 'jobs_path': 'specify the directory to store jobs',
@@ -124,6 +125,7 @@ LOCALES : Locales =\
'reference_face_position': 'specify the position used to create the reference face', 'reference_face_position': 'specify the position used to create the reference face',
'reference_face_distance': 'specify the similarity between the reference face and target face', 'reference_face_distance': 'specify the similarity between the reference face and target face',
'reference_frame_number': 'specify the frame used to create the reference face', 'reference_frame_number': 'specify the frame used to create the reference face',
'face_tracker_score': 'specify the overlap score used to match the tracked faces',
'face_occluder_model': 'choose the model responsible for the occlusion mask', 'face_occluder_model': 'choose the model responsible for the occlusion mask',
'face_parser_model': 'choose the model responsible for the region mask', 'face_parser_model': 'choose the model responsible for the region mask',
'face_mask_types': 'mix and match different face mask types (choices: {choices})', 'face_mask_types': 'mix and match different face mask types (choices: {choices})',
@@ -135,7 +137,8 @@ LOCALES : Locales =\
'trim_frame_start': 'specify the starting frame of the target video', 'trim_frame_start': 'specify the starting frame of the target video',
'trim_frame_end': 'specify the ending frame of the target video', 'trim_frame_end': 'specify the ending frame of the target video',
'temp_frame_format': 'specify the temporary resources format', 'temp_frame_format': 'specify the temporary resources format',
'keep_temp': 'keep the temporary resources after processing', 'temp_pixel_format': 'specify the temporary pixel format',
'target_frame_amount': 'specify the amount of target frames forwarded to the processor',
'output_image_quality': 'specify the image quality which translates to the image compression', 'output_image_quality': 'specify the image quality which translates to the image compression',
'output_image_scale': 'specify the image scale based on the target image', 'output_image_scale': 'specify the image scale based on the target image',
'output_audio_encoder': 'specify the encoder used for the audio', 'output_audio_encoder': 'specify the encoder used for the audio',
@@ -161,7 +164,6 @@ LOCALES : Locales =\
'execution_providers': 'inference using different providers (choices: {choices}, ...)', 'execution_providers': 'inference using different providers (choices: {choices}, ...)',
'execution_thread_count': 'specify the amount of parallel threads while processing', 'execution_thread_count': 'specify the amount of parallel threads while processing',
'video_memory_strategy': 'balance fast processing and low VRAM usage', 'video_memory_strategy': 'balance fast processing and low VRAM usage',
'system_memory_limit': 'limit the available RAM that can be used while processing',
'log_level': 'adjust the message severity displayed in the terminal', 'log_level': 'adjust the message severity displayed in the terminal',
'halt_on_error': 'halt the program once an error occurred', 'halt_on_error': 'halt the program once an error occurred',
'run': 'run the program', 'run': 'run the program',
@@ -200,9 +202,10 @@ LOCALES : Locales =\
'benchmark_cycle_count_slider': 'BENCHMARK CYCLE COUNT', 'benchmark_cycle_count_slider': 'BENCHMARK CYCLE COUNT',
'benchmark_resolutions_checkbox_group': 'BENCHMARK RESOLUTIONS', 'benchmark_resolutions_checkbox_group': 'BENCHMARK RESOLUTIONS',
'clear_button': 'CLEAR', 'clear_button': 'CLEAR',
'common_options_checkbox_group': 'OPTIONS',
'download_providers_checkbox_group': 'DOWNLOAD PROVIDERS', 'download_providers_checkbox_group': 'DOWNLOAD PROVIDERS',
'execution_providers_checkbox_group': 'EXECUTION PROVIDERS', 'execution_providers_checkbox_group': 'EXECUTION PROVIDERS',
'workflow_mode_dropdown': 'WORKFLOW MODE',
'workflow_strategy_dropdown': 'WORKFLOW STRATEGY',
'execution_thread_count_slider': 'EXECUTION THREAD COUNT', 'execution_thread_count_slider': 'EXECUTION THREAD COUNT',
'face_detector_angles_checkbox_group': 'FACE DETECTOR ANGLES', 'face_detector_angles_checkbox_group': 'FACE DETECTOR ANGLES',
'face_detector_model_dropdown': 'FACE DETECTOR MODEL', 'face_detector_model_dropdown': 'FACE DETECTOR MODEL',
@@ -224,6 +227,7 @@ LOCALES : Locales =\
'face_selector_mode_dropdown': 'FACE SELECTOR MODE', 'face_selector_mode_dropdown': 'FACE SELECTOR MODE',
'face_selector_order_dropdown': 'FACE SELECTOR ORDER', 'face_selector_order_dropdown': 'FACE SELECTOR ORDER',
'face_selector_race_dropdown': 'FACE SELECTOR RACE', 'face_selector_race_dropdown': 'FACE SELECTOR RACE',
'face_tracker_score_slider': 'FACE TRACKER SCORE',
'face_occluder_model_dropdown': 'FACE OCCLUDER MODEL', 'face_occluder_model_dropdown': 'FACE OCCLUDER MODEL',
'face_parser_model_dropdown': 'FACE PARSER MODEL', 'face_parser_model_dropdown': 'FACE PARSER MODEL',
'voice_extractor_model_dropdown': 'VOICE EXTRACTOR MODEL', 'voice_extractor_model_dropdown': 'VOICE EXTRACTOR MODEL',
@@ -257,7 +261,6 @@ LOCALES : Locales =\
'source_file': 'SOURCE', 'source_file': 'SOURCE',
'start_button': 'START', 'start_button': 'START',
'stop_button': 'STOP', 'stop_button': 'STOP',
'system_memory_limit_slider': 'SYSTEM MEMORY LIMIT',
'target_file': 'TARGET', 'target_file': 'TARGET',
'temp_frame_format_dropdown': 'TEMP FRAME FORMAT', 'temp_frame_format_dropdown': 'TEMP FRAME FORMAT',
'terminal_textbox': 'TERMINAL', 'terminal_textbox': 'TERMINAL',
-21
View File
@@ -1,21 +0,0 @@
from facefusion.common_helper import is_macos, is_windows
if is_windows():
import ctypes
else:
import resource
def limit_system_memory(system_memory_limit : int = 1) -> bool:
if is_macos():
system_memory_limit = system_memory_limit * (1024 ** 6)
else:
system_memory_limit = system_memory_limit * (1024 ** 3)
try:
if is_windows():
ctypes.windll.kernel32.SetProcessWorkingSetSize(-1, ctypes.c_size_t(system_memory_limit), ctypes.c_size_t(system_memory_limit)) #type:ignore[attr-defined]
else:
resource.setrlimit(resource.RLIMIT_DATA, (system_memory_limit, system_memory_limit))
return True
except Exception:
return False
+1 -1
View File
@@ -4,7 +4,7 @@ METADATA =\
{ {
'name': 'FaceFusion', 'name': 'FaceFusion',
'description': 'Industry leading face manipulation platform', 'description': 'Industry leading face manipulation platform',
'version': '3.5.4', 'version': 'NEXT',
'license': 'OpenRAIL-AS', 'license': 'OpenRAIL-AS',
'author': 'Henry Ruhs', 'author': 'Henry Ruhs',
'url': 'https://facefusion.io' 'url': 'https://facefusion.io'
+1
View File
@@ -12,6 +12,7 @@ PROCESSORS_METHODS =\
'clear_inference_pool', 'clear_inference_pool',
'register_args', 'register_args',
'apply_args', 'apply_args',
'get_common_modules',
'pre_check', 'pre_check',
'pre_process', 'pre_process',
'post_process', 'post_process',
@@ -1,5 +1,7 @@
from argparse import ArgumentParser from argparse import ArgumentParser
from functools import lru_cache from functools import lru_cache
from types import ModuleType
from typing import List
import cv2 import cv2
import numpy import numpy
@@ -8,10 +10,9 @@ import facefusion.choices
import facefusion.jobs.job_manager import facefusion.jobs.job_manager
import facefusion.jobs.job_store import facefusion.jobs.job_store
from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, inference_manager, logger, state_manager, translator, video_manager from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, inference_manager, logger, state_manager, translator, video_manager
from facefusion.common_helper import create_int_metavar, is_macos from facefusion.common_helper import create_int_metavar, get_middle
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
from facefusion.execution import has_execution_provider from facefusion.face_creator import scale_face
from facefusion.face_analyser import scale_face
from facefusion.face_helper import merge_matrix, paste_back, scale_face_landmark_5, warp_face_by_face_landmark_5 from facefusion.face_helper import merge_matrix, paste_back, scale_face_landmark_5, warp_face_by_face_landmark_5
from facefusion.face_masker import create_box_mask, create_occlusion_mask from facefusion.face_masker import create_box_mask, create_occlusion_mask
from facefusion.face_selector import select_faces from facefusion.face_selector import select_faces
@@ -29,6 +30,41 @@ from facefusion.vision import match_frame_color, read_static_image, read_static_
def create_static_model_set(download_scope : DownloadScope) -> ModelSet: def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
return\ return\
{ {
'fran':
{
'__metadata__':
{
'vendor': 'ry-lu',
'license': 'mit',
'year': 2024
},
'hashes':
{
'age_modifier':
{
'url': resolve_download_url('models-3.6.0', 'fran.hash'),
'path': resolve_relative_path('../.assets/models/fran.hash')
}
},
'sources':
{
'age_modifier':
{
'url': resolve_download_url('models-3.6.0', 'fran.onnx'),
'path': resolve_relative_path('../.assets/models/fran.onnx')
}
},
'templates':
{
'target': 'ffhq_512',
},
'sizes':
{
'target': (1024, 1024),
},
'mean': [ 0.0, 0.0, 0.0 ],
'standard_deviation': [ 1.0, 1.0, 1.0 ]
},
'styleganex_age': 'styleganex_age':
{ {
'__metadata__': '__metadata__':
@@ -62,7 +98,9 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
{ {
'target': (256, 256), 'target': (256, 256),
'target_with_background': (384, 384) 'target_with_background': (384, 384)
} },
'mean': [ 0.5, 0.5, 0.5 ],
'standard_deviation': [ 0.5, 0.5, 0.5 ]
} }
} }
@@ -87,7 +125,7 @@ def get_model_options() -> ModelOptions:
def register_args(program : ArgumentParser) -> None: def register_args(program : ArgumentParser) -> None:
group_processors = find_argument_group(program, 'processors') group_processors = find_argument_group(program, 'processors')
if group_processors: if group_processors:
group_processors.add_argument('--age-modifier-model', help = translator.get('help.model', __package__), default = config.get_str_value('processors', 'age_modifier_model', 'styleganex_age'), choices = age_modifier_choices.age_modifier_models) group_processors.add_argument('--age-modifier-model', help = translator.get('help.model', __package__), default = config.get_str_value('processors', 'age_modifier_model', 'fran'), choices = age_modifier_choices.age_modifier_models)
group_processors.add_argument('--age-modifier-direction', help = translator.get('help.direction', __package__), type = int, default = config.get_int_value('processors', 'age_modifier_direction', '0'), choices = age_modifier_choices.age_modifier_direction_range, metavar = create_int_metavar(age_modifier_choices.age_modifier_direction_range)) group_processors.add_argument('--age-modifier-direction', help = translator.get('help.direction', __package__), type = int, default = config.get_int_value('processors', 'age_modifier_direction', '0'), choices = age_modifier_choices.age_modifier_direction_range, metavar = create_int_metavar(age_modifier_choices.age_modifier_direction_range))
facefusion.jobs.job_store.register_step_keys([ 'age_modifier_model', 'age_modifier_direction' ]) facefusion.jobs.job_store.register_step_keys([ 'age_modifier_model', 'age_modifier_direction' ])
@@ -97,10 +135,18 @@ def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('age_modifier_direction', args.get('age_modifier_direction')) apply_state_item('age_modifier_direction', args.get('age_modifier_direction'))
def get_common_modules() -> List[ModuleType]:
return [ content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer ]
def pre_check() -> bool: def pre_check() -> bool:
model_hash_set = get_model_options().get('hashes') model_hash_set = get_model_options().get('hashes')
model_source_set = get_model_options().get('sources') model_source_set = get_model_options().get('sources')
for common_module in get_common_modules():
if not common_module.pre_check():
return False
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set) return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
@@ -121,15 +167,13 @@ def post_process() -> None:
read_static_image.cache_clear() read_static_image.cache_clear()
read_static_video_frame.cache_clear() read_static_video_frame.cache_clear()
video_manager.clear_video_pool() video_manager.clear_video_pool()
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]: if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
clear_inference_pool() clear_inference_pool()
if state_manager.get_item('video_memory_strategy') == 'strict': if state_manager.get_item('video_memory_strategy') == 'strict':
content_analyser.clear_inference_pool() for common_module in get_common_modules():
face_classifier.clear_inference_pool() common_module.clear_inference_pool()
face_detector.clear_inference_pool()
face_landmarker.clear_inference_pool()
face_masker.clear_inference_pool()
face_recognizer.clear_inference_pool()
def modify_age(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame: def modify_age(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
@@ -137,41 +181,63 @@ def modify_age(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFra
model_sizes = get_model_options().get('sizes') model_sizes = get_model_options().get('sizes')
face_landmark_5 = target_face.landmark_set.get('5/68').copy() face_landmark_5 = target_face.landmark_set.get('5/68').copy()
crop_vision_frame, affine_matrix = warp_face_by_face_landmark_5(temp_vision_frame, face_landmark_5, model_templates.get('target'), model_sizes.get('target')) crop_vision_frame, affine_matrix = warp_face_by_face_landmark_5(temp_vision_frame, face_landmark_5, model_templates.get('target'), model_sizes.get('target'))
extend_face_landmark_5 = scale_face_landmark_5(face_landmark_5, 0.875)
extend_vision_frame, extend_affine_matrix = warp_face_by_face_landmark_5(temp_vision_frame, extend_face_landmark_5, model_templates.get('target_with_background'), model_sizes.get('target_with_background'))
extend_vision_frame_raw = extend_vision_frame.copy()
box_mask = create_box_mask(extend_vision_frame, state_manager.get_item('face_mask_blur'), (0, 0, 0, 0))
crop_masks =\
[
box_mask
]
if 'occlusion' in state_manager.get_item('face_mask_types'): if state_manager.get_item('age_modifier_model') == 'fran':
occlusion_mask = create_occlusion_mask(crop_vision_frame) box_mask = create_box_mask(crop_vision_frame, state_manager.get_item('face_mask_blur'), (0, 0, 0, 0))
temp_matrix = merge_matrix([ extend_affine_matrix, cv2.invertAffineTransform(affine_matrix) ]) crop_masks =\
occlusion_mask = cv2.warpAffine(occlusion_mask, temp_matrix, model_sizes.get('target_with_background')) [
crop_masks.append(occlusion_mask) box_mask
]
crop_vision_frame = prepare_vision_frame(crop_vision_frame) if 'occlusion' in state_manager.get_item('face_mask_types'):
extend_vision_frame = prepare_vision_frame(extend_vision_frame) occlusion_mask = create_occlusion_mask(crop_vision_frame)
age_modifier_direction = numpy.array(numpy.interp(state_manager.get_item('age_modifier_direction'), [ -100, 100 ], [ 2.5, -2.5 ])).astype(numpy.float32) crop_masks.append(occlusion_mask)
extend_vision_frame = forward(crop_vision_frame, extend_vision_frame, age_modifier_direction)
extend_vision_frame = normalize_extend_frame(extend_vision_frame) crop_vision_frame = prepare_vision_frame(crop_vision_frame)
extend_vision_frame = match_frame_color(extend_vision_frame_raw, extend_vision_frame) target_age = numpy.mean(target_face.age)
extend_affine_matrix *= (model_sizes.get('target')[0] * 4) / model_sizes.get('target_with_background')[0] age_modifier_direction = numpy.array([ target_age, target_age + state_manager.get_item('age_modifier_direction') ], dtype = numpy.float32) / 100
crop_mask = numpy.minimum.reduce(crop_masks).clip(0, 1) age_modifier_direction = age_modifier_direction.clip(0, 1)
crop_mask = cv2.resize(crop_mask, (model_sizes.get('target')[0] * 4, model_sizes.get('target')[1] * 4)) crop_vision_frame = forward(crop_vision_frame, crop_vision_frame, age_modifier_direction)
paste_vision_frame = paste_back(temp_vision_frame, extend_vision_frame, crop_mask, extend_affine_matrix) crop_vision_frame = normalize_vision_frame(crop_vision_frame)
return paste_vision_frame crop_mask = numpy.minimum.reduce(crop_masks).clip(0, 1)
paste_vision_frame = paste_back(temp_vision_frame, crop_vision_frame, crop_mask, affine_matrix)
return paste_vision_frame
if state_manager.get_item('age_modifier_model') == 'styleganex_age':
extend_face_landmark_5 = scale_face_landmark_5(face_landmark_5, 0.875)
extend_vision_frame, extend_affine_matrix = warp_face_by_face_landmark_5(temp_vision_frame, extend_face_landmark_5, model_templates.get('target_with_background'), model_sizes.get('target_with_background'))
extend_vision_frame_raw = extend_vision_frame.copy()
box_mask = create_box_mask(extend_vision_frame, state_manager.get_item('face_mask_blur'), (0, 0, 0, 0))
crop_masks =\
[
box_mask
]
if 'occlusion' in state_manager.get_item('face_mask_types'):
occlusion_mask = create_occlusion_mask(crop_vision_frame)
temp_matrix = merge_matrix([ extend_affine_matrix, cv2.invertAffineTransform(affine_matrix) ])
occlusion_mask = cv2.warpAffine(occlusion_mask, temp_matrix, model_sizes.get('target_with_background'))
crop_masks.append(occlusion_mask)
crop_vision_frame = prepare_vision_frame(crop_vision_frame)
extend_vision_frame = prepare_vision_frame(extend_vision_frame)
age_modifier_direction = numpy.array(numpy.interp(state_manager.get_item('age_modifier_direction'), [ -100, 100 ], [ 2.5, -2.5 ])).astype(numpy.float32)
extend_vision_frame = forward(crop_vision_frame, extend_vision_frame, age_modifier_direction)
extend_vision_frame = normalize_extend_frame(extend_vision_frame)
extend_vision_frame = match_frame_color(extend_vision_frame_raw, extend_vision_frame)
extend_affine_matrix *= (model_sizes.get('target')[0] * 4) / model_sizes.get('target_with_background')[0]
crop_mask = numpy.minimum.reduce(crop_masks).clip(0, 1)
crop_mask = cv2.resize(crop_mask, (model_sizes.get('target')[0] * 4, model_sizes.get('target')[1] * 4))
paste_vision_frame = paste_back(temp_vision_frame, extend_vision_frame, crop_mask, extend_affine_matrix)
return paste_vision_frame
return temp_vision_frame
def forward(crop_vision_frame : VisionFrame, extend_vision_frame : VisionFrame, age_modifier_direction : AgeModifierDirection) -> VisionFrame: def forward(crop_vision_frame : VisionFrame, extend_vision_frame : VisionFrame, age_modifier_direction : AgeModifierDirection) -> VisionFrame:
age_modifier = get_inference_pool().get('age_modifier') age_modifier = get_inference_pool().get('age_modifier')
age_modifier_inputs = {} age_modifier_inputs = {}
if is_macos() and has_execution_provider('coreml'):
age_modifier.set_providers([ facefusion.choices.execution_provider_set.get('cpu') ])
for age_modifier_input in age_modifier.get_inputs(): for age_modifier_input in age_modifier.get_inputs():
if age_modifier_input.name == 'target': if age_modifier_input.name == 'target':
age_modifier_inputs[age_modifier_input.name] = crop_vision_frame age_modifier_inputs[age_modifier_input.name] = crop_vision_frame
@@ -187,12 +253,24 @@ def forward(crop_vision_frame : VisionFrame, extend_vision_frame : VisionFrame,
def prepare_vision_frame(vision_frame : VisionFrame) -> VisionFrame: def prepare_vision_frame(vision_frame : VisionFrame) -> VisionFrame:
model_mean = get_model_options().get('mean')
model_standard_deviation = get_model_options().get('standard_deviation')
vision_frame = vision_frame[:, :, ::-1] / 255.0 vision_frame = vision_frame[:, :, ::-1] / 255.0
vision_frame = (vision_frame - 0.5) / 0.5 vision_frame = (vision_frame - model_mean) / model_standard_deviation
vision_frame = numpy.expand_dims(vision_frame.transpose(2, 0, 1), axis = 0).astype(numpy.float32) vision_frame = numpy.expand_dims(vision_frame.transpose(2, 0, 1), axis = 0).astype(numpy.float32)
return vision_frame return vision_frame
def normalize_vision_frame(vision_frame : VisionFrame) -> VisionFrame:
model_mean = get_model_options().get('mean')
model_standard_deviation = get_model_options().get('standard_deviation')
vision_frame = vision_frame.transpose(1, 2, 0)
vision_frame = vision_frame * model_standard_deviation + model_mean
vision_frame = vision_frame.clip(0, 1)
vision_frame = vision_frame[:, :, ::-1] * 255
return vision_frame
def normalize_extend_frame(extend_vision_frame : VisionFrame) -> VisionFrame: def normalize_extend_frame(extend_vision_frame : VisionFrame) -> VisionFrame:
model_sizes = get_model_options().get('sizes') model_sizes = get_model_options().get('sizes')
extend_vision_frame = numpy.clip(extend_vision_frame, -1, 1) extend_vision_frame = numpy.clip(extend_vision_frame, -1, 1)
@@ -206,10 +284,13 @@ def normalize_extend_frame(extend_vision_frame : VisionFrame) -> VisionFrame:
def process_frame(inputs : AgeModifierInputs) -> ProcessorOutputs: def process_frame(inputs : AgeModifierInputs) -> ProcessorOutputs:
reference_vision_frame = inputs.get('reference_vision_frame') reference_vision_frame = inputs.get('reference_vision_frame')
target_vision_frame = inputs.get('target_vision_frame') source_vision_frames = inputs.get('source_vision_frames')
target_vision_frames = inputs.get('target_vision_frames')
temp_vision_frame = inputs.get('temp_vision_frame') temp_vision_frame = inputs.get('temp_vision_frame')
temp_vision_mask = inputs.get('temp_vision_mask') temp_vision_mask = inputs.get('temp_vision_mask')
target_faces = select_faces(reference_vision_frame, target_vision_frame)
target_vision_frame = get_middle(target_vision_frames)
target_faces = select_faces(reference_vision_frame, source_vision_frames, target_vision_frames)
if target_faces: if target_faces:
for target_face in target_faces: for target_face in target_faces:
@@ -1,4 +1,4 @@
from typing import Any, Literal, TypeAlias, TypedDict from typing import Any, List, Literal, TypeAlias, TypedDict
from numpy.typing import NDArray from numpy.typing import NDArray
@@ -7,11 +7,12 @@ from facefusion.types import Mask, VisionFrame
AgeModifierInputs = TypedDict('AgeModifierInputs', AgeModifierInputs = TypedDict('AgeModifierInputs',
{ {
'reference_vision_frame' : VisionFrame, 'reference_vision_frame' : VisionFrame,
'target_vision_frame' : VisionFrame, 'source_vision_frames' : List[VisionFrame],
'target_vision_frames' : List[VisionFrame],
'temp_vision_frame' : VisionFrame, 'temp_vision_frame' : VisionFrame,
'temp_vision_mask' : Mask 'temp_vision_mask' : Mask
}) })
AgeModifierModel = Literal['styleganex_age'] AgeModifierModel = Literal['fran', 'styleganex_age']
AgeModifierDirection : TypeAlias = NDArray[Any] AgeModifierDirection : TypeAlias = NDArray[Any]
@@ -1,14 +1,16 @@
from argparse import ArgumentParser from argparse import ArgumentParser
from functools import lru_cache, partial from functools import lru_cache, partial
from types import ModuleType
from typing import List, Tuple from typing import List, Tuple
import cv2 import cv2
import numpy import numpy
import facefusion.choices
import facefusion.jobs.job_manager import facefusion.jobs.job_manager
import facefusion.jobs.job_store import facefusion.jobs.job_store
from facefusion import config, content_analyser, inference_manager, logger, state_manager, translator, video_manager from facefusion import config, content_analyser, inference_manager, logger, state_manager, translator, video_manager
from facefusion.common_helper import is_macos from facefusion.common_helper import is_macos, is_windows
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
from facefusion.execution import has_execution_provider from facefusion.execution import has_execution_provider
from facefusion.filesystem import in_directory, is_image, is_video, resolve_relative_path, same_file_extension from facefusion.filesystem import in_directory, is_image, is_video, resolve_relative_path, same_file_extension
@@ -19,7 +21,7 @@ from facefusion.processors.types import ProcessorOutputs
from facefusion.program_helper import find_argument_group from facefusion.program_helper import find_argument_group
from facefusion.sanitizer import sanitize_int_range from facefusion.sanitizer import sanitize_int_range
from facefusion.thread_helper import thread_semaphore from facefusion.thread_helper import thread_semaphore
from facefusion.types import ApplyStateItem, Args, DownloadScope, ExecutionProvider, InferencePool, Mask, ModelOptions, ModelSet, ProcessMode, VisionFrame from facefusion.types import ApplyStateItem, Args, DownloadScope, InferencePool, InferenceProvider, Mask, ModelOptions, ModelSet, ProcessMode, VisionFrame
from facefusion.vision import read_static_image, read_static_video_frame from facefusion.vision import read_static_image, read_static_video_frame
@@ -51,6 +53,7 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
'path': resolve_relative_path('../.assets/models/ben_2.onnx') 'path': resolve_relative_path('../.assets/models/ben_2.onnx')
} }
}, },
'type': 'ben',
'size': (1024, 1024), 'size': (1024, 1024),
'mean': [ 0.0, 0.0, 0.0 ], 'mean': [ 0.0, 0.0, 0.0 ],
'standard_deviation': [ 1.0, 1.0, 1.0 ] 'standard_deviation': [ 1.0, 1.0, 1.0 ]
@@ -79,6 +82,7 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
'path': resolve_relative_path('../.assets/models/birefnet_general.onnx') 'path': resolve_relative_path('../.assets/models/birefnet_general.onnx')
} }
}, },
'type': 'birefnet',
'size': (1024, 1024), 'size': (1024, 1024),
'mean': [ 0.0, 0.0, 0.0 ], 'mean': [ 0.0, 0.0, 0.0 ],
'standard_deviation': [ 1.0, 1.0, 1.0 ] 'standard_deviation': [ 1.0, 1.0, 1.0 ]
@@ -107,10 +111,69 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
'path': resolve_relative_path('../.assets/models/birefnet_portrait.onnx') 'path': resolve_relative_path('../.assets/models/birefnet_portrait.onnx')
} }
}, },
'type': 'birefnet',
'size': (1024, 1024), 'size': (1024, 1024),
'mean': [ 0.0, 0.0, 0.0 ], 'mean': [ 0.0, 0.0, 0.0 ],
'standard_deviation': [ 1.0, 1.0, 1.0 ] 'standard_deviation': [ 1.0, 1.0, 1.0 ]
}, },
'corridor_key_1024':
{
'__metadata__':
{
'vendor': 'nikopueringer',
'license': 'Non-Commercial',
'year': 2025
},
'hashes':
{
'background_remover':
{
'url': resolve_download_url('models-3.6.0', 'corridor_key_1024.hash'),
'path': resolve_relative_path('../.assets/models/corridor_key_1024.hash')
}
},
'sources':
{
'background_remover':
{
'url': resolve_download_url('models-3.6.0', 'corridor_key_1024.onnx'),
'path': resolve_relative_path('../.assets/models/corridor_key_1024.onnx')
}
},
'type': 'corridor_key',
'size': (1024, 1024),
'mean': [ 0.485, 0.456, 0.406 ],
'standard_deviation': [ 0.229, 0.224, 0.225 ]
},
'corridor_key_2048':
{
'__metadata__':
{
'vendor': 'nikopueringer',
'license': 'Non-Commercial',
'year': 2025
},
'hashes':
{
'background_remover':
{
'url': resolve_download_url('models-3.6.0', 'corridor_key_2048.hash'),
'path': resolve_relative_path('../.assets/models/corridor_key_2048.hash')
}
},
'sources':
{
'background_remover':
{
'url': resolve_download_url('models-3.6.0', 'corridor_key_2048.onnx'),
'path': resolve_relative_path('../.assets/models/corridor_key_2048.onnx')
}
},
'type': 'corridor_key',
'size': (2048, 2048),
'mean': [ 0.485, 0.456, 0.406 ],
'standard_deviation': [ 0.229, 0.224, 0.225 ]
},
'isnet_general': 'isnet_general':
{ {
'__metadata__': '__metadata__':
@@ -135,6 +198,7 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
'path': resolve_relative_path('../.assets/models/isnet_general.onnx') 'path': resolve_relative_path('../.assets/models/isnet_general.onnx')
} }
}, },
'type': 'isnet',
'size': (1024, 1024), 'size': (1024, 1024),
'mean': [ 0.5, 0.5, 0.5 ], 'mean': [ 0.5, 0.5, 0.5 ],
'standard_deviation': [ 1.0, 1.0, 1.0 ] 'standard_deviation': [ 1.0, 1.0, 1.0 ]
@@ -163,6 +227,7 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
'path': resolve_relative_path('../.assets/models/modnet.onnx') 'path': resolve_relative_path('../.assets/models/modnet.onnx')
} }
}, },
'type': 'modnet',
'size': (512, 512), 'size': (512, 512),
'mean': [ 0.5, 0.5, 0.5 ], 'mean': [ 0.5, 0.5, 0.5 ],
'standard_deviation': [ 0.5, 0.5, 0.5 ] 'standard_deviation': [ 0.5, 0.5, 0.5 ]
@@ -191,6 +256,7 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
'path': resolve_relative_path('../.assets/models/ormbg.onnx') 'path': resolve_relative_path('../.assets/models/ormbg.onnx')
} }
}, },
'type': 'ormbg',
'size': (1024, 1024), 'size': (1024, 1024),
'mean': [ 0.0, 0.0, 0.0 ], 'mean': [ 0.0, 0.0, 0.0 ],
'standard_deviation': [ 1.0, 1.0, 1.0 ] 'standard_deviation': [ 1.0, 1.0, 1.0 ]
@@ -219,6 +285,7 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
'path': resolve_relative_path('../.assets/models/rmbg_1.4.onnx') 'path': resolve_relative_path('../.assets/models/rmbg_1.4.onnx')
} }
}, },
'type': 'rmbg',
'size': (1024, 1024), 'size': (1024, 1024),
'mean': [ 0.5, 0.5, 0.5 ], 'mean': [ 0.5, 0.5, 0.5 ],
'standard_deviation': [ 1.0, 1.0, 1.0 ] 'standard_deviation': [ 1.0, 1.0, 1.0 ]
@@ -247,6 +314,7 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
'path': resolve_relative_path('../.assets/models/rmbg_2.0.onnx') 'path': resolve_relative_path('../.assets/models/rmbg_2.0.onnx')
} }
}, },
'type': 'rmbg',
'size': (1024, 1024), 'size': (1024, 1024),
'mean': [ 0.485, 0.456, 0.406 ], 'mean': [ 0.485, 0.456, 0.406 ],
'standard_deviation': [ 0.229, 0.224, 0.225 ] 'standard_deviation': [ 0.229, 0.224, 0.225 ]
@@ -275,6 +343,7 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
'path': resolve_relative_path('../.assets/models/silueta.onnx') 'path': resolve_relative_path('../.assets/models/silueta.onnx')
} }
}, },
'type': 'silueta',
'size': (320, 320), 'size': (320, 320),
'mean': [ 0.485, 0.456, 0.406 ], 'mean': [ 0.485, 0.456, 0.406 ],
'standard_deviation': [ 0.229, 0.224, 0.225 ] 'standard_deviation': [ 0.229, 0.224, 0.225 ]
@@ -303,6 +372,7 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
'path': resolve_relative_path('../.assets/models/u2net_cloth.onnx') 'path': resolve_relative_path('../.assets/models/u2net_cloth.onnx')
} }
}, },
'type': 'u2net_cloth',
'size': (768, 768), 'size': (768, 768),
'mean': [ 0.485, 0.456, 0.406 ], 'mean': [ 0.485, 0.456, 0.406 ],
'standard_deviation': [ 0.229, 0.224, 0.225 ] 'standard_deviation': [ 0.229, 0.224, 0.225 ]
@@ -331,6 +401,7 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
'path': resolve_relative_path('../.assets/models/u2net_general.onnx') 'path': resolve_relative_path('../.assets/models/u2net_general.onnx')
} }
}, },
'type': 'u2net',
'size': (320, 320), 'size': (320, 320),
'mean': [ 0.485, 0.456, 0.406 ], 'mean': [ 0.485, 0.456, 0.406 ],
'standard_deviation': [ 0.229, 0.224, 0.225 ] 'standard_deviation': [ 0.229, 0.224, 0.225 ]
@@ -359,6 +430,7 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
'path': resolve_relative_path('../.assets/models/u2net_human.onnx') 'path': resolve_relative_path('../.assets/models/u2net_human.onnx')
} }
}, },
'type': 'u2net',
'size': (320, 320), 'size': (320, 320),
'mean': [ 0.485, 0.456, 0.406 ], 'mean': [ 0.485, 0.456, 0.406 ],
'standard_deviation': [ 0.229, 0.224, 0.225 ] 'standard_deviation': [ 0.229, 0.224, 0.225 ]
@@ -387,6 +459,7 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
'path': resolve_relative_path('../.assets/models/u2netp.onnx') 'path': resolve_relative_path('../.assets/models/u2netp.onnx')
} }
}, },
'type': 'u2netp',
'size': (320, 320), 'size': (320, 320),
'mean': [ 0.485, 0.456, 0.406 ], 'mean': [ 0.485, 0.456, 0.406 ],
'standard_deviation': [ 0.229, 0.224, 0.225 ] 'standard_deviation': [ 0.229, 0.224, 0.225 ]
@@ -406,10 +479,13 @@ def clear_inference_pool() -> None:
inference_manager.clear_inference_pool(__name__, model_names) inference_manager.clear_inference_pool(__name__, model_names)
def resolve_execution_providers() -> List[ExecutionProvider]: def override_inference_providers() -> List[InferenceProvider]:
if is_macos() and has_execution_provider('coreml'): model_type = get_model_options().get('type')
return [ 'cpu' ]
return state_manager.get_item('execution_providers') if is_macos() and has_execution_provider('coreml') or is_windows() and has_execution_provider('directml') and model_type == 'corridor_key':
return [ facefusion.choices.execution_provider_set.get('cpu') ]
return []
def get_model_options() -> ModelOptions: def get_model_options() -> ModelOptions:
@@ -420,20 +496,30 @@ def get_model_options() -> ModelOptions:
def register_args(program : ArgumentParser) -> None: def register_args(program : ArgumentParser) -> None:
group_processors = find_argument_group(program, 'processors') group_processors = find_argument_group(program, 'processors')
if group_processors: if group_processors:
group_processors.add_argument('--background-remover-model', help = translator.get('help.model', __package__), default = config.get_str_value('processors', 'background_remover_model', 'rmbg_2.0'), choices = background_remover_choices.background_remover_models) group_processors.add_argument('--background-remover-model', help = translator.get('help.model', __package__), default = config.get_str_value('processors', 'background_remover_model', 'modnet'), choices = background_remover_choices.background_remover_models)
group_processors.add_argument('--background-remover-color', help = translator.get('help.color', __package__), type = partial(sanitize_int_range, int_range = background_remover_choices.background_remover_color_range), default = config.get_int_list('processors', 'background_remover_color', '0 0 0 0'), nargs = '+') group_processors.add_argument('--background-remover-fill-color', help = translator.get('help.fill_color', __package__), type = partial(sanitize_int_range, int_range = background_remover_choices.background_remover_color_range), default = config.get_int_list('processors', 'background_remover_fill_color', '0 0 0 0'), nargs = '+')
facefusion.jobs.job_store.register_step_keys([ 'background_remover_model', 'background_remover_color' ]) group_processors.add_argument('--background-remover-despill-color', help = translator.get('help.despill_color', __package__), type = partial(sanitize_int_range, int_range = background_remover_choices.background_remover_color_range), default = config.get_int_list('processors', 'background_remover_despill_color', '0 0 0 0'), nargs = '+')
facefusion.jobs.job_store.register_step_keys([ 'background_remover_model', 'background_remover_fill_color', 'background_remover_despill_color' ])
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None: def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('background_remover_model', args.get('background_remover_model')) apply_state_item('background_remover_model', args.get('background_remover_model'))
apply_state_item('background_remover_color', normalize_color(args.get('background_remover_color'))) apply_state_item('background_remover_fill_color', normalize_color(args.get('background_remover_fill_color')))
apply_state_item('background_remover_despill_color', normalize_color(args.get('background_remover_despill_color')))
def get_common_modules() -> List[ModuleType]:
return [ content_analyser ]
def pre_check() -> bool: def pre_check() -> bool:
model_hash_set = get_model_options().get('hashes') model_hash_set = get_model_options().get('hashes')
model_source_set = get_model_options().get('sources') model_source_set = get_model_options().get('sources')
for common_module in get_common_modules():
if not common_module.pre_check():
return False
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set) return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
@@ -454,23 +540,36 @@ def post_process() -> None:
read_static_image.cache_clear() read_static_image.cache_clear()
read_static_video_frame.cache_clear() read_static_video_frame.cache_clear()
video_manager.clear_video_pool() video_manager.clear_video_pool()
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]: if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
clear_inference_pool() clear_inference_pool()
if state_manager.get_item('video_memory_strategy') == 'strict': if state_manager.get_item('video_memory_strategy') == 'strict':
content_analyser.clear_inference_pool() for common_module in get_common_modules():
common_module.clear_inference_pool()
def remove_background(temp_vision_frame : VisionFrame) -> Tuple[VisionFrame, Mask]: def remove_background(temp_vision_frame : VisionFrame) -> Tuple[VisionFrame, Mask]:
temp_vision_mask = forward(prepare_temp_frame(temp_vision_frame)) model_type = get_model_options().get('type')
temp_vision_mask = normalize_vision_mask(temp_vision_mask)
temp_vision_mask = cv2.resize(temp_vision_mask, temp_vision_frame.shape[:2][::-1]) if model_type == 'corridor_key':
temp_vision_frame = apply_background_color(temp_vision_frame, temp_vision_mask) remove_vision_mask, remove_vision_frame = forward_corridor_key(prepare_temp_frame(temp_vision_frame))
return temp_vision_frame, temp_vision_mask remove_vision_frame = numpy.squeeze(remove_vision_frame).transpose(1, 2, 0)
remove_vision_frame = numpy.clip(remove_vision_frame * 255, 0, 255).astype(numpy.uint8)
temp_vision_frame = cv2.resize(remove_vision_frame[:, :, ::-1], temp_vision_frame.shape[:2][::-1])
else:
remove_vision_mask = forward(prepare_temp_frame(temp_vision_frame))
remove_vision_mask = normalize_vision_mask(remove_vision_mask)
remove_vision_mask = cv2.resize(remove_vision_mask, temp_vision_frame.shape[:2][::-1])
temp_vision_frame = apply_despill_color(temp_vision_frame)
temp_vision_frame = apply_fill_color(temp_vision_frame, remove_vision_mask)
return temp_vision_frame, remove_vision_mask
def forward(temp_vision_frame : VisionFrame) -> VisionFrame: def forward(temp_vision_frame : VisionFrame) -> VisionFrame:
background_remover = get_inference_pool().get('background_remover') background_remover = get_inference_pool().get('background_remover')
model_name = state_manager.get_item('background_remover_model') model_type = get_model_options().get('type')
with thread_semaphore(): with thread_semaphore():
remove_vision_frame = background_remover.run(None, remove_vision_frame = background_remover.run(None,
@@ -478,20 +577,42 @@ def forward(temp_vision_frame : VisionFrame) -> VisionFrame:
'input': temp_vision_frame 'input': temp_vision_frame
})[0] })[0]
if model_name == 'u2net_cloth': if model_type == 'u2net_cloth':
remove_vision_frame = numpy.argmax(remove_vision_frame, axis = 1) remove_vision_frame = numpy.argmax(remove_vision_frame, axis = 1)
return remove_vision_frame return remove_vision_frame
def forward_corridor_key(temp_vision_frame : VisionFrame) -> Tuple[Mask, VisionFrame]:
background_remover = get_inference_pool().get('background_remover')
with thread_semaphore():
remove_vision_mask, remove_vision_frame = background_remover.run(None,
{
'input': temp_vision_frame
})
return remove_vision_mask, remove_vision_frame
def prepare_temp_frame(temp_vision_frame : VisionFrame) -> VisionFrame: def prepare_temp_frame(temp_vision_frame : VisionFrame) -> VisionFrame:
model_type = get_model_options().get('type')
model_size = get_model_options().get('size') model_size = get_model_options().get('size')
model_mean = get_model_options().get('mean') model_mean = get_model_options().get('mean')
model_standard_deviation = get_model_options().get('standard_deviation') model_standard_deviation = get_model_options().get('standard_deviation')
if model_type == 'corridor_key':
coarse_color = temp_vision_frame[:, :, ::-1].astype(numpy.float32) / 255.0
coarse_bias = coarse_color[:, :, 1] - numpy.maximum(coarse_color[:, :, 0], coarse_color[:, :, 2])
coarse_vision_mask = cv2.resize(1.0 - numpy.clip(coarse_bias * 2.0, 0, 1), model_size)[:, :, numpy.newaxis]
temp_vision_frame = cv2.resize(temp_vision_frame, model_size) temp_vision_frame = cv2.resize(temp_vision_frame, model_size)
temp_vision_frame = temp_vision_frame[:, :, ::-1] / 255.0 temp_vision_frame = temp_vision_frame[:, :, ::-1] / 255.0
temp_vision_frame = (temp_vision_frame - model_mean) / model_standard_deviation temp_vision_frame = (temp_vision_frame - model_mean) / model_standard_deviation
if model_type == 'corridor_key':
temp_vision_frame = numpy.concatenate([ temp_vision_frame, coarse_vision_mask ], axis = 2)
temp_vision_frame = temp_vision_frame.transpose(2, 0, 1) temp_vision_frame = temp_vision_frame.transpose(2, 0, 1)
temp_vision_frame = numpy.expand_dims(temp_vision_frame, axis = 0).astype(numpy.float32) temp_vision_frame = numpy.expand_dims(temp_vision_frame, axis = 0).astype(numpy.float32)
return temp_vision_frame return temp_vision_frame
@@ -503,16 +624,32 @@ def normalize_vision_mask(temp_vision_mask : Mask) -> Mask:
return temp_vision_mask return temp_vision_mask
def apply_background_color(temp_vision_frame : VisionFrame, temp_vision_mask : Mask) -> VisionFrame: def apply_fill_color(temp_vision_frame : VisionFrame, temp_vision_mask : Mask) -> VisionFrame:
background_remover_color = state_manager.get_item('background_remover_color') background_remover_fill_color = state_manager.get_item('background_remover_fill_color')
temp_vision_mask = temp_vision_mask.astype(numpy.float32) / 255 temp_vision_mask = temp_vision_mask.astype(numpy.float32) / 255
temp_vision_mask = numpy.expand_dims(temp_vision_mask, axis = 2) temp_vision_mask = numpy.expand_dims(temp_vision_mask, axis = 2)
temp_vision_mask = (1 - temp_vision_mask) * background_remover_color[-1] / 255 temp_vision_mask = (1 - temp_vision_mask) * background_remover_fill_color[-1] / 255
color_frame = numpy.zeros_like(temp_vision_frame) fill_vision_frame = numpy.zeros_like(temp_vision_frame)
color_frame[:, :, 0] = background_remover_color[2] fill_vision_frame[:, :, 0] = background_remover_fill_color[2]
color_frame[:, :, 1] = background_remover_color[1] fill_vision_frame[:, :, 1] = background_remover_fill_color[1]
color_frame[:, :, 2] = background_remover_color[0] fill_vision_frame[:, :, 2] = background_remover_fill_color[0]
temp_vision_frame = temp_vision_frame * (1 - temp_vision_mask) + color_frame * temp_vision_mask temp_vision_frame = temp_vision_frame * (1 - temp_vision_mask) + fill_vision_frame * temp_vision_mask
temp_vision_frame = temp_vision_frame.astype(numpy.uint8)
return temp_vision_frame
def apply_despill_color(temp_vision_frame : VisionFrame) -> VisionFrame:
background_remover_despill_color = state_manager.get_item('background_remover_despill_color')
temp_vision_frame = temp_vision_frame.astype(numpy.float32)
color_alpha = background_remover_despill_color[3] / 255.0
despill_vision_frame = numpy.zeros_like(temp_vision_frame)
despill_vision_frame[:, :, 0] = background_remover_despill_color[2]
despill_vision_frame[:, :, 1] = background_remover_despill_color[1]
despill_vision_frame[:, :, 2] = background_remover_despill_color[0]
color_weight = despill_vision_frame / numpy.maximum(numpy.max(background_remover_despill_color[:3]), 1)
color_limit = numpy.roll(temp_vision_frame, 1, 2) + numpy.roll(temp_vision_frame, -1, 2)
limit_vision_frame = numpy.minimum(temp_vision_frame, color_limit * 0.5)
temp_vision_frame = temp_vision_frame + (limit_vision_frame - temp_vision_frame) * color_alpha * color_weight
temp_vision_frame = temp_vision_frame.astype(numpy.uint8) temp_vision_frame = temp_vision_frame.astype(numpy.uint8)
return temp_vision_frame return temp_vision_frame
@@ -7,15 +7,20 @@ LOCALES : Locales =\
'help': 'help':
{ {
'model': 'choose the model responsible for removing the background', 'model': 'choose the model responsible for removing the background',
'color': 'apply red, green blue and alpha values to the background' 'fill_color': 'apply red, green, blue and alpha values to the background',
'despill_color': 'remove red, green, blue and alpha values from the foreground'
}, },
'uis': 'uis':
{ {
'model_dropdown': 'BACKGROUND REMOVER MODEL', 'model_dropdown': 'BACKGROUND REMOVER MODEL',
'color_red_number': 'BACKGROUND COLOR RED', 'fill_color_red_number': 'FILL COLOR RED',
'color_green_number': 'BACKGROUND COLOR GREEN', 'fill_color_green_number': 'FILL COLOR GREEN',
'color_blue_number': 'BACKGROUND COLOR BLUE', 'fill_color_blue_number': 'FILL COLOR BLUE',
'color_alpha_number': 'BACKGROUND COLOR ALPHA' 'fill_color_alpha_number': 'FILL COLOR ALPHA',
'despill_color_red_number': 'DESPILL COLOR RED',
'despill_color_green_number': 'DESPILL COLOR GREEN',
'despill_color_blue_number': 'DESPILL COLOR BLUE',
'despill_color_alpha_number': 'DESPILL COLOR ALPHA'
} }
} }
} }
@@ -1,12 +1,12 @@
from typing import Literal, TypedDict from typing import List, Literal, TypedDict
from facefusion.types import Mask, VisionFrame from facefusion.types import Mask, VisionFrame
BackgroundRemoverInputs = TypedDict('BackgroundRemoverInputs', BackgroundRemoverInputs = TypedDict('BackgroundRemoverInputs',
{ {
'target_vision_frame' : VisionFrame, 'target_vision_frames' : List[VisionFrame],
'temp_vision_frame' : VisionFrame, 'temp_vision_frame' : VisionFrame,
'temp_vision_mask' : Mask 'temp_vision_mask' : Mask
}) })
BackgroundRemoverModel = Literal['ben_2', 'birefnet_general', 'birefnet_portrait', 'isnet_general', 'modnet', 'ormbg', 'rmbg_1.4', 'rmbg_2.0', 'silueta', 'u2net_cloth', 'u2net_general', 'u2net_human', 'u2netp'] BackgroundRemoverModel = Literal['ben_2', 'birefnet_general', 'birefnet_portrait', 'corridor_key_1024', 'corridor_key_2048', 'isnet_general', 'modnet', 'ormbg', 'rmbg_1.4', 'rmbg_2.0', 'silueta', 'u2net_cloth', 'u2net_general', 'u2net_human', 'u2netp']
@@ -1,6 +1,7 @@
from argparse import ArgumentParser from argparse import ArgumentParser
from functools import lru_cache from functools import lru_cache
from typing import Tuple from types import ModuleType
from typing import List, Tuple
import cv2 import cv2
import numpy import numpy
@@ -9,9 +10,9 @@ from cv2.typing import Size
import facefusion.jobs.job_manager import facefusion.jobs.job_manager
import facefusion.jobs.job_store import facefusion.jobs.job_store
from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, inference_manager, logger, state_manager, translator, video_manager from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, inference_manager, logger, state_manager, translator, video_manager
from facefusion.common_helper import create_int_metavar from facefusion.common_helper import create_int_metavar, get_middle
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url_by_provider from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url_by_provider
from facefusion.face_analyser import scale_face from facefusion.face_creator import scale_face
from facefusion.face_helper import paste_back, warp_face_by_face_landmark_5 from facefusion.face_helper import paste_back, warp_face_by_face_landmark_5
from facefusion.face_masker import create_area_mask, create_box_mask, create_occlusion_mask, create_region_mask from facefusion.face_masker import create_area_mask, create_box_mask, create_occlusion_mask, create_region_mask
from facefusion.face_selector import select_faces from facefusion.face_selector import select_faces
@@ -286,10 +287,18 @@ def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('deep_swapper_morph', args.get('deep_swapper_morph')) apply_state_item('deep_swapper_morph', args.get('deep_swapper_morph'))
def get_common_modules() -> List[ModuleType]:
return [ content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer ]
def pre_check() -> bool: def pre_check() -> bool:
model_hash_set = get_model_options().get('hashes') model_hash_set = get_model_options().get('hashes')
model_source_set = get_model_options().get('sources') model_source_set = get_model_options().get('sources')
for common_module in get_common_modules():
if not common_module.pre_check():
return False
if model_hash_set and model_source_set: if model_hash_set and model_source_set:
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set) return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
return True return True
@@ -312,15 +321,13 @@ def post_process() -> None:
read_static_image.cache_clear() read_static_image.cache_clear()
read_static_video_frame.cache_clear() read_static_video_frame.cache_clear()
video_manager.clear_video_pool() video_manager.clear_video_pool()
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]: if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
clear_inference_pool() clear_inference_pool()
if state_manager.get_item('video_memory_strategy') == 'strict': if state_manager.get_item('video_memory_strategy') == 'strict':
content_analyser.clear_inference_pool() for common_module in get_common_modules():
face_classifier.clear_inference_pool() common_module.clear_inference_pool()
face_detector.clear_inference_pool()
face_landmarker.clear_inference_pool()
face_masker.clear_inference_pool()
face_recognizer.clear_inference_pool()
def swap_face(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame: def swap_face(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
@@ -411,10 +418,13 @@ def prepare_crop_mask(crop_source_mask : Mask, crop_target_mask : Mask) -> Mask:
def process_frame(inputs : DeepSwapperInputs) -> ProcessorOutputs: def process_frame(inputs : DeepSwapperInputs) -> ProcessorOutputs:
reference_vision_frame = inputs.get('reference_vision_frame') reference_vision_frame = inputs.get('reference_vision_frame')
target_vision_frame = inputs.get('target_vision_frame') source_vision_frames = inputs.get('source_vision_frames')
target_vision_frames = inputs.get('target_vision_frames')
temp_vision_frame = inputs.get('temp_vision_frame') temp_vision_frame = inputs.get('temp_vision_frame')
temp_vision_mask = inputs.get('temp_vision_mask') temp_vision_mask = inputs.get('temp_vision_mask')
target_faces = select_faces(reference_vision_frame, target_vision_frame)
target_vision_frame = get_middle(target_vision_frames)
target_faces = select_faces(reference_vision_frame, source_vision_frames, target_vision_frames)
if target_faces: if target_faces:
for target_face in target_faces: for target_face in target_faces:
@@ -1,4 +1,4 @@
from typing import Any, TypeAlias, TypedDict from typing import Any, List, TypeAlias, TypedDict
from numpy.typing import NDArray from numpy.typing import NDArray
@@ -7,7 +7,8 @@ from facefusion.types import Mask, VisionFrame
DeepSwapperInputs = TypedDict('DeepSwapperInputs', DeepSwapperInputs = TypedDict('DeepSwapperInputs',
{ {
'reference_vision_frame' : VisionFrame, 'reference_vision_frame' : VisionFrame,
'target_vision_frame' : VisionFrame, 'source_vision_frames' : List[VisionFrame],
'target_vision_frames' : List[VisionFrame],
'temp_vision_frame' : VisionFrame, 'temp_vision_frame' : VisionFrame,
'temp_vision_mask' : Mask 'temp_vision_mask' : Mask
}) })
@@ -1,6 +1,7 @@
from argparse import ArgumentParser from argparse import ArgumentParser
from functools import lru_cache from functools import lru_cache
from typing import Tuple from types import ModuleType
from typing import List, Tuple
import cv2 import cv2
import numpy import numpy
@@ -8,9 +9,9 @@ import numpy
import facefusion.jobs.job_manager import facefusion.jobs.job_manager
import facefusion.jobs.job_store import facefusion.jobs.job_store
from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, inference_manager, logger, state_manager, translator, video_manager from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, inference_manager, logger, state_manager, translator, video_manager
from facefusion.common_helper import create_int_metavar from facefusion.common_helper import create_int_metavar, get_middle
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
from facefusion.face_analyser import scale_face from facefusion.face_creator import scale_face
from facefusion.face_helper import paste_back, warp_face_by_face_landmark_5 from facefusion.face_helper import paste_back, warp_face_by_face_landmark_5
from facefusion.face_masker import create_box_mask, create_occlusion_mask from facefusion.face_masker import create_box_mask, create_occlusion_mask
from facefusion.face_selector import select_faces from facefusion.face_selector import select_faces
@@ -111,10 +112,18 @@ def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('expression_restorer_areas', args.get('expression_restorer_areas')) apply_state_item('expression_restorer_areas', args.get('expression_restorer_areas'))
def get_common_modules() -> List[ModuleType]:
return [ content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer ]
def pre_check() -> bool: def pre_check() -> bool:
model_hash_set = get_model_options().get('hashes') model_hash_set = get_model_options().get('hashes')
model_source_set = get_model_options().get('sources') model_source_set = get_model_options().get('sources')
for common_module in get_common_modules():
if not common_module.pre_check():
return False
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set) return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
@@ -138,15 +147,13 @@ def post_process() -> None:
read_static_image.cache_clear() read_static_image.cache_clear()
read_static_video_frame.cache_clear() read_static_video_frame.cache_clear()
video_manager.clear_video_pool() video_manager.clear_video_pool()
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]: if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
clear_inference_pool() clear_inference_pool()
if state_manager.get_item('video_memory_strategy') == 'strict': if state_manager.get_item('video_memory_strategy') == 'strict':
content_analyser.clear_inference_pool() for common_module in get_common_modules():
face_classifier.clear_inference_pool() common_module.clear_inference_pool()
face_detector.clear_inference_pool()
face_landmarker.clear_inference_pool()
face_masker.clear_inference_pool()
face_recognizer.clear_inference_pool()
def restore_expression(target_face : Face, target_vision_frame : VisionFrame, temp_vision_frame : VisionFrame) -> VisionFrame: def restore_expression(target_face : Face, target_vision_frame : VisionFrame, temp_vision_frame : VisionFrame) -> VisionFrame:
@@ -257,10 +264,13 @@ def normalize_crop_frame(crop_vision_frame : VisionFrame) -> VisionFrame:
def process_frame(inputs : ExpressionRestorerInputs) -> ProcessorOutputs: def process_frame(inputs : ExpressionRestorerInputs) -> ProcessorOutputs:
reference_vision_frame = inputs.get('reference_vision_frame') reference_vision_frame = inputs.get('reference_vision_frame')
target_vision_frame = inputs.get('target_vision_frame') source_vision_frames = inputs.get('source_vision_frames')
target_vision_frames = inputs.get('target_vision_frames')
temp_vision_frame = inputs.get('temp_vision_frame') temp_vision_frame = inputs.get('temp_vision_frame')
temp_vision_mask = inputs.get('temp_vision_mask') temp_vision_mask = inputs.get('temp_vision_mask')
target_faces = select_faces(reference_vision_frame, target_vision_frame)
target_vision_frame = get_middle(target_vision_frames)
target_faces = select_faces(reference_vision_frame, source_vision_frames, target_vision_frames)
if target_faces: if target_faces:
for target_face in target_faces: for target_face in target_faces:
@@ -6,7 +6,7 @@ ExpressionRestorerInputs = TypedDict('ExpressionRestorerInputs',
{ {
'reference_vision_frame' : VisionFrame, 'reference_vision_frame' : VisionFrame,
'source_vision_frames' : List[VisionFrame], 'source_vision_frames' : List[VisionFrame],
'target_vision_frame' : VisionFrame, 'target_vision_frames' : List[VisionFrame],
'temp_vision_frame' : VisionFrame, 'temp_vision_frame' : VisionFrame,
'temp_vision_mask' : Mask 'temp_vision_mask' : Mask
}) })
@@ -1,4 +1,6 @@
from argparse import ArgumentParser from argparse import ArgumentParser
from types import ModuleType
from typing import List
import cv2 import cv2
import numpy import numpy
@@ -6,7 +8,8 @@ import numpy
import facefusion.jobs.job_manager import facefusion.jobs.job_manager
import facefusion.jobs.job_store import facefusion.jobs.job_store
from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, logger, state_manager, translator, video_manager from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, logger, state_manager, translator, video_manager
from facefusion.face_analyser import scale_face from facefusion.common_helper import get_middle
from facefusion.face_creator import scale_face
from facefusion.face_helper import warp_face_by_face_landmark_5 from facefusion.face_helper import warp_face_by_face_landmark_5
from facefusion.face_masker import create_area_mask, create_box_mask, create_occlusion_mask, create_region_mask from facefusion.face_masker import create_area_mask, create_box_mask, create_occlusion_mask, create_region_mask
from facefusion.face_selector import select_faces from facefusion.face_selector import select_faces
@@ -38,7 +41,14 @@ def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('face_debugger_items', args.get('face_debugger_items')) apply_state_item('face_debugger_items', args.get('face_debugger_items'))
def get_common_modules() -> List[ModuleType]:
return [ content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer ]
def pre_check() -> bool: def pre_check() -> bool:
for common_module in get_common_modules():
if not common_module.pre_check():
return False
return True return True
@@ -59,13 +69,10 @@ def post_process() -> None:
read_static_image.cache_clear() read_static_image.cache_clear()
read_static_video_frame.cache_clear() read_static_video_frame.cache_clear()
video_manager.clear_video_pool() video_manager.clear_video_pool()
if state_manager.get_item('video_memory_strategy') == 'strict': if state_manager.get_item('video_memory_strategy') == 'strict':
content_analyser.clear_inference_pool() for common_module in get_common_modules():
face_classifier.clear_inference_pool() common_module.clear_inference_pool()
face_detector.clear_inference_pool()
face_landmarker.clear_inference_pool()
face_masker.clear_inference_pool()
face_recognizer.clear_inference_pool()
def debug_face(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame: def debug_face(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
@@ -94,21 +101,22 @@ def debug_face(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFra
def draw_bounding_box(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame: def draw_bounding_box(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
temp_vision_frame = numpy.ascontiguousarray(temp_vision_frame) temp_vision_frame = numpy.ascontiguousarray(temp_vision_frame)
box_color = 0, 0, 255
border_color = 100, 100, 255
bounding_box = target_face.bounding_box.astype(numpy.int32) bounding_box = target_face.bounding_box.astype(numpy.int32)
x1, y1, x2, y2 = bounding_box x1, y1, x2, y2 = bounding_box
box_color = 0, 0, 255
border_scale = calculate_scale(temp_vision_frame)
border_color = 100, 100, 255
cv2.rectangle(temp_vision_frame, (x1, y1), (x2, y2), box_color, 2) cv2.rectangle(temp_vision_frame, (x1, y1), (x2, y2), box_color, border_scale)
if target_face.angle == 0: if target_face.angle == 0:
cv2.line(temp_vision_frame, (x1, y1), (x2, y1), border_color, 3) cv2.line(temp_vision_frame, (x1, y1), (x2, y1), border_color, border_scale + 1)
if target_face.angle == 180: if target_face.angle == 180:
cv2.line(temp_vision_frame, (x1, y2), (x2, y2), border_color, 3) cv2.line(temp_vision_frame, (x1, y2), (x2, y2), border_color, border_scale + 1)
if target_face.angle == 90: if target_face.angle == 90:
cv2.line(temp_vision_frame, (x2, y1), (x2, y2), border_color, 3) cv2.line(temp_vision_frame, (x2, y1), (x2, y2), border_color, border_scale + 1)
if target_face.angle == 270: if target_face.angle == 270:
cv2.line(temp_vision_frame, (x1, y1), (x1, y2), border_color, 3) cv2.line(temp_vision_frame, (x1, y1), (x1, y2), border_color, border_scale + 1)
return temp_vision_frame return temp_vision_frame
@@ -122,11 +130,15 @@ def draw_face_mask(target_face : Face, temp_vision_frame : VisionFrame) -> Visio
crop_vision_frame, affine_matrix = warp_face_by_face_landmark_5(temp_vision_frame, face_landmark_5_68, 'arcface_128', (512, 512)) crop_vision_frame, affine_matrix = warp_face_by_face_landmark_5(temp_vision_frame, face_landmark_5_68, 'arcface_128', (512, 512))
inverse_matrix = cv2.invertAffineTransform(affine_matrix) inverse_matrix = cv2.invertAffineTransform(affine_matrix)
temp_size = temp_vision_frame.shape[:2][::-1] temp_size = temp_vision_frame.shape[:2][::-1]
mask_scale = calculate_scale(temp_vision_frame)
mask_color = 0, 255, 0 mask_color = 0, 255, 0
if numpy.array_equal(face_landmark_5, face_landmark_5_68): if numpy.array_equal(face_landmark_5, face_landmark_5_68):
mask_color = 255, 255, 0 mask_color = 255, 255, 0
if target_face.origin == 'refill':
mask_color = 0, 165, 255
if 'box' in state_manager.get_item('face_mask_types'): if 'box' in state_manager.get_item('face_mask_types'):
box_mask = create_box_mask(crop_vision_frame, 0, state_manager.get_item('face_mask_padding')) box_mask = create_box_mask(crop_vision_frame, 0, state_manager.get_item('face_mask_padding'))
crop_masks.append(box_mask) crop_masks.append(box_mask)
@@ -149,7 +161,7 @@ def draw_face_mask(target_face : Face, temp_vision_frame : VisionFrame) -> Visio
inverse_vision_frame = cv2.warpAffine(crop_mask, inverse_matrix, temp_size) inverse_vision_frame = cv2.warpAffine(crop_mask, inverse_matrix, temp_size)
inverse_vision_frame = cv2.threshold(inverse_vision_frame, 100, 255, cv2.THRESH_BINARY)[1] inverse_vision_frame = cv2.threshold(inverse_vision_frame, 100, 255, cv2.THRESH_BINARY)[1]
inverse_contours, _ = cv2.findContours(inverse_vision_frame, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) inverse_contours, _ = cv2.findContours(inverse_vision_frame, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(temp_vision_frame, inverse_contours, -1, mask_color, 2) cv2.drawContours(temp_vision_frame, inverse_contours, -1, mask_color, mask_scale)
return temp_vision_frame return temp_vision_frame
@@ -157,13 +169,17 @@ def draw_face_mask(target_face : Face, temp_vision_frame : VisionFrame) -> Visio
def draw_face_landmark_5(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame: def draw_face_landmark_5(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
temp_vision_frame = numpy.ascontiguousarray(temp_vision_frame) temp_vision_frame = numpy.ascontiguousarray(temp_vision_frame)
face_landmark_5 = target_face.landmark_set.get('5') face_landmark_5 = target_face.landmark_set.get('5')
point_scale = calculate_scale(temp_vision_frame)
point_color = 0, 0, 255 point_color = 0, 0, 255
if target_face.origin == 'refill':
point_color = 0, 165, 255
if numpy.any(face_landmark_5): if numpy.any(face_landmark_5):
face_landmark_5 = face_landmark_5.astype(numpy.int32) face_landmark_5 = face_landmark_5.astype(numpy.int32)
for point in face_landmark_5: for point in face_landmark_5:
cv2.circle(temp_vision_frame, tuple(point), 3, point_color, -1) cv2.circle(temp_vision_frame, tuple(point), point_scale, point_color, -1)
return temp_vision_frame return temp_vision_frame
@@ -172,16 +188,20 @@ def draw_face_landmark_5_68(target_face : Face, temp_vision_frame : VisionFrame)
temp_vision_frame = numpy.ascontiguousarray(temp_vision_frame) temp_vision_frame = numpy.ascontiguousarray(temp_vision_frame)
face_landmark_5 = target_face.landmark_set.get('5') face_landmark_5 = target_face.landmark_set.get('5')
face_landmark_5_68 = target_face.landmark_set.get('5/68') face_landmark_5_68 = target_face.landmark_set.get('5/68')
point_scale = calculate_scale(temp_vision_frame)
point_color = 0, 255, 0 point_color = 0, 255, 0
if numpy.array_equal(face_landmark_5, face_landmark_5_68): if numpy.array_equal(face_landmark_5, face_landmark_5_68):
point_color = 255, 255, 0 point_color = 255, 255, 0
if target_face.origin == 'refill':
point_color = 0, 165, 255
if numpy.any(face_landmark_5_68): if numpy.any(face_landmark_5_68):
face_landmark_5_68 = face_landmark_5_68.astype(numpy.int32) face_landmark_5_68 = face_landmark_5_68.astype(numpy.int32)
for point in face_landmark_5_68: for point in face_landmark_5_68:
cv2.circle(temp_vision_frame, tuple(point), 3, point_color, -1) cv2.circle(temp_vision_frame, tuple(point), point_scale, point_color, -1)
return temp_vision_frame return temp_vision_frame
@@ -190,16 +210,20 @@ def draw_face_landmark_68(target_face : Face, temp_vision_frame : VisionFrame) -
temp_vision_frame = numpy.ascontiguousarray(temp_vision_frame) temp_vision_frame = numpy.ascontiguousarray(temp_vision_frame)
face_landmark_68 = target_face.landmark_set.get('68') face_landmark_68 = target_face.landmark_set.get('68')
face_landmark_68_5 = target_face.landmark_set.get('68/5') face_landmark_68_5 = target_face.landmark_set.get('68/5')
point_scale = calculate_scale(temp_vision_frame)
point_color = 0, 255, 0 point_color = 0, 255, 0
if numpy.array_equal(face_landmark_68, face_landmark_68_5): if numpy.array_equal(face_landmark_68, face_landmark_68_5):
point_color = 255, 255, 0 point_color = 255, 255, 0
if target_face.origin == 'refill':
point_color = 0, 165, 255
if numpy.any(face_landmark_68): if numpy.any(face_landmark_68):
face_landmark_68 = face_landmark_68.astype(numpy.int32) face_landmark_68 = face_landmark_68.astype(numpy.int32)
for point in face_landmark_68: for point in face_landmark_68:
cv2.circle(temp_vision_frame, tuple(point), 3, point_color, -1) cv2.circle(temp_vision_frame, tuple(point), point_scale, point_color, -1)
return temp_vision_frame return temp_vision_frame
@@ -207,23 +231,36 @@ def draw_face_landmark_68(target_face : Face, temp_vision_frame : VisionFrame) -
def draw_face_landmark_68_5(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame: def draw_face_landmark_68_5(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
temp_vision_frame = numpy.ascontiguousarray(temp_vision_frame) temp_vision_frame = numpy.ascontiguousarray(temp_vision_frame)
face_landmark_68_5 = target_face.landmark_set.get('68/5') face_landmark_68_5 = target_face.landmark_set.get('68/5')
point_scale = calculate_scale(temp_vision_frame)
point_color = 255, 255, 0 point_color = 255, 255, 0
if target_face.origin == 'refill':
point_color = 0, 165, 255
if numpy.any(face_landmark_68_5): if numpy.any(face_landmark_68_5):
face_landmark_68_5 = face_landmark_68_5.astype(numpy.int32) face_landmark_68_5 = face_landmark_68_5.astype(numpy.int32)
for point in face_landmark_68_5: for point in face_landmark_68_5:
cv2.circle(temp_vision_frame, tuple(point), 3, point_color, -1) cv2.circle(temp_vision_frame, tuple(point), point_scale, point_color, -1)
return temp_vision_frame return temp_vision_frame
def calculate_scale(temp_vision_frame : VisionFrame) -> int:
frame_height, _ = temp_vision_frame.shape[:2]
frame_scale = round(frame_height / 270)
return max(1, min(10, frame_scale))
def process_frame(inputs : FaceDebuggerInputs) -> ProcessorOutputs: def process_frame(inputs : FaceDebuggerInputs) -> ProcessorOutputs:
reference_vision_frame = inputs.get('reference_vision_frame') reference_vision_frame = inputs.get('reference_vision_frame')
target_vision_frame = inputs.get('target_vision_frame') source_vision_frames = inputs.get('source_vision_frames')
target_vision_frames = inputs.get('target_vision_frames')
temp_vision_frame = inputs.get('temp_vision_frame') temp_vision_frame = inputs.get('temp_vision_frame')
temp_vision_mask = inputs.get('temp_vision_mask') temp_vision_mask = inputs.get('temp_vision_mask')
target_faces = select_faces(reference_vision_frame, target_vision_frame)
target_vision_frame = get_middle(target_vision_frames)
target_faces = select_faces(reference_vision_frame, source_vision_frames, target_vision_frames)
if target_faces: if target_faces:
for target_face in target_faces: for target_face in target_faces:
@@ -231,5 +268,3 @@ def process_frame(inputs : FaceDebuggerInputs) -> ProcessorOutputs:
temp_vision_frame = debug_face(target_face, temp_vision_frame) temp_vision_frame = debug_face(target_face, temp_vision_frame)
return temp_vision_frame, temp_vision_mask return temp_vision_frame, temp_vision_mask
@@ -1,11 +1,12 @@
from typing import Literal, TypedDict from typing import List, Literal, TypedDict
from facefusion.types import Mask, VisionFrame from facefusion.types import Mask, VisionFrame
FaceDebuggerInputs = TypedDict('FaceDebuggerInputs', FaceDebuggerInputs = TypedDict('FaceDebuggerInputs',
{ {
'reference_vision_frame' : VisionFrame, 'reference_vision_frame' : VisionFrame,
'target_vision_frame' : VisionFrame, 'source_vision_frames' : List[VisionFrame],
'target_vision_frames' : List[VisionFrame],
'temp_vision_frame' : VisionFrame, 'temp_vision_frame' : VisionFrame,
'temp_vision_mask' : Mask 'temp_vision_mask' : Mask
}) })
@@ -1,6 +1,7 @@
from argparse import ArgumentParser from argparse import ArgumentParser
from functools import lru_cache from functools import lru_cache
from typing import Tuple from types import ModuleType
from typing import List, Tuple
import cv2 import cv2
import numpy import numpy
@@ -8,9 +9,9 @@ import numpy
import facefusion.jobs.job_manager import facefusion.jobs.job_manager
import facefusion.jobs.job_store import facefusion.jobs.job_store
from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, inference_manager, logger, state_manager, translator, video_manager from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, inference_manager, logger, state_manager, translator, video_manager
from facefusion.common_helper import create_float_metavar from facefusion.common_helper import create_float_metavar, get_middle
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
from facefusion.face_analyser import scale_face from facefusion.face_creator import scale_face
from facefusion.face_helper import paste_back, scale_face_landmark_5, warp_face_by_face_landmark_5 from facefusion.face_helper import paste_back, scale_face_landmark_5, warp_face_by_face_landmark_5
from facefusion.face_masker import create_box_mask from facefusion.face_masker import create_box_mask
from facefusion.face_selector import select_faces from facefusion.face_selector import select_faces
@@ -165,10 +166,18 @@ def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('face_editor_head_roll', args.get('face_editor_head_roll')) apply_state_item('face_editor_head_roll', args.get('face_editor_head_roll'))
def get_common_modules() -> List[ModuleType]:
return [ content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer ]
def pre_check() -> bool: def pre_check() -> bool:
model_hash_set = get_model_options().get('hashes') model_hash_set = get_model_options().get('hashes')
model_source_set = get_model_options().get('sources') model_source_set = get_model_options().get('sources')
for common_module in get_common_modules():
if not common_module.pre_check():
return False
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set) return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
@@ -189,15 +198,13 @@ def post_process() -> None:
read_static_image.cache_clear() read_static_image.cache_clear()
read_static_video_frame.cache_clear() read_static_video_frame.cache_clear()
video_manager.clear_video_pool() video_manager.clear_video_pool()
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]: if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
clear_inference_pool() clear_inference_pool()
if state_manager.get_item('video_memory_strategy') == 'strict': if state_manager.get_item('video_memory_strategy') == 'strict':
content_analyser.clear_inference_pool() for common_module in get_common_modules():
face_classifier.clear_inference_pool() common_module.clear_inference_pool()
face_detector.clear_inference_pool()
face_landmarker.clear_inference_pool()
face_masker.clear_inference_pool()
face_recognizer.clear_inference_pool()
def edit_face(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame: def edit_face(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
@@ -486,10 +493,13 @@ def normalize_crop_frame(crop_vision_frame : VisionFrame) -> VisionFrame:
def process_frame(inputs : FaceEditorInputs) -> ProcessorOutputs: def process_frame(inputs : FaceEditorInputs) -> ProcessorOutputs:
reference_vision_frame = inputs.get('reference_vision_frame') reference_vision_frame = inputs.get('reference_vision_frame')
target_vision_frame = inputs.get('target_vision_frame') source_vision_frames = inputs.get('source_vision_frames')
target_vision_frames = inputs.get('target_vision_frames')
temp_vision_frame = inputs.get('temp_vision_frame') temp_vision_frame = inputs.get('temp_vision_frame')
temp_vision_mask = inputs.get('temp_vision_mask') temp_vision_mask = inputs.get('temp_vision_mask')
target_faces = select_faces(reference_vision_frame, target_vision_frame)
target_vision_frame = get_middle(target_vision_frames)
target_faces = select_faces(reference_vision_frame, source_vision_frames, target_vision_frames)
if target_faces: if target_faces:
for target_face in target_faces: for target_face in target_faces:
@@ -1,11 +1,12 @@
from typing import Literal, TypedDict from typing import List, Literal, TypedDict
from facefusion.types import Mask, VisionFrame from facefusion.types import Mask, VisionFrame
FaceEditorInputs = TypedDict('FaceEditorInputs', FaceEditorInputs = TypedDict('FaceEditorInputs',
{ {
'reference_vision_frame' : VisionFrame, 'reference_vision_frame' : VisionFrame,
'target_vision_frame' : VisionFrame, 'source_vision_frames' : List[VisionFrame],
'target_vision_frames' : List[VisionFrame],
'temp_vision_frame' : VisionFrame, 'temp_vision_frame' : VisionFrame,
'temp_vision_mask' : Mask 'temp_vision_mask' : Mask
}) })
@@ -1,14 +1,16 @@
from argparse import ArgumentParser from argparse import ArgumentParser
from functools import lru_cache from functools import lru_cache
from types import ModuleType
from typing import List
import numpy import numpy
import facefusion.jobs.job_manager import facefusion.jobs.job_manager
import facefusion.jobs.job_store import facefusion.jobs.job_store
from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, inference_manager, logger, state_manager, translator, video_manager from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, inference_manager, logger, state_manager, translator, video_manager
from facefusion.common_helper import create_float_metavar, create_int_metavar from facefusion.common_helper import create_float_metavar, create_int_metavar, get_middle
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
from facefusion.face_analyser import scale_face from facefusion.face_creator import scale_face
from facefusion.face_helper import paste_back, warp_face_by_face_landmark_5 from facefusion.face_helper import paste_back, warp_face_by_face_landmark_5
from facefusion.face_masker import create_box_mask, create_occlusion_mask from facefusion.face_masker import create_box_mask, create_occlusion_mask
from facefusion.face_selector import select_faces from facefusion.face_selector import select_faces
@@ -304,10 +306,18 @@ def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('face_enhancer_weight', args.get('face_enhancer_weight')) apply_state_item('face_enhancer_weight', args.get('face_enhancer_weight'))
def get_common_modules() -> List[ModuleType]:
return [ content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer ]
def pre_check() -> bool: def pre_check() -> bool:
model_hash_set = get_model_options().get('hashes') model_hash_set = get_model_options().get('hashes')
model_source_set = get_model_options().get('sources') model_source_set = get_model_options().get('sources')
for common_module in get_common_modules():
if not common_module.pre_check():
return False
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set) return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
@@ -328,15 +338,13 @@ def post_process() -> None:
read_static_image.cache_clear() read_static_image.cache_clear()
read_static_video_frame.cache_clear() read_static_video_frame.cache_clear()
video_manager.clear_video_pool() video_manager.clear_video_pool()
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]: if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
clear_inference_pool() clear_inference_pool()
if state_manager.get_item('video_memory_strategy') == 'strict': if state_manager.get_item('video_memory_strategy') == 'strict':
content_analyser.clear_inference_pool() for common_module in get_common_modules():
face_classifier.clear_inference_pool() common_module.clear_inference_pool()
face_detector.clear_inference_pool()
face_landmarker.clear_inference_pool()
face_masker.clear_inference_pool()
face_recognizer.clear_inference_pool()
def enhance_face(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame: def enhance_face(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
@@ -413,10 +421,13 @@ def blend_paste_frame(temp_vision_frame : VisionFrame, paste_vision_frame : Visi
def process_frame(inputs : FaceEnhancerInputs) -> ProcessorOutputs: def process_frame(inputs : FaceEnhancerInputs) -> ProcessorOutputs:
reference_vision_frame = inputs.get('reference_vision_frame') reference_vision_frame = inputs.get('reference_vision_frame')
target_vision_frame = inputs.get('target_vision_frame') source_vision_frames = inputs.get('source_vision_frames')
target_vision_frames = inputs.get('target_vision_frames')
temp_vision_frame = inputs.get('temp_vision_frame') temp_vision_frame = inputs.get('temp_vision_frame')
temp_vision_mask = inputs.get('temp_vision_mask') temp_vision_mask = inputs.get('temp_vision_mask')
target_faces = select_faces(reference_vision_frame, target_vision_frame)
target_vision_frame = get_middle(target_vision_frames)
target_faces = select_faces(reference_vision_frame, source_vision_frames, target_vision_frames)
if target_faces: if target_faces:
for target_face in target_faces: for target_face in target_faces:
@@ -1,4 +1,4 @@
from typing import Any, Literal, TypeAlias, TypedDict from typing import Any, List, Literal, TypeAlias, TypedDict
from numpy.typing import NDArray from numpy.typing import NDArray
@@ -7,7 +7,8 @@ from facefusion.types import Mask, VisionFrame
FaceEnhancerInputs = TypedDict('FaceEnhancerInputs', FaceEnhancerInputs = TypedDict('FaceEnhancerInputs',
{ {
'reference_vision_frame' : VisionFrame, 'reference_vision_frame' : VisionFrame,
'target_vision_frame' : VisionFrame, 'source_vision_frames' : List[VisionFrame],
'target_vision_frames' : List[VisionFrame],
'temp_vision_frame' : VisionFrame, 'temp_vision_frame' : VisionFrame,
'temp_vision_mask' : Mask 'temp_vision_mask' : Mask
}) })
@@ -1,5 +1,6 @@
from argparse import ArgumentParser from argparse import ArgumentParser
from functools import lru_cache from functools import lru_cache
from types import ModuleType
from typing import List, Optional, Tuple from typing import List, Optional, Tuple
import cv2 import cv2
@@ -9,10 +10,10 @@ import facefusion.choices
import facefusion.jobs.job_manager import facefusion.jobs.job_manager
import facefusion.jobs.job_store import facefusion.jobs.job_store
from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, inference_manager, logger, state_manager, translator, video_manager from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, inference_manager, logger, state_manager, translator, video_manager
from facefusion.common_helper import get_first, is_macos from facefusion.common_helper import get_first, get_middle, is_macos
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
from facefusion.execution import has_execution_provider from facefusion.execution import has_execution_provider
from facefusion.face_analyser import get_average_face, get_many_faces, get_one_face, scale_face from facefusion.face_creator import average_face_identity, get_one_face, get_static_faces, scale_face
from facefusion.face_helper import paste_back, warp_face_by_face_landmark_5 from facefusion.face_helper import paste_back, warp_face_by_face_landmark_5
from facefusion.face_masker import create_area_mask, create_box_mask, create_occlusion_mask, create_region_mask from facefusion.face_masker import create_area_mask, create_box_mask, create_occlusion_mask, create_region_mask
from facefusion.face_selector import select_faces, sort_faces_by_order from facefusion.face_selector import select_faces, sort_faces_by_order
@@ -24,7 +25,7 @@ from facefusion.processors.pixel_boost import explode_pixel_boost, implode_pixel
from facefusion.processors.types import ProcessorOutputs from facefusion.processors.types import ProcessorOutputs
from facefusion.program_helper import find_argument_group from facefusion.program_helper import find_argument_group
from facefusion.thread_helper import conditional_thread_semaphore from facefusion.thread_helper import conditional_thread_semaphore
from facefusion.types import ApplyStateItem, Args, DownloadScope, Embedding, Face, InferencePool, ModelOptions, ModelSet, ProcessMode, VisionFrame from facefusion.types import ApplyStateItem, Args, DownloadScope, Embedding, Face, InferencePool, InferenceProvider, ModelOptions, ModelSet, ProcessMode, VisionFrame
from facefusion.vision import read_static_image, read_static_images, read_static_video_frame, unpack_resolution from facefusion.vision import read_static_image, read_static_images, read_static_video_frame, unpack_resolution
@@ -246,6 +247,7 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
'path': resolve_relative_path('../.assets/models/hyperswap_1a_256.onnx') 'path': resolve_relative_path('../.assets/models/hyperswap_1a_256.onnx')
} }
}, },
'precision': 'fp16',
'type': 'hyperswap', 'type': 'hyperswap',
'template': 'arcface_128', 'template': 'arcface_128',
'size': (256, 256), 'size': (256, 256),
@@ -276,6 +278,7 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
'path': resolve_relative_path('../.assets/models/hyperswap_1b_256.onnx') 'path': resolve_relative_path('../.assets/models/hyperswap_1b_256.onnx')
} }
}, },
'precision': 'fp16',
'type': 'hyperswap', 'type': 'hyperswap',
'template': 'arcface_128', 'template': 'arcface_128',
'size': (256, 256), 'size': (256, 256),
@@ -306,6 +309,7 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
'path': resolve_relative_path('../.assets/models/hyperswap_1c_256.onnx') 'path': resolve_relative_path('../.assets/models/hyperswap_1c_256.onnx')
} }
}, },
'precision': 'fp16',
'type': 'hyperswap', 'type': 'hyperswap',
'template': 'arcface_128', 'template': 'arcface_128',
'size': (256, 256), 'size': (256, 256),
@@ -366,6 +370,7 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
'path': resolve_relative_path('../.assets/models/inswapper_128_fp16.onnx') 'path': resolve_relative_path('../.assets/models/inswapper_128_fp16.onnx')
} }
}, },
'precision': 'fp16',
'type': 'inswapper', 'type': 'inswapper',
'template': 'arcface_128', 'template': 'arcface_128',
'size': (128, 128), 'size': (128, 128),
@@ -486,28 +491,37 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
def get_inference_pool() -> InferencePool: def get_inference_pool() -> InferencePool:
model_names = [ get_model_name() ] model_names = [ state_manager.get_item('face_swapper_model') ]
model_source_set = get_model_options().get('sources') model_source_set = get_model_options().get('sources')
return inference_manager.get_inference_pool(__name__, model_names, model_source_set) return inference_manager.get_inference_pool(__name__, model_names, model_source_set)
def clear_inference_pool() -> None: def clear_inference_pool() -> None:
model_names = [ get_model_name() ] model_names = [ state_manager.get_item('face_swapper_model') ]
inference_manager.clear_inference_pool(__name__, model_names) inference_manager.clear_inference_pool(__name__, model_names)
def adjust_inference_providers() -> List[InferenceProvider]:
model_precision = get_model_options().get('precision')
model_type = get_model_options().get('type')
if is_macos() and has_execution_provider('coreml'):
if model_type in [ 'ghost', 'uniface' ] or model_precision == 'fp16':
return\
[
(facefusion.choices.execution_provider_set.get('coreml'),
{
'ModelFormat': 'MLProgram'
})
]
return []
def get_model_options() -> ModelOptions: def get_model_options() -> ModelOptions:
model_name = get_model_name()
return create_static_model_set('full').get(model_name)
def get_model_name() -> str:
model_name = state_manager.get_item('face_swapper_model') model_name = state_manager.get_item('face_swapper_model')
return create_static_model_set('full').get(model_name)
if is_macos() and has_execution_provider('coreml') and model_name == 'inswapper_128_fp16':
return 'inswapper_128'
return model_name
def register_args(program : ArgumentParser) -> None: def register_args(program : ArgumentParser) -> None:
@@ -527,10 +541,18 @@ def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('face_swapper_weight', args.get('face_swapper_weight')) apply_state_item('face_swapper_weight', args.get('face_swapper_weight'))
def get_common_modules() -> List[ModuleType]:
return [ content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer ]
def pre_check() -> bool: def pre_check() -> bool:
model_hash_set = get_model_options().get('hashes') model_hash_set = get_model_options().get('hashes')
model_source_set = get_model_options().get('sources') model_source_set = get_model_options().get('sources')
for common_module in get_common_modules():
if not common_module.pre_check():
return False
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set) return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
@@ -540,8 +562,8 @@ def pre_process(mode : ProcessMode) -> bool:
return False return False
source_image_paths = filter_image_paths(state_manager.get_item('source_paths')) source_image_paths = filter_image_paths(state_manager.get_item('source_paths'))
source_frames = read_static_images(source_image_paths) source_vision_frames = read_static_images(source_image_paths)
source_faces = get_many_faces(source_frames) source_faces = get_static_faces(source_vision_frames)
if not get_one_face(source_faces): if not get_one_face(source_faces):
logger.error(translator.get('no_source_face_detected') + translator.get('exclamation_mark'), __name__) logger.error(translator.get('no_source_face_detected') + translator.get('exclamation_mark'), __name__)
@@ -566,19 +588,17 @@ def post_process() -> None:
read_static_image.cache_clear() read_static_image.cache_clear()
read_static_video_frame.cache_clear() read_static_video_frame.cache_clear()
video_manager.clear_video_pool() video_manager.clear_video_pool()
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]: if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
get_static_model_initializer.cache_clear() get_static_model_initializer.cache_clear()
clear_inference_pool() clear_inference_pool()
if state_manager.get_item('video_memory_strategy') == 'strict': if state_manager.get_item('video_memory_strategy') == 'strict':
content_analyser.clear_inference_pool() for common_module in get_common_modules():
face_classifier.clear_inference_pool() common_module.clear_inference_pool()
face_detector.clear_inference_pool()
face_landmarker.clear_inference_pool()
face_masker.clear_inference_pool()
face_recognizer.clear_inference_pool()
def swap_face(source_face : Face, target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame: def swap_face(source_face : Face, target_face : Face, source_vision_frame : VisionFrame, temp_vision_frame : VisionFrame) -> VisionFrame:
model_template = get_model_options().get('template') model_template = get_model_options().get('template')
model_size = get_model_options().get('size') model_size = get_model_options().get('size')
pixel_boost_size = unpack_resolution(state_manager.get_item('face_swapper_pixel_boost')) pixel_boost_size = unpack_resolution(state_manager.get_item('face_swapper_pixel_boost'))
@@ -598,7 +618,7 @@ def swap_face(source_face : Face, target_face : Face, temp_vision_frame : Vision
pixel_boost_vision_frames = implode_pixel_boost(crop_vision_frame, pixel_boost_total, model_size) pixel_boost_vision_frames = implode_pixel_boost(crop_vision_frame, pixel_boost_total, model_size)
for pixel_boost_vision_frame in pixel_boost_vision_frames: for pixel_boost_vision_frame in pixel_boost_vision_frames:
pixel_boost_vision_frame = prepare_crop_frame(pixel_boost_vision_frame) pixel_boost_vision_frame = prepare_crop_frame(pixel_boost_vision_frame)
pixel_boost_vision_frame = forward_swap_face(source_face, target_face, pixel_boost_vision_frame) pixel_boost_vision_frame = forward_swap_face(source_face, target_face, source_vision_frame, pixel_boost_vision_frame)
pixel_boost_vision_frame = normalize_crop_frame(pixel_boost_vision_frame) pixel_boost_vision_frame = normalize_crop_frame(pixel_boost_vision_frame)
temp_vision_frames.append(pixel_boost_vision_frame) temp_vision_frames.append(pixel_boost_vision_frame)
crop_vision_frame = explode_pixel_boost(temp_vision_frames, pixel_boost_total, model_size, pixel_boost_size) crop_vision_frame = explode_pixel_boost(temp_vision_frames, pixel_boost_total, model_size, pixel_boost_size)
@@ -617,18 +637,15 @@ def swap_face(source_face : Face, target_face : Face, temp_vision_frame : Vision
return paste_vision_frame return paste_vision_frame
def forward_swap_face(source_face : Face, target_face : Face, crop_vision_frame : VisionFrame) -> VisionFrame: def forward_swap_face(source_face : Face, target_face : Face, source_vision_frame : VisionFrame, crop_vision_frame : VisionFrame) -> VisionFrame:
face_swapper = get_inference_pool().get('face_swapper') face_swapper = get_inference_pool().get('face_swapper')
model_type = get_model_options().get('type') model_type = get_model_options().get('type')
face_swapper_inputs = {} face_swapper_inputs = {}
if is_macos() and has_execution_provider('coreml') and model_type in [ 'ghost', 'uniface' ]:
face_swapper.set_providers([ facefusion.choices.execution_provider_set.get('cpu') ])
for face_swapper_input in face_swapper.get_inputs(): for face_swapper_input in face_swapper.get_inputs():
if face_swapper_input.name == 'source': if face_swapper_input.name == 'source':
if model_type in [ 'blendswap', 'uniface' ]: if model_type in [ 'blendswap', 'uniface' ]:
face_swapper_inputs[face_swapper_input.name] = prepare_source_frame(source_face) face_swapper_inputs[face_swapper_input.name] = prepare_source_frame(source_face, source_vision_frame)
else: else:
source_embedding = prepare_source_embedding(source_face) source_embedding = prepare_source_embedding(source_face)
source_embedding = balance_source_embedding(source_embedding, target_face.embedding) source_embedding = balance_source_embedding(source_embedding, target_face.embedding)
@@ -654,9 +671,8 @@ def forward_convert_embedding(face_embedding : Embedding) -> Embedding:
return face_embedding return face_embedding
def prepare_source_frame(source_face : Face) -> VisionFrame: def prepare_source_frame(source_face : Face, source_vision_frame : VisionFrame) -> VisionFrame:
model_type = get_model_options().get('type') model_type = get_model_options().get('type')
source_vision_frame = read_static_image(get_first(state_manager.get_item('source_paths')))
if model_type == 'blendswap': if model_type == 'blendswap':
source_vision_frame, _ = warp_face_by_face_landmark_5(source_vision_frame, source_face.landmark_set.get('5/68'), 'arcface_112_v2', (112, 112)) source_vision_frame, _ = warp_face_by_face_landmark_5(source_vision_frame, source_face.landmark_set.get('5/68'), 'arcface_112_v2', (112, 112))
@@ -748,27 +764,31 @@ def extract_source_face(source_vision_frames : List[VisionFrame]) -> Optional[Fa
if source_vision_frames: if source_vision_frames:
for source_vision_frame in source_vision_frames: for source_vision_frame in source_vision_frames:
temp_faces = get_many_faces([source_vision_frame]) temp_faces = get_static_faces([ source_vision_frame ])
temp_faces = sort_faces_by_order(temp_faces, 'large-small') temp_faces = sort_faces_by_order(temp_faces, 'large-small')
if temp_faces: if temp_faces:
source_faces.append(get_first(temp_faces)) source_faces.append(get_first(temp_faces))
return get_average_face(source_faces) return average_face_identity(source_faces)
def process_frame(inputs : FaceSwapperInputs) -> ProcessorOutputs: def process_frame(inputs : FaceSwapperInputs) -> ProcessorOutputs:
reference_vision_frame = inputs.get('reference_vision_frame') reference_vision_frame = inputs.get('reference_vision_frame')
source_vision_frames = inputs.get('source_vision_frames') source_vision_frames = inputs.get('source_vision_frames')
target_vision_frame = inputs.get('target_vision_frame') target_vision_frames = inputs.get('target_vision_frames')
temp_vision_frame = inputs.get('temp_vision_frame') temp_vision_frame = inputs.get('temp_vision_frame')
temp_vision_mask = inputs.get('temp_vision_mask') temp_vision_mask = inputs.get('temp_vision_mask')
target_vision_frame = get_middle(target_vision_frames)
source_face = extract_source_face(source_vision_frames) source_face = extract_source_face(source_vision_frames)
target_faces = select_faces(reference_vision_frame, target_vision_frame) target_faces = select_faces(reference_vision_frame, source_vision_frames, target_vision_frames)
if source_face and target_faces: if source_face and target_faces:
source_vision_frame = get_first(source_vision_frames)
for target_face in target_faces: for target_face in target_faces:
target_face = scale_face(target_face, target_vision_frame, temp_vision_frame) target_face = scale_face(target_face, target_vision_frame, temp_vision_frame)
temp_vision_frame = swap_face(source_face, target_face, temp_vision_frame) temp_vision_frame = swap_face(source_face, target_face, source_vision_frame, temp_vision_frame)
return temp_vision_frame, temp_vision_mask return temp_vision_frame, temp_vision_mask
@@ -6,7 +6,7 @@ FaceSwapperInputs = TypedDict('FaceSwapperInputs',
{ {
'reference_vision_frame' : VisionFrame, 'reference_vision_frame' : VisionFrame,
'source_vision_frames' : List[VisionFrame], 'source_vision_frames' : List[VisionFrame],
'target_vision_frame' : VisionFrame, 'target_vision_frames' : List[VisionFrame],
'temp_vision_frame' : VisionFrame, 'temp_vision_frame' : VisionFrame,
'temp_vision_mask' : Mask 'temp_vision_mask' : Mask
}) })
@@ -1,10 +1,12 @@
from argparse import ArgumentParser from argparse import ArgumentParser
from functools import lru_cache from functools import lru_cache
from types import ModuleType
from typing import List from typing import List
import cv2 import cv2
import numpy import numpy
import facefusion.choices
import facefusion.jobs.job_manager import facefusion.jobs.job_manager
import facefusion.jobs.job_store import facefusion.jobs.job_store
from facefusion import config, content_analyser, inference_manager, logger, state_manager, translator, video_manager from facefusion import config, content_analyser, inference_manager, logger, state_manager, translator, video_manager
@@ -17,7 +19,7 @@ from facefusion.processors.modules.frame_colorizer.types import FrameColorizerIn
from facefusion.processors.types import ProcessorOutputs from facefusion.processors.types import ProcessorOutputs
from facefusion.program_helper import find_argument_group from facefusion.program_helper import find_argument_group
from facefusion.thread_helper import thread_semaphore from facefusion.thread_helper import thread_semaphore
from facefusion.types import ApplyStateItem, Args, DownloadScope, ExecutionProvider, InferencePool, ModelOptions, ModelSet, ProcessMode, VisionFrame from facefusion.types import ApplyStateItem, Args, DownloadScope, InferencePool, InferenceProvider, ModelOptions, ModelSet, ProcessMode, VisionFrame
from facefusion.vision import blend_frame, read_static_image, read_static_video_frame, unpack_resolution from facefusion.vision import blend_frame, read_static_image, read_static_video_frame, unpack_resolution
@@ -170,10 +172,11 @@ def clear_inference_pool() -> None:
inference_manager.clear_inference_pool(__name__, model_names) inference_manager.clear_inference_pool(__name__, model_names)
def resolve_execution_providers() -> List[ExecutionProvider]: def override_inference_providers() -> List[InferenceProvider]:
if is_macos() and has_execution_provider('coreml'): if is_macos() and has_execution_provider('coreml'):
return [ 'cpu' ] return [ facefusion.choices.execution_provider_set.get('cpu') ]
return state_manager.get_item('execution_providers')
return []
def get_model_options() -> ModelOptions: def get_model_options() -> ModelOptions:
@@ -196,10 +199,18 @@ def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('frame_colorizer_size', args.get('frame_colorizer_size')) apply_state_item('frame_colorizer_size', args.get('frame_colorizer_size'))
def get_common_modules() -> List[ModuleType]:
return [ content_analyser ]
def pre_check() -> bool: def pre_check() -> bool:
model_hash_set = get_model_options().get('hashes') model_hash_set = get_model_options().get('hashes')
model_source_set = get_model_options().get('sources') model_source_set = get_model_options().get('sources')
for common_module in get_common_modules():
if not common_module.pre_check():
return False
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set) return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
@@ -220,10 +231,13 @@ def post_process() -> None:
read_static_image.cache_clear() read_static_image.cache_clear()
read_static_video_frame.cache_clear() read_static_video_frame.cache_clear()
video_manager.clear_video_pool() video_manager.clear_video_pool()
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]: if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
clear_inference_pool() clear_inference_pool()
if state_manager.get_item('video_memory_strategy') == 'strict': if state_manager.get_item('video_memory_strategy') == 'strict':
content_analyser.clear_inference_pool() for common_module in get_common_modules():
common_module.clear_inference_pool()
def colorize_frame(temp_vision_frame : VisionFrame) -> VisionFrame: def colorize_frame(temp_vision_frame : VisionFrame) -> VisionFrame:
@@ -1,10 +1,10 @@
from typing import Literal, TypedDict from typing import List, Literal, TypedDict
from facefusion.types import Mask, VisionFrame from facefusion.types import Mask, VisionFrame
FrameColorizerInputs = TypedDict('FrameColorizerInputs', FrameColorizerInputs = TypedDict('FrameColorizerInputs',
{ {
'target_vision_frame' : VisionFrame, 'target_vision_frames' : List[VisionFrame],
'temp_vision_frame' : VisionFrame, 'temp_vision_frame' : VisionFrame,
'temp_vision_mask' : Mask 'temp_vision_mask' : Mask
}) })
@@ -1,9 +1,12 @@
from argparse import ArgumentParser from argparse import ArgumentParser
from functools import lru_cache from functools import lru_cache
from types import ModuleType
from typing import List
import cv2 import cv2
import numpy import numpy
import facefusion.choices
import facefusion.jobs.job_manager import facefusion.jobs.job_manager
import facefusion.jobs.job_store import facefusion.jobs.job_store
from facefusion import config, content_analyser, inference_manager, logger, state_manager, translator, video_manager from facefusion import config, content_analyser, inference_manager, logger, state_manager, translator, video_manager
@@ -16,7 +19,7 @@ from facefusion.processors.modules.frame_enhancer.types import FrameEnhancerInpu
from facefusion.processors.types import ProcessorOutputs from facefusion.processors.types import ProcessorOutputs
from facefusion.program_helper import find_argument_group from facefusion.program_helper import find_argument_group
from facefusion.thread_helper import conditional_thread_semaphore from facefusion.thread_helper import conditional_thread_semaphore
from facefusion.types import ApplyStateItem, Args, DownloadScope, InferencePool, ModelOptions, ModelSet, ProcessMode, VisionFrame from facefusion.types import ApplyStateItem, Args, DownloadScope, InferencePool, InferenceProvider, ModelOptions, ModelSet, ProcessMode, VisionFrame
from facefusion.vision import blend_frame, create_tile_frames, merge_tile_frames, read_static_image, read_static_video_frame from facefusion.vision import blend_frame, create_tile_frames, merge_tile_frames, read_static_image, read_static_video_frame
@@ -56,7 +59,7 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
'__metadata__': '__metadata__':
{ {
'vendor': 'Helaman', 'vendor': 'Helaman',
'license': 'Non-Commercial', 'license': 'CC-BY-4.0',
'year': 2023 'year': 2023
}, },
'hashes': 'hashes':
@@ -83,7 +86,7 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
'__metadata__': '__metadata__':
{ {
'vendor': 'Phhofm', 'vendor': 'Phhofm',
'license': 'Non-Commercial', 'license': 'CC-BY-4.0',
'year': 2023 'year': 2023
}, },
'hashes': 'hashes':
@@ -156,6 +159,7 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
'path': resolve_relative_path('../.assets/models/real_esrgan_x2_fp16.onnx') 'path': resolve_relative_path('../.assets/models/real_esrgan_x2_fp16.onnx')
} }
}, },
'precision': 'fp16',
'size': (256, 16, 8), 'size': (256, 16, 8),
'scale': 2 'scale': 2
}, },
@@ -210,6 +214,7 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
'path': resolve_relative_path('../.assets/models/real_esrgan_x4_fp16.onnx') 'path': resolve_relative_path('../.assets/models/real_esrgan_x4_fp16.onnx')
} }
}, },
'precision': 'fp16',
'size': (256, 16, 8), 'size': (256, 16, 8),
'scale': 4 'scale': 4
}, },
@@ -264,6 +269,7 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
'path': resolve_relative_path('../.assets/models/real_esrgan_x8_fp16.onnx') 'path': resolve_relative_path('../.assets/models/real_esrgan_x8_fp16.onnx')
} }
}, },
'precision': 'fp16',
'size': (256, 16, 8), 'size': (256, 16, 8),
'scale': 8 'scale': 8
}, },
@@ -299,7 +305,7 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
'__metadata__': '__metadata__':
{ {
'vendor': 'Helaman', 'vendor': 'Helaman',
'license': 'Non-Commercial', 'license': 'CC-BY-4.0',
'year': 2024 'year': 2024
}, },
'hashes': 'hashes':
@@ -541,35 +547,37 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
def get_inference_pool() -> InferencePool: def get_inference_pool() -> InferencePool:
model_names = [ get_frame_enhancer_model() ] model_names = [ state_manager.get_item('frame_enhancer_model') ]
model_source_set = get_model_options().get('sources') model_source_set = get_model_options().get('sources')
return inference_manager.get_inference_pool(__name__, model_names, model_source_set) return inference_manager.get_inference_pool(__name__, model_names, model_source_set)
def clear_inference_pool() -> None: def clear_inference_pool() -> None:
model_names = [ get_frame_enhancer_model() ] model_names = [ state_manager.get_item('frame_enhancer_model') ]
inference_manager.clear_inference_pool(__name__, model_names) inference_manager.clear_inference_pool(__name__, model_names)
def adjust_inference_providers() -> List[InferenceProvider]:
model_precision = get_model_options().get('precision')
if is_macos() and has_execution_provider('coreml') and model_precision == 'fp16':
return\
[
(facefusion.choices.execution_provider_set.get('coreml'),
{
'ModelFormat': 'MLProgram'
})
]
return []
def get_model_options() -> ModelOptions: def get_model_options() -> ModelOptions:
model_name = get_frame_enhancer_model() model_name = state_manager.get_item('frame_enhancer_model')
return create_static_model_set('full').get(model_name) return create_static_model_set('full').get(model_name)
def get_frame_enhancer_model() -> str:
frame_enhancer_model = state_manager.get_item('frame_enhancer_model')
if is_macos() and has_execution_provider('coreml'):
if frame_enhancer_model == 'real_esrgan_x2_fp16':
return 'real_esrgan_x2'
if frame_enhancer_model == 'real_esrgan_x4_fp16':
return 'real_esrgan_x4'
if frame_enhancer_model == 'real_esrgan_x8_fp16':
return 'real_esrgan_x8'
return frame_enhancer_model
def register_args(program : ArgumentParser) -> None: def register_args(program : ArgumentParser) -> None:
group_processors = find_argument_group(program, 'processors') group_processors = find_argument_group(program, 'processors')
if group_processors: if group_processors:
@@ -583,10 +591,18 @@ def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('frame_enhancer_blend', args.get('frame_enhancer_blend')) apply_state_item('frame_enhancer_blend', args.get('frame_enhancer_blend'))
def get_common_modules() -> List[ModuleType]:
return [ content_analyser ]
def pre_check() -> bool: def pre_check() -> bool:
model_hash_set = get_model_options().get('hashes') model_hash_set = get_model_options().get('hashes')
model_source_set = get_model_options().get('sources') model_source_set = get_model_options().get('sources')
for common_module in get_common_modules():
if not common_module.pre_check():
return False
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set) return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
@@ -607,10 +623,13 @@ def post_process() -> None:
read_static_image.cache_clear() read_static_image.cache_clear()
read_static_video_frame.cache_clear() read_static_video_frame.cache_clear()
video_manager.clear_video_pool() video_manager.clear_video_pool()
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]: if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
clear_inference_pool() clear_inference_pool()
if state_manager.get_item('video_memory_strategy') == 'strict': if state_manager.get_item('video_memory_strategy') == 'strict':
content_analyser.clear_inference_pool() for common_module in get_common_modules():
common_module.clear_inference_pool()
def enhance_frame(temp_vision_frame : VisionFrame) -> VisionFrame: def enhance_frame(temp_vision_frame : VisionFrame) -> VisionFrame:
@@ -1,10 +1,10 @@
from typing import Literal, TypedDict from typing import List, Literal, TypedDict
from facefusion.types import Mask, VisionFrame from facefusion.types import Mask, VisionFrame
FrameEnhancerInputs = TypedDict('FrameEnhancerInputs', FrameEnhancerInputs = TypedDict('FrameEnhancerInputs',
{ {
'target_vision_frame' : VisionFrame, 'target_vision_frames' : List[VisionFrame],
'temp_vision_frame' : VisionFrame, 'temp_vision_frame' : VisionFrame,
'temp_vision_mask' : Mask 'temp_vision_mask' : Mask
}) })
@@ -1,5 +1,7 @@
from argparse import ArgumentParser from argparse import ArgumentParser
from functools import lru_cache from functools import lru_cache
from types import ModuleType
from typing import List
import cv2 import cv2
import numpy import numpy
@@ -8,9 +10,9 @@ import facefusion.jobs.job_manager
import facefusion.jobs.job_store import facefusion.jobs.job_store
from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, inference_manager, logger, state_manager, translator, video_manager, voice_extractor from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, inference_manager, logger, state_manager, translator, video_manager, voice_extractor
from facefusion.audio import read_static_voice from facefusion.audio import read_static_voice
from facefusion.common_helper import create_float_metavar from facefusion.common_helper import create_float_metavar, get_middle
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
from facefusion.face_analyser import scale_face from facefusion.face_creator import scale_face
from facefusion.face_helper import create_bounding_box, paste_back, warp_face_by_bounding_box, warp_face_by_face_landmark_5 from facefusion.face_helper import create_bounding_box, paste_back, warp_face_by_bounding_box, warp_face_by_face_landmark_5
from facefusion.face_masker import create_area_mask, create_box_mask, create_occlusion_mask from facefusion.face_masker import create_area_mask, create_box_mask, create_occlusion_mask
from facefusion.face_selector import select_faces from facefusion.face_selector import select_faces
@@ -142,10 +144,18 @@ def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
apply_state_item('lip_syncer_weight', args.get('lip_syncer_weight')) apply_state_item('lip_syncer_weight', args.get('lip_syncer_weight'))
def get_common_modules() -> List[ModuleType]:
return [ content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, voice_extractor ]
def pre_check() -> bool: def pre_check() -> bool:
model_hash_set = get_model_options().get('hashes') model_hash_set = get_model_options().get('hashes')
model_source_set = get_model_options().get('sources') model_source_set = get_model_options().get('sources')
for common_module in get_common_modules():
if not common_module.pre_check():
return False
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set) return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
@@ -161,16 +171,13 @@ def post_process() -> None:
read_static_video_frame.cache_clear() read_static_video_frame.cache_clear()
read_static_voice.cache_clear() read_static_voice.cache_clear()
video_manager.clear_video_pool() video_manager.clear_video_pool()
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]: if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
clear_inference_pool() clear_inference_pool()
if state_manager.get_item('video_memory_strategy') == 'strict': if state_manager.get_item('video_memory_strategy') == 'strict':
content_analyser.clear_inference_pool() for common_module in get_common_modules():
face_classifier.clear_inference_pool() common_module.clear_inference_pool()
face_detector.clear_inference_pool()
face_landmarker.clear_inference_pool()
face_masker.clear_inference_pool()
face_recognizer.clear_inference_pool()
voice_extractor.clear_inference_pool()
def sync_lip(target_face : Face, source_voice_frame : AudioFrame, temp_vision_frame : VisionFrame) -> VisionFrame: def sync_lip(target_face : Face, source_voice_frame : AudioFrame, temp_vision_frame : VisionFrame) -> VisionFrame:
@@ -282,11 +289,14 @@ def normalize_crop_frame(crop_vision_frame : VisionFrame) -> VisionFrame:
def process_frame(inputs : LipSyncerInputs) -> ProcessorOutputs: def process_frame(inputs : LipSyncerInputs) -> ProcessorOutputs:
reference_vision_frame = inputs.get('reference_vision_frame') reference_vision_frame = inputs.get('reference_vision_frame')
source_vision_frames = inputs.get('source_vision_frames')
source_voice_frame = inputs.get('source_voice_frame') source_voice_frame = inputs.get('source_voice_frame')
target_vision_frame = inputs.get('target_vision_frame') target_vision_frames = inputs.get('target_vision_frames')
temp_vision_frame = inputs.get('temp_vision_frame') temp_vision_frame = inputs.get('temp_vision_frame')
temp_vision_mask = inputs.get('temp_vision_mask') temp_vision_mask = inputs.get('temp_vision_mask')
target_faces = select_faces(reference_vision_frame, target_vision_frame)
target_vision_frame = get_middle(target_vision_frames)
target_faces = select_faces(reference_vision_frame, source_vision_frames, target_vision_frames)
if target_faces: if target_faces:
for target_face in target_faces: for target_face in target_faces:
@@ -1,4 +1,4 @@
from typing import Any, Literal, TypeAlias, TypedDict from typing import Any, List, Literal, TypeAlias, TypedDict
from numpy.typing import NDArray from numpy.typing import NDArray
@@ -7,8 +7,9 @@ from facefusion.types import AudioFrame, Mask, VisionFrame
LipSyncerInputs = TypedDict('LipSyncerInputs', LipSyncerInputs = TypedDict('LipSyncerInputs',
{ {
'reference_vision_frame' : VisionFrame, 'reference_vision_frame' : VisionFrame,
'source_vision_frames' : List[VisionFrame],
'source_voice_frame' : AudioFrame, 'source_voice_frame' : AudioFrame,
'target_vision_frame' : VisionFrame, 'target_vision_frames' : List[VisionFrame],
'temp_vision_frame' : VisionFrame, 'temp_vision_frame' : VisionFrame,
'temp_vision_mask' : Mask 'temp_vision_mask' : Mask
}) })
+29 -5
View File
@@ -21,6 +21,15 @@ def create_help_formatter_large(prog : str) -> HelpFormatter:
return HelpFormatter(prog, max_help_position = 300) return HelpFormatter(prog, max_help_position = 300)
def create_workflow_program() -> ArgumentParser:
program = ArgumentParser(add_help = False)
group_workflow = program.add_argument_group('workflow')
group_workflow.add_argument('--workflow-mode', help = translator.get('help.workflow_mode'), default = config.get_str_value('workflow', 'workflow_mode', 'auto'), choices = facefusion.choices.workflow_modes)
group_workflow.add_argument('--workflow-strategy', help = translator.get('help.workflow_strategy'), default = config.get_str_value('workflow', 'workflow_strategy', 'stream'), choices = facefusion.choices.workflow_strategies)
job_store.register_step_keys([ 'workflow_mode', 'workflow_strategy' ])
return program
def create_config_path_program() -> ArgumentParser: def create_config_path_program() -> ArgumentParser:
program = ArgumentParser(add_help = False) program = ArgumentParser(add_help = False)
group_paths = program.add_argument_group('paths') group_paths = program.add_argument_group('paths')
@@ -133,6 +142,14 @@ def create_face_selector_program() -> ArgumentParser:
return program return program
def create_face_tracker_program() -> ArgumentParser:
program = ArgumentParser(add_help = False)
group_face_tracker = program.add_argument_group('face tracker')
group_face_tracker.add_argument('--face-tracker-score', help = translator.get('help.face_tracker_score'), type = float, default = config.get_float_value('face_tracker', 'face_tracker_score', '0.0'), choices = facefusion.choices.face_tracker_score_range, metavar = create_float_metavar(facefusion.choices.face_tracker_score_range))
job_store.register_step_keys([ 'face_tracker_score' ])
return program
def create_face_masker_program() -> ArgumentParser: def create_face_masker_program() -> ArgumentParser:
program = ArgumentParser(add_help = False) program = ArgumentParser(add_help = False)
group_face_masker = program.add_argument_group('face masker') group_face_masker = program.add_argument_group('face masker')
@@ -161,8 +178,16 @@ def create_frame_extraction_program() -> ArgumentParser:
group_frame_extraction.add_argument('--trim-frame-start', help = translator.get('help.trim_frame_start'), type = int, default = facefusion.config.get_int_value('frame_extraction', 'trim_frame_start')) group_frame_extraction.add_argument('--trim-frame-start', help = translator.get('help.trim_frame_start'), type = int, default = facefusion.config.get_int_value('frame_extraction', 'trim_frame_start'))
group_frame_extraction.add_argument('--trim-frame-end', help = translator.get('help.trim_frame_end'), type = int, default = facefusion.config.get_int_value('frame_extraction', 'trim_frame_end')) group_frame_extraction.add_argument('--trim-frame-end', help = translator.get('help.trim_frame_end'), type = int, default = facefusion.config.get_int_value('frame_extraction', 'trim_frame_end'))
group_frame_extraction.add_argument('--temp-frame-format', help = translator.get('help.temp_frame_format'), default = config.get_str_value('frame_extraction', 'temp_frame_format', 'png'), choices = facefusion.choices.temp_frame_formats) group_frame_extraction.add_argument('--temp-frame-format', help = translator.get('help.temp_frame_format'), default = config.get_str_value('frame_extraction', 'temp_frame_format', 'png'), choices = facefusion.choices.temp_frame_formats)
group_frame_extraction.add_argument('--keep-temp', help = translator.get('help.keep_temp'), action = 'store_true', default = config.get_bool_value('frame_extraction', 'keep_temp')) group_frame_extraction.add_argument('--temp-pixel-format', help = translator.get('help.temp_pixel_format'), default = config.get_str_value('frame_extraction', 'temp_pixel_format', 'bgr24'), choices = facefusion.choices.temp_pixel_formats)
job_store.register_step_keys([ 'trim_frame_start', 'trim_frame_end', 'temp_frame_format', 'keep_temp' ]) job_store.register_step_keys([ 'trim_frame_start', 'trim_frame_end', 'temp_frame_format', 'temp_pixel_format' ])
return program
def create_frame_distribution_program() -> ArgumentParser:
program = ArgumentParser(add_help = False)
group_frame_distribution = program.add_argument_group('frame distribution')
group_frame_distribution.add_argument('--target-frame-amount', help = translator.get('help.target_frame_amount'), type = int, default = config.get_int_value('frame_distribution', 'target_frame_amount', '2'), choices = facefusion.choices.target_frame_amount_range, metavar = create_int_metavar(facefusion.choices.target_frame_amount_range))
job_store.register_step_keys([ 'target_frame_amount' ])
return program return program
@@ -245,8 +270,7 @@ def create_memory_program() -> ArgumentParser:
program = ArgumentParser(add_help = False) program = ArgumentParser(add_help = False)
group_memory = program.add_argument_group('memory') group_memory = program.add_argument_group('memory')
group_memory.add_argument('--video-memory-strategy', help = translator.get('help.video_memory_strategy'), default = config.get_str_value('memory', 'video_memory_strategy', 'strict'), choices = facefusion.choices.video_memory_strategies) group_memory.add_argument('--video-memory-strategy', help = translator.get('help.video_memory_strategy'), default = config.get_str_value('memory', 'video_memory_strategy', 'strict'), choices = facefusion.choices.video_memory_strategies)
group_memory.add_argument('--system-memory-limit', help = translator.get('help.system_memory_limit'), type = int, default = config.get_int_value('memory', 'system_memory_limit', '0'), choices = facefusion.choices.system_memory_limit_range, metavar = create_int_metavar(facefusion.choices.system_memory_limit_range)) job_store.register_job_keys([ 'video_memory_strategy' ])
job_store.register_job_keys([ 'video_memory_strategy', 'system_memory_limit' ])
return program return program
@@ -285,7 +309,7 @@ def create_step_index_program() -> ArgumentParser:
def collect_step_program() -> ArgumentParser: def collect_step_program() -> ArgumentParser:
return ArgumentParser(parents = [ create_face_detector_program(), create_face_landmarker_program(), create_face_selector_program(), create_face_masker_program(), create_voice_extractor_program(), create_frame_extraction_program(), create_output_creation_program(), create_processors_program() ], add_help = False) return ArgumentParser(parents = [ create_workflow_program(), create_face_detector_program(), create_face_landmarker_program(), create_face_selector_program(), create_face_tracker_program(), create_face_masker_program(), create_voice_extractor_program(), create_frame_extraction_program(), create_frame_distribution_program(), create_output_creation_program(), create_processors_program() ], add_help = False)
def collect_job_program() -> ArgumentParser: def collect_job_program() -> ArgumentParser:
+7 -2
View File
@@ -1,5 +1,7 @@
import hashlib import hashlib
from typing import Sequence from typing import Any, Sequence
from facefusion.common_helper import cast_int
def sanitize_job_id(job_id : str) -> str: def sanitize_job_id(job_id : str) -> str:
@@ -7,10 +9,13 @@ def sanitize_job_id(job_id : str) -> str:
if __job_id__.isalnum(): if __job_id__.isalnum():
return job_id return job_id
return hashlib.sha1(job_id.encode()).hexdigest() return hashlib.sha1(job_id.encode()).hexdigest()
def sanitize_int_range(value : int, int_range : Sequence[int]) -> int: def sanitize_int_range(value : Any, int_range : Sequence[int]) -> int:
value = cast_int(value)
if value in int_range: if value in int_range:
return value return value
return int_range[0] return int_range[0]
+10 -10
View File
@@ -2,7 +2,7 @@ import os
import subprocess import subprocess
from collections import deque from collections import deque
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
from typing import Deque, Iterator from typing import Deque, Iterator, List
import cv2 import cv2
import numpy import numpy
@@ -20,23 +20,24 @@ from facefusion.vision import extract_vision_mask, read_static_images
def multi_process_capture(camera_capture : cv2.VideoCapture, camera_fps : Fps) -> Iterator[VisionFrame]: def multi_process_capture(camera_capture : cv2.VideoCapture, camera_fps : Fps) -> Iterator[VisionFrame]:
capture_deque : Deque[VisionFrame] = deque() capture_deque : Deque[VisionFrame] = deque()
source_vision_frames = read_static_images(state_manager.get_item('source_paths'))
with tqdm(desc = translator.get('streaming'), unit = 'frame', disable = state_manager.get_item('log_level') in [ 'warn', 'error' ]) as progress: with tqdm(desc = translator.get('streaming'), unit = 'frame', disable = state_manager.get_item('log_level') in [ 'warn', 'error' ]) as progress:
with ThreadPoolExecutor(max_workers = state_manager.get_item('execution_thread_count')) as executor: with ThreadPoolExecutor(max_workers = state_manager.get_item('execution_thread_count')) as executor:
futures = [] futures = []
while camera_capture and camera_capture.isOpened(): while camera_capture and camera_capture.isOpened():
_, capture_frame = camera_capture.read() _, capture_vision_frame = camera_capture.read()
if analyse_stream(capture_frame, camera_fps): if analyse_stream(capture_vision_frame, camera_fps):
camera_capture.release() camera_capture.release()
if numpy.any(capture_frame): if numpy.any(capture_vision_frame):
future = executor.submit(process_stream_frame, capture_frame) future = executor.submit(process_stream_frame, source_vision_frames, capture_vision_frame)
futures.append(future) futures.append(future)
for future_done in [ future for future in futures if future.done() ]: for future_done in [ future for future in futures if future.done() ]:
capture_frame = future_done.result() capture_vision_frame = future_done.result()
capture_deque.append(capture_frame) capture_deque.append(capture_vision_frame)
futures.remove(future_done) futures.remove(future_done)
while capture_deque: while capture_deque:
@@ -44,8 +45,7 @@ def multi_process_capture(camera_capture : cv2.VideoCapture, camera_fps : Fps) -
yield capture_deque.popleft() yield capture_deque.popleft()
def process_stream_frame(target_vision_frame : VisionFrame) -> VisionFrame: def process_stream_frame(source_vision_frames : List[VisionFrame], target_vision_frame : VisionFrame) -> VisionFrame:
source_vision_frames = read_static_images(state_manager.get_item('source_paths'))
source_audio_frame = create_empty_audio_frame() source_audio_frame = create_empty_audio_frame()
source_voice_frame = create_empty_audio_frame() source_voice_frame = create_empty_audio_frame()
temp_vision_frame = target_vision_frame.copy() temp_vision_frame = target_vision_frame.copy()
@@ -60,7 +60,7 @@ def process_stream_frame(target_vision_frame : VisionFrame) -> VisionFrame:
'source_vision_frames': source_vision_frames, 'source_vision_frames': source_vision_frames,
'source_audio_frame': source_audio_frame, 'source_audio_frame': source_audio_frame,
'source_voice_frame': source_voice_frame, 'source_voice_frame': source_voice_frame,
'target_vision_frame': target_vision_frame, 'target_vision_frames': [ target_vision_frame ],
'temp_vision_frame': temp_vision_frame, 'temp_vision_frame': temp_vision_frame,
'temp_vision_mask': temp_vision_mask 'temp_vision_mask': temp_vision_mask
}) })
+13 -9
View File
@@ -1,8 +1,8 @@
import os import os
from typing import List
from facefusion import state_manager from facefusion import state_manager
from facefusion.filesystem import create_directory, get_file_extension, get_file_name, move_file, remove_directory, resolve_file_pattern from facefusion.filesystem import create_directory, get_file_extension, get_file_name, move_file, remove_directory, resolve_file_pattern
from facefusion.types import FrameSet
def get_temp_file_path(file_path : str) -> str: def get_temp_file_path(file_path : str) -> str:
@@ -16,12 +16,18 @@ def move_temp_file(file_path : str, move_path : str) -> bool:
return move_file(temp_file_path, move_path) return move_file(temp_file_path, move_path)
def resolve_temp_frame_paths(target_path : str) -> List[str]: def resolve_temp_frame_set(target_path : str) -> FrameSet:
temp_frames_pattern = get_temp_frames_pattern(target_path, '*') temp_frame_pattern = get_temp_frame_pattern(target_path, '*')
return resolve_file_pattern(temp_frames_pattern) temp_frame_set = {}
for temp_frame_path in resolve_file_pattern(temp_frame_pattern):
frame_number = int(get_file_name(temp_frame_path))
temp_frame_set[frame_number] = temp_frame_path
return temp_frame_set
def get_temp_frames_pattern(target_path : str, temp_frame_prefix : str) -> str: def get_temp_frame_pattern(target_path : str, temp_frame_prefix : str) -> str:
temp_directory_path = get_temp_directory_path(target_path) temp_directory_path = get_temp_directory_path(target_path)
return os.path.join(temp_directory_path, temp_frame_prefix + '.' + state_manager.get_item('temp_frame_format')) return os.path.join(temp_directory_path, temp_frame_prefix + '.' + state_manager.get_item('temp_frame_format'))
@@ -37,7 +43,5 @@ def create_temp_directory(file_path : str) -> bool:
def clear_temp_directory(file_path : str) -> bool: def clear_temp_directory(file_path : str) -> bool:
if not state_manager.get_item('keep_temp'): temp_directory_path = get_temp_directory_path(file_path)
temp_directory_path = get_temp_directory_path(file_path) return remove_directory(temp_directory_path)
return remove_directory(temp_directory_path)
return True
+90 -22
View File
@@ -1,5 +1,7 @@
import subprocess
from collections import namedtuple from collections import namedtuple
from typing import Any, Callable, Dict, List, Literal, Optional, Tuple, TypeAlias, TypedDict from threading import Lock
from typing import Any, Callable, Dict, List, Literal, NotRequired, Optional, Tuple, TypeAlias, TypedDict
import cv2 import cv2
import numpy import numpy
@@ -29,45 +31,51 @@ FaceScoreSet = TypedDict('FaceScoreSet',
'landmarker' : Score 'landmarker' : Score
}) })
Embedding : TypeAlias = NDArray[numpy.float64] Embedding : TypeAlias = NDArray[numpy.float64]
Gender = Literal['female', 'male']
Age : TypeAlias = range Age : TypeAlias = range
Gender = Literal['female', 'male']
Race = Literal['white', 'black', 'latino', 'asian', 'indian', 'arabic'] Race = Literal['white', 'black', 'latino', 'asian', 'indian', 'arabic']
FaceSelectorGender = Literal['auto', 'female', 'male']
FaceSelectorRace = Literal['auto', 'white', 'black', 'latino', 'asian', 'indian', 'arabic']
Face = namedtuple('Face', Face = namedtuple('Face',
[ [
'origin',
'bounding_box', 'bounding_box',
'score_set', 'score_set',
'landmark_set', 'landmark_set',
'angle', 'angle',
'embedding', 'embedding',
'embedding_norm', 'embedding_norm',
'gender',
'age', 'age',
'gender',
'race' 'race'
]) ])
FaceSet : TypeAlias = Dict[str, List[Face]] FaceSet = TypedDict('FaceSet',
FaceStore = TypedDict('FaceStore',
{ {
'static_faces' : FaceSet 'lock': Lock,
'faces': NotRequired[List[Face]]
}) })
FaceStore : TypeAlias = Dict[str, FaceSet]
FaceTrack : TypeAlias = Dict[int, Face]
Language = Literal['en'] Language = Literal['en']
Locales : TypeAlias = Dict[Language, Dict[str, Any]] Locales : TypeAlias = Dict[Language, Dict[str, Any]]
LocalePoolSet : TypeAlias = Dict[str, Locales] LocalePoolSet : TypeAlias = Dict[str, Locales]
VideoCaptureSet : TypeAlias = Dict[str, cv2.VideoCapture] WorkflowMode = Literal['auto', 'image-to-image', 'image-to-video']
VideoWriterSet : TypeAlias = Dict[str, cv2.VideoWriter] WorkflowStrategy = Literal['disk', 'stream']
CameraCaptureSet : TypeAlias = Dict[str, cv2.VideoCapture] CameraCaptureSet : TypeAlias = Dict[str, cv2.VideoCapture]
VideoPoolSet = TypedDict('VideoPoolSet',
{
'capture': VideoCaptureSet,
'writer': VideoWriterSet
})
CameraPoolSet = TypedDict('CameraPoolSet', CameraPoolSet = TypedDict('CameraPoolSet',
{ {
'capture': CameraCaptureSet 'capture' : CameraCaptureSet
}) })
ColorMode = Literal['rgb', 'rgba'] ColorMode = Literal['rgb', 'rgba']
ColorSpace = Literal['bt601', 'bt709', 'bt2020']
ColorTransfer : TypeAlias = str
VisionFrame : TypeAlias = NDArray[Any] VisionFrame : TypeAlias = NDArray[Any]
Mask : TypeAlias = NDArray[Any] Mask : TypeAlias = NDArray[Any]
Points : TypeAlias = NDArray[Any] Points : TypeAlias = NDArray[Any]
@@ -86,13 +94,64 @@ MelFilterBank : TypeAlias = NDArray[Any]
Voice : TypeAlias = NDArray[Any] Voice : TypeAlias = NDArray[Any]
VoiceChunk : TypeAlias = NDArray[Any] VoiceChunk : TypeAlias = NDArray[Any]
BitRate : TypeAlias = int
SampleRate : TypeAlias = int
Fps : TypeAlias = float Fps : TypeAlias = float
Duration : TypeAlias = float Duration : TypeAlias = float
Buffer : TypeAlias = bytes
VisionFrameSet : TypeAlias = Dict[int, VisionFrame]
Color : TypeAlias = Tuple[int, int, int, int] Color : TypeAlias = Tuple[int, int, int, int]
Padding : TypeAlias = Tuple[int, int, int, int] Padding : TypeAlias = Tuple[int, int, int, int]
Margin : TypeAlias = Tuple[int, int, int, int] Margin : TypeAlias = Tuple[int, int, int, int]
Orientation = Literal['landscape', 'portrait'] Orientation = Literal['landscape', 'portrait']
Resolution : TypeAlias = Tuple[int, int] Resolution : TypeAlias = Tuple[int, int]
AudioMetadata = TypedDict('AudioMetadata',
{
'duration' : Duration,
'frame_total' : int,
'channel_total' : int,
'sample_rate' : SampleRate,
'bit_rate' : BitRate
})
VideoMetadata = TypedDict('VideoMetadata',
{
'duration' : Duration,
'frame_total' : int,
'fps' : Fps,
'resolution' : Resolution,
'bit_rate' : BitRate,
'color_transfer' : ColorTransfer
})
VideoReaderMetadata : TypeAlias = VideoMetadata
VideoWriterMetadata = TypedDict('VideoWriterMetadata',
{
'fps' : Fps,
'resolution' : Resolution
})
VideoReader = TypedDict('VideoReader',
{
'id' : str,
'file_path' : str,
'process' : subprocess.Popen[bytes],
'metadata' : VideoReaderMetadata,
'frame_number' : int
})
VideoReaderSet : TypeAlias = Dict[str, VideoReader]
VideoWriter = TypedDict('VideoWriter',
{
'id' : str,
'file_path' : str,
'process' : subprocess.Popen[bytes],
'metadata' : VideoWriterMetadata
})
VideoWriterSet : TypeAlias = Dict[str, VideoWriter]
VideoPoolSet = TypedDict('VideoPoolSet',
{
'reader' : VideoReaderSet,
'writer' : VideoWriterSet
})
FrameStoreSet : TypeAlias = Dict[str, VisionFrameSet]
ProcessState = Literal['checking', 'processing', 'stopping', 'pending'] ProcessState = Literal['checking', 'processing', 'stopping', 'pending']
Args : TypeAlias = Dict[str, Any] Args : TypeAlias = Dict[str, Any]
@@ -134,10 +193,13 @@ AudioFormat = Literal['flac', 'm4a', 'mp3', 'ogg', 'opus', 'wav']
ImageFormat = Literal['bmp', 'jpeg', 'png', 'tiff', 'webp'] ImageFormat = Literal['bmp', 'jpeg', 'png', 'tiff', 'webp']
VideoFormat = Literal['avi', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mxf', 'webm', 'wmv'] VideoFormat = Literal['avi', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mxf', 'webm', 'wmv']
TempFrameFormat = Literal['bmp', 'jpeg', 'png', 'tiff'] TempFrameFormat = Literal['bmp', 'jpeg', 'png', 'tiff']
TempPixelFormat = Literal['bgr24', 'bgra']
AudioTypeSet : TypeAlias = Dict[AudioFormat, str] AudioTypeSet : TypeAlias = Dict[AudioFormat, str]
ImageTypeSet : TypeAlias = Dict[ImageFormat, str] ImageTypeSet : TypeAlias = Dict[ImageFormat, str]
VideoTypeSet : TypeAlias = Dict[VideoFormat, str] VideoTypeSet : TypeAlias = Dict[VideoFormat, str]
FrameSet : TypeAlias = Dict[int, str]
AudioEncoder = Literal['flac', 'aac', 'libmp3lame', 'libopus', 'libvorbis', 'pcm_s16le', 'pcm_s32le'] AudioEncoder = Literal['flac', 'aac', 'libmp3lame', 'libopus', 'libvorbis', 'pcm_s16le', 'pcm_s32le']
VideoEncoder = Literal['libx264', 'libx264rgb', 'libx265', 'libvpx-vp9', 'h264_nvenc', 'hevc_nvenc', 'h264_amf', 'hevc_amf', 'h264_qsv', 'hevc_qsv', 'h264_videotoolbox', 'hevc_videotoolbox', 'rawvideo'] VideoEncoder = Literal['libx264', 'libx264rgb', 'libx265', 'libvpx-vp9', 'h264_nvenc', 'hevc_nvenc', 'h264_amf', 'hevc_amf', 'h264_qsv', 'hevc_qsv', 'h264_videotoolbox', 'hevc_videotoolbox', 'rawvideo']
EncoderSet = TypedDict('EncoderSet', EncoderSet = TypedDict('EncoderSet',
@@ -260,6 +322,8 @@ JobSet : TypeAlias = Dict[str, Job]
StateKey = Literal\ StateKey = Literal\
[ [
'command', 'command',
'workflow_mode',
'workflow_strategy',
'config_path', 'config_path',
'temp_path', 'temp_path',
'jobs_path', 'jobs_path',
@@ -290,6 +354,7 @@ StateKey = Literal\
'reference_face_position', 'reference_face_position',
'reference_face_distance', 'reference_face_distance',
'reference_frame_number', 'reference_frame_number',
'face_tracker_score',
'face_occluder_model', 'face_occluder_model',
'face_parser_model', 'face_parser_model',
'face_mask_types', 'face_mask_types',
@@ -301,7 +366,8 @@ StateKey = Literal\
'trim_frame_start', 'trim_frame_start',
'trim_frame_end', 'trim_frame_end',
'temp_frame_format', 'temp_frame_format',
'keep_temp', 'temp_pixel_format',
'target_frame_amount',
'output_image_quality', 'output_image_quality',
'output_image_scale', 'output_image_scale',
'output_audio_encoder', 'output_audio_encoder',
@@ -320,7 +386,6 @@ StateKey = Literal\
'execution_providers', 'execution_providers',
'execution_thread_count', 'execution_thread_count',
'video_memory_strategy', 'video_memory_strategy',
'system_memory_limit',
'log_level', 'log_level',
'halt_on_error', 'halt_on_error',
'job_id', 'job_id',
@@ -330,6 +395,8 @@ StateKey = Literal\
State = TypedDict('State', State = TypedDict('State',
{ {
'command' : str, 'command' : str,
'workflow_mode' : WorkflowMode,
'workflow_strategy' : WorkflowStrategy,
'config_path' : str, 'config_path' : str,
'temp_path' : str, 'temp_path' : str,
'jobs_path' : str, 'jobs_path' : str,
@@ -346,20 +413,21 @@ State = TypedDict('State',
'benchmark_cycle_count' : int, 'benchmark_cycle_count' : int,
'face_detector_model' : FaceDetectorModel, 'face_detector_model' : FaceDetectorModel,
'face_detector_size' : str, 'face_detector_size' : str,
'face_detector_margin': Margin, 'face_detector_margin' : Margin,
'face_detector_angles' : List[Angle], 'face_detector_angles' : List[Angle],
'face_detector_score' : Score, 'face_detector_score' : Score,
'face_landmarker_model' : FaceLandmarkerModel, 'face_landmarker_model' : FaceLandmarkerModel,
'face_landmarker_score' : Score, 'face_landmarker_score' : Score,
'face_selector_mode' : FaceSelectorMode, 'face_selector_mode' : FaceSelectorMode,
'face_selector_order' : FaceSelectorOrder, 'face_selector_order' : FaceSelectorOrder,
'face_selector_race' : Race, 'face_selector_race' : FaceSelectorRace,
'face_selector_gender' : Gender, 'face_selector_gender' : FaceSelectorGender,
'face_selector_age_start' : int, 'face_selector_age_start' : int,
'face_selector_age_end' : int, 'face_selector_age_end' : int,
'reference_face_position' : int, 'reference_face_position' : int,
'reference_face_distance' : float, 'reference_face_distance' : float,
'reference_frame_number' : int, 'reference_frame_number' : int,
'face_tracker_score' : Score,
'face_occluder_model' : FaceOccluderModel, 'face_occluder_model' : FaceOccluderModel,
'face_parser_model' : FaceParserModel, 'face_parser_model' : FaceParserModel,
'face_mask_types' : List[FaceMaskType], 'face_mask_types' : List[FaceMaskType],
@@ -367,11 +435,12 @@ State = TypedDict('State',
'face_mask_regions' : List[FaceMaskRegion], 'face_mask_regions' : List[FaceMaskRegion],
'face_mask_blur' : float, 'face_mask_blur' : float,
'face_mask_padding' : Padding, 'face_mask_padding' : Padding,
'voice_extractor_model': VoiceExtractorModel, 'voice_extractor_model' : VoiceExtractorModel,
'trim_frame_start' : int, 'trim_frame_start' : int,
'trim_frame_end' : int, 'trim_frame_end' : int,
'temp_frame_format' : TempFrameFormat, 'temp_frame_format' : TempFrameFormat,
'keep_temp' : bool, 'temp_pixel_format' : TempPixelFormat,
'target_frame_amount' : int,
'output_image_quality' : int, 'output_image_quality' : int,
'output_image_scale' : Scale, 'output_image_scale' : Scale,
'output_audio_encoder' : AudioEncoder, 'output_audio_encoder' : AudioEncoder,
@@ -390,7 +459,6 @@ State = TypedDict('State',
'execution_providers' : List[ExecutionProvider], 'execution_providers' : List[ExecutionProvider],
'execution_thread_count' : int, 'execution_thread_count' : int,
'video_memory_strategy' : VideoMemoryStrategy, 'video_memory_strategy' : VideoMemoryStrategy,
'system_memory_limit' : int,
'log_level' : LogLevel, 'log_level' : LogLevel,
'halt_on_error' : bool, 'halt_on_error' : bool,
'job_id' : str, 'job_id' : str,
+6 -2
View File
@@ -62,10 +62,14 @@
width: 1.125rem; width: 1.125rem;
} }
:root:root:root:root .thumbnail-item :root:root:root:root .gallery-container .thumbnail-item
{ {
border: unset; border: unset;
box-shadow: unset; }
:root:root:root:root .gallery-container .grid-container
{
grid-template-columns: repeat(7, 1fr);
} }
:root:root:root:root .grid-wrap.fixed-height :root:root:root:root .grid-wrap.fixed-height
+1 -3
View File
@@ -6,15 +6,13 @@ from facefusion.uis.types import JobManagerAction, JobRunnerAction, PreviewMode
job_manager_actions : List[JobManagerAction] = [ 'job-create', 'job-submit', 'job-delete', 'job-add-step', 'job-remix-step', 'job-insert-step', 'job-remove-step' ] job_manager_actions : List[JobManagerAction] = [ 'job-create', 'job-submit', 'job-delete', 'job-add-step', 'job-remix-step', 'job-insert-step', 'job-remove-step' ]
job_runner_actions : List[JobRunnerAction] = [ 'job-run', 'job-run-all', 'job-retry', 'job-retry-all' ] job_runner_actions : List[JobRunnerAction] = [ 'job-run', 'job-run-all', 'job-retry', 'job-retry-all' ]
common_options : List[str] = [ 'keep-temp' ]
preview_modes : List[PreviewMode] = [ 'default', 'frame-by-frame', 'face-by-face' ] preview_modes : List[PreviewMode] = [ 'default', 'frame-by-frame', 'face-by-face' ]
preview_resolutions : List[str] = [ '512x512', '768x768', '1024x1024' ] preview_resolutions : List[str] = [ '512x512', '768x768', '1024x1024' ]
webcam_modes : List[WebcamMode] = [ 'inline', 'udp', 'v4l2' ] webcam_modes : List[WebcamMode] = [ 'inline', 'udp', 'v4l2' ]
webcam_resolutions : List[str] = [ '320x240', '640x480', '800x600', '1024x768', '1280x720', '1280x960', '1920x1080' ] webcam_resolutions : List[str] = [ '320x240', '640x480', '800x600', '1024x768', '1280x720', '1280x960', '1920x1080' ]
background_remover_colors : Dict[str, Color] =\ background_remover_fill_colors : Dict[str, Color] =\
{ {
'red' : (255, 0, 0, 255), 'red' : (255, 0, 0, 255),
'green' : (0, 255, 0, 255), 'green' : (0, 255, 0, 255),
@@ -11,82 +11,132 @@ from facefusion.sanitizer import sanitize_int_range
from facefusion.uis.core import get_ui_component, register_ui_component from facefusion.uis.core import get_ui_component, register_ui_component
BACKGROUND_REMOVER_MODEL_DROPDOWN : Optional[gradio.Dropdown] = None BACKGROUND_REMOVER_MODEL_DROPDOWN : Optional[gradio.Dropdown] = None
BACKGROUND_REMOVER_COLOR_WRAPPER : Optional[gradio.Group] = None BACKGROUND_REMOVER_FILL_COLOR_WRAPPER : Optional[gradio.Group] = None
BACKGROUND_REMOVER_COLOR_RED_NUMBER : Optional[gradio.Number] = None BACKGROUND_REMOVER_FILL_COLOR_RED_NUMBER : Optional[gradio.Number] = None
BACKGROUND_REMOVER_COLOR_GREEN_NUMBER : Optional[gradio.Number] = None BACKGROUND_REMOVER_FILL_COLOR_GREEN_NUMBER : Optional[gradio.Number] = None
BACKGROUND_REMOVER_COLOR_BLUE_NUMBER : Optional[gradio.Number] = None BACKGROUND_REMOVER_FILL_COLOR_BLUE_NUMBER : Optional[gradio.Number] = None
BACKGROUND_REMOVER_COLOR_ALPHA_NUMBER : Optional[gradio.Number] = None BACKGROUND_REMOVER_FILL_COLOR_ALPHA_NUMBER : Optional[gradio.Number] = None
BACKGROUND_REMOVER_DESPILL_COLOR_WRAPPER : Optional[gradio.Group] = None
BACKGROUND_REMOVER_DESPILL_COLOR_RED_NUMBER : Optional[gradio.Number] = None
BACKGROUND_REMOVER_DESPILL_COLOR_GREEN_NUMBER : Optional[gradio.Number] = None
BACKGROUND_REMOVER_DESPILL_COLOR_BLUE_NUMBER : Optional[gradio.Number] = None
BACKGROUND_REMOVER_DESPILL_COLOR_ALPHA_NUMBER : Optional[gradio.Number] = None
def render() -> None: def render() -> None:
global BACKGROUND_REMOVER_MODEL_DROPDOWN global BACKGROUND_REMOVER_MODEL_DROPDOWN
global BACKGROUND_REMOVER_COLOR_WRAPPER global BACKGROUND_REMOVER_FILL_COLOR_WRAPPER
global BACKGROUND_REMOVER_COLOR_RED_NUMBER global BACKGROUND_REMOVER_FILL_COLOR_RED_NUMBER
global BACKGROUND_REMOVER_COLOR_GREEN_NUMBER global BACKGROUND_REMOVER_FILL_COLOR_GREEN_NUMBER
global BACKGROUND_REMOVER_COLOR_BLUE_NUMBER global BACKGROUND_REMOVER_FILL_COLOR_BLUE_NUMBER
global BACKGROUND_REMOVER_COLOR_ALPHA_NUMBER global BACKGROUND_REMOVER_FILL_COLOR_ALPHA_NUMBER
global BACKGROUND_REMOVER_DESPILL_COLOR_WRAPPER
global BACKGROUND_REMOVER_DESPILL_COLOR_RED_NUMBER
global BACKGROUND_REMOVER_DESPILL_COLOR_GREEN_NUMBER
global BACKGROUND_REMOVER_DESPILL_COLOR_BLUE_NUMBER
global BACKGROUND_REMOVER_DESPILL_COLOR_ALPHA_NUMBER
has_background_remover = 'background_remover' in state_manager.get_item('processors') has_background_remover = 'background_remover' in state_manager.get_item('processors')
background_remover_color = state_manager.get_item('background_remover_color') background_remover_fill_color = state_manager.get_item('background_remover_fill_color')
background_remover_despill_color = state_manager.get_item('background_remover_despill_color')
BACKGROUND_REMOVER_MODEL_DROPDOWN = gradio.Dropdown( BACKGROUND_REMOVER_MODEL_DROPDOWN = gradio.Dropdown(
label = translator.get('uis.model_dropdown', 'facefusion.processors.modules.background_remover'), label = translator.get('uis.model_dropdown', 'facefusion.processors.modules.background_remover'),
choices = background_remover_choices.background_remover_models, choices = background_remover_choices.background_remover_models,
value = state_manager.get_item('background_remover_model'), value = state_manager.get_item('background_remover_model'),
visible = has_background_remover visible = has_background_remover
) )
with gradio.Group(visible = has_background_remover) as BACKGROUND_REMOVER_COLOR_WRAPPER: with gradio.Group(visible = has_background_remover) as BACKGROUND_REMOVER_FILL_COLOR_WRAPPER:
with gradio.Row(): with gradio.Row():
BACKGROUND_REMOVER_COLOR_RED_NUMBER = gradio.Number( BACKGROUND_REMOVER_FILL_COLOR_RED_NUMBER = gradio.Number(
label = translator.get('uis.color_red_number', 'facefusion.processors.modules.background_remover'), label = translator.get('uis.fill_color_red_number', 'facefusion.processors.modules.background_remover'),
value = background_remover_color[0], value = background_remover_fill_color[0],
minimum = background_remover_choices.background_remover_color_range[0], minimum = background_remover_choices.background_remover_color_range[0],
maximum = background_remover_choices.background_remover_color_range[-1], maximum = background_remover_choices.background_remover_color_range[-1],
step = calculate_int_step(background_remover_choices.background_remover_color_range) step = calculate_int_step(background_remover_choices.background_remover_color_range)
) )
BACKGROUND_REMOVER_COLOR_GREEN_NUMBER = gradio.Number( BACKGROUND_REMOVER_FILL_COLOR_GREEN_NUMBER = gradio.Number(
label = translator.get('uis.color_green_number', 'facefusion.processors.modules.background_remover'), label = translator.get('uis.fill_color_green_number', 'facefusion.processors.modules.background_remover'),
value = background_remover_color[1], value = background_remover_fill_color[1],
minimum = background_remover_choices.background_remover_color_range[0], minimum = background_remover_choices.background_remover_color_range[0],
maximum = background_remover_choices.background_remover_color_range[-1], maximum = background_remover_choices.background_remover_color_range[-1],
step = calculate_int_step(background_remover_choices.background_remover_color_range) step = calculate_int_step(background_remover_choices.background_remover_color_range)
) )
with gradio.Row(): with gradio.Row():
BACKGROUND_REMOVER_COLOR_BLUE_NUMBER = gradio.Number( BACKGROUND_REMOVER_FILL_COLOR_BLUE_NUMBER = gradio.Number(
label = translator.get('uis.color_blue_number', 'facefusion.processors.modules.background_remover'), label = translator.get('uis.fill_color_blue_number', 'facefusion.processors.modules.background_remover'),
value = background_remover_color[2], value = background_remover_fill_color[2],
minimum = background_remover_choices.background_remover_color_range[0], minimum = background_remover_choices.background_remover_color_range[0],
maximum = background_remover_choices.background_remover_color_range[-1], maximum = background_remover_choices.background_remover_color_range[-1],
step = calculate_int_step(background_remover_choices.background_remover_color_range) step = calculate_int_step(background_remover_choices.background_remover_color_range)
) )
BACKGROUND_REMOVER_COLOR_ALPHA_NUMBER = gradio.Number( BACKGROUND_REMOVER_FILL_COLOR_ALPHA_NUMBER = gradio.Number(
label = translator.get('uis.color_alpha_number', 'facefusion.processors.modules.background_remover'), label = translator.get('uis.fill_color_alpha_number', 'facefusion.processors.modules.background_remover'),
value = background_remover_color[3], value = background_remover_fill_color[3],
minimum = background_remover_choices.background_remover_color_range[0],
maximum = background_remover_choices.background_remover_color_range[-1],
step = calculate_int_step(background_remover_choices.background_remover_color_range)
)
with gradio.Group(visible = has_background_remover) as BACKGROUND_REMOVER_DESPILL_COLOR_WRAPPER:
with gradio.Row():
BACKGROUND_REMOVER_DESPILL_COLOR_RED_NUMBER = gradio.Number(
label = translator.get('uis.despill_color_red_number', 'facefusion.processors.modules.background_remover'),
value = background_remover_despill_color[0],
minimum = background_remover_choices.background_remover_color_range[0],
maximum = background_remover_choices.background_remover_color_range[-1],
step = calculate_int_step(background_remover_choices.background_remover_color_range)
)
BACKGROUND_REMOVER_DESPILL_COLOR_GREEN_NUMBER = gradio.Number(
label = translator.get('uis.despill_color_green_number', 'facefusion.processors.modules.background_remover'),
value = background_remover_despill_color[1],
minimum = background_remover_choices.background_remover_color_range[0],
maximum = background_remover_choices.background_remover_color_range[-1],
step = calculate_int_step(background_remover_choices.background_remover_color_range)
)
with gradio.Row():
BACKGROUND_REMOVER_DESPILL_COLOR_BLUE_NUMBER = gradio.Number(
label = translator.get('uis.despill_color_blue_number', 'facefusion.processors.modules.background_remover'),
value = background_remover_despill_color[2],
minimum = background_remover_choices.background_remover_color_range[0],
maximum = background_remover_choices.background_remover_color_range[-1],
step = calculate_int_step(background_remover_choices.background_remover_color_range)
)
BACKGROUND_REMOVER_DESPILL_COLOR_ALPHA_NUMBER = gradio.Number(
label = translator.get('uis.despill_color_alpha_number', 'facefusion.processors.modules.background_remover'),
value = background_remover_despill_color[3],
minimum = background_remover_choices.background_remover_color_range[0], minimum = background_remover_choices.background_remover_color_range[0],
maximum = background_remover_choices.background_remover_color_range[-1], maximum = background_remover_choices.background_remover_color_range[-1],
step = calculate_int_step(background_remover_choices.background_remover_color_range) step = calculate_int_step(background_remover_choices.background_remover_color_range)
) )
register_ui_component('background_remover_model_dropdown', BACKGROUND_REMOVER_MODEL_DROPDOWN) register_ui_component('background_remover_model_dropdown', BACKGROUND_REMOVER_MODEL_DROPDOWN)
register_ui_component('background_remover_color_red_number', BACKGROUND_REMOVER_COLOR_RED_NUMBER) register_ui_component('background_remover_fill_color_red_number', BACKGROUND_REMOVER_FILL_COLOR_RED_NUMBER)
register_ui_component('background_remover_color_green_number', BACKGROUND_REMOVER_COLOR_GREEN_NUMBER) register_ui_component('background_remover_fill_color_green_number', BACKGROUND_REMOVER_FILL_COLOR_GREEN_NUMBER)
register_ui_component('background_remover_color_blue_number', BACKGROUND_REMOVER_COLOR_BLUE_NUMBER) register_ui_component('background_remover_fill_color_blue_number', BACKGROUND_REMOVER_FILL_COLOR_BLUE_NUMBER)
register_ui_component('background_remover_color_alpha_number', BACKGROUND_REMOVER_COLOR_ALPHA_NUMBER) register_ui_component('background_remover_fill_color_alpha_number', BACKGROUND_REMOVER_FILL_COLOR_ALPHA_NUMBER)
register_ui_component('background_remover_despill_color_red_number', BACKGROUND_REMOVER_DESPILL_COLOR_RED_NUMBER)
register_ui_component('background_remover_despill_color_green_number', BACKGROUND_REMOVER_DESPILL_COLOR_GREEN_NUMBER)
register_ui_component('background_remover_despill_color_blue_number', BACKGROUND_REMOVER_DESPILL_COLOR_BLUE_NUMBER)
register_ui_component('background_remover_despill_color_alpha_number', BACKGROUND_REMOVER_DESPILL_COLOR_ALPHA_NUMBER)
def listen() -> None: def listen() -> None:
BACKGROUND_REMOVER_MODEL_DROPDOWN.change(update_background_remover_model, inputs = BACKGROUND_REMOVER_MODEL_DROPDOWN, outputs = BACKGROUND_REMOVER_MODEL_DROPDOWN) BACKGROUND_REMOVER_MODEL_DROPDOWN.change(update_background_remover_model, inputs = BACKGROUND_REMOVER_MODEL_DROPDOWN, outputs = BACKGROUND_REMOVER_MODEL_DROPDOWN)
background_remover_color_inputs = [ BACKGROUND_REMOVER_COLOR_RED_NUMBER, BACKGROUND_REMOVER_COLOR_GREEN_NUMBER, BACKGROUND_REMOVER_COLOR_BLUE_NUMBER, BACKGROUND_REMOVER_COLOR_ALPHA_NUMBER ] background_remover_fill_color_inputs = [ BACKGROUND_REMOVER_FILL_COLOR_RED_NUMBER, BACKGROUND_REMOVER_FILL_COLOR_GREEN_NUMBER, BACKGROUND_REMOVER_FILL_COLOR_BLUE_NUMBER, BACKGROUND_REMOVER_FILL_COLOR_ALPHA_NUMBER ]
background_remover_despill_color_inputs = [ BACKGROUND_REMOVER_DESPILL_COLOR_RED_NUMBER, BACKGROUND_REMOVER_DESPILL_COLOR_GREEN_NUMBER, BACKGROUND_REMOVER_DESPILL_COLOR_BLUE_NUMBER, BACKGROUND_REMOVER_DESPILL_COLOR_ALPHA_NUMBER ]
for background_remover_color_input in background_remover_color_inputs: for background_remover_fill_color_input in background_remover_fill_color_inputs:
background_remover_color_input.change(update_background_remover_color, inputs = background_remover_color_inputs) background_remover_fill_color_input.change(update_background_remover_fill_color, inputs = background_remover_fill_color_inputs)
for background_remover_despill_color_input in background_remover_despill_color_inputs:
background_remover_despill_color_input.change(update_background_remover_despill_color, inputs = background_remover_despill_color_inputs)
processors_checkbox_group = get_ui_component('processors_checkbox_group') processors_checkbox_group = get_ui_component('processors_checkbox_group')
if processors_checkbox_group: if processors_checkbox_group:
processors_checkbox_group.change(remote_update, inputs = processors_checkbox_group, outputs = [ BACKGROUND_REMOVER_MODEL_DROPDOWN, BACKGROUND_REMOVER_COLOR_WRAPPER ]) processors_checkbox_group.change(remote_update, inputs = processors_checkbox_group, outputs = [ BACKGROUND_REMOVER_MODEL_DROPDOWN, BACKGROUND_REMOVER_FILL_COLOR_WRAPPER, BACKGROUND_REMOVER_DESPILL_COLOR_WRAPPER ])
def remote_update(processors : List[str]) -> Tuple[gradio.Dropdown, gradio.Group]: def remote_update(processors : List[str]) -> Tuple[gradio.Dropdown, gradio.Group, gradio.Group]:
has_background_remover = 'background_remover' in processors has_background_remover = 'background_remover' in processors
return gradio.Dropdown(visible = has_background_remover), gradio.Group(visible = has_background_remover) return gradio.Dropdown(visible = has_background_remover), gradio.Group(visible = has_background_remover), gradio.Group(visible = has_background_remover)
def update_background_remover_model(background_remover_model : BackgroundRemoverModel) -> gradio.Dropdown: def update_background_remover_model(background_remover_model : BackgroundRemoverModel) -> gradio.Dropdown:
@@ -99,9 +149,17 @@ def update_background_remover_model(background_remover_model : BackgroundRemover
return gradio.Dropdown() return gradio.Dropdown()
def update_background_remover_color(red : int, green : int, blue : int, alpha : int) -> None: def update_background_remover_fill_color(red : int, green : int, blue : int, alpha : int) -> None:
red = sanitize_int_range(red, background_remover_choices.background_remover_color_range) red = sanitize_int_range(red, background_remover_choices.background_remover_color_range)
green = sanitize_int_range(green, background_remover_choices.background_remover_color_range) green = sanitize_int_range(green, background_remover_choices.background_remover_color_range)
blue = sanitize_int_range(blue, background_remover_choices.background_remover_color_range) blue = sanitize_int_range(blue, background_remover_choices.background_remover_color_range)
alpha = sanitize_int_range(alpha, background_remover_choices.background_remover_color_range) alpha = sanitize_int_range(alpha, background_remover_choices.background_remover_color_range)
state_manager.set_item('background_remover_color', (red, green, blue, alpha)) state_manager.set_item('background_remover_fill_color', (red, green, blue, alpha))
def update_background_remover_despill_color(red : int, green : int, blue : int, alpha : int) -> None:
red = sanitize_int_range(red, background_remover_choices.background_remover_color_range)
green = sanitize_int_range(green, background_remover_choices.background_remover_color_range)
blue = sanitize_int_range(blue, background_remover_choices.background_remover_color_range)
alpha = sanitize_int_range(alpha, background_remover_choices.background_remover_color_range)
state_manager.set_item('background_remover_despill_color', (red, green, blue, alpha))
@@ -1,32 +0,0 @@
from typing import List, Optional
import gradio
from facefusion import state_manager, translator
from facefusion.uis import choices as uis_choices
COMMON_OPTIONS_CHECKBOX_GROUP : Optional[gradio.Checkboxgroup] = None
def render() -> None:
global COMMON_OPTIONS_CHECKBOX_GROUP
common_options = []
if state_manager.get_item('keep_temp'):
common_options.append('keep-temp')
COMMON_OPTIONS_CHECKBOX_GROUP = gradio.Checkboxgroup(
label = translator.get('uis.common_options_checkbox_group'),
choices = uis_choices.common_options,
value = common_options
)
def listen() -> None:
COMMON_OPTIONS_CHECKBOX_GROUP.change(update, inputs = COMMON_OPTIONS_CHECKBOX_GROUP)
def update(common_options : List[str]) -> None:
keep_temp = 'keep-temp' in common_options
state_manager.set_item('keep_temp', keep_temp)
+23 -18
View File
@@ -7,15 +7,15 @@ from gradio_rangeslider import RangeSlider
import facefusion.choices import facefusion.choices
from facefusion import state_manager, translator from facefusion import state_manager, translator
from facefusion.common_helper import calculate_float_step, calculate_int_step from facefusion.common_helper import calculate_float_step, calculate_int_step
from facefusion.face_analyser import get_many_faces from facefusion.face_creator import get_many_faces
from facefusion.face_selector import sort_and_filter_faces from facefusion.face_selector import sort_and_filter_faces
from facefusion.face_store import clear_static_faces from facefusion.face_store import clear_faces
from facefusion.filesystem import is_image, is_video from facefusion.filesystem import filter_image_paths, is_image, is_video
from facefusion.types import FaceSelectorMode, FaceSelectorOrder, Gender, Race, VisionFrame from facefusion.types import FaceSelectorGender, FaceSelectorMode, FaceSelectorOrder, FaceSelectorRace, VisionFrame
from facefusion.uis.core import get_ui_component, get_ui_components, register_ui_component from facefusion.uis.core import get_ui_component, get_ui_components, register_ui_component
from facefusion.uis.types import ComponentOptions from facefusion.uis.types import ComponentOptions
from facefusion.uis.ui_helper import convert_str_none from facefusion.uis.ui_helper import convert_str_none
from facefusion.vision import fit_cover_frame, read_static_image, read_video_frame from facefusion.vision import fit_cover_frame, read_static_image, read_static_images, read_video_frame
FACE_SELECTOR_MODE_DROPDOWN : Optional[gradio.Dropdown] = None FACE_SELECTOR_MODE_DROPDOWN : Optional[gradio.Dropdown] = None
FACE_SELECTOR_ORDER_DROPDOWN : Optional[gradio.Dropdown] = None FACE_SELECTOR_ORDER_DROPDOWN : Optional[gradio.Dropdown] = None
@@ -39,17 +39,18 @@ def render() -> None:
{ {
'label': translator.get('uis.reference_face_gallery'), 'label': translator.get('uis.reference_face_gallery'),
'object_fit': 'cover', 'object_fit': 'cover',
'columns': 7,
'allow_preview': False, 'allow_preview': False,
'elem_classes': 'box-face-selector', 'elem_classes': 'box-face-selector',
'visible': 'reference' in state_manager.get_item('face_selector_mode') 'visible': 'reference' in state_manager.get_item('face_selector_mode')
} }
source_vision_frames = read_static_images(filter_image_paths(state_manager.get_item('source_paths')))
if is_image(state_manager.get_item('target_path')): if is_image(state_manager.get_item('target_path')):
target_vision_frame = read_static_image(state_manager.get_item('target_path')) target_vision_frame = read_static_image(state_manager.get_item('target_path'))
reference_face_gallery_options['value'] = extract_gallery_frames(target_vision_frame) reference_face_gallery_options['value'] = extract_gallery_frames(source_vision_frames, target_vision_frame)
if is_video(state_manager.get_item('target_path')): if is_video(state_manager.get_item('target_path')):
target_vision_frame = read_video_frame(state_manager.get_item('target_path'), state_manager.get_item('reference_frame_number')) target_vision_frame = read_video_frame(state_manager.get_item('target_path'), state_manager.get_item('reference_frame_number'))
reference_face_gallery_options['value'] = extract_gallery_frames(target_vision_frame) reference_face_gallery_options['value'] = extract_gallery_frames(source_vision_frames, target_vision_frame)
FACE_SELECTOR_MODE_DROPDOWN = gradio.Dropdown( FACE_SELECTOR_MODE_DROPDOWN = gradio.Dropdown(
label = translator.get('uis.face_selector_mode_dropdown'), label = translator.get('uis.face_selector_mode_dropdown'),
choices = facefusion.choices.face_selector_modes, choices = facefusion.choices.face_selector_modes,
@@ -156,12 +157,12 @@ def update_face_selector_order(face_analyser_order : FaceSelectorOrder) -> gradi
return update_reference_position_gallery() return update_reference_position_gallery()
def update_face_selector_gender(face_selector_gender : Gender) -> gradio.Gallery: def update_face_selector_gender(face_selector_gender : FaceSelectorGender) -> gradio.Gallery:
state_manager.set_item('face_selector_gender', convert_str_none(face_selector_gender)) state_manager.set_item('face_selector_gender', convert_str_none(face_selector_gender))
return update_reference_position_gallery() return update_reference_position_gallery()
def update_face_selector_race(face_selector_race : Race) -> gradio.Gallery: def update_face_selector_race(face_selector_race : FaceSelectorRace) -> gradio.Gallery:
state_manager.set_item('face_selector_race', convert_str_none(face_selector_race)) state_manager.set_item('face_selector_race', convert_str_none(face_selector_race))
return update_reference_position_gallery() return update_reference_position_gallery()
@@ -194,30 +195,33 @@ def clear_reference_frame_number() -> None:
def clear_and_update_reference_position_gallery() -> gradio.Gallery: def clear_and_update_reference_position_gallery() -> gradio.Gallery:
clear_static_faces() clear_faces()
return update_reference_position_gallery() return update_reference_position_gallery()
def update_reference_position_gallery(frame_number : int = 0) -> gradio.Gallery: def update_reference_position_gallery(frame_number : int = 0) -> gradio.Gallery:
gallery_vision_frames = [] gallery_vision_frames = []
source_vision_frames = read_static_images(filter_image_paths(state_manager.get_item('source_paths')))
if is_image(state_manager.get_item('target_path')): if is_image(state_manager.get_item('target_path')):
target_vision_frame = read_static_image(state_manager.get_item('target_path')) target_vision_frame = read_static_image(state_manager.get_item('target_path'))
gallery_vision_frames = extract_gallery_frames(target_vision_frame) gallery_vision_frames = extract_gallery_frames(source_vision_frames, target_vision_frame)
if is_video(state_manager.get_item('target_path')): if is_video(state_manager.get_item('target_path')):
target_vision_frame = read_video_frame(state_manager.get_item('target_path'), frame_number) target_vision_frame = read_video_frame(state_manager.get_item('target_path'), frame_number)
gallery_vision_frames = extract_gallery_frames(target_vision_frame) gallery_vision_frames = extract_gallery_frames(source_vision_frames, target_vision_frame)
if gallery_vision_frames: if gallery_vision_frames:
return gradio.Gallery(value = gallery_vision_frames) return gradio.Gallery(value = gallery_vision_frames)
return gradio.Gallery(value = None) return gradio.Gallery(value = None)
def extract_gallery_frames(target_vision_frame : VisionFrame) -> List[VisionFrame]: def extract_gallery_frames(source_vision_frames : List[VisionFrame], target_vision_frame : VisionFrame) -> List[VisionFrame]:
gallery_vision_frames = [] gallery_vision_frames = []
faces = get_many_faces([ target_vision_frame ]) source_faces = get_many_faces(source_vision_frames)
faces = sort_and_filter_faces(faces) target_faces = get_many_faces([ target_vision_frame ])
target_faces = sort_and_filter_faces(source_faces, target_faces)
for face in faces: for target_face in target_faces:
start_x, start_y, end_x, end_y = map(int, face.bounding_box) start_x, start_y, end_x, end_y = map(int, target_face.bounding_box)
padding_x = int((end_x - start_x) * 0.25) padding_x = int((end_x - start_x) * 0.25)
padding_y = int((end_y - start_y) * 0.25) padding_y = int((end_y - start_y) * 0.25)
start_x = max(0, start_x - padding_x) start_x = max(0, start_x - padding_x)
@@ -228,4 +232,5 @@ def extract_gallery_frames(target_vision_frame : VisionFrame) -> List[VisionFram
crop_vision_frame = fit_cover_frame(crop_vision_frame, (128, 128)) crop_vision_frame = fit_cover_frame(crop_vision_frame, (128, 128))
crop_vision_frame = cv2.cvtColor(crop_vision_frame, cv2.COLOR_BGR2RGB) crop_vision_frame = cv2.cvtColor(crop_vision_frame, cv2.COLOR_BGR2RGB)
gallery_vision_frames.append(crop_vision_frame) gallery_vision_frames.append(crop_vision_frame)
return gallery_vision_frames return gallery_vision_frames
+32
View File
@@ -0,0 +1,32 @@
from typing import Optional
import gradio
import facefusion.choices
from facefusion import state_manager, translator
from facefusion.common_helper import calculate_float_step
from facefusion.types import Score
from facefusion.uis.core import register_ui_component
FACE_TRACKER_SCORE_SLIDER : Optional[gradio.Slider] = None
def render() -> None:
global FACE_TRACKER_SCORE_SLIDER
FACE_TRACKER_SCORE_SLIDER = gradio.Slider(
label = translator.get('uis.face_tracker_score_slider'),
value = state_manager.get_item('face_tracker_score'),
step = calculate_float_step(facefusion.choices.face_tracker_score_range),
minimum = facefusion.choices.face_tracker_score_range[0],
maximum = facefusion.choices.face_tracker_score_range[-1]
)
register_ui_component('face_tracker_score_slider', FACE_TRACKER_SCORE_SLIDER)
def listen() -> None:
FACE_TRACKER_SCORE_SLIDER.release(update_face_tracker_score, inputs = FACE_TRACKER_SCORE_SLIDER)
def update_face_tracker_score(face_tracker_score : Score) -> None:
state_manager.set_item('face_tracker_score', face_tracker_score)
-15
View File
@@ -4,39 +4,24 @@ import gradio
import facefusion.choices import facefusion.choices
from facefusion import state_manager, translator from facefusion import state_manager, translator
from facefusion.common_helper import calculate_int_step
from facefusion.types import VideoMemoryStrategy from facefusion.types import VideoMemoryStrategy
VIDEO_MEMORY_STRATEGY_DROPDOWN : Optional[gradio.Dropdown] = None VIDEO_MEMORY_STRATEGY_DROPDOWN : Optional[gradio.Dropdown] = None
SYSTEM_MEMORY_LIMIT_SLIDER : Optional[gradio.Slider] = None
def render() -> None: def render() -> None:
global VIDEO_MEMORY_STRATEGY_DROPDOWN global VIDEO_MEMORY_STRATEGY_DROPDOWN
global SYSTEM_MEMORY_LIMIT_SLIDER
VIDEO_MEMORY_STRATEGY_DROPDOWN = gradio.Dropdown( VIDEO_MEMORY_STRATEGY_DROPDOWN = gradio.Dropdown(
label = translator.get('uis.video_memory_strategy_dropdown'), label = translator.get('uis.video_memory_strategy_dropdown'),
choices = facefusion.choices.video_memory_strategies, choices = facefusion.choices.video_memory_strategies,
value = state_manager.get_item('video_memory_strategy') value = state_manager.get_item('video_memory_strategy')
) )
SYSTEM_MEMORY_LIMIT_SLIDER = gradio.Slider(
label = translator.get('uis.system_memory_limit_slider'),
step = calculate_int_step(facefusion.choices.system_memory_limit_range),
minimum = facefusion.choices.system_memory_limit_range[0],
maximum = facefusion.choices.system_memory_limit_range[-1],
value = state_manager.get_item('system_memory_limit')
)
def listen() -> None: def listen() -> None:
VIDEO_MEMORY_STRATEGY_DROPDOWN.change(update_video_memory_strategy, inputs = VIDEO_MEMORY_STRATEGY_DROPDOWN) VIDEO_MEMORY_STRATEGY_DROPDOWN.change(update_video_memory_strategy, inputs = VIDEO_MEMORY_STRATEGY_DROPDOWN)
SYSTEM_MEMORY_LIMIT_SLIDER.release(update_system_memory_limit, inputs = SYSTEM_MEMORY_LIMIT_SLIDER)
def update_video_memory_strategy(video_memory_strategy : VideoMemoryStrategy) -> None: def update_video_memory_strategy(video_memory_strategy : VideoMemoryStrategy) -> None:
state_manager.set_item('video_memory_strategy', video_memory_strategy) state_manager.set_item('video_memory_strategy', video_memory_strategy)
def update_system_memory_limit(system_memory_limit : float) -> None:
state_manager.set_item('system_memory_limit', int(system_memory_limit))
+36 -32
View File
@@ -7,18 +7,18 @@ import numpy
from facefusion import logger, process_manager, state_manager, translator from facefusion import logger, process_manager, state_manager, translator
from facefusion.audio import create_empty_audio_frame, get_voice_frame from facefusion.audio import create_empty_audio_frame, get_voice_frame
from facefusion.common_helper import get_first from facefusion.common_helper import get_first, get_middle
from facefusion.content_analyser import analyse_frame from facefusion.content_analyser import analyse_frame
from facefusion.face_analyser import get_one_face from facefusion.face_creator import get_one_face
from facefusion.face_selector import select_faces from facefusion.face_selector import select_faces
from facefusion.face_store import clear_static_faces from facefusion.face_store import clear_faces
from facefusion.filesystem import filter_audio_paths, is_image, is_video from facefusion.filesystem import filter_audio_paths, is_image, is_video
from facefusion.processors.core import get_processors_modules from facefusion.processors.core import get_processors_modules
from facefusion.types import AudioFrame, Face, Mask, VisionFrame from facefusion.types import AudioFrame, Face, Mask, VisionFrame
from facefusion.uis import choices as uis_choices from facefusion.uis import choices as uis_choices
from facefusion.uis.core import get_ui_component, get_ui_components, register_ui_component from facefusion.uis.core import get_ui_component, get_ui_components, register_ui_component
from facefusion.uis.types import ComponentOptions, PreviewMode from facefusion.uis.types import ComponentOptions, PreviewMode
from facefusion.vision import detect_frame_orientation, extract_vision_mask, fit_cover_frame, merge_vision_mask, obscure_frame, read_static_image, read_static_images, read_video_frame, restrict_frame, unpack_resolution from facefusion.vision import detect_frame_orientation, extract_vision_mask, fit_cover_frame, merge_vision_mask, obscure_frame, read_static_image, read_static_images, read_video_frame, restrict_frame, select_video_frames, unpack_resolution
PREVIEW_IMAGE : Optional[gradio.Image] = None PREVIEW_IMAGE : Optional[gradio.Image] = None
@@ -36,7 +36,7 @@ def render() -> None:
source_audio_frame = create_empty_audio_frame() source_audio_frame = create_empty_audio_frame()
source_voice_frame = create_empty_audio_frame() source_voice_frame = create_empty_audio_frame()
if source_audio_path and state_manager.get_item('output_video_fps') and state_manager.get_item('reference_frame_number'): if source_audio_path and state_manager.get_item('output_video_fps'):
temp_voice_frame = get_voice_frame(source_audio_path, state_manager.get_item('output_video_fps'), state_manager.get_item('reference_frame_number')) temp_voice_frame = get_voice_frame(source_audio_path, state_manager.get_item('output_video_fps'), state_manager.get_item('reference_frame_number'))
if numpy.any(temp_voice_frame): if numpy.any(temp_voice_frame):
source_voice_frame = temp_voice_frame source_voice_frame = temp_voice_frame
@@ -44,14 +44,14 @@ def render() -> None:
if is_image(state_manager.get_item('target_path')): if is_image(state_manager.get_item('target_path')):
target_vision_frame = read_static_image(state_manager.get_item('target_path')) target_vision_frame = read_static_image(state_manager.get_item('target_path'))
reference_vision_frame = read_static_image(state_manager.get_item('target_path')) reference_vision_frame = read_static_image(state_manager.get_item('target_path'))
preview_vision_frame = process_preview_frame(reference_vision_frame, source_vision_frames, source_audio_frame, source_voice_frame, target_vision_frame, uis_choices.preview_modes[0], uis_choices.preview_resolutions[-1]) preview_vision_frame = process_preview_frame(reference_vision_frame, source_vision_frames, source_audio_frame, source_voice_frame, [ target_vision_frame ], uis_choices.preview_modes[0], uis_choices.preview_resolutions[-1])
preview_image_options['value'] = cv2.cvtColor(preview_vision_frame, cv2.COLOR_BGR2RGB) preview_image_options['value'] = cv2.cvtColor(preview_vision_frame, cv2.COLOR_BGR2RGB)
preview_image_options['elem_classes'] = [ 'image-preview', 'is-' + detect_frame_orientation(preview_vision_frame) ] preview_image_options['elem_classes'] = [ 'image-preview', 'is-' + detect_frame_orientation(preview_vision_frame) ]
if is_video(state_manager.get_item('target_path')): if is_video(state_manager.get_item('target_path')):
temp_vision_frame = read_video_frame(state_manager.get_item('target_path'), state_manager.get_item('reference_frame_number'))
reference_vision_frame = read_video_frame(state_manager.get_item('target_path'), state_manager.get_item('reference_frame_number')) reference_vision_frame = read_video_frame(state_manager.get_item('target_path'), state_manager.get_item('reference_frame_number'))
preview_vision_frame = process_preview_frame(reference_vision_frame, source_vision_frames, source_audio_frame, source_voice_frame, temp_vision_frame, uis_choices.preview_modes[0], uis_choices.preview_resolutions[-1]) target_vision_frames = select_video_frames(state_manager.get_item('target_path'), state_manager.get_item('reference_frame_number'), state_manager.get_item('target_frame_amount'))
preview_vision_frame = process_preview_frame(reference_vision_frame, source_vision_frames, source_audio_frame, source_voice_frame, target_vision_frames, uis_choices.preview_modes[0], uis_choices.preview_resolutions[-1])
preview_image_options['value'] = cv2.cvtColor(preview_vision_frame, cv2.COLOR_BGR2RGB) preview_image_options['value'] = cv2.cvtColor(preview_vision_frame, cv2.COLOR_BGR2RGB)
preview_image_options['elem_classes'] = [ 'image-preview', 'is-' + detect_frame_orientation(preview_vision_frame) ] preview_image_options['elem_classes'] = [ 'image-preview', 'is-' + detect_frame_orientation(preview_vision_frame) ]
preview_image_options['visible'] = True preview_image_options['visible'] = True
@@ -90,10 +90,14 @@ def listen() -> None:
for ui_component in get_ui_components( for ui_component in get_ui_components(
[ [
'background_remover_color_red_number', 'background_remover_fill_color_red_number',
'background_remover_color_green_number', 'background_remover_fill_color_green_number',
'background_remover_color_blue_number', 'background_remover_fill_color_blue_number',
'background_remover_color_alpha_number', 'background_remover_fill_color_alpha_number',
'background_remover_despill_color_red_number',
'background_remover_despill_color_green_number',
'background_remover_despill_color_blue_number',
'background_remover_despill_color_alpha_number',
'face_debugger_items_checkbox_group', 'face_debugger_items_checkbox_group',
'frame_colorizer_size_dropdown', 'frame_colorizer_size_dropdown',
'face_mask_types_checkbox_group', 'face_mask_types_checkbox_group',
@@ -130,6 +134,7 @@ def listen() -> None:
'lip_syncer_weight_slider', 'lip_syncer_weight_slider',
'reference_face_distance_slider', 'reference_face_distance_slider',
'face_selector_age_range_slider', 'face_selector_age_range_slider',
'face_tracker_score_slider',
'face_mask_blur_slider', 'face_mask_blur_slider',
'face_mask_padding_top_slider', 'face_mask_padding_top_slider',
'face_mask_padding_bottom_slider', 'face_mask_padding_bottom_slider',
@@ -185,43 +190,42 @@ def update_preview_image(preview_mode : PreviewMode, preview_resolution : str, f
source_audio_frame = create_empty_audio_frame() source_audio_frame = create_empty_audio_frame()
source_voice_frame = create_empty_audio_frame() source_voice_frame = create_empty_audio_frame()
if source_audio_path and state_manager.get_item('output_video_fps') and state_manager.get_item('reference_frame_number'): if source_audio_path and state_manager.get_item('output_video_fps'):
reference_audio_frame_number = state_manager.get_item('reference_frame_number') audio_frame_number = frame_number
if state_manager.get_item('trim_frame_start'): if state_manager.get_item('trim_frame_start'):
reference_audio_frame_number -= state_manager.get_item('trim_frame_start') audio_frame_number -= state_manager.get_item('trim_frame_start')
temp_voice_frame = get_voice_frame(source_audio_path, state_manager.get_item('output_video_fps'), reference_audio_frame_number) temp_voice_frame = get_voice_frame(source_audio_path, state_manager.get_item('output_video_fps'), audio_frame_number)
if numpy.any(temp_voice_frame): if numpy.any(temp_voice_frame):
source_voice_frame = temp_voice_frame source_voice_frame = temp_voice_frame
if is_image(state_manager.get_item('target_path')): if is_image(state_manager.get_item('target_path')):
reference_vision_frame = read_static_image(state_manager.get_item('target_path')) reference_vision_frame = read_static_image(state_manager.get_item('target_path'))
target_vision_frame = read_static_image(state_manager.get_item('target_path'), 'rgba') target_vision_frame = read_static_image(state_manager.get_item('target_path'), 'rgba')
target_vision_mask = extract_vision_mask(target_vision_frame) preview_vision_frame = process_preview_frame(reference_vision_frame, source_vision_frames, source_audio_frame, source_voice_frame, [ target_vision_frame ], preview_mode, preview_resolution)
target_vision_frame = merge_vision_mask(target_vision_frame, target_vision_mask)
preview_vision_frame = process_preview_frame(reference_vision_frame, source_vision_frames, source_audio_frame, source_voice_frame, target_vision_frame, preview_mode, preview_resolution)
preview_vision_frame = cv2.cvtColor(preview_vision_frame, cv2.COLOR_BGRA2RGBA) preview_vision_frame = cv2.cvtColor(preview_vision_frame, cv2.COLOR_BGRA2RGBA)
return gradio.Image(value = preview_vision_frame, elem_classes = [ 'image-preview', 'is-' + detect_frame_orientation(preview_vision_frame) ]) return gradio.Image(value = preview_vision_frame, elem_classes = [ 'image-preview', 'is-' + detect_frame_orientation(preview_vision_frame) ])
if is_video(state_manager.get_item('target_path')): if is_video(state_manager.get_item('target_path')):
reference_vision_frame = read_video_frame(state_manager.get_item('target_path'), state_manager.get_item('reference_frame_number')) reference_vision_frame = read_video_frame(state_manager.get_item('target_path'), state_manager.get_item('reference_frame_number'))
temp_vision_frame = read_video_frame(state_manager.get_item('target_path'), frame_number) target_vision_frames = select_video_frames(state_manager.get_item('target_path'), frame_number, state_manager.get_item('target_frame_amount'))
temp_vision_mask = extract_vision_mask(temp_vision_frame) preview_vision_frame = process_preview_frame(reference_vision_frame, source_vision_frames, source_audio_frame, source_voice_frame, target_vision_frames, preview_mode, preview_resolution)
temp_vision_frame = merge_vision_mask(temp_vision_frame, temp_vision_mask)
preview_vision_frame = process_preview_frame(reference_vision_frame, source_vision_frames, source_audio_frame, source_voice_frame, temp_vision_frame, preview_mode, preview_resolution)
preview_vision_frame = cv2.cvtColor(preview_vision_frame, cv2.COLOR_BGRA2RGBA) preview_vision_frame = cv2.cvtColor(preview_vision_frame, cv2.COLOR_BGRA2RGBA)
return gradio.Image(value = preview_vision_frame, elem_classes = [ 'image-preview', 'is-' + detect_frame_orientation(preview_vision_frame) ]) return gradio.Image(value = preview_vision_frame, elem_classes = [ 'image-preview', 'is-' + detect_frame_orientation(preview_vision_frame) ])
return gradio.Image(value = None, elem_classes = None) return gradio.Image(value = None, elem_classes = None)
def clear_and_update_preview_image(preview_mode : PreviewMode, preview_resolution : str, frame_number : int = 0) -> gradio.Image: def clear_and_update_preview_image(preview_mode : PreviewMode, preview_resolution : str, frame_number : int = 0) -> gradio.Image:
clear_static_faces() clear_faces()
return update_preview_image(preview_mode, preview_resolution, frame_number) return update_preview_image(preview_mode, preview_resolution, frame_number)
def process_preview_frame(reference_vision_frame : VisionFrame, source_vision_frames : List[VisionFrame], source_audio_frame : AudioFrame, source_voice_frame : AudioFrame, target_vision_frame : VisionFrame, preview_mode : PreviewMode, preview_resolution : str) -> VisionFrame: def process_preview_frame(reference_vision_frame : VisionFrame, source_vision_frames : List[VisionFrame], source_audio_frame : AudioFrame, source_voice_frame : AudioFrame, target_vision_frames : List[VisionFrame], preview_mode : PreviewMode, preview_resolution : str) -> VisionFrame:
target_vision_frame = get_middle(target_vision_frames)
target_vision_frame = restrict_frame(target_vision_frame, unpack_resolution(preview_resolution)) target_vision_frame = restrict_frame(target_vision_frame, unpack_resolution(preview_resolution))
temp_vision_mask = extract_vision_mask(target_vision_frame)
target_vision_frame = merge_vision_mask(target_vision_frame, temp_vision_mask)
target_vision_frames = [ restrict_frame(vision_frame, unpack_resolution(preview_resolution))[:, :, :3] for vision_frame in target_vision_frames ]
temp_vision_frame = target_vision_frame.copy() temp_vision_frame = target_vision_frame.copy()
temp_vision_mask = extract_vision_mask(temp_vision_frame)
if analyse_frame(target_vision_frame[:, :, :3]): if analyse_frame(target_vision_frame[:, :, :3]):
if preview_mode == 'frame-by-frame': if preview_mode == 'frame-by-frame':
@@ -229,7 +233,7 @@ def process_preview_frame(reference_vision_frame : VisionFrame, source_vision_fr
return numpy.hstack((temp_vision_frame, temp_vision_frame)) return numpy.hstack((temp_vision_frame, temp_vision_frame))
if preview_mode == 'face-by-face': if preview_mode == 'face-by-face':
target_crop_vision_frame, output_crop_vision_frame = create_face_by_face(reference_vision_frame, target_vision_frame[:, :, :3], temp_vision_frame[:, :, :3]) target_crop_vision_frame, output_crop_vision_frame = create_face_by_face(reference_vision_frame, source_vision_frames, target_vision_frame[:, :, :3], temp_vision_frame[:, :, :3])
target_crop_vision_frame = obscure_frame(target_crop_vision_frame) target_crop_vision_frame = obscure_frame(target_crop_vision_frame)
output_crop_vision_frame = obscure_frame(output_crop_vision_frame) output_crop_vision_frame = obscure_frame(output_crop_vision_frame)
return numpy.hstack((target_crop_vision_frame, output_crop_vision_frame)) return numpy.hstack((target_crop_vision_frame, output_crop_vision_frame))
@@ -247,7 +251,7 @@ def process_preview_frame(reference_vision_frame : VisionFrame, source_vision_fr
'source_audio_frame': source_audio_frame, 'source_audio_frame': source_audio_frame,
'source_voice_frame': source_voice_frame, 'source_voice_frame': source_voice_frame,
'source_vision_frames': source_vision_frames, 'source_vision_frames': source_vision_frames,
'target_vision_frame': target_vision_frame[:, :, :3], 'target_vision_frames': target_vision_frames,
'temp_vision_frame': temp_vision_frame[:, :, :3], 'temp_vision_frame': temp_vision_frame[:, :, :3],
'temp_vision_mask': temp_vision_mask 'temp_vision_mask': temp_vision_mask
}) })
@@ -259,14 +263,14 @@ def process_preview_frame(reference_vision_frame : VisionFrame, source_vision_fr
return numpy.hstack((target_vision_frame, temp_vision_frame)) return numpy.hstack((target_vision_frame, temp_vision_frame))
if preview_mode == 'face-by-face': if preview_mode == 'face-by-face':
target_crop_vision_frame, output_crop_vision_frame = create_face_by_face(reference_vision_frame, target_vision_frame, temp_vision_frame) target_crop_vision_frame, output_crop_vision_frame = create_face_by_face(reference_vision_frame, source_vision_frames, target_vision_frame, temp_vision_frame)
return numpy.hstack((target_crop_vision_frame, output_crop_vision_frame)) return numpy.hstack((target_crop_vision_frame, output_crop_vision_frame))
return temp_vision_frame return temp_vision_frame
def create_face_by_face(reference_vision_frame : VisionFrame, target_vision_frame : VisionFrame, temp_vision_frame : VisionFrame) -> Tuple[VisionFrame, VisionFrame]: def create_face_by_face(reference_vision_frame : VisionFrame, source_vision_frames : List[VisionFrame], target_vision_frame : VisionFrame, temp_vision_frame : VisionFrame) -> Tuple[VisionFrame, VisionFrame]:
target_faces = select_faces(reference_vision_frame[:, :, :3], target_vision_frame[:, :, :3]) target_faces = select_faces(reference_vision_frame[:, :, :3], source_vision_frames, [ target_vision_frame[:, :, :3] ])
target_face = get_one_face(target_faces) target_face = get_one_face(target_faces)
if target_face: if target_face:
@@ -296,7 +300,7 @@ def extract_crop_frame(vision_frame : VisionFrame, face : Face) -> Optional[Visi
def prepare_output_frame(target_vision_frame : VisionFrame, temp_vision_frame : VisionFrame, temp_vision_mask : Mask) -> VisionFrame: def prepare_output_frame(target_vision_frame : VisionFrame, temp_vision_frame : VisionFrame, temp_vision_mask : Mask) -> VisionFrame:
temp_vision_mask = temp_vision_mask.clip(state_manager.get_item('background_remover_color')[-1], 255) temp_vision_mask = temp_vision_mask.clip(state_manager.get_item('background_remover_fill_color')[-1], 255)
temp_vision_frame = merge_vision_mask(temp_vision_frame, temp_vision_mask) temp_vision_frame = merge_vision_mask(temp_vision_frame, temp_vision_mask)
temp_vision_frame = cv2.resize(temp_vision_frame, target_vision_frame.shape[1::-1]) temp_vision_frame = cv2.resize(temp_vision_frame, target_vision_frame.shape[1::-1])
return temp_vision_frame return temp_vision_frame
+3 -2
View File
@@ -26,8 +26,9 @@ def render() -> None:
'visible': False 'visible': False
} }
if is_video(state_manager.get_item('target_path')): if is_video(state_manager.get_item('target_path')):
video_frame_total = count_video_frame_total(state_manager.get_item('target_path'))
preview_frame_slider_options['value'] = state_manager.get_item('reference_frame_number') preview_frame_slider_options['value'] = state_manager.get_item('reference_frame_number')
preview_frame_slider_options['maximum'] = count_video_frame_total(state_manager.get_item('target_path')) preview_frame_slider_options['maximum'] = video_frame_total - 1
preview_frame_slider_options['visible'] = True preview_frame_slider_options['visible'] = True
PREVIEW_FRAME_SLIDER = gradio.Slider(**preview_frame_slider_options) PREVIEW_FRAME_SLIDER = gradio.Slider(**preview_frame_slider_options)
with gradio.Row(): with gradio.Row():
@@ -57,5 +58,5 @@ def listen() -> None:
def update_preview_frame_slider() -> gradio.Slider: def update_preview_frame_slider() -> gradio.Slider:
if is_video(state_manager.get_item('target_path')): if is_video(state_manager.get_item('target_path')):
video_frame_total = count_video_frame_total(state_manager.get_item('target_path')) video_frame_total = count_video_frame_total(state_manager.get_item('target_path'))
return gradio.Slider(maximum = video_frame_total, visible = True) return gradio.Slider(maximum = video_frame_total - 1, visible = True)
return gradio.Slider(value = 0, visible = False) return gradio.Slider(value = 0, visible = False)
+2 -2
View File
@@ -3,7 +3,7 @@ from typing import Optional, Tuple
import gradio import gradio
from facefusion import state_manager, translator from facefusion import state_manager, translator
from facefusion.face_store import clear_static_faces from facefusion.face_store import clear_faces
from facefusion.filesystem import is_image, is_video from facefusion.filesystem import is_image, is_video
from facefusion.uis.core import register_ui_component from facefusion.uis.core import register_ui_component
from facefusion.uis.types import ComponentOptions, File from facefusion.uis.types import ComponentOptions, File
@@ -51,7 +51,7 @@ def listen() -> None:
def update(file : File) -> Tuple[gradio.Image, gradio.Video]: def update(file : File) -> Tuple[gradio.Image, gradio.Video]:
clear_static_faces() clear_faces()
if file and is_image(file.name): if file and is_image(file.name):
state_manager.set_item('target_path', file.name) state_manager.set_item('target_path', file.name)
+2 -2
View File
@@ -3,7 +3,7 @@ from typing import Optional, Tuple
from gradio_rangeslider import RangeSlider from gradio_rangeslider import RangeSlider
from facefusion import state_manager, translator from facefusion import state_manager, translator
from facefusion.face_store import clear_static_faces from facefusion.face_store import clear_faces
from facefusion.filesystem import is_video from facefusion.filesystem import is_video
from facefusion.uis.core import get_ui_components from facefusion.uis.core import get_ui_components
from facefusion.uis.types import ComponentOptions from facefusion.uis.types import ComponentOptions
@@ -53,7 +53,7 @@ def remote_update() -> RangeSlider:
def update_trim_frame(trim_frame : Tuple[float, float]) -> None: def update_trim_frame(trim_frame : Tuple[float, float]) -> None:
clear_static_faces() clear_faces()
trim_frame_start, trim_frame_end = trim_frame trim_frame_start, trim_frame_end = trim_frame
video_frame_total = count_video_frame_total(state_manager.get_item('target_path')) video_frame_total = count_video_frame_total(state_manager.get_item('target_path'))
trim_frame_start = int(trim_frame_start) if trim_frame_start > 0 else None trim_frame_start = int(trim_frame_start) if trim_frame_start > 0 else None
+18 -12
View File
@@ -1,4 +1,4 @@
from typing import Optional from typing import List, Optional
import gradio import gradio
@@ -6,7 +6,7 @@ import facefusion.choices
from facefusion import state_manager, translator, voice_extractor from facefusion import state_manager, translator, voice_extractor
from facefusion.filesystem import is_video from facefusion.filesystem import is_video
from facefusion.types import VoiceExtractorModel from facefusion.types import VoiceExtractorModel
from facefusion.uis.core import get_ui_components, register_ui_component from facefusion.uis.core import get_ui_component, get_ui_components, register_ui_component
VOICE_EXTRACTOR_MODEL_DROPDOWN : Optional[gradio.Dropdown] = None VOICE_EXTRACTOR_MODEL_DROPDOWN : Optional[gradio.Dropdown] = None
@@ -14,11 +14,12 @@ VOICE_EXTRACTOR_MODEL_DROPDOWN : Optional[gradio.Dropdown] = None
def render() -> None: def render() -> None:
global VOICE_EXTRACTOR_MODEL_DROPDOWN global VOICE_EXTRACTOR_MODEL_DROPDOWN
has_lip_syncer = 'lip_syncer' in state_manager.get_item('processors')
VOICE_EXTRACTOR_MODEL_DROPDOWN = gradio.Dropdown( VOICE_EXTRACTOR_MODEL_DROPDOWN = gradio.Dropdown(
label = translator.get('uis.voice_extractor_model_dropdown'), label = translator.get('uis.voice_extractor_model_dropdown'),
choices = facefusion.choices.voice_extractor_models, choices = facefusion.choices.voice_extractor_models,
value = state_manager.get_item('voice_extractor_model'), value = state_manager.get_item('voice_extractor_model'),
visible = is_video(state_manager.get_item('target_path')) visible = is_video(state_manager.get_item('target_path')) and has_lip_syncer
) )
register_ui_component('voice_extractor_model_dropdown', VOICE_EXTRACTOR_MODEL_DROPDOWN) register_ui_component('voice_extractor_model_dropdown', VOICE_EXTRACTOR_MODEL_DROPDOWN)
@@ -26,17 +27,22 @@ def render() -> None:
def listen() -> None: def listen() -> None:
VOICE_EXTRACTOR_MODEL_DROPDOWN.change(update_voice_extractor_model, inputs = VOICE_EXTRACTOR_MODEL_DROPDOWN, outputs = VOICE_EXTRACTOR_MODEL_DROPDOWN) VOICE_EXTRACTOR_MODEL_DROPDOWN.change(update_voice_extractor_model, inputs = VOICE_EXTRACTOR_MODEL_DROPDOWN, outputs = VOICE_EXTRACTOR_MODEL_DROPDOWN)
for ui_component in get_ui_components( processors_checkbox_group = get_ui_component('processors_checkbox_group')
[ if processors_checkbox_group:
'target_image', processors_checkbox_group.change(remote_update, inputs = processors_checkbox_group, outputs = VOICE_EXTRACTOR_MODEL_DROPDOWN)
'target_video'
]): for ui_component in get_ui_components(
for method in [ 'change', 'clear' ]: [
getattr(ui_component, method)(remote_update, outputs = VOICE_EXTRACTOR_MODEL_DROPDOWN) 'target_image',
'target_video'
]):
for method in [ 'change', 'clear' ]:
getattr(ui_component, method)(remote_update, inputs = processors_checkbox_group, outputs = VOICE_EXTRACTOR_MODEL_DROPDOWN)
def remote_update() -> gradio.Dropdown: def remote_update(processors : List[str]) -> gradio.Dropdown:
if is_video(state_manager.get_item('target_path')): has_lip_syncer = 'lip_syncer' in processors
if is_video(state_manager.get_item('target_path')) and has_lip_syncer:
return gradio.Dropdown(visible = True) return gradio.Dropdown(visible = True)
return gradio.Dropdown(visible = False) return gradio.Dropdown(visible = False)
+5 -5
View File
@@ -98,15 +98,15 @@ def start(webcam_device_id : int, webcam_mode : WebcamMode, webcam_resolution :
camera_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, webcam_height) camera_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, webcam_height)
camera_capture.set(cv2.CAP_PROP_FPS, webcam_fps) camera_capture.set(cv2.CAP_PROP_FPS, webcam_fps)
for capture_frame in multi_process_capture(camera_capture, webcam_fps): for capture_vision_frame in multi_process_capture(camera_capture, webcam_fps):
capture_frame = cv2.cvtColor(capture_frame, cv2.COLOR_BGR2RGB) capture_vision_frame = cv2.cvtColor(capture_vision_frame, cv2.COLOR_BGR2RGB)
capture_frame = fit_cover_frame(capture_frame, (webcam_width, webcam_height)) capture_vision_frame = fit_cover_frame(capture_vision_frame, (webcam_width, webcam_height))
if webcam_mode == 'inline': if webcam_mode == 'inline':
yield capture_frame yield capture_vision_frame
if webcam_mode in [ 'udp', 'v4l2' ]: if webcam_mode in [ 'udp', 'v4l2' ]:
try: try:
stream.stdin.write(capture_frame.tobytes()) stream.stdin.write(capture_vision_frame.tobytes())
except Exception: except Exception:
pass pass
+39
View File
@@ -0,0 +1,39 @@
from typing import Optional
import gradio
import facefusion.choices
from facefusion import state_manager, translator
from facefusion.types import WorkflowMode, WorkflowStrategy
WORKFLOW_MODE_DROPDOWN : Optional[gradio.Dropdown] = None
WORKFLOW_STRATEGY_DROPDOWN : Optional[gradio.Dropdown] = None
def render() -> None:
global WORKFLOW_MODE_DROPDOWN
global WORKFLOW_STRATEGY_DROPDOWN
WORKFLOW_MODE_DROPDOWN = gradio.Dropdown(
label = translator.get('uis.workflow_mode_dropdown'),
choices = facefusion.choices.workflow_modes,
value = state_manager.get_item('workflow_mode')
)
WORKFLOW_STRATEGY_DROPDOWN = gradio.Dropdown(
label = translator.get('uis.workflow_strategy_dropdown'),
choices = facefusion.choices.workflow_strategies,
value = state_manager.get_item('workflow_strategy')
)
def listen() -> None:
WORKFLOW_MODE_DROPDOWN.change(update_workflow_mode, inputs = WORKFLOW_MODE_DROPDOWN)
WORKFLOW_STRATEGY_DROPDOWN.change(update_workflow_strategy, inputs = WORKFLOW_STRATEGY_DROPDOWN)
def update_workflow_mode(workflow_mode : WorkflowMode) -> None:
state_manager.set_item('workflow_mode', workflow_mode)
def update_workflow_strategy(workflow_strategy : WorkflowStrategy) -> None:
state_manager.set_item('workflow_strategy', workflow_strategy)
+7 -4
View File
@@ -1,7 +1,7 @@
import gradio import gradio
from facefusion import state_manager from facefusion import state_manager
from facefusion.uis.components import about, age_modifier_options, background_remover_options, common_options, deep_swapper_options, download, execution, execution_thread_count, expression_restorer_options, face_debugger_options, face_detector, face_editor_options, face_enhancer_options, face_landmarker, face_masker, face_selector, face_swapper_options, frame_colorizer_options, frame_enhancer_options, instant_runner, job_manager, job_runner, lip_syncer_options, memory, output, output_options, preview, preview_options, processors, source, target, temp_frame, terminal, trim_frame, ui_workflow, voice_extractor from facefusion.uis.components import about, age_modifier_options, background_remover_options, deep_swapper_options, download, execution, execution_thread_count, expression_restorer_options, face_debugger_options, face_detector, face_editor_options, face_enhancer_options, face_landmarker, face_masker, face_selector, face_swapper_options, face_tracker, frame_colorizer_options, frame_enhancer_options, instant_runner, job_manager, job_runner, lip_syncer_options, memory, output, output_options, preview, preview_options, processors, source, target, temp_frame, terminal, trim_frame, ui_workflow, voice_extractor, workflow
def pre_check() -> bool: def pre_check() -> bool:
@@ -40,6 +40,8 @@ def render() -> gradio.Blocks:
lip_syncer_options.render() lip_syncer_options.render()
with gradio.Blocks(): with gradio.Blocks():
voice_extractor.render() voice_extractor.render()
with gradio.Blocks():
workflow.render()
with gradio.Blocks(): with gradio.Blocks():
execution.render() execution.render()
execution_thread_count.render() execution_thread_count.render()
@@ -73,14 +75,14 @@ def render() -> gradio.Blocks:
trim_frame.render() trim_frame.render()
with gradio.Blocks(): with gradio.Blocks():
face_selector.render() face_selector.render()
with gradio.Blocks():
face_tracker.render()
with gradio.Blocks(): with gradio.Blocks():
face_masker.render() face_masker.render()
with gradio.Blocks(): with gradio.Blocks():
face_detector.render() face_detector.render()
with gradio.Blocks(): with gradio.Blocks():
face_landmarker.render() face_landmarker.render()
with gradio.Blocks():
common_options.render()
return layout return layout
@@ -114,11 +116,12 @@ def listen() -> None:
preview_options.listen() preview_options.listen()
trim_frame.listen() trim_frame.listen()
face_selector.listen() face_selector.listen()
face_tracker.listen()
face_masker.listen() face_masker.listen()
face_detector.listen() face_detector.listen()
face_landmarker.listen() face_landmarker.listen()
voice_extractor.listen() voice_extractor.listen()
common_options.listen() workflow.listen()
def run(ui : gradio.Blocks) -> None: def run(ui : gradio.Blocks) -> None:
+9 -4
View File
@@ -6,10 +6,14 @@ ComponentName = Literal\
'age_modifier_direction_slider', 'age_modifier_direction_slider',
'age_modifier_model_dropdown', 'age_modifier_model_dropdown',
'background_remover_model_dropdown', 'background_remover_model_dropdown',
'background_remover_color_red_number', 'background_remover_fill_color_red_number',
'background_remover_color_green_number', 'background_remover_fill_color_green_number',
'background_remover_color_blue_number', 'background_remover_fill_color_blue_number',
'background_remover_color_alpha_number', 'background_remover_fill_color_alpha_number',
'background_remover_despill_color_red_number',
'background_remover_despill_color_green_number',
'background_remover_despill_color_blue_number',
'background_remover_despill_color_alpha_number',
'deep_swapper_model_dropdown', 'deep_swapper_model_dropdown',
'deep_swapper_morph_slider', 'deep_swapper_morph_slider',
'expression_restorer_factor_slider', 'expression_restorer_factor_slider',
@@ -54,6 +58,7 @@ ComponentName = Literal\
'face_selector_mode_dropdown', 'face_selector_mode_dropdown',
'face_selector_order_dropdown', 'face_selector_order_dropdown',
'face_selector_race_dropdown', 'face_selector_race_dropdown',
'face_tracker_score_slider',
'face_swapper_model_dropdown', 'face_swapper_model_dropdown',
'face_swapper_pixel_boost_dropdown', 'face_swapper_pixel_boost_dropdown',
'face_swapper_weight_slider', 'face_swapper_weight_slider',
+133 -18
View File
@@ -1,40 +1,155 @@
import cv2 import hashlib
import uuid
from io import BufferedReader
from typing import Optional, cast
from facefusion.types import VideoPoolSet import numpy
from facefusion import ffmpeg, ffprobe, frame_store, vision
from facefusion.common_helper import get_first, get_last
from facefusion.types import Fps, Resolution, VideoPoolSet, VideoReader, VideoWriter, VisionFrame, VisionFrameSet
VIDEO_POOL_SET : VideoPoolSet =\ VIDEO_POOL_SET : VideoPoolSet =\
{ {
'capture': {}, 'reader': {},
'writer': {} 'writer': {}
} }
def get_video_capture(video_path : str) -> cv2.VideoCapture: def get_reader(video_path : str, context : str) -> VideoReader:
if video_path not in VIDEO_POOL_SET.get('capture'): reader_id = hashlib.sha1((video_path + '_' + context).encode()).hexdigest()
video_capture = cv2.VideoCapture(video_path)
if video_capture.isOpened(): if reader_id not in VIDEO_POOL_SET.get('reader'):
VIDEO_POOL_SET['capture'][video_path] = video_capture video_metadata = ffprobe.extract_static_video_metadata(video_path)
return VIDEO_POOL_SET.get('capture').get(video_path) VIDEO_POOL_SET['reader'][reader_id] =\
{
'id': reader_id,
'file_path': video_path,
'process': ffmpeg.create_video_reader(video_path, 0, video_metadata),
'metadata': video_metadata,
'frame_number': 0
}
return VIDEO_POOL_SET.get('reader').get(reader_id)
def get_video_writer(video_path : str) -> cv2.VideoWriter: def conditional_seek_video_reader(video_reader : VideoReader, frame_number : int = 0) -> None:
frame_total = video_reader.get('metadata').get('frame_total')
frame_number = min(frame_total - 1, frame_number)
skip_total = frame_number - video_reader.get('frame_number')
skip_margin = 128
if 0 < skip_total <= skip_margin:
drain_video_reader(video_reader, skip_total)
if not video_reader.get('frame_number') == frame_number:
seek_video_reader(video_reader, frame_number)
def seek_video_reader(video_reader : VideoReader, frame_number : int = 0) -> None:
close_video_reader(video_reader)
video_reader['process'] = ffmpeg.create_video_reader(video_reader.get('file_path'), frame_number, video_reader.get('metadata'))
video_reader['frame_number'] = frame_number
def drain_video_reader(video_reader : VideoReader, skip_total : int) -> None:
width, height = video_reader.get('metadata').get('resolution')
channel_total = 3
frame_size = width * height * channel_total
for _ in range(skip_total):
video_reader.get('process').stdout.read(frame_size)
video_reader['frame_number'] = video_reader.get('frame_number') + skip_total
def read_video_frame(video_reader : VideoReader) -> Optional[VisionFrame]:
width, height = video_reader.get('metadata').get('resolution')
channel_total = 3
video_stream = cast(BufferedReader, video_reader.get('process').stdout)
vision_frame = numpy.empty(width * height * channel_total, numpy.uint8)
if video_stream.readinto(vision_frame) == vision_frame.size:
video_reader['frame_number'] = video_reader.get('frame_number') + 1
return vision_frame.reshape(height, width, channel_total)
return None
def read_video_frames(video_reader : VideoReader, frame_start : int, frame_end : int) -> VisionFrameSet:
reader_id = video_reader.get('id')
frame_set = frame_store.get_frame_store(reader_id)
keep_margin = 4
frame_gaps = []
for frame_number in range(frame_start, frame_end + 1):
if frame_number not in frame_set:
frame_gaps.append(frame_number)
if frame_gaps:
collect_video_frames(video_reader, get_first(frame_gaps), get_last(frame_gaps))
frame_store.reduce_frames(reader_id, frame_start - keep_margin, frame_end + keep_margin)
return frame_store.select_frame_set(reader_id, frame_start, frame_end)
def collect_video_frames(video_reader : VideoReader, frame_start : int, frame_end : int) -> None:
reader_id = video_reader.get('id')
skip_total = frame_start - video_reader.get('frame_number')
skip_margin = 16
if skip_total < 0 or skip_total > skip_margin:
seek_video_reader(video_reader, frame_start)
for frame_number in range(video_reader.get('frame_number'), frame_end + 1):
vision_frame = read_video_frame(video_reader)
if vision.is_vision_frame(vision_frame):
frame_store.set_frame(reader_id, frame_number, vision_frame)
def close_video_reader(video_reader : VideoReader) -> None:
video_reader.get('process').kill()
video_reader.get('process').wait()
def get_writer(video_path : str, temp_video_fps : Fps, temp_video_resolution : Resolution, output_video_resolution : Resolution, output_video_fps : Fps) -> VideoWriter:
if video_path not in VIDEO_POOL_SET.get('writer'): if video_path not in VIDEO_POOL_SET.get('writer'):
video_writer = cv2.VideoWriter() VIDEO_POOL_SET['writer'][video_path] =\
{
if video_writer.isOpened(): 'id': uuid.uuid4().hex,
VIDEO_POOL_SET['writer'][video_path] = video_writer 'file_path': video_path,
'process': ffmpeg.create_video_writer(video_path, temp_video_fps, temp_video_resolution, output_video_resolution, output_video_fps),
'metadata':
{
'fps': output_video_fps,
'resolution': output_video_resolution
}
}
return VIDEO_POOL_SET.get('writer').get(video_path) return VIDEO_POOL_SET.get('writer').get(video_path)
def write_video_frame(video_writer : VideoWriter, vision_frame : VisionFrame) -> None:
video_writer.get('process').stdin.write(vision_frame.tobytes())
def close_video_writer(video_writer : VideoWriter) -> bool:
video_writer.get('process').stdin.close()
video_writer.get('process').wait()
return video_writer.get('process').returncode == 0
def clear_video_pool() -> None: def clear_video_pool() -> None:
for video_capture in VIDEO_POOL_SET.get('capture').values(): for video_reader in VIDEO_POOL_SET.get('reader').values():
video_capture.release() close_video_reader(video_reader)
frame_store.clear_frames(video_reader.get('id'))
for video_writer in VIDEO_POOL_SET.get('writer').values(): for video_writer in VIDEO_POOL_SET.get('writer').values():
video_writer.release() close_video_writer(video_writer)
VIDEO_POOL_SET['capture'].clear() VIDEO_POOL_SET['reader'].clear()
VIDEO_POOL_SET['writer'].clear() VIDEO_POOL_SET['writer'].clear()
+38 -31
View File
@@ -6,11 +6,11 @@ import cv2
import numpy import numpy
from cv2.typing import Size from cv2.typing import Size
from facefusion import ffprobe, video_manager
from facefusion.common_helper import is_windows from facefusion.common_helper import is_windows
from facefusion.filesystem import get_file_extension, is_image, is_video from facefusion.filesystem import get_file_extension, is_image, is_video
from facefusion.thread_helper import thread_semaphore from facefusion.thread_helper import thread_lock, thread_semaphore
from facefusion.types import ColorMode, Duration, Fps, Mask, Orientation, Resolution, Scale, VisionFrame from facefusion.types import ColorMode, Duration, Fps, Mask, Orientation, Resolution, Scale, VisionFrame
from facefusion.video_manager import get_video_capture
def read_static_images(image_paths : List[str], color_mode : ColorMode = 'rgb') -> List[VisionFrame]: def read_static_images(image_paths : List[str], color_mode : ColorMode = 'rgb') -> List[VisionFrame]:
@@ -77,29 +77,39 @@ def read_static_video_frame(video_path : str, frame_number : int = 0) -> Optiona
def read_video_frame(video_path : str, frame_number : int = 0) -> Optional[VisionFrame]: def read_video_frame(video_path : str, frame_number : int = 0) -> Optional[VisionFrame]:
if is_video(video_path): if is_video(video_path):
video_capture = get_video_capture(video_path) video_reader = video_manager.get_reader(video_path, 'read_video_frame')
if video_capture and video_capture.isOpened(): with thread_semaphore():
frame_total = video_capture.get(cv2.CAP_PROP_FRAME_COUNT) video_manager.conditional_seek_video_reader(video_reader, frame_number)
return video_manager.read_video_frame(video_reader)
with thread_semaphore():
video_capture.set(cv2.CAP_PROP_POS_FRAMES, min(frame_total, frame_number - 1))
has_vision_frame, vision_frame = video_capture.read()
if has_vision_frame:
return vision_frame
return None return None
def select_video_frames(video_path : str, frame_number : int = 0, frame_offset : int = 2) -> List[VisionFrame]:
vision_frames = []
frame_start = frame_number - frame_offset
frame_end = frame_number + frame_offset
if is_video(video_path):
with thread_lock():
video_reader = video_manager.get_reader(video_path, 'select_video_frames')
frame_set = video_manager.read_video_frames(video_reader, max(frame_start, 0), frame_end)
for frame_number in range(frame_start, frame_end + 1):
vision_frame = create_empty_vision_frame()
if frame_number in frame_set:
vision_frame = frame_set.get(frame_number)
vision_frames.append(vision_frame)
return vision_frames
def count_video_frame_total(video_path : str) -> int: def count_video_frame_total(video_path : str) -> int:
if is_video(video_path): if is_video(video_path):
video_capture = get_video_capture(video_path) return ffprobe.extract_static_video_metadata(video_path).get('frame_total')
if video_capture and video_capture.isOpened():
with thread_semaphore():
video_frame_total = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT))
return video_frame_total
return 0 return 0
@@ -114,12 +124,7 @@ def predict_video_frame_total(video_path : str, fps : Fps, trim_frame_start : in
def detect_video_fps(video_path : str) -> Optional[float]: def detect_video_fps(video_path : str) -> Optional[float]:
if is_video(video_path): if is_video(video_path):
video_capture = get_video_capture(video_path) return ffprobe.extract_static_video_metadata(video_path).get('fps')
if video_capture and video_capture.isOpened():
with thread_semaphore():
video_fps = video_capture.get(cv2.CAP_PROP_FPS)
return video_fps
return None return None
@@ -167,13 +172,7 @@ def restrict_trim_frame(video_path : str, trim_frame_start : Optional[int], trim
def detect_video_resolution(video_path : str) -> Optional[Resolution]: def detect_video_resolution(video_path : str) -> Optional[Resolution]:
if is_video(video_path): if is_video(video_path):
video_capture = get_video_capture(video_path) return ffprobe.extract_static_video_metadata(video_path).get('resolution')
if video_capture and video_capture.isOpened():
with thread_semaphore():
width = video_capture.get(cv2.CAP_PROP_FRAME_WIDTH)
height = video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
return int(width), int(height)
return None return None
@@ -307,6 +306,14 @@ def blend_vision_frames(source_vision_frame : VisionFrame, target_vision_frame :
return blend_vision_frame return blend_vision_frame
def create_empty_vision_frame() -> VisionFrame:
return numpy.zeros((1, 1, 3)).astype(numpy.uint8)
def is_vision_frame(vision_frame : VisionFrame) -> bool:
return numpy.ndim(vision_frame) == 3
def create_tile_frames(vision_frame : VisionFrame, size : Size) -> Tuple[List[VisionFrame], int, int]: def create_tile_frames(vision_frame : VisionFrame, size : Size) -> Tuple[List[VisionFrame], int, int]:
tile_width = size[0] - 2 * size[2] tile_width = size[0] - 2 * size[2]
pad_size_top = size[1] + size[2] pad_size_top = size[1] + size[2]
+2 -2
View File
@@ -69,8 +69,8 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
{ {
'__metadata__': '__metadata__':
{ {
'vendor': 'Unknown', 'vendor': 'Anjok07',
'license': 'Non-Commercial', 'license': 'MIT',
'year': 2023 'year': 2023
}, },
'hashes': 'hashes':
+84 -1
View File
@@ -1,4 +1,15 @@
from facefusion import logger, process_manager, translator from typing import List
import numpy
from facefusion import logger, process_manager, state_manager, translator
from facefusion.audio import create_empty_audio_frame, get_audio_frame, get_voice_frame
from facefusion.common_helper import get_first
from facefusion.filesystem import filter_audio_paths
from facefusion.processors.core import get_processors_modules
from facefusion.temp_helper import clear_temp_directory, create_temp_directory
from facefusion.types import AudioFrame, ErrorCode, VisionFrame
from facefusion.vision import conditional_merge_vision_mask, extract_vision_mask, read_static_image, read_static_images, read_static_video_frame, restrict_trim_frame, restrict_video_fps, select_video_frames
def is_process_stopping() -> bool: def is_process_stopping() -> bool:
@@ -6,3 +17,75 @@ def is_process_stopping() -> bool:
process_manager.end() process_manager.end()
logger.info(translator.get('processing_stopped'), __name__) logger.info(translator.get('processing_stopped'), __name__)
return process_manager.is_pending() return process_manager.is_pending()
def setup() -> ErrorCode:
if create_temp_directory(state_manager.get_item('target_path')):
logger.debug(translator.get('creating_temp'), __name__)
return 0
def clear() -> ErrorCode:
if clear_temp_directory(state_manager.get_item('target_path')):
logger.debug(translator.get('clearing_temp'), __name__)
return 0
def conditional_get_reference_vision_frame() -> VisionFrame:
if state_manager.get_item('workflow_mode') == 'image-to-video':
return read_static_video_frame(state_manager.get_item('target_path'), state_manager.get_item('reference_frame_number'))
return read_static_image(state_manager.get_item('target_path'))
def conditional_get_source_audio_frame(frame_number : int) -> AudioFrame:
if state_manager.get_item('workflow_mode') == 'image-to-video':
trim_frame_start, _ = restrict_trim_frame(state_manager.get_item('target_path'), state_manager.get_item('trim_frame_start'), state_manager.get_item('trim_frame_end'))
temp_video_fps = restrict_video_fps(state_manager.get_item('target_path'), state_manager.get_item('output_video_fps'))
source_audio_path = get_first(filter_audio_paths(state_manager.get_item('source_paths')))
source_audio_frame = get_audio_frame(source_audio_path, temp_video_fps, frame_number - trim_frame_start)
if numpy.any(source_audio_frame):
return source_audio_frame
return create_empty_audio_frame()
def conditional_get_source_voice_frame(frame_number : int) -> AudioFrame:
if state_manager.get_item('workflow_mode') == 'image-to-video':
trim_frame_start, _ = restrict_trim_frame(state_manager.get_item('target_path'), state_manager.get_item('trim_frame_start'), state_manager.get_item('trim_frame_end'))
temp_video_fps = restrict_video_fps(state_manager.get_item('target_path'), state_manager.get_item('output_video_fps'))
source_audio_path = get_first(filter_audio_paths(state_manager.get_item('source_paths')))
source_voice_frame = get_voice_frame(source_audio_path, temp_video_fps, frame_number - trim_frame_start)
if numpy.any(source_voice_frame):
return source_voice_frame
return create_empty_audio_frame()
def conditional_get_target_vision_frames(frame_number : int) -> List[VisionFrame]:
if state_manager.get_item('workflow_mode') == 'image-to-video':
return select_video_frames(state_manager.get_item('target_path'), frame_number, state_manager.get_item('target_frame_amount'))
return [ read_static_image(state_manager.get_item('target_path')) ]
def process_temp_frame(target_vision_frames : List[VisionFrame], temp_vision_frame : VisionFrame, frame_number : int) -> VisionFrame:
reference_vision_frame = conditional_get_reference_vision_frame()
source_vision_frames = read_static_images(state_manager.get_item('source_paths'))
source_audio_frame = conditional_get_source_audio_frame(frame_number)
source_voice_frame = conditional_get_source_voice_frame(frame_number)
temp_vision_mask = extract_vision_mask(temp_vision_frame)
for processor_module in get_processors_modules(state_manager.get_item('processors')):
temp_vision_frame, temp_vision_mask = processor_module.process_frame(
{
'reference_vision_frame': reference_vision_frame,
'source_vision_frames': source_vision_frames,
'source_audio_frame': source_audio_frame,
'source_voice_frame': source_voice_frame,
'target_vision_frames': target_vision_frames,
'temp_vision_frame': temp_vision_frame[:, :, :3],
'temp_vision_mask': temp_vision_mask
})
return conditional_merge_vision_mask(temp_vision_frame, temp_vision_mask)
+7 -90
View File
@@ -1,25 +1,21 @@
from functools import partial from functools import partial
from facefusion import ffmpeg from facefusion import process_manager
from facefusion import logger, process_manager, state_manager, translator
from facefusion.audio import create_empty_audio_frame
from facefusion.content_analyser import analyse_image
from facefusion.filesystem import is_image
from facefusion.processors.core import get_processors_modules
from facefusion.temp_helper import clear_temp_directory, create_temp_directory, get_temp_file_path
from facefusion.time_helper import calculate_end_time
from facefusion.types import ErrorCode from facefusion.types import ErrorCode
from facefusion.vision import conditional_merge_vision_mask, detect_image_resolution, extract_vision_mask, pack_resolution, read_static_image, read_static_images, restrict_image_resolution, scale_resolution, write_image from facefusion.workflows.core import clear, setup
from facefusion.workflows.core import is_process_stopping from facefusion.workflows.to_image import analyse_image, finalize_image, prepare_image, process_image
def process(start_time : float) -> ErrorCode: def process(start_time : float) -> ErrorCode:
tasks =\ tasks =\
[ [
analyse_image,
clear,
setup, setup,
prepare_image, prepare_image,
process_image, process_image,
partial(finalize_image, start_time) partial(finalize_image, start_time),
clear
] ]
process_manager.start() process_manager.start()
@@ -32,82 +28,3 @@ def process(start_time : float) -> ErrorCode:
process_manager.end() process_manager.end()
return 0 return 0
def setup() -> ErrorCode:
if analyse_image(state_manager.get_item('target_path')):
return 3
logger.debug(translator.get('clearing_temp'), __name__)
clear_temp_directory(state_manager.get_item('target_path'))
logger.debug(translator.get('creating_temp'), __name__)
create_temp_directory(state_manager.get_item('target_path'))
return 0
def prepare_image() -> ErrorCode:
output_image_resolution = scale_resolution(detect_image_resolution(state_manager.get_item('target_path')), state_manager.get_item('output_image_scale'))
temp_image_resolution = restrict_image_resolution(state_manager.get_item('target_path'), output_image_resolution)
logger.info(translator.get('copying_image').format(resolution = pack_resolution(temp_image_resolution)), __name__)
if ffmpeg.copy_image(state_manager.get_item('target_path'), temp_image_resolution):
logger.debug(translator.get('copying_image_succeeded'), __name__)
else:
logger.error(translator.get('copying_image_failed'), __name__)
process_manager.end()
return 1
return 0
def process_image() -> ErrorCode:
temp_image_path = get_temp_file_path(state_manager.get_item('target_path'))
reference_vision_frame = read_static_image(temp_image_path)
source_vision_frames = read_static_images(state_manager.get_item('source_paths'))
source_audio_frame = create_empty_audio_frame()
source_voice_frame = create_empty_audio_frame()
target_vision_frame = read_static_image(temp_image_path, 'rgba')
temp_vision_frame = target_vision_frame.copy()
temp_vision_mask = extract_vision_mask(temp_vision_frame)
for processor_module in get_processors_modules(state_manager.get_item('processors')):
logger.info(translator.get('processing'), processor_module.__name__)
temp_vision_frame, temp_vision_mask = processor_module.process_frame(
{
'reference_vision_frame': reference_vision_frame,
'source_vision_frames': source_vision_frames,
'source_audio_frame': source_audio_frame,
'source_voice_frame': source_voice_frame,
'target_vision_frame': target_vision_frame[:, :, :3],
'temp_vision_frame': temp_vision_frame[:, :, :3],
'temp_vision_mask': temp_vision_mask
})
processor_module.post_process()
temp_vision_frame = conditional_merge_vision_mask(temp_vision_frame, temp_vision_mask)
write_image(temp_image_path, temp_vision_frame)
if is_process_stopping():
return 4
return 0
def finalize_image(start_time : float) -> ErrorCode:
output_image_resolution = scale_resolution(detect_image_resolution(state_manager.get_item('target_path')), state_manager.get_item('output_image_scale'))
logger.info(translator.get('finalizing_image').format(resolution = pack_resolution(output_image_resolution)), __name__)
if ffmpeg.finalize_image(state_manager.get_item('target_path'), state_manager.get_item('output_path'), output_image_resolution):
logger.debug(translator.get('finalizing_image_succeeded'), __name__)
else:
logger.warn(translator.get('finalizing_image_skipped'), __name__)
logger.debug(translator.get('clearing_temp'), __name__)
clear_temp_directory(state_manager.get_item('target_path'))
if is_image(state_manager.get_item('output_path')):
logger.info(translator.get('processing_image_succeeded').format(seconds = calculate_end_time(start_time)), __name__)
else:
logger.error(translator.get('processing_image_failed'), __name__)
return 1
return 0
+24 -177
View File
@@ -1,33 +1,36 @@
from concurrent.futures import ThreadPoolExecutor, as_completed
from functools import partial from functools import partial
import numpy from facefusion import process_manager, state_manager
from tqdm import tqdm
from facefusion import ffmpeg
from facefusion import logger, process_manager, state_manager, translator, video_manager
from facefusion.audio import create_empty_audio_frame, get_audio_frame, get_voice_frame
from facefusion.common_helper import get_first
from facefusion.content_analyser import analyse_video
from facefusion.filesystem import filter_audio_paths, is_video
from facefusion.processors.core import get_processors_modules
from facefusion.temp_helper import clear_temp_directory, create_temp_directory, move_temp_file, resolve_temp_frame_paths
from facefusion.time_helper import calculate_end_time
from facefusion.types import ErrorCode from facefusion.types import ErrorCode
from facefusion.vision import conditional_merge_vision_mask, detect_video_resolution, extract_vision_mask, pack_resolution, read_static_image, read_static_images, read_static_video_frame, restrict_trim_frame, restrict_video_fps, restrict_video_resolution, scale_resolution, write_image from facefusion.workflows.core import clear, setup
from facefusion.workflows.core import is_process_stopping from facefusion.workflows.to_video import analyse_video, extract_frames, finalize_video, merge_frames, process_disk_frames, process_stream_frames, restore_audio
def process(start_time : float) -> ErrorCode: def process(start_time : float) -> ErrorCode:
tasks =\ tasks =\
[ [
setup, analyse_video,
extract_frames, clear,
process_video, setup
merge_frames,
restore_audio,
partial(finalize_video, start_time)
] ]
if state_manager.get_item('workflow_strategy') == 'disk':
tasks.extend(
[
extract_frames,
process_disk_frames,
merge_frames
])
if state_manager.get_item('workflow_strategy') == 'stream':
tasks.append(process_stream_frames)
tasks.extend(
[
restore_audio,
partial(finalize_video, start_time),
clear
])
process_manager.start() process_manager.start()
for task in tasks: for task in tasks:
@@ -39,159 +42,3 @@ def process(start_time : float) -> ErrorCode:
process_manager.end() process_manager.end()
return 0 return 0
def setup() -> ErrorCode:
trim_frame_start, trim_frame_end = restrict_trim_frame(state_manager.get_item('target_path'), state_manager.get_item('trim_frame_start'), state_manager.get_item('trim_frame_end'))
if analyse_video(state_manager.get_item('target_path'), trim_frame_start, trim_frame_end):
return 3
logger.debug(translator.get('clearing_temp'), __name__)
clear_temp_directory(state_manager.get_item('target_path'))
logger.debug(translator.get('creating_temp'), __name__)
create_temp_directory(state_manager.get_item('target_path'))
return 0
def extract_frames() -> ErrorCode:
trim_frame_start, trim_frame_end = restrict_trim_frame(state_manager.get_item('target_path'), state_manager.get_item('trim_frame_start'), state_manager.get_item('trim_frame_end'))
output_video_resolution = scale_resolution(detect_video_resolution(state_manager.get_item('target_path')), state_manager.get_item('output_video_scale'))
temp_video_resolution = restrict_video_resolution(state_manager.get_item('target_path'), output_video_resolution)
temp_video_fps = restrict_video_fps(state_manager.get_item('target_path'), state_manager.get_item('output_video_fps'))
logger.info(translator.get('extracting_frames').format(resolution=pack_resolution(temp_video_resolution), fps=temp_video_fps), __name__)
if ffmpeg.extract_frames(state_manager.get_item('target_path'), temp_video_resolution, temp_video_fps, trim_frame_start, trim_frame_end):
logger.debug(translator.get('extracting_frames_succeeded'), __name__)
else:
if is_process_stopping():
return 4
logger.error(translator.get('extracting_frames_failed'), __name__)
return 1
return 0
def process_video() -> ErrorCode:
temp_frame_paths = resolve_temp_frame_paths(state_manager.get_item('target_path'))
if temp_frame_paths:
with tqdm(total = len(temp_frame_paths), desc = translator.get('processing'), unit = 'frame', ascii = ' =', disable = state_manager.get_item('log_level') in [ 'warn', 'error' ]) as progress:
progress.set_postfix(execution_providers = state_manager.get_item('execution_providers'))
with ThreadPoolExecutor(max_workers = state_manager.get_item('execution_thread_count')) as executor:
futures = []
for frame_number, temp_frame_path in enumerate(temp_frame_paths):
future = executor.submit(process_temp_frame, temp_frame_path, frame_number)
futures.append(future)
for future in as_completed(futures):
if is_process_stopping():
for __future__ in futures:
__future__.cancel()
if not future.cancelled():
future.result()
progress.update()
for processor_module in get_processors_modules(state_manager.get_item('processors')):
processor_module.post_process()
if is_process_stopping():
return 4
else:
logger.error(translator.get('temp_frames_not_found'), __name__)
return 1
return 0
def merge_frames() -> ErrorCode:
trim_frame_start, trim_frame_end = restrict_trim_frame(state_manager.get_item('target_path'), state_manager.get_item('trim_frame_start'), state_manager.get_item('trim_frame_end'))
output_video_resolution = scale_resolution(detect_video_resolution(state_manager.get_item('target_path')), state_manager.get_item('output_video_scale'))
temp_video_fps = restrict_video_fps(state_manager.get_item('target_path'), state_manager.get_item('output_video_fps'))
logger.info(translator.get('merging_video').format(resolution = pack_resolution(output_video_resolution), fps = state_manager.get_item('output_video_fps')), __name__)
if ffmpeg.merge_video(state_manager.get_item('target_path'), temp_video_fps, output_video_resolution, state_manager.get_item('output_video_fps'), trim_frame_start, trim_frame_end):
logger.debug(translator.get('merging_video_succeeded'), __name__)
else:
if is_process_stopping():
return 4
logger.error(translator.get('merging_video_failed'), __name__)
return 1
return 0
def restore_audio() -> ErrorCode:
trim_frame_start, trim_frame_end = restrict_trim_frame(state_manager.get_item('target_path'), state_manager.get_item('trim_frame_start'), state_manager.get_item('trim_frame_end'))
if state_manager.get_item('output_audio_volume') == 0:
logger.info(translator.get('skipping_audio'), __name__)
move_temp_file(state_manager.get_item('target_path'), state_manager.get_item('output_path'))
else:
source_audio_path = get_first(filter_audio_paths(state_manager.get_item('source_paths')))
if source_audio_path:
if ffmpeg.replace_audio(state_manager.get_item('target_path'), source_audio_path, state_manager.get_item('output_path')):
video_manager.clear_video_pool()
logger.debug(translator.get('replacing_audio_succeeded'), __name__)
else:
video_manager.clear_video_pool()
if is_process_stopping():
return 4
logger.warn(translator.get('replacing_audio_skipped'), __name__)
move_temp_file(state_manager.get_item('target_path'), state_manager.get_item('output_path'))
else:
if ffmpeg.restore_audio(state_manager.get_item('target_path'), state_manager.get_item('output_path'), trim_frame_start, trim_frame_end):
video_manager.clear_video_pool()
logger.debug(translator.get('restoring_audio_succeeded'), __name__)
else:
video_manager.clear_video_pool()
if is_process_stopping():
return 4
logger.warn(translator.get('restoring_audio_skipped'), __name__)
move_temp_file(state_manager.get_item('target_path'), state_manager.get_item('output_path'))
return 0
def process_temp_frame(temp_frame_path : str, frame_number : int) -> bool:
reference_vision_frame = read_static_video_frame(state_manager.get_item('target_path'), state_manager.get_item('reference_frame_number'))
source_vision_frames = read_static_images(state_manager.get_item('source_paths'))
source_audio_path = get_first(filter_audio_paths(state_manager.get_item('source_paths')))
temp_video_fps = restrict_video_fps(state_manager.get_item('target_path'), state_manager.get_item('output_video_fps'))
target_vision_frame = read_static_image(temp_frame_path, 'rgba')
temp_vision_frame = target_vision_frame.copy()
temp_vision_mask = extract_vision_mask(temp_vision_frame)
source_audio_frame = get_audio_frame(source_audio_path, temp_video_fps, frame_number)
source_voice_frame = get_voice_frame(source_audio_path, temp_video_fps, frame_number)
if not numpy.any(source_audio_frame):
source_audio_frame = create_empty_audio_frame()
if not numpy.any(source_voice_frame):
source_voice_frame = create_empty_audio_frame()
for processor_module in get_processors_modules(state_manager.get_item('processors')):
temp_vision_frame, temp_vision_mask = processor_module.process_frame(
{
'reference_vision_frame': reference_vision_frame,
'source_vision_frames': source_vision_frames,
'source_audio_frame': source_audio_frame,
'source_voice_frame': source_voice_frame,
'target_vision_frame': target_vision_frame[:, :, :3],
'temp_vision_frame': temp_vision_frame[:, :, :3],
'temp_vision_mask': temp_vision_mask
})
temp_vision_frame = conditional_merge_vision_mask(temp_vision_frame, temp_vision_mask)
return write_image(temp_frame_path, temp_vision_frame)
def finalize_video(start_time : float) -> ErrorCode:
logger.debug(translator.get('clearing_temp'), __name__)
clear_temp_directory(state_manager.get_item('target_path'))
if is_video(state_manager.get_item('output_path')):
logger.info(translator.get('processing_video_succeeded').format(seconds = calculate_end_time(start_time)), __name__)
else:
logger.error(translator.get('processing_video_failed'), __name__)
return 1
return 0
+60
View File
@@ -0,0 +1,60 @@
from facefusion import content_analyser, ffmpeg, logger, process_manager, state_manager, translator
from facefusion.filesystem import is_image
from facefusion.processors.core import get_processors_modules
from facefusion.temp_helper import get_temp_file_path
from facefusion.time_helper import calculate_end_time
from facefusion.types import ErrorCode
from facefusion.vision import detect_image_resolution, pack_resolution, read_static_image, restrict_image_resolution, scale_resolution, write_image
from facefusion.workflows.core import conditional_get_target_vision_frames, is_process_stopping, process_temp_frame
def analyse_image() -> ErrorCode:
if content_analyser.analyse_image(state_manager.get_item('target_path')):
return 3
return 0
def prepare_image() -> ErrorCode:
output_image_resolution = scale_resolution(detect_image_resolution(state_manager.get_item('target_path')), state_manager.get_item('output_image_scale'))
temp_image_resolution = restrict_image_resolution(state_manager.get_item('target_path'), output_image_resolution)
logger.info(translator.get('copying_image').format(resolution = pack_resolution(temp_image_resolution)), __name__)
if ffmpeg.copy_image(state_manager.get_item('target_path'), temp_image_resolution):
logger.debug(translator.get('copying_image_succeeded'), __name__)
else:
logger.error(translator.get('copying_image_failed'), __name__)
process_manager.end()
return 1
return 0
def process_image() -> ErrorCode:
temp_image_path = get_temp_file_path(state_manager.get_item('target_path'))
target_vision_frames = conditional_get_target_vision_frames(0)
temp_vision_frame = read_static_image(temp_image_path, 'rgba')
temp_vision_frame = process_temp_frame(target_vision_frames, temp_vision_frame, 0)
write_image(temp_image_path, temp_vision_frame)
for processor_module in get_processors_modules(state_manager.get_item('processors')):
processor_module.post_process()
if is_process_stopping():
return 4
return 0
def finalize_image(start_time : float) -> ErrorCode:
output_image_resolution = scale_resolution(detect_image_resolution(state_manager.get_item('target_path')), state_manager.get_item('output_image_scale'))
logger.info(translator.get('finalizing_image').format(resolution = pack_resolution(output_image_resolution)), __name__)
if ffmpeg.finalize_image(state_manager.get_item('target_path'), state_manager.get_item('output_path'), output_image_resolution):
logger.debug(translator.get('finalizing_image_succeeded'), __name__)
else:
logger.warn(translator.get('finalizing_image_skipped'), __name__)
if is_image(state_manager.get_item('output_path')):
logger.info(translator.get('processing_image_succeeded').format(seconds = calculate_end_time(start_time)), __name__)
else:
logger.error(translator.get('processing_image_failed'), __name__)
return 1
return 0
+205
View File
@@ -0,0 +1,205 @@
from concurrent.futures import ThreadPoolExecutor, as_completed
from typing import Tuple
import cv2
import numpy
from tqdm import tqdm
from facefusion import content_analyser, ffmpeg, logger, process_manager, state_manager, translator, video_manager
from facefusion.common_helper import get_first, get_middle
from facefusion.filesystem import filter_audio_paths, is_video
from facefusion.processors.core import get_processors_modules
from facefusion.temp_helper import move_temp_file, resolve_temp_frame_set
from facefusion.time_helper import calculate_end_time
from facefusion.types import ErrorCode, Resolution, VisionFrame
from facefusion.vision import detect_video_resolution, pack_resolution, read_static_image, read_static_video_frame, restrict_trim_frame, restrict_video_fps, restrict_video_resolution, scale_resolution, select_video_frames, write_image
from facefusion.workflows.core import conditional_get_target_vision_frames, is_process_stopping, process_temp_frame
def analyse_video() -> ErrorCode:
trim_frame_start, trim_frame_end = restrict_trim_frame(state_manager.get_item('target_path'), state_manager.get_item('trim_frame_start'), state_manager.get_item('trim_frame_end'))
if content_analyser.analyse_video(state_manager.get_item('target_path'), trim_frame_start, trim_frame_end):
return 3
return 0
def extract_frames() -> ErrorCode:
trim_frame_start, trim_frame_end = restrict_trim_frame(state_manager.get_item('target_path'), state_manager.get_item('trim_frame_start'), state_manager.get_item('trim_frame_end'))
output_video_resolution = scale_resolution(detect_video_resolution(state_manager.get_item('target_path')), state_manager.get_item('output_video_scale'))
temp_video_resolution = restrict_video_resolution(state_manager.get_item('target_path'), output_video_resolution)
temp_video_fps = restrict_video_fps(state_manager.get_item('target_path'), state_manager.get_item('output_video_fps'))
logger.info(translator.get('extracting_frames').format(resolution=pack_resolution(temp_video_resolution), fps=temp_video_fps), __name__)
if ffmpeg.extract_frames(state_manager.get_item('target_path'), temp_video_resolution, temp_video_fps, trim_frame_start, trim_frame_end):
logger.debug(translator.get('extracting_frames_succeeded'), __name__)
else:
if is_process_stopping():
return 4
logger.error(translator.get('extracting_frames_failed'), __name__)
return 1
return 0
def process_disk_frame(temp_frame_path : str, frame_number : int) -> bool:
target_vision_frames = conditional_get_target_vision_frames(frame_number)
temp_vision_frame = read_static_image(temp_frame_path, 'rgba')
temp_vision_frame = process_temp_frame(target_vision_frames, temp_vision_frame, frame_number)
return write_image(temp_frame_path, temp_vision_frame)
def process_disk_frames() -> ErrorCode:
temp_frame_set = resolve_temp_frame_set(state_manager.get_item('target_path'))
if temp_frame_set:
with tqdm(total = len(temp_frame_set), desc = translator.get('processing'), unit = 'frame', ascii = ' =', disable = state_manager.get_item('log_level') in [ 'warn', 'error' ]) as progress:
progress.set_postfix(execution_providers = state_manager.get_item('execution_providers'))
read_static_video_frame(state_manager.get_item('target_path'), state_manager.get_item('reference_frame_number'))
with ThreadPoolExecutor(max_workers = state_manager.get_item('execution_thread_count')) as executor:
futures = []
for frame_number, temp_frame_path in temp_frame_set.items():
future = executor.submit(process_disk_frame, temp_frame_path, frame_number)
futures.append(future)
for future in as_completed(futures):
if is_process_stopping():
for pending_future in futures:
pending_future.cancel()
if not future.cancelled():
future.result()
progress.update()
for processor_module in get_processors_modules(state_manager.get_item('processors')):
processor_module.post_process()
if is_process_stopping():
return 4
else:
logger.error(translator.get('temp_frames_not_found'), __name__)
return 1
return 0
def process_stream_frame(frame_number : int, temp_video_resolution : Resolution) -> Tuple[int, VisionFrame]:
target_vision_frames = select_video_frames(state_manager.get_item('target_path'), frame_number, state_manager.get_item('target_frame_amount'))
target_vision_frame = get_middle(target_vision_frames)
temp_vision_frame = target_vision_frame.copy()
if not (target_vision_frame.shape[1], target_vision_frame.shape[0]) == temp_video_resolution:
temp_vision_frame = cv2.resize(target_vision_frame, temp_video_resolution)
temp_vision_frame = process_temp_frame(target_vision_frames, temp_vision_frame, frame_number)
if state_manager.get_item('temp_pixel_format') == 'bgra':
temp_vision_frame = cv2.cvtColor(temp_vision_frame, cv2.COLOR_BGR2BGRA)
if state_manager.get_item('temp_pixel_format') == 'bgr24':
temp_vision_frame = temp_vision_frame[:, :, :3]
return frame_number, numpy.ascontiguousarray(temp_vision_frame)
def process_stream_frames() -> ErrorCode:
trim_frame_start, trim_frame_end = restrict_trim_frame(state_manager.get_item('target_path'), state_manager.get_item('trim_frame_start'), state_manager.get_item('trim_frame_end'))
output_video_resolution = scale_resolution(detect_video_resolution(state_manager.get_item('target_path')), state_manager.get_item('output_video_scale'))
temp_video_resolution = restrict_video_resolution(state_manager.get_item('target_path'), output_video_resolution)
temp_video_fps = restrict_video_fps(state_manager.get_item('target_path'), state_manager.get_item('output_video_fps'))
temp_frame_range = range(trim_frame_start, trim_frame_end)
if temp_frame_range:
video_writer = video_manager.get_writer(state_manager.get_item('target_path'), temp_video_fps, temp_video_resolution, output_video_resolution, state_manager.get_item('output_video_fps'))
with tqdm(total = len(temp_frame_range), desc = translator.get('processing'), unit = 'frame', ascii = ' =', disable = state_manager.get_item('log_level') in [ 'warn', 'error' ]) as progress:
progress.set_postfix(execution_providers = state_manager.get_item('execution_providers'))
read_static_video_frame(state_manager.get_item('target_path'), state_manager.get_item('reference_frame_number'))
with ThreadPoolExecutor(max_workers = state_manager.get_item('execution_thread_count')) as executor:
futures = []
for frame_number in temp_frame_range:
future = executor.submit(process_stream_frame, frame_number, temp_video_resolution)
futures.append(future)
for future in futures:
if is_process_stopping():
for pending_future in futures:
pending_future.cancel()
if not future.cancelled():
_, temp_vision_frame = future.result()
video_manager.write_video_frame(video_writer, temp_vision_frame)
progress.update()
if not video_manager.close_video_writer(video_writer):
process_manager.stop()
for processor_module in get_processors_modules(state_manager.get_item('processors')):
processor_module.post_process()
if is_process_stopping():
return 4
else:
logger.error(translator.get('temp_frames_not_found'), __name__)
return 1
return 0
def merge_frames() -> ErrorCode:
trim_frame_start, trim_frame_end = restrict_trim_frame(state_manager.get_item('target_path'), state_manager.get_item('trim_frame_start'), state_manager.get_item('trim_frame_end'))
output_video_resolution = scale_resolution(detect_video_resolution(state_manager.get_item('target_path')), state_manager.get_item('output_video_scale'))
temp_video_fps = restrict_video_fps(state_manager.get_item('target_path'), state_manager.get_item('output_video_fps'))
logger.info(translator.get('merging_video').format(resolution = pack_resolution(output_video_resolution), fps = state_manager.get_item('output_video_fps')), __name__)
if ffmpeg.merge_video(state_manager.get_item('target_path'), temp_video_fps, output_video_resolution, state_manager.get_item('output_video_fps'), trim_frame_start, trim_frame_end):
logger.debug(translator.get('merging_video_succeeded'), __name__)
else:
if is_process_stopping():
return 4
logger.error(translator.get('merging_video_failed'), __name__)
return 1
return 0
def restore_audio() -> ErrorCode:
trim_frame_start, trim_frame_end = restrict_trim_frame(state_manager.get_item('target_path'), state_manager.get_item('trim_frame_start'), state_manager.get_item('trim_frame_end'))
if state_manager.get_item('output_audio_volume') == 0:
logger.info(translator.get('skipping_audio'), __name__)
move_temp_file(state_manager.get_item('target_path'), state_manager.get_item('output_path'))
else:
source_audio_path = get_first(filter_audio_paths(state_manager.get_item('source_paths')))
if source_audio_path:
if ffmpeg.replace_audio(state_manager.get_item('target_path'), source_audio_path, state_manager.get_item('output_path')):
video_manager.clear_video_pool()
logger.debug(translator.get('replacing_audio_succeeded'), __name__)
else:
video_manager.clear_video_pool()
if is_process_stopping():
return 4
logger.warn(translator.get('replacing_audio_skipped'), __name__)
move_temp_file(state_manager.get_item('target_path'), state_manager.get_item('output_path'))
else:
if ffmpeg.restore_audio(state_manager.get_item('target_path'), state_manager.get_item('output_path'), trim_frame_start, trim_frame_end):
video_manager.clear_video_pool()
logger.debug(translator.get('restoring_audio_succeeded'), __name__)
else:
video_manager.clear_video_pool()
if is_process_stopping():
return 4
logger.warn(translator.get('restoring_audio_skipped'), __name__)
move_temp_file(state_manager.get_item('target_path'), state_manager.get_item('output_path'))
return 0
def finalize_video(start_time : float) -> ErrorCode:
if is_video(state_manager.get_item('output_path')):
logger.info(translator.get('processing_video_succeeded').format(seconds = calculate_end_time(start_time)), __name__)
else:
logger.error(translator.get('processing_video_failed'), __name__)
return 1
return 0
+3 -3
View File
@@ -1,8 +1,8 @@
gradio-rangeslider==0.0.8 gradio-rangeslider==0.0.8
gradio==5.44.1 gradio==5.50.0
numpy==2.2.1 numpy==2.2.1
onnx==1.20.1 onnx==1.21.0
onnxruntime==1.24.3 onnxruntime==1.26.0
opencv-python==4.13.0.92 opencv-python==4.13.0.92
tqdm==4.67.3 tqdm==4.67.3
scipy==1.17.1 scipy==1.17.1
+11 -4
View File
@@ -1,7 +1,7 @@
import subprocess
import pytest import pytest
from facefusion import ffmpeg, ffmpeg_builder, process_manager
from facefusion.audio import get_audio_frame, read_static_audio from facefusion.audio import get_audio_frame, read_static_audio
from facefusion.download import conditional_download from facefusion.download import conditional_download
from .helper import get_test_example_file, get_test_examples_directory from .helper import get_test_example_file, get_test_examples_directory
@@ -9,16 +9,23 @@ from .helper import get_test_example_file, get_test_examples_directory
@pytest.fixture(scope = 'module', autouse = True) @pytest.fixture(scope = 'module', autouse = True)
def before_all() -> None: def before_all() -> None:
process_manager.start()
conditional_download(get_test_examples_directory(), conditional_download(get_test_examples_directory(),
[ [
'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/source.mp3' 'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/source.mp3'
]) ])
subprocess.run([ 'ffmpeg', '-i', get_test_example_file('source.mp3'), get_test_example_file('source.wav') ])
ffmpeg.run_ffmpeg(
ffmpeg_builder.chain(
ffmpeg_builder.set_input(get_test_example_file('source.mp3')),
ffmpeg_builder.set_output(get_test_example_file('source.wav'))
)
)
def test_get_audio_frame() -> None: def test_get_audio_frame() -> None:
assert hasattr(get_audio_frame(get_test_example_file('source.mp3'), 25), '__array_interface__') assert get_audio_frame(get_test_example_file('source.mp3'), 25).shape == (80, 16)
assert hasattr(get_audio_frame(get_test_example_file('source.wav'), 25), '__array_interface__') assert get_audio_frame(get_test_example_file('source.wav'), 25).shape == (80, 16)
assert get_audio_frame('invalid', 25) is None assert get_audio_frame('invalid', 25) is None
+13 -1
View File
@@ -3,6 +3,7 @@ import sys
import pytest import pytest
from facefusion import ffmpeg, ffmpeg_builder, process_manager
from facefusion.download import conditional_download from facefusion.download import conditional_download
from facefusion.jobs.job_manager import clear_jobs, init_jobs from facefusion.jobs.job_manager import clear_jobs, init_jobs
from .helper import get_test_example_file, get_test_examples_directory, get_test_jobs_directory, get_test_output_file, is_test_output_file, prepare_test_output_directory from .helper import get_test_example_file, get_test_examples_directory, get_test_jobs_directory, get_test_output_file, is_test_output_file, prepare_test_output_directory
@@ -10,11 +11,22 @@ from .helper import get_test_example_file, get_test_examples_directory, get_test
@pytest.fixture(scope = 'module', autouse = True) @pytest.fixture(scope = 'module', autouse = True)
def before_all() -> None: def before_all() -> None:
process_manager.start()
conditional_download(get_test_examples_directory(), conditional_download(get_test_examples_directory(),
[ [
'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/target-240p.mp4' '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.jpg') ])
ffmpeg.run_ffmpeg(
ffmpeg_builder.chain(
ffmpeg_builder.set_input(get_test_example_file('target-240p.mp4')),
[
'-vframes',
'1'
],
ffmpeg_builder.set_output(get_test_example_file('target-240p.jpg'))
)
)
@pytest.fixture(scope = 'function', autouse = True) @pytest.fixture(scope = 'function', autouse = True)
+13 -1
View File
@@ -3,6 +3,7 @@ import sys
import pytest import pytest
from facefusion import ffmpeg, ffmpeg_builder, process_manager
from facefusion.download import conditional_download from facefusion.download import conditional_download
from facefusion.jobs.job_manager import clear_jobs, init_jobs from facefusion.jobs.job_manager import clear_jobs, init_jobs
from .helper import get_test_example_file, get_test_examples_directory, get_test_jobs_directory, get_test_output_file, is_test_output_file, prepare_test_output_directory from .helper import get_test_example_file, get_test_examples_directory, get_test_jobs_directory, get_test_output_file, is_test_output_file, prepare_test_output_directory
@@ -10,12 +11,23 @@ from .helper import get_test_example_file, get_test_examples_directory, get_test
@pytest.fixture(scope = 'module', autouse = True) @pytest.fixture(scope = 'module', autouse = True)
def before_all() -> None: def before_all() -> None:
process_manager.start()
conditional_download(get_test_examples_directory(), conditional_download(get_test_examples_directory(),
[ [
'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/source.jpg', 'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/source.jpg',
'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/target-240p.mp4' '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.jpg') ])
ffmpeg.run_ffmpeg(
ffmpeg_builder.chain(
ffmpeg_builder.set_input(get_test_example_file('target-240p.mp4')),
[
'-vframes',
'1'
],
ffmpeg_builder.set_output(get_test_example_file('target-240p.jpg'))
)
)
@pytest.fixture(scope = 'function', autouse = True) @pytest.fixture(scope = 'function', autouse = True)
+14 -2
View File
@@ -3,6 +3,7 @@ import sys
import pytest import pytest
from facefusion import ffmpeg, ffmpeg_builder, process_manager
from facefusion.download import conditional_download from facefusion.download import conditional_download
from facefusion.jobs.job_manager import clear_jobs, init_jobs from facefusion.jobs.job_manager import clear_jobs, init_jobs
from .helper import get_test_example_file, get_test_examples_directory, get_test_jobs_directory, get_test_output_file, is_test_output_file, prepare_test_output_directory from .helper import get_test_example_file, get_test_examples_directory, get_test_jobs_directory, get_test_output_file, is_test_output_file, prepare_test_output_directory
@@ -10,12 +11,23 @@ from .helper import get_test_example_file, get_test_examples_directory, get_test
@pytest.fixture(scope = 'module', autouse = True) @pytest.fixture(scope = 'module', autouse = True)
def before_all() -> None: def before_all() -> None:
process_manager.start()
conditional_download(get_test_examples_directory(), conditional_download(get_test_examples_directory(),
[ [
'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/target-240p.mp4' '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-batch-1.jpg') ])
subprocess.run([ 'ffmpeg', '-i', get_test_example_file('target-240p.mp4'), '-vframes', '2', get_test_example_file('target-240p-batch-2.jpg') ]) for frame_number in [ 1, 2 ]:
ffmpeg.run_ffmpeg(
ffmpeg_builder.chain(
ffmpeg_builder.set_input(get_test_example_file('target-240p.mp4')),
[
'-vframes',
str(frame_number)
],
ffmpeg_builder.set_output(get_test_example_file('target-240p-batch-' + str(frame_number) + '.jpg'))
)
)
@pytest.fixture(scope = 'function', autouse = True) @pytest.fixture(scope = 'function', autouse = True)
+13 -1
View File
@@ -3,6 +3,7 @@ import sys
import pytest import pytest
from facefusion import ffmpeg, ffmpeg_builder, process_manager
from facefusion.download import conditional_download from facefusion.download import conditional_download
from facefusion.jobs.job_manager import clear_jobs, init_jobs from facefusion.jobs.job_manager import clear_jobs, init_jobs
from .helper import get_test_example_file, get_test_examples_directory, get_test_jobs_directory, get_test_output_file, is_test_output_file, prepare_test_output_directory from .helper import get_test_example_file, get_test_examples_directory, get_test_jobs_directory, get_test_output_file, is_test_output_file, prepare_test_output_directory
@@ -10,11 +11,22 @@ from .helper import get_test_example_file, get_test_examples_directory, get_test
@pytest.fixture(scope = 'module', autouse = True) @pytest.fixture(scope = 'module', autouse = True)
def before_all() -> None: def before_all() -> None:
process_manager.start()
conditional_download(get_test_examples_directory(), conditional_download(get_test_examples_directory(),
[ [
'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/target-240p.mp4' '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.jpg') ])
ffmpeg.run_ffmpeg(
ffmpeg_builder.chain(
ffmpeg_builder.set_input(get_test_example_file('target-240p.mp4')),
[
'-vframes',
'1'
],
ffmpeg_builder.set_output(get_test_example_file('target-240p.jpg'))
)
)
@pytest.fixture(scope = 'function', autouse = True) @pytest.fixture(scope = 'function', autouse = True)
+13 -1
View File
@@ -3,6 +3,7 @@ import sys
import pytest import pytest
from facefusion import ffmpeg, ffmpeg_builder, process_manager
from facefusion.download import conditional_download from facefusion.download import conditional_download
from facefusion.jobs.job_manager import clear_jobs, init_jobs from facefusion.jobs.job_manager import clear_jobs, init_jobs
from .helper import get_test_example_file, get_test_examples_directory, get_test_jobs_directory, get_test_output_file, is_test_output_file, prepare_test_output_directory from .helper import get_test_example_file, get_test_examples_directory, get_test_jobs_directory, get_test_output_file, is_test_output_file, prepare_test_output_directory
@@ -10,12 +11,23 @@ from .helper import get_test_example_file, get_test_examples_directory, get_test
@pytest.fixture(scope = 'module', autouse = True) @pytest.fixture(scope = 'module', autouse = True)
def before_all() -> None: def before_all() -> None:
process_manager.start()
conditional_download(get_test_examples_directory(), conditional_download(get_test_examples_directory(),
[ [
'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/source.jpg', 'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/source.jpg',
'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/target-240p.mp4' '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.jpg') ])
ffmpeg.run_ffmpeg(
ffmpeg_builder.chain(
ffmpeg_builder.set_input(get_test_example_file('target-240p.mp4')),
[
'-vframes',
'1'
],
ffmpeg_builder.set_output(get_test_example_file('target-240p.jpg'))
)
)
@pytest.fixture(scope = 'function', autouse = True) @pytest.fixture(scope = 'function', autouse = True)
+13 -1
View File
@@ -3,6 +3,7 @@ import sys
import pytest import pytest
from facefusion import ffmpeg, ffmpeg_builder, process_manager
from facefusion.download import conditional_download from facefusion.download import conditional_download
from facefusion.jobs.job_manager import clear_jobs, init_jobs from facefusion.jobs.job_manager import clear_jobs, init_jobs
from .helper import get_test_example_file, get_test_examples_directory, get_test_jobs_directory, get_test_output_file, is_test_output_file, prepare_test_output_directory from .helper import get_test_example_file, get_test_examples_directory, get_test_jobs_directory, get_test_output_file, is_test_output_file, prepare_test_output_directory
@@ -10,12 +11,23 @@ from .helper import get_test_example_file, get_test_examples_directory, get_test
@pytest.fixture(scope = 'module', autouse = True) @pytest.fixture(scope = 'module', autouse = True)
def before_all() -> None: def before_all() -> None:
process_manager.start()
conditional_download(get_test_examples_directory(), conditional_download(get_test_examples_directory(),
[ [
'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/source.jpg', 'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/source.jpg',
'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/target-240p.mp4' '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.jpg') ])
ffmpeg.run_ffmpeg(
ffmpeg_builder.chain(
ffmpeg_builder.set_input(get_test_example_file('target-240p.mp4')),
[
'-vframes',
'1'
],
ffmpeg_builder.set_output(get_test_example_file('target-240p.jpg'))
)
)
@pytest.fixture(scope = 'function', autouse = True) @pytest.fixture(scope = 'function', autouse = True)
+13 -1
View File
@@ -3,6 +3,7 @@ import sys
import pytest import pytest
from facefusion import ffmpeg, ffmpeg_builder, process_manager
from facefusion.download import conditional_download from facefusion.download import conditional_download
from facefusion.jobs.job_manager import clear_jobs, init_jobs from facefusion.jobs.job_manager import clear_jobs, init_jobs
from .helper import get_test_example_file, get_test_examples_directory, get_test_jobs_directory, get_test_output_file, is_test_output_file, prepare_test_output_directory from .helper import get_test_example_file, get_test_examples_directory, get_test_jobs_directory, get_test_output_file, is_test_output_file, prepare_test_output_directory
@@ -10,12 +11,23 @@ from .helper import get_test_example_file, get_test_examples_directory, get_test
@pytest.fixture(scope = 'module', autouse = True) @pytest.fixture(scope = 'module', autouse = True)
def before_all() -> None: def before_all() -> None:
process_manager.start()
conditional_download(get_test_examples_directory(), conditional_download(get_test_examples_directory(),
[ [
'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/source.jpg', 'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/source.jpg',
'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/target-240p.mp4' '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.jpg') ])
ffmpeg.run_ffmpeg(
ffmpeg_builder.chain(
ffmpeg_builder.set_input(get_test_example_file('target-240p.mp4')),
[
'-vframes',
'1'
],
ffmpeg_builder.set_output(get_test_example_file('target-240p.jpg'))
)
)
@pytest.fixture(scope = 'function', autouse = True) @pytest.fixture(scope = 'function', autouse = True)
+13 -1
View File
@@ -3,6 +3,7 @@ import sys
import pytest import pytest
from facefusion import ffmpeg, ffmpeg_builder, process_manager
from facefusion.download import conditional_download from facefusion.download import conditional_download
from facefusion.jobs.job_manager import clear_jobs, init_jobs from facefusion.jobs.job_manager import clear_jobs, init_jobs
from .helper import get_test_example_file, get_test_examples_directory, get_test_jobs_directory, get_test_output_file, is_test_output_file, prepare_test_output_directory from .helper import get_test_example_file, get_test_examples_directory, get_test_jobs_directory, get_test_output_file, is_test_output_file, prepare_test_output_directory
@@ -10,12 +11,23 @@ from .helper import get_test_example_file, get_test_examples_directory, get_test
@pytest.fixture(scope = 'module', autouse = True) @pytest.fixture(scope = 'module', autouse = True)
def before_all() -> None: def before_all() -> None:
process_manager.start()
conditional_download(get_test_examples_directory(), conditional_download(get_test_examples_directory(),
[ [
'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/source.jpg', 'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/source.jpg',
'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/target-240p.mp4' '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.jpg') ])
ffmpeg.run_ffmpeg(
ffmpeg_builder.chain(
ffmpeg_builder.set_input(get_test_example_file('target-240p.mp4')),
[
'-vframes',
'1'
],
ffmpeg_builder.set_output(get_test_example_file('target-240p.jpg'))
)
)
@pytest.fixture(scope = 'function', autouse = True) @pytest.fixture(scope = 'function', autouse = True)
+27 -2
View File
@@ -3,6 +3,7 @@ import sys
import pytest import pytest
from facefusion import ffmpeg, ffmpeg_builder, process_manager
from facefusion.download import conditional_download from facefusion.download import conditional_download
from facefusion.jobs.job_manager import clear_jobs, init_jobs from facefusion.jobs.job_manager import clear_jobs, init_jobs
from .helper import get_test_example_file, get_test_examples_directory, get_test_jobs_directory, get_test_output_file, is_test_output_file, prepare_test_output_directory from .helper import get_test_example_file, get_test_examples_directory, get_test_jobs_directory, get_test_output_file, is_test_output_file, prepare_test_output_directory
@@ -10,13 +11,37 @@ from .helper import get_test_example_file, get_test_examples_directory, get_test
@pytest.fixture(scope = 'module', autouse = True) @pytest.fixture(scope = 'module', autouse = True)
def before_all() -> None: def before_all() -> None:
process_manager.start()
conditional_download(get_test_examples_directory(), conditional_download(get_test_examples_directory(),
[ [
'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/source.jpg', 'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/source.jpg',
'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/target-240p.mp4' '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', '-vf', 'hue=s=0', get_test_example_file('target-240p-0sat.jpg') ])
subprocess.run([ 'ffmpeg', '-i', get_test_example_file('target-240p.mp4'), '-vf', 'hue=s=0', get_test_example_file('target-240p-0sat.mp4') ]) ffmpeg.run_ffmpeg(
ffmpeg_builder.chain(
ffmpeg_builder.set_input(get_test_example_file('target-240p.mp4')),
[
'-vframes',
'1'
],
[
'-vf',
'hue=s=0'
],
ffmpeg_builder.set_output(get_test_example_file('target-240p-0sat.jpg'))
)
)
ffmpeg.run_ffmpeg(
ffmpeg_builder.chain(
ffmpeg_builder.set_input(get_test_example_file('target-240p.mp4')),
[
'-vf',
'hue=s=0'
],
ffmpeg_builder.set_output(get_test_example_file('target-240p-0sat.mp4'))
)
)
@pytest.fixture(scope = 'function', autouse = True) @pytest.fixture(scope = 'function', autouse = True)
+13 -1
View File
@@ -3,6 +3,7 @@ import sys
import pytest import pytest
from facefusion import ffmpeg, ffmpeg_builder, process_manager
from facefusion.download import conditional_download from facefusion.download import conditional_download
from facefusion.jobs.job_manager import clear_jobs, init_jobs from facefusion.jobs.job_manager import clear_jobs, init_jobs
from .helper import get_test_example_file, get_test_examples_directory, get_test_jobs_directory, get_test_output_file, is_test_output_file, prepare_test_output_directory from .helper import get_test_example_file, get_test_examples_directory, get_test_jobs_directory, get_test_output_file, is_test_output_file, prepare_test_output_directory
@@ -10,12 +11,23 @@ from .helper import get_test_example_file, get_test_examples_directory, get_test
@pytest.fixture(scope = 'module', autouse = True) @pytest.fixture(scope = 'module', autouse = True)
def before_all() -> None: def before_all() -> None:
process_manager.start()
conditional_download(get_test_examples_directory(), conditional_download(get_test_examples_directory(),
[ [
'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/source.jpg', 'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/source.jpg',
'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/target-240p.mp4' '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.jpg') ])
ffmpeg.run_ffmpeg(
ffmpeg_builder.chain(
ffmpeg_builder.set_input(get_test_example_file('target-240p.mp4')),
[
'-vframes',
'1'
],
ffmpeg_builder.set_output(get_test_example_file('target-240p.jpg'))
)
)
@pytest.fixture(scope = 'function', autouse = True) @pytest.fixture(scope = 'function', autouse = True)
+13 -1
View File
@@ -3,6 +3,7 @@ import sys
import pytest import pytest
from facefusion import ffmpeg, ffmpeg_builder, process_manager
from facefusion.download import conditional_download from facefusion.download import conditional_download
from facefusion.jobs.job_manager import clear_jobs, count_step_total, init_jobs from facefusion.jobs.job_manager import clear_jobs, count_step_total, init_jobs
from .helper import get_test_example_file, get_test_examples_directory, get_test_jobs_directory, get_test_output_file, is_test_job_file from .helper import get_test_example_file, get_test_examples_directory, get_test_jobs_directory, get_test_output_file, is_test_job_file
@@ -10,12 +11,23 @@ from .helper import get_test_example_file, get_test_examples_directory, get_test
@pytest.fixture(scope = 'module', autouse = True) @pytest.fixture(scope = 'module', autouse = True)
def before_all() -> None: def before_all() -> None:
process_manager.start()
conditional_download(get_test_examples_directory(), conditional_download(get_test_examples_directory(),
[ [
'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/source.jpg', 'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/source.jpg',
'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/target-240p.mp4' '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.jpg') ])
ffmpeg.run_ffmpeg(
ffmpeg_builder.chain(
ffmpeg_builder.set_input(get_test_example_file('target-240p.mp4')),
[
'-vframes',
'1'
],
ffmpeg_builder.set_output(get_test_example_file('target-240p.jpg'))
)
)
@pytest.fixture(scope = 'function', autouse = True) @pytest.fixture(scope = 'function', autouse = True)

Some files were not shown because too many files have changed in this diff Show More