mirror of
https://github.com/facefusion/facefusion.git
synced 2026-07-28 21:08:54 +02:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
11bbc89a0e | ||
|
|
0ce48da740 | ||
|
|
2c72bb21c8 | ||
|
|
280713ed13 | ||
|
|
3834e3856a | ||
|
|
ac9f514d60 | ||
|
|
4895615334 | ||
|
|
df2bda6145 | ||
|
|
942d435245 | ||
|
|
5d0b2afbe9 | ||
|
|
e0921ba4b5 | ||
|
|
0b976f80a8 | ||
|
|
bd1448805c | ||
|
|
8a9c596fde | ||
|
|
300470e7c7 | ||
|
|
5f403ada65 | ||
|
|
c6aed91698 | ||
|
|
2854bf1db0 | ||
|
|
d5271d21a1 | ||
|
|
e7d22e84bf | ||
|
|
57189c638e | ||
|
|
00fb89d4f1 | ||
|
|
67cc3de934 | ||
|
|
accad0a2cb | ||
|
|
46d1575c9b | ||
|
|
f3bc5ffb4b | ||
|
|
5287ce6dcb | ||
|
|
5d6258e17e | ||
|
|
036c5c0225 | ||
|
|
2e884941f8 | ||
|
|
0ef5de1d02 | ||
|
|
dcb9a7a59b | ||
|
|
3f6738dd1d | ||
|
|
19f8f5f206 | ||
|
|
775985645e | ||
|
|
1f494f54db | ||
|
|
476a21cc7a | ||
|
|
121c3a5af6 | ||
|
|
2ac9b70550 | ||
|
|
d9553b12e8 | ||
|
|
3bda73699c | ||
|
|
7181b41f2d | ||
|
|
314ee61826 | ||
|
|
beeb1d99e9 | ||
|
|
e2e4e6a95b | ||
|
|
3eeb505c86 | ||
|
|
02f684e220 | ||
|
|
a88d8ead49 | ||
|
|
7735c3740f | ||
|
|
f2de9f1843 | ||
|
|
aea36a9e55 | ||
|
|
0f5f75ba51 | ||
|
|
460c65004b | ||
|
|
1ac0e3e9a4 | ||
|
|
9f6a19c1d2 | ||
|
|
3ff327e670 | ||
|
|
2553ad7cad | ||
|
|
6b9ddd9a4f | ||
|
|
871559cb6a | ||
|
|
2a8672b54d | ||
|
|
cc0af9175a | ||
|
|
d77be89177 | ||
|
|
e813d7df95 | ||
|
|
6eaabe123e | ||
|
|
e2bac200d6 | ||
|
|
4fe79483ea | ||
|
|
520dcbfd6b | ||
|
|
48869bedf0 | ||
|
|
927857d70d | ||
|
|
fbacb24fcc | ||
|
|
c00ea92f35 | ||
|
|
c48c238f88 | ||
|
|
dd1ded1408 | ||
|
|
95435f842c | ||
|
|
0019d3ad0f | ||
|
|
532464032b | ||
|
|
ad3b582c49 | ||
|
|
98adce8a2b | ||
|
|
3def6c8fcd | ||
|
|
504f2240f7 | ||
|
|
061522e5b7 | ||
|
|
37420eac7c | ||
|
|
1562fe2fee | ||
|
|
dc74e1c783 | ||
|
|
a097034889 | ||
|
|
d8d9d5a280 | ||
|
|
eeb342ce36 | ||
|
|
b8d00d6389 | ||
|
|
18a487347a | ||
|
|
b607e4a99e | ||
|
|
b1bc0ea43c | ||
|
|
912d7eaa52 | ||
|
|
832d954df6 | ||
|
|
78a068107a | ||
|
|
9e1c068938 | ||
|
|
bff222a12f | ||
|
|
9453a042a1 | ||
|
|
5e39c60b5c | ||
|
|
8690ccf49e | ||
|
|
e53cb63577 | ||
|
|
717ff0aa33 | ||
|
|
4922be4ad3 | ||
|
|
fdf1b841b2 | ||
|
|
92296fc5a5 | ||
|
|
6968d8fe47 | ||
|
|
b6549e873b | ||
|
|
ab7110eb92 | ||
|
|
9321b41e8e | ||
|
|
450075e20e | ||
|
|
20b392f760 | ||
|
|
bb1b8ccf13 | ||
|
|
6eace4ce29 | ||
|
|
6fe735618e | ||
|
|
527bb0ff45 | ||
|
|
e05d13c47e | ||
|
|
8bbb6e7062 | ||
|
|
76c413a2c1 | ||
|
|
430b16ce56 | ||
|
|
9a390bd5bc | ||
|
|
97e0df01b1 | ||
|
|
a8db033033 | ||
|
|
fda6b7f69d | ||
|
|
4198cca18f | ||
|
|
9a2d57ae54 | ||
|
|
fe002dc821 | ||
|
|
f8f5d6197d | ||
|
|
2290e4ea57 | ||
|
|
6ec9257e92 | ||
|
|
d727558900 | ||
|
|
07c1c936af | ||
|
|
7322bd5d52 | ||
|
|
7f40516f71 | ||
|
|
ef8567bd3a | ||
|
|
f8c90b4b25 | ||
|
|
343b7f6aad | ||
|
|
9b878431d3 | ||
|
|
74a9df35a6 | ||
|
|
0816bea6d3 | ||
|
|
6c1475f720 | ||
|
|
b549a92a35 | ||
|
|
949d9cd276 | ||
|
|
99fa2875a9 | ||
|
|
b9445fd3a4 | ||
|
|
66ee9144fb | ||
|
|
dfaa1f9cd4 | ||
|
|
a2aedc8814 | ||
|
|
cc8bfc1af4 | ||
|
|
31a16982f3 | ||
|
|
db545b8ae4 | ||
|
|
701a1b5f9e | ||
|
|
1f80aa735a | ||
|
|
ccfc5f61b6 | ||
|
|
6e7bcb599a | ||
|
|
47b703f4f5 | ||
|
|
7111af232c | ||
|
|
2709515c39 | ||
|
|
a9a4adb083 | ||
|
|
eac03796f7 | ||
|
|
5790e03009 | ||
|
|
fe7f402474 | ||
|
|
0fd6a403b8 | ||
|
|
df38778558 | ||
|
|
e15a2dec76 | ||
|
|
399e07261d | ||
|
|
61c67c8637 | ||
|
|
a16bd54493 | ||
|
|
edf8914da3 | ||
|
|
2aced392e1 | ||
|
|
2b76f3381f | ||
|
|
f40ee8335a | ||
|
|
b9c1ff8185 | ||
|
|
48bae74001 | ||
|
|
6654abbdca | ||
|
|
facd908196 | ||
|
|
5c6b247449 | ||
|
|
efab505adb | ||
|
|
246d48e079 | ||
|
|
980c2d3939 | ||
|
|
f0cacba52b | ||
|
|
61aa016659 | ||
|
|
dedf2bf829 | ||
|
|
ad1a6c9ea3 | ||
|
|
9c0859ade0 | ||
|
|
1ba2adc10b | ||
|
|
078041461d | ||
|
|
5ca5e1c2af | ||
|
|
401118106a | ||
|
|
f69fa62fa2 | ||
|
|
ef47b17a4a | ||
|
|
7c078644ad | ||
|
|
47bdcd67f1 | ||
|
|
f83ff29116 | ||
|
|
91067c6394 | ||
|
|
1e8c042473 | ||
|
|
1f0af5906d | ||
|
|
887a6ac1c7 | ||
|
|
4338d16d17 | ||
|
|
a21c23173e | ||
|
|
cd3f6f2224 | ||
|
|
f2d9eb7f0b | ||
|
|
c59596e096 | ||
|
|
4a0f4bcb9a | ||
|
|
b5a89971c4 | ||
|
|
53845585ef | ||
|
|
16d2fbf6f2 | ||
|
|
7a6ef94b26 | ||
|
|
9486750f38 | ||
|
|
1181307a0e | ||
|
|
e7a28cf072 | ||
|
|
ecc280a93f | ||
|
|
aa83142ab5 | ||
|
|
2a60280182 | ||
|
|
b9c11815f8 | ||
|
|
accb615181 | ||
|
|
5f70cbd0a3 | ||
|
|
485041c5cc | ||
|
|
e16f5bca11 | ||
|
|
02ba86308e | ||
|
|
fddb5c927b | ||
|
|
f0dc10a2f5 | ||
|
|
53d0ed0e1c | ||
|
|
f71bbab71e | ||
|
|
300b4f73a1 | ||
|
|
7e00035fdd | ||
|
|
f475ec4bbc | ||
|
|
9e63832740 | ||
|
|
ef0bfe81e2 | ||
|
|
f2f09fc215 | ||
|
|
73a298cd97 | ||
|
|
b1947ffaa2 | ||
|
|
b7e80bdafa | ||
|
|
a94015c5ee | ||
|
|
14b1ed6b76 | ||
|
|
2624c281eb | ||
|
|
bae6169274 | ||
|
|
9a27c8346c | ||
|
|
e4b8adb218 | ||
|
|
4cd588ebc8 | ||
|
|
aaebb56804 | ||
|
|
fa16752a6c | ||
|
|
09dc2ec50b | ||
|
|
3a25476ca6 | ||
|
|
01baf8d2ea | ||
|
|
a818e7f610 | ||
|
|
5f6e0cc567 | ||
|
|
e2738f1f14 | ||
|
|
db5ffdc449 | ||
|
|
2db7ad426a | ||
|
|
abeef470cc | ||
|
|
0c18508f70 | ||
|
|
60def0f30d | ||
|
|
c2676fa471 | ||
|
|
dcfbbc041f | ||
|
|
dc500eb398 | ||
|
|
3575aeea86 | ||
|
|
06a3854fbb | ||
|
|
d1a068b9d5 | ||
|
|
38ece8f7d0 | ||
|
|
5fa088457e | ||
|
|
77c1682078 | ||
|
|
40f5b403a4 | ||
|
|
908f3d2873 | ||
|
|
02e8a13adb | ||
|
|
ec3e6d777d | ||
|
|
71c79f4e96 | ||
|
|
746b3249ef | ||
|
|
e3aa3d15f5 | ||
|
|
2c07d8ca45 | ||
|
|
6e84970e0d | ||
|
|
f84b2cfc00 | ||
|
|
56842a1c58 | ||
|
|
98384a55df | ||
|
|
a01bed0bda | ||
|
|
6edf6c7266 | ||
|
|
516b5039d0 | ||
|
|
dae96f90a1 | ||
|
|
9bf9ee40a7 | ||
|
|
621bddd174 | ||
|
|
5cbc50a5aa | ||
|
|
03bbc10251 | ||
|
|
1e2a66e7b0 | ||
|
|
1b2fffd82a | ||
|
|
97801ceab3 | ||
|
|
69463b34bc | ||
|
|
e42939df9c | ||
|
|
c7ab933a8e | ||
|
|
92ec60933e | ||
|
|
f754828d08 | ||
|
|
403e89eeac | ||
|
|
672d65d61a | ||
|
|
678f4edd4c | ||
|
|
38e8610523 | ||
|
|
3444194aed | ||
|
|
c4f1d14d71 | ||
|
|
0aa56d54bb | ||
|
|
cec736d03a | ||
|
|
2485a409b8 | ||
|
|
35881a4f8d | ||
|
|
86cd7cbc8f | ||
|
|
7666411a3d | ||
|
|
b333280b8d | ||
|
|
9664bb98db | ||
|
|
139e73589a | ||
|
|
f11e8eaaea | ||
|
|
b0e849a251 | ||
|
|
d224e47d38 | ||
|
|
7b1570dc3f | ||
|
|
85eeaf39ca | ||
|
|
a10b740a80 | ||
|
|
85b85b31a2 | ||
|
|
ba884a19b1 | ||
|
|
87678da498 | ||
|
|
f1149ebc84 | ||
|
|
7870e67fdc | ||
|
|
05264ac760 | ||
|
|
71916e4e66 | ||
|
|
c903438c3f | ||
|
|
51d4ec63b0 | ||
|
|
92b1e1b3e3 | ||
|
|
1173493e84 | ||
|
|
daf906097e | ||
|
|
5b7d145aa7 | ||
|
|
519360bcd6 | ||
|
|
57fcb86b82 | ||
|
|
2cc05d4fba | ||
|
|
a498f3d618 | ||
|
|
c7976ec9d4 | ||
|
|
8801668562 | ||
|
|
a7f3de3dbc | ||
|
|
666c15f9da | ||
|
|
420d738a6b | ||
|
|
81c5e85dea | ||
|
|
8bf9170577 | ||
|
|
189d750621 | ||
|
|
f3be23d19b | ||
|
|
16e84b43ce | ||
|
|
da0da3a4b4 | ||
|
|
7b8bea4e0a | ||
|
|
b3678c4e4c |
@@ -0,0 +1,2 @@
|
||||
[run]
|
||||
patch = subprocess
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 1.3 MiB |
@@ -33,8 +33,11 @@ jobs:
|
||||
uses: actions/setup-python@v5
|
||||
with:
|
||||
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-mock
|
||||
- run: pip install httpx
|
||||
- run: pip install python-multipart
|
||||
- run: pytest
|
||||
report:
|
||||
needs: test
|
||||
@@ -48,10 +51,13 @@ jobs:
|
||||
uses: actions/setup-python@v5
|
||||
with:
|
||||
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 pytest
|
||||
- run: pip install pytest-cov
|
||||
- run: pip install pytest-mock
|
||||
- run: pip install httpx
|
||||
- run: pip install python-multipart
|
||||
- run: pytest tests --cov facefusion
|
||||
- run: coveralls --service github
|
||||
env:
|
||||
|
||||
+3
-1
@@ -1,6 +1,8 @@
|
||||
__pycache__
|
||||
.assets
|
||||
.claude
|
||||
.caches
|
||||
.jobs
|
||||
.idea
|
||||
.jobs
|
||||
.libraries
|
||||
.vscode
|
||||
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
OpenRAIL-AS license
|
||||
|
||||
Copyright (c) 2025 Henry Ruhs
|
||||
Copyright (c) 2026 Henry Ruhs
|
||||
|
||||
@@ -8,12 +8,6 @@ FaceFusion
|
||||

|
||||
|
||||
|
||||
Preview
|
||||
-------
|
||||
|
||||

|
||||
|
||||
|
||||
Installation
|
||||
------------
|
||||
|
||||
@@ -34,10 +28,10 @@ options:
|
||||
|
||||
commands:
|
||||
run run the program
|
||||
headless-run run the program in headless mode
|
||||
batch-run run the program in batch mode
|
||||
force-download force automate downloads and exit
|
||||
benchmark benchmark the program
|
||||
api start the API server
|
||||
job-list list jobs by status
|
||||
job-create create a drafted job
|
||||
job-submit submit a drafted job to become a queued job
|
||||
|
||||
+28
-11
@@ -1,3 +1,6 @@
|
||||
[workflow]
|
||||
workflow_mode =
|
||||
|
||||
[paths]
|
||||
temp_path =
|
||||
jobs_path =
|
||||
@@ -13,6 +16,7 @@ output_pattern =
|
||||
[face_detector]
|
||||
face_detector_model =
|
||||
face_detector_size =
|
||||
face_detector_margin =
|
||||
face_detector_angles =
|
||||
face_detector_score =
|
||||
|
||||
@@ -31,6 +35,9 @@ reference_face_position =
|
||||
reference_face_distance =
|
||||
reference_frame_number =
|
||||
|
||||
[face_tracker]
|
||||
face_tracker_score =
|
||||
|
||||
[face_masker]
|
||||
face_occluder_model =
|
||||
face_parser_model =
|
||||
@@ -40,32 +47,42 @@ face_mask_regions =
|
||||
face_mask_blur =
|
||||
face_mask_padding =
|
||||
|
||||
[voice_extractor]
|
||||
voice_extractor_model =
|
||||
|
||||
[frame_extraction]
|
||||
trim_frame_start =
|
||||
trim_frame_end =
|
||||
temp_frame_format =
|
||||
keep_temp =
|
||||
|
||||
[frame_distribution]
|
||||
target_frame_amount =
|
||||
|
||||
[output_creation]
|
||||
output_image_quality =
|
||||
output_image_resolution =
|
||||
output_image_scale =
|
||||
output_audio_encoder =
|
||||
output_audio_quality =
|
||||
output_audio_volume =
|
||||
output_audio_fps =
|
||||
output_video_encoder =
|
||||
output_video_preset =
|
||||
output_video_quality =
|
||||
output_video_resolution =
|
||||
output_video_scale =
|
||||
output_video_fps =
|
||||
|
||||
[processors]
|
||||
processors =
|
||||
age_modifier_model =
|
||||
age_modifier_direction =
|
||||
background_remover_model =
|
||||
background_remover_fill_color =
|
||||
background_remover_despill_color =
|
||||
deep_swapper_model =
|
||||
deep_swapper_morph =
|
||||
expression_restorer_model =
|
||||
expression_restorer_factor =
|
||||
expression_restorer_areas =
|
||||
face_debugger_items =
|
||||
face_editor_model =
|
||||
face_editor_eyebrow_direction =
|
||||
@@ -87,6 +104,7 @@ face_enhancer_blend =
|
||||
face_enhancer_weight =
|
||||
face_swapper_model =
|
||||
face_swapper_pixel_boost =
|
||||
face_swapper_weight =
|
||||
frame_colorizer_model =
|
||||
frame_colorizer_size =
|
||||
frame_colorizer_blend =
|
||||
@@ -95,28 +113,27 @@ frame_enhancer_blend =
|
||||
lip_syncer_model =
|
||||
lip_syncer_weight =
|
||||
|
||||
[uis]
|
||||
open_browser =
|
||||
ui_layouts =
|
||||
ui_workflow =
|
||||
|
||||
[download]
|
||||
download_providers =
|
||||
download_scope =
|
||||
|
||||
[benchmark]
|
||||
benchmark_mode =
|
||||
benchmark_resolutions =
|
||||
benchmark_cycle_count =
|
||||
|
||||
[api]
|
||||
api_host =
|
||||
api_port =
|
||||
api_security_strategy =
|
||||
|
||||
[execution]
|
||||
execution_device_id =
|
||||
execution_device_ids =
|
||||
execution_providers =
|
||||
execution_thread_count =
|
||||
execution_queue_count =
|
||||
|
||||
[memory]
|
||||
video_memory_strategy =
|
||||
system_memory_limit =
|
||||
|
||||
[misc]
|
||||
log_level =
|
||||
|
||||
+2
-1
@@ -4,7 +4,8 @@ import os
|
||||
|
||||
os.environ['OMP_NUM_THREADS'] = '1'
|
||||
|
||||
from facefusion import core
|
||||
from facefusion import conda, core
|
||||
|
||||
if __name__ == '__main__':
|
||||
conda.setup()
|
||||
core.cli()
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
from typing import Optional
|
||||
|
||||
from starlette.datastructures import Headers
|
||||
from starlette.types import Scope
|
||||
|
||||
|
||||
def get_sec_websocket_protocol(scope : Scope) -> Optional[str]:
|
||||
protocol_header = Headers(scope = scope).get('Sec-WebSocket-Protocol')
|
||||
|
||||
if protocol_header:
|
||||
protocol, _, _ = protocol_header.partition(',')
|
||||
return protocol.strip()
|
||||
|
||||
return None
|
||||
@@ -0,0 +1,91 @@
|
||||
import asyncio
|
||||
import os
|
||||
import uuid
|
||||
from typing import List, Optional
|
||||
|
||||
from starlette.datastructures import UploadFile
|
||||
|
||||
import facefusion.choices
|
||||
from facefusion import ffmpeg, process_manager, state_manager
|
||||
from facefusion.filesystem import create_directory, get_file_extension, get_file_format, is_audio, is_image, is_video
|
||||
from facefusion.types import ImageMetadata, MediaType
|
||||
from facefusion.vision import detect_image_resolution
|
||||
|
||||
|
||||
def extract_image_metadata(file_path : str) -> ImageMetadata:
|
||||
metadata : ImageMetadata =\
|
||||
{
|
||||
'resolution': detect_image_resolution(file_path)
|
||||
}
|
||||
return metadata
|
||||
|
||||
|
||||
def detect_media_type_by_path(file_path : str) -> Optional[MediaType]:
|
||||
if is_audio(file_path):
|
||||
return 'audio'
|
||||
if is_image(file_path):
|
||||
return 'image'
|
||||
if is_video(file_path):
|
||||
return 'video'
|
||||
return None
|
||||
|
||||
|
||||
def detect_media_type_by_format(file_format : str) -> Optional[MediaType]:
|
||||
if file_format in facefusion.choices.audio_set:
|
||||
return 'audio'
|
||||
if file_format in facefusion.choices.image_set:
|
||||
return 'image'
|
||||
if file_format in facefusion.choices.video_set:
|
||||
return 'video'
|
||||
return None
|
||||
|
||||
|
||||
def validate_asset_files(upload_files : List[UploadFile]) -> bool:
|
||||
available_encoder_set = ffmpeg.get_static_available_encoder_set()
|
||||
|
||||
for upload_file in upload_files:
|
||||
file_format = get_file_format(upload_file.filename)
|
||||
media_type = detect_media_type_by_format(file_format)
|
||||
|
||||
if media_type == 'audio' and facefusion.choices.audio_set.get(file_format) not in available_encoder_set.get('audio'): #type:ignore[call-overload]
|
||||
return False
|
||||
|
||||
if media_type == 'image' and facefusion.choices.image_set.get(file_format) not in available_encoder_set.get('image'): #type:ignore[call-overload]
|
||||
return False
|
||||
|
||||
if media_type == 'video' and facefusion.choices.video_set.get(file_format) not in available_encoder_set.get('video'): #type:ignore[call-overload]
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def save_asset_files(upload_files : List[UploadFile]) -> List[str]:
|
||||
asset_paths : List[str] = []
|
||||
api_security_strategy = state_manager.get_item('api_security_strategy')
|
||||
|
||||
for upload_file in upload_files:
|
||||
file_format = get_file_format(upload_file.filename)
|
||||
file_extension = get_file_extension(upload_file.filename)
|
||||
media_type = detect_media_type_by_format(file_format)
|
||||
temp_path = state_manager.get_temp_path()
|
||||
|
||||
create_directory(temp_path)
|
||||
|
||||
asset_file_name = uuid.uuid4().hex
|
||||
asset_path = os.path.join(temp_path, asset_file_name + file_extension)
|
||||
file_content = await upload_file.read()
|
||||
|
||||
process_manager.start()
|
||||
|
||||
if media_type == 'audio' and await asyncio.to_thread(ffmpeg.sanitize_audio, file_content, asset_path, api_security_strategy):
|
||||
asset_paths.append(asset_path)
|
||||
|
||||
if media_type == 'image' and await asyncio.to_thread(ffmpeg.sanitize_image, file_content, asset_path):
|
||||
asset_paths.append(asset_path)
|
||||
|
||||
if media_type == 'video' and await asyncio.to_thread(ffmpeg.sanitize_video, file_content, asset_path, api_security_strategy):
|
||||
asset_paths.append(asset_path)
|
||||
|
||||
process_manager.end()
|
||||
|
||||
return asset_paths
|
||||
@@ -0,0 +1,92 @@
|
||||
import uuid
|
||||
from datetime import datetime, timedelta
|
||||
from typing import List, Optional, cast
|
||||
|
||||
from facefusion.apis.asset_helper import detect_media_type_by_path, extract_image_metadata
|
||||
from facefusion.ffprobe import extract_audio_metadata, extract_video_metadata
|
||||
from facefusion.filesystem import get_file_format, get_file_name, get_file_size
|
||||
from facefusion.types import AssetId, AssetSet, AssetStore, AssetType, AudioAsset, AudioFormat, ImageAsset, ImageFormat, SessionId, VideoAsset, VideoFormat
|
||||
|
||||
ASSET_STORE : AssetStore = {}
|
||||
|
||||
|
||||
def create_asset(session_id : SessionId, asset_type : AssetType, asset_path : str) -> Optional[AudioAsset | ImageAsset | VideoAsset]:
|
||||
asset_id = str(uuid.uuid4())
|
||||
asset_name = get_file_name(asset_path)
|
||||
asset_format = get_file_format(asset_path)
|
||||
asset_size = get_file_size(asset_path)
|
||||
media_type = detect_media_type_by_path(asset_path)
|
||||
created_at = datetime.now()
|
||||
expires_at = created_at + timedelta(hours = 2)
|
||||
|
||||
if session_id not in ASSET_STORE:
|
||||
ASSET_STORE[session_id] = {}
|
||||
|
||||
if media_type == 'audio':
|
||||
ASSET_STORE[session_id][asset_id] = cast(AudioAsset,
|
||||
{
|
||||
'id': asset_id,
|
||||
'created_at': created_at,
|
||||
'expires_at': expires_at,
|
||||
'type': asset_type,
|
||||
'media': media_type,
|
||||
'name': asset_name,
|
||||
'format': cast(AudioFormat, asset_format),
|
||||
'size': asset_size,
|
||||
'path': asset_path,
|
||||
'metadata': extract_audio_metadata(asset_path)
|
||||
})
|
||||
|
||||
if media_type == 'image':
|
||||
ASSET_STORE[session_id][asset_id] = cast(ImageAsset,
|
||||
{
|
||||
'id': asset_id,
|
||||
'created_at': created_at,
|
||||
'expires_at': expires_at,
|
||||
'type': asset_type,
|
||||
'media': media_type,
|
||||
'name': asset_name,
|
||||
'format': cast(ImageFormat, asset_format),
|
||||
'size': asset_size,
|
||||
'path': asset_path,
|
||||
'metadata': extract_image_metadata(asset_path)
|
||||
})
|
||||
|
||||
if media_type == 'video':
|
||||
ASSET_STORE[session_id][asset_id] = cast(VideoAsset,
|
||||
{
|
||||
'id': asset_id,
|
||||
'created_at': created_at,
|
||||
'expires_at': expires_at,
|
||||
'type': asset_type,
|
||||
'media': media_type,
|
||||
'name': asset_name,
|
||||
'format': cast(VideoFormat, asset_format),
|
||||
'size': asset_size,
|
||||
'path': asset_path,
|
||||
'metadata': extract_video_metadata(asset_path)
|
||||
})
|
||||
|
||||
return ASSET_STORE[session_id].get(asset_id)
|
||||
|
||||
|
||||
def get_assets(session_id : SessionId) -> Optional[AssetSet]:
|
||||
return ASSET_STORE.get(session_id)
|
||||
|
||||
|
||||
def get_asset(session_id : SessionId, asset_id : AssetId) -> Optional[AudioAsset | ImageAsset | VideoAsset]:
|
||||
if session_id in ASSET_STORE:
|
||||
return ASSET_STORE.get(session_id).get(asset_id)
|
||||
return None
|
||||
|
||||
|
||||
def delete_assets(session_id : SessionId, asset_ids : List[AssetId]) -> None:
|
||||
if session_id in ASSET_STORE:
|
||||
for asset_id in asset_ids:
|
||||
if asset_id in ASSET_STORE.get(session_id):
|
||||
del ASSET_STORE[session_id][asset_id]
|
||||
return None
|
||||
|
||||
|
||||
def clear() -> None:
|
||||
ASSET_STORE.clear()
|
||||
@@ -0,0 +1,58 @@
|
||||
from types import ModuleType
|
||||
from typing import List
|
||||
|
||||
from starlette.applications import Starlette
|
||||
from starlette.middleware import Middleware
|
||||
from starlette.middleware.cors import CORSMiddleware
|
||||
from starlette.routing import Route, WebSocketRoute
|
||||
|
||||
from facefusion.apis.endpoints.assets import delete_assets, get_asset, get_assets, upload_asset
|
||||
from facefusion.apis.endpoints.capabilities import get_capabilities
|
||||
from facefusion.apis.endpoints.metrics import get_metrics, websocket_metrics
|
||||
from facefusion.apis.endpoints.ping import websocket_ping
|
||||
from facefusion.apis.endpoints.session import create_session, destroy_session, get_session, refresh_session
|
||||
from facefusion.apis.endpoints.state import get_state, set_state
|
||||
from facefusion.apis.endpoints.stream import delete_stream, post_stream, websocket_stream
|
||||
from facefusion.apis.middlewares.session import create_session_guard
|
||||
from facefusion.libraries import aom as aom_module, datachannel as datachannel_module, opus as opus_module, vpx as vpx_module
|
||||
|
||||
|
||||
def get_common_modules() -> List[ModuleType]:
|
||||
return [ aom_module, datachannel_module, opus_module, vpx_module ]
|
||||
|
||||
|
||||
def pre_check() -> bool:
|
||||
for common_module in get_common_modules():
|
||||
if not common_module.pre_check():
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def create_api() -> Starlette:
|
||||
session_guard = Middleware(create_session_guard)
|
||||
routes =\
|
||||
[
|
||||
Route('/session', create_session, methods = [ 'POST' ]),
|
||||
Route('/session', get_session, methods = [ 'GET' ], middleware = [ session_guard ]),
|
||||
Route('/session', refresh_session, methods = [ 'PUT' ]),
|
||||
Route('/session', destroy_session, methods = [ 'DELETE' ], middleware = [ session_guard ]),
|
||||
Route('/state', get_state, methods = [ 'GET' ], middleware = [ session_guard ]),
|
||||
Route('/state', set_state, methods = [ 'PUT' ], middleware = [ session_guard ]),
|
||||
Route('/assets', get_assets, methods = [ 'GET' ], middleware = [ session_guard ]),
|
||||
Route('/assets', upload_asset, methods = [ 'POST' ], middleware = [ session_guard ]),
|
||||
Route('/assets/{asset_id}', get_asset, methods = [ 'GET' ], middleware = [ session_guard ]),
|
||||
Route('/assets', delete_assets, methods = [ 'DELETE' ], middleware = [ session_guard ]),
|
||||
Route('/capabilities', get_capabilities, methods = [ 'GET' ]),
|
||||
Route('/metrics', get_metrics, methods = [ 'GET' ], middleware = [ session_guard ]),
|
||||
Route('/stream', post_stream, methods = [ 'POST' ], middleware = [ session_guard ]),
|
||||
Route('/stream', delete_stream, methods = [ 'DELETE' ], name = 'delete_stream', middleware = [ session_guard ]),
|
||||
WebSocketRoute('/metrics', websocket_metrics, middleware = [ session_guard ]),
|
||||
WebSocketRoute('/ping', websocket_ping, middleware = [ session_guard ]),
|
||||
WebSocketRoute('/stream', websocket_stream, middleware = [ session_guard ])
|
||||
]
|
||||
|
||||
api = Starlette(routes = routes)
|
||||
api.add_middleware(CORSMiddleware, allow_origins = [ '*' ], allow_methods = [ '*' ], allow_headers = [ '*' ])
|
||||
|
||||
return api
|
||||
@@ -0,0 +1,135 @@
|
||||
import os
|
||||
from typing import List
|
||||
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import FileResponse, JSONResponse, Response
|
||||
from starlette.status import HTTP_200_OK, HTTP_201_CREATED, HTTP_400_BAD_REQUEST, HTTP_404_NOT_FOUND, HTTP_415_UNSUPPORTED_MEDIA_TYPE
|
||||
|
||||
from facefusion import session_context, session_manager
|
||||
from facefusion.apis import asset_store
|
||||
from facefusion.apis.asset_helper import save_asset_files, validate_asset_files
|
||||
from facefusion.apis.endpoints.session import extract_access_token
|
||||
from facefusion.filesystem import remove_file
|
||||
|
||||
|
||||
async def upload_asset(request : Request) -> Response:
|
||||
access_token = extract_access_token(request.scope)
|
||||
session_id = session_manager.find_session_id(access_token)
|
||||
asset_type = request.query_params.get('type')
|
||||
|
||||
if session_id and asset_type in [ 'source', 'target' ]:
|
||||
session_context.set_session_id(session_id)
|
||||
|
||||
form = await request.form()
|
||||
upload_files = form.getlist('file')
|
||||
|
||||
if upload_files and validate_asset_files(upload_files):
|
||||
asset_paths = await save_asset_files(upload_files)
|
||||
|
||||
if asset_paths:
|
||||
asset_ids : List[str] = []
|
||||
|
||||
for asset_path in asset_paths:
|
||||
asset = asset_store.create_asset(session_id, asset_type, asset_path)
|
||||
|
||||
if asset:
|
||||
asset_id = asset.get('id')
|
||||
|
||||
if asset_id:
|
||||
asset_ids.append(asset_id)
|
||||
|
||||
if asset_ids:
|
||||
return JSONResponse(
|
||||
{
|
||||
'asset_ids': asset_ids
|
||||
}, status_code = HTTP_201_CREATED)
|
||||
|
||||
return Response(status_code = HTTP_415_UNSUPPORTED_MEDIA_TYPE)
|
||||
|
||||
return Response(status_code = HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
async def get_assets(request : Request) -> Response:
|
||||
access_token = extract_access_token(request.scope)
|
||||
session_id = session_manager.find_session_id(access_token)
|
||||
|
||||
if session_id:
|
||||
asset_set = asset_store.get_assets(session_id)
|
||||
assets = []
|
||||
|
||||
if asset_set:
|
||||
for asset in asset_set.values():
|
||||
assets.append(
|
||||
{
|
||||
'id': asset.get('id'),
|
||||
'created_at': asset.get('created_at').isoformat(),
|
||||
'expires_at': asset.get('expires_at').isoformat(),
|
||||
'type': asset.get('type'),
|
||||
'media': asset.get('media'),
|
||||
'name': asset.get('name'),
|
||||
'format': asset.get('format'),
|
||||
'size': asset.get('size'),
|
||||
'metadata': asset.get('metadata')
|
||||
})
|
||||
|
||||
return JSONResponse(
|
||||
{
|
||||
'assets': assets
|
||||
}, status_code = HTTP_200_OK)
|
||||
|
||||
return Response(status_code = HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
async def get_asset(request : Request) -> Response:
|
||||
access_token = extract_access_token(request.scope)
|
||||
session_id = session_manager.find_session_id(access_token)
|
||||
asset_id = request.path_params.get('asset_id')
|
||||
|
||||
if session_id and asset_id:
|
||||
asset = asset_store.get_asset(session_id, asset_id)
|
||||
|
||||
if asset:
|
||||
if request.query_params.get('action') == 'download':
|
||||
asset_path = asset.get('path')
|
||||
|
||||
if os.path.exists(asset_path):
|
||||
return FileResponse(asset_path, filename = asset.get('name'))
|
||||
|
||||
return JSONResponse(
|
||||
{
|
||||
'id': asset.get('id'),
|
||||
'created_at': asset.get('created_at').isoformat(),
|
||||
'expires_at': asset.get('expires_at').isoformat(),
|
||||
'type': asset.get('type'),
|
||||
'media': asset.get('media'),
|
||||
'name': asset.get('name'),
|
||||
'format': asset.get('format'),
|
||||
'size': asset.get('size'),
|
||||
'metadata': asset.get('metadata')
|
||||
}, status_code = HTTP_200_OK)
|
||||
|
||||
return Response(status_code = HTTP_404_NOT_FOUND)
|
||||
|
||||
|
||||
async def delete_assets(request : Request) -> Response:
|
||||
access_token = extract_access_token(request.scope)
|
||||
session_id = session_manager.find_session_id(access_token)
|
||||
body = await request.json()
|
||||
asset_ids = body.get('asset_ids')
|
||||
|
||||
if session_id and asset_ids:
|
||||
asset_set = asset_store.get_assets(session_id)
|
||||
|
||||
if asset_set:
|
||||
|
||||
for asset_id in asset_ids:
|
||||
if asset_id in asset_set:
|
||||
asset = asset_set.get(asset_id)
|
||||
|
||||
if asset:
|
||||
remove_file(asset.get('path'))
|
||||
|
||||
asset_store.delete_assets(session_id, asset_ids)
|
||||
return Response(status_code = HTTP_200_OK)
|
||||
|
||||
return Response(status_code = HTTP_404_NOT_FOUND)
|
||||
@@ -0,0 +1,20 @@
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import JSONResponse
|
||||
from starlette.status import HTTP_200_OK
|
||||
|
||||
import facefusion.choices
|
||||
from facefusion import capability_store
|
||||
|
||||
|
||||
async def get_capabilities(request : Request) -> JSONResponse:
|
||||
capabilities =\
|
||||
{
|
||||
'formats':
|
||||
{
|
||||
'audio': facefusion.choices.audio_formats,
|
||||
'image': facefusion.choices.image_formats,
|
||||
'video': facefusion.choices.video_formats
|
||||
},
|
||||
'arguments': capability_store.get_api_capability_set()
|
||||
}
|
||||
return JSONResponse(capabilities, status_code = HTTP_200_OK)
|
||||
@@ -0,0 +1,32 @@
|
||||
import asyncio
|
||||
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import JSONResponse, Response
|
||||
from starlette.status import HTTP_404_NOT_FOUND
|
||||
from starlette.websockets import WebSocket
|
||||
|
||||
from facefusion.apis.api_helper import get_sec_websocket_protocol
|
||||
from facefusion.system import get_metrics_set
|
||||
|
||||
|
||||
async def get_metrics(request : Request) -> Response:
|
||||
metrics_set = get_metrics_set()
|
||||
|
||||
if metrics_set:
|
||||
return JSONResponse(metrics_set)
|
||||
|
||||
return Response(status_code = HTTP_404_NOT_FOUND)
|
||||
|
||||
|
||||
async def websocket_metrics(websocket : WebSocket) -> None:
|
||||
subprotocol = get_sec_websocket_protocol(websocket.scope)
|
||||
await websocket.accept(subprotocol = subprotocol)
|
||||
|
||||
try:
|
||||
while True:
|
||||
metrics_set = get_metrics_set()
|
||||
await websocket.send_json(metrics_set)
|
||||
await asyncio.sleep(2)
|
||||
|
||||
except Exception:
|
||||
pass
|
||||
@@ -0,0 +1,16 @@
|
||||
from starlette.websockets import WebSocket
|
||||
|
||||
from facefusion.apis.api_helper import get_sec_websocket_protocol
|
||||
|
||||
|
||||
async def websocket_ping(websocket : WebSocket) -> None:
|
||||
subprotocol = get_sec_websocket_protocol(websocket.scope)
|
||||
|
||||
await websocket.accept(subprotocol = subprotocol)
|
||||
|
||||
try:
|
||||
while True:
|
||||
await websocket.receive()
|
||||
|
||||
except Exception:
|
||||
pass
|
||||
@@ -0,0 +1,75 @@
|
||||
import os
|
||||
import secrets
|
||||
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import JSONResponse
|
||||
from starlette.status import HTTP_200_OK, HTTP_201_CREATED, HTTP_401_UNAUTHORIZED
|
||||
|
||||
from facefusion import session_context, session_manager, translator
|
||||
from facefusion.apis.session_helper import extract_access_token
|
||||
|
||||
|
||||
async def create_session(request : Request) -> JSONResponse:
|
||||
body = await request.json()
|
||||
|
||||
if not body.get('api_key') or body.get('api_key') == os.getenv('FACEFUSION_API_KEY'):
|
||||
session_id = secrets.token_urlsafe(16)
|
||||
session = session_manager.create_session()
|
||||
session_context.set_session_id(session_id)
|
||||
session_manager.set_session(session_id, session)
|
||||
|
||||
return JSONResponse(
|
||||
{
|
||||
'access_token': session.get('access_token'),
|
||||
'refresh_token': session.get('refresh_token')
|
||||
}, status_code = HTTP_201_CREATED)
|
||||
|
||||
return JSONResponse(
|
||||
{
|
||||
'message': translator.get('something_went_wrong', 'facefusion.apis')
|
||||
}, status_code = HTTP_401_UNAUTHORIZED)
|
||||
|
||||
|
||||
async def get_session(request : Request) -> JSONResponse:
|
||||
access_token = extract_access_token(request.scope)
|
||||
session_id = session_manager.find_session_id(access_token)
|
||||
session = session_manager.get_session(session_id)
|
||||
|
||||
return JSONResponse(
|
||||
{
|
||||
'access_token': session.get('access_token'),
|
||||
'refresh_token': session.get('refresh_token'),
|
||||
'created_at': session.get('created_at').isoformat(),
|
||||
'expires_at': session.get('expires_at').isoformat()
|
||||
}, status_code = HTTP_200_OK)
|
||||
|
||||
|
||||
async def refresh_session(request : Request) -> JSONResponse:
|
||||
body = await request.json()
|
||||
|
||||
for session_id, session in session_manager.SESSIONS.items():
|
||||
if session.get('refresh_token') == body.get('refresh_token') and session_manager.validate_session(session_id):
|
||||
__session__ = session_manager.create_session()
|
||||
session_manager.set_session(session_id, __session__)
|
||||
|
||||
return JSONResponse(
|
||||
{
|
||||
'access_token': __session__.get('access_token'),
|
||||
'refresh_token': __session__.get('refresh_token')
|
||||
}, status_code = HTTP_200_OK)
|
||||
|
||||
return JSONResponse(
|
||||
{
|
||||
'message': translator.get('something_went_wrong', 'facefusion.apis')
|
||||
}, status_code = HTTP_401_UNAUTHORIZED)
|
||||
|
||||
|
||||
async def destroy_session(request : Request) -> JSONResponse:
|
||||
access_token = extract_access_token(request.scope)
|
||||
session_id = session_manager.find_session_id(access_token)
|
||||
session_manager.clear_session(session_id)
|
||||
|
||||
return JSONResponse(
|
||||
{
|
||||
'message': translator.get('ok', 'facefusion.apis')
|
||||
}, status_code = HTTP_200_OK)
|
||||
@@ -0,0 +1,93 @@
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import JSONResponse
|
||||
from starlette.status import HTTP_200_OK, HTTP_400_BAD_REQUEST, HTTP_404_NOT_FOUND, HTTP_422_UNPROCESSABLE_CONTENT
|
||||
|
||||
from facefusion import args_helper, capability_store, session_manager, state_manager, translator
|
||||
from facefusion.apis import asset_store
|
||||
from facefusion.apis.endpoints.session import extract_access_token
|
||||
|
||||
|
||||
async def get_state(request : Request) -> JSONResponse:
|
||||
api_args = args_helper.extract_api_args(state_manager.get_state())
|
||||
return JSONResponse(state_manager.collect_state(api_args), status_code = HTTP_200_OK)
|
||||
|
||||
|
||||
async def set_state(request : Request) -> JSONResponse:
|
||||
__api_args__ = {}
|
||||
|
||||
action = request.query_params.get('action')
|
||||
asset_type = request.query_params.get('type')
|
||||
|
||||
if action == 'select' and asset_type == 'source':
|
||||
return await select_source(request)
|
||||
|
||||
if action == 'select' and asset_type == 'target':
|
||||
return await select_target(request)
|
||||
|
||||
body = await request.json()
|
||||
api_args = capability_store.get_api_arguments()
|
||||
|
||||
for key, value in body.items():
|
||||
if key not in api_args:
|
||||
return JSONResponse(
|
||||
{
|
||||
'message': translator.get('invalid_state_key', 'facefusion.apis')
|
||||
}, status_code = HTTP_400_BAD_REQUEST)
|
||||
__api_args__[key] = value
|
||||
|
||||
if __api_args__:
|
||||
|
||||
for key, value in __api_args__.items():
|
||||
state_manager.set_item(key, value)
|
||||
|
||||
__api_args__ = args_helper.extract_api_args(state_manager.get_state())
|
||||
return JSONResponse(state_manager.collect_state(__api_args__), status_code = HTTP_200_OK)
|
||||
|
||||
return JSONResponse({}, status_code = HTTP_422_UNPROCESSABLE_CONTENT)
|
||||
|
||||
|
||||
async def select_source(request : Request) -> JSONResponse:
|
||||
body = await request.json()
|
||||
asset_ids = body.get('asset_ids')
|
||||
access_token = extract_access_token(request.scope)
|
||||
session_id = session_manager.find_session_id(access_token)
|
||||
|
||||
if isinstance(asset_ids, list) and session_id:
|
||||
source_paths = []
|
||||
|
||||
for asset_id in asset_ids:
|
||||
asset = asset_store.get_asset(session_id, asset_id)
|
||||
|
||||
if asset:
|
||||
source_paths.append(asset.get('path'))
|
||||
|
||||
state_manager.set_item('source_paths', source_paths)
|
||||
|
||||
__api_args__ = args_helper.extract_api_args(state_manager.get_state())
|
||||
return JSONResponse(state_manager.collect_state(__api_args__), status_code = HTTP_200_OK)
|
||||
|
||||
return JSONResponse(
|
||||
{
|
||||
'message': translator.get('source_asset_not_found', 'facefusion.apis')
|
||||
}, status_code = HTTP_404_NOT_FOUND)
|
||||
|
||||
|
||||
async def select_target(request : Request) -> JSONResponse:
|
||||
body = await request.json()
|
||||
asset_id = body.get('asset_id')
|
||||
access_token = extract_access_token(request.scope)
|
||||
session_id = session_manager.find_session_id(access_token)
|
||||
|
||||
if isinstance(asset_id, str) and session_id:
|
||||
asset = asset_store.get_asset(session_id, asset_id)
|
||||
|
||||
if asset:
|
||||
state_manager.set_item('target_path', asset.get('path'))
|
||||
|
||||
__api_args__ = args_helper.extract_api_args(state_manager.get_state())
|
||||
return JSONResponse(state_manager.collect_state(__api_args__), status_code = HTTP_200_OK)
|
||||
|
||||
return JSONResponse(
|
||||
{
|
||||
'message': translator.get('target_asset_not_found', 'facefusion.apis')
|
||||
}, status_code = HTTP_404_NOT_FOUND)
|
||||
@@ -0,0 +1,52 @@
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import Response
|
||||
from starlette.status import HTTP_200_OK, HTTP_201_CREATED, HTTP_404_NOT_FOUND
|
||||
from starlette.websockets import WebSocket, WebSocketState
|
||||
|
||||
from facefusion import session_context, session_manager
|
||||
from facefusion.apis.api_helper import get_sec_websocket_protocol
|
||||
from facefusion.apis.session_helper import extract_access_token
|
||||
from facefusion.apis.stream_manager import destroy_stream, process_image, process_video
|
||||
|
||||
|
||||
async def websocket_stream(websocket : WebSocket) -> None:
|
||||
subprotocol = get_sec_websocket_protocol(websocket.scope)
|
||||
access_token = extract_access_token(websocket.scope)
|
||||
session_id = session_manager.find_session_id(access_token)
|
||||
session_context.set_session_id(session_id)
|
||||
|
||||
await websocket.accept(subprotocol = subprotocol)
|
||||
await process_image(websocket)
|
||||
|
||||
if websocket.client_state == WebSocketState.CONNECTED:
|
||||
await websocket.close()
|
||||
|
||||
|
||||
async def post_stream(request : Request) -> Response:
|
||||
headers =\
|
||||
{
|
||||
'Location': request.url_for('delete_stream').path
|
||||
}
|
||||
content_type = request.headers.get('content-type')
|
||||
access_token = extract_access_token(request.scope)
|
||||
session_id = session_manager.find_session_id(access_token)
|
||||
session_context.set_session_id(session_id)
|
||||
|
||||
if session_id and content_type == 'application/sdp':
|
||||
sdp_offer = await request.body()
|
||||
sdp_answer = process_video(session_id, sdp_offer.decode())
|
||||
|
||||
if sdp_answer:
|
||||
return Response(sdp_answer, status_code = HTTP_201_CREATED, media_type = 'application/sdp', headers = headers)
|
||||
|
||||
return Response(status_code = HTTP_404_NOT_FOUND)
|
||||
|
||||
|
||||
async def delete_stream(request : Request) -> Response:
|
||||
access_token = extract_access_token(request.scope)
|
||||
session_id = session_manager.find_session_id(access_token)
|
||||
|
||||
if session_id and destroy_stream(session_id):
|
||||
return Response(status_code = HTTP_200_OK)
|
||||
|
||||
return Response(status_code = HTTP_404_NOT_FOUND)
|
||||
@@ -0,0 +1,15 @@
|
||||
from facefusion.types import Locales
|
||||
|
||||
LOCALES : Locales =\
|
||||
{
|
||||
'en':
|
||||
{
|
||||
'ok': 'ok',
|
||||
'something_went_wrong': 'something went wrong',
|
||||
'invalid_access_token': 'invalid access token',
|
||||
'invalid_refresh_token': 'invalid refresh token',
|
||||
'source_asset_not_found': 'source asset not found',
|
||||
'target_asset_not_found': 'target asset not found',
|
||||
'invalid_state_key': 'invalid state key'
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
from starlette.responses import JSONResponse
|
||||
from starlette.status import HTTP_401_UNAUTHORIZED, HTTP_426_UPGRADE_REQUIRED
|
||||
from starlette.types import ASGIApp, Receive, Scope, Send
|
||||
|
||||
from facefusion import session_manager, translator
|
||||
from facefusion.apis.session_helper import extract_access_token
|
||||
|
||||
|
||||
def create_session_guard(app : ASGIApp) -> ASGIApp:
|
||||
async def middleware(scope : Scope, receive : Receive, send : Send) -> None:
|
||||
access_token = extract_access_token(scope)
|
||||
|
||||
if access_token:
|
||||
session_id = session_manager.find_session_id(access_token)
|
||||
|
||||
if session_id:
|
||||
if session_manager.validate_session(session_id):
|
||||
return await app(scope, receive, send)
|
||||
|
||||
response = JSONResponse(
|
||||
{
|
||||
'message': translator.get('invalid_access_token', 'facefusion.apis')
|
||||
}, status_code = HTTP_426_UPGRADE_REQUIRED)
|
||||
|
||||
return await response(scope, receive, send)
|
||||
|
||||
response = JSONResponse(
|
||||
{
|
||||
'message': translator.get('invalid_access_token', 'facefusion.apis')
|
||||
}, status_code = HTTP_401_UNAUTHORIZED)
|
||||
|
||||
return await response(scope, receive, send)
|
||||
|
||||
return middleware
|
||||
@@ -0,0 +1,29 @@
|
||||
from typing import Optional
|
||||
|
||||
from starlette.datastructures import Headers
|
||||
from starlette.types import Scope
|
||||
|
||||
from facefusion.apis.api_helper import get_sec_websocket_protocol
|
||||
from facefusion.types import Token
|
||||
|
||||
|
||||
def extract_access_token(scope : Scope) -> Optional[Token]:
|
||||
if scope.get('type') == 'http':
|
||||
auth_header = Headers(scope = scope).get('Authorization')
|
||||
|
||||
if auth_header:
|
||||
auth_prefix, _, access_token = auth_header.partition(' ')
|
||||
|
||||
if auth_prefix.lower() == 'bearer' and access_token:
|
||||
return access_token
|
||||
|
||||
if scope.get('type') == 'websocket':
|
||||
subprotocol = get_sec_websocket_protocol(scope)
|
||||
|
||||
if subprotocol:
|
||||
protocol_prefix, _, access_token = subprotocol.partition('.')
|
||||
|
||||
if protocol_prefix == 'access_token' and access_token:
|
||||
return access_token
|
||||
|
||||
return None
|
||||
@@ -0,0 +1,67 @@
|
||||
from functools import partial
|
||||
from queue import Queue
|
||||
from typing import Optional, Tuple
|
||||
|
||||
import numpy
|
||||
|
||||
from facefusion import rtc
|
||||
from facefusion.apis.stream_event import create_receive_event
|
||||
from facefusion.codecs import opus_decoder, opus_encoder
|
||||
from facefusion.types import AudioCodec, AudioFrame, Buffer, OpusDecoder, RtcPeer, RtcPeerAudio, Time
|
||||
|
||||
|
||||
def run_audio_encode_loop(rtc_peer : RtcPeer, audio_queue : Queue[Tuple[Time, AudioFrame]]) -> None:
|
||||
audio_codec = rtc_peer.get('audio').get('codec')
|
||||
temp_audio_time, temp_audio_frame = audio_queue.get()
|
||||
audio_encoder = opus_encoder.create(48000, 2)
|
||||
|
||||
while numpy.any(temp_audio_frame):
|
||||
audio_buffer = opus_encoder.encode(audio_encoder, temp_audio_frame.tobytes(), 2)
|
||||
|
||||
if audio_buffer:
|
||||
audio_timestamp = rtc.convert_time_to_timestamp(audio_codec, temp_audio_time)
|
||||
rtc.send_audio(rtc_peer, audio_buffer, audio_timestamp)
|
||||
|
||||
temp_audio_time, temp_audio_frame = audio_queue.get()
|
||||
|
||||
opus_encoder.destroy(audio_encoder)
|
||||
|
||||
|
||||
def receive_audio_frames(rtc_peer_audio : RtcPeerAudio, audio_queue : Queue[Tuple[Time, AudioFrame]]) -> None:
|
||||
audio_track = rtc_peer_audio.get('receiver_track')
|
||||
audio_codec = rtc_peer_audio.get('codec')
|
||||
audio_decoder = create_audio_decoder(audio_codec)
|
||||
|
||||
audio_frame_handler = partial(handle_audio_frame, audio_codec, audio_decoder, audio_queue)
|
||||
receive_event = create_receive_event(audio_track, audio_frame_handler)
|
||||
receive_event.wait()
|
||||
|
||||
empty_audio_frame = numpy.empty(0)
|
||||
audio_queue.put((0.0, empty_audio_frame))
|
||||
destroy_audio_decoder(audio_codec, audio_decoder)
|
||||
|
||||
|
||||
def decode_audio_frame(audio_codec : AudioCodec, audio_decoder : OpusDecoder, input_buffer : Buffer) -> Optional[Buffer]:
|
||||
if audio_codec == 'opus':
|
||||
return opus_decoder.decode(audio_decoder, input_buffer, 2)
|
||||
return None
|
||||
|
||||
|
||||
def create_audio_decoder(audio_codec : AudioCodec) -> Optional[OpusDecoder]:
|
||||
if audio_codec == 'opus':
|
||||
return opus_decoder.create(48000, 2)
|
||||
return None
|
||||
|
||||
|
||||
def destroy_audio_decoder(audio_codec : AudioCodec, audio_decoder : OpusDecoder) -> None:
|
||||
if audio_codec == 'opus':
|
||||
opus_decoder.destroy(audio_decoder)
|
||||
|
||||
|
||||
def handle_audio_frame(audio_codec : AudioCodec, audio_decoder : OpusDecoder, audio_queue : Queue[Tuple[Time, AudioFrame]], audio_buffer : Buffer, audio_timestamp : int) -> None:
|
||||
audio_frame = decode_audio_frame(audio_codec, audio_decoder, audio_buffer)
|
||||
|
||||
if audio_frame:
|
||||
audio_frame = numpy.frombuffer(audio_frame, dtype = numpy.float32)
|
||||
audio_time = rtc.convert_timestamp_to_time(audio_codec, audio_timestamp)
|
||||
audio_queue.put((audio_time, audio_frame))
|
||||
@@ -0,0 +1,30 @@
|
||||
import ctypes
|
||||
import threading
|
||||
from functools import partial
|
||||
|
||||
from facefusion.libraries import datachannel as datachannel_module
|
||||
from facefusion.types import FrameHandler
|
||||
|
||||
|
||||
def create_receive_event(track : int, frame_handler : FrameHandler) -> threading.Event:
|
||||
datachannel_library = datachannel_module.create_static_library()
|
||||
receive_event = threading.Event()
|
||||
|
||||
frame_callback = ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p)(partial(dispatch_frame, frame_handler))
|
||||
close_callback = ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_void_p)(partial(dispatch_event, receive_event))
|
||||
datachannel_library.rtcSetFrameCallback(track, frame_callback)
|
||||
datachannel_library.rtcSetClosedCallback(track, close_callback)
|
||||
receive_event.frame_callback = frame_callback # type: ignore[attr-defined]
|
||||
receive_event.close_callback = close_callback # type: ignore[attr-defined]
|
||||
|
||||
return receive_event
|
||||
|
||||
|
||||
def dispatch_frame(frame_handler : FrameHandler, track : int, data : ctypes.c_void_p, size : int, info : ctypes.c_void_p, pointer : ctypes.c_void_p) -> None:
|
||||
frame_buffer = ctypes.string_at(data, size)
|
||||
frame_timestamp = ctypes.cast(info, ctypes.POINTER(ctypes.c_uint32)).contents.value
|
||||
frame_handler(frame_buffer, frame_timestamp)
|
||||
|
||||
|
||||
def dispatch_event(event : threading.Event, track : int, pointer : ctypes.c_void_p) -> None:
|
||||
event.set()
|
||||
@@ -0,0 +1,136 @@
|
||||
import ctypes
|
||||
import threading
|
||||
from collections.abc import AsyncIterator
|
||||
from concurrent.futures import Future, ThreadPoolExecutor
|
||||
from queue import Queue
|
||||
from typing import Optional, Tuple
|
||||
|
||||
import cv2
|
||||
import numpy
|
||||
from starlette.websockets import WebSocket
|
||||
|
||||
from facefusion import rtc, rtc_store, state_manager, streamer
|
||||
from facefusion.apis.stream_audio import receive_audio_frames, run_audio_encode_loop
|
||||
from facefusion.apis.stream_video import receive_video_frames, run_video_encode_loop
|
||||
from facefusion.libraries import datachannel as datachannel_module
|
||||
from facefusion.types import AudioCodec, AudioFrame, BufferPack, PeerConnection, RtcPeer, RtcPeerAudio, SdpAnswer, SdpOffer, SessionId, Time, VideoCodec, VisionFrame
|
||||
|
||||
|
||||
async def process_image(websocket : WebSocket) -> None:
|
||||
capture_vision_frame = await anext(receive_vision_frames(websocket), None)
|
||||
|
||||
if numpy.any(capture_vision_frame):
|
||||
output_vision_frame = streamer.process_stream_frame(capture_vision_frame)
|
||||
is_success, output_frame_buffer = cv2.imencode('.jpg', output_vision_frame)
|
||||
|
||||
if is_success:
|
||||
await websocket.send_bytes(output_frame_buffer.tobytes())
|
||||
|
||||
|
||||
async def receive_vision_frames(websocket : WebSocket) -> AsyncIterator[VisionFrame]:
|
||||
websocket_event = await websocket.receive()
|
||||
|
||||
while websocket_event.get('type') == 'websocket.receive':
|
||||
frame_buffer = websocket_event.get('bytes') or bytes()
|
||||
vision_frame = cv2.imdecode(numpy.frombuffer(frame_buffer, numpy.uint8), cv2.IMREAD_COLOR)
|
||||
|
||||
if numpy.any(vision_frame):
|
||||
yield vision_frame
|
||||
|
||||
websocket_event = await websocket.receive()
|
||||
|
||||
|
||||
def process_video(session_id : SessionId, sdp_offer : SdpOffer) -> Optional[SdpAnswer]:
|
||||
video_codec : VideoCodec = 'vp8'
|
||||
|
||||
if rtc.get_payload_type(sdp_offer, 'vp9'):
|
||||
video_codec = 'vp9'
|
||||
|
||||
if rtc.get_payload_type(sdp_offer, 'av1'):
|
||||
video_codec = 'av1'
|
||||
|
||||
video_payload_type = rtc.get_payload_type(sdp_offer, video_codec)
|
||||
|
||||
if video_payload_type:
|
||||
peer_connection : PeerConnection = rtc.create_peer_connection()
|
||||
video_receiver_track = rtc.add_video_track(peer_connection, 'recvonly', video_codec, video_payload_type)
|
||||
video_sender_track = rtc.add_video_track(peer_connection, 'sendonly', video_codec, video_payload_type)
|
||||
|
||||
sender_bitrate = ctypes.c_uint(0)
|
||||
receiver_bitrate = ctypes.c_uint(8000)
|
||||
rtc.wire_sender_bitrate(video_sender_track, sender_bitrate)
|
||||
|
||||
audio_codec : AudioCodec = 'opus'
|
||||
audio_payload_type = rtc.get_payload_type(sdp_offer, audio_codec)
|
||||
|
||||
if audio_payload_type:
|
||||
audio_receiver_track = rtc.add_audio_track(peer_connection, 'recvonly', audio_codec, audio_payload_type)
|
||||
audio_sender_track = rtc.add_audio_track(peer_connection, 'sendonly', audio_codec, audio_payload_type)
|
||||
|
||||
rtc.set_remote_description(peer_connection, sdp_offer)
|
||||
sdp_answer = rtc.create_sdp_answer(peer_connection)
|
||||
|
||||
if sdp_answer:
|
||||
rtc_peer : RtcPeer =\
|
||||
{
|
||||
'peer_connection': peer_connection,
|
||||
'video':
|
||||
{
|
||||
'sender_track': video_sender_track,
|
||||
'receiver_track': video_receiver_track,
|
||||
'codec': video_codec
|
||||
},
|
||||
'sender_bitrate': sender_bitrate,
|
||||
'receiver_bitrate': receiver_bitrate
|
||||
}
|
||||
|
||||
if audio_payload_type:
|
||||
rtc_peer['audio'] = RtcPeerAudio(
|
||||
sender_track = audio_sender_track,
|
||||
receiver_track = audio_receiver_track,
|
||||
codec = audio_codec
|
||||
)
|
||||
|
||||
rtc_store.init_peers(session_id)
|
||||
rtc_store.get_peers(session_id).append(rtc_peer)
|
||||
|
||||
threading.Thread(target = run_peer_loop, args = (session_id, rtc_peer), daemon = True).start()
|
||||
|
||||
return sdp_answer
|
||||
|
||||
datachannel_module.create_static_library().rtcDeletePeerConnection(peer_connection)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def run_peer_loop(session_id : SessionId, rtc_peer : RtcPeer) -> None:
|
||||
execution_thread_count = state_manager.get_item('execution_thread_count')
|
||||
video_queue : Queue[Tuple[Time, Future[BufferPack]]] = Queue(maxsize = execution_thread_count)
|
||||
audio_queue : Queue[Tuple[Time, AudioFrame]] = Queue(maxsize = execution_thread_count * 10)
|
||||
video_executor = ThreadPoolExecutor(max_workers = execution_thread_count)
|
||||
|
||||
video_receiver_thread = threading.Thread(target = receive_video_frames, args = (rtc_peer.get('video'), video_queue, video_executor), daemon = True)
|
||||
video_encoder_thread = threading.Thread(target = run_video_encode_loop, args = (rtc_peer, video_queue), daemon = True)
|
||||
video_receiver_thread.start()
|
||||
video_encoder_thread.start()
|
||||
|
||||
if rtc_peer.get('audio'):
|
||||
audio_receiver_thread = threading.Thread(target = receive_audio_frames, args = (rtc_peer.get('audio'), audio_queue), daemon = True)
|
||||
audio_encoder_thread = threading.Thread(target = run_audio_encode_loop, args = (rtc_peer, audio_queue), daemon = True)
|
||||
audio_receiver_thread.start()
|
||||
audio_encoder_thread.start()
|
||||
audio_receiver_thread.join()
|
||||
audio_encoder_thread.join()
|
||||
|
||||
video_receiver_thread.join()
|
||||
video_encoder_thread.join()
|
||||
video_executor.shutdown(wait = True)
|
||||
rtc_store.delete_peers(session_id)
|
||||
|
||||
|
||||
def destroy_stream(session_id : SessionId) -> bool:
|
||||
if rtc_store.has_peers(session_id):
|
||||
rtc_store.delete_peers(session_id)
|
||||
return not rtc_store.has_peers(session_id)
|
||||
|
||||
return False
|
||||
@@ -0,0 +1,174 @@
|
||||
from concurrent.futures import Future, ThreadPoolExecutor
|
||||
from functools import partial
|
||||
from queue import Queue
|
||||
from typing import Optional, Tuple
|
||||
|
||||
import cv2
|
||||
import numpy
|
||||
|
||||
from facefusion import rtc, streamer
|
||||
from facefusion.apis.stream_event import create_receive_event
|
||||
from facefusion.codecs import aom_decoder, aom_encoder, vpx_decoder, vpx_encoder
|
||||
from facefusion.types import AomDecoder, AomEncoder, BitRate, Buffer, BufferPack, Resolution, RtcPeer, RtcPeerVideo, Time, VideoCodec, VisionFrame, VpxDecoder, VpxEncoder
|
||||
|
||||
|
||||
def run_video_encode_loop(rtc_peer : RtcPeer, video_queue : Queue[Tuple[Time, Future[BufferPack]]]) -> None:
|
||||
video_codec = rtc_peer.get('video').get('codec')
|
||||
video_time, video_future = video_queue.get()
|
||||
video_pack = video_future.result()
|
||||
video_buffer = video_pack.get('buffer')
|
||||
video_resolution = video_pack.get('resolution')
|
||||
|
||||
if video_buffer:
|
||||
temp_resolution : Resolution = video_resolution
|
||||
temp_bitrate : BitRate = 8000
|
||||
video_encoder = create_video_encoder(video_codec, temp_resolution, temp_bitrate)
|
||||
frame_index = 0
|
||||
|
||||
while video_buffer:
|
||||
sender_bitrate = rtc_peer.get('sender_bitrate').value
|
||||
|
||||
if video_resolution[0] - temp_resolution[0] or video_resolution[1] - temp_resolution[1]:
|
||||
temp_resolution = video_resolution
|
||||
update_video_encoder_resolution(video_codec, video_encoder, temp_resolution)
|
||||
|
||||
if sender_bitrate > 0 and sender_bitrate - temp_bitrate:
|
||||
temp_bitrate = sender_bitrate
|
||||
update_video_encoder_bitrate(video_codec, video_encoder, temp_bitrate)
|
||||
|
||||
__video_buffer__ = encode_video_frame(video_codec, video_encoder, video_buffer, temp_resolution, frame_index)
|
||||
|
||||
if __video_buffer__:
|
||||
video_timestamp = rtc.convert_time_to_timestamp(video_codec, video_time)
|
||||
rtc.send_video(rtc_peer, __video_buffer__, video_timestamp)
|
||||
|
||||
receiver_bitrate = rtc_peer.get('receiver_bitrate').value
|
||||
rtc.adapt_receiver_bitrate(rtc_peer, receiver_bitrate)
|
||||
frame_index += 1
|
||||
|
||||
video_time, video_future = video_queue.get()
|
||||
video_pack = video_future.result()
|
||||
video_buffer = video_pack.get('buffer')
|
||||
video_resolution = video_pack.get('resolution')
|
||||
|
||||
destroy_video_encoder(video_codec, video_encoder)
|
||||
rtc.clear_bitrate(rtc_peer)
|
||||
|
||||
|
||||
def receive_video_frames(rtc_peer_video : RtcPeerVideo, video_queue : Queue[Tuple[Time, Future[BufferPack]]], video_executor : ThreadPoolExecutor) -> None:
|
||||
video_track = rtc_peer_video.get('receiver_track')
|
||||
video_codec = rtc_peer_video.get('codec')
|
||||
video_decoder = create_video_decoder(video_codec)
|
||||
|
||||
video_frame_handler = partial(handle_video_frame, video_codec, video_decoder, video_queue, video_executor)
|
||||
receive_event = create_receive_event(video_track, video_frame_handler)
|
||||
receive_event.wait()
|
||||
|
||||
empty_future : Future[BufferPack] = Future()
|
||||
empty_future.set_result(BufferPack(buffer = bytes(), resolution = (0, 0)))
|
||||
video_queue.put((0.0, empty_future))
|
||||
destroy_video_decoder(video_codec, video_decoder)
|
||||
|
||||
|
||||
def process_video_frame(input_vision_frame : VisionFrame) -> BufferPack:
|
||||
output_vision_frame = streamer.process_stream_frame(input_vision_frame)
|
||||
output_resolution : Resolution = (output_vision_frame.shape[1], output_vision_frame.shape[0])
|
||||
output_buffer = cv2.cvtColor(output_vision_frame, cv2.COLOR_BGR2YUV_I420).tobytes()
|
||||
return BufferPack(buffer = output_buffer, resolution = output_resolution)
|
||||
|
||||
|
||||
def decode_video_frame(video_codec : VideoCodec, video_decoder : VpxDecoder | AomDecoder, input_buffer : Buffer) -> Optional[VisionFrame]:
|
||||
if video_codec == 'av1':
|
||||
aom_pointer = aom_decoder.decode(video_decoder, input_buffer)
|
||||
|
||||
if aom_pointer:
|
||||
return normalize_vision_frame(aom_pointer)
|
||||
|
||||
if video_codec in [ 'vp8', 'vp9' ]:
|
||||
vpx_pointer = vpx_decoder.decode(video_decoder, input_buffer)
|
||||
|
||||
if vpx_pointer:
|
||||
return normalize_vision_frame(vpx_pointer)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def encode_video_frame(video_codec : VideoCodec, video_encoder : VpxEncoder | AomEncoder, input_buffer : Buffer, frame_resolution : Resolution, frame_index : int) -> Buffer:
|
||||
if video_codec == 'av1':
|
||||
return aom_encoder.encode(video_encoder, input_buffer, frame_resolution, frame_index)
|
||||
|
||||
if video_codec in [ 'vp8', 'vp9' ]:
|
||||
return vpx_encoder.encode(video_encoder, input_buffer, frame_resolution, frame_index)
|
||||
|
||||
return bytes()
|
||||
|
||||
|
||||
def normalize_vision_frame(frame_pointer : BufferPack) -> VisionFrame:
|
||||
frame_width, frame_height = frame_pointer.get('resolution')
|
||||
vision_frame = numpy.frombuffer(frame_pointer.get('buffer'), dtype = numpy.uint8).reshape((frame_height * 3 // 2, frame_width))
|
||||
return cv2.cvtColor(vision_frame, cv2.COLOR_YUV2BGR_I420)
|
||||
|
||||
|
||||
def create_video_decoder(video_codec : VideoCodec) -> Optional[VpxDecoder | AomDecoder]:
|
||||
if video_codec == 'av1':
|
||||
return aom_decoder.create(8)
|
||||
|
||||
if video_codec in [ 'vp8', 'vp9' ]:
|
||||
return vpx_decoder.create(video_codec, 8)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def create_video_encoder(video_codec : VideoCodec, frame_resolution : Resolution, bitrate : BitRate) -> Optional[VpxEncoder | AomEncoder]:
|
||||
if video_codec == 'av1':
|
||||
return aom_encoder.create(frame_resolution, bitrate, 8, 10)
|
||||
|
||||
if video_codec in [ 'vp8', 'vp9' ]:
|
||||
return vpx_encoder.create(video_codec, frame_resolution, bitrate, 8, 10)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def destroy_video_decoder(video_codec : VideoCodec, video_decoder : VpxDecoder | AomDecoder) -> None:
|
||||
if video_codec == 'av1':
|
||||
aom_decoder.destroy(video_decoder)
|
||||
|
||||
if video_codec in [ 'vp8', 'vp9' ]:
|
||||
vpx_decoder.destroy(video_decoder)
|
||||
|
||||
|
||||
def destroy_video_encoder(video_codec : VideoCodec, video_encoder : VpxEncoder | AomEncoder) -> None:
|
||||
if video_codec == 'av1':
|
||||
aom_encoder.destroy(video_encoder)
|
||||
|
||||
if video_codec in [ 'vp8', 'vp9' ]:
|
||||
vpx_encoder.destroy(video_encoder)
|
||||
|
||||
|
||||
def update_video_encoder_resolution(video_codec : VideoCodec, video_encoder : VpxEncoder | AomEncoder, frame_resolution : Resolution) -> bool:
|
||||
if video_codec == 'av1':
|
||||
return aom_encoder.update_resolution(video_encoder, frame_resolution)
|
||||
|
||||
if video_codec in [ 'vp8', 'vp9' ]:
|
||||
return vpx_encoder.update_resolution(video_encoder, frame_resolution)
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def update_video_encoder_bitrate(video_codec : VideoCodec, video_encoder : VpxEncoder | AomEncoder, bitrate : BitRate) -> bool:
|
||||
if video_codec == 'av1':
|
||||
return aom_encoder.update_bitrate(video_encoder, bitrate)
|
||||
|
||||
if video_codec in [ 'vp8', 'vp9' ]:
|
||||
return vpx_encoder.update_bitrate(video_encoder, bitrate)
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def handle_video_frame(video_codec : VideoCodec, video_decoder : VpxDecoder | AomDecoder, video_queue : Queue[Tuple[Time, Future[BufferPack]]], video_executor : ThreadPoolExecutor, video_buffer : Buffer, video_timestamp : int) -> None:
|
||||
vision_frame = decode_video_frame(video_codec, video_decoder, video_buffer)
|
||||
|
||||
if numpy.any(vision_frame) and video_queue.qsize() < video_queue.maxsize:
|
||||
video_future = video_executor.submit(process_video_frame, vision_frame)
|
||||
video_time = rtc.convert_timestamp_to_time(video_codec, video_timestamp)
|
||||
video_queue.put((video_time, video_future))
|
||||
@@ -5,12 +5,14 @@ from facefusion.types import AppContext
|
||||
|
||||
|
||||
def detect_app_context() -> AppContext:
|
||||
jobs_path = os.path.join('facefusion', 'jobs')
|
||||
apis_path = os.path.join('facefusion', 'apis')
|
||||
frame = sys._getframe(1)
|
||||
|
||||
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'
|
||||
if os.path.join('facefusion', 'uis') in frame.f_code.co_filename:
|
||||
return 'ui'
|
||||
if apis_path in frame.f_code.co_filename:
|
||||
return 'api'
|
||||
frame = frame.f_back
|
||||
return 'cli'
|
||||
|
||||
@@ -1,66 +1,32 @@
|
||||
from facefusion import state_manager
|
||||
from facefusion.filesystem import get_file_name, is_image, is_video, resolve_file_paths
|
||||
from facefusion.jobs import job_store
|
||||
from facefusion.normalizer import normalize_fps, normalize_padding
|
||||
from typing import Union
|
||||
|
||||
from facefusion.capability_store import get_api_arguments, get_cli_arguments, get_sys_arguments
|
||||
from facefusion.filesystem import get_file_name, is_video, resolve_file_paths
|
||||
from facefusion.normalizer import normalize_fps, normalize_space
|
||||
from facefusion.processors.core import get_processors_modules
|
||||
from facefusion.types import ApplyStateItem, Args
|
||||
from facefusion.vision import create_image_resolutions, create_video_resolutions, detect_image_resolution, detect_video_fps, detect_video_resolution, pack_resolution
|
||||
|
||||
|
||||
def reduce_step_args(args : Args) -> Args:
|
||||
step_args =\
|
||||
{
|
||||
key: args[key] for key in args if key in job_store.get_step_keys()
|
||||
}
|
||||
return step_args
|
||||
|
||||
|
||||
def reduce_job_args(args : Args) -> Args:
|
||||
job_args =\
|
||||
{
|
||||
key: args[key] for key in args if key in job_store.get_job_keys()
|
||||
}
|
||||
return job_args
|
||||
|
||||
|
||||
def collect_step_args() -> Args:
|
||||
step_args =\
|
||||
{
|
||||
key: state_manager.get_item(key) for key in job_store.get_step_keys() #type:ignore[arg-type]
|
||||
}
|
||||
return step_args
|
||||
|
||||
|
||||
def collect_job_args() -> Args:
|
||||
job_args =\
|
||||
{
|
||||
key: state_manager.get_item(key) for key in job_store.get_job_keys() #type:ignore[arg-type]
|
||||
}
|
||||
return job_args
|
||||
from facefusion.processors.types import ProcessorState
|
||||
from facefusion.types import ApplyStateItem, Args, State
|
||||
from facefusion.vision import detect_video_fps
|
||||
|
||||
|
||||
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
|
||||
# general
|
||||
apply_state_item('command', args.get('command'))
|
||||
# paths
|
||||
apply_state_item('workflow_mode', args.get('workflow_mode'))
|
||||
apply_state_item('temp_path', args.get('temp_path'))
|
||||
apply_state_item('jobs_path', args.get('jobs_path'))
|
||||
apply_state_item('source_paths', args.get('source_paths'))
|
||||
apply_state_item('target_path', args.get('target_path'))
|
||||
apply_state_item('output_path', args.get('output_path'))
|
||||
# patterns
|
||||
apply_state_item('source_pattern', args.get('source_pattern'))
|
||||
apply_state_item('target_pattern', args.get('target_pattern'))
|
||||
apply_state_item('output_pattern', args.get('output_pattern'))
|
||||
# face detector
|
||||
apply_state_item('face_detector_model', args.get('face_detector_model'))
|
||||
apply_state_item('face_detector_size', args.get('face_detector_size'))
|
||||
apply_state_item('face_detector_margin', normalize_space(args.get('face_detector_margin')))
|
||||
apply_state_item('face_detector_angles', args.get('face_detector_angles'))
|
||||
apply_state_item('face_detector_score', args.get('face_detector_score'))
|
||||
# face landmarker
|
||||
apply_state_item('face_landmarker_model', args.get('face_landmarker_model'))
|
||||
apply_state_item('face_landmarker_score', args.get('face_landmarker_score'))
|
||||
# face selector
|
||||
apply_state_item('face_selector_mode', args.get('face_selector_mode'))
|
||||
apply_state_item('face_selector_order', args.get('face_selector_order'))
|
||||
apply_state_item('face_selector_age_start', args.get('face_selector_age_start'))
|
||||
@@ -70,71 +36,94 @@ 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_distance', args.get('reference_face_distance'))
|
||||
apply_state_item('reference_frame_number', args.get('reference_frame_number'))
|
||||
# face masker
|
||||
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_parser_model', args.get('face_parser_model'))
|
||||
apply_state_item('face_mask_types', args.get('face_mask_types'))
|
||||
apply_state_item('face_mask_areas', args.get('face_mask_areas'))
|
||||
apply_state_item('face_mask_regions', args.get('face_mask_regions'))
|
||||
apply_state_item('face_mask_blur', args.get('face_mask_blur'))
|
||||
apply_state_item('face_mask_padding', normalize_padding(args.get('face_mask_padding')))
|
||||
# frame extraction
|
||||
apply_state_item('face_mask_padding', normalize_space(args.get('face_mask_padding')))
|
||||
apply_state_item('voice_extractor_model', args.get('voice_extractor_model'))
|
||||
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('temp_frame_format', args.get('temp_frame_format'))
|
||||
apply_state_item('keep_temp', args.get('keep_temp'))
|
||||
# output creation
|
||||
apply_state_item('target_frame_amount', args.get('target_frame_amount'))
|
||||
apply_state_item('output_image_quality', args.get('output_image_quality'))
|
||||
if is_image(args.get('target_path')):
|
||||
output_image_resolution = detect_image_resolution(args.get('target_path'))
|
||||
output_image_resolutions = create_image_resolutions(output_image_resolution)
|
||||
if args.get('output_image_resolution') in output_image_resolutions:
|
||||
apply_state_item('output_image_resolution', args.get('output_image_resolution'))
|
||||
else:
|
||||
apply_state_item('output_image_resolution', pack_resolution(output_image_resolution))
|
||||
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_quality', args.get('output_audio_quality'))
|
||||
apply_state_item('output_audio_volume', args.get('output_audio_volume'))
|
||||
apply_state_item('output_audio_fps', normalize_fps(args.get('output_audio_fps')))
|
||||
apply_state_item('output_video_encoder', args.get('output_video_encoder'))
|
||||
apply_state_item('output_video_preset', args.get('output_video_preset'))
|
||||
apply_state_item('output_video_quality', args.get('output_video_quality'))
|
||||
if is_video(args.get('target_path')):
|
||||
output_video_resolution = detect_video_resolution(args.get('target_path'))
|
||||
output_video_resolutions = create_video_resolutions(output_video_resolution)
|
||||
if args.get('output_video_resolution') in output_video_resolutions:
|
||||
apply_state_item('output_video_resolution', args.get('output_video_resolution'))
|
||||
else:
|
||||
apply_state_item('output_video_resolution', pack_resolution(output_video_resolution))
|
||||
apply_state_item('output_video_scale', args.get('output_video_scale'))
|
||||
|
||||
if args.get('output_video_fps') or is_video(args.get('target_path')):
|
||||
output_video_fps = normalize_fps(args.get('output_video_fps')) or detect_video_fps(args.get('target_path'))
|
||||
apply_state_item('output_video_fps', output_video_fps)
|
||||
# processors
|
||||
|
||||
available_processors = [ get_file_name(file_path) for file_path in resolve_file_paths('facefusion/processors/modules') ]
|
||||
apply_state_item('processors', args.get('processors'))
|
||||
|
||||
for processor_module in get_processors_modules(available_processors):
|
||||
processor_module.apply_args(args, apply_state_item)
|
||||
# uis
|
||||
apply_state_item('open_browser', args.get('open_browser'))
|
||||
apply_state_item('ui_layouts', args.get('ui_layouts'))
|
||||
apply_state_item('ui_workflow', args.get('ui_workflow'))
|
||||
# execution
|
||||
apply_state_item('execution_device_id', args.get('execution_device_id'))
|
||||
|
||||
apply_state_item('execution_device_ids', args.get('execution_device_ids'))
|
||||
apply_state_item('execution_providers', args.get('execution_providers'))
|
||||
apply_state_item('execution_thread_count', args.get('execution_thread_count'))
|
||||
apply_state_item('execution_queue_count', args.get('execution_queue_count'))
|
||||
# download
|
||||
apply_state_item('download_providers', args.get('download_providers'))
|
||||
apply_state_item('download_scope', args.get('download_scope'))
|
||||
# benchmark
|
||||
apply_state_item('benchmark_mode', args.get('benchmark_mode'))
|
||||
apply_state_item('benchmark_resolutions', args.get('benchmark_resolutions'))
|
||||
apply_state_item('benchmark_cycle_count', args.get('benchmark_cycle_count'))
|
||||
# memory
|
||||
apply_state_item('api_host', args.get('api_host'))
|
||||
apply_state_item('api_port', args.get('api_port'))
|
||||
apply_state_item('api_security_strategy', args.get('api_security_strategy'))
|
||||
apply_state_item('video_memory_strategy', args.get('video_memory_strategy'))
|
||||
apply_state_item('system_memory_limit', args.get('system_memory_limit'))
|
||||
# misc
|
||||
apply_state_item('log_level', args.get('log_level'))
|
||||
apply_state_item('halt_on_error', args.get('halt_on_error'))
|
||||
# jobs
|
||||
apply_state_item('job_id', args.get('job_id'))
|
||||
apply_state_item('job_status', args.get('job_status'))
|
||||
apply_state_item('step_index', args.get('step_index'))
|
||||
|
||||
|
||||
def extract_api_args(state : Union[State, ProcessorState]) -> Args:
|
||||
api_args =\
|
||||
{
|
||||
key: state.get(key) for key in state if key in get_api_arguments()
|
||||
}
|
||||
return api_args
|
||||
|
||||
|
||||
def extract_cli_args(state : Union[State, ProcessorState]) -> Args:
|
||||
cli_args =\
|
||||
{
|
||||
key: state.get(key) for key in state if key in get_cli_arguments()
|
||||
}
|
||||
return cli_args
|
||||
|
||||
|
||||
def extract_sys_args(state : Union[State, ProcessorState]) -> Args:
|
||||
sys_args =\
|
||||
{
|
||||
key: state.get(key) for key in state if key in get_sys_arguments()
|
||||
}
|
||||
return sys_args
|
||||
|
||||
|
||||
def extract_step_args(state : Union[State, ProcessorState]) -> Args:
|
||||
step_args =\
|
||||
{
|
||||
key: state.get(key) for key in state if key in get_cli_arguments() and key not in get_sys_arguments()
|
||||
}
|
||||
return step_args
|
||||
|
||||
|
||||
def filter_step_args(args : Args) -> Args:
|
||||
step_args =\
|
||||
{
|
||||
key: args.get(key) for key in args if key in get_cli_arguments() and key not in get_sys_arguments()
|
||||
}
|
||||
return step_args
|
||||
+35
-7
@@ -1,5 +1,6 @@
|
||||
import math
|
||||
from functools import lru_cache
|
||||
from typing import Any, List, Optional
|
||||
from typing import Any, List, Optional, Tuple
|
||||
|
||||
import numpy
|
||||
import scipy
|
||||
@@ -7,11 +8,12 @@ from numpy.typing import NDArray
|
||||
|
||||
from facefusion.ffmpeg import read_audio_buffer
|
||||
from facefusion.filesystem import is_audio
|
||||
from facefusion.types import Audio, AudioFrame, Fps, Mel, MelFilterBank, Spectrogram
|
||||
from facefusion.media_helper import restrict_trim_frame
|
||||
from facefusion.types import Audio, AudioFrame, Duration, Fps, Mel, MelFilterBank, Spectrogram
|
||||
from facefusion.voice_extractor import batch_extract_voice
|
||||
|
||||
|
||||
@lru_cache()
|
||||
@lru_cache(maxsize = 64)
|
||||
def read_static_audio(audio_path : str, fps : Fps) -> Optional[List[AudioFrame]]:
|
||||
return read_audio(audio_path, fps)
|
||||
|
||||
@@ -31,7 +33,7 @@ def read_audio(audio_path : str, fps : Fps) -> Optional[List[AudioFrame]]:
|
||||
return None
|
||||
|
||||
|
||||
@lru_cache()
|
||||
@lru_cache(maxsize = 64)
|
||||
def read_static_voice(audio_path : str, fps : Fps) -> Optional[List[AudioFrame]]:
|
||||
return read_voice(audio_path, fps)
|
||||
|
||||
@@ -118,12 +120,12 @@ def convert_mel_to_hertz(mel : Mel) -> NDArray[Any]:
|
||||
|
||||
def create_mel_filter_bank() -> MelFilterBank:
|
||||
audio_sample_rate = 16000
|
||||
audio_min_frequency = 55.0
|
||||
audio_max_frequency = 7600.0
|
||||
audio_frequency_min = 55.0
|
||||
audio_frequency_max = 7600.0
|
||||
mel_filter_total = 80
|
||||
mel_bin_total = 800
|
||||
mel_filter_bank = numpy.zeros((mel_filter_total, mel_bin_total // 2 + 1))
|
||||
mel_frequency_range = numpy.linspace(convert_hertz_to_mel(audio_min_frequency), convert_hertz_to_mel(audio_max_frequency), mel_filter_total + 2)
|
||||
mel_frequency_range = numpy.linspace(convert_hertz_to_mel(audio_frequency_min), convert_hertz_to_mel(audio_frequency_max), mel_filter_total + 2)
|
||||
indices = numpy.floor((mel_bin_total + 1) * convert_mel_to_hertz(mel_frequency_range) / audio_sample_rate).astype(numpy.int16)
|
||||
|
||||
for index in range(mel_filter_total):
|
||||
@@ -141,3 +143,29 @@ def create_spectrogram(audio : Audio) -> Spectrogram:
|
||||
spectrogram = scipy.signal.stft(audio, nperseg = mel_bin_total, nfft = mel_bin_total, noverlap = mel_bin_overlap)[2]
|
||||
spectrogram = numpy.dot(mel_filter_bank, numpy.abs(spectrogram))
|
||||
return spectrogram
|
||||
|
||||
|
||||
def count_audio_frame_total(audio_path : str, fps : Fps) -> int:
|
||||
audio_duration = detect_audio_duration(audio_path)
|
||||
if audio_duration > 0:
|
||||
return math.ceil(audio_duration * fps)
|
||||
return 0
|
||||
|
||||
|
||||
def detect_audio_duration(audio_path : str) -> Duration:
|
||||
audio_sample_rate = 48000
|
||||
audio_sample_size = 16
|
||||
audio_channel_total = 2
|
||||
|
||||
if is_audio(audio_path):
|
||||
audio_buffer = read_audio_buffer(audio_path, audio_sample_rate, audio_sample_size, audio_channel_total)
|
||||
if audio_buffer:
|
||||
audio = numpy.frombuffer(audio_buffer, dtype = numpy.int16).reshape(-1, audio_channel_total)
|
||||
audio_duration = len(audio) / audio_sample_rate
|
||||
return audio_duration
|
||||
return 0
|
||||
|
||||
|
||||
def restrict_trim_audio_frame(audio_path : str, fps : Fps, trim_frame_start : Optional[int], trim_frame_end : Optional[int]) -> Tuple[int, int]:
|
||||
audio_frame_total = count_audio_frame_total(audio_path, fps)
|
||||
return restrict_trim_frame(audio_frame_total, trim_frame_start, trim_frame_end)
|
||||
|
||||
+17
-12
@@ -3,15 +3,16 @@ import os
|
||||
import statistics
|
||||
import tempfile
|
||||
from time import perf_counter
|
||||
from typing import Generator, List
|
||||
from typing import Iterator, List
|
||||
|
||||
import facefusion.choices
|
||||
from facefusion import core, state_manager
|
||||
from facefusion import content_analyser, core, state_manager
|
||||
from facefusion.cli_helper import render_table
|
||||
from facefusion.download import conditional_download, resolve_download_url
|
||||
from facefusion.face_store import clear_faces
|
||||
from facefusion.filesystem import get_file_extension
|
||||
from facefusion.types import BenchmarkCycleSet
|
||||
from facefusion.vision import count_video_frame_total, detect_video_fps, detect_video_resolution, pack_resolution
|
||||
from facefusion.vision import count_video_frame_total, detect_video_fps
|
||||
|
||||
|
||||
def pre_check() -> bool:
|
||||
@@ -30,7 +31,7 @@ def pre_check() -> bool:
|
||||
return True
|
||||
|
||||
|
||||
def run() -> Generator[List[BenchmarkCycleSet], None, None]:
|
||||
def run() -> Iterator[List[BenchmarkCycleSet]]:
|
||||
benchmark_resolutions = state_manager.get_item('benchmark_resolutions')
|
||||
benchmark_cycle_count = state_manager.get_item('benchmark_cycle_count')
|
||||
|
||||
@@ -42,11 +43,11 @@ def run() -> Generator[List[BenchmarkCycleSet], None, None]:
|
||||
state_manager.init_item('video_memory_strategy', 'tolerant')
|
||||
|
||||
benchmarks = []
|
||||
target_paths = [facefusion.choices.benchmark_set.get(benchmark_resolution) for benchmark_resolution in benchmark_resolutions if benchmark_resolution in facefusion.choices.benchmark_set]
|
||||
target_paths = [ facefusion.choices.benchmark_set.get(benchmark_resolution) for benchmark_resolution in benchmark_resolutions if benchmark_resolution in facefusion.choices.benchmark_set ]
|
||||
|
||||
for target_path in target_paths:
|
||||
state_manager.set_item('target_path', target_path)
|
||||
state_manager.set_item('output_path', suggest_output_path(state_manager.get_item('target_path')))
|
||||
state_manager.init_item('target_path', target_path)
|
||||
state_manager.init_item('output_path', suggest_output_path(state_manager.get_item('target_path')))
|
||||
benchmarks.append(cycle(benchmark_cycle_count))
|
||||
yield benchmarks
|
||||
|
||||
@@ -54,13 +55,17 @@ def run() -> Generator[List[BenchmarkCycleSet], None, None]:
|
||||
def cycle(cycle_count : int) -> BenchmarkCycleSet:
|
||||
process_times = []
|
||||
video_frame_total = count_video_frame_total(state_manager.get_item('target_path'))
|
||||
output_video_resolution = detect_video_resolution(state_manager.get_item('target_path'))
|
||||
state_manager.set_item('output_video_resolution', pack_resolution(output_video_resolution))
|
||||
state_manager.set_item('output_video_fps', detect_video_fps(state_manager.get_item('target_path')))
|
||||
state_manager.init_item('output_video_fps', detect_video_fps(state_manager.get_item('target_path')))
|
||||
|
||||
core.conditional_process()
|
||||
if state_manager.get_item('benchmark_mode') == 'warm':
|
||||
core.conditional_process()
|
||||
|
||||
for index in range(cycle_count):
|
||||
if state_manager.get_item('benchmark_mode') == 'cold':
|
||||
content_analyser.analyse_image.cache_clear()
|
||||
content_analyser.analyse_video.cache_clear()
|
||||
clear_faces()
|
||||
|
||||
start_time = perf_counter()
|
||||
core.conditional_process()
|
||||
end_time = perf_counter()
|
||||
@@ -84,7 +89,7 @@ def cycle(cycle_count : int) -> BenchmarkCycleSet:
|
||||
|
||||
def suggest_output_path(target_path : str) -> str:
|
||||
target_file_extension = get_file_extension(target_path)
|
||||
return os.path.join(tempfile.gettempdir(), hashlib.sha1().hexdigest()[:8] + target_file_extension)
|
||||
return os.path.join(tempfile.gettempdir(), hashlib.sha1(target_path.encode()).hexdigest() + target_file_extension)
|
||||
|
||||
|
||||
def render() -> None:
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
from typing import List
|
||||
|
||||
import cv2
|
||||
|
||||
from facefusion.types import CameraPoolSet
|
||||
|
||||
CAMERA_POOL_SET : CameraPoolSet =\
|
||||
{
|
||||
'capture': {}
|
||||
}
|
||||
|
||||
|
||||
def get_local_camera_capture(camera_id : int) -> cv2.VideoCapture:
|
||||
camera_key = str(camera_id)
|
||||
|
||||
if camera_key not in CAMERA_POOL_SET.get('capture'):
|
||||
camera_capture = cv2.VideoCapture(camera_id)
|
||||
|
||||
if camera_capture.isOpened():
|
||||
CAMERA_POOL_SET['capture'][camera_key] = camera_capture
|
||||
|
||||
return CAMERA_POOL_SET.get('capture').get(camera_key)
|
||||
|
||||
|
||||
def get_remote_camera_capture(camera_url : str) -> cv2.VideoCapture:
|
||||
if camera_url not in CAMERA_POOL_SET.get('capture'):
|
||||
camera_capture = cv2.VideoCapture(camera_url)
|
||||
|
||||
if camera_capture.isOpened():
|
||||
CAMERA_POOL_SET['capture'][camera_url] = camera_capture
|
||||
|
||||
return CAMERA_POOL_SET.get('capture').get(camera_url)
|
||||
|
||||
|
||||
def clear_camera_pool() -> None:
|
||||
for camera_capture in CAMERA_POOL_SET.get('capture').values():
|
||||
camera_capture.release()
|
||||
|
||||
CAMERA_POOL_SET['capture'].clear()
|
||||
|
||||
|
||||
def detect_local_camera_ids(id_start : int, id_end : int) -> List[int]:
|
||||
local_camera_ids = []
|
||||
|
||||
for camera_id in range(id_start, id_end):
|
||||
cv2.utils.logging.setLogLevel(0)
|
||||
camera_capture = get_local_camera_capture(camera_id)
|
||||
cv2.utils.logging.setLogLevel(3)
|
||||
|
||||
if camera_capture and camera_capture.isOpened():
|
||||
local_camera_ids.append(camera_id)
|
||||
|
||||
return local_camera_ids
|
||||
@@ -0,0 +1,54 @@
|
||||
from argparse import Action
|
||||
from typing import Dict, List
|
||||
|
||||
from facefusion.types import CapabilitySet, CapabilityStore, Scope
|
||||
|
||||
CAPABILITY_STORE : CapabilityStore =\
|
||||
{
|
||||
'api': {},
|
||||
'cli': {},
|
||||
'sys': {}
|
||||
}
|
||||
|
||||
|
||||
def get_api_capability_set() -> Dict[str, CapabilitySet]:
|
||||
return CAPABILITY_STORE.get('api')
|
||||
|
||||
|
||||
def get_cli_capability_set() -> Dict[str, CapabilitySet]:
|
||||
return CAPABILITY_STORE.get('cli')
|
||||
|
||||
|
||||
def get_sys_capability_set() -> Dict[str, CapabilitySet]:
|
||||
return CAPABILITY_STORE.get('sys')
|
||||
|
||||
|
||||
def get_api_arguments() -> List[str]:
|
||||
return list(get_api_capability_set().keys())
|
||||
|
||||
|
||||
def get_cli_arguments() -> List[str]:
|
||||
return list(get_cli_capability_set().keys())
|
||||
|
||||
|
||||
def get_sys_arguments() -> List[str]:
|
||||
return list(get_sys_capability_set().keys())
|
||||
|
||||
|
||||
def register_capability_set(actions : List[Action], scopes : List[Scope]) -> None:
|
||||
for action in actions:
|
||||
value : CapabilitySet =\
|
||||
{
|
||||
'default': action.default
|
||||
}
|
||||
|
||||
if action.choices:
|
||||
value['choices'] = list(action.choices)
|
||||
|
||||
for scope in scopes:
|
||||
if scope == 'api':
|
||||
CAPABILITY_STORE['api'][action.dest] = value
|
||||
if scope == 'cli':
|
||||
CAPABILITY_STORE['cli'][action.dest] = value
|
||||
if scope == 'sys':
|
||||
CAPABILITY_STORE['sys'][action.dest] = value
|
||||
+73
-67
@@ -1,25 +1,28 @@
|
||||
import logging
|
||||
from typing import List, Sequence
|
||||
from typing import List, Sequence, get_args
|
||||
|
||||
from facefusion.common_helper import create_float_range, create_int_range
|
||||
from facefusion.types import Angle, AudioEncoder, AudioFormat, AudioTypeSet, 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, WebcamMode
|
||||
from facefusion.types import Angle, ApiSecurityStrategy, AudioEncoder, AudioFormat, AudioSet, BenchmarkMode, BenchmarkResolution, BenchmarkSet, DownloadProvider, DownloadProviderSet, DownloadScope, ExecutionProvider, ExecutionProviderSet, FaceDetectorModel, FaceDetectorSet, FaceLandmarkerModel, FaceMaskArea, FaceMaskAreaSet, FaceMaskRegion, FaceMaskRegionSet, FaceMaskType, FaceOccluderModel, FaceParserModel, FaceSelectorGender, FaceSelectorMode, FaceSelectorOrder, FaceSelectorRace, Gender, ImageEncoder, ImageFormat, ImageSet, JobStatus, LogLevel, LogLevelSet, Race, Score, TempFrameFormat, VideoEncoder, VideoFormat, VideoMemoryStrategy, VideoPreset, VideoSet, VoiceExtractorModel, WorkflowMode
|
||||
|
||||
face_detector_set : FaceDetectorSet =\
|
||||
{
|
||||
'many': [ '640x640' ],
|
||||
'retinaface': [ '160x160', '320x320', '480x480', '512x512', '640x640' ],
|
||||
'scrfd': [ '160x160', '320x320', '480x480', '512x512', '640x640' ],
|
||||
'yolo_face': [ '640x640' ]
|
||||
'yolo_face': [ '640x640' ],
|
||||
'yunet': [ '640x640' ]
|
||||
}
|
||||
face_detector_models : List[FaceDetectorModel] = list(face_detector_set.keys())
|
||||
face_landmarker_models : List[FaceLandmarkerModel] = [ 'many', '2dfan4', 'peppa_wutz' ]
|
||||
face_selector_modes : List[FaceSelectorMode] = [ 'many', 'one', 'reference' ]
|
||||
face_selector_orders : List[FaceSelectorOrder] = [ 'left-right', 'right-left', 'top-bottom', 'bottom-top', 'small-large', 'large-small', 'best-worst', 'worst-best' ]
|
||||
face_selector_genders : List[Gender] = [ 'female', 'male' ]
|
||||
face_selector_races : List[Race] = [ 'white', 'black', 'latino', 'asian', 'indian', 'arabic' ]
|
||||
face_occluder_models : List[FaceOccluderModel] = [ 'xseg_1', 'xseg_2', 'xseg_3' ]
|
||||
face_parser_models : List[FaceParserModel] = [ 'bisenet_resnet_18', 'bisenet_resnet_34' ]
|
||||
face_mask_types : List[FaceMaskType] = [ 'box', 'occlusion', 'area', 'region' ]
|
||||
face_detector_models : List[FaceDetectorModel] = list(get_args(FaceDetectorModel))
|
||||
face_landmarker_models : List[FaceLandmarkerModel] = list(get_args(FaceLandmarkerModel))
|
||||
face_selector_modes : List[FaceSelectorMode] = list(get_args(FaceSelectorMode))
|
||||
face_selector_orders : List[FaceSelectorOrder] = list(get_args(FaceSelectorOrder))
|
||||
genders : List[Gender] = list(get_args(Gender))
|
||||
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_parser_models : List[FaceParserModel] = list(get_args(FaceParserModel))
|
||||
face_mask_types : List[FaceMaskType] = list(get_args(FaceMaskType))
|
||||
face_mask_area_set : FaceMaskAreaSet =\
|
||||
{
|
||||
'upper-face': [ 0, 1, 2, 31, 32, 33, 34, 35, 14, 15, 16, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17 ],
|
||||
@@ -39,52 +42,53 @@ face_mask_region_set : FaceMaskRegionSet =\
|
||||
'upper-lip': 12,
|
||||
'lower-lip': 13
|
||||
}
|
||||
face_mask_areas : List[FaceMaskArea] = list(face_mask_area_set.keys())
|
||||
face_mask_regions : List[FaceMaskRegion] = list(face_mask_region_set.keys())
|
||||
face_mask_areas : List[FaceMaskArea] = list(get_args(FaceMaskArea))
|
||||
face_mask_regions : List[FaceMaskRegion] = list(get_args(FaceMaskRegion))
|
||||
|
||||
audio_type_set : AudioTypeSet =\
|
||||
{
|
||||
'flac': 'audio/flac',
|
||||
'm4a': 'audio/mp4',
|
||||
'mp3': 'audio/mpeg',
|
||||
'ogg': 'audio/ogg',
|
||||
'opus': 'audio/opus',
|
||||
'wav': 'audio/x-wav'
|
||||
}
|
||||
image_type_set : ImageTypeSet =\
|
||||
{
|
||||
'bmp': 'image/bmp',
|
||||
'jpeg': 'image/jpeg',
|
||||
'png': 'image/png',
|
||||
'tiff': 'image/tiff',
|
||||
'webp': 'image/webp'
|
||||
}
|
||||
video_type_set : VideoTypeSet =\
|
||||
{
|
||||
'avi': 'video/x-msvideo',
|
||||
'm4v': 'video/mp4',
|
||||
'mkv': 'video/x-matroska',
|
||||
'mp4': 'video/mp4',
|
||||
'mov': 'video/quicktime',
|
||||
'webm': 'video/webm'
|
||||
}
|
||||
audio_formats : List[AudioFormat] = list(audio_type_set.keys())
|
||||
image_formats : List[ImageFormat] = list(image_type_set.keys())
|
||||
video_formats : List[VideoFormat] = list(video_type_set.keys())
|
||||
temp_frame_formats : List[TempFrameFormat] = [ 'bmp', 'jpeg', 'png', 'tiff' ]
|
||||
voice_extractor_models : List[VoiceExtractorModel] = list(get_args(VoiceExtractorModel))
|
||||
|
||||
output_encoder_set : EncoderSet =\
|
||||
workflow_modes : List[WorkflowMode] = [ 'auto', 'audio-to-image:frames', 'audio-to-image:video', 'image-to-image', 'image-to-video', 'image-to-video:frames' ]
|
||||
|
||||
audio_set : AudioSet =\
|
||||
{
|
||||
'audio': [ 'flac', 'aac', 'libmp3lame', 'libopus', 'libvorbis', 'pcm_s16le', 'pcm_s32le' ],
|
||||
'video': [ 'libx264', 'libx265', 'libvpx-vp9', 'h264_nvenc', 'hevc_nvenc', 'h264_amf', 'hevc_amf', 'h264_qsv', 'hevc_qsv', 'h264_videotoolbox', 'hevc_videotoolbox', 'rawvideo' ]
|
||||
'flac': 'flac',
|
||||
'm4a': 'aac',
|
||||
'mp3': 'libmp3lame',
|
||||
'ogg': 'flac',
|
||||
'opus': 'libopus',
|
||||
'wav': 'pcm_s16le'
|
||||
}
|
||||
output_audio_encoders : List[AudioEncoder] = output_encoder_set.get('audio')
|
||||
output_video_encoders : List[VideoEncoder] = output_encoder_set.get('video')
|
||||
output_video_presets : List[VideoPreset] = [ 'ultrafast', 'superfast', 'veryfast', 'faster', 'fast', 'medium', 'slow', 'slower', 'veryslow' ]
|
||||
image_set : ImageSet =\
|
||||
{
|
||||
'bmp': 'bmp',
|
||||
'jpeg': 'mjpeg',
|
||||
'png': 'png',
|
||||
'tiff': 'tiff',
|
||||
'webp': 'libwebp'
|
||||
}
|
||||
video_set : VideoSet =\
|
||||
{
|
||||
'avi': 'mpeg4',
|
||||
'm4v': 'libx264',
|
||||
'mkv': 'libx264',
|
||||
'mov': 'libx264',
|
||||
'mp4': 'libx264',
|
||||
'mpeg': 'mpeg1video',
|
||||
'mxf': 'mpeg2video',
|
||||
'webm': 'libvpx-vp9',
|
||||
'wmv': 'msmpeg4'
|
||||
}
|
||||
audio_formats : List[AudioFormat] = list(get_args(AudioFormat))
|
||||
image_formats : List[ImageFormat] = list(get_args(ImageFormat))
|
||||
video_formats : List[VideoFormat] = list(get_args(VideoFormat))
|
||||
temp_frame_formats : List[TempFrameFormat] = list(get_args(TempFrameFormat))
|
||||
|
||||
image_template_sizes : List[float] = [ 0.25, 0.5, 0.75, 1, 1.5, 2, 2.5, 3, 3.5, 4 ]
|
||||
video_template_sizes : List[int] = [ 240, 360, 480, 540, 720, 1080, 1440, 2160, 4320 ]
|
||||
audio_encoders : List[AudioEncoder] = list(get_args(AudioEncoder))
|
||||
image_encoders : List[ImageEncoder] = list(get_args(ImageEncoder))
|
||||
video_encoders : List[VideoEncoder] = list(get_args(VideoEncoder))
|
||||
video_presets : List[VideoPreset] = list(get_args(VideoPreset))
|
||||
|
||||
benchmark_modes : List[BenchmarkMode] = list(get_args(BenchmarkMode))
|
||||
benchmark_set : BenchmarkSet =\
|
||||
{
|
||||
'240p': '.assets/examples/target-240p.mp4',
|
||||
@@ -95,22 +99,21 @@ benchmark_set : BenchmarkSet =\
|
||||
'1440p': '.assets/examples/target-1440p.mp4',
|
||||
'2160p': '.assets/examples/target-2160p.mp4'
|
||||
}
|
||||
benchmark_resolutions : List[BenchmarkResolution] = list(benchmark_set.keys())
|
||||
|
||||
webcam_modes : List[WebcamMode] = [ 'inline', 'udp', 'v4l2' ]
|
||||
webcam_resolutions : List[str] = [ '320x240', '640x480', '800x600', '1024x768', '1280x720', '1280x960', '1920x1080', '2560x1440', '3840x2160' ]
|
||||
benchmark_resolutions : List[BenchmarkResolution] = list(get_args(BenchmarkResolution))
|
||||
|
||||
execution_provider_set : ExecutionProviderSet =\
|
||||
{
|
||||
'cuda': 'CUDAExecutionProvider',
|
||||
'tensorrt': 'TensorrtExecutionProvider',
|
||||
'directml': 'DmlExecutionProvider',
|
||||
'rocm': 'ROCMExecutionProvider',
|
||||
'openvino': 'OpenVINOExecutionProvider',
|
||||
'migraphx': 'MIGraphXExecutionProvider',
|
||||
'coreml': 'CoreMLExecutionProvider',
|
||||
'openvino': 'OpenVINOExecutionProvider',
|
||||
'qnn': 'QNNExecutionProvider',
|
||||
'directml': 'DmlExecutionProvider',
|
||||
'cpu': 'CPUExecutionProvider'
|
||||
}
|
||||
execution_providers : List[ExecutionProvider] = list(execution_provider_set.keys())
|
||||
execution_providers : List[ExecutionProvider] = list(get_args(ExecutionProvider))
|
||||
download_provider_set : DownloadProviderSet =\
|
||||
{
|
||||
'github':
|
||||
@@ -131,10 +134,11 @@ download_provider_set : DownloadProviderSet =\
|
||||
'path': '/facefusion/{base_name}/resolve/main/{file_name}'
|
||||
}
|
||||
}
|
||||
download_providers : List[DownloadProvider] = list(download_provider_set.keys())
|
||||
download_scopes : List[DownloadScope] = [ 'lite', 'full' ]
|
||||
download_providers : List[DownloadProvider] = list(get_args(DownloadProvider))
|
||||
download_scopes : List[DownloadScope] = list(get_args(DownloadScope))
|
||||
|
||||
video_memory_strategies : List[VideoMemoryStrategy] = [ 'strict', 'moderate', 'tolerant' ]
|
||||
video_memory_strategies : List[VideoMemoryStrategy] = list(get_args(VideoMemoryStrategy))
|
||||
api_security_strategies : List[ApiSecurityStrategy] = list(get_args(ApiSecurityStrategy))
|
||||
|
||||
log_level_set : LogLevelSet =\
|
||||
{
|
||||
@@ -143,15 +147,13 @@ log_level_set : LogLevelSet =\
|
||||
'info': logging.INFO,
|
||||
'debug': logging.DEBUG
|
||||
}
|
||||
log_levels : List[LogLevel] = list(log_level_set.keys())
|
||||
log_levels : List[LogLevel] = list(get_args(LogLevel))
|
||||
|
||||
ui_workflows : List[UiWorkflow] = [ 'instant_runner', 'job_runner', 'job_manager' ]
|
||||
job_statuses : List[JobStatus] = [ 'drafted', 'queued', 'completed', 'failed' ]
|
||||
job_statuses : List[JobStatus] = list(get_args(JobStatus))
|
||||
|
||||
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_queue_count_range : Sequence[int] = create_int_range(1, 4, 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_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_landmarker_score_range : Sequence[Score] = create_float_range(0.0, 1.0, 0.05)
|
||||
@@ -159,7 +161,11 @@ 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_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)
|
||||
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_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_volume_range : Sequence[int] = create_int_range(0, 100, 1)
|
||||
output_video_quality_range : Sequence[int] = create_int_range(0, 100, 1)
|
||||
output_video_scale_range : Sequence[float] = create_float_range(0.25, 8.0, 0.25)
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
from typing import Tuple
|
||||
from typing import List, Tuple
|
||||
|
||||
from facefusion.logger import get_package_logger
|
||||
from facefusion.types import TableContents, TableHeaders
|
||||
from facefusion.types import TableContent, TableHeader
|
||||
|
||||
|
||||
def render_table(headers : TableHeaders, contents : TableContents) -> None:
|
||||
def render_table(headers : List[TableHeader], contents : List[List[TableContent]]) -> None:
|
||||
package_logger = get_package_logger()
|
||||
table_column, table_separator = create_table_parts(headers, contents)
|
||||
|
||||
@@ -19,7 +19,7 @@ def render_table(headers : TableHeaders, contents : TableContents) -> None:
|
||||
package_logger.critical(table_separator)
|
||||
|
||||
|
||||
def create_table_parts(headers : TableHeaders, contents : TableContents) -> Tuple[str, str]:
|
||||
def create_table_parts(headers : List[TableHeader], contents : List[List[TableContent]]) -> Tuple[str, str]:
|
||||
column_parts = []
|
||||
separator_parts = []
|
||||
widths = [ len(header) for header in headers ]
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
import ctypes
|
||||
import struct
|
||||
from typing import Optional
|
||||
|
||||
from facefusion.libraries import aom as aom_module
|
||||
from facefusion.types import AomDecoder, Buffer, BufferPack
|
||||
|
||||
|
||||
def create(thread_count : int) -> Optional[AomDecoder]:
|
||||
aom_library = aom_module.create_static_library()
|
||||
|
||||
if aom_library:
|
||||
aom_decoder = ctypes.create_string_buffer(128)
|
||||
aom_codec = ctypes.c_void_p.in_dll(aom_library, 'aom_codec_av1_dx_algo')
|
||||
config_buffer = ctypes.create_string_buffer(128)
|
||||
|
||||
struct.pack_into('I', config_buffer, 0, thread_count)
|
||||
struct.pack_into('I', config_buffer, 12, 1)
|
||||
|
||||
if aom_library.aom_codec_dec_init_ver(aom_decoder, ctypes.byref(aom_codec), config_buffer, 0, 22) == 0:
|
||||
return aom_decoder
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def decode(aom_decoder : AomDecoder, input_buffer : Buffer) -> Optional[BufferPack]:
|
||||
aom_library = aom_module.create_static_library()
|
||||
|
||||
if aom_library and input_buffer:
|
||||
input_total = len(input_buffer)
|
||||
temp_buffer = ctypes.create_string_buffer(input_buffer)
|
||||
|
||||
if aom_library.aom_codec_decode(aom_decoder, temp_buffer, input_total, None) == 0:
|
||||
address = aom_library.aom_codec_get_frame(aom_decoder, ctypes.byref(ctypes.c_void_p(0)))
|
||||
|
||||
if address:
|
||||
frame_width = ctypes.c_uint.from_address(address + 28).value & ~1
|
||||
frame_height = ctypes.c_uint.from_address(address + 32).value & ~1
|
||||
|
||||
return BufferPack(
|
||||
buffer = collect(address, frame_width, frame_height),
|
||||
resolution = (frame_width, frame_height)
|
||||
)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def collect(address : int, frame_width : int, frame_height : int) -> Buffer:
|
||||
output_parts = []
|
||||
|
||||
for index in range(3):
|
||||
plane_pointer = ctypes.c_void_p.from_address(address + 64 + index * 8).value
|
||||
stride = ctypes.c_int.from_address(address + 88 + index * 4).value
|
||||
plane_width = frame_width >> (index > 0)
|
||||
plane_height = frame_height >> (index > 0)
|
||||
|
||||
if stride == plane_width:
|
||||
output_parts.append(ctypes.string_at(plane_pointer, plane_width * plane_height))
|
||||
else:
|
||||
for row in range(plane_height):
|
||||
output_parts.append(ctypes.string_at(plane_pointer + row * stride, plane_width))
|
||||
|
||||
return bytes().join(output_parts)
|
||||
|
||||
|
||||
def destroy(aom_decoder : AomDecoder) -> None:
|
||||
aom_library = aom_module.create_static_library()
|
||||
|
||||
if aom_library:
|
||||
aom_library.aom_codec_destroy(aom_decoder)
|
||||
@@ -0,0 +1,94 @@
|
||||
import ctypes
|
||||
import struct
|
||||
from typing import Optional
|
||||
|
||||
from facefusion.libraries import aom as aom_module
|
||||
from facefusion.types import AomEncoder, BitRate, Buffer, Resolution
|
||||
|
||||
|
||||
def create(frame_resolution : Resolution, bitrate : BitRate, thread_count : int, cpu_count : int) -> Optional[AomEncoder]:
|
||||
aom_library = aom_module.create_static_library()
|
||||
|
||||
if aom_library:
|
||||
aom_encoder = ctypes.create_string_buffer(1152)
|
||||
aom_codec = ctypes.c_void_p.in_dll(aom_library, 'aom_codec_av1_cx_algo')
|
||||
|
||||
config_buffer = ctypes.create_string_buffer(1024)
|
||||
|
||||
if aom_library.aom_codec_enc_config_default(ctypes.byref(aom_codec), config_buffer, 1) == 0:
|
||||
struct.pack_into('I', config_buffer, 4, thread_count)
|
||||
struct.pack_into('I', config_buffer, 12, frame_resolution[0])
|
||||
struct.pack_into('I', config_buffer, 16, frame_resolution[1])
|
||||
struct.pack_into('I', config_buffer, 136, bitrate)
|
||||
struct.pack_into('I', config_buffer, 192, 30)
|
||||
|
||||
if aom_library.aom_codec_enc_init_ver(aom_encoder, ctypes.byref(aom_codec), config_buffer, 0, 25) == 0:
|
||||
aom_library.aom_codec_control(aom_encoder, 13, ctypes.c_int(cpu_count))
|
||||
aom_library.aom_codec_control(aom_encoder, 75, ctypes.c_int(1))
|
||||
aom_library.aom_codec_control(aom_encoder, 106, ctypes.c_int(0))
|
||||
aom_library.aom_codec_control(aom_encoder, 122, ctypes.c_int(0))
|
||||
aom_library.aom_codec_control(aom_encoder, 123, ctypes.c_int(0))
|
||||
ctypes.memmove(ctypes.addressof(aom_encoder) + 128, config_buffer, 1024)
|
||||
return aom_encoder
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def encode(aom_encoder : AomEncoder, input_buffer : Buffer, frame_resolution : Resolution, frame_index : int) -> Buffer:
|
||||
aom_library = aom_module.create_static_library()
|
||||
output_buffer = bytes()
|
||||
|
||||
if aom_library:
|
||||
temp_buffer = ctypes.create_string_buffer(256)
|
||||
encode_buffer = ctypes.create_string_buffer(input_buffer)
|
||||
|
||||
if aom_library.aom_img_wrap(temp_buffer, 0x102, frame_resolution[0], frame_resolution[1], 1, encode_buffer) and aom_library.aom_codec_encode(aom_encoder, temp_buffer, frame_index, 1, 0, 1) == 0:
|
||||
output_buffer = collect(aom_encoder)
|
||||
|
||||
return output_buffer
|
||||
|
||||
|
||||
def collect(aom_encoder : AomEncoder) -> Buffer:
|
||||
aom_library = aom_module.create_static_library()
|
||||
output_parts = []
|
||||
|
||||
packet_cursor = ctypes.c_void_p(0)
|
||||
packet = aom_library.aom_codec_get_cx_data(aom_encoder, ctypes.byref(packet_cursor))
|
||||
|
||||
while packet:
|
||||
if ctypes.c_int.from_address(packet).value == 0:
|
||||
buffer_pointer = ctypes.c_void_p.from_address(packet + 8).value
|
||||
buffer_size = ctypes.c_size_t.from_address(packet + 16).value
|
||||
output_parts.append(ctypes.string_at(buffer_pointer, buffer_size))
|
||||
|
||||
packet = aom_library.aom_codec_get_cx_data(aom_encoder, ctypes.byref(packet_cursor))
|
||||
|
||||
return bytes().join(output_parts)
|
||||
|
||||
|
||||
def update_resolution(aom_encoder : AomEncoder, frame_resolution : Resolution) -> bool:
|
||||
aom_library = aom_module.create_static_library()
|
||||
|
||||
if aom_library:
|
||||
struct.pack_into('I', aom_encoder, 128 + 12, frame_resolution[0])
|
||||
struct.pack_into('I', aom_encoder, 128 + 16, frame_resolution[1])
|
||||
return aom_library.aom_codec_enc_config_set(aom_encoder, ctypes.cast(ctypes.addressof(aom_encoder) + 128, ctypes.c_void_p)) == 0
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def update_bitrate(aom_encoder : AomEncoder, bitrate : BitRate) -> bool:
|
||||
aom_library = aom_module.create_static_library()
|
||||
|
||||
if aom_library:
|
||||
struct.pack_into('I', aom_encoder, 128 + 136, bitrate)
|
||||
return aom_library.aom_codec_enc_config_set(aom_encoder, ctypes.cast(ctypes.addressof(aom_encoder) + 128, ctypes.c_void_p)) == 0
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def destroy(aom_encoder : AomEncoder) -> None:
|
||||
aom_library = aom_module.create_static_library()
|
||||
|
||||
if aom_library:
|
||||
aom_library.aom_codec_destroy(aom_encoder)
|
||||
@@ -0,0 +1,38 @@
|
||||
import ctypes
|
||||
from typing import Optional
|
||||
|
||||
from facefusion.libraries import opus as opus_module
|
||||
from facefusion.types import Buffer, OpusDecoder
|
||||
|
||||
|
||||
def create(sample_rate : int, channel_total : int) -> Optional[OpusDecoder]:
|
||||
opus_library = opus_module.create_static_library()
|
||||
|
||||
if opus_library:
|
||||
return opus_library.opus_decoder_create(sample_rate, channel_total, ctypes.byref(ctypes.c_int(0)))
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def decode(opus_decoder : OpusDecoder, input_buffer : Buffer, channel_total : int) -> Buffer:
|
||||
opus_library = opus_module.create_static_library()
|
||||
output_buffer = bytes()
|
||||
|
||||
if opus_library:
|
||||
input_total = len(input_buffer)
|
||||
sample_size = ctypes.sizeof(ctypes.c_float)
|
||||
sample_total = opus_library.opus_decoder_get_nb_samples(opus_decoder, input_buffer, input_total)
|
||||
sample_buffer = (ctypes.c_float * (sample_total * channel_total))()
|
||||
output_total = opus_library.opus_decode_float(opus_decoder, input_buffer, input_total, sample_buffer, sample_total, 0)
|
||||
|
||||
if output_total:
|
||||
output_buffer = ctypes.string_at(ctypes.addressof(sample_buffer), output_total * channel_total * sample_size)
|
||||
|
||||
return output_buffer
|
||||
|
||||
|
||||
def destroy(opus_decoder : OpusDecoder) -> None:
|
||||
opus_library = opus_module.create_static_library()
|
||||
|
||||
if opus_library:
|
||||
opus_library.opus_decoder_destroy(opus_decoder)
|
||||
@@ -0,0 +1,38 @@
|
||||
import ctypes
|
||||
from typing import Optional
|
||||
|
||||
from facefusion.libraries import opus as opus_module
|
||||
from facefusion.types import Buffer, OpusEncoder
|
||||
|
||||
|
||||
def create(sample_rate : int, channel_total : int) -> Optional[OpusEncoder]:
|
||||
opus_library = opus_module.create_static_library()
|
||||
|
||||
if opus_library:
|
||||
return opus_library.opus_encoder_create(sample_rate, channel_total, 2049, ctypes.byref(ctypes.c_int(0)))
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def encode(opus_encoder : OpusEncoder, input_buffer : Buffer, channel_total : int) -> Buffer:
|
||||
opus_library = opus_module.create_static_library()
|
||||
output_buffer = bytes()
|
||||
|
||||
if opus_library:
|
||||
sample_size = ctypes.sizeof(ctypes.c_float)
|
||||
sample_total = len(input_buffer) // (sample_size * channel_total)
|
||||
sample_buffer = (ctypes.c_float * (sample_total * channel_total)).from_buffer_copy(input_buffer)
|
||||
temp_buffer = ctypes.create_string_buffer(2048)
|
||||
output_total = opus_library.opus_encode_float(opus_encoder, sample_buffer, sample_total, temp_buffer, 2048)
|
||||
|
||||
if output_total:
|
||||
output_buffer = temp_buffer.raw[:output_total]
|
||||
|
||||
return output_buffer
|
||||
|
||||
|
||||
def destroy(opus_encoder : OpusEncoder) -> None:
|
||||
opus_library = opus_module.create_static_library()
|
||||
|
||||
if opus_library:
|
||||
opus_library.opus_encoder_destroy(opus_encoder)
|
||||
@@ -0,0 +1,74 @@
|
||||
import ctypes
|
||||
import struct
|
||||
from typing import Optional
|
||||
|
||||
from facefusion.libraries import vpx as vpx_module
|
||||
from facefusion.types import Buffer, BufferPack, VpxDecoder, VxpVideoCodec
|
||||
|
||||
|
||||
def create(video_codec : VxpVideoCodec, thread_count : int) -> Optional[VpxDecoder]:
|
||||
vpx_library = vpx_module.create_static_library()
|
||||
|
||||
if vpx_library:
|
||||
vpx_decoder = ctypes.create_string_buffer(64)
|
||||
vpx_algo = 'vpx_codec_vp8_dx_algo'
|
||||
|
||||
if video_codec == 'vp9':
|
||||
vpx_algo = 'vpx_codec_vp9_dx_algo'
|
||||
|
||||
vpx_codec = ctypes.c_void_p.in_dll(vpx_library, vpx_algo)
|
||||
config_buffer = ctypes.create_string_buffer(128)
|
||||
|
||||
struct.pack_into('I', config_buffer, 0, thread_count)
|
||||
|
||||
if vpx_library.vpx_codec_dec_init_ver(vpx_decoder, ctypes.byref(vpx_codec), config_buffer, 0, 12) == 0:
|
||||
return vpx_decoder
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def decode(vpx_decoder : VpxDecoder, input_buffer : Buffer) -> Optional[BufferPack]:
|
||||
vpx_library = vpx_module.create_static_library()
|
||||
|
||||
if vpx_library and input_buffer:
|
||||
input_total = len(input_buffer)
|
||||
temp_buffer = ctypes.create_string_buffer(input_buffer)
|
||||
|
||||
if vpx_library.vpx_codec_decode(vpx_decoder, temp_buffer, input_total, None, 0) == 0:
|
||||
address = vpx_library.vpx_codec_get_frame(vpx_decoder, ctypes.byref(ctypes.c_void_p(0)))
|
||||
|
||||
if address:
|
||||
frame_width = ctypes.c_uint.from_address(address + 24).value & ~1
|
||||
frame_height = ctypes.c_uint.from_address(address + 28).value & ~1
|
||||
|
||||
return BufferPack(
|
||||
buffer = collect(address, frame_width, frame_height),
|
||||
resolution = (frame_width, frame_height)
|
||||
)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def collect(address : int, frame_width : int, frame_height : int) -> Buffer:
|
||||
output_parts = []
|
||||
|
||||
for index in range(3):
|
||||
plane_pointer = ctypes.c_void_p.from_address(address + 48 + index * 8).value
|
||||
stride = ctypes.c_int.from_address(address + 80 + index * 4).value
|
||||
plane_width = frame_width >> (index > 0)
|
||||
plane_height = frame_height >> (index > 0)
|
||||
|
||||
if stride == plane_width:
|
||||
output_parts.append(ctypes.string_at(plane_pointer, plane_width * plane_height))
|
||||
else:
|
||||
for row in range(plane_height):
|
||||
output_parts.append(ctypes.string_at(plane_pointer + row * stride, plane_width))
|
||||
|
||||
return bytes().join(output_parts)
|
||||
|
||||
|
||||
def destroy(vpx_decoder : VpxDecoder) -> None:
|
||||
vpx_library = vpx_module.create_static_library()
|
||||
|
||||
if vpx_library:
|
||||
vpx_library.vpx_codec_destroy(vpx_decoder)
|
||||
@@ -0,0 +1,104 @@
|
||||
import ctypes
|
||||
import struct
|
||||
from typing import Optional
|
||||
|
||||
from facefusion.libraries import vpx as vpx_module
|
||||
from facefusion.types import BitRate, Buffer, Resolution, VpxEncoder, VxpVideoCodec
|
||||
|
||||
|
||||
def create(video_codec : VxpVideoCodec, frame_resolution : Resolution, bitrate : BitRate, thread_count : int, cpu_count : int) -> Optional[VpxEncoder]:
|
||||
vpx_library = vpx_module.create_static_library()
|
||||
|
||||
if vpx_library:
|
||||
vpx_encoder = ctypes.create_string_buffer(640)
|
||||
vpx_algo = 'vpx_codec_vp8_cx_algo'
|
||||
|
||||
if video_codec == 'vp9':
|
||||
vpx_algo = 'vpx_codec_vp9_cx_algo'
|
||||
|
||||
vpx_codec = ctypes.c_void_p.in_dll(vpx_library, vpx_algo)
|
||||
|
||||
config_buffer = ctypes.create_string_buffer(512)
|
||||
|
||||
if vpx_library.vpx_codec_enc_config_default(ctypes.byref(vpx_codec), config_buffer, 0) == 0:
|
||||
struct.pack_into('I', config_buffer, 4, thread_count)
|
||||
struct.pack_into('I', config_buffer, 12, frame_resolution[0])
|
||||
struct.pack_into('I', config_buffer, 16, frame_resolution[1])
|
||||
struct.pack_into('I', config_buffer, 28, 1)
|
||||
struct.pack_into('I', config_buffer, 36, 0)
|
||||
struct.pack_into('I', config_buffer, 44, 0)
|
||||
struct.pack_into('I', config_buffer, 72, 0)
|
||||
struct.pack_into('I', config_buffer, 112, bitrate)
|
||||
struct.pack_into('I', config_buffer, 116, 2)
|
||||
struct.pack_into('I', config_buffer, 120, 50)
|
||||
struct.pack_into('I', config_buffer, 124, 50)
|
||||
struct.pack_into('I', config_buffer, 128, 50)
|
||||
|
||||
if vpx_library.vpx_codec_enc_init_ver(vpx_encoder, ctypes.byref(vpx_codec), config_buffer, 0, 39) == 0:
|
||||
vpx_library.vpx_codec_control_(vpx_encoder, 13, ctypes.c_int(cpu_count))
|
||||
vpx_library.vpx_codec_control_(vpx_encoder, 12, ctypes.c_int(0))
|
||||
vpx_library.vpx_codec_control_(vpx_encoder, 27, ctypes.c_int(10))
|
||||
ctypes.memmove(ctypes.addressof(vpx_encoder) + 64, config_buffer, 512)
|
||||
return vpx_encoder
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def encode(vpx_encoder : VpxEncoder, input_buffer : Buffer, frame_resolution : Resolution, frame_index : int) -> Buffer:
|
||||
vpx_library = vpx_module.create_static_library()
|
||||
output_buffer = bytes()
|
||||
|
||||
if vpx_library:
|
||||
temp_buffer = ctypes.create_string_buffer(256)
|
||||
encode_buffer = ctypes.create_string_buffer(input_buffer)
|
||||
|
||||
if vpx_library.vpx_img_wrap(temp_buffer, 0x102, frame_resolution[0], frame_resolution[1], 1, encode_buffer) and vpx_library.vpx_codec_encode(vpx_encoder, temp_buffer, frame_index, 1, 0, 1) == 0:
|
||||
output_buffer = collect(vpx_encoder)
|
||||
|
||||
return output_buffer
|
||||
|
||||
|
||||
def collect(vpx_encoder : VpxEncoder) -> Buffer:
|
||||
vpx_library = vpx_module.create_static_library()
|
||||
output_parts = []
|
||||
|
||||
packet_cursor = ctypes.c_void_p(0)
|
||||
packet = vpx_library.vpx_codec_get_cx_data(vpx_encoder, ctypes.byref(packet_cursor))
|
||||
|
||||
while packet:
|
||||
if ctypes.c_int.from_address(packet).value == 0:
|
||||
buffer_pointer = ctypes.c_void_p.from_address(packet + 8).value
|
||||
buffer_size = ctypes.c_size_t.from_address(packet + 16).value
|
||||
output_parts.append(ctypes.string_at(buffer_pointer, buffer_size))
|
||||
|
||||
packet = vpx_library.vpx_codec_get_cx_data(vpx_encoder, ctypes.byref(packet_cursor))
|
||||
|
||||
return bytes().join(output_parts)
|
||||
|
||||
|
||||
def update_resolution(vpx_encoder : VpxEncoder, frame_resolution : Resolution) -> bool:
|
||||
vpx_library = vpx_module.create_static_library()
|
||||
|
||||
if vpx_library:
|
||||
struct.pack_into('I', vpx_encoder, 64 + 12, frame_resolution[0])
|
||||
struct.pack_into('I', vpx_encoder, 64 + 16, frame_resolution[1])
|
||||
return vpx_library.vpx_codec_enc_config_set(vpx_encoder, ctypes.cast(ctypes.addressof(vpx_encoder) + 64, ctypes.c_void_p)) == 0
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def update_bitrate(vpx_encoder : VpxEncoder, bitrate : BitRate) -> bool:
|
||||
vpx_library = vpx_module.create_static_library()
|
||||
|
||||
if vpx_library:
|
||||
struct.pack_into('I', vpx_encoder, 64 + 112, bitrate)
|
||||
return vpx_library.vpx_codec_enc_config_set(vpx_encoder, ctypes.cast(ctypes.addressof(vpx_encoder) + 64, ctypes.c_void_p)) == 0
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def destroy(vpx_encoder : VpxEncoder) -> None:
|
||||
vpx_library = vpx_module.create_static_library()
|
||||
|
||||
if vpx_library:
|
||||
vpx_library.vpx_codec_destroy(vpx_encoder)
|
||||
@@ -15,11 +15,11 @@ def is_windows() -> bool:
|
||||
|
||||
|
||||
def create_int_metavar(int_range : Sequence[int]) -> str:
|
||||
return '[' + str(int_range[0]) + '..' + str(int_range[-1]) + ':' + str(calc_int_step(int_range)) + ']'
|
||||
return '[' + str(int_range[0]) + '..' + str(int_range[-1]) + ':' + str(calculate_int_step(int_range)) + ']'
|
||||
|
||||
|
||||
def create_float_metavar(float_range : Sequence[float]) -> str:
|
||||
return '[' + str(float_range[0]) + '..' + str(float_range[-1]) + ':' + str(calc_float_step(float_range)) + ']'
|
||||
return '[' + str(float_range[0]) + '..' + str(float_range[-1]) + ':' + str(calculate_float_step(float_range)) + ']'
|
||||
|
||||
|
||||
def create_int_range(start : int, end : int, step : int) -> Sequence[int]:
|
||||
@@ -42,11 +42,11 @@ def create_float_range(start : float, end : float, step : float) -> Sequence[flo
|
||||
return float_range
|
||||
|
||||
|
||||
def calc_int_step(int_range : Sequence[int]) -> int:
|
||||
def calculate_int_step(int_range : Sequence[int]) -> int:
|
||||
return int_range[1] - int_range[0]
|
||||
|
||||
|
||||
def calc_float_step(float_range : Sequence[float]) -> float:
|
||||
def calculate_float_step(float_range : Sequence[float]) -> float:
|
||||
return round(float_range[1] - float_range[0], 2)
|
||||
|
||||
|
||||
@@ -78,6 +78,12 @@ def get_first(__list__ : Any) -> Any:
|
||||
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:
|
||||
if isinstance(__list__, Reversible):
|
||||
return next(reversed(__list__), None)
|
||||
|
||||
@@ -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
@@ -1,29 +1,20 @@
|
||||
from configparser import ConfigParser
|
||||
from functools import lru_cache
|
||||
from typing import List, Optional
|
||||
|
||||
from facefusion import state_manager
|
||||
from facefusion.common_helper import cast_bool, cast_float, cast_int
|
||||
|
||||
CONFIG_PARSER = None
|
||||
|
||||
|
||||
def get_config_parser() -> ConfigParser:
|
||||
global CONFIG_PARSER
|
||||
|
||||
if CONFIG_PARSER is None:
|
||||
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
|
||||
@lru_cache
|
||||
def get_static_config_parser() -> ConfigParser:
|
||||
config_parser = ConfigParser()
|
||||
config_parser.read(state_manager.get_item('config_path'), encoding = 'utf-8')
|
||||
return config_parser
|
||||
|
||||
|
||||
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():
|
||||
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]:
|
||||
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():
|
||||
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]:
|
||||
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():
|
||||
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]:
|
||||
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():
|
||||
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]]:
|
||||
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():
|
||||
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]]:
|
||||
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():
|
||||
return list(map(int, config_parser.get(section, option).split()))
|
||||
|
||||
@@ -1,26 +1,31 @@
|
||||
from functools import lru_cache
|
||||
from typing import List, Tuple
|
||||
from typing import Tuple
|
||||
|
||||
import numpy
|
||||
from tqdm import tqdm
|
||||
|
||||
from facefusion import inference_manager, state_manager, wording
|
||||
from facefusion import inference_manager, state_manager, translator
|
||||
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.thread_helper import conditional_thread_semaphore
|
||||
from facefusion.types import Detection, DownloadScope, DownloadSet, ExecutionProvider, Fps, InferencePool, ModelSet, VisionFrame
|
||||
from facefusion.vision import detect_video_fps, fit_frame, read_image, read_video_frame
|
||||
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
|
||||
|
||||
STREAM_COUNTER = 0
|
||||
|
||||
|
||||
@lru_cache(maxsize = None)
|
||||
@lru_cache()
|
||||
def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
||||
return\
|
||||
{
|
||||
'nsfw_1':
|
||||
{
|
||||
'__metadata__':
|
||||
{
|
||||
'vendor': 'EraX',
|
||||
'license': 'Apache-2.0',
|
||||
'year': 2024
|
||||
},
|
||||
'hashes':
|
||||
{
|
||||
'content_analyser':
|
||||
@@ -43,6 +48,12 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
||||
},
|
||||
'nsfw_2':
|
||||
{
|
||||
'__metadata__':
|
||||
{
|
||||
'vendor': 'Marqo',
|
||||
'license': 'Apache-2.0',
|
||||
'year': 2024
|
||||
},
|
||||
'hashes':
|
||||
{
|
||||
'content_analyser':
|
||||
@@ -65,6 +76,12 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
||||
},
|
||||
'nsfw_3':
|
||||
{
|
||||
'__metadata__':
|
||||
{
|
||||
'vendor': 'Freepik',
|
||||
'license': 'MIT',
|
||||
'year': 2025
|
||||
},
|
||||
'hashes':
|
||||
{
|
||||
'content_analyser':
|
||||
@@ -100,12 +117,6 @@ def clear_inference_pool() -> None:
|
||||
inference_manager.clear_inference_pool(__name__, model_names)
|
||||
|
||||
|
||||
def resolve_execution_providers() -> List[ExecutionProvider]:
|
||||
if has_execution_provider('coreml'):
|
||||
return [ 'cpu' ]
|
||||
return state_manager.get_item('execution_providers')
|
||||
|
||||
|
||||
def collect_model_downloads() -> Tuple[DownloadSet, DownloadSet]:
|
||||
model_set = create_static_model_set('full')
|
||||
model_hash_set = {}
|
||||
@@ -137,13 +148,13 @@ def analyse_frame(vision_frame : VisionFrame) -> bool:
|
||||
return detect_nsfw(vision_frame)
|
||||
|
||||
|
||||
@lru_cache(maxsize = None)
|
||||
@lru_cache()
|
||||
def analyse_image(image_path : str) -> bool:
|
||||
vision_frame = read_image(image_path)
|
||||
return analyse_frame(vision_frame)
|
||||
|
||||
|
||||
@lru_cache(maxsize = None)
|
||||
@lru_cache()
|
||||
def analyse_video(video_path : str, trim_frame_start : int, trim_frame_end : int) -> bool:
|
||||
video_fps = detect_video_fps(video_path)
|
||||
frame_range = range(trim_frame_start, trim_frame_end)
|
||||
@@ -151,16 +162,21 @@ def analyse_video(video_path : str, trim_frame_start : int, trim_frame_end : int
|
||||
total = 0
|
||||
counter = 0
|
||||
|
||||
with tqdm(total = len(frame_range), desc = wording.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:
|
||||
if frame_number % int(video_fps) == 0:
|
||||
vision_frame = read_video_frame(video_path, frame_number)
|
||||
total += 1
|
||||
if analyse_frame(vision_frame):
|
||||
counter += 1
|
||||
|
||||
if numpy.any(vision_frame):
|
||||
total += 1
|
||||
|
||||
if analyse_frame(vision_frame):
|
||||
counter += 1
|
||||
|
||||
if counter > 0 and total > 0:
|
||||
rate = counter / total * 100
|
||||
|
||||
progress.set_postfix(rate = rate)
|
||||
progress.update()
|
||||
|
||||
@@ -196,8 +212,8 @@ def detect_with_nsfw_3(vision_frame : VisionFrame) -> bool:
|
||||
return bool(detection_score > 10.5)
|
||||
|
||||
|
||||
def forward_nsfw(vision_frame : VisionFrame, nsfw_model : str) -> Detection:
|
||||
content_analyser = get_inference_pool().get(nsfw_model)
|
||||
def forward_nsfw(vision_frame : VisionFrame, model_name : str) -> Detection:
|
||||
content_analyser = get_inference_pool().get(model_name)
|
||||
|
||||
with conditional_thread_semaphore():
|
||||
detection = content_analyser.run(None,
|
||||
@@ -205,7 +221,7 @@ def forward_nsfw(vision_frame : VisionFrame, nsfw_model : str) -> Detection:
|
||||
'input': vision_frame
|
||||
})[0]
|
||||
|
||||
if nsfw_model in [ 'nsfw_2', 'nsfw_3' ]:
|
||||
if model_name in [ 'nsfw_2', 'nsfw_3' ]:
|
||||
return detection[0]
|
||||
|
||||
return detection
|
||||
@@ -217,7 +233,7 @@ def prepare_detect_frame(temp_vision_frame : VisionFrame, model_name : str) -> V
|
||||
model_mean = model_set.get('mean')
|
||||
model_standard_deviation = model_set.get('standard_deviation')
|
||||
|
||||
detect_vision_frame = fit_frame(temp_vision_frame, model_size)
|
||||
detect_vision_frame = fit_contain_frame(temp_vision_frame, model_size)
|
||||
detect_vision_frame = detect_vision_frame[:, :, ::-1] / 255.0
|
||||
detect_vision_frame -= model_mean
|
||||
detect_vision_frame /= model_standard_deviation
|
||||
|
||||
+115
-280
@@ -5,28 +5,22 @@ import signal
|
||||
import sys
|
||||
from time import time
|
||||
|
||||
import numpy
|
||||
import uvicorn
|
||||
|
||||
from facefusion import benchmarker, cli_helper, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, hash_helper, logger, process_manager, state_manager, video_manager, voice_extractor, wording
|
||||
from facefusion.args import apply_args, collect_job_args, reduce_job_args, reduce_step_args
|
||||
from facefusion.common_helper import get_first
|
||||
from facefusion.content_analyser import analyse_image, analyse_video
|
||||
import facefusion.apis.core
|
||||
from facefusion import args_helper, benchmarker, cli_helper, content_analyser, hash_helper, logger, state_manager, translator
|
||||
from facefusion.args_helper import apply_args
|
||||
from facefusion.download import conditional_download_hashes, conditional_download_sources
|
||||
from facefusion.exit_helper import hard_exit, signal_exit
|
||||
from facefusion.face_analyser import get_average_face, get_many_faces, get_one_face
|
||||
from facefusion.face_selector import sort_and_filter_faces
|
||||
from facefusion.face_store import append_reference_face, clear_reference_faces, get_reference_faces
|
||||
from facefusion.ffmpeg import copy_image, extract_frames, finalize_image, merge_video, replace_audio, restore_audio
|
||||
from facefusion.filesystem import filter_audio_paths, get_file_name, is_image, is_video, resolve_file_paths, resolve_file_pattern
|
||||
from facefusion.filesystem import get_file_extension, has_audio, has_image, has_video
|
||||
from facefusion.filesystem import get_file_name, resolve_file_paths, resolve_file_pattern
|
||||
from facefusion.jobs import job_helper, job_manager, job_runner
|
||||
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.program import create_program
|
||||
from facefusion.program_helper import validate_args
|
||||
from facefusion.temp_helper import clear_temp_directory, create_temp_directory, get_temp_file_path, move_temp_file, resolve_temp_frame_paths
|
||||
from facefusion.types import Args, ErrorCode
|
||||
from facefusion.vision import pack_resolution, read_image, read_static_images, read_video_frame, restrict_image_resolution, restrict_trim_frame, restrict_video_fps, restrict_video_resolution, unpack_resolution
|
||||
from facefusion.types import Args, ErrorCode, WorkflowMode
|
||||
from facefusion.workflows import audio_to_image, audio_to_image_as_frames, image_to_image, image_to_video, image_to_video_as_frames
|
||||
|
||||
|
||||
def cli() -> None:
|
||||
@@ -50,51 +44,43 @@ def cli() -> 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':
|
||||
error_code = force_download()
|
||||
return hard_exit(error_code)
|
||||
hard_exit(error_code)
|
||||
|
||||
if state_manager.get_item('command') == 'benchmark':
|
||||
if not common_pre_check() or not processors_pre_check() or not benchmarker.pre_check():
|
||||
return hard_exit(2)
|
||||
hard_exit(2)
|
||||
benchmarker.render()
|
||||
|
||||
if state_manager.get_item('command') == 'api':
|
||||
if not common_pre_check() or not processors_pre_check() or not facefusion.apis.core.pre_check():
|
||||
hard_exit(2)
|
||||
|
||||
logger.info(translator.get('api_started').format(host = state_manager.get_item('api_host'), port = state_manager.get_item('api_port')), __name__)
|
||||
uvicorn.run(facefusion.apis.core.create_api(), host = state_manager.get_item('api_host'), port = state_manager.get_item('api_port'))
|
||||
hard_exit(1)
|
||||
|
||||
if state_manager.get_item('command') in [ 'job-list', 'job-create', 'job-submit', 'job-submit-all', 'job-delete', 'job-delete-all', 'job-add-step', 'job-remix-step', 'job-insert-step', 'job-remove-step' ]:
|
||||
if not job_manager.init_jobs(state_manager.get_item('jobs_path')):
|
||||
if not job_manager.init_jobs(state_manager.get_jobs_path()):
|
||||
hard_exit(1)
|
||||
error_code = route_job_manager(args)
|
||||
hard_exit(error_code)
|
||||
|
||||
if state_manager.get_item('command') == 'run':
|
||||
import facefusion.uis.core as ui
|
||||
|
||||
if not common_pre_check() or not processors_pre_check():
|
||||
return hard_exit(2)
|
||||
for ui_layout in ui.get_ui_layouts_modules(state_manager.get_item('ui_layouts')):
|
||||
if not ui_layout.pre_check():
|
||||
return hard_exit(2)
|
||||
ui.init()
|
||||
ui.launch()
|
||||
|
||||
if state_manager.get_item('command') == 'headless-run':
|
||||
if not job_manager.init_jobs(state_manager.get_item('jobs_path')):
|
||||
if not job_manager.init_jobs(state_manager.get_jobs_path()):
|
||||
hard_exit(1)
|
||||
error_core = process_headless(args)
|
||||
hard_exit(error_core)
|
||||
error_code = process_headless(args)
|
||||
hard_exit(error_code)
|
||||
|
||||
if state_manager.get_item('command') == 'batch-run':
|
||||
if not job_manager.init_jobs(state_manager.get_item('jobs_path')):
|
||||
if not job_manager.init_jobs(state_manager.get_jobs_path()):
|
||||
hard_exit(1)
|
||||
error_core = process_batch(args)
|
||||
hard_exit(error_core)
|
||||
error_code = process_batch(args)
|
||||
hard_exit(error_code)
|
||||
|
||||
if state_manager.get_item('command') in [ 'job-run', 'job-run-all', 'job-retry', 'job-retry-all' ]:
|
||||
if not job_manager.init_jobs(state_manager.get_item('jobs_path')):
|
||||
if not job_manager.init_jobs(state_manager.get_jobs_path()):
|
||||
hard_exit(1)
|
||||
error_code = route_job_runner()
|
||||
hard_exit(error_code)
|
||||
@@ -102,35 +88,23 @@ def route(args : Args) -> None:
|
||||
|
||||
def pre_check() -> bool:
|
||||
if sys.version_info < (3, 10):
|
||||
logger.error(wording.get('python_not_supported').format(version = '3.10'), __name__)
|
||||
logger.error(translator.get('python_not_supported').format(version = '3.10'), __name__)
|
||||
return False
|
||||
|
||||
if not shutil.which('curl'):
|
||||
logger.error(wording.get('curl_not_installed'), __name__)
|
||||
logger.error(translator.get('curl_not_installed'), __name__)
|
||||
return False
|
||||
|
||||
if not shutil.which('ffmpeg'):
|
||||
logger.error(wording.get('ffmpeg_not_installed'), __name__)
|
||||
logger.error(translator.get('ffmpeg_not_installed'), __name__)
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
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()
|
||||
is_valid = hash_helper.create_hash(content_analyser_content) == 'b159fd9d'
|
||||
|
||||
return all(module.pre_check() for module in common_modules) and is_valid
|
||||
return hash_helper.create_hash(content_analyser_content) == '975d67d6'
|
||||
|
||||
|
||||
def processors_pre_check() -> bool:
|
||||
@@ -141,22 +115,19 @@ def processors_pre_check() -> bool:
|
||||
|
||||
|
||||
def force_download() -> ErrorCode:
|
||||
common_modules =\
|
||||
[
|
||||
content_analyser,
|
||||
face_classifier,
|
||||
face_detector,
|
||||
face_landmarker,
|
||||
face_masker,
|
||||
face_recognizer,
|
||||
voice_extractor
|
||||
]
|
||||
download_scope = state_manager.get_item('download_scope')
|
||||
available_processors = [ get_file_name(file_path) for file_path in resolve_file_paths('facefusion/processors/modules') ]
|
||||
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:
|
||||
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_source_set = model.get('sources')
|
||||
|
||||
@@ -178,113 +149,113 @@ def route_job_manager(args : Args) -> ErrorCode:
|
||||
|
||||
if state_manager.get_item('command') == 'job-create':
|
||||
if job_manager.create_job(state_manager.get_item('job_id')):
|
||||
logger.info(wording.get('job_created').format(job_id = state_manager.get_item('job_id')), __name__)
|
||||
logger.info(translator.get('job_created').format(job_id = state_manager.get_item('job_id')), __name__)
|
||||
return 0
|
||||
logger.error(wording.get('job_not_created').format(job_id = state_manager.get_item('job_id')), __name__)
|
||||
logger.error(translator.get('job_not_created').format(job_id = state_manager.get_item('job_id')), __name__)
|
||||
return 1
|
||||
|
||||
if state_manager.get_item('command') == 'job-submit':
|
||||
if job_manager.submit_job(state_manager.get_item('job_id')):
|
||||
logger.info(wording.get('job_submitted').format(job_id = state_manager.get_item('job_id')), __name__)
|
||||
logger.info(translator.get('job_submitted').format(job_id = state_manager.get_item('job_id')), __name__)
|
||||
return 0
|
||||
logger.error(wording.get('job_not_submitted').format(job_id = state_manager.get_item('job_id')), __name__)
|
||||
logger.error(translator.get('job_not_submitted').format(job_id = state_manager.get_item('job_id')), __name__)
|
||||
return 1
|
||||
|
||||
if state_manager.get_item('command') == 'job-submit-all':
|
||||
if job_manager.submit_jobs(state_manager.get_item('halt_on_error')):
|
||||
logger.info(wording.get('job_all_submitted'), __name__)
|
||||
logger.info(translator.get('job_all_submitted'), __name__)
|
||||
return 0
|
||||
logger.error(wording.get('job_all_not_submitted'), __name__)
|
||||
logger.error(translator.get('job_all_not_submitted'), __name__)
|
||||
return 1
|
||||
|
||||
if state_manager.get_item('command') == 'job-delete':
|
||||
if job_manager.delete_job(state_manager.get_item('job_id')):
|
||||
logger.info(wording.get('job_deleted').format(job_id = state_manager.get_item('job_id')), __name__)
|
||||
logger.info(translator.get('job_deleted').format(job_id = state_manager.get_item('job_id')), __name__)
|
||||
return 0
|
||||
logger.error(wording.get('job_not_deleted').format(job_id = state_manager.get_item('job_id')), __name__)
|
||||
logger.error(translator.get('job_not_deleted').format(job_id = state_manager.get_item('job_id')), __name__)
|
||||
return 1
|
||||
|
||||
if state_manager.get_item('command') == 'job-delete-all':
|
||||
if job_manager.delete_jobs(state_manager.get_item('halt_on_error')):
|
||||
logger.info(wording.get('job_all_deleted'), __name__)
|
||||
logger.info(translator.get('job_all_deleted'), __name__)
|
||||
return 0
|
||||
logger.error(wording.get('job_all_not_deleted'), __name__)
|
||||
logger.error(translator.get('job_all_not_deleted'), __name__)
|
||||
return 1
|
||||
|
||||
if state_manager.get_item('command') == 'job-add-step':
|
||||
step_args = reduce_step_args(args)
|
||||
step_args = args_helper.filter_step_args(args)
|
||||
|
||||
if job_manager.add_step(state_manager.get_item('job_id'), step_args):
|
||||
logger.info(wording.get('job_step_added').format(job_id = state_manager.get_item('job_id')), __name__)
|
||||
logger.info(translator.get('job_step_added').format(job_id = state_manager.get_item('job_id')), __name__)
|
||||
return 0
|
||||
logger.error(wording.get('job_step_not_added').format(job_id = state_manager.get_item('job_id')), __name__)
|
||||
logger.error(translator.get('job_step_not_added').format(job_id = state_manager.get_item('job_id')), __name__)
|
||||
return 1
|
||||
|
||||
if state_manager.get_item('command') == 'job-remix-step':
|
||||
step_args = reduce_step_args(args)
|
||||
step_args = args_helper.filter_step_args(args)
|
||||
|
||||
if job_manager.remix_step(state_manager.get_item('job_id'), state_manager.get_item('step_index'), step_args):
|
||||
logger.info(wording.get('job_remix_step_added').format(job_id = state_manager.get_item('job_id'), step_index = state_manager.get_item('step_index')), __name__)
|
||||
logger.info(translator.get('job_remix_step_added').format(job_id = state_manager.get_item('job_id'), step_index = state_manager.get_item('step_index')), __name__)
|
||||
return 0
|
||||
logger.error(wording.get('job_remix_step_not_added').format(job_id = state_manager.get_item('job_id'), step_index = state_manager.get_item('step_index')), __name__)
|
||||
logger.error(translator.get('job_remix_step_not_added').format(job_id = state_manager.get_item('job_id'), step_index = state_manager.get_item('step_index')), __name__)
|
||||
return 1
|
||||
|
||||
if state_manager.get_item('command') == 'job-insert-step':
|
||||
step_args = reduce_step_args(args)
|
||||
step_args = args_helper.filter_step_args(args)
|
||||
|
||||
if job_manager.insert_step(state_manager.get_item('job_id'), state_manager.get_item('step_index'), step_args):
|
||||
logger.info(wording.get('job_step_inserted').format(job_id = state_manager.get_item('job_id'), step_index = state_manager.get_item('step_index')), __name__)
|
||||
logger.info(translator.get('job_step_inserted').format(job_id = state_manager.get_item('job_id'), step_index = state_manager.get_item('step_index')), __name__)
|
||||
return 0
|
||||
logger.error(wording.get('job_step_not_inserted').format(job_id = state_manager.get_item('job_id'), step_index = state_manager.get_item('step_index')), __name__)
|
||||
logger.error(translator.get('job_step_not_inserted').format(job_id = state_manager.get_item('job_id'), step_index = state_manager.get_item('step_index')), __name__)
|
||||
return 1
|
||||
|
||||
if state_manager.get_item('command') == 'job-remove-step':
|
||||
if job_manager.remove_step(state_manager.get_item('job_id'), state_manager.get_item('step_index')):
|
||||
logger.info(wording.get('job_step_removed').format(job_id = state_manager.get_item('job_id'), step_index = state_manager.get_item('step_index')), __name__)
|
||||
logger.info(translator.get('job_step_removed').format(job_id = state_manager.get_item('job_id'), step_index = state_manager.get_item('step_index')), __name__)
|
||||
return 0
|
||||
logger.error(wording.get('job_step_not_removed').format(job_id = state_manager.get_item('job_id'), step_index = state_manager.get_item('step_index')), __name__)
|
||||
logger.error(translator.get('job_step_not_removed').format(job_id = state_manager.get_item('job_id'), step_index = state_manager.get_item('step_index')), __name__)
|
||||
return 1
|
||||
return 1
|
||||
|
||||
|
||||
def route_job_runner() -> ErrorCode:
|
||||
if state_manager.get_item('command') == 'job-run':
|
||||
logger.info(wording.get('running_job').format(job_id = state_manager.get_item('job_id')), __name__)
|
||||
logger.info(translator.get('running_job').format(job_id = state_manager.get_item('job_id')), __name__)
|
||||
if job_runner.run_job(state_manager.get_item('job_id'), process_step):
|
||||
logger.info(wording.get('processing_job_succeed').format(job_id = state_manager.get_item('job_id')), __name__)
|
||||
logger.info(translator.get('processing_job_succeeded').format(job_id = state_manager.get_item('job_id')), __name__)
|
||||
return 0
|
||||
logger.info(wording.get('processing_job_failed').format(job_id = state_manager.get_item('job_id')), __name__)
|
||||
logger.info(translator.get('processing_job_failed').format(job_id = state_manager.get_item('job_id')), __name__)
|
||||
return 1
|
||||
|
||||
if state_manager.get_item('command') == 'job-run-all':
|
||||
logger.info(wording.get('running_jobs'), __name__)
|
||||
logger.info(translator.get('running_jobs'), __name__)
|
||||
if job_runner.run_jobs(process_step, state_manager.get_item('halt_on_error')):
|
||||
logger.info(wording.get('processing_jobs_succeed'), __name__)
|
||||
logger.info(translator.get('processing_jobs_succeeded'), __name__)
|
||||
return 0
|
||||
logger.info(wording.get('processing_jobs_failed'), __name__)
|
||||
logger.info(translator.get('processing_jobs_failed'), __name__)
|
||||
return 1
|
||||
|
||||
if state_manager.get_item('command') == 'job-retry':
|
||||
logger.info(wording.get('retrying_job').format(job_id = state_manager.get_item('job_id')), __name__)
|
||||
logger.info(translator.get('retrying_job').format(job_id = state_manager.get_item('job_id')), __name__)
|
||||
if job_runner.retry_job(state_manager.get_item('job_id'), process_step):
|
||||
logger.info(wording.get('processing_job_succeed').format(job_id = state_manager.get_item('job_id')), __name__)
|
||||
logger.info(translator.get('processing_job_succeeded').format(job_id = state_manager.get_item('job_id')), __name__)
|
||||
return 0
|
||||
logger.info(wording.get('processing_job_failed').format(job_id = state_manager.get_item('job_id')), __name__)
|
||||
logger.info(translator.get('processing_job_failed').format(job_id = state_manager.get_item('job_id')), __name__)
|
||||
return 1
|
||||
|
||||
if state_manager.get_item('command') == 'job-retry-all':
|
||||
logger.info(wording.get('retrying_jobs'), __name__)
|
||||
logger.info(translator.get('retrying_jobs'), __name__)
|
||||
if job_runner.retry_jobs(process_step, state_manager.get_item('halt_on_error')):
|
||||
logger.info(wording.get('processing_jobs_succeed'), __name__)
|
||||
logger.info(translator.get('processing_jobs_succeeded'), __name__)
|
||||
return 0
|
||||
logger.info(wording.get('processing_jobs_failed'), __name__)
|
||||
logger.info(translator.get('processing_jobs_failed'), __name__)
|
||||
return 1
|
||||
return 2
|
||||
|
||||
|
||||
def process_headless(args : Args) -> ErrorCode:
|
||||
job_id = job_helper.suggest_job_id('headless')
|
||||
step_args = reduce_step_args(args)
|
||||
step_args = args_helper.filter_step_args(args)
|
||||
|
||||
if job_manager.create_job(job_id) and job_manager.add_step(job_id, step_args) and job_manager.submit_job(job_id) and job_runner.run_job(job_id, process_step):
|
||||
return 0
|
||||
@@ -293,17 +264,21 @@ def process_headless(args : Args) -> ErrorCode:
|
||||
|
||||
def process_batch(args : Args) -> ErrorCode:
|
||||
job_id = job_helper.suggest_job_id('batch')
|
||||
step_args = reduce_step_args(args)
|
||||
job_args = reduce_job_args(args)
|
||||
source_paths = resolve_file_pattern(job_args.get('source_pattern'))
|
||||
target_paths = resolve_file_pattern(job_args.get('target_pattern'))
|
||||
step_args = args_helper.filter_step_args(args)
|
||||
source_paths = resolve_file_pattern(step_args.get('source_pattern'))
|
||||
target_paths = resolve_file_pattern(step_args.get('target_pattern'))
|
||||
|
||||
if job_manager.create_job(job_id):
|
||||
if source_paths and target_paths:
|
||||
for index, (source_path, target_path) in enumerate(itertools.product(source_paths, target_paths)):
|
||||
step_args['source_paths'] = [ source_path ]
|
||||
step_args['target_path'] = target_path
|
||||
step_args['output_path'] = job_args.get('output_pattern').format(index = index)
|
||||
|
||||
try:
|
||||
step_args['output_path'] = step_args.get('output_pattern').format(index = index, source_name = get_file_name(source_path), target_name = get_file_name(target_path), target_extension = get_file_extension(target_path))
|
||||
except KeyError:
|
||||
return 1
|
||||
|
||||
if not job_manager.add_step(job_id, step_args):
|
||||
return 1
|
||||
if job_manager.submit_job(job_id) and job_runner.run_job(job_id, process_step):
|
||||
@@ -312,7 +287,12 @@ def process_batch(args : Args) -> ErrorCode:
|
||||
if not source_paths and target_paths:
|
||||
for index, target_path in enumerate(target_paths):
|
||||
step_args['target_path'] = target_path
|
||||
step_args['output_path'] = job_args.get('output_pattern').format(index = index)
|
||||
|
||||
try:
|
||||
step_args['output_path'] = step_args.get('output_pattern').format(index = index, target_name = get_file_name(target_path), target_extension = get_file_extension(target_path))
|
||||
except KeyError:
|
||||
return 1
|
||||
|
||||
if not job_manager.add_step(job_id, step_args):
|
||||
return 1
|
||||
if job_manager.submit_job(job_id) and job_runner.run_job(job_id, process_step):
|
||||
@@ -321,12 +301,13 @@ def process_batch(args : Args) -> ErrorCode:
|
||||
|
||||
|
||||
def process_step(job_id : str, step_index : int, step_args : Args) -> bool:
|
||||
clear_reference_faces()
|
||||
step_total = job_manager.count_step_total(job_id)
|
||||
step_args.update(collect_job_args())
|
||||
apply_args(step_args, state_manager.set_item)
|
||||
cli_args = args_helper.extract_cli_args(state_manager.get_state())
|
||||
args = cli_args.copy()
|
||||
args.update(step_args)
|
||||
apply_args(args, state_manager.set_item)
|
||||
|
||||
logger.info(wording.get('processing_step').format(step_current = step_index + 1, step_total = step_total), __name__)
|
||||
logger.info(translator.get('processing_step').format(step_current = step_index + 1, step_total = step_total), __name__)
|
||||
if common_pre_check() and processors_pre_check():
|
||||
error_code = conditional_process()
|
||||
return error_code == 0
|
||||
@@ -336,182 +317,36 @@ def process_step(job_id : str, step_index : int, step_args : Args) -> bool:
|
||||
def conditional_process() -> ErrorCode:
|
||||
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')):
|
||||
if not processor_module.pre_process('output'):
|
||||
return 2
|
||||
|
||||
conditional_append_reference_faces()
|
||||
|
||||
if is_image(state_manager.get_item('target_path')):
|
||||
return process_image(start_time)
|
||||
if is_video(state_manager.get_item('target_path')):
|
||||
return process_video(start_time)
|
||||
if state_manager.get_item('workflow_mode') == 'audio-to-image:video':
|
||||
return audio_to_image.process(start_time)
|
||||
if state_manager.get_item('workflow_mode') == 'audio-to-image:frames':
|
||||
return audio_to_image_as_frames.process(start_time)
|
||||
if state_manager.get_item('workflow_mode') == 'image-to-image':
|
||||
return image_to_image.process(start_time)
|
||||
if state_manager.get_item('workflow_mode') == 'image-to-video':
|
||||
return image_to_video.process(start_time)
|
||||
if state_manager.get_item('workflow_mode') == 'image-to-video:frames':
|
||||
return image_to_video_as_frames.process(start_time)
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
def conditional_append_reference_faces() -> None:
|
||||
if 'reference' in state_manager.get_item('face_selector_mode') and not get_reference_faces():
|
||||
source_frames = read_static_images(state_manager.get_item('source_paths'))
|
||||
source_faces = get_many_faces(source_frames)
|
||||
source_face = get_average_face(source_faces)
|
||||
if is_video(state_manager.get_item('target_path')):
|
||||
reference_frame = read_video_frame(state_manager.get_item('target_path'), state_manager.get_item('reference_frame_number'))
|
||||
else:
|
||||
reference_frame = read_image(state_manager.get_item('target_path'))
|
||||
reference_faces = sort_and_filter_faces(get_many_faces([ reference_frame ]))
|
||||
reference_face = get_one_face(reference_faces, state_manager.get_item('reference_face_position'))
|
||||
append_reference_face('origin', reference_face)
|
||||
def detect_workflow_mode() -> WorkflowMode:
|
||||
if has_video([ state_manager.get_item('target_path') ]):
|
||||
if get_file_extension(state_manager.get_item('output_path')):
|
||||
return 'image-to-video'
|
||||
return 'image-to-video:frames'
|
||||
|
||||
if source_face and reference_face:
|
||||
for processor_module in get_processors_modules(state_manager.get_item('processors')):
|
||||
abstract_reference_frame = processor_module.get_reference_frame(source_face, reference_face, reference_frame)
|
||||
if numpy.any(abstract_reference_frame):
|
||||
abstract_reference_faces = sort_and_filter_faces(get_many_faces([ abstract_reference_frame ]))
|
||||
abstract_reference_face = get_one_face(abstract_reference_faces, state_manager.get_item('reference_face_position'))
|
||||
append_reference_face(processor_module.__name__, abstract_reference_face)
|
||||
if has_audio(state_manager.get_item('source_paths')) and has_image([ state_manager.get_item('target_path') ]):
|
||||
if get_file_extension(state_manager.get_item('output_path')):
|
||||
return 'audio-to-image:video'
|
||||
return 'audio-to-image:frames'
|
||||
|
||||
|
||||
def process_image(start_time : float) -> ErrorCode:
|
||||
if analyse_image(state_manager.get_item('target_path')):
|
||||
return 3
|
||||
|
||||
logger.debug(wording.get('clearing_temp'), __name__)
|
||||
clear_temp_directory(state_manager.get_item('target_path'))
|
||||
logger.debug(wording.get('creating_temp'), __name__)
|
||||
create_temp_directory(state_manager.get_item('target_path'))
|
||||
|
||||
process_manager.start()
|
||||
temp_image_resolution = pack_resolution(restrict_image_resolution(state_manager.get_item('target_path'), unpack_resolution(state_manager.get_item('output_image_resolution'))))
|
||||
logger.info(wording.get('copying_image').format(resolution = temp_image_resolution), __name__)
|
||||
if copy_image(state_manager.get_item('target_path'), temp_image_resolution):
|
||||
logger.debug(wording.get('copying_image_succeed'), __name__)
|
||||
else:
|
||||
logger.error(wording.get('copying_image_failed'), __name__)
|
||||
process_manager.end()
|
||||
return 1
|
||||
|
||||
temp_image_path = get_temp_file_path(state_manager.get_item('target_path'))
|
||||
for processor_module in get_processors_modules(state_manager.get_item('processors')):
|
||||
logger.info(wording.get('processing'), processor_module.__name__)
|
||||
processor_module.process_image(state_manager.get_item('source_paths'), temp_image_path, temp_image_path)
|
||||
processor_module.post_process()
|
||||
if is_process_stopping():
|
||||
process_manager.end()
|
||||
return 4
|
||||
|
||||
logger.info(wording.get('finalizing_image').format(resolution = state_manager.get_item('output_image_resolution')), __name__)
|
||||
if finalize_image(state_manager.get_item('target_path'), state_manager.get_item('output_path'), state_manager.get_item('output_image_resolution')):
|
||||
logger.debug(wording.get('finalizing_image_succeed'), __name__)
|
||||
else:
|
||||
logger.warn(wording.get('finalizing_image_skipped'), __name__)
|
||||
|
||||
logger.debug(wording.get('clearing_temp'), __name__)
|
||||
clear_temp_directory(state_manager.get_item('target_path'))
|
||||
|
||||
if is_image(state_manager.get_item('output_path')):
|
||||
seconds = '{:.2f}'.format((time() - start_time) % 60)
|
||||
logger.info(wording.get('processing_image_succeed').format(seconds = seconds), __name__)
|
||||
else:
|
||||
logger.error(wording.get('processing_image_failed'), __name__)
|
||||
process_manager.end()
|
||||
return 1
|
||||
process_manager.end()
|
||||
return 0
|
||||
|
||||
|
||||
def process_video(start_time : float) -> 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(wording.get('clearing_temp'), __name__)
|
||||
clear_temp_directory(state_manager.get_item('target_path'))
|
||||
logger.debug(wording.get('creating_temp'), __name__)
|
||||
create_temp_directory(state_manager.get_item('target_path'))
|
||||
|
||||
process_manager.start()
|
||||
temp_video_resolution = pack_resolution(restrict_video_resolution(state_manager.get_item('target_path'), unpack_resolution(state_manager.get_item('output_video_resolution'))))
|
||||
temp_video_fps = restrict_video_fps(state_manager.get_item('target_path'), state_manager.get_item('output_video_fps'))
|
||||
logger.info(wording.get('extracting_frames').format(resolution = temp_video_resolution, fps = temp_video_fps), __name__)
|
||||
if extract_frames(state_manager.get_item('target_path'), temp_video_resolution, temp_video_fps, trim_frame_start, trim_frame_end):
|
||||
logger.debug(wording.get('extracting_frames_succeed'), __name__)
|
||||
else:
|
||||
if is_process_stopping():
|
||||
process_manager.end()
|
||||
return 4
|
||||
logger.error(wording.get('extracting_frames_failed'), __name__)
|
||||
process_manager.end()
|
||||
return 1
|
||||
|
||||
temp_frame_paths = resolve_temp_frame_paths(state_manager.get_item('target_path'))
|
||||
if temp_frame_paths:
|
||||
for processor_module in get_processors_modules(state_manager.get_item('processors')):
|
||||
logger.info(wording.get('processing'), processor_module.__name__)
|
||||
processor_module.process_video(state_manager.get_item('source_paths'), temp_frame_paths)
|
||||
processor_module.post_process()
|
||||
if is_process_stopping():
|
||||
return 4
|
||||
else:
|
||||
logger.error(wording.get('temp_frames_not_found'), __name__)
|
||||
process_manager.end()
|
||||
return 1
|
||||
|
||||
logger.info(wording.get('merging_video').format(resolution = state_manager.get_item('output_video_resolution'), fps = state_manager.get_item('output_video_fps')), __name__)
|
||||
if merge_video(state_manager.get_item('target_path'), temp_video_fps, state_manager.get_item('output_video_resolution'), state_manager.get_item('output_video_fps'), trim_frame_start, trim_frame_end):
|
||||
logger.debug(wording.get('merging_video_succeed'), __name__)
|
||||
else:
|
||||
if is_process_stopping():
|
||||
process_manager.end()
|
||||
return 4
|
||||
logger.error(wording.get('merging_video_failed'), __name__)
|
||||
process_manager.end()
|
||||
return 1
|
||||
|
||||
if state_manager.get_item('output_audio_volume') == 0:
|
||||
logger.info(wording.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 replace_audio(state_manager.get_item('target_path'), source_audio_path, state_manager.get_item('output_path')):
|
||||
video_manager.clear_video_pool()
|
||||
logger.debug(wording.get('replacing_audio_succeed'), __name__)
|
||||
else:
|
||||
video_manager.clear_video_pool()
|
||||
if is_process_stopping():
|
||||
process_manager.end()
|
||||
return 4
|
||||
logger.warn(wording.get('replacing_audio_skipped'), __name__)
|
||||
move_temp_file(state_manager.get_item('target_path'), state_manager.get_item('output_path'))
|
||||
else:
|
||||
if 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(wording.get('restoring_audio_succeed'), __name__)
|
||||
else:
|
||||
video_manager.clear_video_pool()
|
||||
if is_process_stopping():
|
||||
process_manager.end()
|
||||
return 4
|
||||
logger.warn(wording.get('restoring_audio_skipped'), __name__)
|
||||
move_temp_file(state_manager.get_item('target_path'), state_manager.get_item('output_path'))
|
||||
|
||||
logger.debug(wording.get('clearing_temp'), __name__)
|
||||
clear_temp_directory(state_manager.get_item('target_path'))
|
||||
|
||||
if is_video(state_manager.get_item('output_path')):
|
||||
seconds = '{:.2f}'.format((time() - start_time))
|
||||
logger.info(wording.get('processing_video_succeed').format(seconds = seconds), __name__)
|
||||
else:
|
||||
logger.error(wording.get('processing_video_failed'), __name__)
|
||||
process_manager.end()
|
||||
return 1
|
||||
process_manager.end()
|
||||
return 0
|
||||
|
||||
|
||||
def is_process_stopping() -> bool:
|
||||
if process_manager.is_stopping():
|
||||
process_manager.end()
|
||||
logger.info(wording.get('processing_stopped'), __name__)
|
||||
return process_manager.is_pending()
|
||||
return 'image-to-image'
|
||||
|
||||
@@ -1,27 +1,32 @@
|
||||
import itertools
|
||||
import shutil
|
||||
from typing import List
|
||||
|
||||
from facefusion import metadata
|
||||
from facefusion.types import Commands
|
||||
from facefusion.types import Command
|
||||
|
||||
|
||||
def run(commands : Commands) -> Commands:
|
||||
def run(commands : List[Command]) -> List[Command]:
|
||||
user_agent = metadata.get('name') + '/' + metadata.get('version')
|
||||
|
||||
return [ shutil.which('curl'), '--user-agent', user_agent, '--insecure', '--location', '--silent' ] + commands
|
||||
return [ shutil.which('curl'), '--user-agent', user_agent, '--location', '--silent', '--ssl-no-revoke' ] + commands
|
||||
|
||||
|
||||
def chain(*commands : Commands) -> Commands:
|
||||
def chain(*commands : List[Command]) -> List[Command]:
|
||||
return list(itertools.chain(*commands))
|
||||
|
||||
|
||||
def head(url : str) -> Commands:
|
||||
def ping(url : str) -> List[Command]:
|
||||
return [ '-I', url ]
|
||||
|
||||
|
||||
def download(url : str, download_file_path : str) -> Commands:
|
||||
def download(url : str, download_file_path : str) -> List[Command]:
|
||||
return [ '--create-dirs', '--continue-at', '-', '--output', download_file_path, url ]
|
||||
|
||||
|
||||
def set_timeout(timeout : int) -> Commands:
|
||||
def set_timeout(timeout : int) -> List[Command]:
|
||||
return [ '--connect-timeout', str(timeout) ]
|
||||
|
||||
|
||||
def set_retry(retry : int) -> List[Command]:
|
||||
return [ '--retry', str(retry) ]
|
||||
|
||||
+15
-14
@@ -7,13 +7,13 @@ from urllib.parse import urlparse
|
||||
from tqdm import tqdm
|
||||
|
||||
import facefusion.choices
|
||||
from facefusion import curl_builder, logger, process_manager, state_manager, wording
|
||||
from facefusion import curl_builder, logger, process_manager, state_manager, translator
|
||||
from facefusion.filesystem import get_file_name, get_file_size, is_file, remove_file
|
||||
from facefusion.hash_helper import validate_hash
|
||||
from facefusion.types import Commands, DownloadProvider, DownloadSet
|
||||
from facefusion.types import Buffer, Command, DownloadProvider, DownloadSet
|
||||
|
||||
|
||||
def open_curl(commands : Commands) -> subprocess.Popen[bytes]:
|
||||
def open_curl(commands : List[Command]) -> subprocess.Popen[Buffer]:
|
||||
commands = curl_builder.run(commands)
|
||||
return subprocess.Popen(commands, stdin = subprocess.PIPE, stdout = subprocess.PIPE)
|
||||
|
||||
@@ -26,10 +26,11 @@ def conditional_download(download_directory_path : str, urls : List[str]) -> Non
|
||||
download_size = get_static_download_size(url)
|
||||
|
||||
if initial_size < download_size:
|
||||
with tqdm(total = download_size, initial = initial_size, desc = wording.get('downloading'), unit = 'B', unit_scale = True, unit_divisor = 1024, ascii = ' =', disable = state_manager.get_item('log_level') in [ 'warn', 'error' ]) as progress:
|
||||
with tqdm(total = download_size, initial = initial_size, desc = translator.get('downloading'), unit = 'B', unit_scale = True, unit_divisor = 1024, ascii = ' =', disable = state_manager.get_item('log_level') in [ 'warn', 'error' ]) as progress:
|
||||
commands = curl_builder.chain(
|
||||
curl_builder.download(url, download_file_path),
|
||||
curl_builder.set_timeout(10)
|
||||
curl_builder.set_timeout(5),
|
||||
curl_builder.set_retry(5)
|
||||
)
|
||||
open_curl(commands)
|
||||
current_size = initial_size
|
||||
@@ -41,10 +42,10 @@ def conditional_download(download_directory_path : str, urls : List[str]) -> Non
|
||||
progress.update(current_size - progress.n)
|
||||
|
||||
|
||||
@lru_cache(maxsize = None)
|
||||
@lru_cache(maxsize = 64)
|
||||
def get_static_download_size(url : str) -> int:
|
||||
commands = curl_builder.chain(
|
||||
curl_builder.head(url),
|
||||
curl_builder.ping(url),
|
||||
curl_builder.set_timeout(5)
|
||||
)
|
||||
process = open_curl(commands)
|
||||
@@ -59,10 +60,10 @@ def get_static_download_size(url : str) -> int:
|
||||
return 0
|
||||
|
||||
|
||||
@lru_cache(maxsize = None)
|
||||
@lru_cache(maxsize = 64)
|
||||
def ping_static_url(url : str) -> bool:
|
||||
commands = curl_builder.chain(
|
||||
curl_builder.head(url),
|
||||
curl_builder.ping(url),
|
||||
curl_builder.set_timeout(5)
|
||||
)
|
||||
process = open_curl(commands)
|
||||
@@ -87,10 +88,10 @@ def conditional_download_hashes(hash_set : DownloadSet) -> bool:
|
||||
|
||||
for valid_hash_path in valid_hash_paths:
|
||||
valid_hash_file_name = get_file_name(valid_hash_path)
|
||||
logger.debug(wording.get('validating_hash_succeed').format(hash_file_name = valid_hash_file_name), __name__)
|
||||
logger.debug(translator.get('validating_hash_succeeded').format(hash_file_name = valid_hash_file_name), __name__)
|
||||
for invalid_hash_path in invalid_hash_paths:
|
||||
invalid_hash_file_name = get_file_name(invalid_hash_path)
|
||||
logger.error(wording.get('validating_hash_failed').format(hash_file_name = invalid_hash_file_name), __name__)
|
||||
logger.error(translator.get('validating_hash_failed').format(hash_file_name = invalid_hash_file_name), __name__)
|
||||
|
||||
if not invalid_hash_paths:
|
||||
process_manager.end()
|
||||
@@ -114,13 +115,13 @@ def conditional_download_sources(source_set : DownloadSet) -> bool:
|
||||
|
||||
for valid_source_path in valid_source_paths:
|
||||
valid_source_file_name = get_file_name(valid_source_path)
|
||||
logger.debug(wording.get('validating_source_succeed').format(source_file_name = valid_source_file_name), __name__)
|
||||
logger.debug(translator.get('validating_source_succeeded').format(source_file_name = valid_source_file_name), __name__)
|
||||
for invalid_source_path in invalid_source_paths:
|
||||
invalid_source_file_name = get_file_name(invalid_source_path)
|
||||
logger.error(wording.get('validating_source_failed').format(source_file_name = invalid_source_file_name), __name__)
|
||||
logger.error(translator.get('validating_source_failed').format(source_file_name = invalid_source_file_name), __name__)
|
||||
|
||||
if remove_file(invalid_source_path):
|
||||
logger.error(wording.get('deleting_corrupt_source').format(source_file_name = invalid_source_file_name), __name__)
|
||||
logger.error(translator.get('deleting_corrupt_source').format(source_file_name = invalid_source_file_name), __name__)
|
||||
|
||||
if not invalid_source_paths:
|
||||
process_manager.end()
|
||||
|
||||
+81
-104
@@ -1,15 +1,15 @@
|
||||
import shutil
|
||||
import subprocess
|
||||
import xml.etree.ElementTree as ElementTree
|
||||
import os
|
||||
from functools import lru_cache
|
||||
from typing import List, Optional
|
||||
from typing import List, Tuple
|
||||
|
||||
from onnxruntime import get_available_providers, set_default_logger_severity
|
||||
import onnxruntime
|
||||
|
||||
import facefusion.choices
|
||||
from facefusion.types import ExecutionDevice, ExecutionProvider, InferenceSessionProvider, ValueAndUnit
|
||||
from facefusion.filesystem import create_directory, is_directory
|
||||
from facefusion.system import detect_graphic_devices
|
||||
from facefusion.types import ExecutionProvider, InferenceOptionSet, InferenceProvider
|
||||
|
||||
set_default_logger_severity(3)
|
||||
onnxruntime.set_default_logger_severity(3)
|
||||
|
||||
|
||||
def has_execution_provider(execution_provider : ExecutionProvider) -> bool:
|
||||
@@ -17,7 +17,7 @@ def has_execution_provider(execution_provider : ExecutionProvider) -> bool:
|
||||
|
||||
|
||||
def get_available_execution_providers() -> List[ExecutionProvider]:
|
||||
inference_session_providers = get_available_providers()
|
||||
inference_session_providers = onnxruntime.get_available_providers()
|
||||
available_execution_providers : List[ExecutionProvider] = []
|
||||
|
||||
for execution_provider, execution_provider_value in facefusion.choices.execution_provider_set.items():
|
||||
@@ -28,129 +28,106 @@ def get_available_execution_providers() -> List[ExecutionProvider]:
|
||||
return available_execution_providers
|
||||
|
||||
|
||||
def create_inference_session_providers(execution_device_id : str, execution_providers : List[ExecutionProvider]) -> List[InferenceSessionProvider]:
|
||||
inference_session_providers : List[InferenceSessionProvider] = []
|
||||
def create_inference_providers(execution_device_id : int, execution_providers : List[ExecutionProvider]) -> List[InferenceProvider]:
|
||||
inference_providers : List[InferenceProvider] = []
|
||||
cache_path = resolve_cache_path()
|
||||
|
||||
for execution_provider in execution_providers:
|
||||
if execution_provider == 'cuda':
|
||||
inference_session_providers.append((facefusion.choices.execution_provider_set.get(execution_provider),
|
||||
inference_providers.append((facefusion.choices.execution_provider_set.get(execution_provider),
|
||||
{
|
||||
'device_id': execution_device_id,
|
||||
'cudnn_conv_algo_search': resolve_cudnn_conv_algo_search()
|
||||
'cudnn_conv_algo_search': resolve_static_cudnn_conv_algo_search(tuple(execution_providers))
|
||||
}))
|
||||
|
||||
if execution_provider == 'tensorrt':
|
||||
inference_session_providers.append((facefusion.choices.execution_provider_set.get(execution_provider),
|
||||
inference_option_set : InferenceOptionSet =\
|
||||
{
|
||||
'device_id': execution_device_id,
|
||||
'trt_engine_cache_enable': True,
|
||||
'trt_engine_cache_path': '.caches',
|
||||
'trt_timing_cache_enable': True,
|
||||
'trt_timing_cache_path': '.caches',
|
||||
'trt_builder_optimization_level': 5
|
||||
}))
|
||||
'device_id': execution_device_id
|
||||
}
|
||||
if is_directory(cache_path) or create_directory(cache_path):
|
||||
inference_option_set.update(
|
||||
{
|
||||
'trt_engine_cache_enable': True,
|
||||
'trt_engine_cache_path': cache_path,
|
||||
'trt_timing_cache_enable': True,
|
||||
'trt_timing_cache_path': cache_path,
|
||||
'trt_builder_optimization_level': 4
|
||||
})
|
||||
inference_providers.append((facefusion.choices.execution_provider_set.get(execution_provider), inference_option_set))
|
||||
|
||||
if execution_provider in [ 'directml', 'rocm' ]:
|
||||
inference_session_providers.append((facefusion.choices.execution_provider_set.get(execution_provider),
|
||||
inference_providers.append((facefusion.choices.execution_provider_set.get(execution_provider),
|
||||
{
|
||||
'device_id': execution_device_id
|
||||
}))
|
||||
|
||||
if execution_provider == 'migraphx':
|
||||
inference_option_set =\
|
||||
{
|
||||
'device_id': execution_device_id
|
||||
}
|
||||
if is_directory(cache_path) or create_directory(cache_path):
|
||||
inference_option_set.update(
|
||||
{
|
||||
'migraphx_model_cache_dir': cache_path
|
||||
})
|
||||
inference_providers.append((facefusion.choices.execution_provider_set.get(execution_provider), inference_option_set))
|
||||
|
||||
if execution_provider == 'coreml':
|
||||
inference_option_set =\
|
||||
{
|
||||
'SpecializationStrategy': 'FastPrediction'
|
||||
}
|
||||
if is_directory(cache_path) or create_directory(cache_path):
|
||||
inference_option_set.update(
|
||||
{
|
||||
'ModelCacheDirectory': cache_path
|
||||
})
|
||||
inference_providers.append((facefusion.choices.execution_provider_set.get(execution_provider), inference_option_set))
|
||||
|
||||
if execution_provider == 'openvino':
|
||||
inference_session_providers.append((facefusion.choices.execution_provider_set.get(execution_provider),
|
||||
inference_providers.append((facefusion.choices.execution_provider_set.get(execution_provider),
|
||||
{
|
||||
'device_type': resolve_openvino_device_type(execution_device_id),
|
||||
'precision': 'FP32'
|
||||
}))
|
||||
if execution_provider == 'coreml':
|
||||
inference_session_providers.append((facefusion.choices.execution_provider_set.get(execution_provider),
|
||||
|
||||
if execution_provider == 'qnn':
|
||||
inference_providers.append((facefusion.choices.execution_provider_set.get(execution_provider),
|
||||
{
|
||||
'SpecializationStrategy': 'FastPrediction',
|
||||
'ModelCacheDirectory': '.caches'
|
||||
'device_id': execution_device_id,
|
||||
'backend_type': 'htp'
|
||||
}))
|
||||
|
||||
if 'cpu' in execution_providers:
|
||||
inference_session_providers.append(facefusion.choices.execution_provider_set.get('cpu'))
|
||||
inference_providers.append(facefusion.choices.execution_provider_set.get('cpu'))
|
||||
|
||||
return inference_session_providers
|
||||
return inference_providers
|
||||
|
||||
|
||||
def resolve_cudnn_conv_algo_search() -> str:
|
||||
execution_devices = detect_static_execution_devices()
|
||||
product_names = ('GeForce GTX 1630', 'GeForce GTX 1650', 'GeForce GTX 1660')
|
||||
def resolve_cache_path() -> str:
|
||||
return os.path.join('.caches', onnxruntime.get_version_string())
|
||||
|
||||
for execution_device in execution_devices:
|
||||
if execution_device.get('product').get('name').startswith(product_names):
|
||||
return 'DEFAULT'
|
||||
|
||||
@lru_cache()
|
||||
def resolve_static_cudnn_conv_algo_search(execution_providers : Tuple[ExecutionProvider, ...]) -> str:
|
||||
return resolve_cudnn_conv_algo_search(list(execution_providers))
|
||||
|
||||
|
||||
def resolve_cudnn_conv_algo_search(execution_providers : List[ExecutionProvider]) -> str:
|
||||
if has_execution_provider('cuda') or has_execution_provider('tensorrt'):
|
||||
graphic_devices = detect_graphic_devices(execution_providers)
|
||||
product_names = ('GeForce GTX 1630', 'GeForce GTX 1650', 'GeForce GTX 1660')
|
||||
|
||||
for graphic_device in graphic_devices:
|
||||
if graphic_device.get('product').get('name').startswith(product_names):
|
||||
return 'DEFAULT'
|
||||
|
||||
return 'EXHAUSTIVE'
|
||||
|
||||
|
||||
def resolve_openvino_device_type(execution_device_id : str) -> str:
|
||||
if execution_device_id == '0':
|
||||
def resolve_openvino_device_type(execution_device_id : int) -> str:
|
||||
if execution_device_id == 0:
|
||||
return 'GPU'
|
||||
if execution_device_id == '∞':
|
||||
return 'MULTI:GPU'
|
||||
return 'GPU.' + execution_device_id
|
||||
|
||||
|
||||
def run_nvidia_smi() -> subprocess.Popen[bytes]:
|
||||
commands = [ shutil.which('nvidia-smi'), '--query', '--xml-format' ]
|
||||
return subprocess.Popen(commands, stdout = subprocess.PIPE)
|
||||
|
||||
|
||||
@lru_cache(maxsize = None)
|
||||
def detect_static_execution_devices() -> List[ExecutionDevice]:
|
||||
return detect_execution_devices()
|
||||
|
||||
|
||||
def detect_execution_devices() -> List[ExecutionDevice]:
|
||||
execution_devices : List[ExecutionDevice] = []
|
||||
|
||||
try:
|
||||
output, _ = run_nvidia_smi().communicate()
|
||||
root_element = ElementTree.fromstring(output)
|
||||
except Exception:
|
||||
root_element = ElementTree.Element('xml')
|
||||
|
||||
for gpu_element in root_element.findall('gpu'):
|
||||
execution_devices.append(
|
||||
{
|
||||
'driver_version': root_element.findtext('driver_version'),
|
||||
'framework':
|
||||
{
|
||||
'name': 'CUDA',
|
||||
'version': root_element.findtext('cuda_version')
|
||||
},
|
||||
'product':
|
||||
{
|
||||
'vendor': 'NVIDIA',
|
||||
'name': gpu_element.findtext('product_name').replace('NVIDIA', '').strip()
|
||||
},
|
||||
'video_memory':
|
||||
{
|
||||
'total': create_value_and_unit(gpu_element.findtext('fb_memory_usage/total')),
|
||||
'free': create_value_and_unit(gpu_element.findtext('fb_memory_usage/free'))
|
||||
},
|
||||
'temperature':
|
||||
{
|
||||
'gpu': create_value_and_unit(gpu_element.findtext('temperature/gpu_temp')),
|
||||
'memory': create_value_and_unit(gpu_element.findtext('temperature/memory_temp'))
|
||||
},
|
||||
'utilization':
|
||||
{
|
||||
'gpu': create_value_and_unit(gpu_element.findtext('utilization/gpu_util')),
|
||||
'memory': create_value_and_unit(gpu_element.findtext('utilization/memory_util'))
|
||||
}
|
||||
})
|
||||
|
||||
return execution_devices
|
||||
|
||||
|
||||
def create_value_and_unit(text : str) -> Optional[ValueAndUnit]:
|
||||
if ' ' in text:
|
||||
value, unit = text.split()
|
||||
|
||||
return\
|
||||
{
|
||||
'value': int(value),
|
||||
'unit': str(unit)
|
||||
}
|
||||
return None
|
||||
return 'GPU.' + str(execution_device_id)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import os
|
||||
import signal
|
||||
import sys
|
||||
from time import sleep
|
||||
@@ -8,8 +9,11 @@ from facefusion.temp_helper import clear_temp_directory
|
||||
from facefusion.types import ErrorCode
|
||||
|
||||
|
||||
def fatal_exit(error_code : ErrorCode) -> None:
|
||||
os._exit(error_code)
|
||||
|
||||
|
||||
def hard_exit(error_code : ErrorCode) -> None:
|
||||
signal.signal(signal.SIGINT, signal.SIG_IGN)
|
||||
sys.exit(error_code)
|
||||
|
||||
|
||||
@@ -18,9 +22,13 @@ def signal_exit(signum : int, frame : FrameType) -> None:
|
||||
|
||||
|
||||
def graceful_exit(error_code : ErrorCode) -> None:
|
||||
signal.signal(signal.SIGINT, signal.SIG_IGN)
|
||||
process_manager.stop()
|
||||
|
||||
while process_manager.is_processing():
|
||||
sleep(0.5)
|
||||
if state_manager.get_item('target_path'):
|
||||
clear_temp_directory(state_manager.get_item('target_path'))
|
||||
|
||||
if state_manager.get_item('output_path'):
|
||||
clear_temp_directory(state_manager.get_temp_path(), state_manager.get_item('output_path'))
|
||||
|
||||
hard_exit(error_code)
|
||||
|
||||
@@ -1,124 +0,0 @@
|
||||
from typing import List, Optional
|
||||
|
||||
import numpy
|
||||
|
||||
from facefusion import state_manager
|
||||
from facefusion.common_helper import get_first
|
||||
from facefusion.face_classifier import classify_face
|
||||
from facefusion.face_detector import detect_faces, detect_rotated_faces
|
||||
from facefusion.face_helper import apply_nms, 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_recognizer import calc_embedding
|
||||
from facefusion.face_store import get_static_faces, set_static_faces
|
||||
from facefusion.types import BoundingBox, Face, FaceLandmark5, FaceLandmarkSet, FaceScoreSet, Score, VisionFrame
|
||||
|
||||
|
||||
def create_faces(vision_frame : VisionFrame, bounding_boxes : List[BoundingBox], face_scores : List[Score], face_landmarks_5 : List[FaceLandmark5]) -> List[Face]:
|
||||
faces = []
|
||||
nms_threshold = get_nms_threshold(state_manager.get_item('face_detector_model'), state_manager.get_item('face_detector_angles'))
|
||||
keep_indices = apply_nms(bounding_boxes, face_scores, state_manager.get_item('face_detector_score'), nms_threshold)
|
||||
|
||||
for index in keep_indices:
|
||||
bounding_box = bounding_boxes[index]
|
||||
face_score = face_scores[index]
|
||||
face_landmark_5 = face_landmarks_5[index]
|
||||
face_landmark_5_68 = face_landmark_5
|
||||
face_landmark_68_5 = estimate_face_landmark_68_5(face_landmark_5_68)
|
||||
face_landmark_68 = face_landmark_68_5
|
||||
face_landmark_score_68 = 0.0
|
||||
face_angle = estimate_face_angle(face_landmark_68_5)
|
||||
|
||||
if state_manager.get_item('face_landmarker_score') > 0:
|
||||
face_landmark_68, face_landmark_score_68 = detect_face_landmark(vision_frame, bounding_box, face_angle)
|
||||
if face_landmark_score_68 > state_manager.get_item('face_landmarker_score'):
|
||||
face_landmark_5_68 = convert_to_face_landmark_5(face_landmark_68)
|
||||
|
||||
face_landmark_set : FaceLandmarkSet =\
|
||||
{
|
||||
'5': face_landmark_5,
|
||||
'5/68': face_landmark_5_68,
|
||||
'68': face_landmark_68,
|
||||
'68/5': face_landmark_68_5
|
||||
}
|
||||
face_score_set : FaceScoreSet =\
|
||||
{
|
||||
'detector': face_score,
|
||||
'landmarker': face_landmark_score_68
|
||||
}
|
||||
embedding, normed_embedding = calc_embedding(vision_frame, face_landmark_set.get('5/68'))
|
||||
gender, age, race = classify_face(vision_frame, face_landmark_set.get('5/68'))
|
||||
faces.append(Face(
|
||||
bounding_box = bounding_box,
|
||||
score_set = face_score_set,
|
||||
landmark_set = face_landmark_set,
|
||||
angle = face_angle,
|
||||
embedding = embedding,
|
||||
normed_embedding = normed_embedding,
|
||||
gender = gender,
|
||||
age = age,
|
||||
race = race
|
||||
))
|
||||
return faces
|
||||
|
||||
|
||||
def get_one_face(faces : List[Face], position : int = 0) -> Optional[Face]:
|
||||
if faces:
|
||||
position = min(position, len(faces) - 1)
|
||||
return faces[position]
|
||||
return None
|
||||
|
||||
|
||||
def get_average_face(faces : List[Face]) -> Optional[Face]:
|
||||
embeddings = []
|
||||
normed_embeddings = []
|
||||
|
||||
if faces:
|
||||
first_face = get_first(faces)
|
||||
|
||||
for face in faces:
|
||||
embeddings.append(face.embedding)
|
||||
normed_embeddings.append(face.normed_embedding)
|
||||
|
||||
return Face(
|
||||
bounding_box = first_face.bounding_box,
|
||||
score_set = first_face.score_set,
|
||||
landmark_set = first_face.landmark_set,
|
||||
angle = first_face.angle,
|
||||
embedding = numpy.mean(embeddings, axis = 0),
|
||||
normed_embedding = numpy.mean(normed_embeddings, axis = 0),
|
||||
gender = first_face.gender,
|
||||
age = first_face.age,
|
||||
race = first_face.race
|
||||
)
|
||||
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_rotated_faces(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
|
||||
@@ -11,12 +11,18 @@ from facefusion.thread_helper import conditional_thread_semaphore
|
||||
from facefusion.types import Age, DownloadScope, FaceLandmark5, Gender, InferencePool, ModelOptions, ModelSet, Race, VisionFrame
|
||||
|
||||
|
||||
@lru_cache(maxsize = None)
|
||||
@lru_cache()
|
||||
def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
||||
return\
|
||||
{
|
||||
'fairface':
|
||||
{
|
||||
'__metadata__':
|
||||
{
|
||||
'vendor': 'dchen236',
|
||||
'license': 'CC-BY-4.0',
|
||||
'year': 2021
|
||||
},
|
||||
'hashes':
|
||||
{
|
||||
'face_classifier':
|
||||
|
||||
@@ -0,0 +1,209 @@
|
||||
from typing import List, Optional
|
||||
|
||||
import numpy
|
||||
|
||||
from facefusion import face_store, state_manager
|
||||
from facefusion.common_helper import get_first, get_middle
|
||||
from facefusion.face_classifier import classify_face
|
||||
from facefusion.face_detector import detect_faces, detect_faces_by_angle
|
||||
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_recognizer import calculate_face_embedding
|
||||
from facefusion.types import BoundingBox, Face, FaceLandmark5, FaceLandmarkSet, FaceScoreSet, Score, VisionFrame
|
||||
|
||||
|
||||
def create_faces(vision_frame : VisionFrame, bounding_boxes : List[BoundingBox], face_scores : List[Score], face_landmarks_5 : List[FaceLandmark5]) -> List[Face]:
|
||||
faces = []
|
||||
nms_threshold = get_nms_threshold(state_manager.get_item('face_detector_model'), state_manager.get_item('face_detector_angles'))
|
||||
keep_indices = apply_nms(bounding_boxes, face_scores, state_manager.get_item('face_detector_score'), nms_threshold)
|
||||
|
||||
for index in keep_indices:
|
||||
bounding_box = bounding_boxes[index]
|
||||
face_score = face_scores[index]
|
||||
face_landmark_5 = face_landmarks_5[index]
|
||||
face_landmark_5_68 = face_landmark_5
|
||||
face_landmark_68_5 = estimate_face_landmark_68_5(face_landmark_5_68)
|
||||
face_landmark_68 = face_landmark_68_5
|
||||
face_landmark_score_68 = 0.0
|
||||
face_angle = estimate_face_angle(face_landmark_68_5)
|
||||
|
||||
if state_manager.get_item('face_landmarker_score') > 0:
|
||||
face_landmark_68, face_landmark_score_68 = detect_face_landmark(vision_frame, bounding_box, face_angle)
|
||||
if face_landmark_score_68 > state_manager.get_item('face_landmarker_score'):
|
||||
face_landmark_5_68 = convert_to_face_landmark_5(face_landmark_68)
|
||||
|
||||
face_landmark_set : FaceLandmarkSet =\
|
||||
{
|
||||
'5': face_landmark_5,
|
||||
'5/68': face_landmark_5_68,
|
||||
'68': face_landmark_68,
|
||||
'68/5': face_landmark_68_5
|
||||
}
|
||||
face_score_set : FaceScoreSet =\
|
||||
{
|
||||
'detector': face_score,
|
||||
'landmarker': face_landmark_score_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'))
|
||||
|
||||
faces.append(Face(
|
||||
origin = 'detect',
|
||||
bounding_box = bounding_box,
|
||||
score_set = face_score_set,
|
||||
landmark_set = face_landmark_set,
|
||||
angle = face_angle,
|
||||
embedding = face_embedding,
|
||||
embedding_norm = face_embedding_norm,
|
||||
gender = gender,
|
||||
age = age,
|
||||
race = race
|
||||
))
|
||||
return faces
|
||||
|
||||
|
||||
def get_one_face(faces : List[Face], position : int = 0) -> Optional[Face]:
|
||||
if faces:
|
||||
position = min(position, len(faces) - 1)
|
||||
return faces[position]
|
||||
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):
|
||||
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_norm = []
|
||||
|
||||
if faces:
|
||||
first_face = get_first(faces)
|
||||
|
||||
for face in faces:
|
||||
face_embeddings.append(face.embedding)
|
||||
face_embeddings_norm.append(face.embedding_norm)
|
||||
|
||||
return Face(
|
||||
origin = first_face.origin,
|
||||
bounding_box = first_face.bounding_box,
|
||||
score_set = first_face.score_set,
|
||||
landmark_set = first_face.landmark_set,
|
||||
angle = first_face.angle,
|
||||
embedding = numpy.mean(face_embeddings, axis = 0),
|
||||
embedding_norm = numpy.mean(face_embeddings_norm, axis = 0),
|
||||
gender = first_face.gender,
|
||||
age = first_face.age,
|
||||
race = first_face.race
|
||||
)
|
||||
return None
|
||||
|
||||
|
||||
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_y = temp_vision_frame.shape[0] / target_vision_frame.shape[0]
|
||||
|
||||
bounding_box = target_face.bounding_box * [ scale_x, scale_y, scale_x, scale_y ]
|
||||
landmark_set =\
|
||||
{
|
||||
'5': target_face.landmark_set.get('5') * numpy.array([ scale_x, scale_y ]),
|
||||
'5/68': target_face.landmark_set.get('5/68') * numpy.array([ scale_x, scale_y ]),
|
||||
'68': target_face.landmark_set.get('68') * numpy.array([ scale_x, scale_y ]),
|
||||
'68/5': target_face.landmark_set.get('68/5') * numpy.array([ scale_x, scale_y ])
|
||||
}
|
||||
|
||||
return target_face._replace(
|
||||
bounding_box = bounding_box,
|
||||
landmark_set = landmark_set
|
||||
)
|
||||
+156
-21
@@ -6,19 +6,25 @@ import numpy
|
||||
|
||||
from facefusion import inference_manager, state_manager
|
||||
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
|
||||
from facefusion.face_helper import create_rotated_matrix_and_size, create_static_anchors, distance_to_bounding_box, distance_to_face_landmark_5, normalize_bounding_box, transform_bounding_box, transform_points
|
||||
from facefusion.face_helper import create_rotation_matrix_and_size, create_static_anchors, distance_to_bounding_box, distance_to_face_landmark_5, normalize_bounding_box, transform_bounding_box, transform_points
|
||||
from facefusion.filesystem import resolve_relative_path
|
||||
from facefusion.thread_helper import thread_semaphore
|
||||
from facefusion.types import Angle, BoundingBox, Detection, DownloadScope, DownloadSet, FaceLandmark5, InferencePool, ModelSet, Score, VisionFrame
|
||||
from facefusion.types import Angle, BoundingBox, Detection, DownloadScope, DownloadSet, FaceLandmark5, InferencePool, Margin, ModelSet, Score, VisionFrame
|
||||
from facefusion.vision import restrict_frame, unpack_resolution
|
||||
|
||||
|
||||
@lru_cache(maxsize = None)
|
||||
@lru_cache()
|
||||
def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
||||
return\
|
||||
{
|
||||
'retinaface':
|
||||
{
|
||||
'__metadata__':
|
||||
{
|
||||
'vendor': 'InsightFace',
|
||||
'license': 'Non-Commercial',
|
||||
'year': 2020
|
||||
},
|
||||
'hashes':
|
||||
{
|
||||
'retinaface':
|
||||
@@ -38,6 +44,12 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
||||
},
|
||||
'scrfd':
|
||||
{
|
||||
'__metadata__':
|
||||
{
|
||||
'vendor': 'InsightFace',
|
||||
'license': 'Non-Commercial',
|
||||
'year': 2021
|
||||
},
|
||||
'hashes':
|
||||
{
|
||||
'scrfd':
|
||||
@@ -57,6 +69,12 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
||||
},
|
||||
'yolo_face':
|
||||
{
|
||||
'__metadata__':
|
||||
{
|
||||
'vendor': 'derronqi',
|
||||
'license': 'GPL-3.0',
|
||||
'year': 2022
|
||||
},
|
||||
'hashes':
|
||||
{
|
||||
'yolo_face':
|
||||
@@ -73,6 +91,31 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
||||
'path': resolve_relative_path('../.assets/models/yoloface_8n.onnx')
|
||||
}
|
||||
}
|
||||
},
|
||||
'yunet':
|
||||
{
|
||||
'__metadata__':
|
||||
{
|
||||
'vendor': 'OpenCV',
|
||||
'license': 'MIT',
|
||||
'year': 2023
|
||||
},
|
||||
'hashes':
|
||||
{
|
||||
'yunet':
|
||||
{
|
||||
'url': resolve_download_url('models-3.4.0', 'yunet_2023_mar.hash'),
|
||||
'path': resolve_relative_path('../.assets/models/yunet_2023_mar.hash')
|
||||
}
|
||||
},
|
||||
'sources':
|
||||
{
|
||||
'yunet':
|
||||
{
|
||||
'url': resolve_download_url('models-3.4.0', 'yunet_2023_mar.onnx'),
|
||||
'path': resolve_relative_path('../.assets/models/yunet_2023_mar.onnx')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,7 +137,7 @@ def collect_model_downloads() -> Tuple[DownloadSet, DownloadSet]:
|
||||
model_hash_set = {}
|
||||
model_source_set = {}
|
||||
|
||||
for face_detector_model in [ 'retinaface', 'scrfd', 'yolo_face' ]:
|
||||
for face_detector_model in [ 'retinaface', 'scrfd', 'yolo_face', 'yunet' ]:
|
||||
if state_manager.get_item('face_detector_model') in [ 'many', face_detector_model ]:
|
||||
model_hash_set[face_detector_model] = model_set.get(face_detector_model).get('hashes').get(face_detector_model)
|
||||
model_source_set[face_detector_model] = model_set.get(face_detector_model).get('sources').get(face_detector_model)
|
||||
@@ -109,39 +152,56 @@ def pre_check() -> bool:
|
||||
|
||||
|
||||
def detect_faces(vision_frame : VisionFrame) -> Tuple[List[BoundingBox], List[Score], List[FaceLandmark5]]:
|
||||
margin_top, margin_right, margin_bottom, margin_left = prepare_margin(vision_frame)
|
||||
margin_vision_frame = numpy.pad(vision_frame, ((margin_top, margin_bottom), (margin_left, margin_right), (0, 0)))
|
||||
all_bounding_boxes : List[BoundingBox] = []
|
||||
all_face_scores : List[Score] = []
|
||||
all_face_landmarks_5 : List[FaceLandmark5] = []
|
||||
|
||||
if state_manager.get_item('face_detector_model') in [ 'many', 'retinaface' ]:
|
||||
bounding_boxes, face_scores, face_landmarks_5 = detect_with_retinaface(vision_frame, state_manager.get_item('face_detector_size'))
|
||||
bounding_boxes, face_scores, face_landmarks_5 = detect_with_retinaface(margin_vision_frame, state_manager.get_item('face_detector_size'))
|
||||
all_bounding_boxes.extend(bounding_boxes)
|
||||
all_face_scores.extend(face_scores)
|
||||
all_face_landmarks_5.extend(face_landmarks_5)
|
||||
|
||||
if state_manager.get_item('face_detector_model') in [ 'many', 'scrfd' ]:
|
||||
bounding_boxes, face_scores, face_landmarks_5 = detect_with_scrfd(vision_frame, state_manager.get_item('face_detector_size'))
|
||||
bounding_boxes, face_scores, face_landmarks_5 = detect_with_scrfd(margin_vision_frame, state_manager.get_item('face_detector_size'))
|
||||
all_bounding_boxes.extend(bounding_boxes)
|
||||
all_face_scores.extend(face_scores)
|
||||
all_face_landmarks_5.extend(face_landmarks_5)
|
||||
|
||||
if state_manager.get_item('face_detector_model') in [ 'many', 'yolo_face' ]:
|
||||
bounding_boxes, face_scores, face_landmarks_5 = detect_with_yolo_face(vision_frame, state_manager.get_item('face_detector_size'))
|
||||
bounding_boxes, face_scores, face_landmarks_5 = detect_with_yolo_face(margin_vision_frame, state_manager.get_item('face_detector_size'))
|
||||
all_bounding_boxes.extend(bounding_boxes)
|
||||
all_face_scores.extend(face_scores)
|
||||
all_face_landmarks_5.extend(face_landmarks_5)
|
||||
|
||||
all_bounding_boxes = [ normalize_bounding_box(all_bounding_box) for all_bounding_box in all_bounding_boxes ]
|
||||
if state_manager.get_item('face_detector_model') == 'yunet':
|
||||
bounding_boxes, face_scores, face_landmarks_5 = detect_with_yunet(margin_vision_frame, state_manager.get_item('face_detector_size'))
|
||||
all_bounding_boxes.extend(bounding_boxes)
|
||||
all_face_scores.extend(face_scores)
|
||||
all_face_landmarks_5.extend(face_landmarks_5)
|
||||
|
||||
all_bounding_boxes = [ normalize_bounding_box(all_bounding_box) - numpy.array([ margin_left, margin_top, margin_left, margin_top ]) for all_bounding_box in all_bounding_boxes ]
|
||||
all_face_landmarks_5 = [ all_face_landmark_5 - numpy.array([ margin_left, margin_top ]) for all_face_landmark_5 in all_face_landmarks_5 ]
|
||||
return all_bounding_boxes, all_face_scores, all_face_landmarks_5
|
||||
|
||||
|
||||
def detect_rotated_faces(vision_frame : VisionFrame, angle : Angle) -> Tuple[List[BoundingBox], List[Score], List[FaceLandmark5]]:
|
||||
rotated_matrix, rotated_size = create_rotated_matrix_and_size(angle, vision_frame.shape[:2][::-1])
|
||||
rotated_vision_frame = cv2.warpAffine(vision_frame, rotated_matrix, rotated_size)
|
||||
rotated_inverse_matrix = cv2.invertAffineTransform(rotated_matrix)
|
||||
bounding_boxes, face_scores, face_landmarks_5 = detect_faces(rotated_vision_frame)
|
||||
bounding_boxes = [ transform_bounding_box(bounding_box, rotated_inverse_matrix) for bounding_box in bounding_boxes ]
|
||||
face_landmarks_5 = [ transform_points(face_landmark_5, rotated_inverse_matrix) for face_landmark_5 in face_landmarks_5 ]
|
||||
def prepare_margin(vision_frame : VisionFrame) -> Margin:
|
||||
margin_top = int(vision_frame.shape[0] * numpy.interp(state_manager.get_item('face_detector_margin')[0], [ 0, 100 ], [ 0, 0.5 ]))
|
||||
margin_right = int(vision_frame.shape[1] * numpy.interp(state_manager.get_item('face_detector_margin')[1], [ 0, 100 ], [ 0, 0.5 ]))
|
||||
margin_bottom = int(vision_frame.shape[0] * numpy.interp(state_manager.get_item('face_detector_margin')[2], [ 0, 100 ], [ 0, 0.5 ]))
|
||||
margin_left = int(vision_frame.shape[1] * numpy.interp(state_manager.get_item('face_detector_margin')[3], [ 0, 100 ], [ 0, 0.5 ]))
|
||||
return margin_top, margin_right, margin_bottom, margin_left
|
||||
|
||||
|
||||
def detect_faces_by_angle(vision_frame : VisionFrame, face_angle : Angle) -> Tuple[List[BoundingBox], List[Score], List[FaceLandmark5]]:
|
||||
rotation_matrix, rotation_size = create_rotation_matrix_and_size(face_angle, vision_frame.shape[:2][::-1])
|
||||
rotation_vision_frame = cv2.warpAffine(vision_frame, rotation_matrix, rotation_size)
|
||||
rotation_inverse_matrix = cv2.invertAffineTransform(rotation_matrix)
|
||||
bounding_boxes, face_scores, face_landmarks_5 = detect_faces(rotation_vision_frame)
|
||||
bounding_boxes = [ transform_bounding_box(bounding_box, rotation_inverse_matrix) for bounding_box in bounding_boxes ]
|
||||
face_landmarks_5 = [ transform_points(face_landmark_5, rotation_inverse_matrix) for face_landmark_5 in face_landmarks_5 ]
|
||||
return bounding_boxes, face_scores, face_landmarks_5
|
||||
|
||||
|
||||
@@ -162,12 +222,13 @@ def detect_with_retinaface(vision_frame : VisionFrame, face_detector_size : str)
|
||||
detection = forward_with_retinaface(detect_vision_frame)
|
||||
|
||||
for index, feature_stride in enumerate(feature_strides):
|
||||
keep_indices = numpy.where(detection[index] >= face_detector_score)[0]
|
||||
face_scores_raw = detection[index]
|
||||
keep_indices = numpy.where(face_scores_raw >= face_detector_score)[0]
|
||||
|
||||
if numpy.any(keep_indices):
|
||||
stride_height = face_detector_height // feature_stride
|
||||
stride_width = face_detector_width // feature_stride
|
||||
anchors = create_static_anchors(feature_stride, anchor_total, stride_height, stride_width)
|
||||
anchors = create_static_anchors(feature_stride, anchor_total, stride_width, stride_height)
|
||||
bounding_boxes_raw = detection[index + feature_map_channel] * feature_stride
|
||||
face_landmarks_5_raw = detection[index + feature_map_channel * 2] * feature_stride
|
||||
|
||||
@@ -180,7 +241,7 @@ def detect_with_retinaface(vision_frame : VisionFrame, face_detector_size : str)
|
||||
bounding_box_raw[3] * ratio_height
|
||||
]))
|
||||
|
||||
for face_score_raw in detection[index][keep_indices]:
|
||||
for face_score_raw in face_scores_raw[keep_indices]:
|
||||
face_scores.append(face_score_raw[0])
|
||||
|
||||
for face_landmark_raw_5 in distance_to_face_landmark_5(anchors, face_landmarks_5_raw)[keep_indices]:
|
||||
@@ -206,12 +267,13 @@ def detect_with_scrfd(vision_frame : VisionFrame, face_detector_size : str) -> T
|
||||
detection = forward_with_scrfd(detect_vision_frame)
|
||||
|
||||
for index, feature_stride in enumerate(feature_strides):
|
||||
keep_indices = numpy.where(detection[index] >= face_detector_score)[0]
|
||||
face_scores_raw = detection[index]
|
||||
keep_indices = numpy.where(face_scores_raw >= face_detector_score)[0]
|
||||
|
||||
if numpy.any(keep_indices):
|
||||
stride_height = face_detector_height // feature_stride
|
||||
stride_width = face_detector_width // feature_stride
|
||||
anchors = create_static_anchors(feature_stride, anchor_total, stride_height, stride_width)
|
||||
anchors = create_static_anchors(feature_stride, anchor_total, stride_width, stride_height)
|
||||
bounding_boxes_raw = detection[index + feature_map_channel] * feature_stride
|
||||
face_landmarks_5_raw = detection[index + feature_map_channel * 2] * feature_stride
|
||||
|
||||
@@ -224,7 +286,7 @@ def detect_with_scrfd(vision_frame : VisionFrame, face_detector_size : str) -> T
|
||||
bounding_box_raw[3] * ratio_height
|
||||
]))
|
||||
|
||||
for face_score_raw in detection[index][keep_indices]:
|
||||
for face_score_raw in face_scores_raw[keep_indices]:
|
||||
face_scores.append(face_score_raw[0])
|
||||
|
||||
for face_landmark_raw_5 in distance_to_face_landmark_5(anchors, face_landmarks_5_raw)[keep_indices]:
|
||||
@@ -271,6 +333,67 @@ def detect_with_yolo_face(vision_frame : VisionFrame, face_detector_size : str)
|
||||
return bounding_boxes, face_scores, face_landmarks_5
|
||||
|
||||
|
||||
def detect_with_yunet(vision_frame : VisionFrame, face_detector_size : str) -> Tuple[List[BoundingBox], List[Score], List[FaceLandmark5]]:
|
||||
bounding_boxes = []
|
||||
face_scores = []
|
||||
face_landmarks_5 = []
|
||||
feature_strides = [ 8, 16, 32 ]
|
||||
feature_map_channel = 3
|
||||
anchor_total = 1
|
||||
face_detector_score = state_manager.get_item('face_detector_score')
|
||||
face_detector_width, face_detector_height = unpack_resolution(face_detector_size)
|
||||
temp_vision_frame = restrict_frame(vision_frame, (face_detector_width, face_detector_height))
|
||||
ratio_height = vision_frame.shape[0] / temp_vision_frame.shape[0]
|
||||
ratio_width = vision_frame.shape[1] / temp_vision_frame.shape[1]
|
||||
detect_vision_frame = prepare_detect_frame(temp_vision_frame, face_detector_size)
|
||||
detect_vision_frame = normalize_detect_frame(detect_vision_frame, [ 0, 255 ])
|
||||
detection = forward_with_yunet(detect_vision_frame)
|
||||
|
||||
for index, feature_stride in enumerate(feature_strides):
|
||||
face_scores_raw = (detection[index] * detection[index + feature_map_channel]).reshape(-1)
|
||||
keep_indices = numpy.where(face_scores_raw >= face_detector_score)[0]
|
||||
|
||||
if numpy.any(keep_indices):
|
||||
stride_height = face_detector_height // feature_stride
|
||||
stride_width = face_detector_width // feature_stride
|
||||
anchors = create_static_anchors(feature_stride, anchor_total, stride_width, stride_height)
|
||||
bounding_boxes_center = detection[index + feature_map_channel * 2].squeeze(0)[:, :2] * feature_stride + anchors
|
||||
bounding_boxes_size = numpy.exp(detection[index + feature_map_channel * 2].squeeze(0)[:, 2:4]) * feature_stride
|
||||
face_landmarks_5_raw = detection[index + feature_map_channel * 3].squeeze(0)
|
||||
|
||||
bounding_boxes_raw = numpy.stack(
|
||||
[
|
||||
bounding_boxes_center[:, 0] - bounding_boxes_size[:, 0] / 2,
|
||||
bounding_boxes_center[:, 1] - bounding_boxes_size[:, 1] / 2,
|
||||
bounding_boxes_center[:, 0] + bounding_boxes_size[:, 0] / 2,
|
||||
bounding_boxes_center[:, 1] + bounding_boxes_size[:, 1] / 2
|
||||
], axis = -1)
|
||||
|
||||
for bounding_box_raw in bounding_boxes_raw[keep_indices]:
|
||||
bounding_boxes.append(numpy.array(
|
||||
[
|
||||
bounding_box_raw[0] * ratio_width,
|
||||
bounding_box_raw[1] * ratio_height,
|
||||
bounding_box_raw[2] * ratio_width,
|
||||
bounding_box_raw[3] * ratio_height
|
||||
]))
|
||||
|
||||
face_scores.extend(face_scores_raw[keep_indices])
|
||||
face_landmarks_5_raw = numpy.concatenate(
|
||||
[
|
||||
face_landmarks_5_raw[:, [ 0, 1 ]] * feature_stride + anchors,
|
||||
face_landmarks_5_raw[:, [ 2, 3 ]] * feature_stride + anchors,
|
||||
face_landmarks_5_raw[:, [ 4, 5 ]] * feature_stride + anchors,
|
||||
face_landmarks_5_raw[:, [ 6, 7 ]] * feature_stride + anchors,
|
||||
face_landmarks_5_raw[:, [ 8, 9 ]] * feature_stride + anchors
|
||||
], axis = -1).reshape(-1, 5, 2)
|
||||
|
||||
for face_landmark_raw_5 in face_landmarks_5_raw[keep_indices]:
|
||||
face_landmarks_5.append(face_landmark_raw_5 * [ ratio_width, ratio_height ])
|
||||
|
||||
return bounding_boxes, face_scores, face_landmarks_5
|
||||
|
||||
|
||||
def forward_with_retinaface(detect_vision_frame : VisionFrame) -> Detection:
|
||||
face_detector = get_inference_pool().get('retinaface')
|
||||
|
||||
@@ -307,6 +430,18 @@ def forward_with_yolo_face(detect_vision_frame : VisionFrame) -> Detection:
|
||||
return detection
|
||||
|
||||
|
||||
def forward_with_yunet(detect_vision_frame : VisionFrame) -> Detection:
|
||||
face_detector = get_inference_pool().get('yunet')
|
||||
|
||||
with thread_semaphore():
|
||||
detection = face_detector.run(None,
|
||||
{
|
||||
'input': detect_vision_frame
|
||||
})
|
||||
|
||||
return detection
|
||||
|
||||
|
||||
def prepare_detect_frame(temp_vision_frame : VisionFrame, face_detector_size : str) -> VisionFrame:
|
||||
face_detector_width, face_detector_height = unpack_resolution(face_detector_size)
|
||||
detect_vision_frame = numpy.zeros((face_detector_height, face_detector_width, 3))
|
||||
|
||||
+63
-41
@@ -69,8 +69,8 @@ WARP_TEMPLATE_SET : WarpTemplateSet =\
|
||||
|
||||
|
||||
def estimate_matrix_by_face_landmark_5(face_landmark_5 : FaceLandmark5, warp_template : WarpTemplate, crop_size : Size) -> Matrix:
|
||||
normed_warp_template = WARP_TEMPLATE_SET.get(warp_template) * crop_size
|
||||
affine_matrix = cv2.estimateAffinePartial2D(face_landmark_5, normed_warp_template, method = cv2.RANSAC, ransacReprojThreshold = 100)[0]
|
||||
warp_template_norm = WARP_TEMPLATE_SET.get(warp_template) * crop_size
|
||||
affine_matrix = cv2.estimateAffinePartial2D(face_landmark_5, warp_template_norm, method = cv2.RANSAC, ransacReprojThreshold = 100)[0]
|
||||
return affine_matrix
|
||||
|
||||
|
||||
@@ -81,7 +81,7 @@ def warp_face_by_face_landmark_5(temp_vision_frame : VisionFrame, face_landmark_
|
||||
|
||||
|
||||
def warp_face_by_bounding_box(temp_vision_frame : VisionFrame, bounding_box : BoundingBox, crop_size : Size) -> Tuple[VisionFrame, Matrix]:
|
||||
source_points = numpy.array([ [ bounding_box[0], bounding_box[1] ], [bounding_box[2], bounding_box[1] ], [ bounding_box[0], bounding_box[3] ] ]).astype(numpy.float32)
|
||||
source_points = numpy.array([ [ bounding_box[0], bounding_box[1] ], [ bounding_box[2], bounding_box[1] ], [ bounding_box[0], bounding_box[3] ] ]).astype(numpy.float32)
|
||||
target_points = numpy.array([ [ 0, 0 ], [ crop_size[0], 0 ], [ 0, crop_size[1] ] ]).astype(numpy.float32)
|
||||
affine_matrix = cv2.getAffineTransform(source_points, target_points)
|
||||
if bounding_box[2] - bounding_box[0] > crop_size[0] or bounding_box[3] - bounding_box[1] > crop_size[1]:
|
||||
@@ -98,59 +98,59 @@ def warp_face_by_translation(temp_vision_frame : VisionFrame, translation : Tran
|
||||
return crop_vision_frame, affine_matrix
|
||||
|
||||
|
||||
def paste_back(temp_vision_frame : VisionFrame, crop_vision_frame : VisionFrame, crop_mask : Mask, affine_matrix : Matrix) -> VisionFrame:
|
||||
paste_bounding_box, paste_matrix = calc_paste_area(temp_vision_frame, crop_vision_frame, affine_matrix)
|
||||
x_min, y_min, x_max, y_max = paste_bounding_box
|
||||
paste_width = x_max - x_min
|
||||
paste_height = y_max - y_min
|
||||
inverse_mask = cv2.warpAffine(crop_mask, paste_matrix, (paste_width, paste_height)).clip(0, 1)
|
||||
inverse_mask = numpy.expand_dims(inverse_mask, axis = -1)
|
||||
def paste_back(temp_vision_frame : VisionFrame, crop_vision_frame : VisionFrame, crop_vision_mask : Mask, affine_matrix : Matrix) -> VisionFrame:
|
||||
paste_bounding_box, paste_matrix = calculate_paste_area(temp_vision_frame, crop_vision_frame, affine_matrix)
|
||||
x1, y1, x2, y2 = paste_bounding_box
|
||||
paste_width = x2 - x1
|
||||
paste_height = y2 - y1
|
||||
inverse_vision_mask = cv2.warpAffine(crop_vision_mask, paste_matrix, (paste_width, paste_height)).clip(0, 1)
|
||||
inverse_vision_mask = numpy.expand_dims(inverse_vision_mask, axis = -1)
|
||||
inverse_vision_frame = cv2.warpAffine(crop_vision_frame, paste_matrix, (paste_width, paste_height), borderMode = cv2.BORDER_REPLICATE)
|
||||
temp_vision_frame = temp_vision_frame.copy()
|
||||
paste_vision_frame = temp_vision_frame[y_min:y_max, x_min:x_max]
|
||||
paste_vision_frame = paste_vision_frame * (1 - inverse_mask) + inverse_vision_frame * inverse_mask
|
||||
temp_vision_frame[y_min:y_max, x_min:x_max] = paste_vision_frame.astype(temp_vision_frame.dtype)
|
||||
paste_vision_frame = temp_vision_frame[y1:y2, x1:x2]
|
||||
paste_vision_frame = paste_vision_frame * (1 - inverse_vision_mask) + inverse_vision_frame * inverse_vision_mask
|
||||
temp_vision_frame[y1:y2, x1:x2] = paste_vision_frame.astype(temp_vision_frame.dtype)
|
||||
return temp_vision_frame
|
||||
|
||||
|
||||
def calc_paste_area(temp_vision_frame : VisionFrame, crop_vision_frame : VisionFrame, affine_matrix : Matrix) -> Tuple[BoundingBox, Matrix]:
|
||||
def calculate_paste_area(temp_vision_frame : VisionFrame, crop_vision_frame : VisionFrame, affine_matrix : Matrix) -> Tuple[BoundingBox, Matrix]:
|
||||
temp_height, temp_width = temp_vision_frame.shape[:2]
|
||||
crop_height, crop_width = crop_vision_frame.shape[:2]
|
||||
inverse_matrix = cv2.invertAffineTransform(affine_matrix)
|
||||
crop_points = numpy.array([ [ 0, 0 ], [ crop_width, 0 ], [ crop_width, crop_height ], [ 0, crop_height ] ])
|
||||
paste_region_points = transform_points(crop_points, inverse_matrix)
|
||||
min_point = numpy.floor(paste_region_points.min(axis = 0)).astype(int)
|
||||
max_point = numpy.ceil(paste_region_points.max(axis = 0)).astype(int)
|
||||
x_min, y_min = numpy.clip(min_point, 0, [ temp_width, temp_height ])
|
||||
x_max, y_max = numpy.clip(max_point, 0, [ temp_width, temp_height ])
|
||||
paste_bounding_box = numpy.array([ x_min, y_min, x_max, y_max ])
|
||||
paste_region_point_min = numpy.floor(paste_region_points.min(axis = 0)).astype(int)
|
||||
paste_region_point_max = numpy.ceil(paste_region_points.max(axis = 0)).astype(int)
|
||||
x1, y1 = numpy.clip(paste_region_point_min, 0, [ temp_width, temp_height ])
|
||||
x2, y2 = numpy.clip(paste_region_point_max, 0, [ temp_width, temp_height ])
|
||||
paste_bounding_box = numpy.array([ x1, y1, x2, y2 ])
|
||||
paste_matrix = inverse_matrix.copy()
|
||||
paste_matrix[0, 2] -= x_min
|
||||
paste_matrix[1, 2] -= y_min
|
||||
paste_matrix[0, 2] -= x1
|
||||
paste_matrix[1, 2] -= y1
|
||||
return paste_bounding_box, paste_matrix
|
||||
|
||||
|
||||
@lru_cache(maxsize = None)
|
||||
def create_static_anchors(feature_stride : int, anchor_total : int, stride_height : int, stride_width : int) -> Anchors:
|
||||
y, x = numpy.mgrid[:stride_height, :stride_width][::-1]
|
||||
@lru_cache()
|
||||
def create_static_anchors(feature_stride : int, anchor_total : int, stride_width : int, stride_height : int) -> Anchors:
|
||||
x, y = numpy.mgrid[:stride_width, :stride_height]
|
||||
anchors = numpy.stack((y, x), axis = -1)
|
||||
anchors = (anchors * feature_stride).reshape((-1, 2))
|
||||
anchors = numpy.stack([ anchors ] * anchor_total, axis = 1).reshape((-1, 2))
|
||||
return anchors
|
||||
|
||||
|
||||
def create_rotated_matrix_and_size(angle : Angle, size : Size) -> Tuple[Matrix, Size]:
|
||||
rotated_matrix = cv2.getRotationMatrix2D((size[0] / 2, size[1] / 2), angle, 1)
|
||||
rotated_size = numpy.dot(numpy.abs(rotated_matrix[:, :2]), size)
|
||||
rotated_matrix[:, -1] += (rotated_size - size) * 0.5 #type:ignore[misc]
|
||||
rotated_size = int(rotated_size[0]), int(rotated_size[1])
|
||||
return rotated_matrix, rotated_size
|
||||
def create_rotation_matrix_and_size(angle : Angle, size : Size) -> Tuple[Matrix, Size]:
|
||||
rotation_matrix = cv2.getRotationMatrix2D((size[0] / 2, size[1] / 2), angle, 1)
|
||||
rotation_size = numpy.dot(numpy.abs(rotation_matrix[:, :2]), size)
|
||||
rotation_matrix[:, -1] += (rotation_size - size) * 0.5 #type:ignore[misc]
|
||||
rotation_size = int(rotation_size[0]), int(rotation_size[1])
|
||||
return rotation_matrix, rotation_size
|
||||
|
||||
|
||||
def create_bounding_box(face_landmark_68 : FaceLandmark68) -> BoundingBox:
|
||||
min_x, min_y = numpy.min(face_landmark_68, axis = 0)
|
||||
max_x, max_y = numpy.max(face_landmark_68, axis = 0)
|
||||
bounding_box = normalize_bounding_box(numpy.array([ min_x, min_y, max_x, max_y ]))
|
||||
x1, y1 = numpy.min(face_landmark_68, axis = 0)
|
||||
x2, y2 = numpy.max(face_landmark_68, axis = 0)
|
||||
bounding_box = normalize_bounding_box(numpy.array([ x1, y1, x2, y2 ]))
|
||||
return bounding_box
|
||||
|
||||
|
||||
@@ -229,8 +229,8 @@ def estimate_face_angle(face_landmark_68 : FaceLandmark68) -> Angle:
|
||||
|
||||
|
||||
def apply_nms(bounding_boxes : List[BoundingBox], scores : List[Score], score_threshold : float, nms_threshold : float) -> Sequence[int]:
|
||||
normed_bounding_boxes = [ (x1, y1, x2 - x1, y2 - y1) for (x1, y1, x2, y2) in bounding_boxes ]
|
||||
keep_indices = cv2.dnn.NMSBoxes(normed_bounding_boxes, scores, score_threshold = score_threshold, nms_threshold = nms_threshold)
|
||||
bounding_boxes_norm = [ (x1, y1, x2 - x1, y2 - y1) for (x1, y1, x2, y2) in bounding_boxes ]
|
||||
keep_indices = cv2.dnn.NMSBoxes(bounding_boxes_norm, scores, score_threshold = score_threshold, nms_threshold = nms_threshold)
|
||||
return keep_indices
|
||||
|
||||
|
||||
@@ -246,9 +246,31 @@ def get_nms_threshold(face_detector_model : FaceDetectorModel, face_detector_ang
|
||||
return 0.4
|
||||
|
||||
|
||||
def merge_matrix(matrices : List[Matrix]) -> Matrix:
|
||||
merged_matrix = numpy.vstack([ matrices[0], [ 0, 0, 1 ] ])
|
||||
for matrix in matrices[1:]:
|
||||
matrix = numpy.vstack([ matrix, [ 0, 0, 1 ] ])
|
||||
merged_matrix = numpy.dot(merged_matrix, matrix)
|
||||
return merged_matrix[:2, :]
|
||||
def merge_matrix(temp_matrices : List[Matrix]) -> Matrix:
|
||||
matrix = numpy.vstack([ temp_matrices[0], [ 0, 0, 1 ] ])
|
||||
|
||||
for temp_matrix in temp_matrices[1:]:
|
||||
temp_matrix = numpy.vstack([ temp_matrix, [ 0, 0, 1 ] ])
|
||||
matrix = numpy.dot(temp_matrix, matrix)
|
||||
|
||||
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
|
||||
|
||||
@@ -6,18 +6,24 @@ import numpy
|
||||
|
||||
from facefusion import inference_manager, state_manager
|
||||
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
|
||||
from facefusion.face_helper import create_rotated_matrix_and_size, estimate_matrix_by_face_landmark_5, transform_points, warp_face_by_translation
|
||||
from facefusion.face_helper import create_rotation_matrix_and_size, estimate_matrix_by_face_landmark_5, transform_points, warp_face_by_translation
|
||||
from facefusion.filesystem import resolve_relative_path
|
||||
from facefusion.thread_helper import conditional_thread_semaphore
|
||||
from facefusion.types import Angle, BoundingBox, DownloadScope, DownloadSet, FaceLandmark5, FaceLandmark68, InferencePool, ModelSet, Prediction, Score, VisionFrame
|
||||
|
||||
|
||||
@lru_cache(maxsize = None)
|
||||
@lru_cache()
|
||||
def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
||||
return\
|
||||
{
|
||||
'2dfan4':
|
||||
{
|
||||
'__metadata__':
|
||||
{
|
||||
'vendor': 'breadbread1984',
|
||||
'license': 'MIT',
|
||||
'year': 2018
|
||||
},
|
||||
'hashes':
|
||||
{
|
||||
'2dfan4':
|
||||
@@ -38,6 +44,12 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
||||
},
|
||||
'peppa_wutz':
|
||||
{
|
||||
'__metadata__':
|
||||
{
|
||||
'vendor': 'Unknown',
|
||||
'license': 'Apache-2.0',
|
||||
'year': 2023
|
||||
},
|
||||
'hashes':
|
||||
{
|
||||
'peppa_wutz':
|
||||
@@ -58,6 +70,12 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
||||
},
|
||||
'fan_68_5':
|
||||
{
|
||||
'__metadata__':
|
||||
{
|
||||
'vendor': 'FaceFusion',
|
||||
'license': 'OpenRAIL-M',
|
||||
'year': 2024
|
||||
},
|
||||
'hashes':
|
||||
{
|
||||
'fan_68_5':
|
||||
@@ -136,14 +154,14 @@ def detect_with_2dfan4(temp_vision_frame: VisionFrame, bounding_box: BoundingBox
|
||||
model_size = create_static_model_set('full').get('2dfan4').get('size')
|
||||
scale = 195 / numpy.subtract(bounding_box[2:], bounding_box[:2]).max().clip(1, None)
|
||||
translation = (model_size[0] - numpy.add(bounding_box[2:], bounding_box[:2]) * scale) * 0.5
|
||||
rotated_matrix, rotated_size = create_rotated_matrix_and_size(face_angle, model_size)
|
||||
rotation_matrix, rotation_size = create_rotation_matrix_and_size(face_angle, model_size)
|
||||
crop_vision_frame, affine_matrix = warp_face_by_translation(temp_vision_frame, translation, scale, model_size)
|
||||
crop_vision_frame = cv2.warpAffine(crop_vision_frame, rotated_matrix, rotated_size)
|
||||
crop_vision_frame = cv2.warpAffine(crop_vision_frame, rotation_matrix, rotation_size)
|
||||
crop_vision_frame = conditional_optimize_contrast(crop_vision_frame)
|
||||
crop_vision_frame = crop_vision_frame.transpose(2, 0, 1).astype(numpy.float32) / 255.0
|
||||
face_landmark_68, face_heatmap = forward_with_2dfan4(crop_vision_frame)
|
||||
face_landmark_68 = face_landmark_68[:, :, :2][0] / 64 * 256
|
||||
face_landmark_68 = transform_points(face_landmark_68, cv2.invertAffineTransform(rotated_matrix))
|
||||
face_landmark_68 = transform_points(face_landmark_68, cv2.invertAffineTransform(rotation_matrix))
|
||||
face_landmark_68 = transform_points(face_landmark_68, cv2.invertAffineTransform(affine_matrix))
|
||||
face_landmark_score_68 = numpy.amax(face_heatmap, axis = (2, 3))
|
||||
face_landmark_score_68 = numpy.mean(face_landmark_score_68)
|
||||
@@ -155,15 +173,15 @@ def detect_with_peppa_wutz(temp_vision_frame : VisionFrame, bounding_box : Bound
|
||||
model_size = create_static_model_set('full').get('peppa_wutz').get('size')
|
||||
scale = 195 / numpy.subtract(bounding_box[2:], bounding_box[:2]).max().clip(1, None)
|
||||
translation = (model_size[0] - numpy.add(bounding_box[2:], bounding_box[:2]) * scale) * 0.5
|
||||
rotated_matrix, rotated_size = create_rotated_matrix_and_size(face_angle, model_size)
|
||||
rotation_matrix, rotation_size = create_rotation_matrix_and_size(face_angle, model_size)
|
||||
crop_vision_frame, affine_matrix = warp_face_by_translation(temp_vision_frame, translation, scale, model_size)
|
||||
crop_vision_frame = cv2.warpAffine(crop_vision_frame, rotated_matrix, rotated_size)
|
||||
crop_vision_frame = cv2.warpAffine(crop_vision_frame, rotation_matrix, rotation_size)
|
||||
crop_vision_frame = conditional_optimize_contrast(crop_vision_frame)
|
||||
crop_vision_frame = crop_vision_frame.transpose(2, 0, 1).astype(numpy.float32) / 255.0
|
||||
crop_vision_frame = numpy.expand_dims(crop_vision_frame, axis = 0)
|
||||
prediction = forward_with_peppa_wutz(crop_vision_frame)
|
||||
face_landmark_68 = prediction.reshape(-1, 3)[:, :2] / 64 * model_size[0]
|
||||
face_landmark_68 = transform_points(face_landmark_68, cv2.invertAffineTransform(rotated_matrix))
|
||||
face_landmark_68 = transform_points(face_landmark_68, cv2.invertAffineTransform(rotation_matrix))
|
||||
face_landmark_68 = transform_points(face_landmark_68, cv2.invertAffineTransform(affine_matrix))
|
||||
face_landmark_score_68 = prediction.reshape(-1, 3)[:, 2].mean()
|
||||
face_landmark_score_68 = numpy.interp(face_landmark_score_68, [ 0, 0.95 ], [ 0, 1 ])
|
||||
|
||||
+52
-13
@@ -12,12 +12,18 @@ from facefusion.thread_helper import conditional_thread_semaphore
|
||||
from facefusion.types import DownloadScope, DownloadSet, FaceLandmark68, FaceMaskArea, FaceMaskRegion, InferencePool, Mask, ModelSet, Padding, VisionFrame
|
||||
|
||||
|
||||
@lru_cache(maxsize = None)
|
||||
@lru_cache()
|
||||
def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
||||
return\
|
||||
{
|
||||
'xseg_1':
|
||||
{
|
||||
'__metadata__':
|
||||
{
|
||||
'vendor': 'DeepFaceLab',
|
||||
'license': 'GPL-3.0',
|
||||
'year': 2021
|
||||
},
|
||||
'hashes':
|
||||
{
|
||||
'face_occluder':
|
||||
@@ -38,6 +44,12 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
||||
},
|
||||
'xseg_2':
|
||||
{
|
||||
'__metadata__':
|
||||
{
|
||||
'vendor': 'DeepFaceLab',
|
||||
'license': 'GPL-3.0',
|
||||
'year': 2021
|
||||
},
|
||||
'hashes':
|
||||
{
|
||||
'face_occluder':
|
||||
@@ -58,6 +70,12 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
||||
},
|
||||
'xseg_3':
|
||||
{
|
||||
'__metadata__':
|
||||
{
|
||||
'vendor': 'DeepFaceLab',
|
||||
'license': 'GPL-3.0',
|
||||
'year': 2021
|
||||
},
|
||||
'hashes':
|
||||
{
|
||||
'face_occluder':
|
||||
@@ -78,6 +96,12 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
||||
},
|
||||
'bisenet_resnet_18':
|
||||
{
|
||||
'__metadata__':
|
||||
{
|
||||
'vendor': 'yakhyo',
|
||||
'license': 'MIT',
|
||||
'year': 2024
|
||||
},
|
||||
'hashes':
|
||||
{
|
||||
'face_parser':
|
||||
@@ -98,6 +122,12 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
||||
},
|
||||
'bisenet_resnet_34':
|
||||
{
|
||||
'__metadata__':
|
||||
{
|
||||
'vendor': 'yakhyo',
|
||||
'license': 'MIT',
|
||||
'year': 2024
|
||||
},
|
||||
'hashes':
|
||||
{
|
||||
'face_parser':
|
||||
@@ -137,7 +167,7 @@ def collect_model_downloads() -> Tuple[DownloadSet, DownloadSet]:
|
||||
model_source_set = {}
|
||||
|
||||
for face_occluder_model in [ 'xseg_1', 'xseg_2', 'xseg_3' ]:
|
||||
if state_manager.get_item('face_occluder_model') == face_occluder_model:
|
||||
if state_manager.get_item('face_occluder_model') in [ 'many', face_occluder_model ]:
|
||||
model_hash_set[face_occluder_model] = model_set.get(face_occluder_model).get('hashes').get('face_occluder')
|
||||
model_source_set[face_occluder_model] = model_set.get(face_occluder_model).get('sources').get('face_occluder')
|
||||
|
||||
@@ -171,14 +201,24 @@ def create_box_mask(crop_vision_frame : VisionFrame, face_mask_blur : float, fac
|
||||
|
||||
|
||||
def create_occlusion_mask(crop_vision_frame : VisionFrame) -> Mask:
|
||||
model_name = state_manager.get_item('face_occluder_model')
|
||||
model_size = create_static_model_set('full').get(model_name).get('size')
|
||||
prepare_vision_frame = cv2.resize(crop_vision_frame, model_size)
|
||||
prepare_vision_frame = numpy.expand_dims(prepare_vision_frame, axis = 0).astype(numpy.float32) / 255.0
|
||||
prepare_vision_frame = prepare_vision_frame.transpose(0, 1, 2, 3)
|
||||
occlusion_mask = forward_occlude_face(prepare_vision_frame)
|
||||
occlusion_mask = occlusion_mask.transpose(0, 1, 2).clip(0, 1).astype(numpy.float32)
|
||||
occlusion_mask = cv2.resize(occlusion_mask, crop_vision_frame.shape[:2][::-1])
|
||||
temp_masks = []
|
||||
|
||||
if state_manager.get_item('face_occluder_model') == 'many':
|
||||
model_names = [ 'xseg_1', 'xseg_2', 'xseg_3' ]
|
||||
else:
|
||||
model_names = [ state_manager.get_item('face_occluder_model') ]
|
||||
|
||||
for model_name in model_names:
|
||||
model_size = create_static_model_set('full').get(model_name).get('size')
|
||||
prepare_vision_frame = cv2.resize(crop_vision_frame, model_size)
|
||||
prepare_vision_frame = numpy.expand_dims(prepare_vision_frame, axis = 0).astype(numpy.float32) / 255.0
|
||||
prepare_vision_frame = prepare_vision_frame.transpose(0, 1, 2, 3)
|
||||
temp_mask = forward_occlude_face(prepare_vision_frame, model_name)
|
||||
temp_mask = temp_mask.transpose(0, 1, 2).clip(0, 1).astype(numpy.float32)
|
||||
temp_mask = cv2.resize(temp_mask, crop_vision_frame.shape[:2][::-1])
|
||||
temp_masks.append(temp_mask)
|
||||
|
||||
occlusion_mask = numpy.minimum.reduce(temp_masks)
|
||||
occlusion_mask = (cv2.GaussianBlur(occlusion_mask.clip(0, 1), (0, 0), 5).clip(0.5, 1) - 0.5) * 2
|
||||
return occlusion_mask
|
||||
|
||||
@@ -193,7 +233,7 @@ def create_area_mask(crop_vision_frame : VisionFrame, face_landmark_68 : FaceLan
|
||||
|
||||
convex_hull = cv2.convexHull(face_landmark_68[landmark_points].astype(numpy.int32))
|
||||
area_mask = numpy.zeros(crop_size).astype(numpy.float32)
|
||||
cv2.fillConvexPoly(area_mask, convex_hull, 1.0) # type: ignore[call-overload]
|
||||
cv2.fillConvexPoly(area_mask, convex_hull, 1.0) #type:ignore[call-overload]
|
||||
area_mask = (cv2.GaussianBlur(area_mask.clip(0, 1), (0, 0), 5).clip(0.5, 1) - 0.5) * 2
|
||||
return area_mask
|
||||
|
||||
@@ -214,8 +254,7 @@ def create_region_mask(crop_vision_frame : VisionFrame, face_mask_regions : List
|
||||
return region_mask
|
||||
|
||||
|
||||
def forward_occlude_face(prepare_vision_frame : VisionFrame) -> Mask:
|
||||
model_name = state_manager.get_item('face_occluder_model')
|
||||
def forward_occlude_face(prepare_vision_frame : VisionFrame, model_name : str) -> Mask:
|
||||
face_occluder = get_inference_pool().get(model_name)
|
||||
|
||||
with conditional_thread_semaphore():
|
||||
|
||||
@@ -11,12 +11,18 @@ from facefusion.thread_helper import conditional_thread_semaphore
|
||||
from facefusion.types import DownloadScope, Embedding, FaceLandmark5, InferencePool, ModelOptions, ModelSet, VisionFrame
|
||||
|
||||
|
||||
@lru_cache(maxsize = None)
|
||||
@lru_cache()
|
||||
def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
||||
return\
|
||||
{
|
||||
'arcface':
|
||||
{
|
||||
'__metadata__':
|
||||
{
|
||||
'vendor': 'InsightFace',
|
||||
'license': 'Non-Commercial',
|
||||
'year': 2018
|
||||
},
|
||||
'hashes':
|
||||
{
|
||||
'face_recognizer':
|
||||
@@ -62,26 +68,26 @@ def pre_check() -> bool:
|
||||
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
|
||||
|
||||
|
||||
def calc_embedding(temp_vision_frame : VisionFrame, face_landmark_5 : FaceLandmark5) -> Tuple[Embedding, Embedding]:
|
||||
def calculate_face_embedding(temp_vision_frame : VisionFrame, face_landmark_5 : FaceLandmark5) -> Tuple[Embedding, Embedding]:
|
||||
model_template = get_model_options().get('template')
|
||||
model_size = get_model_options().get('size')
|
||||
crop_vision_frame, matrix = warp_face_by_face_landmark_5(temp_vision_frame, face_landmark_5, model_template, model_size)
|
||||
crop_vision_frame = crop_vision_frame / 127.5 - 1
|
||||
crop_vision_frame = crop_vision_frame[:, :, ::-1].transpose(2, 0, 1).astype(numpy.float32)
|
||||
crop_vision_frame = numpy.expand_dims(crop_vision_frame, axis = 0)
|
||||
embedding = forward(crop_vision_frame)
|
||||
embedding = embedding.ravel()
|
||||
normed_embedding = embedding / numpy.linalg.norm(embedding)
|
||||
return embedding, normed_embedding
|
||||
face_embedding = forward(crop_vision_frame)
|
||||
face_embedding = face_embedding.ravel()
|
||||
face_embedding_norm = face_embedding / numpy.linalg.norm(face_embedding)
|
||||
return face_embedding, face_embedding_norm
|
||||
|
||||
|
||||
def forward(crop_vision_frame : VisionFrame) -> Embedding:
|
||||
face_recognizer = get_inference_pool().get('face_recognizer')
|
||||
|
||||
with conditional_thread_semaphore():
|
||||
embedding = face_recognizer.run(None,
|
||||
face_embedding = face_recognizer.run(None,
|
||||
{
|
||||
'input': crop_vision_frame
|
||||
})[0]
|
||||
|
||||
return embedding
|
||||
return face_embedding
|
||||
|
||||
+71
-24
@@ -2,46 +2,93 @@ from typing import List
|
||||
|
||||
import numpy
|
||||
|
||||
import facefusion.choices
|
||||
from facefusion import state_manager
|
||||
from facefusion.types import Face, FaceSelectorOrder, FaceSet, Gender, Race, Score
|
||||
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
|
||||
|
||||
|
||||
def find_similar_faces(faces : List[Face], reference_faces : FaceSet, face_distance : float) -> List[Face]:
|
||||
similar_faces : List[Face] = []
|
||||
def select_faces(reference_vision_frame : VisionFrame, source_vision_frames : List[VisionFrame], target_vision_frames : List[VisionFrame]) -> List[Face]:
|
||||
source_faces = get_static_faces(source_vision_frames)
|
||||
|
||||
if faces and reference_faces:
|
||||
for reference_set in reference_faces:
|
||||
if not similar_faces:
|
||||
for reference_face in reference_faces[reference_set]:
|
||||
for face in faces:
|
||||
if compare_faces(face, reference_face, face_distance):
|
||||
similar_faces.append(face)
|
||||
return similar_faces
|
||||
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':
|
||||
return sort_and_filter_faces(source_faces, target_faces)
|
||||
|
||||
if state_manager.get_item('face_selector_mode') == 'one':
|
||||
target_face = get_one_face(sort_and_filter_faces(source_faces, target_faces))
|
||||
if target_face:
|
||||
return [ target_face ]
|
||||
|
||||
if state_manager.get_item('face_selector_mode') == 'reference':
|
||||
reference_faces = get_static_faces([ reference_vision_frame ])
|
||||
reference_faces = sort_and_filter_faces(source_faces, reference_faces)
|
||||
reference_face = get_one_face(reference_faces, state_manager.get_item('reference_face_position'))
|
||||
|
||||
if reference_face:
|
||||
match_faces = find_match_faces([ reference_face ], target_faces, state_manager.get_item('reference_face_distance'))
|
||||
return match_faces
|
||||
|
||||
return []
|
||||
|
||||
|
||||
def find_match_faces(reference_faces : List[Face], target_faces : List[Face], face_distance : float) -> List[Face]:
|
||||
match_faces : List[Face] = []
|
||||
|
||||
for reference_face in reference_faces:
|
||||
if reference_face:
|
||||
for index, target_face in enumerate(target_faces):
|
||||
if compare_faces(target_face, reference_face, face_distance):
|
||||
match_faces.append(target_faces[index])
|
||||
|
||||
return match_faces
|
||||
|
||||
|
||||
def compare_faces(face : Face, reference_face : Face, face_distance : float) -> bool:
|
||||
current_face_distance = calc_face_distance(face, reference_face)
|
||||
current_face_distance = calculate_face_distance(face, reference_face)
|
||||
current_face_distance = float(numpy.interp(current_face_distance, [ 0, 2 ], [ 0, 1 ]))
|
||||
return current_face_distance < face_distance
|
||||
|
||||
|
||||
def calc_face_distance(face : Face, reference_face : Face) -> float:
|
||||
if hasattr(face, 'normed_embedding') and hasattr(reference_face, 'normed_embedding'):
|
||||
return 1 - numpy.dot(face.normed_embedding, reference_face.normed_embedding)
|
||||
def calculate_face_distance(face : Face, reference_face : Face) -> float:
|
||||
if hasattr(face, 'embedding_norm') and hasattr(reference_face, 'embedding_norm'):
|
||||
return 1 - numpy.dot(face.embedding_norm, reference_face.embedding_norm)
|
||||
return 0
|
||||
|
||||
|
||||
def sort_and_filter_faces(faces : List[Face]) -> List[Face]:
|
||||
if faces:
|
||||
def sort_and_filter_faces(source_faces : List[Face], target_faces : List[Face]) -> List[Face]:
|
||||
if target_faces:
|
||||
if state_manager.get_item('face_selector_order'):
|
||||
faces = sort_faces_by_order(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'))
|
||||
if state_manager.get_item('face_selector_race'):
|
||||
faces = filter_faces_by_race(faces, state_manager.get_item('face_selector_race'))
|
||||
target_faces = sort_faces_by_order(target_faces, state_manager.get_item('face_selector_order'))
|
||||
|
||||
face_selector_gender = state_manager.get_item('face_selector_gender')
|
||||
face_selector_race = 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'):
|
||||
faces = filter_faces_by_age(faces, state_manager.get_item('face_selector_age_start'), state_manager.get_item('face_selector_age_end'))
|
||||
return faces
|
||||
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 target_faces
|
||||
|
||||
|
||||
def sort_faces_by_order(faces : List[Face], order : FaceSelectorOrder) -> List[Face]:
|
||||
|
||||
+30
-31
@@ -1,43 +1,42 @@
|
||||
import threading
|
||||
from typing import List, Optional
|
||||
|
||||
import numpy
|
||||
|
||||
from facefusion.hash_helper import create_hash
|
||||
from facefusion.types import Face, FaceSet, FaceStore, VisionFrame
|
||||
from facefusion.types import Face, FaceStore, VisionFrame
|
||||
|
||||
FACE_STORE : FaceStore =\
|
||||
{
|
||||
'static_faces': {},
|
||||
'reference_faces': {}
|
||||
}
|
||||
FACE_STORE : FaceStore = {}
|
||||
|
||||
|
||||
def get_face_store() -> FaceStore:
|
||||
return FACE_STORE
|
||||
def get_faces(vision_frame : VisionFrame) -> Optional[List[Face]]:
|
||||
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]]:
|
||||
vision_hash = create_hash(vision_frame.tobytes())
|
||||
return FACE_STORE.get('static_faces').get(vision_hash)
|
||||
def set_faces(vision_frame : VisionFrame, faces : List[Face]) -> None:
|
||||
if numpy.any(vision_frame):
|
||||
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:
|
||||
vision_hash = create_hash(vision_frame.tobytes())
|
||||
if vision_hash:
|
||||
FACE_STORE['static_faces'][vision_hash] = faces
|
||||
def resolve_lock(vision_frame : VisionFrame) -> threading.Lock:
|
||||
if numpy.any(vision_frame):
|
||||
vision_hash = create_hash(vision_frame.tobytes())
|
||||
return FACE_STORE.setdefault(vision_hash,
|
||||
{
|
||||
'lock': threading.Lock()
|
||||
}).get('lock')
|
||||
return threading.Lock()
|
||||
|
||||
|
||||
def clear_static_faces() -> None:
|
||||
FACE_STORE['static_faces'].clear()
|
||||
|
||||
|
||||
def get_reference_faces() -> Optional[FaceSet]:
|
||||
return FACE_STORE.get('reference_faces')
|
||||
|
||||
|
||||
def append_reference_face(name : str, face : Face) -> None:
|
||||
if name not in FACE_STORE.get('reference_faces'):
|
||||
FACE_STORE['reference_faces'][name] = []
|
||||
FACE_STORE['reference_faces'][name].append(face)
|
||||
|
||||
|
||||
def clear_reference_faces() -> None:
|
||||
FACE_STORE['reference_faces'].clear()
|
||||
def clear_faces() -> None:
|
||||
FACE_STORE.clear()
|
||||
|
||||
@@ -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
|
||||
+152
-48
@@ -1,20 +1,20 @@
|
||||
import os
|
||||
import subprocess
|
||||
import tempfile
|
||||
from functools import partial
|
||||
from functools import lru_cache, partial
|
||||
from typing import List, Optional, cast
|
||||
|
||||
from tqdm import tqdm
|
||||
|
||||
import facefusion.choices
|
||||
from facefusion import ffmpeg_builder, logger, process_manager, state_manager, wording
|
||||
from facefusion import ffmpeg_builder, logger, process_manager, state_manager, translator
|
||||
from facefusion.filesystem import get_file_format, remove_file
|
||||
from facefusion.temp_helper import get_temp_file_path, get_temp_frames_pattern
|
||||
from facefusion.types import AudioBuffer, AudioEncoder, Commands, EncoderSet, Fps, UpdateProgress, VideoEncoder, VideoFormat
|
||||
from facefusion.vision import detect_video_duration, detect_video_fps, predict_video_frame_total
|
||||
from facefusion.types import ApiSecurityStrategy, AudioEncoder, Buffer, Command, EncoderSet, Fps, Resolution, SampleRate, UpdateProgress, VideoEncoder, VideoFormat
|
||||
from facefusion.vision import detect_video_duration, detect_video_fps, pack_resolution, predict_video_frame_total
|
||||
|
||||
|
||||
def run_ffmpeg_with_progress(commands : Commands, update_progress : UpdateProgress) -> subprocess.Popen[bytes]:
|
||||
def run_ffmpeg_with_progress(commands : List[Command], update_progress : UpdateProgress) -> subprocess.Popen[Buffer]:
|
||||
log_level = state_manager.get_item('log_level')
|
||||
commands.extend(ffmpeg_builder.set_progress())
|
||||
commands.extend(ffmpeg_builder.cast_stream())
|
||||
@@ -23,8 +23,10 @@ def run_ffmpeg_with_progress(commands : Commands, update_progress : UpdateProgre
|
||||
|
||||
while process_manager.is_processing():
|
||||
try:
|
||||
|
||||
while __line__ := process.stdout.readline().decode().lower():
|
||||
if process_manager.is_stopping():
|
||||
process.terminate()
|
||||
|
||||
if 'frame=' in __line__:
|
||||
_, frame_number = __line__.split('frame=')
|
||||
update_progress(int(frame_number))
|
||||
@@ -36,8 +38,6 @@ def run_ffmpeg_with_progress(commands : Commands, update_progress : UpdateProgre
|
||||
continue
|
||||
return process
|
||||
|
||||
if process_manager.is_stopping():
|
||||
process.terminate()
|
||||
return process
|
||||
|
||||
|
||||
@@ -45,7 +45,14 @@ def update_progress(progress : tqdm, frame_number : int) -> None:
|
||||
progress.update(frame_number - progress.n)
|
||||
|
||||
|
||||
def run_ffmpeg(commands : Commands) -> subprocess.Popen[bytes]:
|
||||
def run_ffmpeg_with_pipe(commands : List[Command], file_content : Buffer) -> subprocess.Popen[Buffer]:
|
||||
commands = ffmpeg_builder.run(commands)
|
||||
process = subprocess.Popen(commands, stdin = subprocess.PIPE, stderr = subprocess.PIPE, stdout = subprocess.PIPE)
|
||||
process.communicate(input = file_content)
|
||||
return process
|
||||
|
||||
|
||||
def run_ffmpeg(commands : List[Command]) -> subprocess.Popen[Buffer]:
|
||||
log_level = state_manager.get_item('log_level')
|
||||
commands = ffmpeg_builder.run(commands)
|
||||
process = subprocess.Popen(commands, stderr = subprocess.PIPE, stdout = subprocess.PIPE)
|
||||
@@ -61,17 +68,18 @@ def run_ffmpeg(commands : Commands) -> subprocess.Popen[bytes]:
|
||||
|
||||
if process_manager.is_stopping():
|
||||
process.terminate()
|
||||
|
||||
return process
|
||||
|
||||
|
||||
def open_ffmpeg(commands : Commands) -> subprocess.Popen[bytes]:
|
||||
def open_ffmpeg(commands : List[Command]) -> subprocess.Popen[Buffer]:
|
||||
commands = ffmpeg_builder.run(commands)
|
||||
return subprocess.Popen(commands, stdin = subprocess.PIPE, stdout = subprocess.PIPE)
|
||||
|
||||
|
||||
def log_debug(process : subprocess.Popen[bytes]) -> None:
|
||||
def log_debug(process : subprocess.Popen[Buffer]) -> None:
|
||||
_, stderr = process.communicate()
|
||||
errors = stderr.decode().split(os.linesep)
|
||||
errors = stderr.decode().splitlines()
|
||||
|
||||
for error in errors:
|
||||
if error.strip():
|
||||
@@ -82,6 +90,7 @@ def get_available_encoder_set() -> EncoderSet:
|
||||
available_encoder_set : EncoderSet =\
|
||||
{
|
||||
'audio': [],
|
||||
'image': [],
|
||||
'video': []
|
||||
}
|
||||
commands = ffmpeg_builder.chain(
|
||||
@@ -93,60 +102,87 @@ def get_available_encoder_set() -> EncoderSet:
|
||||
if line.startswith(' a'):
|
||||
audio_encoder = line.split()[1]
|
||||
|
||||
if audio_encoder in facefusion.choices.output_audio_encoders:
|
||||
index = facefusion.choices.output_audio_encoders.index(audio_encoder) #type:ignore[arg-type]
|
||||
available_encoder_set['audio'].insert(index, audio_encoder) #type:ignore[arg-type]
|
||||
if line.startswith(' v'):
|
||||
video_encoder = line.split()[1]
|
||||
if audio_encoder in facefusion.choices.audio_encoders and audio_encoder not in available_encoder_set.get('audio'):
|
||||
available_encoder_set['audio'].append(audio_encoder) #type:ignore[arg-type]
|
||||
|
||||
if video_encoder in facefusion.choices.output_video_encoders:
|
||||
index = facefusion.choices.output_video_encoders.index(video_encoder) #type:ignore[arg-type]
|
||||
available_encoder_set['video'].insert(index, video_encoder) #type:ignore[arg-type]
|
||||
if line.startswith(' v'):
|
||||
vision_encoder = line.split()[1]
|
||||
|
||||
if vision_encoder in facefusion.choices.image_encoders and vision_encoder not in available_encoder_set.get('image'):
|
||||
available_encoder_set['image'].append(vision_encoder) #type:ignore[arg-type]
|
||||
|
||||
if vision_encoder in facefusion.choices.video_encoders and vision_encoder not in available_encoder_set.get('video'):
|
||||
available_encoder_set['video'].append(vision_encoder) #type:ignore[arg-type]
|
||||
|
||||
return available_encoder_set
|
||||
|
||||
|
||||
def extract_frames(target_path : str, temp_video_resolution : str, temp_video_fps : Fps, trim_frame_start : int, trim_frame_end : int) -> bool:
|
||||
@lru_cache(maxsize = None)
|
||||
def get_static_available_encoder_set() -> EncoderSet:
|
||||
return get_available_encoder_set()
|
||||
|
||||
|
||||
def extract_frames(target_path : str, output_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)
|
||||
temp_frames_pattern = get_temp_frames_pattern(target_path, '%08d')
|
||||
temp_frames_pattern = get_temp_frames_pattern(state_manager.get_temp_path(), output_path, state_manager.get_item('temp_frame_format'), '%08d')
|
||||
commands = ffmpeg_builder.chain(
|
||||
ffmpeg_builder.set_input(target_path),
|
||||
ffmpeg_builder.set_media_resolution(temp_video_resolution),
|
||||
ffmpeg_builder.set_media_resolution(pack_resolution(temp_video_resolution)),
|
||||
ffmpeg_builder.set_frame_quality(0),
|
||||
ffmpeg_builder.enforce_pixel_format('rgb24'),
|
||||
ffmpeg_builder.select_frame_range(trim_frame_start, trim_frame_end, temp_video_fps),
|
||||
ffmpeg_builder.prevent_frame_drop(),
|
||||
ffmpeg_builder.set_start_number(trim_frame_start),
|
||||
ffmpeg_builder.set_output(temp_frames_pattern)
|
||||
)
|
||||
|
||||
with tqdm(total = extract_frame_total, desc = wording.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:
|
||||
process = run_ffmpeg_with_progress(commands, partial(update_progress, progress))
|
||||
return process.returncode == 0
|
||||
|
||||
|
||||
def copy_image(target_path : str, temp_image_resolution : str) -> bool:
|
||||
temp_image_path = get_temp_file_path(target_path)
|
||||
def spawn_frames(target_path : str, output_path : str, temp_video_resolution : Resolution, temp_video_fps : Fps, trim_frame_start : int, trim_frame_end : int) -> bool:
|
||||
spawn_frame_total = trim_frame_end - trim_frame_start
|
||||
duration = spawn_frame_total / temp_video_fps
|
||||
temp_frames_pattern = get_temp_frames_pattern(state_manager.get_temp_path(), output_path, state_manager.get_item('temp_frame_format'), '%08d')
|
||||
commands = ffmpeg_builder.chain(
|
||||
ffmpeg_builder.set_loop(),
|
||||
ffmpeg_builder.set_input(target_path),
|
||||
ffmpeg_builder.set_video_duration(duration),
|
||||
ffmpeg_builder.set_video_fps(temp_video_fps),
|
||||
ffmpeg_builder.set_media_resolution(pack_resolution(temp_video_resolution)),
|
||||
ffmpeg_builder.set_output(temp_frames_pattern)
|
||||
)
|
||||
|
||||
with tqdm(total = spawn_frame_total, desc = translator.get('spawning'), unit = 'frame', ascii = ' =', disable = state_manager.get_item('log_level') in [ 'warn', 'error' ]) as progress:
|
||||
process = run_ffmpeg_with_progress(commands, partial(update_progress, progress))
|
||||
return process.returncode == 0
|
||||
|
||||
|
||||
def copy_image(target_path : str, output_path : str, temp_image_resolution : Resolution) -> bool:
|
||||
temp_image_path = get_temp_file_path(state_manager.get_temp_path(), output_path)
|
||||
commands = ffmpeg_builder.chain(
|
||||
ffmpeg_builder.set_input(target_path),
|
||||
ffmpeg_builder.set_media_resolution(temp_image_resolution),
|
||||
ffmpeg_builder.set_media_resolution(pack_resolution(temp_image_resolution)),
|
||||
ffmpeg_builder.set_image_quality(target_path, 100),
|
||||
ffmpeg_builder.force_output(temp_image_path)
|
||||
)
|
||||
return run_ffmpeg(commands).returncode == 0
|
||||
|
||||
|
||||
def finalize_image(target_path : str, output_path : str, output_image_resolution : str) -> bool:
|
||||
def finalize_image(output_path : str, output_image_resolution : Resolution) -> bool:
|
||||
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(state_manager.get_temp_path(), output_path)
|
||||
commands = ffmpeg_builder.chain(
|
||||
ffmpeg_builder.set_input(temp_image_path),
|
||||
ffmpeg_builder.set_media_resolution(output_image_resolution),
|
||||
ffmpeg_builder.set_image_quality(target_path, output_image_quality),
|
||||
ffmpeg_builder.set_media_resolution(pack_resolution(output_image_resolution)),
|
||||
ffmpeg_builder.set_image_quality(output_path, output_image_quality),
|
||||
ffmpeg_builder.force_output(output_path)
|
||||
)
|
||||
return run_ffmpeg(commands).returncode == 0
|
||||
|
||||
|
||||
def read_audio_buffer(target_path : str, audio_sample_rate : int, audio_sample_size : int, audio_channel_total : int) -> Optional[AudioBuffer]:
|
||||
def read_audio_buffer(target_path : str, audio_sample_rate : SampleRate, audio_sample_size : int, audio_channel_total : int) -> Optional[Buffer]:
|
||||
commands = ffmpeg_builder.chain(
|
||||
ffmpeg_builder.set_input(target_path),
|
||||
ffmpeg_builder.ignore_video_stream(),
|
||||
@@ -158,6 +194,7 @@ def read_audio_buffer(target_path : str, audio_sample_rate : int, audio_sample_s
|
||||
|
||||
process = open_ffmpeg(commands)
|
||||
audio_buffer, _ = process.communicate()
|
||||
|
||||
if process.returncode == 0:
|
||||
return audio_buffer
|
||||
return None
|
||||
@@ -168,9 +205,10 @@ def restore_audio(target_path : str, output_path : str, trim_frame_start : int,
|
||||
output_audio_quality = state_manager.get_item('output_audio_quality')
|
||||
output_audio_volume = state_manager.get_item('output_audio_volume')
|
||||
target_video_fps = detect_video_fps(target_path)
|
||||
temp_video_path = get_temp_file_path(target_path)
|
||||
temp_video_format = cast(VideoFormat, get_file_format(temp_video_path))
|
||||
temp_video_path = get_temp_file_path(state_manager.get_temp_path(), output_path)
|
||||
temp_video_format = cast(VideoFormat, get_file_format(output_path))
|
||||
temp_video_duration = 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)
|
||||
commands = ffmpeg_builder.chain(
|
||||
@@ -184,18 +222,20 @@ 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('1:a:0'),
|
||||
ffmpeg_builder.set_video_duration(temp_video_duration),
|
||||
ffmpeg_builder.set_faststart(output_video_format),
|
||||
ffmpeg_builder.force_output(output_path)
|
||||
)
|
||||
return run_ffmpeg(commands).returncode == 0
|
||||
|
||||
|
||||
def replace_audio(target_path : str, audio_path : str, output_path : str) -> bool:
|
||||
def replace_audio(audio_path : str, output_path : str) -> bool:
|
||||
output_audio_encoder = state_manager.get_item('output_audio_encoder')
|
||||
output_audio_quality = state_manager.get_item('output_audio_quality')
|
||||
output_audio_volume = state_manager.get_item('output_audio_volume')
|
||||
temp_video_path = get_temp_file_path(target_path)
|
||||
temp_video_format = cast(VideoFormat, get_file_format(temp_video_path))
|
||||
temp_video_path = get_temp_file_path(state_manager.get_temp_path(), output_path)
|
||||
temp_video_format = cast(VideoFormat, get_file_format(output_path))
|
||||
temp_video_duration = 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)
|
||||
commands = ffmpeg_builder.chain(
|
||||
@@ -206,41 +246,47 @@ 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_volume(output_audio_volume),
|
||||
ffmpeg_builder.set_video_duration(temp_video_duration),
|
||||
ffmpeg_builder.set_faststart(output_video_format),
|
||||
ffmpeg_builder.force_output(output_path)
|
||||
)
|
||||
return run_ffmpeg(commands).returncode == 0
|
||||
|
||||
|
||||
def merge_video(target_path : str, temp_video_fps : Fps, output_video_resolution : str, output_video_fps : Fps, trim_frame_start : int, trim_frame_end : int) -> bool:
|
||||
def merge_video(target_path : str, output_path : str, temp_video_fps : Fps, output_video_fps : Fps, output_video_resolution : Resolution, trim_frame_start : int, trim_frame_end : int) -> bool:
|
||||
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')
|
||||
merge_frame_total = 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_format = cast(VideoFormat, get_file_format(temp_video_path))
|
||||
temp_frames_pattern = get_temp_frames_pattern(target_path, '%08d')
|
||||
temp_video_path = get_temp_file_path(state_manager.get_temp_path(), output_path)
|
||||
temp_video_format = cast(VideoFormat, get_file_format(output_path))
|
||||
temp_frames_pattern = get_temp_frames_pattern(state_manager.get_temp_path(), output_path, state_manager.get_item('temp_frame_format'), '%08d')
|
||||
|
||||
output_video_encoder = fix_video_encoder(temp_video_format, output_video_encoder)
|
||||
commands = ffmpeg_builder.chain(
|
||||
ffmpeg_builder.set_input_fps(temp_video_fps),
|
||||
ffmpeg_builder.set_start_number(trim_frame_start),
|
||||
ffmpeg_builder.set_input(temp_frames_pattern),
|
||||
ffmpeg_builder.set_media_resolution(output_video_resolution),
|
||||
ffmpeg_builder.set_media_resolution(pack_resolution(output_video_resolution)),
|
||||
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_preset(output_video_encoder, output_video_preset),
|
||||
ffmpeg_builder.set_video_fps(output_video_fps),
|
||||
ffmpeg_builder.concat(
|
||||
ffmpeg_builder.set_video_fps(output_video_fps),
|
||||
ffmpeg_builder.keep_video_alpha(output_video_encoder)
|
||||
),
|
||||
ffmpeg_builder.set_pixel_format(output_video_encoder),
|
||||
ffmpeg_builder.set_video_colorspace('bt709'),
|
||||
ffmpeg_builder.force_output(temp_video_path)
|
||||
)
|
||||
|
||||
with tqdm(total = merge_frame_total, desc = wording.get('merging'), unit = 'frame', ascii = ' =', disable = state_manager.get_item('log_level') in [ 'warn', 'error' ]) as progress:
|
||||
with tqdm(total = merge_frame_total, desc = translator.get('merging'), unit = 'frame', ascii = ' =', disable = state_manager.get_item('log_level') in [ 'warn', 'error' ]) as progress:
|
||||
process = run_ffmpeg_with_progress(commands, partial(update_progress, progress))
|
||||
return process.returncode == 0
|
||||
|
||||
|
||||
def concat_video(output_path : str, temp_output_paths : List[str]) -> bool:
|
||||
concat_video_path = tempfile.mktemp()
|
||||
file_descriptor, concat_video_path = tempfile.mkstemp()
|
||||
os.close(file_descriptor)
|
||||
|
||||
with open(concat_video_path, 'w') as concat_video_file:
|
||||
for temp_output_path in temp_output_paths:
|
||||
@@ -249,11 +295,13 @@ def concat_video(output_path : str, temp_output_paths : List[str]) -> bool:
|
||||
concat_video_file.close()
|
||||
|
||||
output_path = os.path.abspath(output_path)
|
||||
output_video_format = cast(VideoFormat, get_file_format(output_path))
|
||||
commands = ffmpeg_builder.chain(
|
||||
ffmpeg_builder.unsafe_concat(),
|
||||
ffmpeg_builder.set_input(concat_video_file.name),
|
||||
ffmpeg_builder.copy_video_encoder(),
|
||||
ffmpeg_builder.copy_audio_encoder(),
|
||||
ffmpeg_builder.set_faststart(output_video_format),
|
||||
ffmpeg_builder.force_output(output_path)
|
||||
)
|
||||
process = run_ffmpeg(commands)
|
||||
@@ -262,20 +310,76 @@ def concat_video(output_path : str, temp_output_paths : List[str]) -> bool:
|
||||
return process.returncode == 0
|
||||
|
||||
|
||||
def sanitize_audio(file_content : Buffer, asset_path : str, security_strategy : ApiSecurityStrategy) -> bool:
|
||||
if security_strategy == 'strict':
|
||||
commands = ffmpeg_builder.chain(
|
||||
ffmpeg_builder.set_input('pipe:0'),
|
||||
ffmpeg_builder.deep_copy_audio(),
|
||||
ffmpeg_builder.strip_metadata(),
|
||||
ffmpeg_builder.force_output(asset_path)
|
||||
)
|
||||
return run_ffmpeg_with_pipe(commands, file_content).returncode == 0
|
||||
|
||||
commands = ffmpeg_builder.chain(
|
||||
ffmpeg_builder.set_input('pipe:0'),
|
||||
ffmpeg_builder.copy_audio_encoder(),
|
||||
ffmpeg_builder.strip_metadata(),
|
||||
ffmpeg_builder.force_output(asset_path)
|
||||
)
|
||||
return run_ffmpeg_with_pipe(commands, file_content).returncode == 0
|
||||
|
||||
|
||||
def sanitize_image(file_content : Buffer, asset_path : str) -> bool:
|
||||
commands = ffmpeg_builder.chain(
|
||||
ffmpeg_builder.set_input('pipe:0'),
|
||||
ffmpeg_builder.deep_copy_image(),
|
||||
ffmpeg_builder.strip_metadata(),
|
||||
ffmpeg_builder.force_output(asset_path)
|
||||
)
|
||||
return run_ffmpeg_with_pipe(commands, file_content).returncode == 0
|
||||
|
||||
|
||||
def sanitize_video(file_content : Buffer, asset_path : str, security_strategy : ApiSecurityStrategy) -> bool:
|
||||
if security_strategy == 'strict':
|
||||
available_video_encoders = get_static_available_encoder_set().get('video')
|
||||
commands = ffmpeg_builder.chain(
|
||||
ffmpeg_builder.set_input('pipe:0'),
|
||||
ffmpeg_builder.set_video_encoder(available_video_encoders[0]),
|
||||
ffmpeg_builder.set_video_preset(available_video_encoders[0], 'ultrafast'),
|
||||
ffmpeg_builder.set_pixel_format(available_video_encoders[0]),
|
||||
ffmpeg_builder.deep_copy_video(),
|
||||
ffmpeg_builder.deep_copy_audio(),
|
||||
ffmpeg_builder.strip_metadata(),
|
||||
ffmpeg_builder.force_output(asset_path)
|
||||
)
|
||||
return run_ffmpeg_with_pipe(commands, file_content).returncode == 0
|
||||
|
||||
commands = ffmpeg_builder.chain(
|
||||
ffmpeg_builder.set_input('pipe:0'),
|
||||
ffmpeg_builder.copy_video_encoder(),
|
||||
ffmpeg_builder.copy_audio_encoder(),
|
||||
ffmpeg_builder.strip_metadata(),
|
||||
ffmpeg_builder.force_output(asset_path)
|
||||
)
|
||||
return run_ffmpeg_with_pipe(commands, file_content).returncode == 0
|
||||
|
||||
|
||||
def fix_audio_encoder(video_format : VideoFormat, audio_encoder : AudioEncoder) -> AudioEncoder:
|
||||
if video_format == 'avi' and audio_encoder == 'libopus':
|
||||
return 'aac'
|
||||
if video_format == 'm4v':
|
||||
if video_format in [ 'm4v', 'mpeg', 'wmv' ]:
|
||||
return 'aac'
|
||||
if video_format == 'mov' and audio_encoder in [ 'flac', 'libopus' ]:
|
||||
return 'aac'
|
||||
if video_format == 'mxf':
|
||||
return 'pcm_s16le'
|
||||
if video_format == 'webm':
|
||||
return 'libopus'
|
||||
return audio_encoder
|
||||
|
||||
|
||||
def fix_video_encoder(video_format : VideoFormat, video_encoder : VideoEncoder) -> VideoEncoder:
|
||||
if video_format == 'm4v':
|
||||
if video_format in [ 'm4v', 'mpeg', 'mxf', 'wmv' ]:
|
||||
return 'libx264'
|
||||
if video_format in [ 'mkv', 'mp4' ] and video_encoder == 'rawvideo':
|
||||
return 'libx264'
|
||||
|
||||
+122
-61
@@ -1,54 +1,77 @@
|
||||
import itertools
|
||||
import shutil
|
||||
from typing import Optional
|
||||
from typing import List, Optional
|
||||
|
||||
import numpy
|
||||
|
||||
from facefusion.filesystem import get_file_format
|
||||
from facefusion.types import AudioEncoder, Commands, Duration, Fps, StreamMode, VideoEncoder, VideoPreset
|
||||
from facefusion.types import AudioEncoder, Command, CommandSet, Duration, Fps, SampleRate, StreamMode, VideoEncoder, VideoFormat, VideoPreset
|
||||
|
||||
|
||||
def run(commands : Commands) -> Commands:
|
||||
def run(commands : List[Command]) -> List[Command]:
|
||||
return [ shutil.which('ffmpeg'), '-loglevel', 'error' ] + commands
|
||||
|
||||
|
||||
def chain(*commands : Commands) -> Commands:
|
||||
def chain(*commands : List[Command]) -> List[Command]:
|
||||
return list(itertools.chain(*commands))
|
||||
|
||||
|
||||
def get_encoders() -> Commands:
|
||||
def concat(*__commands__ : List[Command]) -> List[Command]:
|
||||
commands = []
|
||||
command_set : CommandSet = {}
|
||||
|
||||
for command in __commands__:
|
||||
for argument, value in zip(command[::2], command[1::2]):
|
||||
command_set.setdefault(argument, []).append(value)
|
||||
|
||||
for argument, values in command_set.items():
|
||||
commands.append(argument)
|
||||
commands.append(','.join(values))
|
||||
|
||||
return commands
|
||||
|
||||
|
||||
def get_encoders() -> List[Command]:
|
||||
return [ '-encoders' ]
|
||||
|
||||
|
||||
def set_hardware_accelerator(value : str) -> Commands:
|
||||
def set_hardware_accelerator(value : str) -> List[Command]:
|
||||
return [ '-hwaccel', value ]
|
||||
|
||||
|
||||
def set_progress() -> Commands:
|
||||
def set_progress() -> List[Command]:
|
||||
return [ '-progress' ]
|
||||
|
||||
|
||||
def set_input(input_path : str) -> Commands:
|
||||
def set_input(input_path : str) -> List[Command]:
|
||||
return [ '-i', input_path ]
|
||||
|
||||
|
||||
def set_input_fps(input_fps : Fps) -> Commands:
|
||||
return [ '-r', str(input_fps)]
|
||||
def set_input_fps(input_fps : Fps) -> List[Command]:
|
||||
return [ '-r', str(input_fps) ]
|
||||
|
||||
|
||||
def set_output(output_path : str) -> Commands:
|
||||
def set_start_number(frame_number : int) -> List[Command]:
|
||||
return [ '-start_number', str(frame_number) ]
|
||||
|
||||
|
||||
def set_output(output_path : str) -> List[Command]:
|
||||
return [ output_path ]
|
||||
|
||||
|
||||
def force_output(output_path : str) -> Commands:
|
||||
def force_output(output_path : str) -> List[Command]:
|
||||
return [ '-y', output_path ]
|
||||
|
||||
|
||||
def cast_stream() -> Commands:
|
||||
def set_loop() -> List[Command]:
|
||||
return [ '-loop', '1' ]
|
||||
|
||||
|
||||
def cast_stream() -> List[Command]:
|
||||
return [ '-' ]
|
||||
|
||||
|
||||
def set_stream_mode(stream_mode : StreamMode) -> Commands:
|
||||
def set_stream_mode(stream_mode : StreamMode) -> List[Command]:
|
||||
if stream_mode == 'udp':
|
||||
return [ '-f', 'mpegts' ]
|
||||
if stream_mode == 'v4l2':
|
||||
@@ -56,25 +79,35 @@ def set_stream_mode(stream_mode : StreamMode) -> Commands:
|
||||
return []
|
||||
|
||||
|
||||
def set_stream_quality(stream_quality : int) -> Commands:
|
||||
def set_stream_quality(stream_quality : int) -> List[Command]:
|
||||
return [ '-b:v', str(stream_quality) + 'k' ]
|
||||
|
||||
|
||||
def unsafe_concat() -> Commands:
|
||||
def unsafe_concat() -> List[Command]:
|
||||
return [ '-f', 'concat', '-safe', '0' ]
|
||||
|
||||
|
||||
def set_pixel_format(video_encoder : VideoEncoder) -> Commands:
|
||||
def enforce_pixel_format(pixel_format : str) -> List[Command]:
|
||||
return [ '-pix_fmt', pixel_format ]
|
||||
|
||||
|
||||
def strip_metadata() -> List[Command]:
|
||||
return [ '-map_metadata', '-1' ]
|
||||
|
||||
|
||||
def set_pixel_format(video_encoder : VideoEncoder) -> List[Command]:
|
||||
if video_encoder == 'rawvideo':
|
||||
return [ '-pix_fmt', 'rgb24' ]
|
||||
if video_encoder == 'libvpx-vp9':
|
||||
return [ '-pix_fmt', 'yuva420p' ]
|
||||
return [ '-pix_fmt', 'yuv420p' ]
|
||||
|
||||
|
||||
def set_frame_quality(frame_quality : int) -> Commands:
|
||||
def set_frame_quality(frame_quality : int) -> List[Command]:
|
||||
return [ '-q:v', str(frame_quality) ]
|
||||
|
||||
|
||||
def select_frame_range(frame_start : int, frame_end : int, video_fps : Fps) -> Commands:
|
||||
def select_frame_range(frame_start : int, frame_end : int, video_fps : Fps) -> List[Command]:
|
||||
if isinstance(frame_start, int) and isinstance(frame_end, int):
|
||||
return [ '-vf', 'trim=start_frame=' + str(frame_start) + ':end_frame=' + str(frame_end) + ',fps=' + str(video_fps) ]
|
||||
if isinstance(frame_start, int):
|
||||
@@ -84,11 +117,11 @@ def select_frame_range(frame_start : int, frame_end : int, video_fps : Fps) -> C
|
||||
return [ '-vf', 'fps=' + str(video_fps) ]
|
||||
|
||||
|
||||
def prevent_frame_drop() -> Commands:
|
||||
def prevent_frame_drop() -> List[Command]:
|
||||
return [ '-vsync', '0' ]
|
||||
|
||||
|
||||
def select_media_range(frame_start : int, frame_end : int, media_fps : Fps) -> Commands:
|
||||
def select_media_range(frame_start : int, frame_end : int, media_fps : Fps) -> List[Command]:
|
||||
commands = []
|
||||
|
||||
if isinstance(frame_start, int):
|
||||
@@ -98,35 +131,31 @@ def select_media_range(frame_start : int, frame_end : int, media_fps : Fps) -> C
|
||||
return commands
|
||||
|
||||
|
||||
def select_media_stream(media_stream : str) -> Commands:
|
||||
def select_media_stream(media_stream : str) -> List[Command]:
|
||||
return [ '-map', media_stream ]
|
||||
|
||||
|
||||
def set_media_resolution(video_resolution : str) -> Commands:
|
||||
def set_media_resolution(video_resolution : str) -> List[Command]:
|
||||
return [ '-s', video_resolution ]
|
||||
|
||||
|
||||
def set_image_quality(image_path : str, image_quality : int) -> Commands:
|
||||
if get_file_format(image_path) == 'webp':
|
||||
image_compression = image_quality
|
||||
else:
|
||||
image_compression = round(31 - (image_quality * 0.31))
|
||||
return [ '-q:v', str(image_compression) ]
|
||||
def deep_copy_audio() -> List[Command]:
|
||||
return [ '-q:a', '0' ]
|
||||
|
||||
|
||||
def set_audio_encoder(audio_codec : str) -> Commands:
|
||||
def set_audio_encoder(audio_codec : str) -> List[Command]:
|
||||
return [ '-c:a', audio_codec ]
|
||||
|
||||
|
||||
def copy_audio_encoder() -> Commands:
|
||||
def copy_audio_encoder() -> List[Command]:
|
||||
return set_audio_encoder('copy')
|
||||
|
||||
|
||||
def set_audio_sample_rate(audio_sample_rate : int) -> Commands:
|
||||
def set_audio_sample_rate(audio_sample_rate : SampleRate) -> List[Command]:
|
||||
return [ '-ar', str(audio_sample_rate) ]
|
||||
|
||||
|
||||
def set_audio_sample_size(audio_sample_size : int) -> Commands:
|
||||
def set_audio_sample_size(audio_sample_size : int) -> List[Command]:
|
||||
if audio_sample_size == 16:
|
||||
return [ '-f', 's16le' ]
|
||||
if audio_sample_size == 32:
|
||||
@@ -134,62 +163,90 @@ def set_audio_sample_size(audio_sample_size : int) -> Commands:
|
||||
return []
|
||||
|
||||
|
||||
def set_audio_channel_total(audio_channel_total : int) -> Commands:
|
||||
def set_audio_channel_total(audio_channel_total : int) -> List[Command]:
|
||||
return [ '-ac', str(audio_channel_total) ]
|
||||
|
||||
|
||||
def set_audio_quality(audio_encoder : AudioEncoder, audio_quality : int) -> Commands:
|
||||
def set_audio_quality(audio_encoder : AudioEncoder, audio_quality : int) -> List[Command]:
|
||||
if audio_encoder == 'aac':
|
||||
audio_compression = round(numpy.interp(audio_quality, [ 0, 100 ], [ 0.1, 2.0 ]), 1)
|
||||
audio_compression = numpy.round(numpy.interp(audio_quality, [ 0, 100 ], [ 0.1, 2.0 ]), 1).astype(float).item()
|
||||
return [ '-q:a', str(audio_compression) ]
|
||||
if audio_encoder == 'libmp3lame':
|
||||
audio_compression = round(numpy.interp(audio_quality, [ 0, 100 ], [ 9, 0 ]))
|
||||
audio_compression = numpy.round(numpy.interp(audio_quality, [ 0, 100 ], [ 9, 0 ])).astype(int).item()
|
||||
return [ '-q:a', str(audio_compression) ]
|
||||
if audio_encoder == 'libopus':
|
||||
audio_bit_rate = round(numpy.interp(audio_quality, [ 0, 100 ], [ 64, 256 ]))
|
||||
audio_bit_rate = numpy.round(numpy.interp(audio_quality, [ 0, 100 ], [ 64, 256 ])).astype(int).item()
|
||||
return [ '-b:a', str(audio_bit_rate) + 'k' ]
|
||||
if audio_encoder == 'libvorbis':
|
||||
audio_compression = round(numpy.interp(audio_quality, [ 0, 100 ], [ -1, 10 ]), 1)
|
||||
audio_compression = numpy.round(numpy.interp(audio_quality, [ 0, 100 ], [ -1, 10 ]), 1).astype(float).item()
|
||||
return [ '-q:a', str(audio_compression) ]
|
||||
return []
|
||||
|
||||
|
||||
def set_audio_volume(audio_volume : int) -> Commands:
|
||||
def set_audio_volume(audio_volume : int) -> List[Command]:
|
||||
return [ '-filter:a', 'volume=' + str(audio_volume / 100) ]
|
||||
|
||||
|
||||
def set_video_encoder(video_encoder : str) -> Commands:
|
||||
def deep_copy_image() -> List[Command]:
|
||||
return [ '-q:v', '0' ]
|
||||
|
||||
|
||||
def set_image_quality(image_path : str, image_quality : int) -> List[Command]:
|
||||
if get_file_format(image_path) == 'webp':
|
||||
return [ '-q:v', str(image_quality) ]
|
||||
|
||||
image_compression = round(31 - (image_quality * 0.31))
|
||||
return [ '-q:v', str(image_compression) ]
|
||||
|
||||
|
||||
def deep_copy_video() -> List[Command]:
|
||||
return [ '-q:v', '0' ]
|
||||
|
||||
|
||||
def set_video_encoder(video_encoder : str) -> List[Command]:
|
||||
return [ '-c:v', video_encoder ]
|
||||
|
||||
|
||||
def copy_video_encoder() -> Commands:
|
||||
def copy_video_encoder() -> List[Command]:
|
||||
return set_video_encoder('copy')
|
||||
|
||||
|
||||
def set_video_quality(video_encoder : VideoEncoder, video_quality : int) -> Commands:
|
||||
if video_encoder in [ 'libx264', 'libx265' ]:
|
||||
video_compression = round(numpy.interp(video_quality, [ 0, 100 ], [ 51, 0 ]))
|
||||
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]:
|
||||
if video_encoder in [ 'libx264', 'libx264rgb', 'libx265' ]:
|
||||
video_compression = numpy.round(numpy.interp(video_quality, [ 0, 100 ], [ 51, 0 ])).astype(int).item()
|
||||
return [ '-crf', str(video_compression) ]
|
||||
if video_encoder == 'libvpx-vp9':
|
||||
video_compression = round(numpy.interp(video_quality, [ 0, 100 ], [ 63, 0 ]))
|
||||
video_compression = numpy.round(numpy.interp(video_quality, [ 0, 100 ], [ 63, 0 ])).astype(int).item()
|
||||
return [ '-crf', str(video_compression) ]
|
||||
if video_encoder in [ 'h264_nvenc', 'hevc_nvenc' ]:
|
||||
video_compression = round(numpy.interp(video_quality, [ 0, 100 ], [ 51, 0 ]))
|
||||
video_compression = numpy.round(numpy.interp(video_quality, [ 0, 100 ], [ 51, 0 ])).astype(int).item()
|
||||
return [ '-cq', str(video_compression) ]
|
||||
if video_encoder in [ 'h264_amf', 'hevc_amf' ]:
|
||||
video_compression = round(numpy.interp(video_quality, [ 0, 100 ], [ 51, 0 ]))
|
||||
video_compression = numpy.round(numpy.interp(video_quality, [ 0, 100 ], [ 51, 0 ])).astype(int).item()
|
||||
return [ '-qp_i', str(video_compression), '-qp_p', str(video_compression), '-qp_b', str(video_compression) ]
|
||||
if video_encoder in [ 'h264_qsv', 'hevc_qsv' ]:
|
||||
video_compression = round(numpy.interp(video_quality, [ 0, 100 ], [ 51, 0 ]))
|
||||
video_compression = numpy.round(numpy.interp(video_quality, [ 0, 100 ], [ 51, 0 ])).astype(int).item()
|
||||
return [ '-qp', str(video_compression) ]
|
||||
if video_encoder in [ 'h264_videotoolbox', 'hevc_videotoolbox' ]:
|
||||
video_bit_rate = round(numpy.interp(video_quality, [ 0, 100 ], [ 1024, 50512 ]))
|
||||
video_bit_rate = numpy.round(numpy.interp(video_quality, [ 0, 100 ], [ 1024, 50512 ])).astype(int).item()
|
||||
return [ '-b:v', str(video_bit_rate) + 'k' ]
|
||||
return []
|
||||
|
||||
|
||||
def set_video_preset(video_encoder : VideoEncoder, video_preset : VideoPreset) -> Commands:
|
||||
if video_encoder in [ 'libx264', 'libx265' ]:
|
||||
def set_video_preset(video_encoder : VideoEncoder, video_preset : VideoPreset) -> List[Command]:
|
||||
if video_encoder in [ 'libx264', 'libx264rgb', 'libx265' ]:
|
||||
return [ '-preset', video_preset ]
|
||||
if video_encoder in [ 'h264_nvenc', 'hevc_nvenc' ]:
|
||||
return [ '-preset', map_nvenc_preset(video_preset) ]
|
||||
@@ -200,23 +257,25 @@ def set_video_preset(video_encoder : VideoEncoder, video_preset : VideoPreset) -
|
||||
return []
|
||||
|
||||
|
||||
def set_video_colorspace(video_colorspace : str) -> Commands:
|
||||
return [ '-colorspace', video_colorspace ]
|
||||
def set_video_fps(video_fps : Fps) -> List[Command]:
|
||||
return [ '-vf', 'fps=' + str(video_fps) ]
|
||||
|
||||
|
||||
def set_video_fps(video_fps : Fps) -> Commands:
|
||||
return [ '-vf', 'framerate=fps=' + str(video_fps) ]
|
||||
|
||||
|
||||
def set_video_duration(video_duration : Duration) -> Commands:
|
||||
def set_video_duration(video_duration : Duration) -> List[Command]:
|
||||
return [ '-t', str(video_duration) ]
|
||||
|
||||
|
||||
def capture_video() -> Commands:
|
||||
def keep_video_alpha(video_encoder : VideoEncoder) -> List[Command]:
|
||||
if video_encoder == 'libvpx-vp9':
|
||||
return [ '-vf', 'format=yuva420p' ]
|
||||
return []
|
||||
|
||||
|
||||
def capture_video() -> List[Command]:
|
||||
return [ '-f', 'rawvideo', '-pix_fmt', 'rgb24' ]
|
||||
|
||||
|
||||
def ignore_video_stream() -> Commands:
|
||||
def ignore_video_stream() -> List[Command]:
|
||||
return [ '-vn' ]
|
||||
|
||||
|
||||
@@ -246,3 +305,5 @@ def map_qsv_preset(video_preset : VideoPreset) -> Optional[str]:
|
||||
if video_preset in [ 'faster', 'fast', 'medium', 'slow', 'slower', 'veryslow' ]:
|
||||
return video_preset
|
||||
return None
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
import subprocess
|
||||
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 probe_entries(media_path : str, entries : List[str]) -> Dict[str, str]:
|
||||
media_entries = {}
|
||||
|
||||
commands = ffprobe_builder.chain(
|
||||
ffprobe_builder.show_entries(entries),
|
||||
ffprobe_builder.format_to_key_value(),
|
||||
ffprobe_builder.set_input(media_path)
|
||||
)
|
||||
output, _ = run_ffprobe(commands).communicate()
|
||||
|
||||
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 extract_audio_metadata(audio_path : str) -> AudioMetadata:
|
||||
audio_entries = probe_entries(audio_path, [ 'duration', 'sample_rate', 'channels', 'bit_rate' ])
|
||||
|
||||
duration = float(audio_entries.get('duration'))
|
||||
sample_rate = int(audio_entries.get('sample_rate'))
|
||||
frame_total = int(duration * sample_rate)
|
||||
channel_total = int(audio_entries.get('channels'))
|
||||
bit_rate = int(audio_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
|
||||
|
||||
|
||||
def extract_video_metadata(video_path : str) -> VideoMetadata:
|
||||
video_entries = probe_entries(video_path, [ 'duration', 'width', 'height', 'r_frame_rate', 'bit_rate' ])
|
||||
|
||||
duration = float(video_entries.get('duration'))
|
||||
fps = extract_video_fps(video_entries.get('r_frame_rate'))
|
||||
frame_total = int(duration * fps)
|
||||
width = int(video_entries.get('width'))
|
||||
height = int(video_entries.get('height'))
|
||||
bit_rate = int(video_entries.get('bit_rate'))
|
||||
|
||||
video_metadata : VideoMetadata =\
|
||||
{
|
||||
'duration' : duration,
|
||||
'frame_total' : frame_total,
|
||||
'fps' : fps,
|
||||
'resolution' : (width, height),
|
||||
'bit_rate' : bit_rate
|
||||
}
|
||||
|
||||
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
|
||||
@@ -0,0 +1,25 @@
|
||||
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 show_entries(entries : List[str]) -> List[Command]:
|
||||
return [ '-show_entries', 'stream=' + ','.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 ]
|
||||
+10
-10
@@ -36,19 +36,12 @@ def get_file_format(file_path : str) -> Optional[str]:
|
||||
return 'jpeg'
|
||||
if file_extension == '.tif':
|
||||
return 'tiff'
|
||||
if file_extension == '.mpg':
|
||||
return 'mpeg'
|
||||
return file_extension.lstrip('.')
|
||||
return None
|
||||
|
||||
|
||||
def same_file_extension(first_file_path : str, second_file_path : str) -> bool:
|
||||
first_file_extension = get_file_extension(first_file_path)
|
||||
second_file_extension = get_file_extension(second_file_path)
|
||||
|
||||
if first_file_extension and second_file_extension:
|
||||
return get_file_extension(first_file_path) == get_file_extension(second_file_path)
|
||||
return False
|
||||
|
||||
|
||||
def is_file(file_path : str) -> bool:
|
||||
if file_path:
|
||||
return os.path.isfile(file_path)
|
||||
@@ -99,7 +92,7 @@ def has_video(video_paths : List[str]) -> bool:
|
||||
|
||||
def are_videos(video_paths : List[str]) -> bool:
|
||||
if video_paths:
|
||||
return any(map(is_video, video_paths))
|
||||
return all(map(is_video, video_paths))
|
||||
return False
|
||||
|
||||
|
||||
@@ -177,6 +170,13 @@ def create_directory(directory_path : str) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
def move_directory(directory_path : str, move_path : str) -> bool:
|
||||
if is_directory(directory_path):
|
||||
shutil.move(directory_path, move_path)
|
||||
return is_directory(move_path)
|
||||
return False
|
||||
|
||||
|
||||
def remove_directory(directory_path : str) -> bool:
|
||||
if is_directory(directory_path):
|
||||
shutil.rmtree(directory_path, ignore_errors = True)
|
||||
|
||||
@@ -3,10 +3,11 @@ import zlib
|
||||
from typing import Optional
|
||||
|
||||
from facefusion.filesystem import get_file_name, is_file
|
||||
from facefusion.types import Buffer
|
||||
|
||||
|
||||
def create_hash(content : bytes) -> str:
|
||||
return format(zlib.crc32(content), '08x')
|
||||
def create_hash(buffer : Buffer) -> str:
|
||||
return format(zlib.crc32(buffer), '08x')
|
||||
|
||||
|
||||
def validate_hash(validate_path : str) -> bool:
|
||||
|
||||
@@ -1,74 +1,102 @@
|
||||
import importlib
|
||||
from time import sleep
|
||||
import random
|
||||
from functools import lru_cache
|
||||
from time import sleep, time
|
||||
from typing import List
|
||||
|
||||
from onnxruntime import InferenceSession
|
||||
|
||||
from facefusion import process_manager, state_manager
|
||||
from facefusion import logger, process_manager, state_manager, translator
|
||||
from facefusion.app_context import detect_app_context
|
||||
from facefusion.execution import create_inference_session_providers
|
||||
from facefusion.filesystem import is_file
|
||||
from facefusion.types import DownloadSet, ExecutionProvider, InferencePool, InferencePoolSet
|
||||
from facefusion.common_helper import is_windows
|
||||
from facefusion.execution import create_inference_providers, has_execution_provider
|
||||
from facefusion.exit_helper import fatal_exit
|
||||
from facefusion.filesystem import get_file_name, is_file
|
||||
from facefusion.time_helper import calculate_end_time
|
||||
from facefusion.types import DownloadSet, ExecutionProvider, InferencePool, InferencePoolSet, InferenceProvider
|
||||
|
||||
INFERENCE_POOL_SET : InferencePoolSet =\
|
||||
{
|
||||
'cli': {},
|
||||
'ui': {}
|
||||
'api': {}
|
||||
}
|
||||
|
||||
|
||||
def get_inference_pool(module_name : str, model_names : List[str], model_source_set : DownloadSet) -> InferencePool:
|
||||
while process_manager.is_checking():
|
||||
sleep(0.5)
|
||||
execution_device_id = state_manager.get_item('execution_device_id')
|
||||
execution_providers = resolve_execution_providers(module_name)
|
||||
execution_device_ids = state_manager.get_item('execution_device_ids')
|
||||
execution_providers = state_manager.get_item('execution_providers')
|
||||
app_context = detect_app_context()
|
||||
inference_context = get_inference_context(module_name, model_names, execution_device_id, execution_providers)
|
||||
|
||||
if app_context == 'cli' and INFERENCE_POOL_SET.get('ui').get(inference_context):
|
||||
INFERENCE_POOL_SET['cli'][inference_context] = INFERENCE_POOL_SET.get('ui').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)
|
||||
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)
|
||||
for execution_device_id in execution_device_ids:
|
||||
inference_context = get_inference_context(module_name, model_names, execution_device_id, execution_providers)
|
||||
|
||||
return INFERENCE_POOL_SET.get(app_context).get(inference_context)
|
||||
if app_context == 'cli' and INFERENCE_POOL_SET.get('api').get(inference_context):
|
||||
INFERENCE_POOL_SET['cli'][inference_context] = INFERENCE_POOL_SET.get('api').get(inference_context)
|
||||
if app_context == 'api' and INFERENCE_POOL_SET.get('cli').get(inference_context):
|
||||
INFERENCE_POOL_SET['api'][inference_context] = INFERENCE_POOL_SET.get('cli').get(inference_context)
|
||||
if not INFERENCE_POOL_SET.get(app_context).get(inference_context):
|
||||
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)
|
||||
return INFERENCE_POOL_SET.get(app_context).get(current_inference_context)
|
||||
|
||||
|
||||
def create_inference_pool(model_source_set : DownloadSet, execution_device_id : str, execution_providers : List[ExecutionProvider]) -> InferencePool:
|
||||
def create_inference_pool(model_source_set : DownloadSet, inference_providers : List[InferenceProvider]) -> InferencePool:
|
||||
inference_pool : InferencePool = {}
|
||||
|
||||
for model_name in model_source_set.keys():
|
||||
model_path = model_source_set.get(model_name).get('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
|
||||
|
||||
|
||||
def clear_inference_pool(module_name : str, model_names : List[str]) -> None:
|
||||
execution_device_id = state_manager.get_item('execution_device_id')
|
||||
execution_providers = resolve_execution_providers(module_name)
|
||||
execution_device_ids = state_manager.get_item('execution_device_ids')
|
||||
execution_providers = state_manager.get_item('execution_providers')
|
||||
app_context = detect_app_context()
|
||||
inference_context = get_inference_context(module_name, model_names, execution_device_id, execution_providers)
|
||||
|
||||
if INFERENCE_POOL_SET.get(app_context).get(inference_context):
|
||||
del INFERENCE_POOL_SET[app_context][inference_context]
|
||||
if is_windows() and has_execution_provider('directml'):
|
||||
INFERENCE_POOL_SET[app_context].clear()
|
||||
|
||||
for execution_device_id in execution_device_ids:
|
||||
inference_context = get_inference_context(module_name, model_names, execution_device_id, execution_providers)
|
||||
if INFERENCE_POOL_SET.get(app_context).get(inference_context):
|
||||
del INFERENCE_POOL_SET[app_context][inference_context]
|
||||
|
||||
|
||||
def create_inference_session(model_path : str, execution_device_id : str, execution_providers : List[ExecutionProvider]) -> InferenceSession:
|
||||
inference_session_providers = create_inference_session_providers(execution_device_id, execution_providers)
|
||||
return InferenceSession(model_path, providers = inference_session_providers)
|
||||
def create_inference_session(model_path : str, inference_providers : List[InferenceProvider]) -> InferenceSession:
|
||||
model_file_name = get_file_name(model_path)
|
||||
start_time = time()
|
||||
|
||||
try:
|
||||
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__)
|
||||
return inference_session
|
||||
|
||||
except Exception:
|
||||
logger.error(translator.get('loading_model_failed').format(model_name = model_file_name), __name__)
|
||||
fatal_exit(1)
|
||||
|
||||
|
||||
def get_inference_context(module_name : str, model_names : List[str], execution_device_id : str, execution_providers : List[ExecutionProvider]) -> str:
|
||||
inference_context = '.'.join([ module_name ] + model_names + [ execution_device_id ] + list(execution_providers))
|
||||
def get_inference_context(module_name : str, model_names : List[str], execution_device_id : int, execution_providers : List[ExecutionProvider]) -> str:
|
||||
inference_context = '.'.join([ module_name ] + model_names + [ str(execution_device_id) ] + list(execution_providers))
|
||||
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)
|
||||
execution_providers = state_manager.get_item('execution_providers')
|
||||
|
||||
if hasattr(module, 'resolve_execution_providers'):
|
||||
return getattr(module, 'resolve_execution_providers')()
|
||||
return state_manager.get_item('execution_providers')
|
||||
if hasattr(module, 'resolve_inference_providers'):
|
||||
inference_providers = getattr(module, 'resolve_inference_providers')()
|
||||
|
||||
if inference_providers:
|
||||
return inference_providers
|
||||
|
||||
return create_inference_providers(execution_device_id, execution_providers)
|
||||
|
||||
+29
-51
@@ -7,27 +7,37 @@ from argparse import ArgumentParser, HelpFormatter
|
||||
from functools import partial
|
||||
from types import FrameType
|
||||
|
||||
from facefusion import metadata, wording
|
||||
from facefusion import metadata
|
||||
from facefusion.common_helper import is_linux, is_windows
|
||||
|
||||
LOCALES =\
|
||||
{
|
||||
'install_dependency': 'install the {dependency} package',
|
||||
'force_reinstall': 'force reinstall of packages',
|
||||
'skip_conda': 'skip the conda environment check',
|
||||
'conda_not_activated': 'conda is not activated'
|
||||
}
|
||||
ONNXRUNTIME_SET =\
|
||||
{
|
||||
'default': ('onnxruntime', '1.22.0')
|
||||
'default': ('onnxruntime', '1.26.0')
|
||||
}
|
||||
if is_windows() or is_linux():
|
||||
ONNXRUNTIME_SET['cuda'] = ('onnxruntime-gpu', '1.22.0')
|
||||
ONNXRUNTIME_SET['openvino'] = ('onnxruntime-openvino', '1.22.0')
|
||||
ONNXRUNTIME_SET['cuda'] = ('onnxruntime-gpu', '1.26.0')
|
||||
ONNXRUNTIME_SET['openvino'] = ('onnxruntime-openvino', '1.24.1')
|
||||
if is_windows():
|
||||
ONNXRUNTIME_SET['directml'] = ('onnxruntime-directml', '1.17.3')
|
||||
ONNXRUNTIME_SET['directml'] = ('onnxruntime-directml', '1.24.4')
|
||||
ONNXRUNTIME_SET['qnn'] = ('onnxruntime-qnn', '1.24.4')
|
||||
if is_linux():
|
||||
ONNXRUNTIME_SET['rocm'] = ('onnxruntime-rocm', '1.21.0')
|
||||
ONNXRUNTIME_SET['migraphx'] = ('onnxruntime-migraphx', '1.25.0')
|
||||
ONNXRUNTIME_SET['rocm'] = ('onnxruntime-rocm', '1.22.2.post1')
|
||||
|
||||
|
||||
def cli() -> None:
|
||||
signal.signal(signal.SIGINT, signal_exit)
|
||||
program = ArgumentParser(formatter_class = partial(HelpFormatter, max_help_position = 50))
|
||||
program.add_argument('--onnxruntime', help = wording.get('help.install_dependency').format(dependency = 'onnxruntime'), choices = ONNXRUNTIME_SET.keys(), required = True)
|
||||
program.add_argument('--skip-conda', help = wording.get('help.skip_conda'), action = 'store_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('--skip-conda', help = LOCALES.get('skip_conda'), action = 'store_true')
|
||||
program.add_argument('-v', '--version', version = metadata.get('name') + ' ' + metadata.get('version'), action = 'version')
|
||||
run(program)
|
||||
|
||||
@@ -39,58 +49,26 @@ def signal_exit(signum : int, frame : FrameType) -> None:
|
||||
def run(program : ArgumentParser) -> None:
|
||||
args = program.parse_args()
|
||||
has_conda = 'CONDA_PREFIX' in os.environ
|
||||
onnxruntime_name, onnxruntime_version = ONNXRUNTIME_SET.get(args.onnxruntime)
|
||||
|
||||
if not args.skip_conda and not has_conda:
|
||||
sys.stdout.write(wording.get('conda_not_activated') + os.linesep)
|
||||
sys.stdout.write(LOCALES.get('conda_not_activated') + os.linesep)
|
||||
sys.exit(1)
|
||||
|
||||
commands = [ shutil.which('pip'), 'install' ]
|
||||
|
||||
if args.force_reinstall:
|
||||
commands.append('--force-reinstall')
|
||||
|
||||
with open('requirements.txt') as file:
|
||||
|
||||
for line in file.readlines():
|
||||
__line__ = line.strip()
|
||||
if not __line__.startswith('onnxruntime'):
|
||||
subprocess.call([ shutil.which('pip'), 'install', line, '--force-reinstall' ])
|
||||
commands.append(__line__)
|
||||
|
||||
if args.onnxruntime == 'rocm':
|
||||
python_id = 'cp' + str(sys.version_info.major) + str(sys.version_info.minor)
|
||||
onnxruntime_name, onnxruntime_version = ONNXRUNTIME_SET.get(args.onnxruntime)
|
||||
commands.append(onnxruntime_name + '==' + onnxruntime_version)
|
||||
|
||||
if python_id in [ 'cp310', 'cp312' ]:
|
||||
wheel_name = 'onnxruntime_rocm-' + onnxruntime_version + '-' + python_id + '-' + python_id + '-linux_x86_64.whl'
|
||||
wheel_url = 'https://repo.radeon.com/rocm/manylinux/rocm-rel-6.4/' + wheel_name
|
||||
subprocess.call([ shutil.which('pip'), 'install', wheel_url, '--force-reinstall' ])
|
||||
else:
|
||||
subprocess.call([ shutil.which('pip'), 'install', onnxruntime_name + '==' + onnxruntime_version, '--force-reinstall' ])
|
||||
subprocess.call([ shutil.which('pip'), 'uninstall', 'onnxruntime', onnxruntime_name, '-y', '-q' ])
|
||||
|
||||
if args.onnxruntime == 'cuda' and has_conda:
|
||||
library_paths = []
|
||||
|
||||
if is_linux():
|
||||
if os.getenv('LD_LIBRARY_PATH'):
|
||||
library_paths = os.getenv('LD_LIBRARY_PATH').split(os.pathsep)
|
||||
|
||||
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')
|
||||
])
|
||||
library_paths = list(dict.fromkeys([ library_path for library_path in library_paths if os.path.exists(library_path) ]))
|
||||
|
||||
subprocess.call([ shutil.which('conda'), 'env', 'config', 'vars', 'set', 'LD_LIBRARY_PATH=' + os.pathsep.join(library_paths) ])
|
||||
|
||||
if is_windows():
|
||||
if os.getenv('PATH'):
|
||||
library_paths = os.getenv('PATH').split(os.pathsep)
|
||||
|
||||
library_paths.extend(
|
||||
[
|
||||
os.path.join(os.getenv('CONDA_PREFIX'), 'Lib'),
|
||||
os.path.join(os.getenv('CONDA_PREFIX'), 'Lib', 'site-packages', 'tensorrt_libs')
|
||||
])
|
||||
library_paths = list(dict.fromkeys([ library_path for library_path in library_paths if os.path.exists(library_path) ]))
|
||||
|
||||
subprocess.call([ shutil.which('conda'), 'env', 'config', 'vars', 'set', 'PATH=' + os.pathsep.join(library_paths) ])
|
||||
|
||||
if args.onnxruntime == 'directml':
|
||||
subprocess.call([ shutil.which('pip'), 'install', 'numpy==1.26.4', '--force-reinstall' ])
|
||||
subprocess.call(commands)
|
||||
|
||||
@@ -7,10 +7,14 @@ from facefusion.filesystem import get_file_extension, get_file_name
|
||||
|
||||
def get_step_output_path(job_id : str, step_index : int, output_path : str) -> Optional[str]:
|
||||
if output_path:
|
||||
output_directory_path, _ = os.path.split(output_path)
|
||||
output_file_name = get_file_name(_)
|
||||
output_file_extension = get_file_extension(_)
|
||||
return os.path.join(output_directory_path, output_file_name + '-' + job_id + '-' + str(step_index) + output_file_extension)
|
||||
output_directory_path, output_file_path = os.path.split(output_path)
|
||||
output_file_name = get_file_name(output_file_path)
|
||||
output_file_extension = get_file_extension(output_file_path)
|
||||
|
||||
if output_file_name and output_file_extension:
|
||||
return os.path.join(output_directory_path, output_file_name + '-' + job_id + '-' + str(step_index) + output_file_extension)
|
||||
if output_file_path and output_directory_path:
|
||||
return os.path.join(output_directory_path, output_file_path + '-' + job_id + '-' + str(step_index))
|
||||
return None
|
||||
|
||||
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
from datetime import datetime
|
||||
from typing import Optional, Tuple
|
||||
from typing import List, Optional, Tuple
|
||||
|
||||
from facefusion.date_helper import describe_time_ago
|
||||
from facefusion.jobs import job_manager
|
||||
from facefusion.types import JobStatus, TableContents, TableHeaders
|
||||
from facefusion.time_helper import describe_time_ago
|
||||
from facefusion.types import JobStatus, TableContent, TableHeader
|
||||
|
||||
|
||||
def compose_job_list(job_status : JobStatus) -> Tuple[TableHeaders, TableContents]:
|
||||
def compose_job_list(job_status : JobStatus) -> Tuple[List[TableHeader], List[List[TableContent]]]:
|
||||
jobs = job_manager.find_jobs(job_status)
|
||||
job_headers : TableHeaders = [ 'job id', 'steps', 'date created', 'date updated', 'job status' ]
|
||||
job_contents : TableContents = []
|
||||
job_headers : List[TableHeader] = [ 'job id', 'steps', 'date created', 'date updated', 'job status' ]
|
||||
job_contents : List[List[TableContent]] = []
|
||||
|
||||
for index, job_id in enumerate(jobs):
|
||||
if job_manager.validate_job(job_id):
|
||||
|
||||
@@ -3,10 +3,11 @@ from copy import copy
|
||||
from typing import List, Optional
|
||||
|
||||
import facefusion.choices
|
||||
from facefusion.date_helper import get_current_date_time
|
||||
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.json import read_json, write_json
|
||||
from facefusion.sanitizer import sanitize_job_id
|
||||
from facefusion.time_helper import get_current_date_time
|
||||
from facefusion.types import Args, Job, JobSet, JobStatus, JobStep, JobStepStatus
|
||||
|
||||
JOBS_PATH : Optional[str] = None
|
||||
@@ -261,5 +262,6 @@ def find_job_path(job_id : str) -> Optional[str]:
|
||||
|
||||
def get_job_file_name(job_id : str) -> Optional[str]:
|
||||
if job_id:
|
||||
job_id = sanitize_job_id(job_id)
|
||||
return job_id + '.json'
|
||||
return None
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import os
|
||||
|
||||
from facefusion.ffmpeg import concat_video
|
||||
from facefusion.filesystem import are_images, are_videos, move_file, remove_file
|
||||
from facefusion.filesystem import are_images, are_videos, copy_file, create_directory, is_directory, is_file, move_directory, move_file, remove_directory, remove_file, resolve_file_paths
|
||||
from facefusion.jobs import job_helper, job_manager
|
||||
from facefusion.types import JobOutputSet, JobStep, ProcessStep
|
||||
|
||||
@@ -59,6 +61,8 @@ def run_step(job_id : str, step_index : int, step : JobStep, process_step : Proc
|
||||
output_path = step_args.get('output_path')
|
||||
step_output_path = job_helper.get_step_output_path(job_id, step_index, output_path)
|
||||
|
||||
if is_directory(output_path):
|
||||
return move_directory(output_path, step_output_path) and job_manager.set_step_status(job_id, step_index, 'completed')
|
||||
return move_file(output_path, step_output_path) and job_manager.set_step_status(job_id, step_index, 'completed')
|
||||
job_manager.set_step_status(job_id, step_index, 'failed')
|
||||
return False
|
||||
@@ -79,13 +83,26 @@ def finalize_steps(job_id : str) -> bool:
|
||||
output_set = collect_output_set(job_id)
|
||||
|
||||
for output_path, temp_output_paths in output_set.items():
|
||||
if are_videos(temp_output_paths):
|
||||
has_videos = are_videos(temp_output_paths)
|
||||
has_images = are_images(temp_output_paths)
|
||||
|
||||
if has_videos:
|
||||
if not concat_video(output_path, temp_output_paths):
|
||||
return False
|
||||
if are_images(temp_output_paths):
|
||||
if not has_videos and has_images:
|
||||
for temp_output_path in temp_output_paths:
|
||||
if not move_file(temp_output_path, output_path):
|
||||
return False
|
||||
if not has_videos and not has_images:
|
||||
if not create_directory(output_path):
|
||||
return False
|
||||
|
||||
for temp_output_path in temp_output_paths:
|
||||
if is_directory(temp_output_path):
|
||||
temp_frame_paths = resolve_file_paths(temp_output_path)
|
||||
for temp_frame_path in temp_frame_paths:
|
||||
if not copy_file(temp_frame_path, os.path.join(output_path, os.path.basename(temp_frame_path))):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
@@ -94,8 +111,12 @@ def clean_steps(job_id: str) -> bool:
|
||||
|
||||
for temp_output_paths in output_set.values():
|
||||
for temp_output_path in temp_output_paths:
|
||||
if not remove_file(temp_output_path):
|
||||
return False
|
||||
if is_file(temp_output_path):
|
||||
if not remove_file(temp_output_path):
|
||||
return False
|
||||
if is_directory(temp_output_path):
|
||||
if not remove_directory(temp_output_path):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
from typing import List
|
||||
|
||||
from facefusion.types import JobStore
|
||||
|
||||
JOB_STORE : JobStore =\
|
||||
{
|
||||
'job_keys': [],
|
||||
'step_keys': []
|
||||
}
|
||||
|
||||
|
||||
def get_job_keys() -> List[str]:
|
||||
return JOB_STORE.get('job_keys')
|
||||
|
||||
|
||||
def get_step_keys() -> List[str]:
|
||||
return JOB_STORE.get('step_keys')
|
||||
|
||||
|
||||
def register_job_keys(step_keys : List[str]) -> None:
|
||||
for step_key in step_keys:
|
||||
JOB_STORE['job_keys'].append(step_key)
|
||||
|
||||
|
||||
def register_step_keys(job_keys : List[str]) -> None:
|
||||
for job_key in job_keys:
|
||||
JOB_STORE['step_keys'].append(job_key)
|
||||
@@ -0,0 +1,115 @@
|
||||
import ctypes
|
||||
import ctypes.util
|
||||
from functools import lru_cache
|
||||
from typing import List, Optional
|
||||
|
||||
|
||||
@lru_cache
|
||||
def create_static_library() -> Optional[ctypes.CDLL]:
|
||||
library_path = ctypes.util.find_library('amd_smi')
|
||||
|
||||
if library_path:
|
||||
library = ctypes.CDLL(library_path)
|
||||
|
||||
if library:
|
||||
return init_ctypes(library)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def init_ctypes(library : ctypes.CDLL) -> ctypes.CDLL:
|
||||
library.amdsmi_init.argtypes = [ ctypes.c_uint64 ]
|
||||
library.amdsmi_init.restype = ctypes.c_uint32
|
||||
|
||||
library.amdsmi_shut_down.argtypes = []
|
||||
library.amdsmi_shut_down.restype = ctypes.c_uint32
|
||||
|
||||
library.amdsmi_get_socket_handles.argtypes = [ ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_void_p) ]
|
||||
library.amdsmi_get_socket_handles.restype = ctypes.c_uint32
|
||||
|
||||
library.amdsmi_get_processor_handles.argtypes = [ ctypes.c_void_p, ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_void_p) ]
|
||||
library.amdsmi_get_processor_handles.restype = ctypes.c_uint32
|
||||
|
||||
library.amdsmi_get_gpu_vram_usage.argtypes = [ ctypes.c_void_p, ctypes.c_void_p ]
|
||||
library.amdsmi_get_gpu_vram_usage.restype = ctypes.c_uint32
|
||||
|
||||
library.amdsmi_get_gpu_activity.argtypes = [ ctypes.c_void_p, ctypes.c_void_p ]
|
||||
library.amdsmi_get_gpu_activity.restype = ctypes.c_uint32
|
||||
|
||||
library.amdsmi_get_gpu_asic_info.argtypes = [ ctypes.c_void_p, ctypes.c_void_p ]
|
||||
library.amdsmi_get_gpu_asic_info.restype = ctypes.c_uint32
|
||||
|
||||
library.amdsmi_get_temp_metric.argtypes = [ ctypes.c_void_p, ctypes.c_uint32, ctypes.c_uint32, ctypes.POINTER(ctypes.c_int64) ]
|
||||
library.amdsmi_get_temp_metric.restype = ctypes.c_uint32
|
||||
|
||||
return library
|
||||
|
||||
|
||||
def find_device_handles(amd_smi_library : ctypes.CDLL) -> List[ctypes.c_void_p]:
|
||||
device_handles : List[ctypes.c_void_p] = []
|
||||
|
||||
socket_count = ctypes.c_uint32()
|
||||
amd_smi_library.amdsmi_get_socket_handles(ctypes.byref(socket_count), ctypes.POINTER(ctypes.c_void_p)())
|
||||
socket_handles = (ctypes.c_void_p * socket_count.value)()
|
||||
amd_smi_library.amdsmi_get_socket_handles(ctypes.byref(socket_count), socket_handles)
|
||||
|
||||
for socket_index in range(socket_count.value):
|
||||
device_count = ctypes.c_uint32()
|
||||
amd_smi_library.amdsmi_get_processor_handles(socket_handles[socket_index], ctypes.byref(device_count), ctypes.POINTER(ctypes.c_void_p)())
|
||||
processor_handles = (ctypes.c_void_p * device_count.value)()
|
||||
amd_smi_library.amdsmi_get_processor_handles(socket_handles[socket_index], ctypes.byref(device_count), processor_handles)
|
||||
|
||||
for device_index in range(device_count.value):
|
||||
device_handles.append(ctypes.c_void_p(processor_handles[device_index]))
|
||||
|
||||
return device_handles
|
||||
|
||||
|
||||
def define_product_info() -> ctypes.Structure:
|
||||
return type('AMDSMI_ASIC_INFO', (ctypes.Structure,),
|
||||
{
|
||||
'_pack_': 1,
|
||||
'_fields_':
|
||||
[
|
||||
('market_name', ctypes.c_char * 256),
|
||||
('vendor_id', ctypes.c_uint32),
|
||||
('vendor_name', ctypes.c_char * 256),
|
||||
('subvendor_id', ctypes.c_uint32),
|
||||
('device_id', ctypes.c_uint64),
|
||||
('rev_id', ctypes.c_uint32),
|
||||
('asic_serial', ctypes.c_char * 256),
|
||||
('oam_id', ctypes.c_uint32),
|
||||
('num_of_compute_units', ctypes.c_uint32),
|
||||
('padding', ctypes.c_ubyte * 4),
|
||||
('target_graphics_version', ctypes.c_uint64),
|
||||
('subsystem_id', ctypes.c_uint32),
|
||||
('reserved', ctypes.c_uint32 * 21)
|
||||
]
|
||||
})()
|
||||
|
||||
|
||||
def define_device_memory() -> ctypes.Structure:
|
||||
return type('AMDSMI_VRAM_USAGE', (ctypes.Structure,),
|
||||
{
|
||||
'_pack_': 1,
|
||||
'_fields_':
|
||||
[
|
||||
('vram_total', ctypes.c_uint32),
|
||||
('vram_used', ctypes.c_uint32),
|
||||
('reserved', ctypes.c_uint32 * 2)
|
||||
]
|
||||
})()
|
||||
|
||||
|
||||
def define_device_utilization() -> ctypes.Structure:
|
||||
return type('AMDSMI_ENGINE_USAGE', (ctypes.Structure,),
|
||||
{
|
||||
'_pack_': 1,
|
||||
'_fields_':
|
||||
[
|
||||
('gfx_activity', ctypes.c_uint32),
|
||||
('umc_activity', ctypes.c_uint32),
|
||||
('mm_activity', ctypes.c_uint32),
|
||||
('reserved', ctypes.c_uint32 * 13)
|
||||
]
|
||||
})()
|
||||
@@ -0,0 +1,134 @@
|
||||
import ctypes
|
||||
from functools import lru_cache
|
||||
from typing import Optional
|
||||
|
||||
from facefusion.common_helper import is_linux, is_macos, is_windows
|
||||
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url_by_provider
|
||||
from facefusion.filesystem import resolve_relative_path
|
||||
from facefusion.types import LibrarySet
|
||||
|
||||
|
||||
@lru_cache
|
||||
def create_static_library_set() -> Optional[LibrarySet]:
|
||||
if is_linux():
|
||||
return\
|
||||
{
|
||||
'hashes':
|
||||
{
|
||||
'aom':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'linux/libaom.hash'),
|
||||
'path': resolve_relative_path('../.libraries/libaom.hash')
|
||||
}
|
||||
},
|
||||
'sources':
|
||||
{
|
||||
'aom':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'linux/libaom.so'),
|
||||
'path': resolve_relative_path('../.libraries/libaom.so')
|
||||
}
|
||||
}
|
||||
}
|
||||
if is_macos():
|
||||
return\
|
||||
{
|
||||
'hashes':
|
||||
{
|
||||
'aom':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'macos/libaom.hash'),
|
||||
'path': resolve_relative_path('../.libraries/libaom.hash')
|
||||
}
|
||||
},
|
||||
'sources':
|
||||
{
|
||||
'aom':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'macos/libaom.dylib'),
|
||||
'path': resolve_relative_path('../.libraries/libaom.dylib')
|
||||
}
|
||||
}
|
||||
}
|
||||
if is_windows():
|
||||
return\
|
||||
{
|
||||
'hashes':
|
||||
{
|
||||
'aom':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'windows/aom.hash'),
|
||||
'path': resolve_relative_path('../.libraries/aom.hash')
|
||||
}
|
||||
},
|
||||
'sources':
|
||||
{
|
||||
'aom':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'windows/aom.dll'),
|
||||
'path': resolve_relative_path('../.libraries/aom.dll')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def pre_check() -> bool:
|
||||
library_hash_set = create_static_library_set().get('hashes')
|
||||
library_source_set = create_static_library_set().get('sources')
|
||||
|
||||
return conditional_download_hashes(library_hash_set) and conditional_download_sources(library_source_set)
|
||||
|
||||
|
||||
@lru_cache
|
||||
def create_static_library() -> Optional[ctypes.CDLL]:
|
||||
library_path = create_static_library_set().get('sources').get('aom').get('path')
|
||||
|
||||
if library_path:
|
||||
if is_windows():
|
||||
library = ctypes.CDLL(library_path, winmode = 0)
|
||||
else:
|
||||
library = ctypes.CDLL(library_path)
|
||||
|
||||
if library:
|
||||
return init_ctypes(library)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def init_ctypes(library : ctypes.CDLL) -> ctypes.CDLL:
|
||||
library.aom_codec_enc_config_default.argtypes = [ ctypes.c_void_p, ctypes.c_void_p, ctypes.c_uint ]
|
||||
library.aom_codec_enc_config_default.restype = ctypes.c_int
|
||||
|
||||
library.aom_codec_enc_init_ver.argtypes = [ ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_long, ctypes.c_int ]
|
||||
library.aom_codec_enc_init_ver.restype = ctypes.c_int
|
||||
|
||||
library.aom_codec_encode.argtypes = [ ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int64, ctypes.c_ulong, ctypes.c_long, ctypes.c_ulong ]
|
||||
library.aom_codec_encode.restype = ctypes.c_int
|
||||
|
||||
library.aom_codec_get_cx_data.argtypes = [ ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p) ]
|
||||
library.aom_codec_get_cx_data.restype = ctypes.c_void_p
|
||||
|
||||
library.aom_codec_enc_config_set.argtypes = [ ctypes.c_void_p, ctypes.c_void_p ]
|
||||
library.aom_codec_enc_config_set.restype = ctypes.c_int
|
||||
|
||||
library.aom_codec_destroy.argtypes = [ ctypes.c_void_p ]
|
||||
library.aom_codec_destroy.restype = ctypes.c_int
|
||||
|
||||
library.aom_img_wrap.argtypes = [ ctypes.c_void_p, ctypes.c_int, ctypes.c_uint, ctypes.c_uint, ctypes.c_uint, ctypes.c_void_p ]
|
||||
library.aom_img_wrap.restype = ctypes.c_void_p
|
||||
|
||||
library.aom_codec_control.argtypes = [ ctypes.c_void_p, ctypes.c_int, ctypes.c_int ]
|
||||
library.aom_codec_control.restype = ctypes.c_int
|
||||
|
||||
library.aom_codec_dec_init_ver.argtypes = [ ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_long, ctypes.c_int ]
|
||||
library.aom_codec_dec_init_ver.restype = ctypes.c_int
|
||||
|
||||
library.aom_codec_decode.argtypes = [ ctypes.c_void_p, ctypes.c_void_p, ctypes.c_uint, ctypes.c_void_p ]
|
||||
library.aom_codec_decode.restype = ctypes.c_int
|
||||
|
||||
library.aom_codec_get_frame.argtypes = [ ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p) ]
|
||||
library.aom_codec_get_frame.restype = ctypes.c_void_p
|
||||
|
||||
return library
|
||||
@@ -0,0 +1,294 @@
|
||||
import ctypes
|
||||
from functools import lru_cache
|
||||
from typing import Optional
|
||||
|
||||
from facefusion.common_helper import is_linux, is_macos, is_windows
|
||||
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url_by_provider
|
||||
from facefusion.filesystem import resolve_relative_path
|
||||
from facefusion.types import LibrarySet
|
||||
|
||||
|
||||
@lru_cache
|
||||
def create_static_library_set() -> Optional[LibrarySet]:
|
||||
if is_linux():
|
||||
return\
|
||||
{
|
||||
'hashes':
|
||||
{
|
||||
'crypto':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'linux/libcrypto.hash'),
|
||||
'path': resolve_relative_path('../.libraries/libcrypto.hash')
|
||||
},
|
||||
'datachannel':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'linux/libdatachannel_next.hash'),
|
||||
'path': resolve_relative_path('../.libraries/libdatachannel_next.hash')
|
||||
},
|
||||
'ssl':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'linux/libssl.hash'),
|
||||
'path': resolve_relative_path('../.libraries/libssl.hash')
|
||||
}
|
||||
},
|
||||
'sources':
|
||||
{
|
||||
'crypto':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'linux/libcrypto.so'),
|
||||
'path': resolve_relative_path('../.libraries/libcrypto.so')
|
||||
},
|
||||
'datachannel':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'linux/libdatachannel_next.so'),
|
||||
'path': resolve_relative_path('../.libraries/libdatachannel_next.so')
|
||||
},
|
||||
'ssl':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'linux/libssl.so'),
|
||||
'path': resolve_relative_path('../.libraries/libssl.so')
|
||||
}
|
||||
}
|
||||
}
|
||||
if is_macos():
|
||||
return\
|
||||
{
|
||||
'hashes':
|
||||
{
|
||||
'crypto':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'macos/libcrypto.hash'),
|
||||
'path': resolve_relative_path('../.libraries/libcrypto.hash')
|
||||
},
|
||||
'datachannel':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'macos/libdatachannel_next.hash'),
|
||||
'path': resolve_relative_path('../.libraries/libdatachannel_next.hash')
|
||||
},
|
||||
'ssl':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'macos/libssl.hash'),
|
||||
'path': resolve_relative_path('../.libraries/libssl.hash')
|
||||
}
|
||||
},
|
||||
'sources':
|
||||
{
|
||||
'crypto':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'macos/libcrypto.dylib'),
|
||||
'path': resolve_relative_path('../.libraries/libcrypto.dylib')
|
||||
},
|
||||
'datachannel':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'macos/libdatachannel_next.dylib'),
|
||||
'path': resolve_relative_path('../.libraries/libdatachannel_next.dylib')
|
||||
},
|
||||
'ssl':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'macos/libssl.dylib'),
|
||||
'path': resolve_relative_path('../.libraries/libssl.dylib')
|
||||
}
|
||||
}
|
||||
}
|
||||
if is_windows():
|
||||
return\
|
||||
{
|
||||
'hashes':
|
||||
{
|
||||
'crypto':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'windows/crypto.hash'),
|
||||
'path': resolve_relative_path('../.libraries/crypto.hash')
|
||||
},
|
||||
'datachannel':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'windows/datachannel_next.hash'),
|
||||
'path': resolve_relative_path('../.libraries/datachannel_next.hash')
|
||||
},
|
||||
'ssl':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'windows/ssl.hash'),
|
||||
'path': resolve_relative_path('../.libraries/ssl.hash')
|
||||
}
|
||||
},
|
||||
'sources':
|
||||
{
|
||||
'crypto':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'windows/crypto.dll'),
|
||||
'path': resolve_relative_path('../.libraries/crypto.dll')
|
||||
},
|
||||
'datachannel':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'windows/datachannel_next.dll'),
|
||||
'path': resolve_relative_path('../.libraries/datachannel_next.dll')
|
||||
},
|
||||
'ssl':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'windows/ssl.dll'),
|
||||
'path': resolve_relative_path('../.libraries/ssl.dll')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def pre_check() -> bool:
|
||||
library_hash_set = create_static_library_set().get('hashes')
|
||||
library_source_set = create_static_library_set().get('sources')
|
||||
|
||||
return conditional_download_hashes(library_hash_set) and conditional_download_sources(library_source_set)
|
||||
|
||||
|
||||
@lru_cache
|
||||
def create_static_library() -> Optional[ctypes.CDLL]:
|
||||
crypto_source_path = create_static_library_set().get('sources').get('crypto').get('path')
|
||||
datachannel_source_path = create_static_library_set().get('sources').get('datachannel').get('path')
|
||||
ssl_source_path = create_static_library_set().get('sources').get('ssl').get('path')
|
||||
|
||||
if crypto_source_path and datachannel_source_path and ssl_source_path:
|
||||
if is_windows():
|
||||
ctypes.CDLL(crypto_source_path, winmode = 0)
|
||||
ctypes.CDLL(ssl_source_path, winmode = 0)
|
||||
library = ctypes.CDLL(datachannel_source_path, winmode = 0)
|
||||
else:
|
||||
ctypes.CDLL(crypto_source_path)
|
||||
ctypes.CDLL(ssl_source_path)
|
||||
library = ctypes.CDLL(datachannel_source_path)
|
||||
|
||||
if library:
|
||||
return init_ctypes(library)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def init_ctypes(library : ctypes.CDLL) -> ctypes.CDLL:
|
||||
library.rtcInitLogger.argtypes = [ ctypes.c_int, ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_char_p) ]
|
||||
library.rtcInitLogger.restype = None
|
||||
library.rtcInitLogger(2, ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_char_p)(0))
|
||||
|
||||
library.rtcCreatePeerConnection.restype = ctypes.c_int
|
||||
|
||||
library.rtcDeletePeerConnection.argtypes = [ ctypes.c_int ]
|
||||
library.rtcDeletePeerConnection.restype = ctypes.c_int
|
||||
|
||||
library.rtcSetLocalDescription.argtypes = [ ctypes.c_int, ctypes.c_char_p ]
|
||||
library.rtcSetLocalDescription.restype = ctypes.c_int
|
||||
|
||||
library.rtcSetRemoteDescription.argtypes = [ ctypes.c_int, ctypes.c_char_p, ctypes.c_char_p ]
|
||||
library.rtcSetRemoteDescription.restype = ctypes.c_int
|
||||
|
||||
library.rtcAddTrackEx.restype = ctypes.c_int
|
||||
|
||||
library.rtcSendMessage.argtypes = [ ctypes.c_int, ctypes.c_void_p, ctypes.c_int ]
|
||||
library.rtcSendMessage.restype = ctypes.c_int
|
||||
|
||||
library.rtcSetAV1Packetizer.restype = ctypes.c_int
|
||||
library.rtcSetVP8Packetizer.restype = ctypes.c_int
|
||||
library.rtcSetVP9Packetizer.restype = ctypes.c_int
|
||||
|
||||
library.rtcChainRtcpSrReporter.argtypes = [ ctypes.c_int ]
|
||||
library.rtcChainRtcpSrReporter.restype = ctypes.c_int
|
||||
|
||||
library.rtcSetTrackRtpTimestamp.argtypes = [ ctypes.c_int, ctypes.c_uint32 ]
|
||||
library.rtcSetTrackRtpTimestamp.restype = ctypes.c_int
|
||||
|
||||
library.rtcIsOpen.argtypes = [ ctypes.c_int ]
|
||||
library.rtcIsOpen.restype = ctypes.c_bool
|
||||
|
||||
library.rtcChainRtcpNackResponder.argtypes = [ ctypes.c_int, ctypes.c_uint ]
|
||||
library.rtcChainRtcpNackResponder.restype = ctypes.c_int
|
||||
|
||||
library.rtcGetLocalDescription.argtypes = [ ctypes.c_int, ctypes.c_char_p, ctypes.c_int ]
|
||||
library.rtcGetLocalDescription.restype = ctypes.c_int
|
||||
|
||||
library.rtcSetOpusPacketizer.restype = ctypes.c_int
|
||||
|
||||
library.rtcGetPayloadTypesForCodec.argtypes = [ ctypes.c_char_p, ctypes.c_char_p, ctypes.POINTER(ctypes.c_int), ctypes.c_int ]
|
||||
library.rtcGetPayloadTypesForCodec.restype = ctypes.c_int
|
||||
|
||||
library.rtcSetAV1Depacketizer.argtypes = [ ctypes.c_int, ctypes.c_int ]
|
||||
library.rtcSetAV1Depacketizer.restype = ctypes.c_int
|
||||
library.rtcSetVP8Depacketizer.restype = ctypes.c_int
|
||||
library.rtcSetVP9Depacketizer.restype = ctypes.c_int
|
||||
library.rtcSetOpusDepacketizer.restype = ctypes.c_int
|
||||
|
||||
library.rtcChainRtcpReceivingSession.argtypes = [ ctypes.c_int ]
|
||||
library.rtcChainRtcpReceivingSession.restype = ctypes.c_int
|
||||
|
||||
library.rtcSetFrameCallback.argtypes = [ ctypes.c_int, ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p) ]
|
||||
library.rtcSetFrameCallback.restype = ctypes.c_int
|
||||
|
||||
library.rtcSetUserPointer.argtypes = [ ctypes.c_int, ctypes.c_void_p ]
|
||||
library.rtcSetUserPointer.restype = None
|
||||
|
||||
library.rtcChainRembHandler.argtypes = [ ctypes.c_int, ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_uint, ctypes.c_void_p) ]
|
||||
library.rtcChainRembHandler.restype = ctypes.c_int
|
||||
|
||||
library.rtcRequestBitrate.argtypes = [ ctypes.c_int, ctypes.c_uint ]
|
||||
library.rtcRequestBitrate.restype = ctypes.c_int
|
||||
|
||||
library.rtcSetClosedCallback.argtypes = [ ctypes.c_int, ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_void_p) ]
|
||||
library.rtcSetClosedCallback.restype = ctypes.c_int
|
||||
|
||||
return library
|
||||
|
||||
|
||||
def define_rtc_configuration() -> ctypes.Structure:
|
||||
return type('RTC_CONFIGURATION', (ctypes.Structure,),
|
||||
{
|
||||
'_fields_':
|
||||
[
|
||||
('iceServers', ctypes.POINTER(ctypes.c_char_p)),
|
||||
('iceServersCount', ctypes.c_int),
|
||||
('proxyServer', ctypes.c_char_p),
|
||||
('bindAddress', ctypes.c_char_p),
|
||||
('certificateType', ctypes.c_int),
|
||||
('iceTransportPolicy', ctypes.c_int),
|
||||
('enableIceTcp', ctypes.c_bool),
|
||||
('enableIceUdpMux', ctypes.c_bool),
|
||||
('disableAutoNegotiation', ctypes.c_bool),
|
||||
('forceMediaTransport', ctypes.c_bool),
|
||||
('portRangeBegin', ctypes.c_ushort),
|
||||
('portRangeEnd', ctypes.c_ushort),
|
||||
('mtu', ctypes.c_int),
|
||||
('maxMessageSize', ctypes.c_int)
|
||||
]
|
||||
})()
|
||||
|
||||
|
||||
def define_rtc_track_init() -> ctypes.Structure:
|
||||
return type('RTC_TRACK_INIT', (ctypes.Structure,),
|
||||
{
|
||||
'_fields_':
|
||||
[
|
||||
('direction', ctypes.c_int),
|
||||
('codec', ctypes.c_int),
|
||||
('payloadType', ctypes.c_int),
|
||||
('ssrc', ctypes.c_uint32),
|
||||
('mid', ctypes.c_char_p),
|
||||
('name', ctypes.c_char_p),
|
||||
('msid', ctypes.c_char_p),
|
||||
('trackId', ctypes.c_char_p),
|
||||
('profile', ctypes.c_char_p)
|
||||
]
|
||||
})()
|
||||
|
||||
|
||||
def define_rtc_packetizer_init() -> ctypes.Structure:
|
||||
return type('RTC_PACKETIZER_INIT', (ctypes.Structure,),
|
||||
{
|
||||
'_fields_':
|
||||
[
|
||||
('ssrc', ctypes.c_uint32),
|
||||
('cname', ctypes.c_char_p),
|
||||
('payloadType', ctypes.c_uint8),
|
||||
('clockRate', ctypes.c_uint32),
|
||||
('sequenceNumber', ctypes.c_uint16),
|
||||
('timestamp', ctypes.c_uint32),
|
||||
('maxFragmentSize', ctypes.c_uint16),
|
||||
('nalSeparator', ctypes.c_int),
|
||||
('obuPacketization', ctypes.c_int)
|
||||
]
|
||||
})()
|
||||
@@ -0,0 +1,88 @@
|
||||
import ctypes
|
||||
import ctypes.util
|
||||
from functools import lru_cache
|
||||
from typing import List, Optional
|
||||
|
||||
|
||||
@lru_cache
|
||||
def create_static_library() -> Optional[ctypes.CDLL]:
|
||||
library_path = ctypes.util.find_library('nvidia-ml') or ctypes.util.find_library('nvml')
|
||||
|
||||
if library_path:
|
||||
library = ctypes.CDLL(library_path)
|
||||
|
||||
if library:
|
||||
return init_ctypes(library)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def init_ctypes(library : ctypes.CDLL) -> ctypes.CDLL:
|
||||
library.nvmlInit_v2.argtypes = []
|
||||
library.nvmlInit_v2.restype = ctypes.c_int
|
||||
|
||||
library.nvmlShutdown.argtypes = []
|
||||
library.nvmlShutdown.restype = ctypes.c_int
|
||||
|
||||
library.nvmlDeviceGetCount_v2.argtypes = [ ctypes.POINTER(ctypes.c_uint) ]
|
||||
library.nvmlDeviceGetCount_v2.restype = ctypes.c_int
|
||||
|
||||
library.nvmlSystemGetDriverVersion.argtypes = [ ctypes.c_char_p, ctypes.c_uint ]
|
||||
library.nvmlSystemGetDriverVersion.restype = ctypes.c_int
|
||||
|
||||
library.nvmlSystemGetCudaDriverVersion.argtypes = [ ctypes.POINTER(ctypes.c_int) ]
|
||||
library.nvmlSystemGetCudaDriverVersion.restype = ctypes.c_int
|
||||
|
||||
library.nvmlDeviceGetHandleByIndex_v2.argtypes = [ ctypes.c_uint, ctypes.POINTER(ctypes.c_void_p) ]
|
||||
library.nvmlDeviceGetHandleByIndex_v2.restype = ctypes.c_int
|
||||
|
||||
library.nvmlDeviceGetName.argtypes = [ ctypes.c_void_p, ctypes.c_char_p, ctypes.c_uint ]
|
||||
library.nvmlDeviceGetName.restype = ctypes.c_int
|
||||
|
||||
library.nvmlDeviceGetMemoryInfo.argtypes = [ ctypes.c_void_p, ctypes.c_void_p ]
|
||||
library.nvmlDeviceGetMemoryInfo.restype = ctypes.c_int
|
||||
|
||||
library.nvmlDeviceGetTemperature.argtypes = [ ctypes.c_void_p, ctypes.c_int, ctypes.POINTER(ctypes.c_uint) ]
|
||||
library.nvmlDeviceGetTemperature.restype = ctypes.c_int
|
||||
|
||||
library.nvmlDeviceGetUtilizationRates.argtypes = [ ctypes.c_void_p, ctypes.c_void_p ]
|
||||
library.nvmlDeviceGetUtilizationRates.restype = ctypes.c_int
|
||||
|
||||
return library
|
||||
|
||||
|
||||
def find_device_handles(nvidia_ml_library : ctypes.CDLL) -> List[ctypes.c_void_p]:
|
||||
device_handles : List[ctypes.c_void_p] = []
|
||||
|
||||
device_count = ctypes.c_uint()
|
||||
nvidia_ml_library.nvmlDeviceGetCount_v2(ctypes.byref(device_count))
|
||||
|
||||
for device_id in range(device_count.value):
|
||||
device_handle = ctypes.c_void_p()
|
||||
nvidia_ml_library.nvmlDeviceGetHandleByIndex_v2(device_id, ctypes.byref(device_handle))
|
||||
device_handles.append(device_handle)
|
||||
|
||||
return device_handles
|
||||
|
||||
|
||||
def define_device_memory() -> ctypes.Structure:
|
||||
return type('NVML_MEMORY', (ctypes.Structure,),
|
||||
{
|
||||
'_fields_':
|
||||
[
|
||||
('total', ctypes.c_ulonglong),
|
||||
('free', ctypes.c_ulonglong),
|
||||
('used', ctypes.c_ulonglong)
|
||||
]
|
||||
})()
|
||||
|
||||
|
||||
def define_device_utilization() -> ctypes.Structure:
|
||||
return type('NVML_UTILIZATION', (ctypes.Structure,),
|
||||
{
|
||||
'_fields_':
|
||||
[
|
||||
('gpu', ctypes.c_uint),
|
||||
('memory', ctypes.c_uint)
|
||||
]
|
||||
})()
|
||||
@@ -0,0 +1,124 @@
|
||||
import ctypes
|
||||
from functools import lru_cache
|
||||
from typing import Optional
|
||||
|
||||
from facefusion.common_helper import is_linux, is_macos, is_windows
|
||||
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url_by_provider
|
||||
from facefusion.filesystem import resolve_relative_path
|
||||
from facefusion.types import LibrarySet
|
||||
|
||||
|
||||
@lru_cache
|
||||
def create_static_library_set() -> Optional[LibrarySet]:
|
||||
if is_linux():
|
||||
return\
|
||||
{
|
||||
'hashes':
|
||||
{
|
||||
'opus':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'linux/libopus.hash'),
|
||||
'path': resolve_relative_path('../.libraries/libopus.hash')
|
||||
}
|
||||
},
|
||||
'sources':
|
||||
{
|
||||
'opus':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'linux/libopus.so'),
|
||||
'path': resolve_relative_path('../.libraries/libopus.so')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if is_macos():
|
||||
return\
|
||||
{
|
||||
'hashes':
|
||||
{
|
||||
'opus':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'macos/libopus.hash'),
|
||||
'path': resolve_relative_path('../.libraries/libopus.hash')
|
||||
}
|
||||
},
|
||||
'sources':
|
||||
{
|
||||
'opus':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'macos/libopus.dylib'),
|
||||
'path': resolve_relative_path('../.libraries/libopus.dylib')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if is_windows():
|
||||
return\
|
||||
{
|
||||
'hashes':
|
||||
{
|
||||
'opus':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'windows/opus.hash'),
|
||||
'path': resolve_relative_path('../.libraries/opus.hash')
|
||||
}
|
||||
},
|
||||
'sources':
|
||||
{
|
||||
'opus':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'windows/opus.dll'),
|
||||
'path': resolve_relative_path('../.libraries/opus.dll')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def pre_check() -> bool:
|
||||
library_hash_set = create_static_library_set().get('hashes')
|
||||
library_source_set = create_static_library_set().get('sources')
|
||||
|
||||
return conditional_download_hashes(library_hash_set) and conditional_download_sources(library_source_set)
|
||||
|
||||
|
||||
@lru_cache
|
||||
def create_static_library() -> Optional[ctypes.CDLL]:
|
||||
library_path = create_static_library_set().get('sources').get('opus').get('path')
|
||||
|
||||
if library_path:
|
||||
if is_windows():
|
||||
library = ctypes.CDLL(library_path, winmode = 0)
|
||||
else:
|
||||
library = ctypes.CDLL(library_path)
|
||||
|
||||
if library:
|
||||
return init_ctypes(library)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def init_ctypes(library : ctypes.CDLL) -> ctypes.CDLL:
|
||||
library.opus_encoder_create.argtypes = [ ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.POINTER(ctypes.c_int) ]
|
||||
library.opus_encoder_create.restype = ctypes.c_void_p
|
||||
|
||||
library.opus_encode_float.argtypes = [ ctypes.c_void_p, ctypes.POINTER(ctypes.c_float), ctypes.c_int, ctypes.c_char_p, ctypes.c_int ]
|
||||
library.opus_encode_float.restype = ctypes.c_int
|
||||
|
||||
library.opus_encoder_destroy.argtypes = [ ctypes.c_void_p ]
|
||||
library.opus_encoder_destroy.restype = None
|
||||
|
||||
library.opus_decoder_create.argtypes = [ ctypes.c_int, ctypes.c_int, ctypes.POINTER(ctypes.c_int) ]
|
||||
library.opus_decoder_create.restype = ctypes.c_void_p
|
||||
|
||||
library.opus_decode_float.argtypes = [ ctypes.c_void_p, ctypes.c_char_p, ctypes.c_int, ctypes.POINTER(ctypes.c_float), ctypes.c_int, ctypes.c_int ]
|
||||
library.opus_decode_float.restype = ctypes.c_int
|
||||
|
||||
library.opus_decoder_get_nb_samples.argtypes = [ ctypes.c_void_p, ctypes.c_char_p, ctypes.c_int ]
|
||||
library.opus_decoder_get_nb_samples.restype = ctypes.c_int
|
||||
|
||||
library.opus_decoder_destroy.argtypes = [ ctypes.c_void_p ]
|
||||
library.opus_decoder_destroy.restype = None
|
||||
|
||||
return library
|
||||
@@ -0,0 +1,24 @@
|
||||
import ctypes
|
||||
import ctypes.util
|
||||
from functools import lru_cache
|
||||
from typing import Optional
|
||||
|
||||
|
||||
@lru_cache
|
||||
def create_static_library() -> Optional[ctypes.CDLL]:
|
||||
library_path = ctypes.util.find_library('rocm-core')
|
||||
|
||||
if library_path:
|
||||
library = ctypes.CDLL(library_path)
|
||||
|
||||
if library:
|
||||
return init_ctypes(library)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def init_ctypes(library : ctypes.CDLL) -> ctypes.CDLL:
|
||||
library.getROCmVersion.argtypes = [ ctypes.POINTER(ctypes.c_uint), ctypes.POINTER(ctypes.c_uint), ctypes.POINTER(ctypes.c_uint) ]
|
||||
library.getROCmVersion.restype = ctypes.c_int
|
||||
|
||||
return library
|
||||
@@ -0,0 +1,134 @@
|
||||
import ctypes
|
||||
from functools import lru_cache
|
||||
from typing import Optional
|
||||
|
||||
from facefusion.common_helper import is_linux, is_macos, is_windows
|
||||
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url_by_provider
|
||||
from facefusion.filesystem import resolve_relative_path
|
||||
from facefusion.types import LibrarySet
|
||||
|
||||
|
||||
@lru_cache
|
||||
def create_static_library_set() -> Optional[LibrarySet]:
|
||||
if is_linux():
|
||||
return\
|
||||
{
|
||||
'hashes':
|
||||
{
|
||||
'vpx':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'linux/libvpx.hash'),
|
||||
'path': resolve_relative_path('../.libraries/libvpx.hash')
|
||||
}
|
||||
},
|
||||
'sources':
|
||||
{
|
||||
'vpx':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'linux/libvpx.so'),
|
||||
'path': resolve_relative_path('../.libraries/libvpx.so')
|
||||
}
|
||||
}
|
||||
}
|
||||
if is_macos():
|
||||
return\
|
||||
{
|
||||
'hashes':
|
||||
{
|
||||
'vpx':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'macos/libvpx.hash'),
|
||||
'path': resolve_relative_path('../.libraries/libvpx.hash')
|
||||
}
|
||||
},
|
||||
'sources':
|
||||
{
|
||||
'vpx':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'macos/libvpx.dylib'),
|
||||
'path': resolve_relative_path('../.libraries/libvpx.dylib')
|
||||
}
|
||||
}
|
||||
}
|
||||
if is_windows():
|
||||
return\
|
||||
{
|
||||
'hashes':
|
||||
{
|
||||
'vpx':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'windows/vpx.hash'),
|
||||
'path': resolve_relative_path('../.libraries/vpx.hash')
|
||||
}
|
||||
},
|
||||
'sources':
|
||||
{
|
||||
'vpx':
|
||||
{
|
||||
'url': resolve_download_url_by_provider('huggingface', 'libraries-4.0.0', 'windows/vpx.dll'),
|
||||
'path': resolve_relative_path('../.libraries/vpx.dll')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def pre_check() -> bool:
|
||||
library_hash_set = create_static_library_set().get('hashes')
|
||||
library_source_set = create_static_library_set().get('sources')
|
||||
|
||||
return conditional_download_hashes(library_hash_set) and conditional_download_sources(library_source_set)
|
||||
|
||||
|
||||
@lru_cache
|
||||
def create_static_library() -> Optional[ctypes.CDLL]:
|
||||
library_path = create_static_library_set().get('sources').get('vpx').get('path')
|
||||
|
||||
if library_path:
|
||||
if is_windows():
|
||||
library = ctypes.CDLL(library_path, winmode = 0)
|
||||
else:
|
||||
library = ctypes.CDLL(library_path)
|
||||
|
||||
if library:
|
||||
return init_ctypes(library)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def init_ctypes(library : ctypes.CDLL) -> ctypes.CDLL:
|
||||
library.vpx_codec_enc_config_default.argtypes = [ ctypes.c_void_p, ctypes.c_void_p, ctypes.c_uint ]
|
||||
library.vpx_codec_enc_config_default.restype = ctypes.c_int
|
||||
|
||||
library.vpx_codec_enc_init_ver.argtypes = [ ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_long, ctypes.c_int ]
|
||||
library.vpx_codec_enc_init_ver.restype = ctypes.c_int
|
||||
|
||||
library.vpx_codec_encode.argtypes = [ ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int64, ctypes.c_ulong, ctypes.c_long, ctypes.c_ulong ]
|
||||
library.vpx_codec_encode.restype = ctypes.c_int
|
||||
|
||||
library.vpx_codec_get_cx_data.argtypes = [ ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p) ]
|
||||
library.vpx_codec_get_cx_data.restype = ctypes.c_void_p
|
||||
|
||||
library.vpx_codec_enc_config_set.argtypes = [ ctypes.c_void_p, ctypes.c_void_p ]
|
||||
library.vpx_codec_enc_config_set.restype = ctypes.c_int
|
||||
|
||||
library.vpx_codec_destroy.argtypes = [ ctypes.c_void_p ]
|
||||
library.vpx_codec_destroy.restype = ctypes.c_int
|
||||
|
||||
library.vpx_img_wrap.argtypes = [ ctypes.c_void_p, ctypes.c_int, ctypes.c_uint, ctypes.c_uint, ctypes.c_uint, ctypes.c_void_p ]
|
||||
library.vpx_img_wrap.restype = ctypes.c_void_p
|
||||
|
||||
library.vpx_codec_control_.argtypes = [ ctypes.c_void_p, ctypes.c_int, ctypes.c_int ]
|
||||
library.vpx_codec_control_.restype = ctypes.c_int
|
||||
|
||||
library.vpx_codec_dec_init_ver.argtypes = [ ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_long, ctypes.c_int ]
|
||||
library.vpx_codec_dec_init_ver.restype = ctypes.c_int
|
||||
|
||||
library.vpx_codec_decode.argtypes = [ ctypes.c_void_p, ctypes.c_void_p, ctypes.c_uint, ctypes.c_void_p, ctypes.c_long ]
|
||||
library.vpx_codec_decode.restype = ctypes.c_int
|
||||
|
||||
library.vpx_codec_get_frame.argtypes = [ ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p) ]
|
||||
library.vpx_codec_get_frame.restype = ctypes.c_void_p
|
||||
|
||||
return library
|
||||
@@ -0,0 +1,279 @@
|
||||
from facefusion.types import Locales
|
||||
|
||||
LOCALES : Locales =\
|
||||
{
|
||||
'en':
|
||||
{
|
||||
'conda_not_activated': 'conda is not activated',
|
||||
'python_not_supported': 'python version is not supported, upgrade to {version} or higher',
|
||||
'curl_not_installed': 'curl is not installed',
|
||||
'ffmpeg_not_installed': 'ffmpeg is not installed',
|
||||
'creating_temp': 'creating temporary resources',
|
||||
'extracting_frames': 'extracting frames with a resolution of {resolution} and {fps} frames per second',
|
||||
'extracting_frames_succeeded': 'extracting frames succeeded',
|
||||
'extracting_frames_failed': 'extracting frames failed',
|
||||
'spawning_frames_succeeded': 'spawning frames succeeded',
|
||||
'spawning_frames_failed': 'spawning frames failed',
|
||||
'analysing': 'analysing',
|
||||
'extracting': 'extracting',
|
||||
'spawning': 'spawning',
|
||||
'streaming': 'streaming',
|
||||
'processing': 'processing',
|
||||
'merging': 'merging',
|
||||
'downloading': 'downloading',
|
||||
'temp_frames_not_found': 'temporary frames not found',
|
||||
'copying_image': 'copying image with a resolution of {resolution}',
|
||||
'copying_image_succeeded': 'copying image succeeded',
|
||||
'copying_image_failed': 'copying image failed',
|
||||
'finalizing_image': 'finalizing image with a resolution of {resolution}',
|
||||
'finalizing_image_succeeded': 'finalizing image succeeded',
|
||||
'finalizing_image_skipped': 'finalizing image skipped',
|
||||
'merging_video': 'merging video with a resolution of {resolution} and {fps} frames per second',
|
||||
'merging_video_succeeded': 'merging video succeeded',
|
||||
'merging_video_failed': 'merging video failed',
|
||||
'skipping_audio': 'skipping audio',
|
||||
'replacing_audio_succeeded': 'replacing audio succeeded',
|
||||
'replacing_audio_skipped': 'replacing audio skipped',
|
||||
'restoring_audio_succeeded': 'restoring audio succeeded',
|
||||
'restoring_audio_skipped': 'restoring audio skipped',
|
||||
'clearing_temp': 'clearing temporary resources',
|
||||
'processing_stopped': 'processing stopped',
|
||||
'processing_image_succeeded': 'processing to image succeeded in {seconds} seconds',
|
||||
'processing_image_failed': 'processing to image failed',
|
||||
'processing_frames_succeeded': 'processing to frames succeeded in {seconds} seconds',
|
||||
'processing_frames_failed': 'processing to frames failed',
|
||||
'processing_video_succeeded': 'processing to video succeeded in {seconds} seconds',
|
||||
'processing_video_failed': 'processing to video failed',
|
||||
'choose_image_source': 'choose an image for the source',
|
||||
'choose_audio_source': 'choose an audio for the source',
|
||||
'choose_video_target': 'choose a video for the target',
|
||||
'choose_image_or_video_target': 'choose an image or video for the target',
|
||||
'specify_image_or_video_output': 'specify the output image or video within a directory',
|
||||
'match_target_and_output_extension': 'match the target and output extension',
|
||||
'no_source_face_detected': 'no source face detected',
|
||||
'processor_not_loaded': 'processor {processor} could not be loaded',
|
||||
'processor_not_implemented': 'processor {processor} not implemented correctly',
|
||||
'stream_not_loaded': 'stream {stream_mode} could not be loaded',
|
||||
'stream_not_supported': 'stream not supported',
|
||||
'api_started': 'started API on {host}:{port}',
|
||||
'job_created': 'job {job_id} created',
|
||||
'job_not_created': 'job {job_id} not created',
|
||||
'job_submitted': 'job {job_id} submitted',
|
||||
'job_not_submitted': 'job {job_id} not submitted',
|
||||
'job_all_submitted': 'jobs submitted',
|
||||
'job_all_not_submitted': 'jobs not submitted',
|
||||
'job_deleted': 'job {job_id} deleted',
|
||||
'job_not_deleted': 'job {job_id} not deleted',
|
||||
'job_all_deleted': 'jobs deleted',
|
||||
'job_all_not_deleted': 'jobs not deleted',
|
||||
'job_step_added': 'step added to job {job_id}',
|
||||
'job_step_not_added': 'step not added to job {job_id}',
|
||||
'job_remix_step_added': 'step {step_index} remixed from job {job_id}',
|
||||
'job_remix_step_not_added': 'step {step_index} not remixed from job {job_id}',
|
||||
'job_step_inserted': 'step {step_index} inserted to job {job_id}',
|
||||
'job_step_not_inserted': 'step {step_index} not inserted to job {job_id}',
|
||||
'job_step_removed': 'step {step_index} removed from job {job_id}',
|
||||
'job_step_not_removed': 'step {step_index} not removed from job {job_id}',
|
||||
'running_job': 'running queued job {job_id}',
|
||||
'running_jobs': 'running all queued jobs',
|
||||
'retrying_job': 'retrying failed job {job_id}',
|
||||
'retrying_jobs': 'retrying all failed jobs',
|
||||
'processing_job_succeeded': 'processing of job {job_id} succeeded',
|
||||
'processing_jobs_succeeded': 'processing of all jobs succeeded',
|
||||
'processing_job_failed': 'processing of job {job_id} failed',
|
||||
'processing_jobs_failed': 'processing of all jobs failed',
|
||||
'processing_step': 'processing step {step_current} of {step_total}',
|
||||
'validating_hash_succeeded': 'validating hash for {hash_file_name} succeeded',
|
||||
'validating_hash_failed': 'validating hash for {hash_file_name} failed',
|
||||
'validating_source_succeeded': 'validating source for {source_file_name} succeeded',
|
||||
'validating_source_failed': 'validating source for {source_file_name} failed',
|
||||
'deleting_corrupt_source': 'deleting corrupt source for {source_file_name}',
|
||||
'loading_model_succeeded': 'loading model {model_name} succeeded in {seconds} seconds',
|
||||
'loading_model_failed': 'loading model {model_name} failed',
|
||||
'time_ago_now': 'just now',
|
||||
'time_ago_minutes': '{minutes} minutes ago',
|
||||
'time_ago_hours': '{hours} hours and {minutes} minutes ago',
|
||||
'time_ago_days': '{days} days, {hours} hours and {minutes} minutes ago',
|
||||
'point': '.',
|
||||
'comma': ',',
|
||||
'colon': ':',
|
||||
'question_mark': '?',
|
||||
'exclamation_mark': '!',
|
||||
'help':
|
||||
{
|
||||
'install_dependency': 'choose the variant of {dependency} to install',
|
||||
'skip_conda': 'skip the conda environment check',
|
||||
'workflow_mode': 'choose the workflow mode',
|
||||
'config_path': 'choose the config file to override defaults',
|
||||
'temp_path': 'specify the directory for the temporary resources',
|
||||
'jobs_path': 'specify the directory to store jobs',
|
||||
'source_paths': 'choose the image or audio paths',
|
||||
'target_path': 'choose the image or video path',
|
||||
'output_path': 'specify the image or video within a directory',
|
||||
'source_pattern': 'choose the image or audio pattern',
|
||||
'target_pattern': 'choose the image or video pattern',
|
||||
'output_pattern': 'specify the image or video pattern',
|
||||
'face_detector_model': 'choose the model responsible for detecting the faces',
|
||||
'face_detector_size': 'specify the frame size provided to the face detector',
|
||||
'face_detector_margin': 'apply top, right, bottom and left margin to the frame',
|
||||
'face_detector_angles': 'specify the angles to rotate the frame before detecting faces',
|
||||
'face_detector_score': 'filter the detected faces based on the confidence score',
|
||||
'face_landmarker_model': 'choose the model responsible for detecting the face landmarks',
|
||||
'face_landmarker_score': 'filter the detected face landmarks based on the confidence score',
|
||||
'face_selector_mode': 'use reference based tracking or simple matching',
|
||||
'face_selector_order': 'specify the order of the detected faces',
|
||||
'face_selector_age_start': 'filter the detected faces based on the starting age',
|
||||
'face_selector_age_end': 'filter the detected faces based on the ending age',
|
||||
'face_selector_gender': 'filter the detected faces based on their gender',
|
||||
'face_selector_race': 'filter the detected faces based on their race',
|
||||
'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_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_parser_model': 'choose the model responsible for the region mask',
|
||||
'face_mask_types': 'mix and match different face mask types (choices: {choices})',
|
||||
'face_mask_areas': 'choose the items used for the area mask (choices: {choices})',
|
||||
'face_mask_regions': 'choose the items used for the region mask (choices: {choices})',
|
||||
'face_mask_blur': 'specify the degree of blur applied to the box mask',
|
||||
'face_mask_padding': 'apply top, right, bottom and left padding to the box mask',
|
||||
'voice_extractor_model': 'choose the model responsible for extracting the voices',
|
||||
'trim_frame_start': 'specify the starting frame of the target video',
|
||||
'trim_frame_end': 'specify the ending frame of the target video',
|
||||
'temp_frame_format': 'specify the temporary resources 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_scale': 'specify the image scale based on the target image',
|
||||
'output_audio_encoder': 'specify the encoder used for the audio',
|
||||
'output_audio_quality': 'specify the audio quality which translates to the audio compression',
|
||||
'output_audio_volume': 'specify the audio volume based on the target video',
|
||||
'output_audio_fps': 'specify the fps used when converting audio to video frames',
|
||||
'output_video_encoder': 'specify the encoder used for the video',
|
||||
'output_video_preset': 'balance fast video processing and video file size',
|
||||
'output_video_quality': 'specify the video quality which translates to the video compression',
|
||||
'output_video_scale': 'specify the video scale based on the target video',
|
||||
'output_video_fps': 'specify the video fps based on the target video',
|
||||
'processors': 'load a single or multiple processors (choices: {choices}, ...)',
|
||||
'background-remover-model': 'choose the model responsible for removing the background',
|
||||
'background-remover-color': 'apply red, green blue and alpha values of the background',
|
||||
'download_providers': 'download using different providers (choices: {choices}, ...)',
|
||||
'download_scope': 'specify the download scope',
|
||||
'benchmark_mode': 'choose the benchmark mode',
|
||||
'benchmark_resolutions': 'choose the resolutions for the benchmarks (choices: {choices}, ...)',
|
||||
'benchmark_cycle_count': 'specify the amount of cycles per benchmark',
|
||||
'api_host': 'specify the API host',
|
||||
'api_port': 'specify the API port',
|
||||
'api_security_strategy': 'specify the API security strategy used for sanitizing uploaded assets',
|
||||
'execution_device_ids': 'specify the devices used for processing',
|
||||
'execution_providers': 'inference using different providers (choices: {choices}, ...)',
|
||||
'execution_thread_count': 'specify the amount of parallel threads while processing',
|
||||
'video_memory_strategy': 'balance fast processing and low VRAM usage',
|
||||
'log_level': 'adjust the message severity displayed in the terminal',
|
||||
'halt_on_error': 'halt the program once an error occurred',
|
||||
'run': 'run the program',
|
||||
'batch_run': 'run the program in batch mode',
|
||||
'force_download': 'force automate downloads and exit',
|
||||
'benchmark': 'benchmark the program',
|
||||
'api': 'start the API server',
|
||||
'job_id': 'specify the job id',
|
||||
'job_status': 'specify the job status',
|
||||
'step_index': 'specify the step index',
|
||||
'job_list': 'list jobs by status',
|
||||
'job_create': 'create a drafted job',
|
||||
'job_submit': 'submit a drafted job to become a queued job',
|
||||
'job_submit_all': 'submit all drafted jobs to become a queued jobs',
|
||||
'job_delete': 'delete a drafted, queued, failed or completed job',
|
||||
'job_delete_all': 'delete all drafted, queued, failed and completed jobs',
|
||||
'job_add_step': 'add a step to a drafted job',
|
||||
'job_remix_step': 'remix a previous step from a drafted job',
|
||||
'job_insert_step': 'insert a step to a drafted job',
|
||||
'job_remove_step': 'remove a step from a drafted job',
|
||||
'job_run': 'run a queued job',
|
||||
'job_run_all': 'run all queued jobs',
|
||||
'job_retry': 'retry a failed job',
|
||||
'job_retry_all': 'retry all failed jobs'
|
||||
},
|
||||
'about':
|
||||
{
|
||||
'fund': 'fund ai workstation',
|
||||
'subscribe': 'become a member',
|
||||
'join': 'join our community'
|
||||
},
|
||||
'uis':
|
||||
{
|
||||
'apply_button': 'APPLY',
|
||||
'benchmark_mode_dropdown': 'BENCHMARK MODE',
|
||||
'benchmark_cycle_count_slider': 'BENCHMARK CYCLE COUNT',
|
||||
'benchmark_resolutions_checkbox_group': 'BENCHMARK RESOLUTIONS',
|
||||
'clear_button': 'CLEAR',
|
||||
'common_options_checkbox_group': 'OPTIONS',
|
||||
'download_providers_checkbox_group': 'DOWNLOAD PROVIDERS',
|
||||
'execution_providers_checkbox_group': 'EXECUTION PROVIDERS',
|
||||
'execution_thread_count_slider': 'EXECUTION THREAD COUNT',
|
||||
'face_detector_angles_checkbox_group': 'FACE DETECTOR ANGLES',
|
||||
'face_detector_model_dropdown': 'FACE DETECTOR MODEL',
|
||||
'face_detector_margin_slider': 'FACE DETECTOR MARGIN',
|
||||
'face_detector_score_slider': 'FACE DETECTOR SCORE',
|
||||
'face_detector_size_dropdown': 'FACE DETECTOR SIZE',
|
||||
'face_landmarker_model_dropdown': 'FACE LANDMARKER MODEL',
|
||||
'face_landmarker_score_slider': 'FACE LANDMARKER SCORE',
|
||||
'face_mask_blur_slider': 'FACE MASK BLUR',
|
||||
'face_mask_padding_bottom_slider': 'FACE MASK PADDING BOTTOM',
|
||||
'face_mask_padding_left_slider': 'FACE MASK PADDING LEFT',
|
||||
'face_mask_padding_right_slider': 'FACE MASK PADDING RIGHT',
|
||||
'face_mask_padding_top_slider': 'FACE MASK PADDING TOP',
|
||||
'face_mask_areas_checkbox_group': 'FACE MASK AREAS',
|
||||
'face_mask_regions_checkbox_group': 'FACE MASK REGIONS',
|
||||
'face_mask_types_checkbox_group': 'FACE MASK TYPES',
|
||||
'face_selector_age_range_slider': 'FACE SELECTOR AGE',
|
||||
'face_selector_gender_dropdown': 'FACE SELECTOR GENDER',
|
||||
'face_selector_mode_dropdown': 'FACE SELECTOR MODE',
|
||||
'face_selector_order_dropdown': 'FACE SELECTOR ORDER',
|
||||
'face_selector_race_dropdown': 'FACE SELECTOR RACE',
|
||||
'face_tracker_score_slider': 'FACE TRACKER SCORE',
|
||||
'face_occluder_model_dropdown': 'FACE OCCLUDER MODEL',
|
||||
'face_parser_model_dropdown': 'FACE PARSER MODEL',
|
||||
'voice_extractor_model_dropdown': 'VOICE EXTRACTOR MODEL',
|
||||
'job_list_status_checkbox_group': 'JOB STATUS',
|
||||
'job_manager_job_action_dropdown': 'JOB_ACTION',
|
||||
'job_manager_job_id_dropdown': 'JOB ID',
|
||||
'job_manager_step_index_dropdown': 'STEP INDEX',
|
||||
'job_runner_job_action_dropdown': 'JOB ACTION',
|
||||
'job_runner_job_id_dropdown': 'JOB ID',
|
||||
'log_level_dropdown': 'LOG LEVEL',
|
||||
'output_audio_encoder_dropdown': 'OUTPUT AUDIO ENCODER',
|
||||
'output_audio_quality_slider': 'OUTPUT AUDIO QUALITY',
|
||||
'output_audio_volume_slider': 'OUTPUT AUDIO VOLUME',
|
||||
'output_image_or_video': 'OUTPUT',
|
||||
'output_image_quality_slider': 'OUTPUT IMAGE QUALITY',
|
||||
'output_image_scale_slider': 'OUTPUT IMAGE SCALE',
|
||||
'output_path_textbox': 'OUTPUT PATH',
|
||||
'output_video_encoder_dropdown': 'OUTPUT VIDEO ENCODER',
|
||||
'output_video_fps_slider': 'OUTPUT VIDEO FPS',
|
||||
'output_video_preset_dropdown': 'OUTPUT VIDEO PRESET',
|
||||
'output_video_quality_slider': 'OUTPUT VIDEO QUALITY',
|
||||
'output_video_scale_slider': 'OUTPUT VIDEO SCALE',
|
||||
'preview_frame_slider': 'PREVIEW FRAME',
|
||||
'preview_image': 'PREVIEW',
|
||||
'preview_mode_dropdown': 'PREVIEW MODE',
|
||||
'preview_resolution_dropdown': 'PREVIEW RESOLUTION',
|
||||
'processors_checkbox_group': 'PROCESSORS',
|
||||
'reference_face_distance_slider': 'REFERENCE FACE DISTANCE',
|
||||
'reference_face_gallery': 'REFERENCE FACE',
|
||||
'refresh_button': 'REFRESH',
|
||||
'source_file': 'SOURCE',
|
||||
'start_button': 'START',
|
||||
'stop_button': 'STOP',
|
||||
'target_file': 'TARGET',
|
||||
'temp_frame_format_dropdown': 'TEMP FRAME FORMAT',
|
||||
'terminal_textbox': 'TERMINAL',
|
||||
'trim_frame_slider': 'TRIM FRAME',
|
||||
'video_memory_strategy_dropdown': 'VIDEO MEMORY STRATEGY',
|
||||
'webcam_fps_slider': 'WEBCAM FPS',
|
||||
'webcam_image': 'WEBCAM',
|
||||
'webcam_device_id_dropdown': 'WEBCAM DEVICE ID',
|
||||
'webcam_mode_radio': 'WEBCAM MODE',
|
||||
'webcam_resolution_dropdown': 'WEBCAM RESOLUTION'
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
from typing import Optional, Tuple
|
||||
|
||||
|
||||
def restrict_trim_frame(frame_total : int, trim_frame_start : Optional[int], trim_frame_end : Optional[int]) -> Tuple[int, int]:
|
||||
if isinstance(trim_frame_start, int):
|
||||
trim_frame_start = max(0, min(trim_frame_start, frame_total))
|
||||
if isinstance(trim_frame_end, int):
|
||||
trim_frame_end = max(0, min(trim_frame_end, frame_total))
|
||||
|
||||
if isinstance(trim_frame_start, int) and isinstance(trim_frame_end, int):
|
||||
return trim_frame_start, trim_frame_end
|
||||
if isinstance(trim_frame_start, int):
|
||||
return trim_frame_start, frame_total
|
||||
if isinstance(trim_frame_end, int):
|
||||
return 0, trim_frame_end
|
||||
|
||||
return 0, frame_total
|
||||
@@ -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
|
||||
@@ -4,7 +4,7 @@ METADATA =\
|
||||
{
|
||||
'name': 'FaceFusion',
|
||||
'description': 'Industry leading face manipulation platform',
|
||||
'version': '3.3.1',
|
||||
'version': 'v4',
|
||||
'license': 'OpenRAIL-AS',
|
||||
'author': 'Henry Ruhs',
|
||||
'url': 'https://facefusion.io'
|
||||
@@ -12,6 +12,4 @@ METADATA =\
|
||||
|
||||
|
||||
def get(key : str) -> Optional[str]:
|
||||
if key in METADATA:
|
||||
return METADATA.get(key)
|
||||
return None
|
||||
return METADATA.get(key)
|
||||
|
||||
@@ -5,7 +5,7 @@ import onnx
|
||||
from facefusion.types import ModelInitializer
|
||||
|
||||
|
||||
@lru_cache(maxsize = None)
|
||||
@lru_cache()
|
||||
def get_static_model_initializer(model_path : str) -> ModelInitializer:
|
||||
model = onnx.load(model_path)
|
||||
return onnx.numpy_helper.to_array(model.graph.initializer[-1])
|
||||
|
||||
+22
-10
@@ -1,17 +1,29 @@
|
||||
from typing import List, Optional
|
||||
|
||||
from facefusion.types import Fps, Padding
|
||||
from facefusion.types import Color, Fps, Padding
|
||||
|
||||
|
||||
def normalize_padding(padding : Optional[List[int]]) -> Optional[Padding]:
|
||||
if padding and len(padding) == 1:
|
||||
return tuple([ padding[0] ] * 4) #type:ignore[return-value]
|
||||
if padding and len(padding) == 2:
|
||||
return tuple([ padding[0], padding[1], padding[0], padding[1] ]) #type:ignore[return-value]
|
||||
if padding and len(padding) == 3:
|
||||
return tuple([ padding[0], padding[1], padding[2], padding[1] ]) #type:ignore[return-value]
|
||||
if padding and len(padding) == 4:
|
||||
return tuple(padding) #type:ignore[return-value]
|
||||
def normalize_color(channels : Optional[List[int]]) -> Optional[Color]:
|
||||
if channels and len(channels) == 1:
|
||||
return tuple([ channels[0], channels[0], channels[0], 255 ]) #type:ignore[return-value]
|
||||
if channels and len(channels) == 2:
|
||||
return tuple([ channels[0], channels[1], channels[0], 255 ]) #type:ignore[return-value]
|
||||
if channels and len(channels) == 3:
|
||||
return tuple([ channels[0], channels[1], channels[2], 255 ]) #type:ignore[return-value]
|
||||
if channels and len(channels) == 4:
|
||||
return tuple(channels) #type:ignore[return-value]
|
||||
return None
|
||||
|
||||
|
||||
def normalize_space(spaces : Optional[List[int]]) -> Optional[Padding]:
|
||||
if spaces and len(spaces) == 1:
|
||||
return tuple([spaces[0]] * 4) #type:ignore[return-value]
|
||||
if spaces and len(spaces) == 2:
|
||||
return tuple([ spaces[0], spaces[1], spaces[0], spaces[1] ]) #type:ignore[return-value]
|
||||
if spaces and len(spaces) == 3:
|
||||
return tuple([ spaces[0], spaces[1], spaces[2], spaces[1] ]) #type:ignore[return-value]
|
||||
if spaces and len(spaces) == 4:
|
||||
return tuple(spaces) #type:ignore[return-value]
|
||||
return None
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
from typing import Generator, List
|
||||
|
||||
from facefusion.types import ProcessState, QueuePayload
|
||||
from facefusion.types import ProcessState
|
||||
|
||||
PROCESS_STATE : ProcessState = 'pending'
|
||||
|
||||
@@ -45,9 +43,3 @@ def stop() -> None:
|
||||
|
||||
def end() -> None:
|
||||
set_process_state('pending')
|
||||
|
||||
|
||||
def manage(queue_payloads : List[QueuePayload]) -> Generator[QueuePayload, None, None]:
|
||||
for query_payload in queue_payloads:
|
||||
if is_processing():
|
||||
yield query_payload
|
||||
|
||||
@@ -1,15 +1,10 @@
|
||||
import importlib
|
||||
import os
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
from queue import Queue
|
||||
from types import ModuleType
|
||||
from typing import Any, List
|
||||
|
||||
from tqdm import tqdm
|
||||
|
||||
from facefusion import logger, state_manager, wording
|
||||
from facefusion import logger, translator
|
||||
from facefusion.exit_helper import hard_exit
|
||||
from facefusion.types import ProcessFrames, QueuePayload
|
||||
|
||||
|
||||
PROCESSORS_METHODS =\
|
||||
[
|
||||
@@ -17,29 +12,26 @@ PROCESSORS_METHODS =\
|
||||
'clear_inference_pool',
|
||||
'register_args',
|
||||
'apply_args',
|
||||
'get_common_modules',
|
||||
'pre_check',
|
||||
'pre_process',
|
||||
'post_process',
|
||||
'get_reference_frame',
|
||||
'process_frame',
|
||||
'process_frames',
|
||||
'process_image',
|
||||
'process_video'
|
||||
'process_frame'
|
||||
]
|
||||
|
||||
|
||||
def load_processor_module(processor : str) -> Any:
|
||||
try:
|
||||
processor_module = importlib.import_module('facefusion.processors.modules.' + processor)
|
||||
processor_module = importlib.import_module('facefusion.processors.modules.' + processor + '.core')
|
||||
for method_name in PROCESSORS_METHODS:
|
||||
if not hasattr(processor_module, method_name):
|
||||
raise NotImplementedError
|
||||
except ModuleNotFoundError as exception:
|
||||
logger.error(wording.get('processor_not_loaded').format(processor = processor), __name__)
|
||||
logger.error(translator.get('processor_not_loaded').format(processor = processor), __name__)
|
||||
logger.debug(exception.msg, __name__)
|
||||
hard_exit(1)
|
||||
except NotImplementedError:
|
||||
logger.error(wording.get('processor_not_implemented').format(processor = processor), __name__)
|
||||
logger.error(translator.get('processor_not_implemented').format(processor = processor), __name__)
|
||||
hard_exit(1)
|
||||
return processor_module
|
||||
|
||||
@@ -51,49 +43,3 @@ def get_processors_modules(processors : List[str]) -> List[ModuleType]:
|
||||
processor_module = load_processor_module(processor)
|
||||
processor_modules.append(processor_module)
|
||||
return processor_modules
|
||||
|
||||
|
||||
def multi_process_frames(source_paths : List[str], temp_frame_paths : List[str], process_frames : ProcessFrames) -> None:
|
||||
queue_payloads = create_queue_payloads(temp_frame_paths)
|
||||
with tqdm(total = len(queue_payloads), desc = wording.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 = []
|
||||
queue : Queue[QueuePayload] = create_queue(queue_payloads)
|
||||
queue_per_future = max(len(queue_payloads) // state_manager.get_item('execution_thread_count') * state_manager.get_item('execution_queue_count'), 1)
|
||||
|
||||
while not queue.empty():
|
||||
future = executor.submit(process_frames, source_paths, pick_queue(queue, queue_per_future), progress.update)
|
||||
futures.append(future)
|
||||
|
||||
for future_done in as_completed(futures):
|
||||
future_done.result()
|
||||
|
||||
|
||||
def create_queue(queue_payloads : List[QueuePayload]) -> Queue[QueuePayload]:
|
||||
queue : Queue[QueuePayload] = Queue()
|
||||
for queue_payload in queue_payloads:
|
||||
queue.put(queue_payload)
|
||||
return queue
|
||||
|
||||
|
||||
def pick_queue(queue : Queue[QueuePayload], queue_per_future : int) -> List[QueuePayload]:
|
||||
queues = []
|
||||
for _ in range(queue_per_future):
|
||||
if not queue.empty():
|
||||
queues.append(queue.get())
|
||||
return queues
|
||||
|
||||
|
||||
def create_queue_payloads(temp_frame_paths : List[str]) -> List[QueuePayload]:
|
||||
queue_payloads = []
|
||||
temp_frame_paths = sorted(temp_frame_paths, key = os.path.basename)
|
||||
|
||||
for frame_number, frame_path in enumerate(temp_frame_paths):
|
||||
frame_payload : QueuePayload =\
|
||||
{
|
||||
'frame_number': frame_number,
|
||||
'frame_path': frame_path
|
||||
}
|
||||
queue_payloads.append(frame_payload)
|
||||
return queue_payloads
|
||||
|
||||
@@ -63,15 +63,15 @@ def limit_expression(expression : LivePortraitExpression) -> LivePortraitExpress
|
||||
return numpy.clip(expression, EXPRESSION_MIN, EXPRESSION_MAX)
|
||||
|
||||
|
||||
def limit_euler_angles(target_pitch : LivePortraitPitch, target_yaw : LivePortraitYaw, target_roll : LivePortraitRoll, output_pitch : LivePortraitPitch, output_yaw : LivePortraitYaw, output_roll : LivePortraitRoll) -> Tuple[LivePortraitPitch, LivePortraitYaw, LivePortraitRoll]:
|
||||
pitch_min, pitch_max, yaw_min, yaw_max, roll_min, roll_max = calc_euler_limits(target_pitch, target_yaw, target_roll)
|
||||
def limit_angle(target_pitch : LivePortraitPitch, target_yaw : LivePortraitYaw, target_roll : LivePortraitRoll, output_pitch : LivePortraitPitch, output_yaw : LivePortraitYaw, output_roll : LivePortraitRoll) -> Tuple[LivePortraitPitch, LivePortraitYaw, LivePortraitRoll]:
|
||||
pitch_min, pitch_max, yaw_min, yaw_max, roll_min, roll_max = calculate_euler_limits(target_pitch, target_yaw, target_roll)
|
||||
output_pitch = numpy.clip(output_pitch, pitch_min, pitch_max)
|
||||
output_yaw = numpy.clip(output_yaw, yaw_min, yaw_max)
|
||||
output_roll = numpy.clip(output_roll, roll_min, roll_max)
|
||||
return output_pitch, output_yaw, output_roll
|
||||
|
||||
|
||||
def calc_euler_limits(pitch : LivePortraitPitch, yaw : LivePortraitYaw, roll : LivePortraitRoll) -> Tuple[float, float, float, float, float, float]:
|
||||
def calculate_euler_limits(pitch : LivePortraitPitch, yaw : LivePortraitYaw, roll : LivePortraitRoll) -> Tuple[float, float, float, float, float, float]:
|
||||
pitch_min = -30.0
|
||||
pitch_max = 30.0
|
||||
yaw_min = -60.0
|
||||
|
||||
@@ -1,254 +0,0 @@
|
||||
from argparse import ArgumentParser
|
||||
from functools import lru_cache
|
||||
from typing import List
|
||||
|
||||
import cv2
|
||||
import numpy
|
||||
|
||||
import facefusion.choices
|
||||
import facefusion.jobs.job_manager
|
||||
import facefusion.jobs.job_store
|
||||
import facefusion.processors.core as processors
|
||||
from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, inference_manager, logger, process_manager, state_manager, video_manager, wording
|
||||
from facefusion.common_helper import create_int_metavar
|
||||
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
|
||||
from facefusion.execution import has_execution_provider
|
||||
from facefusion.face_analyser import get_many_faces, get_one_face
|
||||
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_selector import find_similar_faces, sort_and_filter_faces
|
||||
from facefusion.face_store import get_reference_faces
|
||||
from facefusion.filesystem import in_directory, is_image, is_video, resolve_relative_path, same_file_extension
|
||||
from facefusion.processors import choices as processors_choices
|
||||
from facefusion.processors.types import AgeModifierDirection, AgeModifierInputs
|
||||
from facefusion.program_helper import find_argument_group
|
||||
from facefusion.thread_helper import thread_semaphore
|
||||
from facefusion.types import ApplyStateItem, Args, DownloadScope, Face, InferencePool, ModelOptions, ModelSet, ProcessMode, QueuePayload, UpdateProgress, VisionFrame
|
||||
from facefusion.vision import match_frame_color, read_image, read_static_image, write_image
|
||||
|
||||
|
||||
@lru_cache(maxsize = None)
|
||||
def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
||||
return\
|
||||
{
|
||||
'styleganex_age':
|
||||
{
|
||||
'hashes':
|
||||
{
|
||||
'age_modifier':
|
||||
{
|
||||
'url': resolve_download_url('models-3.1.0', 'styleganex_age.hash'),
|
||||
'path': resolve_relative_path('../.assets/models/styleganex_age.hash')
|
||||
}
|
||||
},
|
||||
'sources':
|
||||
{
|
||||
'age_modifier':
|
||||
{
|
||||
'url': resolve_download_url('models-3.1.0', 'styleganex_age.onnx'),
|
||||
'path': resolve_relative_path('../.assets/models/styleganex_age.onnx')
|
||||
}
|
||||
},
|
||||
'templates':
|
||||
{
|
||||
'target': 'ffhq_512',
|
||||
'target_with_background': 'styleganex_384'
|
||||
},
|
||||
'sizes':
|
||||
{
|
||||
'target': (256, 256),
|
||||
'target_with_background': (384, 384)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
def get_inference_pool() -> InferencePool:
|
||||
model_names = [ state_manager.get_item('age_modifier_model') ]
|
||||
model_source_set = get_model_options().get('sources')
|
||||
|
||||
return inference_manager.get_inference_pool(__name__, model_names, model_source_set)
|
||||
|
||||
|
||||
def clear_inference_pool() -> None:
|
||||
model_names = [ state_manager.get_item('age_modifier_model') ]
|
||||
inference_manager.clear_inference_pool(__name__, model_names)
|
||||
|
||||
|
||||
def get_model_options() -> ModelOptions:
|
||||
model_name = state_manager.get_item('age_modifier_model')
|
||||
return create_static_model_set('full').get(model_name)
|
||||
|
||||
|
||||
def register_args(program : ArgumentParser) -> None:
|
||||
group_processors = find_argument_group(program, 'processors')
|
||||
if group_processors:
|
||||
group_processors.add_argument('--age-modifier-model', help = wording.get('help.age_modifier_model'), default = config.get_str_value('processors', 'age_modifier_model', 'styleganex_age'), choices = processors_choices.age_modifier_models)
|
||||
group_processors.add_argument('--age-modifier-direction', help = wording.get('help.age_modifier_direction'), type = int, default = config.get_int_value('processors', 'age_modifier_direction', '0'), choices = processors_choices.age_modifier_direction_range, metavar = create_int_metavar(processors_choices.age_modifier_direction_range))
|
||||
facefusion.jobs.job_store.register_step_keys([ 'age_modifier_model', 'age_modifier_direction' ])
|
||||
|
||||
|
||||
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
|
||||
apply_state_item('age_modifier_model', args.get('age_modifier_model'))
|
||||
apply_state_item('age_modifier_direction', args.get('age_modifier_direction'))
|
||||
|
||||
|
||||
def pre_check() -> bool:
|
||||
model_hash_set = get_model_options().get('hashes')
|
||||
model_source_set = get_model_options().get('sources')
|
||||
|
||||
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
|
||||
|
||||
|
||||
def pre_process(mode : ProcessMode) -> bool:
|
||||
if mode in [ 'output', 'preview' ] and not is_image(state_manager.get_item('target_path')) and not is_video(state_manager.get_item('target_path')):
|
||||
logger.error(wording.get('choose_image_or_video_target') + wording.get('exclamation_mark'), __name__)
|
||||
return False
|
||||
if mode == 'output' and not in_directory(state_manager.get_item('output_path')):
|
||||
logger.error(wording.get('specify_image_or_video_output') + wording.get('exclamation_mark'), __name__)
|
||||
return False
|
||||
if mode == 'output' and not same_file_extension(state_manager.get_item('target_path'), state_manager.get_item('output_path')):
|
||||
logger.error(wording.get('match_target_and_output_extension') + wording.get('exclamation_mark'), __name__)
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def post_process() -> None:
|
||||
read_static_image.cache_clear()
|
||||
video_manager.clear_video_pool()
|
||||
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
|
||||
clear_inference_pool()
|
||||
if state_manager.get_item('video_memory_strategy') == 'strict':
|
||||
content_analyser.clear_inference_pool()
|
||||
face_classifier.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:
|
||||
model_templates = get_model_options().get('templates')
|
||||
model_sizes = get_model_options().get('sizes')
|
||||
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'))
|
||||
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)
|
||||
combined_matrix = merge_matrix([ extend_affine_matrix, cv2.invertAffineTransform(affine_matrix) ])
|
||||
occlusion_mask = cv2.warpAffine(occlusion_mask, combined_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
|
||||
|
||||
|
||||
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_inputs = {}
|
||||
|
||||
if has_execution_provider('coreml'):
|
||||
age_modifier.set_providers([ facefusion.choices.execution_provider_set.get('cpu') ])
|
||||
|
||||
for age_modifier_input in age_modifier.get_inputs():
|
||||
if age_modifier_input.name == 'target':
|
||||
age_modifier_inputs[age_modifier_input.name] = crop_vision_frame
|
||||
if age_modifier_input.name == 'target_with_background':
|
||||
age_modifier_inputs[age_modifier_input.name] = extend_vision_frame
|
||||
if age_modifier_input.name == 'direction':
|
||||
age_modifier_inputs[age_modifier_input.name] = age_modifier_direction
|
||||
|
||||
with thread_semaphore():
|
||||
crop_vision_frame = age_modifier.run(None, age_modifier_inputs)[0][0]
|
||||
|
||||
return crop_vision_frame
|
||||
|
||||
|
||||
def prepare_vision_frame(vision_frame : VisionFrame) -> VisionFrame:
|
||||
vision_frame = vision_frame[:, :, ::-1] / 255.0
|
||||
vision_frame = (vision_frame - 0.5) / 0.5
|
||||
vision_frame = numpy.expand_dims(vision_frame.transpose(2, 0, 1), axis = 0).astype(numpy.float32)
|
||||
return vision_frame
|
||||
|
||||
|
||||
def normalize_extend_frame(extend_vision_frame : VisionFrame) -> VisionFrame:
|
||||
model_sizes = get_model_options().get('sizes')
|
||||
extend_vision_frame = numpy.clip(extend_vision_frame, -1, 1)
|
||||
extend_vision_frame = (extend_vision_frame + 1) / 2
|
||||
extend_vision_frame = extend_vision_frame.transpose(1, 2, 0).clip(0, 255)
|
||||
extend_vision_frame = (extend_vision_frame * 255.0)
|
||||
extend_vision_frame = extend_vision_frame.astype(numpy.uint8)[:, :, ::-1]
|
||||
extend_vision_frame = cv2.resize(extend_vision_frame, (model_sizes.get('target')[0] * 4, model_sizes.get('target')[1] * 4), interpolation = cv2.INTER_AREA)
|
||||
return extend_vision_frame
|
||||
|
||||
|
||||
def get_reference_frame(source_face : Face, target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
|
||||
return modify_age(target_face, temp_vision_frame)
|
||||
|
||||
|
||||
def process_frame(inputs : AgeModifierInputs) -> VisionFrame:
|
||||
reference_faces = inputs.get('reference_faces')
|
||||
target_vision_frame = inputs.get('target_vision_frame')
|
||||
many_faces = sort_and_filter_faces(get_many_faces([ target_vision_frame ]))
|
||||
|
||||
if state_manager.get_item('face_selector_mode') == 'many':
|
||||
if many_faces:
|
||||
for target_face in many_faces:
|
||||
target_vision_frame = modify_age(target_face, target_vision_frame)
|
||||
if state_manager.get_item('face_selector_mode') == 'one':
|
||||
target_face = get_one_face(many_faces)
|
||||
if target_face:
|
||||
target_vision_frame = modify_age(target_face, target_vision_frame)
|
||||
if state_manager.get_item('face_selector_mode') == 'reference':
|
||||
similar_faces = find_similar_faces(many_faces, reference_faces, state_manager.get_item('reference_face_distance'))
|
||||
if similar_faces:
|
||||
for similar_face in similar_faces:
|
||||
target_vision_frame = modify_age(similar_face, target_vision_frame)
|
||||
return target_vision_frame
|
||||
|
||||
|
||||
def process_frames(source_path : List[str], queue_payloads : List[QueuePayload], update_progress : UpdateProgress) -> None:
|
||||
reference_faces = get_reference_faces() if 'reference' in state_manager.get_item('face_selector_mode') else None
|
||||
|
||||
for queue_payload in process_manager.manage(queue_payloads):
|
||||
target_vision_path = queue_payload['frame_path']
|
||||
target_vision_frame = read_image(target_vision_path)
|
||||
output_vision_frame = process_frame(
|
||||
{
|
||||
'reference_faces': reference_faces,
|
||||
'target_vision_frame': target_vision_frame
|
||||
})
|
||||
write_image(target_vision_path, output_vision_frame)
|
||||
update_progress(1)
|
||||
|
||||
|
||||
def process_image(source_path : str, target_path : str, output_path : str) -> None:
|
||||
reference_faces = get_reference_faces() if 'reference' in state_manager.get_item('face_selector_mode') else None
|
||||
target_vision_frame = read_static_image(target_path)
|
||||
output_vision_frame = process_frame(
|
||||
{
|
||||
'reference_faces': reference_faces,
|
||||
'target_vision_frame': target_vision_frame
|
||||
})
|
||||
write_image(output_path, output_vision_frame)
|
||||
|
||||
|
||||
def process_video(source_paths : List[str], temp_frame_paths : List[str]) -> None:
|
||||
processors.multi_process_frames(None, temp_frame_paths, process_frames)
|
||||
@@ -0,0 +1,8 @@
|
||||
from typing import List, Sequence, get_args
|
||||
|
||||
from facefusion.common_helper import create_int_range
|
||||
from facefusion.processors.modules.age_modifier.types import AgeModifierModel
|
||||
|
||||
age_modifier_models : List[AgeModifierModel] = list(get_args(AgeModifierModel))
|
||||
|
||||
age_modifier_direction_range : Sequence[int] = create_int_range(-100, 100, 1)
|
||||
+315
@@ -0,0 +1,315 @@
|
||||
from argparse import ArgumentParser
|
||||
from functools import lru_cache
|
||||
from types import ModuleType
|
||||
from typing import List
|
||||
|
||||
import cv2
|
||||
import numpy
|
||||
|
||||
import facefusion.capability_store
|
||||
import facefusion.choices
|
||||
import facefusion.jobs.job_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, get_middle
|
||||
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
|
||||
from facefusion.face_creator 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_masker import create_box_mask, create_occlusion_mask
|
||||
from facefusion.face_selector import select_faces
|
||||
from facefusion.filesystem import in_directory, is_image, is_video, resolve_relative_path
|
||||
from facefusion.processors.modules.age_modifier import choices as age_modifier_choices
|
||||
from facefusion.processors.modules.age_modifier.types import AgeModifierDirection, AgeModifierInputs
|
||||
from facefusion.processors.types import ApplyStateItem, ProcessorOutputs
|
||||
from facefusion.program_helper import find_argument_group
|
||||
from facefusion.thread_helper import thread_semaphore
|
||||
from facefusion.types import Args, DownloadScope, Face, InferencePool, ModelOptions, ModelSet, ProcessMode, VisionFrame
|
||||
from facefusion.vision import match_frame_color, read_static_image, read_static_video_chunk, read_static_video_frame
|
||||
|
||||
|
||||
@lru_cache()
|
||||
def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
||||
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':
|
||||
{
|
||||
'__metadata__':
|
||||
{
|
||||
'vendor': 'williamyang1991',
|
||||
'license': 'S-Lab-1.0',
|
||||
'year': 2023
|
||||
},
|
||||
'hashes':
|
||||
{
|
||||
'age_modifier':
|
||||
{
|
||||
'url': resolve_download_url('models-3.1.0', 'styleganex_age.hash'),
|
||||
'path': resolve_relative_path('../.assets/models/styleganex_age.hash')
|
||||
}
|
||||
},
|
||||
'sources':
|
||||
{
|
||||
'age_modifier':
|
||||
{
|
||||
'url': resolve_download_url('models-3.1.0', 'styleganex_age.onnx'),
|
||||
'path': resolve_relative_path('../.assets/models/styleganex_age.onnx')
|
||||
}
|
||||
},
|
||||
'templates':
|
||||
{
|
||||
'target': 'ffhq_512',
|
||||
'target_with_background': 'styleganex_384'
|
||||
},
|
||||
'sizes':
|
||||
{
|
||||
'target': (256, 256),
|
||||
'target_with_background': (384, 384)
|
||||
},
|
||||
'mean': [ 0.5, 0.5, 0.5 ],
|
||||
'standard_deviation': [ 0.5, 0.5, 0.5 ]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
def get_inference_pool() -> InferencePool:
|
||||
model_names = [ state_manager.get_item('age_modifier_model') ]
|
||||
model_source_set = get_model_options().get('sources')
|
||||
|
||||
return inference_manager.get_inference_pool(__name__, model_names, model_source_set)
|
||||
|
||||
|
||||
def clear_inference_pool() -> None:
|
||||
model_names = [ state_manager.get_item('age_modifier_model') ]
|
||||
inference_manager.clear_inference_pool(__name__, model_names)
|
||||
|
||||
|
||||
def get_model_options() -> ModelOptions:
|
||||
model_name = state_manager.get_item('age_modifier_model')
|
||||
return create_static_model_set('full').get(model_name)
|
||||
|
||||
|
||||
def register_args(program : ArgumentParser) -> None:
|
||||
group_processors = find_argument_group(program, 'processors')
|
||||
if group_processors:
|
||||
facefusion.capability_store.register_capability_set(
|
||||
[
|
||||
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)
|
||||
)
|
||||
],
|
||||
scopes = [ 'api', 'cli' ]
|
||||
)
|
||||
|
||||
|
||||
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
|
||||
apply_state_item('age_modifier_model', args.get('age_modifier_model'))
|
||||
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:
|
||||
model_hash_set = get_model_options().get('hashes')
|
||||
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)
|
||||
|
||||
|
||||
def pre_process(mode : ProcessMode) -> bool:
|
||||
if mode in [ 'output', 'preview' ] and not is_image(state_manager.get_item('target_path')) and not is_video(state_manager.get_item('target_path')):
|
||||
logger.error(translator.get('choose_image_or_video_target') + translator.get('exclamation_mark'), __name__)
|
||||
return False
|
||||
if state_manager.get_item('workflow_mode') in [ 'audio-to-image:video', 'image-to-image', 'image-to-video' ]:
|
||||
if mode == 'output' and not in_directory(state_manager.get_item('output_path')):
|
||||
logger.error(translator.get('specify_image_or_video_output') + translator.get('exclamation_mark'), __name__)
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def post_process() -> None:
|
||||
read_static_image.cache_clear()
|
||||
read_static_video_frame.cache_clear()
|
||||
read_static_video_chunk.cache_clear()
|
||||
video_manager.clear_video_pool()
|
||||
|
||||
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
|
||||
clear_inference_pool()
|
||||
|
||||
if state_manager.get_item('video_memory_strategy') == 'strict':
|
||||
for common_module in get_common_modules():
|
||||
common_module.clear_inference_pool()
|
||||
|
||||
|
||||
def modify_age(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
|
||||
model_templates = get_model_options().get('templates')
|
||||
model_sizes = get_model_options().get('sizes')
|
||||
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'))
|
||||
|
||||
if state_manager.get_item('age_modifier_model') == 'fran':
|
||||
box_mask = create_box_mask(crop_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)
|
||||
crop_masks.append(occlusion_mask)
|
||||
|
||||
crop_vision_frame = prepare_vision_frame(crop_vision_frame)
|
||||
target_age = numpy.mean(target_face.age)
|
||||
age_modifier_direction = numpy.array([ target_age, target_age + state_manager.get_item('age_modifier_direction') ], dtype = numpy.float32) / 100
|
||||
age_modifier_direction = age_modifier_direction.clip(0, 1)
|
||||
crop_vision_frame = forward(crop_vision_frame, crop_vision_frame, age_modifier_direction)
|
||||
crop_vision_frame = normalize_vision_frame(crop_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:
|
||||
age_modifier = get_inference_pool().get('age_modifier')
|
||||
age_modifier_inputs = {}
|
||||
|
||||
for age_modifier_input in age_modifier.get_inputs():
|
||||
if age_modifier_input.name == 'target':
|
||||
age_modifier_inputs[age_modifier_input.name] = crop_vision_frame
|
||||
if age_modifier_input.name == 'target_with_background':
|
||||
age_modifier_inputs[age_modifier_input.name] = extend_vision_frame
|
||||
if age_modifier_input.name == 'direction':
|
||||
age_modifier_inputs[age_modifier_input.name] = age_modifier_direction
|
||||
|
||||
with thread_semaphore():
|
||||
crop_vision_frame = age_modifier.run(None, age_modifier_inputs)[0][0]
|
||||
|
||||
return crop_vision_frame
|
||||
|
||||
|
||||
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 - model_mean) / model_standard_deviation
|
||||
vision_frame = numpy.expand_dims(vision_frame.transpose(2, 0, 1), axis = 0).astype(numpy.float32)
|
||||
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:
|
||||
model_sizes = get_model_options().get('sizes')
|
||||
extend_vision_frame = numpy.clip(extend_vision_frame, -1, 1)
|
||||
extend_vision_frame = (extend_vision_frame + 1) / 2
|
||||
extend_vision_frame = extend_vision_frame.transpose(1, 2, 0).clip(0, 255)
|
||||
extend_vision_frame = (extend_vision_frame * 255.0)
|
||||
extend_vision_frame = extend_vision_frame.astype(numpy.uint8)[:, :, ::-1]
|
||||
extend_vision_frame = cv2.resize(extend_vision_frame, (model_sizes.get('target')[0] * 4, model_sizes.get('target')[1] * 4), interpolation = cv2.INTER_AREA)
|
||||
return extend_vision_frame
|
||||
|
||||
|
||||
def process_frame(inputs : AgeModifierInputs) -> ProcessorOutputs:
|
||||
reference_vision_frame = inputs.get('reference_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_mask = inputs.get('temp_vision_mask')
|
||||
|
||||
target_vision_frame = get_middle(target_vision_frames)
|
||||
target_faces = select_faces(reference_vision_frame, source_vision_frames, target_vision_frames)
|
||||
|
||||
if target_faces:
|
||||
for target_face in target_faces:
|
||||
target_face = scale_face(target_face, target_vision_frame, temp_vision_frame)
|
||||
temp_vision_frame = modify_age(target_face, temp_vision_frame)
|
||||
|
||||
return temp_vision_frame, temp_vision_mask
|
||||
@@ -0,0 +1,18 @@
|
||||
from facefusion.types import Locales
|
||||
|
||||
LOCALES : Locales =\
|
||||
{
|
||||
'en':
|
||||
{
|
||||
'help':
|
||||
{
|
||||
'model': 'choose the model responsible for aging the face',
|
||||
'direction': 'specify the direction in which the age should be modified'
|
||||
},
|
||||
'uis':
|
||||
{
|
||||
'direction_slider': 'AGE MODIFIER DIRECTION',
|
||||
'model_dropdown': 'AGE MODIFIER MODEL'
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
from typing import Any, List, Literal, TypeAlias, TypedDict
|
||||
|
||||
from numpy.typing import NDArray
|
||||
|
||||
from facefusion.types import Mask, VisionFrame
|
||||
|
||||
AgeModifierInputs = TypedDict('AgeModifierInputs',
|
||||
{
|
||||
'reference_vision_frame' : VisionFrame,
|
||||
'source_vision_frames' : List[VisionFrame],
|
||||
'target_vision_frames' : List[VisionFrame],
|
||||
'temp_vision_frame' : VisionFrame,
|
||||
'temp_vision_mask' : Mask
|
||||
})
|
||||
|
||||
AgeModifierModel = Literal['fran', 'styleganex_age']
|
||||
|
||||
AgeModifierDirection : TypeAlias = NDArray[Any]
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user