mirror of
https://github.com/facefusion/facefusion.git
synced 2026-07-29 05:18:57 +02:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2f388a4d1c | ||
|
|
e003785ecb | ||
|
|
6f70f51d5e | ||
|
|
b7b60c186f | ||
|
|
ed1c9b0b24 | ||
|
|
72bb4b8865 | ||
|
|
423fee9d7f | ||
|
|
bed330f701 | ||
|
|
3b93008906 | ||
|
|
f9f3a9e62b | ||
|
|
3479cd0af3 | ||
|
|
1e8e763516 | ||
|
|
b2a2f84c24 | ||
|
|
0773403d4b | ||
|
|
23f865640a | ||
|
|
396610e88c | ||
|
|
286d25e91d | ||
|
|
deb5909f91 | ||
|
|
17f24784ad | ||
|
|
e9d9dec598 | ||
|
|
12ab871289 | ||
|
|
44830ffd9d | ||
|
|
e13b81207c | ||
|
|
c7e61d1678 | ||
|
|
50473df4c0 | ||
|
|
2dbf4523fe | ||
|
|
1cfabf6639 | ||
|
|
03d1555a6f | ||
|
|
cba29311c1 | ||
|
|
35c2022700 | ||
|
|
549de72bef | ||
|
|
9e6c070629 | ||
|
|
d4a4cf71d4 | ||
|
|
6180ccf6e6 | ||
|
|
65a61d5001 | ||
|
|
7ccf4d5eda | ||
|
|
b035f1a6e5 | ||
|
|
588eb049b0 | ||
|
|
0a4e1c639d | ||
|
|
85b6d962f2 | ||
|
|
6f89e7db51 | ||
|
|
69ad7572c6 | ||
|
|
68a56588e3 | ||
|
|
4b0d6c3333 | ||
|
|
3daf049684 | ||
|
|
f745ddc831 | ||
|
|
8f27d5dd08 | ||
|
|
a6bd2cdcc7 | ||
|
|
3ef90673b5 | ||
|
|
29e3014945 | ||
|
|
7e2f793cf4 | ||
|
|
0d2a434e61 | ||
|
|
cfa2216d0c | ||
|
|
20293d1fa3 |
@@ -33,11 +33,9 @@ jobs:
|
|||||||
uses: actions/setup-python@v5
|
uses: actions/setup-python@v5
|
||||||
with:
|
with:
|
||||||
python-version: '3.12'
|
python-version: '3.12'
|
||||||
- run: python install.py default --skip-conda
|
- run: python install.py --onnxruntime default --skip-conda
|
||||||
- run: pip install pytest
|
- run: pip install pytest
|
||||||
- run: pip install pytest-mock
|
|
||||||
- run: pip install httpx
|
- run: pip install httpx
|
||||||
- run: pip install python-multipart
|
|
||||||
- run: pytest
|
- run: pytest
|
||||||
report:
|
report:
|
||||||
needs: test
|
needs: test
|
||||||
@@ -51,13 +49,11 @@ jobs:
|
|||||||
uses: actions/setup-python@v5
|
uses: actions/setup-python@v5
|
||||||
with:
|
with:
|
||||||
python-version: '3.12'
|
python-version: '3.12'
|
||||||
- run: python install.py default --skip-conda
|
- run: python install.py --onnxruntime default --skip-conda
|
||||||
- run: pip install coveralls
|
- run: pip install coveralls
|
||||||
- run: pip install pytest
|
- run: pip install pytest
|
||||||
- run: pip install pytest-cov
|
- run: pip install pytest-cov
|
||||||
- run: pip install pytest-mock
|
|
||||||
- run: pip install httpx
|
- run: pip install httpx
|
||||||
- run: pip install python-multipart
|
|
||||||
- run: pytest tests --cov facefusion
|
- run: pytest tests --cov facefusion
|
||||||
- run: coveralls --service github
|
- run: coveralls --service github
|
||||||
env:
|
env:
|
||||||
|
|||||||
@@ -4,5 +4,4 @@ __pycache__
|
|||||||
.caches
|
.caches
|
||||||
.idea
|
.idea
|
||||||
.jobs
|
.jobs
|
||||||
.libraries
|
|
||||||
.vscode
|
.vscode
|
||||||
|
|||||||
+1
-1
@@ -1,3 +1,3 @@
|
|||||||
OpenRAIL-AS license
|
OpenRAIL-AS license
|
||||||
|
|
||||||
Copyright (c) 2026 Henry Ruhs
|
Copyright (c) 2025 Henry Ruhs
|
||||||
|
|||||||
+2
-11
@@ -1,5 +1,5 @@
|
|||||||
[workflow]
|
[workflow]
|
||||||
workflow_mode =
|
workflow =
|
||||||
|
|
||||||
[paths]
|
[paths]
|
||||||
temp_path =
|
temp_path =
|
||||||
@@ -35,9 +35,6 @@ reference_face_position =
|
|||||||
reference_face_distance =
|
reference_face_distance =
|
||||||
reference_frame_number =
|
reference_frame_number =
|
||||||
|
|
||||||
[face_tracker]
|
|
||||||
face_tracker_score =
|
|
||||||
|
|
||||||
[face_masker]
|
[face_masker]
|
||||||
face_occluder_model =
|
face_occluder_model =
|
||||||
face_parser_model =
|
face_parser_model =
|
||||||
@@ -55,16 +52,12 @@ trim_frame_start =
|
|||||||
trim_frame_end =
|
trim_frame_end =
|
||||||
temp_frame_format =
|
temp_frame_format =
|
||||||
|
|
||||||
[frame_distribution]
|
|
||||||
target_frame_amount =
|
|
||||||
|
|
||||||
[output_creation]
|
[output_creation]
|
||||||
output_image_quality =
|
output_image_quality =
|
||||||
output_image_scale =
|
output_image_scale =
|
||||||
output_audio_encoder =
|
output_audio_encoder =
|
||||||
output_audio_quality =
|
output_audio_quality =
|
||||||
output_audio_volume =
|
output_audio_volume =
|
||||||
output_audio_fps =
|
|
||||||
output_video_encoder =
|
output_video_encoder =
|
||||||
output_video_preset =
|
output_video_preset =
|
||||||
output_video_quality =
|
output_video_quality =
|
||||||
@@ -76,8 +69,7 @@ processors =
|
|||||||
age_modifier_model =
|
age_modifier_model =
|
||||||
age_modifier_direction =
|
age_modifier_direction =
|
||||||
background_remover_model =
|
background_remover_model =
|
||||||
background_remover_fill_color =
|
background_remover_color =
|
||||||
background_remover_despill_color =
|
|
||||||
deep_swapper_model =
|
deep_swapper_model =
|
||||||
deep_swapper_morph =
|
deep_swapper_morph =
|
||||||
expression_restorer_model =
|
expression_restorer_model =
|
||||||
@@ -125,7 +117,6 @@ benchmark_cycle_count =
|
|||||||
[api]
|
[api]
|
||||||
api_host =
|
api_host =
|
||||||
api_port =
|
api_port =
|
||||||
api_security_strategy =
|
|
||||||
|
|
||||||
[execution]
|
[execution]
|
||||||
execution_device_ids =
|
execution_device_ids =
|
||||||
|
|||||||
+1
-2
@@ -4,8 +4,7 @@ import os
|
|||||||
|
|
||||||
os.environ['OMP_NUM_THREADS'] = '1'
|
os.environ['OMP_NUM_THREADS'] = '1'
|
||||||
|
|
||||||
from facefusion import conda, core
|
from facefusion import core
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
conda.setup()
|
|
||||||
core.cli()
|
core.cli()
|
||||||
|
|||||||
@@ -12,3 +12,4 @@ def get_sec_websocket_protocol(scope : Scope) -> Optional[str]:
|
|||||||
return protocol.strip()
|
return protocol.strip()
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|||||||
@@ -1,26 +1,46 @@
|
|||||||
import asyncio
|
from typing import Optional
|
||||||
import os
|
|
||||||
import uuid
|
|
||||||
from typing import List, Optional
|
|
||||||
|
|
||||||
from starlette.datastructures import UploadFile
|
from facefusion.audio import detect_audio_duration
|
||||||
|
from facefusion.filesystem import is_audio, is_image, is_video
|
||||||
import facefusion.choices
|
from facefusion.types import AudioMetadata, ImageMetadata, MediaType, VideoMetadata
|
||||||
from facefusion import ffmpeg, process_manager, state_manager
|
from facefusion.vision import count_video_frame_total, detect_image_resolution, detect_video_duration, detect_video_fps, detect_video_resolution
|
||||||
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:
|
def extract_audio_metadata(file_path : str) -> AudioMetadata:
|
||||||
metadata : ImageMetadata =\
|
metadata : AudioMetadata =\
|
||||||
{
|
{
|
||||||
'resolution': detect_image_resolution(file_path)
|
'duration': detect_audio_duration(file_path),
|
||||||
|
'sample_rate': 0,
|
||||||
|
'frame_total': 0,
|
||||||
|
'channels': 0,
|
||||||
|
'format': ''
|
||||||
}
|
}
|
||||||
return metadata
|
return metadata
|
||||||
|
|
||||||
|
|
||||||
def detect_media_type_by_path(file_path : str) -> Optional[MediaType]:
|
def extract_image_metadata(file_path : str) -> ImageMetadata:
|
||||||
|
resolution = detect_image_resolution(file_path)
|
||||||
|
metadata : ImageMetadata =\
|
||||||
|
{
|
||||||
|
'resolution': resolution if resolution else (0, 0)
|
||||||
|
}
|
||||||
|
return metadata
|
||||||
|
|
||||||
|
|
||||||
|
def extract_video_metadata(file_path : str) -> VideoMetadata:
|
||||||
|
resolution = detect_video_resolution(file_path)
|
||||||
|
fps = detect_video_fps(file_path)
|
||||||
|
metadata : VideoMetadata =\
|
||||||
|
{
|
||||||
|
'duration': detect_video_duration(file_path),
|
||||||
|
'frame_total': count_video_frame_total(file_path),
|
||||||
|
'fps': fps if fps else 0.0,
|
||||||
|
'resolution': resolution if resolution else (0, 0)
|
||||||
|
}
|
||||||
|
return metadata
|
||||||
|
|
||||||
|
|
||||||
|
def detect_media_type(file_path : str) -> Optional[MediaType]:
|
||||||
if is_audio(file_path):
|
if is_audio(file_path):
|
||||||
return 'audio'
|
return 'audio'
|
||||||
if is_image(file_path):
|
if is_image(file_path):
|
||||||
@@ -28,64 +48,3 @@ def detect_media_type_by_path(file_path : str) -> Optional[MediaType]:
|
|||||||
if is_video(file_path):
|
if is_video(file_path):
|
||||||
return 'video'
|
return 'video'
|
||||||
return None
|
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
|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
|
import os
|
||||||
import uuid
|
import uuid
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from typing import List, Optional, cast
|
from typing import List, Optional, cast
|
||||||
|
|
||||||
from facefusion.apis.asset_helper import detect_media_type_by_path, extract_image_metadata
|
from facefusion.apis.asset_helper import detect_media_type, extract_audio_metadata, extract_image_metadata, extract_video_metadata
|
||||||
from facefusion.ffprobe import extract_audio_metadata, extract_video_metadata
|
from facefusion.filesystem import get_file_format, get_file_name
|
||||||
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
|
from facefusion.types import AssetId, AssetSet, AssetStore, AssetType, AudioAsset, AudioFormat, ImageAsset, ImageFormat, SessionId, VideoAsset, VideoFormat
|
||||||
|
|
||||||
ASSET_STORE : AssetStore = {}
|
ASSET_STORE : AssetStore = {}
|
||||||
@@ -14,8 +14,8 @@ def create_asset(session_id : SessionId, asset_type : AssetType, asset_path : st
|
|||||||
asset_id = str(uuid.uuid4())
|
asset_id = str(uuid.uuid4())
|
||||||
asset_name = get_file_name(asset_path)
|
asset_name = get_file_name(asset_path)
|
||||||
asset_format = get_file_format(asset_path)
|
asset_format = get_file_format(asset_path)
|
||||||
asset_size = get_file_size(asset_path)
|
asset_size = os.path.getsize(asset_path)
|
||||||
media_type = detect_media_type_by_path(asset_path)
|
media_type = detect_media_type(asset_path)
|
||||||
created_at = datetime.now()
|
created_at = datetime.now()
|
||||||
expires_at = created_at + timedelta(hours = 2)
|
expires_at = created_at + timedelta(hours = 2)
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,162 @@
|
|||||||
|
from typing import Any, Dict
|
||||||
|
|
||||||
|
from starlette.requests import Request
|
||||||
|
from starlette.responses import JSONResponse
|
||||||
|
from starlette.status import HTTP_200_OK
|
||||||
|
|
||||||
|
import facefusion.choices
|
||||||
|
from facefusion.execution import get_available_execution_providers
|
||||||
|
from facefusion.ffmpeg import get_available_encoder_set
|
||||||
|
from facefusion.processors.modules.face_debugger import choices as face_debugger_choices
|
||||||
|
from facefusion.processors.modules.face_enhancer import choices as face_enhancer_choices
|
||||||
|
from facefusion.processors.modules.face_swapper import choices as face_swapper_choices
|
||||||
|
from facefusion.processors.modules.frame_enhancer import choices as frame_enhancer_choices
|
||||||
|
|
||||||
|
|
||||||
|
async def get_choices(request : Request) -> JSONResponse:
|
||||||
|
available_execution_providers = get_available_execution_providers()
|
||||||
|
available_encoder_set = get_available_encoder_set()
|
||||||
|
|
||||||
|
choices_data : Dict[str, Any] =\
|
||||||
|
{
|
||||||
|
'face_detector_models': facefusion.choices.face_detector_models,
|
||||||
|
'face_detector_set': facefusion.choices.face_detector_set,
|
||||||
|
'face_landmarker_models': facefusion.choices.face_landmarker_models,
|
||||||
|
'face_selector_modes': facefusion.choices.face_selector_modes,
|
||||||
|
'face_selector_orders': facefusion.choices.face_selector_orders,
|
||||||
|
'face_selector_genders': facefusion.choices.face_selector_genders,
|
||||||
|
'face_selector_races': facefusion.choices.face_selector_races,
|
||||||
|
'face_occluder_models': facefusion.choices.face_occluder_models,
|
||||||
|
'face_parser_models': facefusion.choices.face_parser_models,
|
||||||
|
'face_mask_types': facefusion.choices.face_mask_types,
|
||||||
|
'face_mask_areas': facefusion.choices.face_mask_areas,
|
||||||
|
'face_mask_regions': facefusion.choices.face_mask_regions,
|
||||||
|
'voice_extractor_models': facefusion.choices.voice_extractor_models,
|
||||||
|
'workflows': facefusion.choices.workflows,
|
||||||
|
'audio_formats': facefusion.choices.audio_formats,
|
||||||
|
'image_formats': facefusion.choices.image_formats,
|
||||||
|
'video_formats': facefusion.choices.video_formats,
|
||||||
|
'temp_frame_formats': facefusion.choices.temp_frame_formats,
|
||||||
|
'output_audio_encoders': available_encoder_set.get('audio'),
|
||||||
|
'output_video_encoders': available_encoder_set.get('video'),
|
||||||
|
'output_video_presets': facefusion.choices.output_video_presets,
|
||||||
|
'execution_providers': available_execution_providers,
|
||||||
|
'video_memory_strategies': facefusion.choices.video_memory_strategies,
|
||||||
|
'log_levels': facefusion.choices.log_levels,
|
||||||
|
'face_swapper_models': face_swapper_choices.face_swapper_models,
|
||||||
|
'face_swapper_set': face_swapper_choices.face_swapper_set,
|
||||||
|
'face_enhancer_models': face_enhancer_choices.face_enhancer_models,
|
||||||
|
'frame_enhancer_models': frame_enhancer_choices.frame_enhancer_models,
|
||||||
|
'face_debugger_items': face_debugger_choices.face_debugger_items,
|
||||||
|
'face_detector_angles': list(facefusion.choices.face_detector_angles),
|
||||||
|
'face_detector_score_range':
|
||||||
|
{
|
||||||
|
'min': min(facefusion.choices.face_detector_score_range),
|
||||||
|
'max': max(facefusion.choices.face_detector_score_range),
|
||||||
|
'step': facefusion.choices.face_detector_score_range[1] - facefusion.choices.face_detector_score_range[0]
|
||||||
|
},
|
||||||
|
'face_landmarker_score_range':
|
||||||
|
{
|
||||||
|
'min': min(facefusion.choices.face_landmarker_score_range),
|
||||||
|
'max': max(facefusion.choices.face_landmarker_score_range),
|
||||||
|
'step': facefusion.choices.face_landmarker_score_range[1] - facefusion.choices.face_landmarker_score_range[0]
|
||||||
|
},
|
||||||
|
'face_mask_blur_range':
|
||||||
|
{
|
||||||
|
'min': min(facefusion.choices.face_mask_blur_range),
|
||||||
|
'max': max(facefusion.choices.face_mask_blur_range),
|
||||||
|
'step': facefusion.choices.face_mask_blur_range[1] - facefusion.choices.face_mask_blur_range[0]
|
||||||
|
},
|
||||||
|
'face_mask_padding_range':
|
||||||
|
{
|
||||||
|
'min': min(facefusion.choices.face_mask_padding_range),
|
||||||
|
'max': max(facefusion.choices.face_mask_padding_range),
|
||||||
|
'step': 1
|
||||||
|
},
|
||||||
|
'face_selector_age_range':
|
||||||
|
{
|
||||||
|
'min': min(facefusion.choices.face_selector_age_range),
|
||||||
|
'max': max(facefusion.choices.face_selector_age_range),
|
||||||
|
'step': 1
|
||||||
|
},
|
||||||
|
'reference_face_distance_range':
|
||||||
|
{
|
||||||
|
'min': min(facefusion.choices.reference_face_distance_range),
|
||||||
|
'max': max(facefusion.choices.reference_face_distance_range),
|
||||||
|
'step': facefusion.choices.reference_face_distance_range[1] - facefusion.choices.reference_face_distance_range[0]
|
||||||
|
},
|
||||||
|
'output_image_quality_range':
|
||||||
|
{
|
||||||
|
'min': min(facefusion.choices.output_image_quality_range),
|
||||||
|
'max': max(facefusion.choices.output_image_quality_range),
|
||||||
|
'step': 1
|
||||||
|
},
|
||||||
|
'output_image_scale_range':
|
||||||
|
{
|
||||||
|
'min': min(facefusion.choices.output_image_scale_range),
|
||||||
|
'max': max(facefusion.choices.output_image_scale_range),
|
||||||
|
'step': facefusion.choices.output_image_scale_range[1] - facefusion.choices.output_image_scale_range[0]
|
||||||
|
},
|
||||||
|
'output_audio_quality_range':
|
||||||
|
{
|
||||||
|
'min': min(facefusion.choices.output_audio_quality_range),
|
||||||
|
'max': max(facefusion.choices.output_audio_quality_range),
|
||||||
|
'step': 1
|
||||||
|
},
|
||||||
|
'output_audio_volume_range':
|
||||||
|
{
|
||||||
|
'min': min(facefusion.choices.output_audio_volume_range),
|
||||||
|
'max': max(facefusion.choices.output_audio_volume_range),
|
||||||
|
'step': 1
|
||||||
|
},
|
||||||
|
'output_video_quality_range':
|
||||||
|
{
|
||||||
|
'min': min(facefusion.choices.output_video_quality_range),
|
||||||
|
'max': max(facefusion.choices.output_video_quality_range),
|
||||||
|
'step': 1
|
||||||
|
},
|
||||||
|
'output_video_scale_range':
|
||||||
|
{
|
||||||
|
'min': min(facefusion.choices.output_video_scale_range),
|
||||||
|
'max': max(facefusion.choices.output_video_scale_range),
|
||||||
|
'step': facefusion.choices.output_video_scale_range[1] - facefusion.choices.output_video_scale_range[0]
|
||||||
|
},
|
||||||
|
'execution_thread_count_range':
|
||||||
|
{
|
||||||
|
'min': min(facefusion.choices.execution_thread_count_range),
|
||||||
|
'max': max(facefusion.choices.execution_thread_count_range),
|
||||||
|
'step': 1
|
||||||
|
},
|
||||||
|
'face_detector_margin_range':
|
||||||
|
{
|
||||||
|
'min': min(facefusion.choices.face_detector_margin_range),
|
||||||
|
'max': max(facefusion.choices.face_detector_margin_range),
|
||||||
|
'step': 1
|
||||||
|
},
|
||||||
|
'face_swapper_weight_range':
|
||||||
|
{
|
||||||
|
'min': min(face_swapper_choices.face_swapper_weight_range),
|
||||||
|
'max': max(face_swapper_choices.face_swapper_weight_range),
|
||||||
|
'step': face_swapper_choices.face_swapper_weight_range[1] - face_swapper_choices.face_swapper_weight_range[0]
|
||||||
|
},
|
||||||
|
'face_enhancer_blend_range':
|
||||||
|
{
|
||||||
|
'min': min(face_enhancer_choices.face_enhancer_blend_range),
|
||||||
|
'max': max(face_enhancer_choices.face_enhancer_blend_range),
|
||||||
|
'step': 1
|
||||||
|
},
|
||||||
|
'face_enhancer_weight_range':
|
||||||
|
{
|
||||||
|
'min': min(face_enhancer_choices.face_enhancer_weight_range),
|
||||||
|
'max': max(face_enhancer_choices.face_enhancer_weight_range),
|
||||||
|
'step': face_enhancer_choices.face_enhancer_weight_range[1] - face_enhancer_choices.face_enhancer_weight_range[0]
|
||||||
|
},
|
||||||
|
'frame_enhancer_blend_range':
|
||||||
|
{
|
||||||
|
'min': min(frame_enhancer_choices.frame_enhancer_blend_range),
|
||||||
|
'max': max(frame_enhancer_choices.frame_enhancer_blend_range),
|
||||||
|
'step': 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return JSONResponse(choices_data, status_code = HTTP_200_OK)
|
||||||
+27
-38
@@ -1,55 +1,44 @@
|
|||||||
from types import ModuleType
|
|
||||||
from typing import List
|
|
||||||
|
|
||||||
from starlette.applications import Starlette
|
from starlette.applications import Starlette
|
||||||
from starlette.middleware import Middleware
|
from starlette.middleware import Middleware
|
||||||
from starlette.middleware.cors import CORSMiddleware
|
from starlette.middleware.cors import CORSMiddleware
|
||||||
from starlette.routing import Route, WebSocketRoute
|
from starlette.routing import Route, WebSocketRoute
|
||||||
|
|
||||||
from facefusion.apis.endpoints.assets import delete_assets, get_asset, get_assets, upload_asset
|
from facefusion.apis.choices import get_choices
|
||||||
from facefusion.apis.endpoints.capabilities import get_capabilities
|
from facefusion.apis.endpoints.assets import delete_asset, delete_assets, get_asset, get_assets, upload_asset
|
||||||
from facefusion.apis.endpoints.metrics import get_metrics, websocket_metrics
|
|
||||||
from facefusion.apis.endpoints.ping import websocket_ping
|
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.session import create_session, create_session_guard, destroy_session, get_session, refresh_session
|
||||||
from facefusion.apis.endpoints.state import get_state, set_state
|
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.endpoints.stream import delete_stream, post_stream, websocket_stream
|
||||||
from facefusion.apis.middlewares.session import create_session_guard
|
from facefusion.apis.metrics import websocket_metrics
|
||||||
from facefusion.libraries import aom as aom_module, datachannel as datachannel_module, opus as opus_module, vpx as vpx_module
|
from facefusion.apis.remote import remote
|
||||||
|
from facefusion.apis.timeline import get_timeline
|
||||||
|
from facefusion.apis.version import create_version_guard
|
||||||
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:
|
def create_api() -> Starlette:
|
||||||
|
version_guard = Middleware(create_version_guard)
|
||||||
session_guard = Middleware(create_session_guard)
|
session_guard = Middleware(create_session_guard)
|
||||||
routes =\
|
routes =\
|
||||||
[
|
[
|
||||||
Route('/session', create_session, methods = [ 'POST' ]),
|
Route('/session', create_session, methods = [ 'POST' ], middleware = [ version_guard ]),
|
||||||
Route('/session', get_session, methods = [ 'GET' ], middleware = [ session_guard ]),
|
Route('/session', get_session, methods = [ 'GET' ], middleware = [ version_guard, session_guard ]),
|
||||||
Route('/session', refresh_session, methods = [ 'PUT' ]),
|
Route('/session', refresh_session, methods = [ 'PUT' ], middleware = [ version_guard ]),
|
||||||
Route('/session', destroy_session, methods = [ 'DELETE' ], middleware = [ session_guard ]),
|
Route('/session', destroy_session, methods = [ 'DELETE' ], middleware = [ version_guard, session_guard ]),
|
||||||
Route('/state', get_state, methods = [ 'GET' ], middleware = [ session_guard ]),
|
Route('/state', get_state, methods = [ 'GET' ], middleware = [ version_guard, session_guard ]),
|
||||||
Route('/state', set_state, methods = [ 'PUT' ], middleware = [ session_guard ]),
|
Route('/state', set_state, methods = [ 'PUT' ], middleware = [ version_guard, session_guard ]),
|
||||||
Route('/assets', get_assets, methods = [ 'GET' ], middleware = [ session_guard ]),
|
Route('/assets', get_assets, methods = [ 'GET' ], middleware = [ version_guard, session_guard ]),
|
||||||
Route('/assets', upload_asset, methods = [ 'POST' ], middleware = [ session_guard ]),
|
Route('/assets', upload_asset, methods = [ 'POST' ], middleware = [ version_guard, session_guard ]),
|
||||||
Route('/assets/{asset_id}', get_asset, methods = [ 'GET' ], middleware = [ session_guard ]),
|
Route('/assets/{asset_id}', get_asset, methods = [ 'GET' ], middleware = [ version_guard, session_guard ]),
|
||||||
Route('/assets', delete_assets, methods = [ 'DELETE' ], middleware = [ session_guard ]),
|
Route('/assets/{asset_id}', delete_asset, methods = [ 'DELETE' ], middleware = [ version_guard, session_guard ]),
|
||||||
Route('/capabilities', get_capabilities, methods = [ 'GET' ]),
|
Route('/assets', delete_assets, methods = [ 'DELETE' ], middleware = [ version_guard, session_guard ]),
|
||||||
Route('/metrics', get_metrics, methods = [ 'GET' ], middleware = [ session_guard ]),
|
Route('/choices', get_choices, methods = [ 'GET' ], middleware = [ version_guard, session_guard ]),
|
||||||
Route('/stream', post_stream, methods = [ 'POST' ], middleware = [ session_guard ]),
|
Route('/remote', remote, methods = [ 'POST' ], middleware = [ version_guard, session_guard ]),
|
||||||
Route('/stream', delete_stream, methods = [ 'DELETE' ], name = 'delete_stream', middleware = [ session_guard ]),
|
Route('/timeline/{count:int}', get_timeline, methods = [ 'GET' ], middleware = [ version_guard, session_guard ]),
|
||||||
WebSocketRoute('/metrics', websocket_metrics, middleware = [ session_guard ]),
|
Route('/stream', post_stream, methods = [ 'POST' ], middleware = [ version_guard, session_guard ]),
|
||||||
WebSocketRoute('/ping', websocket_ping, middleware = [ session_guard ]),
|
Route('/stream', delete_stream, methods = [ 'DELETE' ], middleware = [ version_guard, session_guard ]),
|
||||||
WebSocketRoute('/stream', websocket_stream, middleware = [ session_guard ])
|
WebSocketRoute('/stream', websocket_stream, middleware = [ version_guard, session_guard ]),
|
||||||
|
WebSocketRoute('/metrics', websocket_metrics, middleware = [ version_guard, session_guard ]),
|
||||||
|
WebSocketRoute('/ping', websocket_ping, middleware = [ version_guard, session_guard ])
|
||||||
]
|
]
|
||||||
|
|
||||||
api = Starlette(routes = routes)
|
api = Starlette(routes = routes)
|
||||||
|
|||||||
@@ -1,15 +1,32 @@
|
|||||||
import os
|
import tempfile
|
||||||
from typing import List
|
from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
|
from starlette.datastructures import UploadFile
|
||||||
from starlette.requests import Request
|
from starlette.requests import Request
|
||||||
from starlette.responses import FileResponse, JSONResponse, Response
|
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 starlette.status import HTTP_200_OK, HTTP_201_CREATED, HTTP_400_BAD_REQUEST, HTTP_404_NOT_FOUND
|
||||||
|
|
||||||
from facefusion import session_context, session_manager
|
from facefusion import session_manager
|
||||||
from facefusion.apis import asset_store
|
from facefusion.apis import asset_store
|
||||||
from facefusion.apis.asset_helper import save_asset_files, validate_asset_files
|
from facefusion.apis.asset_helper import detect_media_type
|
||||||
from facefusion.apis.endpoints.session import extract_access_token
|
from facefusion.apis.endpoints.session import extract_access_token
|
||||||
from facefusion.filesystem import remove_file
|
from facefusion.filesystem import get_file_extension, remove_file
|
||||||
|
from facefusion.types import AudioAsset, ImageAsset, VideoAsset
|
||||||
|
|
||||||
|
|
||||||
|
def translate_asset(asset : AudioAsset | ImageAsset | VideoAsset) -> Optional[Dict[str, Any]]:
|
||||||
|
return\
|
||||||
|
{
|
||||||
|
'id': asset.get('id'),
|
||||||
|
'created_at': asset.get('created_at').isoformat(),
|
||||||
|
'expires_at': asset.get('expires_at').isoformat(),
|
||||||
|
'type': asset.get('type'),
|
||||||
|
'media_type': asset.get('media'),
|
||||||
|
'filename': asset.get('name'),
|
||||||
|
'format': asset.get('format'),
|
||||||
|
'size': asset.get('size'),
|
||||||
|
'metadata': asset.get('metadata')
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
async def upload_asset(request : Request) -> Response:
|
async def upload_asset(request : Request) -> Response:
|
||||||
@@ -18,40 +35,62 @@ async def upload_asset(request : Request) -> Response:
|
|||||||
asset_type = request.query_params.get('type')
|
asset_type = request.query_params.get('type')
|
||||||
|
|
||||||
if session_id and asset_type in [ 'source', 'target' ]:
|
if session_id and asset_type in [ 'source', 'target' ]:
|
||||||
session_context.set_session_id(session_id)
|
|
||||||
|
|
||||||
form = await request.form()
|
form = await request.form()
|
||||||
upload_files = form.getlist('file')
|
upload_files = form.getlist('file')
|
||||||
|
asset_paths = await save_asset_files(upload_files) # type: ignore[arg-type]
|
||||||
if upload_files and validate_asset_files(upload_files):
|
|
||||||
asset_paths = await save_asset_files(upload_files)
|
|
||||||
|
|
||||||
if asset_paths:
|
if asset_paths:
|
||||||
asset_ids : List[str] = []
|
asset_ids : List[str] = []
|
||||||
|
|
||||||
for asset_path in asset_paths:
|
for asset_path in asset_paths:
|
||||||
asset = asset_store.create_asset(session_id, asset_type, asset_path)
|
asset = asset_store.create_asset(session_id, asset_type, asset_path) # type: ignore[arg-type]
|
||||||
|
|
||||||
if asset:
|
|
||||||
asset_id = asset.get('id')
|
asset_id = asset.get('id')
|
||||||
|
|
||||||
if asset_id:
|
if asset_id:
|
||||||
asset_ids.append(asset_id)
|
asset_ids.append(asset_id)
|
||||||
|
|
||||||
if asset_ids:
|
if asset_ids:
|
||||||
|
if asset_type == 'target':
|
||||||
|
return JSONResponse(
|
||||||
|
{
|
||||||
|
'asset_id': asset_ids[0]
|
||||||
|
}, status_code = HTTP_201_CREATED)
|
||||||
|
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
{
|
{
|
||||||
'asset_ids': asset_ids
|
'asset_ids': asset_ids
|
||||||
}, status_code = HTTP_201_CREATED)
|
}, status_code = HTTP_201_CREATED)
|
||||||
|
|
||||||
return Response(status_code = HTTP_415_UNSUPPORTED_MEDIA_TYPE)
|
|
||||||
|
|
||||||
return Response(status_code = HTTP_400_BAD_REQUEST)
|
return Response(status_code = HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
|
||||||
|
async def save_asset_files(upload_files : List[UploadFile]) -> List[str]:
|
||||||
|
asset_paths : List[str] = []
|
||||||
|
|
||||||
|
for upload_file in upload_files:
|
||||||
|
upload_file_extension = get_file_extension(upload_file.filename)
|
||||||
|
|
||||||
|
with tempfile.NamedTemporaryFile(suffix = upload_file_extension, delete = False) as temp_file:
|
||||||
|
|
||||||
|
while upload_chunk := await upload_file.read(1024):
|
||||||
|
temp_file.write(upload_chunk)
|
||||||
|
|
||||||
|
temp_file.flush()
|
||||||
|
|
||||||
|
media_type = detect_media_type(temp_file.name)
|
||||||
|
|
||||||
|
if media_type:
|
||||||
|
asset_paths.append(temp_file.name)
|
||||||
|
else:
|
||||||
|
remove_file(temp_file.name)
|
||||||
|
|
||||||
|
return asset_paths
|
||||||
|
|
||||||
|
|
||||||
async def get_assets(request : Request) -> Response:
|
async def get_assets(request : Request) -> Response:
|
||||||
access_token = extract_access_token(request.scope)
|
access_token = extract_access_token(request.scope)
|
||||||
session_id = session_manager.find_session_id(access_token)
|
session_id = session_manager.find_session_id(access_token)
|
||||||
|
asset_type = request.query_params.get('type')
|
||||||
|
|
||||||
if session_id:
|
if session_id:
|
||||||
asset_set = asset_store.get_assets(session_id)
|
asset_set = asset_store.get_assets(session_id)
|
||||||
@@ -59,22 +98,13 @@ async def get_assets(request : Request) -> Response:
|
|||||||
|
|
||||||
if asset_set:
|
if asset_set:
|
||||||
for asset in asset_set.values():
|
for asset in asset_set.values():
|
||||||
assets.append(
|
if not asset_type or asset.get('type') == asset_type:
|
||||||
{
|
assets.append(translate_asset(asset))
|
||||||
'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(
|
return JSONResponse(
|
||||||
{
|
{
|
||||||
'assets': assets
|
'assets': assets,
|
||||||
|
'count': len(assets)
|
||||||
}, status_code = HTTP_200_OK)
|
}, status_code = HTTP_200_OK)
|
||||||
|
|
||||||
return Response(status_code = HTTP_400_BAD_REQUEST)
|
return Response(status_code = HTTP_400_BAD_REQUEST)
|
||||||
@@ -84,29 +114,32 @@ async def get_asset(request : Request) -> Response:
|
|||||||
access_token = extract_access_token(request.scope)
|
access_token = extract_access_token(request.scope)
|
||||||
session_id = session_manager.find_session_id(access_token)
|
session_id = session_manager.find_session_id(access_token)
|
||||||
asset_id = request.path_params.get('asset_id')
|
asset_id = request.path_params.get('asset_id')
|
||||||
|
action = request.query_params.get('action')
|
||||||
|
|
||||||
if session_id and asset_id:
|
if session_id and asset_id:
|
||||||
asset = asset_store.get_asset(session_id, asset_id)
|
asset = asset_store.get_asset(session_id, asset_id)
|
||||||
|
|
||||||
if asset:
|
if asset:
|
||||||
if request.query_params.get('action') == 'download':
|
if action == 'download':
|
||||||
asset_path = asset.get('path')
|
return FileResponse(asset.get('path'), filename = asset.get('name'))
|
||||||
|
|
||||||
if os.path.exists(asset_path):
|
return JSONResponse(translate_asset(asset), status_code = HTTP_200_OK)
|
||||||
return FileResponse(asset_path, filename = asset.get('name'))
|
|
||||||
|
|
||||||
return JSONResponse(
|
return Response(status_code = HTTP_404_NOT_FOUND)
|
||||||
{
|
|
||||||
'id': asset.get('id'),
|
|
||||||
'created_at': asset.get('created_at').isoformat(),
|
async def delete_asset(request : Request) -> Response:
|
||||||
'expires_at': asset.get('expires_at').isoformat(),
|
access_token = extract_access_token(request.scope)
|
||||||
'type': asset.get('type'),
|
session_id = session_manager.find_session_id(access_token)
|
||||||
'media': asset.get('media'),
|
asset_id = request.path_params.get('asset_id')
|
||||||
'name': asset.get('name'),
|
|
||||||
'format': asset.get('format'),
|
if session_id and asset_id:
|
||||||
'size': asset.get('size'),
|
asset_set = asset_store.get_assets(session_id)
|
||||||
'metadata': asset.get('metadata')
|
|
||||||
}, status_code = HTTP_200_OK)
|
if asset_set and asset_id in asset_set:
|
||||||
|
remove_file(asset_set.get(asset_id).get('path'))
|
||||||
|
asset_store.delete_assets(session_id, [ asset_id ])
|
||||||
|
return Response(status_code = HTTP_200_OK)
|
||||||
|
|
||||||
return Response(status_code = HTTP_404_NOT_FOUND)
|
return Response(status_code = HTTP_404_NOT_FOUND)
|
||||||
|
|
||||||
@@ -121,14 +154,9 @@ async def delete_assets(request : Request) -> Response:
|
|||||||
asset_set = asset_store.get_assets(session_id)
|
asset_set = asset_store.get_assets(session_id)
|
||||||
|
|
||||||
if asset_set:
|
if asset_set:
|
||||||
|
|
||||||
for asset_id in asset_ids:
|
for asset_id in asset_ids:
|
||||||
if asset_id in asset_set:
|
if asset_id in asset_set:
|
||||||
asset = asset_set.get(asset_id)
|
remove_file(asset_set.get(asset_id).get('path'))
|
||||||
|
|
||||||
if asset:
|
|
||||||
remove_file(asset.get('path'))
|
|
||||||
|
|
||||||
asset_store.delete_assets(session_id, asset_ids)
|
asset_store.delete_assets(session_id, asset_ids)
|
||||||
return Response(status_code = HTTP_200_OK)
|
return Response(status_code = HTTP_200_OK)
|
||||||
|
|
||||||
|
|||||||
@@ -1,20 +0,0 @@
|
|||||||
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)
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
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
|
|
||||||
@@ -1,12 +1,16 @@
|
|||||||
import os
|
import os
|
||||||
import secrets
|
import secrets
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from starlette.datastructures import Headers
|
||||||
from starlette.requests import Request
|
from starlette.requests import Request
|
||||||
from starlette.responses import JSONResponse
|
from starlette.responses import JSONResponse
|
||||||
from starlette.status import HTTP_200_OK, HTTP_201_CREATED, HTTP_401_UNAUTHORIZED
|
from starlette.status import HTTP_200_OK, HTTP_201_CREATED, HTTP_401_UNAUTHORIZED, HTTP_426_UPGRADE_REQUIRED
|
||||||
|
from starlette.types import ASGIApp, Receive, Scope, Send
|
||||||
|
|
||||||
from facefusion import session_context, session_manager, translator
|
from facefusion import session_context, session_manager, translator
|
||||||
from facefusion.apis.session_helper import extract_access_token
|
from facefusion.apis.api_helper import get_sec_websocket_protocol
|
||||||
|
from facefusion.types import Token
|
||||||
|
|
||||||
|
|
||||||
async def create_session(request : Request) -> JSONResponse:
|
async def create_session(request : Request) -> JSONResponse:
|
||||||
@@ -32,7 +36,11 @@ async def create_session(request : Request) -> JSONResponse:
|
|||||||
|
|
||||||
async def get_session(request : Request) -> JSONResponse:
|
async def get_session(request : Request) -> JSONResponse:
|
||||||
access_token = extract_access_token(request.scope)
|
access_token = extract_access_token(request.scope)
|
||||||
|
|
||||||
|
if access_token:
|
||||||
session_id = session_manager.find_session_id(access_token)
|
session_id = session_manager.find_session_id(access_token)
|
||||||
|
|
||||||
|
if session_id:
|
||||||
session = session_manager.get_session(session_id)
|
session = session_manager.get_session(session_id)
|
||||||
|
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
@@ -43,12 +51,17 @@ async def get_session(request : Request) -> JSONResponse:
|
|||||||
'expires_at': session.get('expires_at').isoformat()
|
'expires_at': session.get('expires_at').isoformat()
|
||||||
}, status_code = HTTP_200_OK)
|
}, status_code = HTTP_200_OK)
|
||||||
|
|
||||||
|
return JSONResponse(
|
||||||
|
{
|
||||||
|
'message': translator.get('something_went_wrong', 'facefusion.apis')
|
||||||
|
}, status_code = HTTP_401_UNAUTHORIZED)
|
||||||
|
|
||||||
|
|
||||||
async def refresh_session(request : Request) -> JSONResponse:
|
async def refresh_session(request : Request) -> JSONResponse:
|
||||||
body = await request.json()
|
body = await request.json()
|
||||||
|
|
||||||
for session_id, session in session_manager.SESSIONS.items():
|
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):
|
if session.get('refresh_token') == body.get('refresh_token'):
|
||||||
__session__ = session_manager.create_session()
|
__session__ = session_manager.create_session()
|
||||||
session_manager.set_session(session_id, __session__)
|
session_manager.set_session(session_id, __session__)
|
||||||
|
|
||||||
@@ -66,10 +79,70 @@ async def refresh_session(request : Request) -> JSONResponse:
|
|||||||
|
|
||||||
async def destroy_session(request : Request) -> JSONResponse:
|
async def destroy_session(request : Request) -> JSONResponse:
|
||||||
access_token = extract_access_token(request.scope)
|
access_token = extract_access_token(request.scope)
|
||||||
|
|
||||||
|
if access_token:
|
||||||
session_id = session_manager.find_session_id(access_token)
|
session_id = session_manager.find_session_id(access_token)
|
||||||
|
|
||||||
|
if session_id:
|
||||||
session_manager.clear_session(session_id)
|
session_manager.clear_session(session_id)
|
||||||
|
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
{
|
{
|
||||||
'message': translator.get('ok', 'facefusion.apis')
|
'message': translator.get('ok', 'facefusion.apis')
|
||||||
}, status_code = HTTP_200_OK)
|
}, status_code = HTTP_200_OK)
|
||||||
|
|
||||||
|
return JSONResponse(
|
||||||
|
{
|
||||||
|
'message': translator.get('something_went_wrong', 'facefusion.apis')
|
||||||
|
}, status_code = HTTP_401_UNAUTHORIZED)
|
||||||
|
|
||||||
|
|
||||||
|
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):
|
||||||
|
session_context.set_session_id(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
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
|||||||
@@ -1,22 +1,20 @@
|
|||||||
from starlette.requests import Request
|
from starlette.requests import Request
|
||||||
from starlette.responses import JSONResponse
|
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 starlette.status import HTTP_200_OK, HTTP_404_NOT_FOUND
|
||||||
|
|
||||||
from facefusion import args_helper, capability_store, session_manager, state_manager, translator
|
from facefusion import args_store, session_manager, state_manager, translator
|
||||||
from facefusion.apis import asset_store
|
from facefusion.apis import asset_store
|
||||||
from facefusion.apis.endpoints.session import extract_access_token
|
from facefusion.apis.endpoints.session import extract_access_token
|
||||||
|
|
||||||
|
|
||||||
async def get_state(request : Request) -> JSONResponse:
|
async def get_state(request : Request) -> JSONResponse:
|
||||||
api_args = args_helper.extract_api_args(state_manager.get_state())
|
api_args = args_store.filter_api_args(state_manager.get_state())
|
||||||
return JSONResponse(state_manager.collect_state(api_args), status_code = HTTP_200_OK)
|
return JSONResponse(state_manager.collect_state(api_args), status_code = HTTP_200_OK)
|
||||||
|
|
||||||
|
|
||||||
async def set_state(request : Request) -> JSONResponse:
|
async def set_state(request : Request) -> JSONResponse:
|
||||||
__api_args__ = {}
|
|
||||||
|
|
||||||
action = request.query_params.get('action')
|
action = request.query_params.get('action')
|
||||||
asset_type = request.query_params.get('type')
|
asset_type = request.query_params.get('asset_type')
|
||||||
|
|
||||||
if action == 'select' and asset_type == 'source':
|
if action == 'select' and asset_type == 'source':
|
||||||
return await select_source(request)
|
return await select_source(request)
|
||||||
@@ -25,26 +23,15 @@ async def set_state(request : Request) -> JSONResponse:
|
|||||||
return await select_target(request)
|
return await select_target(request)
|
||||||
|
|
||||||
body = await request.json()
|
body = await request.json()
|
||||||
api_args = capability_store.get_api_arguments()
|
api_args = args_store.get_api_args()
|
||||||
|
|
||||||
for key, value in body.items():
|
for key, value in body.items():
|
||||||
if key not in api_args:
|
if key 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)
|
state_manager.set_item(key, value)
|
||||||
|
|
||||||
__api_args__ = args_helper.extract_api_args(state_manager.get_state())
|
__api_args__ = args_store.filter_api_args(state_manager.get_state())
|
||||||
return JSONResponse(state_manager.collect_state(__api_args__), status_code = HTTP_200_OK)
|
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:
|
async def select_source(request : Request) -> JSONResponse:
|
||||||
body = await request.json()
|
body = await request.json()
|
||||||
@@ -63,7 +50,7 @@ async def select_source(request : Request) -> JSONResponse:
|
|||||||
|
|
||||||
state_manager.set_item('source_paths', source_paths)
|
state_manager.set_item('source_paths', source_paths)
|
||||||
|
|
||||||
__api_args__ = args_helper.extract_api_args(state_manager.get_state())
|
__api_args__ = args_store.filter_api_args(state_manager.get_state())
|
||||||
return JSONResponse(state_manager.collect_state(__api_args__), status_code = HTTP_200_OK)
|
return JSONResponse(state_manager.collect_state(__api_args__), status_code = HTTP_200_OK)
|
||||||
|
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
@@ -84,7 +71,7 @@ async def select_target(request : Request) -> JSONResponse:
|
|||||||
if asset:
|
if asset:
|
||||||
state_manager.set_item('target_path', asset.get('path'))
|
state_manager.set_item('target_path', asset.get('path'))
|
||||||
|
|
||||||
__api_args__ = args_helper.extract_api_args(state_manager.get_state())
|
__api_args__ = args_store.filter_api_args(state_manager.get_state())
|
||||||
return JSONResponse(state_manager.collect_state(__api_args__), status_code = HTTP_200_OK)
|
return JSONResponse(state_manager.collect_state(__api_args__), status_code = HTTP_200_OK)
|
||||||
|
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ from starlette.websockets import WebSocket, WebSocketState
|
|||||||
|
|
||||||
from facefusion import session_context, session_manager
|
from facefusion import session_context, session_manager
|
||||||
from facefusion.apis.api_helper import get_sec_websocket_protocol
|
from facefusion.apis.api_helper import get_sec_websocket_protocol
|
||||||
from facefusion.apis.session_helper import extract_access_token
|
from facefusion.apis.endpoints.session import extract_access_token
|
||||||
from facefusion.apis.stream_manager import destroy_stream, process_image, process_video
|
from facefusion.apis.stream_manager import destroy_stream, process_image, process_video
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ LOCALES : Locales =\
|
|||||||
'invalid_access_token': 'invalid access token',
|
'invalid_access_token': 'invalid access token',
|
||||||
'invalid_refresh_token': 'invalid refresh token',
|
'invalid_refresh_token': 'invalid refresh token',
|
||||||
'source_asset_not_found': 'source asset not found',
|
'source_asset_not_found': 'source asset not found',
|
||||||
'target_asset_not_found': 'target asset not found',
|
'target_asset_not_found': 'target asset not found'
|
||||||
'invalid_state_key': 'invalid state key'
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
from facefusion.types import Locals
|
||||||
|
|
||||||
|
LOCALS : Locals =\
|
||||||
|
{
|
||||||
|
'en':
|
||||||
|
{
|
||||||
|
'ok': 'ok',
|
||||||
|
'something_went_wrong': 'something went wrong',
|
||||||
|
'invalid_access_token': 'invalid access token',
|
||||||
|
'invalid_refresh_token': 'invalid refresh token'
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
import asyncio
|
||||||
|
from functools import lru_cache
|
||||||
|
from typing import Any, Dict, Optional, cast
|
||||||
|
|
||||||
|
from starlette.datastructures import Headers
|
||||||
|
from starlette.websockets import WebSocket, WebSocketDisconnect
|
||||||
|
|
||||||
|
from facefusion import state_manager
|
||||||
|
from facefusion.execution import detect_execution_devices
|
||||||
|
from facefusion.system import get_cpu_info, get_disk_info, get_load_average, get_network_info
|
||||||
|
from facefusion.system import get_operating_system_info, get_python_info, get_ram_info, get_temperature_info
|
||||||
|
from facefusion.types import SystemInfo
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize = 1)
|
||||||
|
def get_cached_static_system_info() -> Dict[str, Any]:
|
||||||
|
return\
|
||||||
|
{
|
||||||
|
'operating_system': get_operating_system_info(),
|
||||||
|
'python': get_python_info()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize = 1)
|
||||||
|
def get_cached_semi_static_system_info(temp_path : Optional[str]) -> Dict[str, Any]:
|
||||||
|
return\
|
||||||
|
{
|
||||||
|
'disk': get_disk_info(temp_path),
|
||||||
|
'network': get_network_info()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def get_optimized_system_info(temp_path : Optional[str] = None) -> SystemInfo:
|
||||||
|
static_data = get_cached_static_system_info()
|
||||||
|
semi_static_data = get_cached_semi_static_system_info(temp_path)
|
||||||
|
dynamic_data : Dict[str, Any] =\
|
||||||
|
{
|
||||||
|
'cpu': get_cpu_info(),
|
||||||
|
'ram': get_ram_info(),
|
||||||
|
'temperatures': get_temperature_info(),
|
||||||
|
'load_average': get_load_average()
|
||||||
|
}
|
||||||
|
|
||||||
|
return cast(SystemInfo, {**static_data, **semi_static_data, **dynamic_data})
|
||||||
|
|
||||||
|
|
||||||
|
async def websocket_metrics(websocket : WebSocket) -> None:
|
||||||
|
subprotocol = get_requested_subprotocol(websocket)
|
||||||
|
await websocket.accept(subprotocol = subprotocol)
|
||||||
|
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
temp_path = state_manager.get_temp_path()
|
||||||
|
execution_devices = detect_execution_devices()
|
||||||
|
system_info = get_optimized_system_info(temp_path)
|
||||||
|
metrics =\
|
||||||
|
{
|
||||||
|
'devices': execution_devices,
|
||||||
|
'system': system_info
|
||||||
|
}
|
||||||
|
await websocket.send_json(metrics)
|
||||||
|
await asyncio.sleep(2)
|
||||||
|
|
||||||
|
except (WebSocketDisconnect, Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def get_requested_subprotocol(websocket : WebSocket) -> Optional[str]:
|
||||||
|
headers = Headers(scope = websocket.scope)
|
||||||
|
protocol_header = headers.get('Sec-WebSocket-Protocol')
|
||||||
|
|
||||||
|
if protocol_header:
|
||||||
|
protocol, _, _ = protocol_header.partition(',')
|
||||||
|
return protocol.strip()
|
||||||
|
|
||||||
|
return None
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
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,384 @@
|
|||||||
|
import os
|
||||||
|
import tempfile
|
||||||
|
from typing import Any, Dict, List
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
import yt_dlp # type: ignore
|
||||||
|
from gallery_dl import config as gallery_config, extractor as gallery_extractor, job as gallery_job
|
||||||
|
from starlette.requests import Request
|
||||||
|
from starlette.responses import JSONResponse
|
||||||
|
from starlette.status import HTTP_200_OK, HTTP_201_CREATED, HTTP_400_BAD_REQUEST, HTTP_500_INTERNAL_SERVER_ERROR
|
||||||
|
|
||||||
|
from facefusion import logger
|
||||||
|
from facefusion.apis import asset_store
|
||||||
|
from facefusion.choices import audio_formats
|
||||||
|
from facefusion.session_context import get_session_id
|
||||||
|
|
||||||
|
|
||||||
|
def resolve_image_urls(url : str) -> List[str]:
|
||||||
|
gallery_config.load()
|
||||||
|
image_urls : List[str] = []
|
||||||
|
|
||||||
|
try:
|
||||||
|
for extractor_instance in gallery_extractor.extractors():
|
||||||
|
if extractor_instance.pattern and extractor_instance.pattern.match(url):
|
||||||
|
logger.info(f'Detected gallery URL using extractor: {extractor_instance.__name__}', __name__)
|
||||||
|
extractor_obj = extractor_instance.from_url(url)
|
||||||
|
|
||||||
|
if extractor_obj:
|
||||||
|
for msg in extractor_obj:
|
||||||
|
if isinstance(msg, tuple) and len(msg) >= 2:
|
||||||
|
msg_type = msg[0]
|
||||||
|
if msg_type == 5:
|
||||||
|
image_data = msg[1]
|
||||||
|
image_url = image_data.get('url')
|
||||||
|
if image_url:
|
||||||
|
image_urls.append(image_url)
|
||||||
|
break
|
||||||
|
|
||||||
|
if not image_urls:
|
||||||
|
logger.info('Not a gallery URL, treating as direct image URL', __name__)
|
||||||
|
image_urls = [url]
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f'Failed to extract image URLs: {e}', __name__)
|
||||||
|
logger.info('Falling back to treating as direct image URL', __name__)
|
||||||
|
image_urls = [url]
|
||||||
|
|
||||||
|
return image_urls
|
||||||
|
|
||||||
|
|
||||||
|
def download_images_from_url(url : str, asset_type : str) -> List[str]:
|
||||||
|
gallery_config.load()
|
||||||
|
temp_dir = tempfile.gettempdir()
|
||||||
|
asset_ids : List[str] = []
|
||||||
|
|
||||||
|
is_gallery = False
|
||||||
|
for extractor_instance in gallery_extractor.extractors():
|
||||||
|
if extractor_instance.pattern and extractor_instance.pattern.match(url):
|
||||||
|
logger.info(f'Detected gallery URL using extractor: {extractor_instance.__name__}', __name__)
|
||||||
|
is_gallery = True
|
||||||
|
|
||||||
|
output_dir = os.path.join(temp_dir, f'facefusion_gallery_{os.urandom(8).hex()}')
|
||||||
|
os.makedirs(output_dir, exist_ok = True)
|
||||||
|
|
||||||
|
gallery_config.set((), 'base-directory', output_dir)
|
||||||
|
gallery_config.set((), 'skip', False)
|
||||||
|
|
||||||
|
gdl_job = gallery_job.DownloadJob(url)
|
||||||
|
gdl_job.run()
|
||||||
|
|
||||||
|
session_id = get_session_id()
|
||||||
|
for root, dirs, files in os.walk(output_dir):
|
||||||
|
for filename in files:
|
||||||
|
file_path = os.path.join(root, filename)
|
||||||
|
asset = asset_store.create_asset(session_id, asset_type, file_path)
|
||||||
|
if asset:
|
||||||
|
asset_ids.append(asset.get('id'))
|
||||||
|
logger.info(f'Registered image as asset {asset.get("id")}', __name__)
|
||||||
|
|
||||||
|
break
|
||||||
|
|
||||||
|
if not is_gallery:
|
||||||
|
logger.info('Not a gallery URL, treating as direct image URL', __name__)
|
||||||
|
with httpx.stream('GET', url, timeout = 30, follow_redirects = True) as response:
|
||||||
|
response.raise_for_status()
|
||||||
|
|
||||||
|
content_type = response.headers.get('content-type', '')
|
||||||
|
if not content_type.startswith('image/'):
|
||||||
|
raise ValueError(f'URL does not point to an image. Content-Type: {content_type}')
|
||||||
|
|
||||||
|
file_extension = None
|
||||||
|
if 'image/jpeg' in content_type or 'image/jpg' in content_type:
|
||||||
|
file_extension = '.jpg'
|
||||||
|
if 'image/png' in content_type:
|
||||||
|
file_extension = '.png'
|
||||||
|
if 'image/gif' in content_type:
|
||||||
|
file_extension = '.gif'
|
||||||
|
if 'image/webp' in content_type:
|
||||||
|
file_extension = '.webp'
|
||||||
|
|
||||||
|
if not file_extension:
|
||||||
|
url_path = url.split('?')[0]
|
||||||
|
if '.' in url_path:
|
||||||
|
file_extension = '.' + url_path.split('.')[-1].lower()
|
||||||
|
else:
|
||||||
|
file_extension = '.jpg'
|
||||||
|
|
||||||
|
filename = f'facefusion_image_{os.urandom(8).hex()}{file_extension}'
|
||||||
|
file_path = os.path.join(temp_dir, filename)
|
||||||
|
|
||||||
|
with open(file_path, 'wb') as f:
|
||||||
|
for chunk in response.iter_bytes(chunk_size = 8192):
|
||||||
|
f.write(chunk)
|
||||||
|
|
||||||
|
session_id = get_session_id()
|
||||||
|
asset = asset_store.create_asset(session_id, asset_type, file_path)
|
||||||
|
if asset:
|
||||||
|
asset_ids.append(asset.get('id'))
|
||||||
|
logger.info(f'Downloaded and registered image as asset {asset.get("id")}', __name__)
|
||||||
|
|
||||||
|
return asset_ids
|
||||||
|
|
||||||
|
|
||||||
|
def download_audio_from_url(url : str, asset_type : str) -> List[str]:
|
||||||
|
temp_dir = tempfile.gettempdir()
|
||||||
|
asset_ids : List[str] = []
|
||||||
|
|
||||||
|
# Extract file extension from URL
|
||||||
|
url_path = url.split('?')[0]
|
||||||
|
url_extension = os.path.splitext(url_path)[1].lstrip('.')
|
||||||
|
|
||||||
|
# Validate extension against supported audio formats
|
||||||
|
if url_extension not in audio_formats:
|
||||||
|
raise ValueError(f'Unsupported audio format: {url_extension}. Supported formats: {", ".join(audio_formats)}')
|
||||||
|
|
||||||
|
logger.info(f'Downloading audio from URL with extension: {url_extension}', __name__)
|
||||||
|
with httpx.stream('GET', url, timeout = 30, follow_redirects = True) as response:
|
||||||
|
response.raise_for_status()
|
||||||
|
|
||||||
|
filename = f'facefusion_audio_{os.urandom(8).hex()}.{url_extension}'
|
||||||
|
file_path = os.path.join(temp_dir, filename)
|
||||||
|
|
||||||
|
with open(file_path, 'wb') as f:
|
||||||
|
for chunk in response.iter_bytes(chunk_size = 8192):
|
||||||
|
f.write(chunk)
|
||||||
|
|
||||||
|
session_id = get_session_id()
|
||||||
|
asset = asset_store.create_asset(session_id, asset_type, file_path)
|
||||||
|
if asset:
|
||||||
|
asset_ids.append(asset.get('id'))
|
||||||
|
logger.info(f'Downloaded and registered audio as asset {asset.get("id")}', __name__)
|
||||||
|
|
||||||
|
return asset_ids
|
||||||
|
|
||||||
|
|
||||||
|
async def remote(request : Request) -> JSONResponse:
|
||||||
|
body = await request.json()
|
||||||
|
url = body.get('url')
|
||||||
|
action = request.query_params.get('action')
|
||||||
|
media_type = request.query_params.get('media_type', 'video')
|
||||||
|
asset_type = request.query_params.get('asset_type', 'target')
|
||||||
|
|
||||||
|
if not action:
|
||||||
|
return JSONResponse({'message': 'No action provided. Must be "resolve" or "download"'}, status_code = HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
if action not in ['resolve', 'download']:
|
||||||
|
return JSONResponse({'message': 'Invalid action. Must be "resolve" or "download"'}, status_code = HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
if media_type not in ['image', 'video', 'audio']:
|
||||||
|
return JSONResponse({'message': 'Invalid media_type. Must be "image", "video", or "audio"'}, status_code = HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
if asset_type not in ['source', 'target']:
|
||||||
|
return JSONResponse({'message': 'Invalid asset_type. Must be "source" or "target"'}, status_code = HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
if not url:
|
||||||
|
return JSONResponse({'message': 'No URL provided'}, status_code = HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
if not isinstance(url, str):
|
||||||
|
return JSONResponse({'message': 'URL must be a string'}, status_code = HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
url = url.strip()
|
||||||
|
|
||||||
|
if not url.startswith('http://') and not url.startswith('https://'):
|
||||||
|
return JSONResponse({'message': 'URL must start with http:// or https://'}, status_code = HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
quality = body.get('quality', '720p')
|
||||||
|
|
||||||
|
if quality not in ['360p', '480p', '720p', '1080p']:
|
||||||
|
return JSONResponse({'message': 'Quality must be 360p, 480p, 720p, or 1080p'}, status_code = HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
if action == 'resolve':
|
||||||
|
if media_type == 'image':
|
||||||
|
image_urls = resolve_image_urls(url)
|
||||||
|
logger.info(f'Resolved {len(image_urls)} image URL(s)', __name__)
|
||||||
|
|
||||||
|
response_data =\
|
||||||
|
{
|
||||||
|
'message': 'Image URL(s) resolved successfully',
|
||||||
|
'image_urls': image_urls,
|
||||||
|
'count': len(image_urls)
|
||||||
|
}
|
||||||
|
|
||||||
|
return JSONResponse(response_data, status_code = HTTP_200_OK)
|
||||||
|
|
||||||
|
quality_map =\
|
||||||
|
{
|
||||||
|
'360p': 'bestvideo[height<=360][ext=mp4]+bestaudio[ext=m4a]/best[height<=360]',
|
||||||
|
'480p': 'bestvideo[height<=480][ext=mp4]+bestaudio[ext=m4a]/best[height<=480]',
|
||||||
|
'720p': 'bestvideo[height<=720][ext=mp4]+bestaudio[ext=m4a]/best[height<=720]',
|
||||||
|
'1080p': 'bestvideo[height<=1080][ext=mp4]+bestaudio[ext=m4a]/best[height<=1080]'
|
||||||
|
}
|
||||||
|
|
||||||
|
ydl_opts : Dict[str, Any] =\
|
||||||
|
{
|
||||||
|
'format': quality_map[quality],
|
||||||
|
'quiet': True,
|
||||||
|
'no_warnings': True
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info(f'Extracting stream URL from {url} at {quality}', __name__)
|
||||||
|
|
||||||
|
try:
|
||||||
|
ydl = yt_dlp.YoutubeDL(ydl_opts)
|
||||||
|
info = ydl.extract_info(url, download = False)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f'Failed to extract video information: {e}', __name__)
|
||||||
|
return JSONResponse({'message': f'Failed to extract video information: {str(e)}'}, status_code = HTTP_500_INTERNAL_SERVER_ERROR)
|
||||||
|
|
||||||
|
if not info:
|
||||||
|
logger.error('Failed to extract video information', __name__)
|
||||||
|
return JSONResponse({'message': 'Failed to extract video information'}, status_code = HTTP_500_INTERNAL_SERVER_ERROR)
|
||||||
|
|
||||||
|
stream_url = info.get('url')
|
||||||
|
|
||||||
|
if not stream_url:
|
||||||
|
if 'requested_formats' in info and len(info['requested_formats']) > 0:
|
||||||
|
stream_url = info['requested_formats'][0].get('url')
|
||||||
|
logger.info('Using URL from requested_formats (video track)', __name__)
|
||||||
|
elif 'formats' in info and len(info['formats']) > 0:
|
||||||
|
for fmt in reversed(info['formats']):
|
||||||
|
if fmt.get('url') and fmt.get('vcodec') != 'none':
|
||||||
|
stream_url = fmt['url']
|
||||||
|
logger.info(f'Using URL from format: {fmt.get("format_id")}', __name__)
|
||||||
|
break
|
||||||
|
|
||||||
|
if not stream_url:
|
||||||
|
logger.error('No stream URL found in any format', __name__)
|
||||||
|
logger.debug(f'Available keys in info: {list(info.keys())}', __name__)
|
||||||
|
return JSONResponse({'message': 'No stream URL found'}, status_code = HTTP_500_INTERNAL_SERVER_ERROR)
|
||||||
|
|
||||||
|
audio_url = None
|
||||||
|
if 'requested_formats' in info and len(info['requested_formats']) > 1:
|
||||||
|
audio_url = info['requested_formats'][1].get('url')
|
||||||
|
if audio_url:
|
||||||
|
logger.info('Found separate audio track URL', __name__)
|
||||||
|
|
||||||
|
duration = info.get('duration')
|
||||||
|
fps = info.get('fps')
|
||||||
|
width = info.get('width')
|
||||||
|
height = info.get('height')
|
||||||
|
|
||||||
|
total_frames = None
|
||||||
|
if duration and fps:
|
||||||
|
total_frames = int(duration * fps)
|
||||||
|
logger.info(f'Calculated total frames: {total_frames} ({duration}s * {fps} fps)', __name__)
|
||||||
|
|
||||||
|
logger.info('Stream URL extracted successfully', __name__)
|
||||||
|
|
||||||
|
response_data =\
|
||||||
|
{
|
||||||
|
'message': 'Stream URL resolved successfully',
|
||||||
|
'stream_url': stream_url,
|
||||||
|
'audio_url': audio_url,
|
||||||
|
'duration': duration,
|
||||||
|
'fps': fps,
|
||||||
|
'total_frames': total_frames,
|
||||||
|
'width': width,
|
||||||
|
'height': height
|
||||||
|
}
|
||||||
|
|
||||||
|
return JSONResponse(response_data, status_code = HTTP_200_OK)
|
||||||
|
|
||||||
|
if action == 'download':
|
||||||
|
if media_type == 'image':
|
||||||
|
try:
|
||||||
|
asset_ids = download_images_from_url(url, asset_type)
|
||||||
|
except ValueError as e:
|
||||||
|
return JSONResponse({'message': str(e)}, status_code = HTTP_400_BAD_REQUEST)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f'Failed to download image(s): {e}', __name__)
|
||||||
|
return JSONResponse({'message': f'Failed to download image(s): {str(e)}'}, status_code = HTTP_500_INTERNAL_SERVER_ERROR)
|
||||||
|
|
||||||
|
response_data =\
|
||||||
|
{
|
||||||
|
'message': f'Downloaded and registered {len(asset_ids)} image(s)',
|
||||||
|
'asset_ids': asset_ids,
|
||||||
|
'count': len(asset_ids)
|
||||||
|
}
|
||||||
|
|
||||||
|
return JSONResponse(response_data, status_code = HTTP_201_CREATED)
|
||||||
|
|
||||||
|
if media_type == 'audio':
|
||||||
|
try:
|
||||||
|
asset_ids = download_audio_from_url(url, asset_type)
|
||||||
|
except ValueError as e:
|
||||||
|
return JSONResponse({'message': str(e)}, status_code = HTTP_400_BAD_REQUEST)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f'Failed to download audio: {e}', __name__)
|
||||||
|
return JSONResponse({'message': f'Failed to download audio: {str(e)}'}, status_code = HTTP_500_INTERNAL_SERVER_ERROR)
|
||||||
|
|
||||||
|
response_data =\
|
||||||
|
{
|
||||||
|
'message': f'Downloaded and registered {len(asset_ids)} audio file(s)',
|
||||||
|
'asset_ids': asset_ids,
|
||||||
|
'count': len(asset_ids)
|
||||||
|
}
|
||||||
|
|
||||||
|
return JSONResponse(response_data, status_code = HTTP_201_CREATED)
|
||||||
|
|
||||||
|
quality_map =\
|
||||||
|
{
|
||||||
|
'360p': 'bestvideo[height<=360][ext=mp4]+bestaudio[ext=m4a]/best[height<=360][ext=mp4]/best[height<=360]',
|
||||||
|
'480p': 'bestvideo[height<=480][ext=mp4]+bestaudio[ext=m4a]/best[height<=480][ext=mp4]/best[height<=480]',
|
||||||
|
'720p': 'bestvideo[height<=720][ext=mp4]+bestaudio[ext=m4a]/best[height<=720][ext=mp4]/best[height<=720]',
|
||||||
|
'1080p': 'bestvideo[height<=1080][ext=mp4]+bestaudio[ext=m4a]/best[height<=1080][ext=mp4]/best[height<=1080]'
|
||||||
|
}
|
||||||
|
|
||||||
|
temp_dir = tempfile.gettempdir()
|
||||||
|
output_path = os.path.join(temp_dir, 'facefusion_remote_%(id)s.%(ext)s')
|
||||||
|
|
||||||
|
download_opts : Dict[str, Any] =\
|
||||||
|
{
|
||||||
|
'format': quality_map[quality],
|
||||||
|
'outtmpl': output_path,
|
||||||
|
'quiet': False,
|
||||||
|
'no_warnings': False
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info(f'Downloading video from {url} at {quality}', __name__)
|
||||||
|
|
||||||
|
ydl = yt_dlp.YoutubeDL(download_opts)
|
||||||
|
info = ydl.extract_info(url, download = True)
|
||||||
|
|
||||||
|
if not info:
|
||||||
|
logger.error('Failed to download video', __name__)
|
||||||
|
return JSONResponse({'message': 'Failed to download video'}, status_code = HTTP_500_INTERNAL_SERVER_ERROR)
|
||||||
|
|
||||||
|
downloaded_file = ydl.prepare_filename(info)
|
||||||
|
if not os.path.exists(downloaded_file):
|
||||||
|
logger.error(f'Downloaded file not found: {downloaded_file}', __name__)
|
||||||
|
return JSONResponse({'message': 'Downloaded file not found'}, status_code = HTTP_500_INTERNAL_SERVER_ERROR)
|
||||||
|
|
||||||
|
duration = info.get('duration')
|
||||||
|
fps = info.get('fps')
|
||||||
|
width = info.get('width')
|
||||||
|
height = info.get('height')
|
||||||
|
|
||||||
|
total_frames = None
|
||||||
|
if duration and fps:
|
||||||
|
total_frames = int(duration * fps)
|
||||||
|
logger.info(f'Calculated total frames: {total_frames} ({duration}s * {fps} fps)', __name__)
|
||||||
|
|
||||||
|
session_id = get_session_id()
|
||||||
|
asset = asset_store.create_asset(session_id, asset_type, downloaded_file)
|
||||||
|
asset_id = asset.get('id') if asset else None
|
||||||
|
logger.info(f'Video downloaded and registered as asset {asset_id}', __name__)
|
||||||
|
|
||||||
|
response_data =\
|
||||||
|
{
|
||||||
|
'message': 'Video downloaded and registered as asset',
|
||||||
|
'asset_id': asset_id,
|
||||||
|
'metadata':
|
||||||
|
{
|
||||||
|
'duration': duration,
|
||||||
|
'fps': fps,
|
||||||
|
'total_frames': total_frames,
|
||||||
|
'width': width,
|
||||||
|
'height': height
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return JSONResponse(response_data, status_code = HTTP_201_CREATED)
|
||||||
|
|
||||||
|
return JSONResponse({'message': 'Invalid request'}, status_code = HTTP_400_BAD_REQUEST)
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
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
|
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import ctypes
|
||||||
|
import time
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from queue import Queue
|
from queue import Queue
|
||||||
from typing import Optional, Tuple
|
from typing import Optional, Tuple
|
||||||
@@ -7,27 +9,28 @@ import numpy
|
|||||||
from facefusion import rtc
|
from facefusion import rtc
|
||||||
from facefusion.apis.stream_event import create_receive_event
|
from facefusion.apis.stream_event import create_receive_event
|
||||||
from facefusion.codecs import opus_decoder, opus_encoder
|
from facefusion.codecs import opus_decoder, opus_encoder
|
||||||
from facefusion.types import AudioCodec, AudioFrame, Buffer, OpusDecoder, RtcPeer, RtcPeerAudio, Time
|
from facefusion.types import AudioCodec, AudioFrame, OpusDecoder, RtcPeer, RtcPeerAudio
|
||||||
|
|
||||||
|
|
||||||
def run_audio_encode_loop(rtc_peer : RtcPeer, audio_queue : Queue[Tuple[Time, AudioFrame]]) -> None:
|
def run_audio_encode_loop(rtc_peer : RtcPeer, audio_queue : Queue[Tuple[float, AudioFrame]]) -> None:
|
||||||
audio_codec = rtc_peer.get('audio').get('codec')
|
|
||||||
temp_audio_time, temp_audio_frame = audio_queue.get()
|
temp_audio_time, temp_audio_frame = audio_queue.get()
|
||||||
audio_encoder = opus_encoder.create(48000, 2)
|
audio_encoder = opus_encoder.create(48000, 2)
|
||||||
|
audio_timestamp = 0
|
||||||
|
|
||||||
while numpy.any(temp_audio_frame):
|
while numpy.any(temp_audio_frame):
|
||||||
audio_buffer = opus_encoder.encode(audio_encoder, temp_audio_frame.tobytes(), 2)
|
audio_frame_size = len(temp_audio_frame) // 2
|
||||||
|
audio_buffer = opus_encoder.encode(audio_encoder, temp_audio_frame.tobytes(), audio_frame_size)
|
||||||
|
|
||||||
if audio_buffer:
|
if audio_buffer:
|
||||||
audio_timestamp = rtc.convert_time_to_timestamp(audio_codec, temp_audio_time)
|
|
||||||
rtc.send_audio(rtc_peer, audio_buffer, audio_timestamp)
|
rtc.send_audio(rtc_peer, audio_buffer, audio_timestamp)
|
||||||
|
audio_timestamp += audio_frame_size
|
||||||
|
|
||||||
temp_audio_time, temp_audio_frame = audio_queue.get()
|
temp_audio_time, temp_audio_frame = audio_queue.get()
|
||||||
|
|
||||||
opus_encoder.destroy(audio_encoder)
|
opus_encoder.destroy(audio_encoder)
|
||||||
|
|
||||||
|
|
||||||
def receive_audio_frames(rtc_peer_audio : RtcPeerAudio, audio_queue : Queue[Tuple[Time, AudioFrame]]) -> None:
|
def receive_audio_frames(rtc_peer_audio : RtcPeerAudio, audio_queue : Queue[Tuple[float, AudioFrame]]) -> None:
|
||||||
audio_track = rtc_peer_audio.get('receiver_track')
|
audio_track = rtc_peer_audio.get('receiver_track')
|
||||||
audio_codec = rtc_peer_audio.get('codec')
|
audio_codec = rtc_peer_audio.get('codec')
|
||||||
audio_decoder = create_audio_decoder(audio_codec)
|
audio_decoder = create_audio_decoder(audio_codec)
|
||||||
@@ -41,9 +44,9 @@ def receive_audio_frames(rtc_peer_audio : RtcPeerAudio, audio_queue : Queue[Tupl
|
|||||||
destroy_audio_decoder(audio_codec, audio_decoder)
|
destroy_audio_decoder(audio_codec, audio_decoder)
|
||||||
|
|
||||||
|
|
||||||
def decode_audio_frame(audio_codec : AudioCodec, audio_decoder : OpusDecoder, input_buffer : Buffer) -> Optional[Buffer]:
|
def decode_audio_frame(audio_codec : AudioCodec, audio_decoder : OpusDecoder, input_buffer : bytes) -> Optional[bytes]:
|
||||||
if audio_codec == 'opus':
|
if audio_codec == 'opus':
|
||||||
return opus_decoder.decode(audio_decoder, input_buffer, 2)
|
return opus_decoder.decode(audio_decoder, input_buffer, 960, 2)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
@@ -58,10 +61,11 @@ def destroy_audio_decoder(audio_codec : AudioCodec, audio_decoder : OpusDecoder)
|
|||||||
opus_decoder.destroy(audio_decoder)
|
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:
|
#todo: Alias Time for float
|
||||||
|
def handle_audio_frame(audio_codec : AudioCodec, audio_decoder : OpusDecoder, audio_queue : Queue[Tuple[float, AudioFrame]], track : int, data : ctypes.c_void_p, size : int, info : ctypes.c_void_p, pointer : ctypes.c_void_p) -> None:
|
||||||
|
audio_buffer = ctypes.string_at(data, size)
|
||||||
audio_frame = decode_audio_frame(audio_codec, audio_decoder, audio_buffer)
|
audio_frame = decode_audio_frame(audio_codec, audio_decoder, audio_buffer)
|
||||||
|
|
||||||
if audio_frame:
|
if audio_frame:
|
||||||
audio_frame = numpy.frombuffer(audio_frame, dtype = numpy.float32)
|
temp_audio_frame = numpy.frombuffer(audio_frame, dtype = numpy.float32)
|
||||||
audio_time = rtc.convert_timestamp_to_time(audio_codec, audio_timestamp)
|
audio_queue.put((time.monotonic(), temp_audio_frame))
|
||||||
audio_queue.put((audio_time, audio_frame))
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ def create_receive_event(track : int, frame_handler : FrameHandler) -> threading
|
|||||||
datachannel_library = datachannel_module.create_static_library()
|
datachannel_library = datachannel_module.create_static_library()
|
||||||
receive_event = threading.Event()
|
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))
|
frame_callback = ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p)(frame_handler)
|
||||||
close_callback = ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_void_p)(partial(dispatch_event, receive_event))
|
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.rtcSetFrameCallback(track, frame_callback)
|
||||||
datachannel_library.rtcSetClosedCallback(track, close_callback)
|
datachannel_library.rtcSetClosedCallback(track, close_callback)
|
||||||
@@ -20,11 +20,5 @@ def create_receive_event(track : int, frame_handler : FrameHandler) -> threading
|
|||||||
return receive_event
|
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:
|
def dispatch_event(event : threading.Event, track : int, pointer : ctypes.c_void_p) -> None:
|
||||||
event.set()
|
event.set()
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ 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_audio import receive_audio_frames, run_audio_encode_loop
|
||||||
from facefusion.apis.stream_video import receive_video_frames, run_video_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.libraries import datachannel as datachannel_module
|
||||||
from facefusion.types import AudioCodec, AudioFrame, BufferPack, PeerConnection, RtcPeer, RtcPeerAudio, SdpAnswer, SdpOffer, SessionId, Time, VideoCodec, VisionFrame
|
from facefusion.types import AudioCodec, AudioFrame, PeerConnection, Resolution, RtcPeer, RtcPeerAudio, SdpAnswer, SdpOffer, SessionId, VideoCodec, VisionFrame
|
||||||
|
|
||||||
|
|
||||||
async def process_image(websocket : WebSocket) -> None:
|
async def process_image(websocket : WebSocket) -> None:
|
||||||
@@ -105,8 +105,9 @@ def process_video(session_id : SessionId, sdp_offer : SdpOffer) -> Optional[SdpA
|
|||||||
|
|
||||||
def run_peer_loop(session_id : SessionId, rtc_peer : RtcPeer) -> None:
|
def run_peer_loop(session_id : SessionId, rtc_peer : RtcPeer) -> None:
|
||||||
execution_thread_count = state_manager.get_item('execution_thread_count')
|
execution_thread_count = state_manager.get_item('execution_thread_count')
|
||||||
video_queue : Queue[Tuple[Time, Future[BufferPack]]] = Queue(maxsize = execution_thread_count)
|
#todo: is bytes, Resolution not a XXXPointer type
|
||||||
audio_queue : Queue[Tuple[Time, AudioFrame]] = Queue(maxsize = execution_thread_count * 10)
|
video_queue : Queue[Tuple[float, Future[Tuple[bytes, Resolution]]]] = Queue(maxsize = execution_thread_count)
|
||||||
|
audio_queue : Queue[Tuple[float, AudioFrame]] = Queue(maxsize = execution_thread_count * 10)
|
||||||
video_executor = ThreadPoolExecutor(max_workers = execution_thread_count)
|
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_receiver_thread = threading.Thread(target = receive_video_frames, args = (rtc_peer.get('video'), video_queue, video_executor), daemon = True)
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import ctypes
|
||||||
|
import time
|
||||||
from concurrent.futures import Future, ThreadPoolExecutor
|
from concurrent.futures import Future, ThreadPoolExecutor
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from queue import Queue
|
from queue import Queue
|
||||||
@@ -9,53 +11,55 @@ import numpy
|
|||||||
from facefusion import rtc, streamer
|
from facefusion import rtc, streamer
|
||||||
from facefusion.apis.stream_event import create_receive_event
|
from facefusion.apis.stream_event import create_receive_event
|
||||||
from facefusion.codecs import aom_decoder, aom_encoder, vpx_decoder, vpx_encoder
|
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
|
from facefusion.types import AomDecoder, AomEncoder, AomPointer, BitRate, Resolution, RtcPeer, RtcPeerVideo, VideoCodec, VisionFrame, VpxDecoder, VpxEncoder, VpxPointer
|
||||||
|
|
||||||
|
|
||||||
def run_video_encode_loop(rtc_peer : RtcPeer, video_queue : Queue[Tuple[Time, Future[BufferPack]]]) -> None:
|
def run_video_encode_loop(rtc_peer : RtcPeer, video_queue : Queue[Tuple[float, Future[Tuple[bytes, Resolution]]]]) -> None:
|
||||||
video_codec = rtc_peer.get('video').get('codec')
|
video_codec = rtc_peer.get('video').get('codec')
|
||||||
video_time, video_future = video_queue.get()
|
video_time, video_future = video_queue.get()
|
||||||
video_pack = video_future.result()
|
video_buffer, video_resolution = video_future.result()
|
||||||
video_buffer = video_pack.get('buffer')
|
|
||||||
video_resolution = video_pack.get('resolution')
|
|
||||||
|
|
||||||
if video_buffer:
|
if video_buffer:
|
||||||
temp_resolution : Resolution = video_resolution
|
temp_resolution : Resolution = video_resolution
|
||||||
temp_bitrate : BitRate = 8000
|
temp_bitrate : BitRate = 8000
|
||||||
video_encoder = create_video_encoder(video_codec, temp_resolution, temp_bitrate)
|
video_encoder = create_video_encoder(video_codec, temp_resolution, temp_bitrate)
|
||||||
|
temp_video_time = video_time
|
||||||
frame_index = 0
|
frame_index = 0
|
||||||
|
|
||||||
while video_buffer:
|
while video_buffer:
|
||||||
sender_bitrate = rtc_peer.get('sender_bitrate').value
|
encode_start = time.monotonic()
|
||||||
|
sender_bitrate = calculate_sender_bitrate(rtc_peer, temp_bitrate)
|
||||||
|
|
||||||
if video_resolution[0] - temp_resolution[0] or video_resolution[1] - temp_resolution[1]:
|
if video_resolution[0] - temp_resolution[0] or video_resolution[1] - temp_resolution[1]:
|
||||||
temp_resolution = video_resolution
|
temp_resolution = video_resolution
|
||||||
update_video_encoder_resolution(video_codec, video_encoder, temp_resolution)
|
update_video_encoder_resolution(video_codec, video_encoder, temp_resolution)
|
||||||
|
|
||||||
if sender_bitrate > 0 and sender_bitrate - temp_bitrate:
|
if sender_bitrate - temp_bitrate:
|
||||||
temp_bitrate = sender_bitrate
|
temp_bitrate = sender_bitrate
|
||||||
update_video_encoder_bitrate(video_codec, video_encoder, temp_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)
|
__video_buffer__ = encode_video_frame(video_codec, video_encoder, video_buffer, temp_resolution, frame_index)
|
||||||
|
|
||||||
if __video_buffer__:
|
if __video_buffer__:
|
||||||
video_timestamp = rtc.convert_time_to_timestamp(video_codec, video_time)
|
video_timestamp = int(video_time * 90000)
|
||||||
rtc.send_video(rtc_peer, __video_buffer__, video_timestamp)
|
rtc.send_video(rtc_peer, __video_buffer__, video_timestamp)
|
||||||
|
|
||||||
receiver_bitrate = rtc_peer.get('receiver_bitrate').value
|
encode_time = time.monotonic() - encode_start
|
||||||
|
frame_interval = video_time - temp_video_time
|
||||||
|
temp_video_time = video_time
|
||||||
|
|
||||||
|
receiver_bitrate = calculate_receiver_bitrate(rtc_peer, encode_time, frame_interval)
|
||||||
rtc.adapt_receiver_bitrate(rtc_peer, receiver_bitrate)
|
rtc.adapt_receiver_bitrate(rtc_peer, receiver_bitrate)
|
||||||
frame_index += 1
|
frame_index += 1
|
||||||
|
|
||||||
video_time, video_future = video_queue.get()
|
video_time, video_future = video_queue.get()
|
||||||
video_pack = video_future.result()
|
video_buffer, video_resolution = video_future.result()
|
||||||
video_buffer = video_pack.get('buffer')
|
|
||||||
video_resolution = video_pack.get('resolution')
|
|
||||||
|
|
||||||
destroy_video_encoder(video_codec, video_encoder)
|
destroy_video_encoder(video_codec, video_encoder)
|
||||||
rtc.clear_bitrate(rtc_peer)
|
rtc.clear_bitrate(rtc_peer)
|
||||||
|
|
||||||
|
|
||||||
def receive_video_frames(rtc_peer_video : RtcPeerVideo, video_queue : Queue[Tuple[Time, Future[BufferPack]]], video_executor : ThreadPoolExecutor) -> None:
|
def receive_video_frames(rtc_peer_video : RtcPeerVideo, video_queue : Queue[Tuple[float, Future[Tuple[bytes, Resolution]]]], video_executor : ThreadPoolExecutor) -> None:
|
||||||
video_track = rtc_peer_video.get('receiver_track')
|
video_track = rtc_peer_video.get('receiver_track')
|
||||||
video_codec = rtc_peer_video.get('codec')
|
video_codec = rtc_peer_video.get('codec')
|
||||||
video_decoder = create_video_decoder(video_codec)
|
video_decoder = create_video_decoder(video_codec)
|
||||||
@@ -64,20 +68,45 @@ def receive_video_frames(rtc_peer_video : RtcPeerVideo, video_queue : Queue[Tupl
|
|||||||
receive_event = create_receive_event(video_track, video_frame_handler)
|
receive_event = create_receive_event(video_track, video_frame_handler)
|
||||||
receive_event.wait()
|
receive_event.wait()
|
||||||
|
|
||||||
empty_future : Future[BufferPack] = Future()
|
empty_future : Future[Tuple[bytes, Resolution]] = Future()
|
||||||
empty_future.set_result(BufferPack(buffer = bytes(), resolution = (0, 0)))
|
empty_future.set_result((bytes(), (0, 0)))
|
||||||
video_queue.put((0.0, empty_future))
|
video_queue.put((0.0, empty_future))
|
||||||
destroy_video_decoder(video_codec, video_decoder)
|
destroy_video_decoder(video_codec, video_decoder)
|
||||||
|
|
||||||
|
|
||||||
def process_video_frame(input_vision_frame : VisionFrame) -> BufferPack:
|
def process_video_frame(input_vision_frame : VisionFrame) -> Tuple[bytes, Resolution]:
|
||||||
output_vision_frame = streamer.process_stream_frame(input_vision_frame)
|
output_vision_frame = streamer.process_stream_frame(input_vision_frame)
|
||||||
output_resolution : Resolution = (output_vision_frame.shape[1], output_vision_frame.shape[0])
|
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()
|
output_buffer = cv2.cvtColor(output_vision_frame, cv2.COLOR_BGR2YUV_I420).tobytes()
|
||||||
return BufferPack(buffer = output_buffer, resolution = output_resolution)
|
return output_buffer, output_resolution
|
||||||
|
|
||||||
|
|
||||||
def decode_video_frame(video_codec : VideoCodec, video_decoder : VpxDecoder | AomDecoder, input_buffer : Buffer) -> Optional[VisionFrame]:
|
def calculate_receiver_bitrate(rtc_peer : RtcPeer, encode_time : float, frame_interval : float) -> BitRate:
|
||||||
|
min_bitrate : BitRate = 500
|
||||||
|
max_bitrate : BitRate = 8000
|
||||||
|
bitrate : BitRate = rtc_peer.get('receiver_bitrate').value
|
||||||
|
|
||||||
|
if frame_interval > 0:
|
||||||
|
scale = frame_interval / encode_time
|
||||||
|
bitrate = int(bitrate * scale)
|
||||||
|
bitrate = max(min_bitrate, min(max_bitrate, bitrate))
|
||||||
|
|
||||||
|
return bitrate
|
||||||
|
|
||||||
|
|
||||||
|
#todo: does not feel final as this is an clamp and not calculate
|
||||||
|
def calculate_sender_bitrate(rtc_peer : RtcPeer, bitrate : BitRate) -> BitRate:
|
||||||
|
min_bitrate : BitRate = 500
|
||||||
|
max_bitrate : BitRate = 8000
|
||||||
|
peer_bitrate : BitRate = rtc_peer.get('sender_bitrate').value
|
||||||
|
|
||||||
|
if peer_bitrate > 0:
|
||||||
|
bitrate = max(min_bitrate, min(max_bitrate, peer_bitrate))
|
||||||
|
|
||||||
|
return bitrate
|
||||||
|
|
||||||
|
|
||||||
|
def decode_video_frame(video_codec : VideoCodec, video_decoder : VpxDecoder | AomDecoder, input_buffer : bytes) -> Optional[VisionFrame]:
|
||||||
if video_codec == 'av1':
|
if video_codec == 'av1':
|
||||||
aom_pointer = aom_decoder.decode(video_decoder, input_buffer)
|
aom_pointer = aom_decoder.decode(video_decoder, input_buffer)
|
||||||
|
|
||||||
@@ -93,7 +122,7 @@ def decode_video_frame(video_codec : VideoCodec, video_decoder : VpxDecoder | Ao
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def encode_video_frame(video_codec : VideoCodec, video_encoder : VpxEncoder | AomEncoder, input_buffer : Buffer, frame_resolution : Resolution, frame_index : int) -> Buffer:
|
def encode_video_frame(video_codec : VideoCodec, video_encoder : VpxEncoder | AomEncoder, input_buffer : bytes, frame_resolution : Resolution, frame_index : int) -> bytes:
|
||||||
if video_codec == 'av1':
|
if video_codec == 'av1':
|
||||||
return aom_encoder.encode(video_encoder, input_buffer, frame_resolution, frame_index)
|
return aom_encoder.encode(video_encoder, input_buffer, frame_resolution, frame_index)
|
||||||
|
|
||||||
@@ -103,7 +132,7 @@ def encode_video_frame(video_codec : VideoCodec, video_encoder : VpxEncoder | Ao
|
|||||||
return bytes()
|
return bytes()
|
||||||
|
|
||||||
|
|
||||||
def normalize_vision_frame(frame_pointer : BufferPack) -> VisionFrame:
|
def normalize_vision_frame(frame_pointer : AomPointer | VpxPointer) -> VisionFrame:
|
||||||
frame_width, frame_height = frame_pointer.get('resolution')
|
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))
|
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)
|
return cv2.cvtColor(vision_frame, cv2.COLOR_YUV2BGR_I420)
|
||||||
@@ -165,10 +194,11 @@ def update_video_encoder_bitrate(video_codec : VideoCodec, video_encoder : VpxEn
|
|||||||
return False
|
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:
|
#todo: we can remove the dead args or pass audio buffer
|
||||||
|
def handle_video_frame(video_codec : VideoCodec, video_decoder : VpxDecoder | AomDecoder, video_queue : Queue[Tuple[float, Future[Tuple[bytes, Resolution]]]], video_executor : ThreadPoolExecutor, track : int, data : ctypes.c_void_p, size : int, info : ctypes.c_void_p, pointer : ctypes.c_void_p) -> None:
|
||||||
|
video_buffer = ctypes.string_at(data, size)
|
||||||
vision_frame = decode_video_frame(video_codec, video_decoder, video_buffer)
|
vision_frame = decode_video_frame(video_codec, video_decoder, video_buffer)
|
||||||
|
|
||||||
if numpy.any(vision_frame) and video_queue.qsize() < video_queue.maxsize:
|
if numpy.any(vision_frame) and video_queue.qsize() < video_queue.maxsize:
|
||||||
video_future = video_executor.submit(process_video_frame, vision_frame)
|
video_future = video_executor.submit(process_video_frame, vision_frame)
|
||||||
video_time = rtc.convert_timestamp_to_time(video_codec, video_timestamp)
|
video_queue.put((time.monotonic(), video_future))
|
||||||
video_queue.put((video_time, video_future))
|
|
||||||
|
|||||||
@@ -0,0 +1,207 @@
|
|||||||
|
import base64
|
||||||
|
import subprocess
|
||||||
|
from typing import List, Optional
|
||||||
|
|
||||||
|
import cv2
|
||||||
|
import numpy
|
||||||
|
from starlette.requests import Request
|
||||||
|
from starlette.responses import JSONResponse
|
||||||
|
from starlette.status import HTTP_200_OK, HTTP_400_BAD_REQUEST
|
||||||
|
|
||||||
|
from facefusion import logger
|
||||||
|
from facefusion.apis import asset_store
|
||||||
|
from facefusion.filesystem import is_video
|
||||||
|
from facefusion.video_manager import get_video_capture
|
||||||
|
from facefusion.vision import fit_contain_frame
|
||||||
|
|
||||||
|
|
||||||
|
def extract_frame_at_timestamp(stream_url : str, timestamp : float, width : int, height : int) -> Optional[numpy.ndarray]:
|
||||||
|
ffmpeg_command =\
|
||||||
|
[
|
||||||
|
'ffmpeg',
|
||||||
|
'-user_agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
|
||||||
|
'-ss', str(timestamp),
|
||||||
|
'-i', stream_url,
|
||||||
|
'-vf', f'scale={width}:{height}',
|
||||||
|
'-frames:v', '1',
|
||||||
|
'-f', 'rawvideo',
|
||||||
|
'-pix_fmt', 'bgr24',
|
||||||
|
'-'
|
||||||
|
]
|
||||||
|
|
||||||
|
try:
|
||||||
|
result = subprocess.run(ffmpeg_command, capture_output = True, timeout = 10)
|
||||||
|
if result.returncode == 0 and result.stdout:
|
||||||
|
frame_size = width * height * 3
|
||||||
|
if len(result.stdout) >= frame_size:
|
||||||
|
frame = numpy.frombuffer(result.stdout[:frame_size], dtype = numpy.uint8).reshape((height, width, 3))
|
||||||
|
return frame
|
||||||
|
except Exception as e:
|
||||||
|
logger.debug(f'Failed to extract frame at {timestamp}s: {e}', __name__)
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
async def get_timeline(request: Request) -> JSONResponse:
|
||||||
|
"""
|
||||||
|
Return N preview frames (as base64 JPEGs) from the target video,
|
||||||
|
resized to specified resolution for timeline preview.
|
||||||
|
|
||||||
|
Route: /timeline/{count:int}?target_path=...&is_remote_stream=true&duration=120&fps=30&target_width=1920&target_height=1080&width=160&height=120
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Extract and validate requested count
|
||||||
|
try:
|
||||||
|
count = int(request.path_params.get('count', 0))
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return JSONResponse({'message': 'Invalid count parameter'}, status_code=HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
if count <= 0:
|
||||||
|
return JSONResponse({'message': 'Count must be a positive integer'}, status_code=HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
# Extract and validate preview resolution parameters
|
||||||
|
try:
|
||||||
|
preview_width = int(request.query_params.get('width', 160))
|
||||||
|
preview_height = int(request.query_params.get('height', 120))
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return JSONResponse({'message': 'Invalid width or height parameter'}, status_code=HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
if preview_width <= 0 or preview_height <= 0 or preview_width > 1920 or preview_height > 1080:
|
||||||
|
return JSONResponse({'message': 'Width and height must be between 1 and 1920x1080'}, status_code=HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
# Extract target_path or asset_id (one is required)
|
||||||
|
target_path = request.query_params.get('target_path')
|
||||||
|
asset_id = request.query_params.get('asset_id')
|
||||||
|
|
||||||
|
# Extract is_remote_stream flag
|
||||||
|
is_remote_stream_param = request.query_params.get('is_remote_stream', 'false').lower()
|
||||||
|
is_remote_stream = is_remote_stream_param in ['true', '1', 'yes']
|
||||||
|
|
||||||
|
# Resolve asset_id to path if provided (for local files)
|
||||||
|
if asset_id and not target_path:
|
||||||
|
from facefusion.session_context import get_session_id
|
||||||
|
|
||||||
|
session_id = get_session_id()
|
||||||
|
asset = asset_store.get_asset(session_id, asset_id)
|
||||||
|
if not asset:
|
||||||
|
return JSONResponse({'message': f'Asset not found: {asset_id}'}, status_code=HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
target_path = asset.get('path')
|
||||||
|
if not target_path:
|
||||||
|
return JSONResponse({'message': 'Asset has no path'}, status_code=HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
is_remote_stream = False # Assets are always local files
|
||||||
|
logger.debug(f'Resolved asset_id {asset_id} to path for timeline preview', __name__)
|
||||||
|
|
||||||
|
# Now check if we have a target_path
|
||||||
|
if not target_path:
|
||||||
|
return JSONResponse({'message': 'Missing required parameter: either target_path or asset_id'}, status_code=HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
# Extract video metadata (optional for local files, required for remote streams)
|
||||||
|
duration = None
|
||||||
|
fps = None
|
||||||
|
width = 1280
|
||||||
|
height = 720
|
||||||
|
|
||||||
|
if request.query_params.get('duration'):
|
||||||
|
try:
|
||||||
|
duration = float(request.query_params.get('duration'))
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return JSONResponse({'message': 'Invalid duration parameter'}, status_code=HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
if request.query_params.get('fps'):
|
||||||
|
try:
|
||||||
|
fps = float(request.query_params.get('fps'))
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return JSONResponse({'message': 'Invalid fps parameter'}, status_code=HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
if request.query_params.get('target_width'):
|
||||||
|
try:
|
||||||
|
width = int(request.query_params.get('target_width'))
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return JSONResponse({'message': 'Invalid target_width parameter'}, status_code=HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
if request.query_params.get('target_height'):
|
||||||
|
try:
|
||||||
|
height = int(request.query_params.get('target_height'))
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return JSONResponse({'message': 'Invalid target_height parameter'}, status_code=HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
previews: List[str] = []
|
||||||
|
|
||||||
|
if is_remote_stream:
|
||||||
|
if not duration or duration <= 0:
|
||||||
|
return JSONResponse({'message': 'Duration not available for remote stream'}, status_code=HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
frame_total = 0
|
||||||
|
if duration and fps:
|
||||||
|
try:
|
||||||
|
frame_total = int(float(duration) * float(fps))
|
||||||
|
except Exception:
|
||||||
|
frame_total = 0
|
||||||
|
|
||||||
|
sample_count = min(count, frame_total) if frame_total > 0 else count
|
||||||
|
timestamps = list(numpy.linspace(0, float(duration), num=sample_count, endpoint=False))
|
||||||
|
|
||||||
|
logger.info(f'Extracting {sample_count} frames from remote stream using ffmpeg', __name__)
|
||||||
|
|
||||||
|
for timestamp in timestamps:
|
||||||
|
frame = extract_frame_at_timestamp(target_path, timestamp, width, height)
|
||||||
|
if frame is None:
|
||||||
|
logger.warn(f'Failed to extract frame at {timestamp}s', __name__)
|
||||||
|
continue
|
||||||
|
|
||||||
|
thumb_bgr = fit_contain_frame(frame, (preview_width, preview_height))
|
||||||
|
if thumb_bgr.shape[1] != preview_width or thumb_bgr.shape[0] != preview_height:
|
||||||
|
thumb_bgr = cv2.resize(thumb_bgr, (preview_width, preview_height))
|
||||||
|
|
||||||
|
ok_enc, buf = cv2.imencode('.jpg', thumb_bgr, [cv2.IMWRITE_JPEG_QUALITY, 50])
|
||||||
|
if not ok_enc:
|
||||||
|
logger.warn(f'JPEG encode failed for timestamp {timestamp}s', __name__)
|
||||||
|
continue
|
||||||
|
|
||||||
|
b64 = base64.b64encode(buf.tobytes()).decode('ascii')
|
||||||
|
previews.append(b64)
|
||||||
|
else:
|
||||||
|
video_capture = get_video_capture(target_path)
|
||||||
|
if not video_capture or not video_capture.isOpened():
|
||||||
|
logger.error(f'Unable to open video capture for target: {target_path}', __name__)
|
||||||
|
return JSONResponse({'message': 'Unable to open target video'}, status_code=HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
frame_total = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT) or 0)
|
||||||
|
|
||||||
|
if frame_total <= 0 and is_video(target_path):
|
||||||
|
return JSONResponse({'message': 'Could not determine frame count for target video'}, status_code=HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
sample_count = min(count, frame_total)
|
||||||
|
indices: List[int] = list(numpy.linspace(1, frame_total, num=sample_count, endpoint=True, dtype=int))
|
||||||
|
|
||||||
|
for frame_number in indices:
|
||||||
|
video_capture.set(cv2.CAP_PROP_POS_FRAMES, max(0, frame_number - 1))
|
||||||
|
ok_read, frame = video_capture.read()
|
||||||
|
if not ok_read or frame is None:
|
||||||
|
logger.warn(f'Failed reading frame {frame_number}', __name__)
|
||||||
|
continue
|
||||||
|
|
||||||
|
thumb_bgr = fit_contain_frame(frame, (preview_width, preview_height))
|
||||||
|
if thumb_bgr.shape[1] != preview_width or thumb_bgr.shape[0] != preview_height:
|
||||||
|
thumb_bgr = cv2.resize(thumb_bgr, (preview_width, preview_height))
|
||||||
|
|
||||||
|
ok_enc, buf = cv2.imencode('.jpg', thumb_bgr, [cv2.IMWRITE_JPEG_QUALITY, 50])
|
||||||
|
if not ok_enc:
|
||||||
|
logger.warn(f'JPEG encode failed for frame {frame_number}', __name__)
|
||||||
|
continue
|
||||||
|
b64 = base64.b64encode(buf.tobytes()).decode('ascii')
|
||||||
|
previews.append(b64)
|
||||||
|
|
||||||
|
logger.info(f'Returned {len(previews)}/{sample_count} timeline frames at {preview_width}x{preview_height}', __name__)
|
||||||
|
|
||||||
|
return JSONResponse({
|
||||||
|
'message': 'ok',
|
||||||
|
'count': len(previews),
|
||||||
|
'requested': count,
|
||||||
|
'width': preview_width,
|
||||||
|
'height': preview_height,
|
||||||
|
'format': 'jpeg',
|
||||||
|
'frames': previews
|
||||||
|
}, status_code=HTTP_200_OK)
|
||||||
@@ -0,0 +1,98 @@
|
|||||||
|
import subprocess
|
||||||
|
from functools import lru_cache
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from starlette.datastructures import Headers
|
||||||
|
from starlette.requests import Request
|
||||||
|
from starlette.responses import JSONResponse
|
||||||
|
from starlette.types import ASGIApp, Receive, Scope, Send
|
||||||
|
from starlette.websockets import WebSocket
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize = 1)
|
||||||
|
def get_api_version() -> str:
|
||||||
|
try:
|
||||||
|
result = subprocess.run(['git', 'rev-parse', 'HEAD'], capture_output = True, text = True, check = True)
|
||||||
|
return result.stdout.strip()
|
||||||
|
except Exception:
|
||||||
|
return 'unknown'
|
||||||
|
|
||||||
|
|
||||||
|
def check_version_match(request : Request) -> Optional[JSONResponse]:
|
||||||
|
client_version = request.headers.get('X-API-Version')
|
||||||
|
server_version = get_api_version()
|
||||||
|
|
||||||
|
if not client_version:
|
||||||
|
return JSONResponse({'error': 'Missing X-API-Version header', 'server_version': server_version}, status_code = 400)
|
||||||
|
|
||||||
|
if client_version != server_version:
|
||||||
|
return JSONResponse({'error': 'Version mismatch', 'client_version': client_version, 'server_version': server_version}, status_code = 409)
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def check_version_match_websocket(websocket : WebSocket) -> Optional[str]:
|
||||||
|
client_version = websocket.headers.get('X-API-Version')
|
||||||
|
server_version = get_api_version()
|
||||||
|
|
||||||
|
if not client_version:
|
||||||
|
return f'Missing X-API-Version header, server version: {server_version}'
|
||||||
|
|
||||||
|
if client_version != server_version:
|
||||||
|
return f'Version mismatch: client={client_version}, server={server_version}'
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
async def version_guard_middleware(scope : Scope, receive : Receive, send : Send, app : ASGIApp) -> None:
|
||||||
|
if scope['type'] == 'http':
|
||||||
|
# Skip version check for OPTIONS requests (CORS preflight)
|
||||||
|
if scope.get('method') == 'OPTIONS':
|
||||||
|
await app(scope, receive, send)
|
||||||
|
return
|
||||||
|
|
||||||
|
headers = Headers(scope = scope)
|
||||||
|
client_version = headers.get('X-API-Version')
|
||||||
|
server_version = get_api_version()
|
||||||
|
|
||||||
|
if not client_version:
|
||||||
|
response = JSONResponse({'error': 'Missing X-API-Version header', 'server_version': server_version}, status_code = 400)
|
||||||
|
await response(scope, receive, send)
|
||||||
|
return
|
||||||
|
|
||||||
|
if client_version != server_version:
|
||||||
|
response = JSONResponse({'error': 'Version mismatch', 'client_version': client_version, 'server_version': server_version}, status_code = 409)
|
||||||
|
await response(scope, receive, send)
|
||||||
|
return
|
||||||
|
|
||||||
|
if scope['type'] == 'websocket':
|
||||||
|
headers = Headers(scope = scope)
|
||||||
|
client_version = headers.get('X-API-Version')
|
||||||
|
|
||||||
|
# For WebSocket connections, also check subprotocols since browsers can't set custom headers
|
||||||
|
if not client_version:
|
||||||
|
protocol_header = headers.get('Sec-WebSocket-Protocol')
|
||||||
|
if protocol_header:
|
||||||
|
# Parse subprotocols to find api_version
|
||||||
|
protocols = [p.strip() for p in protocol_header.split(',')]
|
||||||
|
for protocol in protocols:
|
||||||
|
if protocol.startswith('api_version.'):
|
||||||
|
client_version = protocol.split('.', 1)[1]
|
||||||
|
break
|
||||||
|
|
||||||
|
server_version = get_api_version()
|
||||||
|
|
||||||
|
if not client_version or client_version != server_version:
|
||||||
|
websocket = WebSocket(scope, receive = receive, send = send)
|
||||||
|
reason = f'Missing X-API-Version header, server version: {server_version}' if not client_version else f'Version mismatch: client={client_version}, server={server_version}'
|
||||||
|
await websocket.close(code = 1008, reason = reason)
|
||||||
|
return
|
||||||
|
|
||||||
|
await app(scope, receive, send)
|
||||||
|
|
||||||
|
|
||||||
|
def create_version_guard(app : ASGIApp) -> ASGIApp:
|
||||||
|
async def version_guard_app(scope : Scope, receive : Receive, send : Send) -> None:
|
||||||
|
await version_guard_middleware(scope, receive, send, app)
|
||||||
|
|
||||||
|
return version_guard_app
|
||||||
@@ -5,14 +5,12 @@ from facefusion.types import AppContext
|
|||||||
|
|
||||||
|
|
||||||
def detect_app_context() -> AppContext:
|
def detect_app_context() -> AppContext:
|
||||||
jobs_path = os.path.join('facefusion', 'jobs')
|
|
||||||
apis_path = os.path.join('facefusion', 'apis')
|
|
||||||
frame = sys._getframe(1)
|
frame = sys._getframe(1)
|
||||||
|
|
||||||
while frame:
|
while frame:
|
||||||
if jobs_path in frame.f_code.co_filename:
|
if os.path.join('facefusion', 'jobs') in frame.f_code.co_filename:
|
||||||
return 'cli'
|
return 'cli'
|
||||||
if apis_path in frame.f_code.co_filename:
|
if os.path.join('facefusion', 'apis') in frame.f_code.co_filename:
|
||||||
return 'api'
|
return 'api'
|
||||||
frame = frame.f_back
|
frame = frame.f_back
|
||||||
return 'cli'
|
return 'cli'
|
||||||
|
|||||||
+21
-54
@@ -1,32 +1,35 @@
|
|||||||
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.filesystem import get_file_name, is_video, resolve_file_paths
|
||||||
from facefusion.normalizer import normalize_fps, normalize_space
|
from facefusion.normalizer import normalize_fps, normalize_space
|
||||||
from facefusion.processors.core import get_processors_modules
|
from facefusion.processors.core import get_processors_modules
|
||||||
from facefusion.processors.types import ProcessorState
|
from facefusion.types import ApplyStateItem, Args
|
||||||
from facefusion.types import ApplyStateItem, Args, State
|
|
||||||
from facefusion.vision import detect_video_fps
|
from facefusion.vision import detect_video_fps
|
||||||
|
|
||||||
|
|
||||||
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
|
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
|
||||||
|
# general
|
||||||
apply_state_item('command', args.get('command'))
|
apply_state_item('command', args.get('command'))
|
||||||
apply_state_item('workflow_mode', args.get('workflow_mode'))
|
# workflow
|
||||||
|
apply_state_item('workflow', args.get('workflow'))
|
||||||
|
# paths
|
||||||
apply_state_item('temp_path', args.get('temp_path'))
|
apply_state_item('temp_path', args.get('temp_path'))
|
||||||
apply_state_item('jobs_path', args.get('jobs_path'))
|
apply_state_item('jobs_path', args.get('jobs_path'))
|
||||||
apply_state_item('source_paths', args.get('source_paths'))
|
apply_state_item('source_paths', args.get('source_paths'))
|
||||||
apply_state_item('target_path', args.get('target_path'))
|
apply_state_item('target_path', args.get('target_path'))
|
||||||
apply_state_item('output_path', args.get('output_path'))
|
apply_state_item('output_path', args.get('output_path'))
|
||||||
|
# patterns
|
||||||
apply_state_item('source_pattern', args.get('source_pattern'))
|
apply_state_item('source_pattern', args.get('source_pattern'))
|
||||||
apply_state_item('target_pattern', args.get('target_pattern'))
|
apply_state_item('target_pattern', args.get('target_pattern'))
|
||||||
apply_state_item('output_pattern', args.get('output_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_model', args.get('face_detector_model'))
|
||||||
apply_state_item('face_detector_size', args.get('face_detector_size'))
|
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_margin', normalize_space(args.get('face_detector_margin')))
|
||||||
apply_state_item('face_detector_angles', args.get('face_detector_angles'))
|
apply_state_item('face_detector_angles', args.get('face_detector_angles'))
|
||||||
apply_state_item('face_detector_score', args.get('face_detector_score'))
|
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_model', args.get('face_landmarker_model'))
|
||||||
apply_state_item('face_landmarker_score', args.get('face_landmarker_score'))
|
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_mode', args.get('face_selector_mode'))
|
||||||
apply_state_item('face_selector_order', args.get('face_selector_order'))
|
apply_state_item('face_selector_order', args.get('face_selector_order'))
|
||||||
apply_state_item('face_selector_age_start', args.get('face_selector_age_start'))
|
apply_state_item('face_selector_age_start', args.get('face_selector_age_start'))
|
||||||
@@ -36,7 +39,7 @@ def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
|
|||||||
apply_state_item('reference_face_position', args.get('reference_face_position'))
|
apply_state_item('reference_face_position', args.get('reference_face_position'))
|
||||||
apply_state_item('reference_face_distance', args.get('reference_face_distance'))
|
apply_state_item('reference_face_distance', args.get('reference_face_distance'))
|
||||||
apply_state_item('reference_frame_number', args.get('reference_frame_number'))
|
apply_state_item('reference_frame_number', args.get('reference_frame_number'))
|
||||||
apply_state_item('face_tracker_score', args.get('face_tracker_score'))
|
# face masker
|
||||||
apply_state_item('face_occluder_model', args.get('face_occluder_model'))
|
apply_state_item('face_occluder_model', args.get('face_occluder_model'))
|
||||||
apply_state_item('face_parser_model', args.get('face_parser_model'))
|
apply_state_item('face_parser_model', args.get('face_parser_model'))
|
||||||
apply_state_item('face_mask_types', args.get('face_mask_types'))
|
apply_state_item('face_mask_types', args.get('face_mask_types'))
|
||||||
@@ -44,86 +47,50 @@ def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
|
|||||||
apply_state_item('face_mask_regions', args.get('face_mask_regions'))
|
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_blur', args.get('face_mask_blur'))
|
||||||
apply_state_item('face_mask_padding', normalize_space(args.get('face_mask_padding')))
|
apply_state_item('face_mask_padding', normalize_space(args.get('face_mask_padding')))
|
||||||
|
# voice extractor
|
||||||
apply_state_item('voice_extractor_model', args.get('voice_extractor_model'))
|
apply_state_item('voice_extractor_model', args.get('voice_extractor_model'))
|
||||||
|
# frame extraction
|
||||||
apply_state_item('trim_frame_start', args.get('trim_frame_start'))
|
apply_state_item('trim_frame_start', args.get('trim_frame_start'))
|
||||||
apply_state_item('trim_frame_end', args.get('trim_frame_end'))
|
apply_state_item('trim_frame_end', args.get('trim_frame_end'))
|
||||||
apply_state_item('temp_frame_format', args.get('temp_frame_format'))
|
apply_state_item('temp_frame_format', args.get('temp_frame_format'))
|
||||||
apply_state_item('target_frame_amount', args.get('target_frame_amount'))
|
# output creation
|
||||||
apply_state_item('output_image_quality', args.get('output_image_quality'))
|
apply_state_item('output_image_quality', args.get('output_image_quality'))
|
||||||
apply_state_item('output_image_scale', args.get('output_image_scale'))
|
apply_state_item('output_image_scale', args.get('output_image_scale'))
|
||||||
apply_state_item('output_audio_encoder', args.get('output_audio_encoder'))
|
apply_state_item('output_audio_encoder', args.get('output_audio_encoder'))
|
||||||
apply_state_item('output_audio_quality', args.get('output_audio_quality'))
|
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_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_encoder', args.get('output_video_encoder'))
|
||||||
apply_state_item('output_video_preset', args.get('output_video_preset'))
|
apply_state_item('output_video_preset', args.get('output_video_preset'))
|
||||||
apply_state_item('output_video_quality', args.get('output_video_quality'))
|
apply_state_item('output_video_quality', args.get('output_video_quality'))
|
||||||
apply_state_item('output_video_scale', args.get('output_video_scale'))
|
apply_state_item('output_video_scale', args.get('output_video_scale'))
|
||||||
|
|
||||||
if args.get('output_video_fps') or is_video(args.get('target_path')):
|
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'))
|
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)
|
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') ]
|
available_processors = [ get_file_name(file_path) for file_path in resolve_file_paths('facefusion/processors/modules') ]
|
||||||
apply_state_item('processors', args.get('processors'))
|
apply_state_item('processors', args.get('processors'))
|
||||||
|
|
||||||
for processor_module in get_processors_modules(available_processors):
|
for processor_module in get_processors_modules(available_processors):
|
||||||
processor_module.apply_args(args, apply_state_item)
|
processor_module.apply_args(args, apply_state_item)
|
||||||
|
# execution
|
||||||
apply_state_item('execution_device_ids', args.get('execution_device_ids'))
|
apply_state_item('execution_device_ids', args.get('execution_device_ids'))
|
||||||
apply_state_item('execution_providers', args.get('execution_providers'))
|
apply_state_item('execution_providers', args.get('execution_providers'))
|
||||||
apply_state_item('execution_thread_count', args.get('execution_thread_count'))
|
apply_state_item('execution_thread_count', args.get('execution_thread_count'))
|
||||||
|
# download
|
||||||
apply_state_item('download_providers', args.get('download_providers'))
|
apply_state_item('download_providers', args.get('download_providers'))
|
||||||
apply_state_item('download_scope', args.get('download_scope'))
|
apply_state_item('download_scope', args.get('download_scope'))
|
||||||
|
# benchmark
|
||||||
apply_state_item('benchmark_mode', args.get('benchmark_mode'))
|
apply_state_item('benchmark_mode', args.get('benchmark_mode'))
|
||||||
apply_state_item('benchmark_resolutions', args.get('benchmark_resolutions'))
|
apply_state_item('benchmark_resolutions', args.get('benchmark_resolutions'))
|
||||||
apply_state_item('benchmark_cycle_count', args.get('benchmark_cycle_count'))
|
apply_state_item('benchmark_cycle_count', args.get('benchmark_cycle_count'))
|
||||||
|
# api
|
||||||
apply_state_item('api_host', args.get('api_host'))
|
apply_state_item('api_host', args.get('api_host'))
|
||||||
apply_state_item('api_port', args.get('api_port'))
|
apply_state_item('api_port', args.get('api_port'))
|
||||||
apply_state_item('api_security_strategy', args.get('api_security_strategy'))
|
# memory
|
||||||
apply_state_item('video_memory_strategy', args.get('video_memory_strategy'))
|
apply_state_item('video_memory_strategy', args.get('video_memory_strategy'))
|
||||||
|
# misc
|
||||||
apply_state_item('log_level', args.get('log_level'))
|
apply_state_item('log_level', args.get('log_level'))
|
||||||
apply_state_item('halt_on_error', args.get('halt_on_error'))
|
apply_state_item('halt_on_error', args.get('halt_on_error'))
|
||||||
|
# jobs
|
||||||
apply_state_item('job_id', args.get('job_id'))
|
apply_state_item('job_id', args.get('job_id'))
|
||||||
apply_state_item('job_status', args.get('job_status'))
|
apply_state_item('job_status', args.get('job_status'))
|
||||||
apply_state_item('step_index', args.get('step_index'))
|
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
|
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
from typing import List
|
||||||
|
|
||||||
|
from facefusion.types import Args, ArgsStore, Scope
|
||||||
|
|
||||||
|
|
||||||
|
ARGS_STORE : ArgsStore =\
|
||||||
|
{
|
||||||
|
'api': [],
|
||||||
|
'cli': [],
|
||||||
|
'sys': []
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def get_api_args() -> List[str]:
|
||||||
|
return ARGS_STORE.get('api')
|
||||||
|
|
||||||
|
|
||||||
|
def get_sys_args() -> List[str]:
|
||||||
|
return ARGS_STORE.get('sys')
|
||||||
|
|
||||||
|
|
||||||
|
def get_cli_args() -> List[str]:
|
||||||
|
return ARGS_STORE.get('cli')
|
||||||
|
|
||||||
|
|
||||||
|
def register_args(keys : List[str], scopes : List[Scope]) -> None:
|
||||||
|
for key in keys:
|
||||||
|
for scope in scopes:
|
||||||
|
if scope == 'api':
|
||||||
|
ARGS_STORE['api'].append(key)
|
||||||
|
if scope == 'cli':
|
||||||
|
ARGS_STORE['cli'].append(key)
|
||||||
|
if scope == 'sys':
|
||||||
|
ARGS_STORE['sys'].append(key)
|
||||||
|
|
||||||
|
|
||||||
|
def filter_api_args(args : Args) -> Args:
|
||||||
|
api_args =\
|
||||||
|
{
|
||||||
|
key: args.get(key) for key in args if key in get_api_args() #type:ignore[literal-required]
|
||||||
|
}
|
||||||
|
return api_args
|
||||||
|
|
||||||
|
|
||||||
|
def filter_sys_args(args : Args) -> Args:
|
||||||
|
sys_args =\
|
||||||
|
{
|
||||||
|
key: args.get(key) for key in args if key in get_sys_args() #type:ignore[literal-required]
|
||||||
|
}
|
||||||
|
return sys_args
|
||||||
|
|
||||||
|
|
||||||
|
def filter_cli_args(args : Args) -> Args:
|
||||||
|
cli_args =\
|
||||||
|
{
|
||||||
|
key: args.get(key) for key in args if key in get_cli_args() #type:ignore[literal-required]
|
||||||
|
}
|
||||||
|
return cli_args
|
||||||
|
|
||||||
|
|
||||||
|
def filter_step_args(args : Args) -> Args:
|
||||||
|
step_args =\
|
||||||
|
{
|
||||||
|
key: args.get(key) for key in args if key in get_cli_args() and key not in get_sys_args() #type:ignore[literal-required]
|
||||||
|
}
|
||||||
|
return step_args
|
||||||
@@ -0,0 +1,111 @@
|
|||||||
|
import os
|
||||||
|
import uuid
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
from typing import Any, Dict, List, Optional, TypeAlias
|
||||||
|
|
||||||
|
from facefusion import filesystem, state_manager
|
||||||
|
from facefusion.session_context import get_session_id
|
||||||
|
|
||||||
|
AssetRegistry : TypeAlias = Dict[str, Dict[str, Any]]
|
||||||
|
|
||||||
|
|
||||||
|
def get_asset_registry() -> AssetRegistry:
|
||||||
|
registry = state_manager.get_item('asset_registry')
|
||||||
|
if not registry:
|
||||||
|
registry = {}
|
||||||
|
state_manager.set_item('asset_registry', registry)
|
||||||
|
return registry
|
||||||
|
|
||||||
|
|
||||||
|
def register(asset_type : str, file_path : str, filename : str = None, metadata : Optional[Dict[str, Any]] = None) -> str:
|
||||||
|
if asset_type not in ['source', 'target', 'output']:
|
||||||
|
raise ValueError(f"Invalid asset_type: {asset_type}. Must be 'source', 'target', or 'output'")
|
||||||
|
|
||||||
|
asset_id = str(uuid.uuid4())
|
||||||
|
session_id = get_session_id()
|
||||||
|
|
||||||
|
if not session_id:
|
||||||
|
raise ValueError("No active session - cannot register asset without session_id")
|
||||||
|
|
||||||
|
if not filename:
|
||||||
|
filename = os.path.basename(file_path)
|
||||||
|
|
||||||
|
file_size = os.path.getsize(file_path)
|
||||||
|
file_format = filesystem.get_file_format(file_path)
|
||||||
|
media_type = None
|
||||||
|
|
||||||
|
if filesystem.is_image(file_path):
|
||||||
|
media_type = 'image'
|
||||||
|
if filesystem.is_video(file_path):
|
||||||
|
media_type = 'video'
|
||||||
|
if filesystem.is_audio(file_path):
|
||||||
|
media_type = 'audio'
|
||||||
|
|
||||||
|
asset_data =\
|
||||||
|
{
|
||||||
|
'id': asset_id,
|
||||||
|
'session_id': session_id,
|
||||||
|
'type': asset_type,
|
||||||
|
'media_type': media_type,
|
||||||
|
'format': file_format,
|
||||||
|
'path': file_path,
|
||||||
|
'filename': filename,
|
||||||
|
'size': file_size,
|
||||||
|
'created_at': datetime.now(timezone.utc).isoformat()
|
||||||
|
}
|
||||||
|
|
||||||
|
if metadata:
|
||||||
|
asset_data['metadata'] = metadata
|
||||||
|
|
||||||
|
registry = get_asset_registry()
|
||||||
|
registry[asset_id] = asset_data
|
||||||
|
state_manager.set_item('asset_registry', registry)
|
||||||
|
|
||||||
|
return asset_id
|
||||||
|
|
||||||
|
|
||||||
|
def get_asset(asset_id : str) -> Optional[Dict[str, Any]]:
|
||||||
|
registry = get_asset_registry()
|
||||||
|
return registry.get(asset_id)
|
||||||
|
|
||||||
|
|
||||||
|
def list_assets(asset_type : Optional[str] = None) -> List[Dict[str, Any]]:
|
||||||
|
registry = get_asset_registry()
|
||||||
|
session_id = get_session_id()
|
||||||
|
|
||||||
|
assets = list(registry.values())
|
||||||
|
|
||||||
|
if session_id:
|
||||||
|
assets = [a for a in assets if a.get('session_id') == session_id]
|
||||||
|
|
||||||
|
if asset_type:
|
||||||
|
if asset_type not in ['source', 'target', 'output']:
|
||||||
|
raise ValueError(f"Invalid asset_type: {asset_type}")
|
||||||
|
assets = [a for a in assets if a.get('type') == asset_type]
|
||||||
|
|
||||||
|
return assets
|
||||||
|
|
||||||
|
|
||||||
|
def delete_asset(asset_id : str) -> bool:
|
||||||
|
registry = get_asset_registry()
|
||||||
|
asset = registry.get(asset_id)
|
||||||
|
|
||||||
|
if not asset:
|
||||||
|
return False
|
||||||
|
|
||||||
|
file_path = asset.get('path')
|
||||||
|
if file_path and os.path.exists(file_path):
|
||||||
|
os.remove(file_path)
|
||||||
|
|
||||||
|
del registry[asset_id]
|
||||||
|
state_manager.set_item('asset_registry', registry)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def cleanup_session_assets(session_id : str) -> None:
|
||||||
|
registry = get_asset_registry()
|
||||||
|
assets_to_delete = [aid for aid, asset in registry.items() if asset.get('session_id') == session_id]
|
||||||
|
|
||||||
|
for asset_id in assets_to_delete:
|
||||||
|
delete_asset(asset_id)
|
||||||
@@ -9,7 +9,7 @@ import facefusion.choices
|
|||||||
from facefusion import content_analyser, core, state_manager
|
from facefusion import content_analyser, core, state_manager
|
||||||
from facefusion.cli_helper import render_table
|
from facefusion.cli_helper import render_table
|
||||||
from facefusion.download import conditional_download, resolve_download_url
|
from facefusion.download import conditional_download, resolve_download_url
|
||||||
from facefusion.face_store import clear_faces
|
from facefusion.face_store import clear_static_faces
|
||||||
from facefusion.filesystem import get_file_extension
|
from facefusion.filesystem import get_file_extension
|
||||||
from facefusion.types import BenchmarkCycleSet
|
from facefusion.types import BenchmarkCycleSet
|
||||||
from facefusion.vision import count_video_frame_total, detect_video_fps
|
from facefusion.vision import count_video_frame_total, detect_video_fps
|
||||||
@@ -64,7 +64,7 @@ def cycle(cycle_count : int) -> BenchmarkCycleSet:
|
|||||||
if state_manager.get_item('benchmark_mode') == 'cold':
|
if state_manager.get_item('benchmark_mode') == 'cold':
|
||||||
content_analyser.analyse_image.cache_clear()
|
content_analyser.analyse_image.cache_clear()
|
||||||
content_analyser.analyse_video.cache_clear()
|
content_analyser.analyse_video.cache_clear()
|
||||||
clear_faces()
|
clear_static_faces()
|
||||||
|
|
||||||
start_time = perf_counter()
|
start_time = perf_counter()
|
||||||
core.conditional_process()
|
core.conditional_process()
|
||||||
@@ -89,7 +89,7 @@ def cycle(cycle_count : int) -> BenchmarkCycleSet:
|
|||||||
|
|
||||||
def suggest_output_path(target_path : str) -> str:
|
def suggest_output_path(target_path : str) -> str:
|
||||||
target_file_extension = get_file_extension(target_path)
|
target_file_extension = get_file_extension(target_path)
|
||||||
return os.path.join(tempfile.gettempdir(), hashlib.sha1(target_path.encode()).hexdigest() + target_file_extension)
|
return os.path.join(tempfile.gettempdir(), hashlib.sha1().hexdigest()[:8] + target_file_extension)
|
||||||
|
|
||||||
|
|
||||||
def render() -> None:
|
def render() -> None:
|
||||||
|
|||||||
@@ -43,9 +43,9 @@ def detect_local_camera_ids(id_start : int, id_end : int) -> List[int]:
|
|||||||
local_camera_ids = []
|
local_camera_ids = []
|
||||||
|
|
||||||
for camera_id in range(id_start, id_end):
|
for camera_id in range(id_start, id_end):
|
||||||
cv2.utils.logging.setLogLevel(0)
|
cv2.setLogLevel(0)
|
||||||
camera_capture = get_local_camera_capture(camera_id)
|
camera_capture = get_local_camera_capture(camera_id)
|
||||||
cv2.utils.logging.setLogLevel(3)
|
cv2.setLogLevel(3)
|
||||||
|
|
||||||
if camera_capture and camera_capture.isOpened():
|
if camera_capture and camera_capture.isOpened():
|
||||||
local_camera_ids.append(camera_id)
|
local_camera_ids.append(camera_id)
|
||||||
|
|||||||
@@ -1,54 +0,0 @@
|
|||||||
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
|
|
||||||
+60
-62
@@ -1,8 +1,8 @@
|
|||||||
import logging
|
import logging
|
||||||
from typing import List, Sequence, get_args
|
from typing import List, Sequence
|
||||||
|
|
||||||
from facefusion.common_helper import create_float_range, create_int_range
|
from facefusion.common_helper import create_float_range, create_int_range
|
||||||
from facefusion.types import Angle, 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
|
from facefusion.types import Angle, AudioEncoder, AudioFormat, AudioTypeSet, BenchmarkMode, BenchmarkResolution, BenchmarkSet, DownloadProvider, DownloadProviderSet, DownloadScope, EncoderSet, ExecutionProvider, ExecutionProviderSet, FaceDetectorModel, FaceDetectorSet, FaceLandmarkerModel, FaceMaskArea, FaceMaskAreaSet, FaceMaskRegion, FaceMaskRegionSet, FaceMaskType, FaceOccluderModel, FaceParserModel, FaceSelectorMode, FaceSelectorOrder, Gender, ImageFormat, ImageTypeSet, JobStatus, LogLevel, LogLevelSet, Race, Score, TempFrameFormat, VideoEncoder, VideoFormat, VideoMemoryStrategy, VideoPreset, VideoTypeSet, VoiceExtractorModel, WorkFlow
|
||||||
|
|
||||||
face_detector_set : FaceDetectorSet =\
|
face_detector_set : FaceDetectorSet =\
|
||||||
{
|
{
|
||||||
@@ -12,17 +12,15 @@ face_detector_set : FaceDetectorSet =\
|
|||||||
'yolo_face': [ '640x640' ],
|
'yolo_face': [ '640x640' ],
|
||||||
'yunet': [ '640x640' ]
|
'yunet': [ '640x640' ]
|
||||||
}
|
}
|
||||||
face_detector_models : List[FaceDetectorModel] = list(get_args(FaceDetectorModel))
|
face_detector_models : List[FaceDetectorModel] = list(face_detector_set.keys())
|
||||||
face_landmarker_models : List[FaceLandmarkerModel] = list(get_args(FaceLandmarkerModel))
|
face_landmarker_models : List[FaceLandmarkerModel] = [ 'many', '2dfan4', 'peppa_wutz' ]
|
||||||
face_selector_modes : List[FaceSelectorMode] = list(get_args(FaceSelectorMode))
|
face_selector_modes : List[FaceSelectorMode] = [ 'many', 'one', 'reference' ]
|
||||||
face_selector_orders : List[FaceSelectorOrder] = list(get_args(FaceSelectorOrder))
|
face_selector_orders : List[FaceSelectorOrder] = [ 'left-right', 'right-left', 'top-bottom', 'bottom-top', 'small-large', 'large-small', 'best-worst', 'worst-best' ]
|
||||||
genders : List[Gender] = list(get_args(Gender))
|
face_selector_genders : List[Gender] = [ 'female', 'male' ]
|
||||||
races : List[Race] = list(get_args(Race))
|
face_selector_races : List[Race] = [ 'white', 'black', 'latino', 'asian', 'indian', 'arabic' ]
|
||||||
face_selector_genders : List[FaceSelectorGender] = list(get_args(FaceSelectorGender))
|
face_occluder_models : List[FaceOccluderModel] = [ 'many', 'xseg_1', 'xseg_2', 'xseg_3' ]
|
||||||
face_selector_races : List[FaceSelectorRace] = list(get_args(FaceSelectorRace))
|
face_parser_models : List[FaceParserModel] = [ 'bisenet_resnet_18', 'bisenet_resnet_34' ]
|
||||||
face_occluder_models : List[FaceOccluderModel] = list(get_args(FaceOccluderModel))
|
face_mask_types : List[FaceMaskType] = [ 'box', 'occlusion', 'area', 'region' ]
|
||||||
face_parser_models : List[FaceParserModel] = list(get_args(FaceParserModel))
|
|
||||||
face_mask_types : List[FaceMaskType] = list(get_args(FaceMaskType))
|
|
||||||
face_mask_area_set : FaceMaskAreaSet =\
|
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 ],
|
'upper-face': [ 0, 1, 2, 31, 32, 33, 34, 35, 14, 15, 16, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17 ],
|
||||||
@@ -42,53 +40,57 @@ face_mask_region_set : FaceMaskRegionSet =\
|
|||||||
'upper-lip': 12,
|
'upper-lip': 12,
|
||||||
'lower-lip': 13
|
'lower-lip': 13
|
||||||
}
|
}
|
||||||
face_mask_areas : List[FaceMaskArea] = list(get_args(FaceMaskArea))
|
face_mask_areas : List[FaceMaskArea] = list(face_mask_area_set.keys())
|
||||||
face_mask_regions : List[FaceMaskRegion] = list(get_args(FaceMaskRegion))
|
face_mask_regions : List[FaceMaskRegion] = list(face_mask_region_set.keys())
|
||||||
|
|
||||||
voice_extractor_models : List[VoiceExtractorModel] = list(get_args(VoiceExtractorModel))
|
voice_extractor_models : List[VoiceExtractorModel] = [ 'kim_vocal_1', 'kim_vocal_2', 'uvr_mdxnet' ]
|
||||||
|
|
||||||
workflow_modes : List[WorkflowMode] = [ 'auto', 'audio-to-image:frames', 'audio-to-image:video', 'image-to-image', 'image-to-video', 'image-to-video:frames' ]
|
workflows : List[WorkFlow] = [ 'auto', 'audio-to-image', 'image-to-image', 'image-to-video' ]
|
||||||
|
|
||||||
audio_set : AudioSet =\
|
audio_type_set : AudioTypeSet =\
|
||||||
{
|
{
|
||||||
'flac': 'flac',
|
'flac': 'audio/flac',
|
||||||
'm4a': 'aac',
|
'm4a': 'audio/mp4',
|
||||||
'mp3': 'libmp3lame',
|
'mp3': 'audio/mpeg',
|
||||||
'ogg': 'flac',
|
'ogg': 'audio/ogg',
|
||||||
'opus': 'libopus',
|
'opus': 'audio/opus',
|
||||||
'wav': 'pcm_s16le'
|
'wav': 'audio/x-wav'
|
||||||
}
|
}
|
||||||
image_set : ImageSet =\
|
image_type_set : ImageTypeSet =\
|
||||||
{
|
{
|
||||||
'bmp': 'bmp',
|
'bmp': 'image/bmp',
|
||||||
'jpeg': 'mjpeg',
|
'jpeg': 'image/jpeg',
|
||||||
'png': 'png',
|
'png': 'image/png',
|
||||||
'tiff': 'tiff',
|
'tiff': 'image/tiff',
|
||||||
'webp': 'libwebp'
|
'webp': 'image/webp'
|
||||||
}
|
}
|
||||||
video_set : VideoSet =\
|
video_type_set : VideoTypeSet =\
|
||||||
{
|
{
|
||||||
'avi': 'mpeg4',
|
'avi': 'video/x-msvideo',
|
||||||
'm4v': 'libx264',
|
'm4v': 'video/mp4',
|
||||||
'mkv': 'libx264',
|
'mkv': 'video/x-matroska',
|
||||||
'mov': 'libx264',
|
'mp4': 'video/mp4',
|
||||||
'mp4': 'libx264',
|
'mpeg': 'video/mpeg',
|
||||||
'mpeg': 'mpeg1video',
|
'mov': 'video/quicktime',
|
||||||
'mxf': 'mpeg2video',
|
'mxf': 'application/mxf',
|
||||||
'webm': 'libvpx-vp9',
|
'webm': 'video/webm',
|
||||||
'wmv': 'msmpeg4'
|
'wmv': 'video/x-ms-wmv'
|
||||||
}
|
}
|
||||||
audio_formats : List[AudioFormat] = list(get_args(AudioFormat))
|
audio_formats : List[AudioFormat] = list(audio_type_set.keys())
|
||||||
image_formats : List[ImageFormat] = list(get_args(ImageFormat))
|
image_formats : List[ImageFormat] = list(image_type_set.keys())
|
||||||
video_formats : List[VideoFormat] = list(get_args(VideoFormat))
|
video_formats : List[VideoFormat] = list(video_type_set.keys())
|
||||||
temp_frame_formats : List[TempFrameFormat] = list(get_args(TempFrameFormat))
|
temp_frame_formats : List[TempFrameFormat] = [ 'bmp', 'jpeg', 'png', 'tiff' ]
|
||||||
|
|
||||||
audio_encoders : List[AudioEncoder] = list(get_args(AudioEncoder))
|
output_encoder_set : EncoderSet =\
|
||||||
image_encoders : List[ImageEncoder] = list(get_args(ImageEncoder))
|
{
|
||||||
video_encoders : List[VideoEncoder] = list(get_args(VideoEncoder))
|
'audio': [ 'flac', 'aac', 'libmp3lame', 'libopus', 'libvorbis', 'pcm_s16le', 'pcm_s32le' ],
|
||||||
video_presets : List[VideoPreset] = list(get_args(VideoPreset))
|
'video': [ 'libx264', 'libx264rgb', 'libx265', 'libvpx-vp9', 'h264_nvenc', 'hevc_nvenc', 'h264_amf', 'hevc_amf', 'h264_qsv', 'hevc_qsv', 'h264_videotoolbox', 'hevc_videotoolbox', 'rawvideo' ]
|
||||||
|
}
|
||||||
|
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' ]
|
||||||
|
|
||||||
benchmark_modes : List[BenchmarkMode] = list(get_args(BenchmarkMode))
|
benchmark_modes : List[BenchmarkMode] = [ 'warm', 'cold' ]
|
||||||
benchmark_set : BenchmarkSet =\
|
benchmark_set : BenchmarkSet =\
|
||||||
{
|
{
|
||||||
'240p': '.assets/examples/target-240p.mp4',
|
'240p': '.assets/examples/target-240p.mp4',
|
||||||
@@ -99,21 +101,20 @@ benchmark_set : BenchmarkSet =\
|
|||||||
'1440p': '.assets/examples/target-1440p.mp4',
|
'1440p': '.assets/examples/target-1440p.mp4',
|
||||||
'2160p': '.assets/examples/target-2160p.mp4'
|
'2160p': '.assets/examples/target-2160p.mp4'
|
||||||
}
|
}
|
||||||
benchmark_resolutions : List[BenchmarkResolution] = list(get_args(BenchmarkResolution))
|
benchmark_resolutions : List[BenchmarkResolution] = list(benchmark_set.keys())
|
||||||
|
|
||||||
execution_provider_set : ExecutionProviderSet =\
|
execution_provider_set : ExecutionProviderSet =\
|
||||||
{
|
{
|
||||||
'cuda': 'CUDAExecutionProvider',
|
'cuda': 'CUDAExecutionProvider',
|
||||||
'tensorrt': 'TensorrtExecutionProvider',
|
'tensorrt': 'TensorrtExecutionProvider',
|
||||||
|
'directml': 'DmlExecutionProvider',
|
||||||
'rocm': 'ROCMExecutionProvider',
|
'rocm': 'ROCMExecutionProvider',
|
||||||
'migraphx': 'MIGraphXExecutionProvider',
|
'migraphx': 'MIGraphXExecutionProvider',
|
||||||
'coreml': 'CoreMLExecutionProvider',
|
|
||||||
'openvino': 'OpenVINOExecutionProvider',
|
'openvino': 'OpenVINOExecutionProvider',
|
||||||
'qnn': 'QNNExecutionProvider',
|
'coreml': 'CoreMLExecutionProvider',
|
||||||
'directml': 'DmlExecutionProvider',
|
|
||||||
'cpu': 'CPUExecutionProvider'
|
'cpu': 'CPUExecutionProvider'
|
||||||
}
|
}
|
||||||
execution_providers : List[ExecutionProvider] = list(get_args(ExecutionProvider))
|
execution_providers : List[ExecutionProvider] = list(execution_provider_set.keys())
|
||||||
download_provider_set : DownloadProviderSet =\
|
download_provider_set : DownloadProviderSet =\
|
||||||
{
|
{
|
||||||
'github':
|
'github':
|
||||||
@@ -134,11 +135,10 @@ download_provider_set : DownloadProviderSet =\
|
|||||||
'path': '/facefusion/{base_name}/resolve/main/{file_name}'
|
'path': '/facefusion/{base_name}/resolve/main/{file_name}'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
download_providers : List[DownloadProvider] = list(get_args(DownloadProvider))
|
download_providers : List[DownloadProvider] = list(download_provider_set.keys())
|
||||||
download_scopes : List[DownloadScope] = list(get_args(DownloadScope))
|
download_scopes : List[DownloadScope] = [ 'lite', 'full' ]
|
||||||
|
|
||||||
video_memory_strategies : List[VideoMemoryStrategy] = list(get_args(VideoMemoryStrategy))
|
video_memory_strategies : List[VideoMemoryStrategy] = [ 'strict', 'moderate', 'tolerant' ]
|
||||||
api_security_strategies : List[ApiSecurityStrategy] = list(get_args(ApiSecurityStrategy))
|
|
||||||
|
|
||||||
log_level_set : LogLevelSet =\
|
log_level_set : LogLevelSet =\
|
||||||
{
|
{
|
||||||
@@ -147,9 +147,9 @@ log_level_set : LogLevelSet =\
|
|||||||
'info': logging.INFO,
|
'info': logging.INFO,
|
||||||
'debug': logging.DEBUG
|
'debug': logging.DEBUG
|
||||||
}
|
}
|
||||||
log_levels : List[LogLevel] = list(get_args(LogLevel))
|
log_levels : List[LogLevel] = list(log_level_set.keys())
|
||||||
|
|
||||||
job_statuses : List[JobStatus] = list(get_args(JobStatus))
|
job_statuses : List[JobStatus] = [ 'drafted', 'queued', 'completed', 'failed' ]
|
||||||
|
|
||||||
benchmark_cycle_count_range : Sequence[int] = create_int_range(1, 10, 1)
|
benchmark_cycle_count_range : Sequence[int] = create_int_range(1, 10, 1)
|
||||||
execution_thread_count_range : Sequence[int] = create_int_range(1, 32, 1)
|
execution_thread_count_range : Sequence[int] = create_int_range(1, 32, 1)
|
||||||
@@ -161,8 +161,6 @@ face_mask_blur_range : Sequence[float] = create_float_range(0.0, 1.0, 0.05)
|
|||||||
face_mask_padding_range : Sequence[int] = create_int_range(0, 100, 1)
|
face_mask_padding_range : Sequence[int] = create_int_range(0, 100, 1)
|
||||||
face_selector_age_range : Sequence[int] = create_int_range(0, 100, 1)
|
face_selector_age_range : Sequence[int] = create_int_range(0, 100, 1)
|
||||||
reference_face_distance_range : Sequence[float] = create_float_range(0.0, 1.0, 0.05)
|
reference_face_distance_range : Sequence[float] = create_float_range(0.0, 1.0, 0.05)
|
||||||
face_tracker_score_range : Sequence[Score] = create_float_range(0.0, 0.5, 0.05)
|
|
||||||
target_frame_amount_range : Sequence[int] = create_int_range(0, 10, 1)
|
|
||||||
output_image_quality_range : Sequence[int] = create_int_range(0, 100, 1)
|
output_image_quality_range : Sequence[int] = create_int_range(0, 100, 1)
|
||||||
output_image_scale_range : Sequence[float] = create_float_range(0.25, 8.0, 0.25)
|
output_image_scale_range : Sequence[float] = create_float_range(0.25, 8.0, 0.25)
|
||||||
output_audio_quality_range : Sequence[int] = create_int_range(0, 100, 1)
|
output_audio_quality_range : Sequence[int] = create_int_range(0, 100, 1)
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import struct
|
|||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from facefusion.libraries import aom as aom_module
|
from facefusion.libraries import aom as aom_module
|
||||||
from facefusion.types import AomDecoder, Buffer, BufferPack
|
from facefusion.types import AomDecoder, AomPointer
|
||||||
|
|
||||||
|
|
||||||
def create(thread_count : int) -> Optional[AomDecoder]:
|
def create(thread_count : int) -> Optional[AomDecoder]:
|
||||||
@@ -23,7 +23,7 @@ def create(thread_count : int) -> Optional[AomDecoder]:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def decode(aom_decoder : AomDecoder, input_buffer : Buffer) -> Optional[BufferPack]:
|
def decode(aom_decoder : AomDecoder, input_buffer : bytes) -> Optional[AomPointer]:
|
||||||
aom_library = aom_module.create_static_library()
|
aom_library = aom_module.create_static_library()
|
||||||
|
|
||||||
if aom_library and input_buffer:
|
if aom_library and input_buffer:
|
||||||
@@ -37,7 +37,7 @@ def decode(aom_decoder : AomDecoder, input_buffer : Buffer) -> Optional[BufferPa
|
|||||||
frame_width = ctypes.c_uint.from_address(address + 28).value & ~1
|
frame_width = ctypes.c_uint.from_address(address + 28).value & ~1
|
||||||
frame_height = ctypes.c_uint.from_address(address + 32).value & ~1
|
frame_height = ctypes.c_uint.from_address(address + 32).value & ~1
|
||||||
|
|
||||||
return BufferPack(
|
return AomPointer(
|
||||||
buffer = collect(address, frame_width, frame_height),
|
buffer = collect(address, frame_width, frame_height),
|
||||||
resolution = (frame_width, frame_height)
|
resolution = (frame_width, frame_height)
|
||||||
)
|
)
|
||||||
@@ -45,7 +45,7 @@ def decode(aom_decoder : AomDecoder, input_buffer : Buffer) -> Optional[BufferPa
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def collect(address : int, frame_width : int, frame_height : int) -> Buffer:
|
def collect(address : int, frame_width : int, frame_height : int) -> bytes:
|
||||||
output_parts = []
|
output_parts = []
|
||||||
|
|
||||||
for index in range(3):
|
for index in range(3):
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import struct
|
|||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from facefusion.libraries import aom as aom_module
|
from facefusion.libraries import aom as aom_module
|
||||||
from facefusion.types import AomEncoder, BitRate, Buffer, Resolution
|
from facefusion.types import AomEncoder, BitRate, Resolution
|
||||||
|
|
||||||
|
|
||||||
def create(frame_resolution : Resolution, bitrate : BitRate, thread_count : int, cpu_count : int) -> Optional[AomEncoder]:
|
def create(frame_resolution : Resolution, bitrate : BitRate, thread_count : int, cpu_count : int) -> Optional[AomEncoder]:
|
||||||
@@ -34,7 +34,7 @@ def create(frame_resolution : Resolution, bitrate : BitRate, thread_count : int,
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def encode(aom_encoder : AomEncoder, input_buffer : Buffer, frame_resolution : Resolution, frame_index : int) -> Buffer:
|
def encode(aom_encoder : AomEncoder, input_buffer : bytes, frame_resolution : Resolution, frame_index : int) -> bytes:
|
||||||
aom_library = aom_module.create_static_library()
|
aom_library = aom_module.create_static_library()
|
||||||
output_buffer = bytes()
|
output_buffer = bytes()
|
||||||
|
|
||||||
@@ -48,7 +48,7 @@ def encode(aom_encoder : AomEncoder, input_buffer : Buffer, frame_resolution : R
|
|||||||
return output_buffer
|
return output_buffer
|
||||||
|
|
||||||
|
|
||||||
def collect(aom_encoder : AomEncoder) -> Buffer:
|
def collect(aom_encoder : AomEncoder) -> bytes:
|
||||||
aom_library = aom_module.create_static_library()
|
aom_library = aom_module.create_static_library()
|
||||||
output_parts = []
|
output_parts = []
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import ctypes
|
|||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from facefusion.libraries import opus as opus_module
|
from facefusion.libraries import opus as opus_module
|
||||||
from facefusion.types import Buffer, OpusDecoder
|
from facefusion.types import OpusDecoder
|
||||||
|
|
||||||
|
|
||||||
def create(sample_rate : int, channel_total : int) -> Optional[OpusDecoder]:
|
def create(sample_rate : int, channel_total : int) -> Optional[OpusDecoder]:
|
||||||
@@ -14,19 +14,17 @@ def create(sample_rate : int, channel_total : int) -> Optional[OpusDecoder]:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def decode(opus_decoder : OpusDecoder, input_buffer : Buffer, channel_total : int) -> Buffer:
|
def decode(opus_decoder : OpusDecoder, input_buffer : bytes, frame_size : int, channel_total : int) -> bytes:
|
||||||
opus_library = opus_module.create_static_library()
|
opus_library = opus_module.create_static_library()
|
||||||
output_buffer = bytes()
|
output_buffer = bytes()
|
||||||
|
|
||||||
if opus_library:
|
if opus_library:
|
||||||
input_total = len(input_buffer)
|
input_total = len(input_buffer)
|
||||||
sample_size = ctypes.sizeof(ctypes.c_float)
|
decode_buffer = (ctypes.c_float * (frame_size * channel_total))()
|
||||||
sample_total = opus_library.opus_decoder_get_nb_samples(opus_decoder, input_buffer, input_total)
|
decode_length = opus_library.opus_decode_float(opus_decoder, input_buffer, input_total, decode_buffer, frame_size, 0)
|
||||||
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:
|
if decode_length:
|
||||||
output_buffer = ctypes.string_at(ctypes.addressof(sample_buffer), output_total * channel_total * sample_size)
|
output_buffer = ctypes.string_at(ctypes.addressof(decode_buffer), decode_length * channel_total * ctypes.sizeof(ctypes.c_float))
|
||||||
|
|
||||||
return output_buffer
|
return output_buffer
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import ctypes
|
|||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from facefusion.libraries import opus as opus_module
|
from facefusion.libraries import opus as opus_module
|
||||||
from facefusion.types import Buffer, OpusEncoder
|
from facefusion.types import OpusEncoder
|
||||||
|
|
||||||
|
|
||||||
def create(sample_rate : int, channel_total : int) -> Optional[OpusEncoder]:
|
def create(sample_rate : int, channel_total : int) -> Optional[OpusEncoder]:
|
||||||
@@ -14,19 +14,17 @@ def create(sample_rate : int, channel_total : int) -> Optional[OpusEncoder]:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def encode(opus_encoder : OpusEncoder, input_buffer : Buffer, channel_total : int) -> Buffer:
|
def encode(opus_encoder : OpusEncoder, input_buffer : bytes, frame_size : int) -> bytes:
|
||||||
opus_library = opus_module.create_static_library()
|
opus_library = opus_module.create_static_library()
|
||||||
output_buffer = bytes()
|
output_buffer = bytes()
|
||||||
|
|
||||||
if opus_library:
|
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)
|
temp_buffer = ctypes.create_string_buffer(2048)
|
||||||
output_total = opus_library.opus_encode_float(opus_encoder, sample_buffer, sample_total, temp_buffer, 2048)
|
encode_buffer = ctypes.cast(ctypes.create_string_buffer(input_buffer), ctypes.POINTER(ctypes.c_float))
|
||||||
|
encode_length = opus_library.opus_encode_float(opus_encoder, encode_buffer, frame_size, temp_buffer, 2048)
|
||||||
|
|
||||||
if output_total:
|
if encode_length:
|
||||||
output_buffer = temp_buffer.raw[:output_total]
|
output_buffer = temp_buffer.raw[:encode_length]
|
||||||
|
|
||||||
return output_buffer
|
return output_buffer
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import struct
|
|||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from facefusion.libraries import vpx as vpx_module
|
from facefusion.libraries import vpx as vpx_module
|
||||||
from facefusion.types import Buffer, BufferPack, VpxDecoder, VxpVideoCodec
|
from facefusion.types import VpxDecoder, VpxPointer, VxpVideoCodec
|
||||||
|
|
||||||
|
|
||||||
def create(video_codec : VxpVideoCodec, thread_count : int) -> Optional[VpxDecoder]:
|
def create(video_codec : VxpVideoCodec, thread_count : int) -> Optional[VpxDecoder]:
|
||||||
@@ -27,7 +27,7 @@ def create(video_codec : VxpVideoCodec, thread_count : int) -> Optional[VpxDecod
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def decode(vpx_decoder : VpxDecoder, input_buffer : Buffer) -> Optional[BufferPack]:
|
def decode(vpx_decoder : VpxDecoder, input_buffer : bytes) -> Optional[VpxPointer]:
|
||||||
vpx_library = vpx_module.create_static_library()
|
vpx_library = vpx_module.create_static_library()
|
||||||
|
|
||||||
if vpx_library and input_buffer:
|
if vpx_library and input_buffer:
|
||||||
@@ -41,7 +41,7 @@ def decode(vpx_decoder : VpxDecoder, input_buffer : Buffer) -> Optional[BufferPa
|
|||||||
frame_width = ctypes.c_uint.from_address(address + 24).value & ~1
|
frame_width = ctypes.c_uint.from_address(address + 24).value & ~1
|
||||||
frame_height = ctypes.c_uint.from_address(address + 28).value & ~1
|
frame_height = ctypes.c_uint.from_address(address + 28).value & ~1
|
||||||
|
|
||||||
return BufferPack(
|
return VpxPointer(
|
||||||
buffer = collect(address, frame_width, frame_height),
|
buffer = collect(address, frame_width, frame_height),
|
||||||
resolution = (frame_width, frame_height)
|
resolution = (frame_width, frame_height)
|
||||||
)
|
)
|
||||||
@@ -49,7 +49,7 @@ def decode(vpx_decoder : VpxDecoder, input_buffer : Buffer) -> Optional[BufferPa
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def collect(address : int, frame_width : int, frame_height : int) -> Buffer:
|
def collect(address : int, frame_width : int, frame_height : int) -> bytes:
|
||||||
output_parts = []
|
output_parts = []
|
||||||
|
|
||||||
for index in range(3):
|
for index in range(3):
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import struct
|
|||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from facefusion.libraries import vpx as vpx_module
|
from facefusion.libraries import vpx as vpx_module
|
||||||
from facefusion.types import BitRate, Buffer, Resolution, VpxEncoder, VxpVideoCodec
|
from facefusion.types import BitRate, Resolution, VpxEncoder, VxpVideoCodec
|
||||||
|
|
||||||
|
|
||||||
def create(video_codec : VxpVideoCodec, frame_resolution : Resolution, bitrate : BitRate, thread_count : int, cpu_count : int) -> Optional[VpxEncoder]:
|
def create(video_codec : VxpVideoCodec, frame_resolution : Resolution, bitrate : BitRate, thread_count : int, cpu_count : int) -> Optional[VpxEncoder]:
|
||||||
@@ -44,7 +44,7 @@ def create(video_codec : VxpVideoCodec, frame_resolution : Resolution, bitrate :
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def encode(vpx_encoder : VpxEncoder, input_buffer : Buffer, frame_resolution : Resolution, frame_index : int) -> Buffer:
|
def encode(vpx_encoder : VpxEncoder, input_buffer : bytes, frame_resolution : Resolution, frame_index : int) -> bytes:
|
||||||
vpx_library = vpx_module.create_static_library()
|
vpx_library = vpx_module.create_static_library()
|
||||||
output_buffer = bytes()
|
output_buffer = bytes()
|
||||||
|
|
||||||
@@ -58,7 +58,7 @@ def encode(vpx_encoder : VpxEncoder, input_buffer : Buffer, frame_resolution : R
|
|||||||
return output_buffer
|
return output_buffer
|
||||||
|
|
||||||
|
|
||||||
def collect(vpx_encoder : VpxEncoder) -> Buffer:
|
def collect(vpx_encoder : VpxEncoder) -> bytes:
|
||||||
vpx_library = vpx_module.create_static_library()
|
vpx_library = vpx_module.create_static_library()
|
||||||
output_parts = []
|
output_parts = []
|
||||||
|
|
||||||
|
|||||||
@@ -78,12 +78,6 @@ def get_first(__list__ : Any) -> Any:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def get_middle(__list__ : Any) -> Any:
|
|
||||||
if isinstance(__list__, Sequence) and __list__:
|
|
||||||
return __list__[len(__list__) // 2]
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def get_last(__list__ : Any) -> Any:
|
def get_last(__list__ : Any) -> Any:
|
||||||
if isinstance(__list__, Reversible):
|
if isinstance(__list__, Reversible):
|
||||||
return next(reversed(__list__), None)
|
return next(reversed(__list__), None)
|
||||||
|
|||||||
@@ -1,41 +0,0 @@
|
|||||||
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'
|
|
||||||
+21
-12
@@ -1,20 +1,29 @@
|
|||||||
from configparser import ConfigParser
|
from configparser import ConfigParser
|
||||||
from functools import lru_cache
|
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
|
|
||||||
from facefusion import state_manager
|
from facefusion import state_manager
|
||||||
from facefusion.common_helper import cast_bool, cast_float, cast_int
|
from facefusion.common_helper import cast_bool, cast_float, cast_int
|
||||||
|
|
||||||
|
CONFIG_PARSER = None
|
||||||
|
|
||||||
@lru_cache
|
|
||||||
def get_static_config_parser() -> ConfigParser:
|
def get_config_parser() -> ConfigParser:
|
||||||
config_parser = ConfigParser()
|
global CONFIG_PARSER
|
||||||
config_parser.read(state_manager.get_item('config_path'), encoding = 'utf-8')
|
|
||||||
return 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
|
||||||
|
|
||||||
|
|
||||||
def get_str_value(section : str, option : str, fallback : Optional[str] = None) -> Optional[str]:
|
def get_str_value(section : str, option : str, fallback : Optional[str] = None) -> Optional[str]:
|
||||||
config_parser = get_static_config_parser()
|
config_parser = get_config_parser()
|
||||||
|
|
||||||
if config_parser.has_option(section, option) and config_parser.get(section, option).strip():
|
if config_parser.has_option(section, option) and config_parser.get(section, option).strip():
|
||||||
return config_parser.get(section, option)
|
return config_parser.get(section, option)
|
||||||
@@ -22,7 +31,7 @@ def get_str_value(section : str, option : str, fallback : Optional[str] = None)
|
|||||||
|
|
||||||
|
|
||||||
def get_int_value(section : str, option : str, fallback : Optional[str] = None) -> Optional[int]:
|
def get_int_value(section : str, option : str, fallback : Optional[str] = None) -> Optional[int]:
|
||||||
config_parser = get_static_config_parser()
|
config_parser = get_config_parser()
|
||||||
|
|
||||||
if config_parser.has_option(section, option) and config_parser.get(section, option).strip():
|
if config_parser.has_option(section, option) and config_parser.get(section, option).strip():
|
||||||
return config_parser.getint(section, option)
|
return config_parser.getint(section, option)
|
||||||
@@ -30,7 +39,7 @@ def get_int_value(section : str, option : str, fallback : Optional[str] = None)
|
|||||||
|
|
||||||
|
|
||||||
def get_float_value(section : str, option : str, fallback : Optional[str] = None) -> Optional[float]:
|
def get_float_value(section : str, option : str, fallback : Optional[str] = None) -> Optional[float]:
|
||||||
config_parser = get_static_config_parser()
|
config_parser = get_config_parser()
|
||||||
|
|
||||||
if config_parser.has_option(section, option) and config_parser.get(section, option).strip():
|
if config_parser.has_option(section, option) and config_parser.get(section, option).strip():
|
||||||
return config_parser.getfloat(section, option)
|
return config_parser.getfloat(section, option)
|
||||||
@@ -38,7 +47,7 @@ def get_float_value(section : str, option : str, fallback : Optional[str] = None
|
|||||||
|
|
||||||
|
|
||||||
def get_bool_value(section : str, option : str, fallback : Optional[str] = None) -> Optional[bool]:
|
def get_bool_value(section : str, option : str, fallback : Optional[str] = None) -> Optional[bool]:
|
||||||
config_parser = get_static_config_parser()
|
config_parser = get_config_parser()
|
||||||
|
|
||||||
if config_parser.has_option(section, option) and config_parser.get(section, option).strip():
|
if config_parser.has_option(section, option) and config_parser.get(section, option).strip():
|
||||||
return config_parser.getboolean(section, option)
|
return config_parser.getboolean(section, option)
|
||||||
@@ -46,7 +55,7 @@ def get_bool_value(section : str, option : str, fallback : Optional[str] = None)
|
|||||||
|
|
||||||
|
|
||||||
def get_str_list(section : str, option : str, fallback : Optional[str] = None) -> Optional[List[str]]:
|
def get_str_list(section : str, option : str, fallback : Optional[str] = None) -> Optional[List[str]]:
|
||||||
config_parser = get_static_config_parser()
|
config_parser = get_config_parser()
|
||||||
|
|
||||||
if config_parser.has_option(section, option) and config_parser.get(section, option).strip():
|
if config_parser.has_option(section, option) and config_parser.get(section, option).strip():
|
||||||
return config_parser.get(section, option).split()
|
return config_parser.get(section, option).split()
|
||||||
@@ -56,7 +65,7 @@ def get_str_list(section : str, option : str, fallback : Optional[str] = None) -
|
|||||||
|
|
||||||
|
|
||||||
def get_int_list(section : str, option : str, fallback : Optional[str] = None) -> Optional[List[int]]:
|
def get_int_list(section : str, option : str, fallback : Optional[str] = None) -> Optional[List[int]]:
|
||||||
config_parser = get_static_config_parser()
|
config_parser = get_config_parser()
|
||||||
|
|
||||||
if config_parser.has_option(section, option) and config_parser.get(section, option).strip():
|
if config_parser.has_option(section, option) and config_parser.get(section, option).strip():
|
||||||
return list(map(int, config_parser.get(section, option).split()))
|
return list(map(int, config_parser.get(section, option).split()))
|
||||||
|
|||||||
@@ -1,14 +1,16 @@
|
|||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
from typing import Tuple
|
from typing import List, Tuple
|
||||||
|
|
||||||
import numpy
|
import numpy
|
||||||
from tqdm import tqdm
|
from tqdm import tqdm
|
||||||
|
|
||||||
from facefusion import inference_manager, state_manager, translator
|
from facefusion import inference_manager, state_manager, translator
|
||||||
|
from facefusion.common_helper import is_macos
|
||||||
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
|
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
|
||||||
|
from facefusion.execution import has_execution_provider
|
||||||
from facefusion.filesystem import resolve_relative_path
|
from facefusion.filesystem import resolve_relative_path
|
||||||
from facefusion.thread_helper import conditional_thread_semaphore
|
from facefusion.thread_helper import conditional_thread_semaphore
|
||||||
from facefusion.types import Detection, DownloadScope, DownloadSet, Fps, InferencePool, ModelSet, VisionFrame
|
from facefusion.types import Detection, DownloadScope, DownloadSet, ExecutionProvider, Fps, InferencePool, ModelSet, VisionFrame
|
||||||
from facefusion.vision import detect_video_fps, fit_contain_frame, read_image, read_video_frame
|
from facefusion.vision import detect_video_fps, fit_contain_frame, read_image, read_video_frame
|
||||||
|
|
||||||
STREAM_COUNTER = 0
|
STREAM_COUNTER = 0
|
||||||
@@ -117,6 +119,12 @@ def clear_inference_pool() -> None:
|
|||||||
inference_manager.clear_inference_pool(__name__, model_names)
|
inference_manager.clear_inference_pool(__name__, model_names)
|
||||||
|
|
||||||
|
|
||||||
|
def resolve_execution_providers() -> List[ExecutionProvider]:
|
||||||
|
if is_macos() and has_execution_provider('coreml'):
|
||||||
|
return [ 'cpu' ]
|
||||||
|
return state_manager.get_item('execution_providers')
|
||||||
|
|
||||||
|
|
||||||
def collect_model_downloads() -> Tuple[DownloadSet, DownloadSet]:
|
def collect_model_downloads() -> Tuple[DownloadSet, DownloadSet]:
|
||||||
model_set = create_static_model_set('full')
|
model_set = create_static_model_set('full')
|
||||||
model_hash_set = {}
|
model_hash_set = {}
|
||||||
@@ -167,8 +175,6 @@ def analyse_video(video_path : str, trim_frame_start : int, trim_frame_end : int
|
|||||||
for frame_number in frame_range:
|
for frame_number in frame_range:
|
||||||
if frame_number % int(video_fps) == 0:
|
if frame_number % int(video_fps) == 0:
|
||||||
vision_frame = read_video_frame(video_path, frame_number)
|
vision_frame = read_video_frame(video_path, frame_number)
|
||||||
|
|
||||||
if numpy.any(vision_frame):
|
|
||||||
total += 1
|
total += 1
|
||||||
|
|
||||||
if analyse_frame(vision_frame):
|
if analyse_frame(vision_frame):
|
||||||
|
|||||||
+50
-41
@@ -7,20 +7,21 @@ from time import time
|
|||||||
|
|
||||||
import uvicorn
|
import uvicorn
|
||||||
|
|
||||||
import facefusion.apis.core
|
from facefusion import args_store, benchmarker, cli_helper, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, hash_helper, logger, state_manager, translator, voice_extractor
|
||||||
from facefusion import args_helper, benchmarker, cli_helper, content_analyser, hash_helper, logger, state_manager, translator
|
from facefusion.apis.core import create_api
|
||||||
from facefusion.args_helper import apply_args
|
from facefusion.args_helper import apply_args
|
||||||
from facefusion.download import conditional_download_hashes, conditional_download_sources
|
from facefusion.download import conditional_download_hashes, conditional_download_sources
|
||||||
from facefusion.exit_helper import hard_exit, signal_exit
|
from facefusion.exit_helper import hard_exit, signal_exit
|
||||||
from facefusion.filesystem import get_file_extension, has_audio, has_image, has_video
|
from facefusion.filesystem import get_file_extension, get_file_name, resolve_file_paths, resolve_file_pattern
|
||||||
from facefusion.filesystem import get_file_name, resolve_file_paths, resolve_file_pattern
|
from facefusion.filesystem import has_audio, has_image, has_video
|
||||||
from facefusion.jobs import job_helper, job_manager, job_runner
|
from facefusion.jobs import job_helper, job_manager, job_runner
|
||||||
from facefusion.jobs.job_list import compose_job_list
|
from facefusion.jobs.job_list import compose_job_list
|
||||||
|
from facefusion.libraries import aom as aom_module, datachannel as datachannel_module, opus as opus_module, vpx as vpx_module
|
||||||
from facefusion.processors.core import get_processors_modules
|
from facefusion.processors.core import get_processors_modules
|
||||||
from facefusion.program import create_program
|
from facefusion.program import create_program
|
||||||
from facefusion.program_helper import validate_args
|
from facefusion.program_helper import validate_args
|
||||||
from facefusion.types import Args, ErrorCode, WorkflowMode
|
from facefusion.types import Args, ErrorCode, WorkFlow
|
||||||
from facefusion.workflows import audio_to_image, audio_to_image_as_frames, image_to_image, image_to_video, image_to_video_as_frames
|
from facefusion.workflows import audio_to_image, image_to_image, image_to_video
|
||||||
|
|
||||||
|
|
||||||
def cli() -> None:
|
def cli() -> None:
|
||||||
@@ -54,11 +55,8 @@ def route(args : Args) -> None:
|
|||||||
benchmarker.render()
|
benchmarker.render()
|
||||||
|
|
||||||
if state_manager.get_item('command') == 'api':
|
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__)
|
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'))
|
uvicorn.run(create_api(), host = state_manager.get_item('api_host'), port = state_manager.get_item('api_port'))
|
||||||
hard_exit(1)
|
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 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' ]:
|
||||||
@@ -102,9 +100,25 @@ def pre_check() -> bool:
|
|||||||
|
|
||||||
|
|
||||||
def common_pre_check() -> bool:
|
def common_pre_check() -> bool:
|
||||||
content_analyser_content = inspect.getsource(content_analyser).encode()
|
common_modules =\
|
||||||
|
[
|
||||||
|
aom_module,
|
||||||
|
datachannel_module,
|
||||||
|
content_analyser,
|
||||||
|
face_classifier,
|
||||||
|
face_detector,
|
||||||
|
face_landmarker,
|
||||||
|
face_masker,
|
||||||
|
face_recognizer,
|
||||||
|
opus_module,
|
||||||
|
voice_extractor,
|
||||||
|
vpx_module
|
||||||
|
]
|
||||||
|
|
||||||
return hash_helper.create_hash(content_analyser_content) == '975d67d6'
|
content_analyser_content = inspect.getsource(content_analyser).encode()
|
||||||
|
content_analyser_hash = hash_helper.create_hash(content_analyser_content)
|
||||||
|
|
||||||
|
return all(module.pre_check() for module in common_modules) and content_analyser_hash == 'b14e7b92'
|
||||||
|
|
||||||
|
|
||||||
def processors_pre_check() -> bool:
|
def processors_pre_check() -> bool:
|
||||||
@@ -115,19 +129,22 @@ def processors_pre_check() -> bool:
|
|||||||
|
|
||||||
|
|
||||||
def force_download() -> ErrorCode:
|
def force_download() -> ErrorCode:
|
||||||
download_scope = state_manager.get_item('download_scope')
|
common_modules =\
|
||||||
|
[
|
||||||
|
content_analyser,
|
||||||
|
face_classifier,
|
||||||
|
face_detector,
|
||||||
|
face_landmarker,
|
||||||
|
face_masker,
|
||||||
|
face_recognizer,
|
||||||
|
voice_extractor
|
||||||
|
]
|
||||||
available_processors = [ get_file_name(file_path) for file_path in resolve_file_paths('facefusion/processors/modules') ]
|
available_processors = [ get_file_name(file_path) for file_path in resolve_file_paths('facefusion/processors/modules') ]
|
||||||
processor_modules = get_processors_modules(available_processors)
|
processor_modules = get_processors_modules(available_processors)
|
||||||
common_modules = []
|
|
||||||
|
|
||||||
for processor_module in processor_modules:
|
|
||||||
for common_module in processor_module.get_common_modules():
|
|
||||||
if common_module not in common_modules:
|
|
||||||
common_modules.append(common_module)
|
|
||||||
|
|
||||||
for module in common_modules + processor_modules:
|
for module in common_modules + processor_modules:
|
||||||
if hasattr(module, 'create_static_model_set'):
|
if hasattr(module, 'create_static_model_set'):
|
||||||
for model in module.create_static_model_set(download_scope).values():
|
for model in module.create_static_model_set(state_manager.get_item('download_scope')).values():
|
||||||
model_hash_set = model.get('hashes')
|
model_hash_set = model.get('hashes')
|
||||||
model_source_set = model.get('sources')
|
model_source_set = model.get('sources')
|
||||||
|
|
||||||
@@ -183,7 +200,7 @@ def route_job_manager(args : Args) -> ErrorCode:
|
|||||||
return 1
|
return 1
|
||||||
|
|
||||||
if state_manager.get_item('command') == 'job-add-step':
|
if state_manager.get_item('command') == 'job-add-step':
|
||||||
step_args = args_helper.filter_step_args(args)
|
step_args = args_store.filter_step_args(args)
|
||||||
|
|
||||||
if job_manager.add_step(state_manager.get_item('job_id'), step_args):
|
if job_manager.add_step(state_manager.get_item('job_id'), step_args):
|
||||||
logger.info(translator.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__)
|
||||||
@@ -192,7 +209,7 @@ def route_job_manager(args : Args) -> ErrorCode:
|
|||||||
return 1
|
return 1
|
||||||
|
|
||||||
if state_manager.get_item('command') == 'job-remix-step':
|
if state_manager.get_item('command') == 'job-remix-step':
|
||||||
step_args = args_helper.filter_step_args(args)
|
step_args = args_store.filter_step_args(args)
|
||||||
|
|
||||||
if job_manager.remix_step(state_manager.get_item('job_id'), state_manager.get_item('step_index'), step_args):
|
if job_manager.remix_step(state_manager.get_item('job_id'), state_manager.get_item('step_index'), step_args):
|
||||||
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__)
|
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__)
|
||||||
@@ -201,7 +218,7 @@ def route_job_manager(args : Args) -> ErrorCode:
|
|||||||
return 1
|
return 1
|
||||||
|
|
||||||
if state_manager.get_item('command') == 'job-insert-step':
|
if state_manager.get_item('command') == 'job-insert-step':
|
||||||
step_args = args_helper.filter_step_args(args)
|
step_args = args_store.filter_step_args(args)
|
||||||
|
|
||||||
if job_manager.insert_step(state_manager.get_item('job_id'), state_manager.get_item('step_index'), step_args):
|
if job_manager.insert_step(state_manager.get_item('job_id'), state_manager.get_item('step_index'), step_args):
|
||||||
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__)
|
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__)
|
||||||
@@ -255,7 +272,7 @@ def route_job_runner() -> ErrorCode:
|
|||||||
|
|
||||||
def process_headless(args : Args) -> ErrorCode:
|
def process_headless(args : Args) -> ErrorCode:
|
||||||
job_id = job_helper.suggest_job_id('headless')
|
job_id = job_helper.suggest_job_id('headless')
|
||||||
step_args = args_helper.filter_step_args(args)
|
step_args = args_store.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):
|
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
|
return 0
|
||||||
@@ -264,7 +281,7 @@ def process_headless(args : Args) -> ErrorCode:
|
|||||||
|
|
||||||
def process_batch(args : Args) -> ErrorCode:
|
def process_batch(args : Args) -> ErrorCode:
|
||||||
job_id = job_helper.suggest_job_id('batch')
|
job_id = job_helper.suggest_job_id('batch')
|
||||||
step_args = args_helper.filter_step_args(args)
|
step_args = args_store.filter_step_args(args)
|
||||||
source_paths = resolve_file_pattern(step_args.get('source_pattern'))
|
source_paths = resolve_file_pattern(step_args.get('source_pattern'))
|
||||||
target_paths = resolve_file_pattern(step_args.get('target_pattern'))
|
target_paths = resolve_file_pattern(step_args.get('target_pattern'))
|
||||||
|
|
||||||
@@ -302,7 +319,7 @@ def process_batch(args : Args) -> ErrorCode:
|
|||||||
|
|
||||||
def process_step(job_id : str, step_index : int, step_args : Args) -> bool:
|
def process_step(job_id : str, step_index : int, step_args : Args) -> bool:
|
||||||
step_total = job_manager.count_step_total(job_id)
|
step_total = job_manager.count_step_total(job_id)
|
||||||
cli_args = args_helper.extract_cli_args(state_manager.get_state())
|
cli_args = args_store.filter_cli_args(state_manager.get_state()) #type:ignore[arg-type]
|
||||||
args = cli_args.copy()
|
args = cli_args.copy()
|
||||||
args.update(step_args)
|
args.update(step_args)
|
||||||
apply_args(args, state_manager.set_item)
|
apply_args(args, state_manager.set_item)
|
||||||
@@ -317,36 +334,28 @@ def process_step(job_id : str, step_index : int, step_args : Args) -> bool:
|
|||||||
def conditional_process() -> ErrorCode:
|
def conditional_process() -> ErrorCode:
|
||||||
start_time = time()
|
start_time = time()
|
||||||
|
|
||||||
if state_manager.get_item('workflow_mode') == 'auto':
|
if state_manager.get_item('workflow') == 'auto':
|
||||||
state_manager.set_item('workflow_mode', detect_workflow_mode())
|
state_manager.set_item('workflow', detect_workflow())
|
||||||
|
|
||||||
for processor_module in get_processors_modules(state_manager.get_item('processors')):
|
for processor_module in get_processors_modules(state_manager.get_item('processors')):
|
||||||
if not processor_module.pre_process('output'):
|
if not processor_module.pre_process('output'):
|
||||||
return 2
|
return 2
|
||||||
|
|
||||||
if state_manager.get_item('workflow_mode') == 'audio-to-image:video':
|
if state_manager.get_item('workflow') == 'audio-to-image':
|
||||||
return audio_to_image.process(start_time)
|
return audio_to_image.process(start_time)
|
||||||
if state_manager.get_item('workflow_mode') == 'audio-to-image:frames':
|
if state_manager.get_item('workflow') == 'image-to-image':
|
||||||
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)
|
return image_to_image.process(start_time)
|
||||||
if state_manager.get_item('workflow_mode') == 'image-to-video':
|
if state_manager.get_item('workflow') == 'image-to-video':
|
||||||
return image_to_video.process(start_time)
|
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
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def detect_workflow_mode() -> WorkflowMode:
|
def detect_workflow() -> WorkFlow:
|
||||||
if has_video([ state_manager.get_item('target_path') ]):
|
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'
|
||||||
return 'image-to-video:frames'
|
|
||||||
|
|
||||||
if has_audio(state_manager.get_item('source_paths')) and has_image([ state_manager.get_item('target_path') ]):
|
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'
|
||||||
return 'audio-to-image:video'
|
|
||||||
return 'audio-to-image:frames'
|
|
||||||
|
|
||||||
return 'image-to-image'
|
return 'image-to-image'
|
||||||
|
|||||||
@@ -9,14 +9,14 @@ from facefusion.types import Command
|
|||||||
def run(commands : List[Command]) -> List[Command]:
|
def run(commands : List[Command]) -> List[Command]:
|
||||||
user_agent = metadata.get('name') + '/' + metadata.get('version')
|
user_agent = metadata.get('name') + '/' + metadata.get('version')
|
||||||
|
|
||||||
return [ shutil.which('curl'), '--user-agent', user_agent, '--location', '--silent', '--ssl-no-revoke' ] + commands
|
return [ shutil.which('curl'), '--user-agent', user_agent, '--insecure', '--location', '--silent' ] + commands
|
||||||
|
|
||||||
|
|
||||||
def chain(*commands : List[Command]) -> List[Command]:
|
def chain(*commands : List[Command]) -> List[Command]:
|
||||||
return list(itertools.chain(*commands))
|
return list(itertools.chain(*commands))
|
||||||
|
|
||||||
|
|
||||||
def ping(url : str) -> List[Command]:
|
def head(url : str) -> List[Command]:
|
||||||
return [ '-I', url ]
|
return [ '-I', url ]
|
||||||
|
|
||||||
|
|
||||||
@@ -26,7 +26,3 @@ def download(url : str, download_file_path : str) -> List[Command]:
|
|||||||
|
|
||||||
def set_timeout(timeout : int) -> List[Command]:
|
def set_timeout(timeout : int) -> List[Command]:
|
||||||
return [ '--connect-timeout', str(timeout) ]
|
return [ '--connect-timeout', str(timeout) ]
|
||||||
|
|
||||||
|
|
||||||
def set_retry(retry : int) -> List[Command]:
|
|
||||||
return [ '--retry', str(retry) ]
|
|
||||||
|
|||||||
@@ -10,10 +10,10 @@ import facefusion.choices
|
|||||||
from facefusion import curl_builder, logger, process_manager, state_manager, translator
|
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.filesystem import get_file_name, get_file_size, is_file, remove_file
|
||||||
from facefusion.hash_helper import validate_hash
|
from facefusion.hash_helper import validate_hash
|
||||||
from facefusion.types import Buffer, Command, DownloadProvider, DownloadSet
|
from facefusion.types import Command, DownloadProvider, DownloadSet
|
||||||
|
|
||||||
|
|
||||||
def open_curl(commands : List[Command]) -> subprocess.Popen[Buffer]:
|
def open_curl(commands : List[Command]) -> subprocess.Popen[bytes]:
|
||||||
commands = curl_builder.run(commands)
|
commands = curl_builder.run(commands)
|
||||||
return subprocess.Popen(commands, stdin = subprocess.PIPE, stdout = subprocess.PIPE)
|
return subprocess.Popen(commands, stdin = subprocess.PIPE, stdout = subprocess.PIPE)
|
||||||
|
|
||||||
@@ -29,8 +29,7 @@ def conditional_download(download_directory_path : str, urls : List[str]) -> Non
|
|||||||
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:
|
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(
|
commands = curl_builder.chain(
|
||||||
curl_builder.download(url, download_file_path),
|
curl_builder.download(url, download_file_path),
|
||||||
curl_builder.set_timeout(5),
|
curl_builder.set_timeout(5)
|
||||||
curl_builder.set_retry(5)
|
|
||||||
)
|
)
|
||||||
open_curl(commands)
|
open_curl(commands)
|
||||||
current_size = initial_size
|
current_size = initial_size
|
||||||
@@ -45,7 +44,7 @@ def conditional_download(download_directory_path : str, urls : List[str]) -> Non
|
|||||||
@lru_cache(maxsize = 64)
|
@lru_cache(maxsize = 64)
|
||||||
def get_static_download_size(url : str) -> int:
|
def get_static_download_size(url : str) -> int:
|
||||||
commands = curl_builder.chain(
|
commands = curl_builder.chain(
|
||||||
curl_builder.ping(url),
|
curl_builder.head(url),
|
||||||
curl_builder.set_timeout(5)
|
curl_builder.set_timeout(5)
|
||||||
)
|
)
|
||||||
process = open_curl(commands)
|
process = open_curl(commands)
|
||||||
@@ -63,7 +62,7 @@ def get_static_download_size(url : str) -> int:
|
|||||||
@lru_cache(maxsize = 64)
|
@lru_cache(maxsize = 64)
|
||||||
def ping_static_url(url : str) -> bool:
|
def ping_static_url(url : str) -> bool:
|
||||||
commands = curl_builder.chain(
|
commands = curl_builder.chain(
|
||||||
curl_builder.ping(url),
|
curl_builder.head(url),
|
||||||
curl_builder.set_timeout(5)
|
curl_builder.set_timeout(5)
|
||||||
)
|
)
|
||||||
process = open_curl(commands)
|
process = open_curl(commands)
|
||||||
|
|||||||
+99
-72
@@ -1,15 +1,15 @@
|
|||||||
import os
|
import shutil
|
||||||
|
import subprocess
|
||||||
|
import xml.etree.ElementTree as ElementTree
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
from typing import List, Tuple
|
from typing import List, Optional
|
||||||
|
|
||||||
import onnxruntime
|
from onnxruntime import get_available_providers, set_default_logger_severity
|
||||||
|
|
||||||
import facefusion.choices
|
import facefusion.choices
|
||||||
from facefusion.filesystem import create_directory, is_directory
|
from facefusion.types import ExecutionDevice, ExecutionProvider, InferenceSessionProvider, ValueAndUnit
|
||||||
from facefusion.system import detect_graphic_devices
|
|
||||||
from facefusion.types import ExecutionProvider, InferenceOptionSet, InferenceProvider
|
|
||||||
|
|
||||||
onnxruntime.set_default_logger_severity(3)
|
set_default_logger_severity(3)
|
||||||
|
|
||||||
|
|
||||||
def has_execution_provider(execution_provider : ExecutionProvider) -> bool:
|
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]:
|
def get_available_execution_providers() -> List[ExecutionProvider]:
|
||||||
inference_session_providers = onnxruntime.get_available_providers()
|
inference_session_providers = get_available_providers()
|
||||||
available_execution_providers : List[ExecutionProvider] = []
|
available_execution_providers : List[ExecutionProvider] = []
|
||||||
|
|
||||||
for execution_provider, execution_provider_value in facefusion.choices.execution_provider_set.items():
|
for execution_provider, execution_provider_value in facefusion.choices.execution_provider_set.items():
|
||||||
@@ -28,100 +28,62 @@ def get_available_execution_providers() -> List[ExecutionProvider]:
|
|||||||
return available_execution_providers
|
return available_execution_providers
|
||||||
|
|
||||||
|
|
||||||
def create_inference_providers(execution_device_id : int, execution_providers : List[ExecutionProvider]) -> List[InferenceProvider]:
|
def create_inference_session_providers(execution_device_id : int, execution_providers : List[ExecutionProvider]) -> List[InferenceSessionProvider]:
|
||||||
inference_providers : List[InferenceProvider] = []
|
inference_session_providers : List[InferenceSessionProvider] = []
|
||||||
cache_path = resolve_cache_path()
|
|
||||||
|
|
||||||
for execution_provider in execution_providers:
|
for execution_provider in execution_providers:
|
||||||
if execution_provider == 'cuda':
|
if execution_provider == 'cuda':
|
||||||
inference_providers.append((facefusion.choices.execution_provider_set.get(execution_provider),
|
inference_session_providers.append((facefusion.choices.execution_provider_set.get(execution_provider),
|
||||||
{
|
{
|
||||||
'device_id': execution_device_id,
|
'device_id': execution_device_id,
|
||||||
'cudnn_conv_algo_search': resolve_static_cudnn_conv_algo_search(tuple(execution_providers))
|
'cudnn_conv_algo_search': resolve_cudnn_conv_algo_search()
|
||||||
}))
|
}))
|
||||||
|
|
||||||
if execution_provider == 'tensorrt':
|
if execution_provider == 'tensorrt':
|
||||||
inference_option_set : InferenceOptionSet =\
|
inference_session_providers.append((facefusion.choices.execution_provider_set.get(execution_provider),
|
||||||
{
|
|
||||||
'device_id': execution_device_id
|
|
||||||
}
|
|
||||||
if is_directory(cache_path) or create_directory(cache_path):
|
|
||||||
inference_option_set.update(
|
|
||||||
{
|
{
|
||||||
|
'device_id': execution_device_id,
|
||||||
'trt_engine_cache_enable': True,
|
'trt_engine_cache_enable': True,
|
||||||
'trt_engine_cache_path': cache_path,
|
'trt_engine_cache_path': '.caches',
|
||||||
'trt_timing_cache_enable': True,
|
'trt_timing_cache_enable': True,
|
||||||
'trt_timing_cache_path': cache_path,
|
'trt_timing_cache_path': '.caches',
|
||||||
'trt_builder_optimization_level': 4
|
'trt_builder_optimization_level': 5
|
||||||
})
|
}))
|
||||||
inference_providers.append((facefusion.choices.execution_provider_set.get(execution_provider), inference_option_set))
|
|
||||||
|
|
||||||
if execution_provider in [ 'directml', 'rocm' ]:
|
if execution_provider in [ 'directml', 'rocm' ]:
|
||||||
inference_providers.append((facefusion.choices.execution_provider_set.get(execution_provider),
|
inference_session_providers.append((facefusion.choices.execution_provider_set.get(execution_provider),
|
||||||
{
|
{
|
||||||
'device_id': execution_device_id
|
'device_id': execution_device_id
|
||||||
}))
|
}))
|
||||||
|
|
||||||
if execution_provider == 'migraphx':
|
if execution_provider == 'migraphx':
|
||||||
inference_option_set =\
|
inference_session_providers.append((facefusion.choices.execution_provider_set.get(execution_provider),
|
||||||
{
|
{
|
||||||
'device_id': execution_device_id
|
'device_id': execution_device_id,
|
||||||
}
|
'migraphx_model_cache_dir': '.caches'
|
||||||
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':
|
if execution_provider == 'openvino':
|
||||||
inference_providers.append((facefusion.choices.execution_provider_set.get(execution_provider),
|
inference_session_providers.append((facefusion.choices.execution_provider_set.get(execution_provider),
|
||||||
{
|
{
|
||||||
'device_type': resolve_openvino_device_type(execution_device_id),
|
'device_type': resolve_openvino_device_type(execution_device_id),
|
||||||
'precision': 'FP32'
|
'precision': 'FP32'
|
||||||
}))
|
}))
|
||||||
|
if execution_provider == 'coreml':
|
||||||
if execution_provider == 'qnn':
|
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,
|
'SpecializationStrategy': 'FastPrediction',
|
||||||
'backend_type': 'htp'
|
'ModelCacheDirectory': '.caches'
|
||||||
}))
|
}))
|
||||||
|
|
||||||
if 'cpu' in execution_providers:
|
if 'cpu' in execution_providers:
|
||||||
inference_providers.append(facefusion.choices.execution_provider_set.get('cpu'))
|
inference_session_providers.append(facefusion.choices.execution_provider_set.get('cpu'))
|
||||||
|
|
||||||
return inference_providers
|
return inference_session_providers
|
||||||
|
|
||||||
|
|
||||||
def resolve_cache_path() -> str:
|
def resolve_cudnn_conv_algo_search() -> str:
|
||||||
return os.path.join('.caches', onnxruntime.get_version_string())
|
execution_devices = detect_static_execution_devices()
|
||||||
|
|
||||||
|
|
||||||
@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')
|
product_names = ('GeForce GTX 1630', 'GeForce GTX 1650', 'GeForce GTX 1660')
|
||||||
|
|
||||||
for graphic_device in graphic_devices:
|
for execution_device in execution_devices:
|
||||||
if graphic_device.get('product').get('name').startswith(product_names):
|
if execution_device.get('product').get('name').startswith(product_names):
|
||||||
return 'DEFAULT'
|
return 'DEFAULT'
|
||||||
|
|
||||||
return 'EXHAUSTIVE'
|
return 'EXHAUSTIVE'
|
||||||
@@ -131,3 +93,68 @@ def resolve_openvino_device_type(execution_device_id : int) -> str:
|
|||||||
if execution_device_id == 0:
|
if execution_device_id == 0:
|
||||||
return 'GPU'
|
return 'GPU'
|
||||||
return 'GPU.' + str(execution_device_id)
|
return 'GPU.' + str(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()
|
||||||
|
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
|
||||||
|
|||||||
@@ -2,13 +2,14 @@ from typing import List, Optional
|
|||||||
|
|
||||||
import numpy
|
import numpy
|
||||||
|
|
||||||
from facefusion import face_store, state_manager
|
from facefusion import state_manager
|
||||||
from facefusion.common_helper import get_first, get_middle
|
from facefusion.common_helper import get_first
|
||||||
from facefusion.face_classifier import classify_face
|
from facefusion.face_classifier import classify_face
|
||||||
from facefusion.face_detector import detect_faces, detect_faces_by_angle
|
from facefusion.face_detector import detect_faces, detect_faces_by_angle
|
||||||
from facefusion.face_helper import apply_nms, average_points, convert_to_face_landmark_5, estimate_face_angle, get_nms_threshold
|
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_landmarker import detect_face_landmark, estimate_face_landmark_68_5
|
||||||
from facefusion.face_recognizer import calculate_face_embedding
|
from facefusion.face_recognizer import calculate_face_embedding
|
||||||
|
from facefusion.face_store import get_static_faces, set_static_faces
|
||||||
from facefusion.types import BoundingBox, Face, FaceLandmark5, FaceLandmarkSet, FaceScoreSet, Score, VisionFrame
|
from facefusion.types import BoundingBox, Face, FaceLandmark5, FaceLandmarkSet, FaceScoreSet, Score, VisionFrame
|
||||||
|
|
||||||
|
|
||||||
@@ -46,9 +47,7 @@ def create_faces(vision_frame : VisionFrame, bounding_boxes : List[BoundingBox],
|
|||||||
}
|
}
|
||||||
face_embedding, face_embedding_norm = calculate_face_embedding(vision_frame, face_landmark_set.get('5/68'))
|
face_embedding, face_embedding_norm = calculate_face_embedding(vision_frame, face_landmark_set.get('5/68'))
|
||||||
gender, age, race = classify_face(vision_frame, face_landmark_set.get('5/68'))
|
gender, age, race = classify_face(vision_frame, face_landmark_set.get('5/68'))
|
||||||
|
|
||||||
faces.append(Face(
|
faces.append(Face(
|
||||||
origin = 'detect',
|
|
||||||
bounding_box = bounding_box,
|
bounding_box = bounding_box,
|
||||||
score_set = face_score_set,
|
score_set = face_score_set,
|
||||||
landmark_set = face_landmark_set,
|
landmark_set = face_landmark_set,
|
||||||
@@ -69,11 +68,40 @@ def get_one_face(faces : List[Face], position : int = 0) -> Optional[Face]:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def get_average_face(faces : List[Face]) -> Optional[Face]:
|
||||||
|
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(
|
||||||
|
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 get_many_faces(vision_frames : List[VisionFrame]) -> List[Face]:
|
def get_many_faces(vision_frames : List[VisionFrame]) -> List[Face]:
|
||||||
many_faces : List[Face] = []
|
many_faces : List[Face] = []
|
||||||
|
|
||||||
for vision_frame in vision_frames:
|
for vision_frame in vision_frames:
|
||||||
if numpy.any(vision_frame):
|
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_bounding_boxes = []
|
||||||
all_face_scores = []
|
all_face_scores = []
|
||||||
all_face_landmarks_5 = []
|
all_face_landmarks_5 = []
|
||||||
@@ -92,104 +120,10 @@ def get_many_faces(vision_frames : List[VisionFrame]) -> List[Face]:
|
|||||||
|
|
||||||
if faces:
|
if faces:
|
||||||
many_faces.extend(faces)
|
many_faces.extend(faces)
|
||||||
|
set_static_faces(vision_frame, faces)
|
||||||
return many_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:
|
def scale_face(target_face : Face, target_vision_frame : VisionFrame, temp_vision_frame : VisionFrame) -> Face:
|
||||||
scale_x = temp_vision_frame.shape[1] / target_vision_frame.shape[1]
|
scale_x = temp_vision_frame.shape[1] / target_vision_frame.shape[1]
|
||||||
scale_y = temp_vision_frame.shape[0] / target_vision_frame.shape[0]
|
scale_y = temp_vision_frame.shape[0] / target_vision_frame.shape[0]
|
||||||
@@ -20,7 +20,7 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
|||||||
'__metadata__':
|
'__metadata__':
|
||||||
{
|
{
|
||||||
'vendor': 'dchen236',
|
'vendor': 'dchen236',
|
||||||
'license': 'CC-BY-4.0',
|
'license': 'Non-Commercial',
|
||||||
'year': 2021
|
'year': 2021
|
||||||
},
|
},
|
||||||
'hashes':
|
'hashes':
|
||||||
|
|||||||
@@ -228,7 +228,7 @@ def detect_with_retinaface(vision_frame : VisionFrame, face_detector_size : str)
|
|||||||
if numpy.any(keep_indices):
|
if numpy.any(keep_indices):
|
||||||
stride_height = face_detector_height // feature_stride
|
stride_height = face_detector_height // feature_stride
|
||||||
stride_width = face_detector_width // feature_stride
|
stride_width = face_detector_width // feature_stride
|
||||||
anchors = create_static_anchors(feature_stride, anchor_total, stride_width, stride_height)
|
anchors = create_static_anchors(feature_stride, anchor_total, stride_height, stride_width)
|
||||||
bounding_boxes_raw = detection[index + feature_map_channel] * feature_stride
|
bounding_boxes_raw = detection[index + feature_map_channel] * feature_stride
|
||||||
face_landmarks_5_raw = detection[index + feature_map_channel * 2] * feature_stride
|
face_landmarks_5_raw = detection[index + feature_map_channel * 2] * feature_stride
|
||||||
|
|
||||||
@@ -273,7 +273,7 @@ def detect_with_scrfd(vision_frame : VisionFrame, face_detector_size : str) -> T
|
|||||||
if numpy.any(keep_indices):
|
if numpy.any(keep_indices):
|
||||||
stride_height = face_detector_height // feature_stride
|
stride_height = face_detector_height // feature_stride
|
||||||
stride_width = face_detector_width // feature_stride
|
stride_width = face_detector_width // feature_stride
|
||||||
anchors = create_static_anchors(feature_stride, anchor_total, stride_width, stride_height)
|
anchors = create_static_anchors(feature_stride, anchor_total, stride_height, stride_width)
|
||||||
bounding_boxes_raw = detection[index + feature_map_channel] * feature_stride
|
bounding_boxes_raw = detection[index + feature_map_channel] * feature_stride
|
||||||
face_landmarks_5_raw = detection[index + feature_map_channel * 2] * feature_stride
|
face_landmarks_5_raw = detection[index + feature_map_channel * 2] * feature_stride
|
||||||
|
|
||||||
@@ -356,7 +356,7 @@ def detect_with_yunet(vision_frame : VisionFrame, face_detector_size : str) -> T
|
|||||||
if numpy.any(keep_indices):
|
if numpy.any(keep_indices):
|
||||||
stride_height = face_detector_height // feature_stride
|
stride_height = face_detector_height // feature_stride
|
||||||
stride_width = face_detector_width // feature_stride
|
stride_width = face_detector_width // feature_stride
|
||||||
anchors = create_static_anchors(feature_stride, anchor_total, stride_width, stride_height)
|
anchors = create_static_anchors(feature_stride, anchor_total, stride_height, stride_width)
|
||||||
bounding_boxes_center = detection[index + feature_map_channel * 2].squeeze(0)[:, :2] * feature_stride + anchors
|
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
|
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)
|
face_landmarks_5_raw = detection[index + feature_map_channel * 3].squeeze(0)
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ def calculate_paste_area(temp_vision_frame : VisionFrame, crop_vision_frame : Vi
|
|||||||
|
|
||||||
|
|
||||||
@lru_cache()
|
@lru_cache()
|
||||||
def create_static_anchors(feature_stride : int, anchor_total : int, stride_width : int, stride_height : int) -> Anchors:
|
def create_static_anchors(feature_stride : int, anchor_total : int, stride_height : int, stride_width : int) -> Anchors:
|
||||||
x, y = numpy.mgrid[:stride_width, :stride_height]
|
x, y = numpy.mgrid[:stride_width, :stride_height]
|
||||||
anchors = numpy.stack((y, x), axis = -1)
|
anchors = numpy.stack((y, x), axis = -1)
|
||||||
anchors = (anchors * feature_stride).reshape((-1, 2))
|
anchors = (anchors * feature_stride).reshape((-1, 2))
|
||||||
@@ -254,23 +254,3 @@ def merge_matrix(temp_matrices : List[Matrix]) -> Matrix:
|
|||||||
matrix = numpy.dot(temp_matrix, matrix)
|
matrix = numpy.dot(temp_matrix, matrix)
|
||||||
|
|
||||||
return matrix[:2, :]
|
return matrix[:2, :]
|
||||||
|
|
||||||
|
|
||||||
def calculate_bounding_box_overlap(bounding_box_a : BoundingBox, bounding_box_b : BoundingBox) -> float:
|
|
||||||
intersection_x1 = max(bounding_box_a[0], bounding_box_b[0])
|
|
||||||
intersection_y1 = max(bounding_box_a[1], bounding_box_b[1])
|
|
||||||
intersection_x2 = min(bounding_box_a[2], bounding_box_b[2])
|
|
||||||
intersection_y2 = min(bounding_box_a[3], bounding_box_b[3])
|
|
||||||
intersection = max(0, intersection_x2 - intersection_x1) * max(0, intersection_y2 - intersection_y1)
|
|
||||||
bounding_box_area = (bounding_box_a[2] - bounding_box_a[0]) * (bounding_box_a[3] - bounding_box_a[1])
|
|
||||||
reference_bounding_box_area = (bounding_box_b[2] - bounding_box_b[0]) * (bounding_box_b[3] - bounding_box_b[1])
|
|
||||||
union = bounding_box_area + reference_bounding_box_area - intersection
|
|
||||||
|
|
||||||
if union > 0:
|
|
||||||
return intersection / union
|
|
||||||
|
|
||||||
return 0.0
|
|
||||||
|
|
||||||
|
|
||||||
def average_points(points_previous : Points, points_next : Points, average_factor : float) -> Points:
|
|
||||||
return points_previous * (1 - average_factor) + points_next * average_factor
|
|
||||||
|
|||||||
+16
-41
@@ -2,35 +2,26 @@ from typing import List
|
|||||||
|
|
||||||
import numpy
|
import numpy
|
||||||
|
|
||||||
import facefusion.choices
|
|
||||||
from facefusion import state_manager
|
from facefusion import state_manager
|
||||||
from facefusion.common_helper import get_first, get_middle
|
from facefusion.face_analyser import get_many_faces, get_one_face
|
||||||
from facefusion.face_creator import get_one_face, get_static_faces
|
|
||||||
from facefusion.face_tracker import track_faces
|
|
||||||
from facefusion.types import Face, FaceSelectorOrder, Gender, Race, Score, VisionFrame
|
from facefusion.types import Face, FaceSelectorOrder, Gender, Race, Score, VisionFrame
|
||||||
|
|
||||||
|
|
||||||
def select_faces(reference_vision_frame : VisionFrame, source_vision_frames : List[VisionFrame], target_vision_frames : List[VisionFrame]) -> List[Face]:
|
def select_faces(reference_vision_frame : VisionFrame, target_vision_frame : VisionFrame) -> List[Face]:
|
||||||
source_faces = get_static_faces(source_vision_frames)
|
target_faces = get_many_faces([ target_vision_frame ])
|
||||||
|
|
||||||
if state_manager.get_item('face_tracker_score') > 0:
|
|
||||||
target_faces = track_faces(target_vision_frames, state_manager.get_item('face_tracker_score'))
|
|
||||||
else:
|
|
||||||
target_faces = get_static_faces([ get_middle(target_vision_frames) ])
|
|
||||||
|
|
||||||
if state_manager.get_item('face_selector_mode') == 'many':
|
if state_manager.get_item('face_selector_mode') == 'many':
|
||||||
return sort_and_filter_faces(source_faces, target_faces)
|
return sort_and_filter_faces(target_faces)
|
||||||
|
|
||||||
if state_manager.get_item('face_selector_mode') == 'one':
|
if state_manager.get_item('face_selector_mode') == 'one':
|
||||||
target_face = get_one_face(sort_and_filter_faces(source_faces, target_faces))
|
target_face = get_one_face(sort_and_filter_faces(target_faces))
|
||||||
if target_face:
|
if target_face:
|
||||||
return [ target_face ]
|
return [ target_face ]
|
||||||
|
|
||||||
if state_manager.get_item('face_selector_mode') == 'reference':
|
if state_manager.get_item('face_selector_mode') == 'reference':
|
||||||
reference_faces = get_static_faces([ reference_vision_frame ])
|
reference_faces = get_many_faces([ reference_vision_frame ])
|
||||||
reference_faces = sort_and_filter_faces(source_faces, reference_faces)
|
reference_faces = sort_and_filter_faces(reference_faces)
|
||||||
reference_face = get_one_face(reference_faces, state_manager.get_item('reference_face_position'))
|
reference_face = get_one_face(reference_faces, state_manager.get_item('reference_face_position'))
|
||||||
|
|
||||||
if reference_face:
|
if reference_face:
|
||||||
match_faces = find_match_faces([ reference_face ], target_faces, state_manager.get_item('reference_face_distance'))
|
match_faces = find_match_faces([ reference_face ], target_faces, state_manager.get_item('reference_face_distance'))
|
||||||
return match_faces
|
return match_faces
|
||||||
@@ -62,33 +53,17 @@ def calculate_face_distance(face : Face, reference_face : Face) -> float:
|
|||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def sort_and_filter_faces(source_faces : List[Face], target_faces : List[Face]) -> List[Face]:
|
def sort_and_filter_faces(faces : List[Face]) -> List[Face]:
|
||||||
if target_faces:
|
if faces:
|
||||||
if state_manager.get_item('face_selector_order'):
|
if state_manager.get_item('face_selector_order'):
|
||||||
target_faces = sort_faces_by_order(target_faces, 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'):
|
||||||
face_selector_gender = state_manager.get_item('face_selector_gender')
|
faces = filter_faces_by_gender(faces, state_manager.get_item('face_selector_gender'))
|
||||||
face_selector_race = state_manager.get_item('face_selector_race')
|
if state_manager.get_item('face_selector_race'):
|
||||||
|
faces = filter_faces_by_race(faces, state_manager.get_item('face_selector_race'))
|
||||||
if source_faces and face_selector_gender == 'auto' or face_selector_race == 'auto':
|
|
||||||
source_face = get_first(sort_faces_by_order(source_faces, 'large-small'))
|
|
||||||
|
|
||||||
if source_face:
|
|
||||||
if face_selector_gender == 'auto':
|
|
||||||
face_selector_gender = source_face.gender
|
|
||||||
if face_selector_race == 'auto':
|
|
||||||
face_selector_race = source_face.race
|
|
||||||
|
|
||||||
if face_selector_gender in facefusion.choices.genders:
|
|
||||||
target_faces = filter_faces_by_gender(target_faces, face_selector_gender)
|
|
||||||
|
|
||||||
if face_selector_race in facefusion.choices.races:
|
|
||||||
target_faces = filter_faces_by_race(target_faces, face_selector_race)
|
|
||||||
|
|
||||||
if state_manager.get_item('face_selector_age_start') or state_manager.get_item('face_selector_age_end'):
|
if state_manager.get_item('face_selector_age_start') or state_manager.get_item('face_selector_age_end'):
|
||||||
target_faces = filter_faces_by_age(target_faces, state_manager.get_item('face_selector_age_start'), 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
|
||||||
return target_faces
|
|
||||||
|
|
||||||
|
|
||||||
def sort_faces_by_order(faces : List[Face], order : FaceSelectorOrder) -> List[Face]:
|
def sort_faces_by_order(faces : List[Face], order : FaceSelectorOrder) -> List[Face]:
|
||||||
|
|||||||
+17
-31
@@ -1,42 +1,28 @@
|
|||||||
import threading
|
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
|
|
||||||
import numpy
|
|
||||||
|
|
||||||
from facefusion.hash_helper import create_hash
|
from facefusion.hash_helper import create_hash
|
||||||
from facefusion.types import Face, FaceStore, VisionFrame
|
from facefusion.types import Face, FaceStore, VisionFrame
|
||||||
|
|
||||||
FACE_STORE : FaceStore = {}
|
FACE_STORE : FaceStore =\
|
||||||
|
|
||||||
|
|
||||||
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 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()
|
'static_faces': {}
|
||||||
})['faces'] = faces
|
}
|
||||||
|
|
||||||
|
|
||||||
def resolve_lock(vision_frame : VisionFrame) -> threading.Lock:
|
def get_face_store() -> FaceStore:
|
||||||
if numpy.any(vision_frame):
|
return FACE_STORE
|
||||||
|
|
||||||
|
|
||||||
|
def get_static_faces(vision_frame : VisionFrame) -> Optional[List[Face]]:
|
||||||
vision_hash = create_hash(vision_frame.tobytes())
|
vision_hash = create_hash(vision_frame.tobytes())
|
||||||
return FACE_STORE.setdefault(vision_hash,
|
return FACE_STORE.get('static_faces').get(vision_hash)
|
||||||
{
|
|
||||||
'lock': threading.Lock()
|
|
||||||
}).get('lock')
|
|
||||||
return threading.Lock()
|
|
||||||
|
|
||||||
|
|
||||||
def clear_faces() -> None:
|
def set_static_faces(vision_frame : VisionFrame, faces : List[Face]) -> None:
|
||||||
FACE_STORE.clear()
|
vision_hash = create_hash(vision_frame.tobytes())
|
||||||
|
if vision_hash:
|
||||||
|
FACE_STORE['static_faces'][vision_hash] = faces
|
||||||
|
|
||||||
|
|
||||||
|
def clear_static_faces() -> None:
|
||||||
|
FACE_STORE['static_faces'].clear()
|
||||||
|
|||||||
@@ -1,61 +0,0 @@
|
|||||||
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
|
|
||||||
+18
-98
@@ -1,7 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import tempfile
|
import tempfile
|
||||||
from functools import lru_cache, partial
|
from functools import partial
|
||||||
from typing import List, Optional, cast
|
from typing import List, Optional, cast
|
||||||
|
|
||||||
from tqdm import tqdm
|
from tqdm import tqdm
|
||||||
@@ -10,11 +10,11 @@ import facefusion.choices
|
|||||||
from facefusion import ffmpeg_builder, logger, process_manager, state_manager, translator
|
from facefusion import ffmpeg_builder, logger, process_manager, state_manager, translator
|
||||||
from facefusion.filesystem import get_file_format, remove_file
|
from facefusion.filesystem import get_file_format, remove_file
|
||||||
from facefusion.temp_helper import get_temp_file_path, get_temp_frames_pattern
|
from facefusion.temp_helper import get_temp_file_path, get_temp_frames_pattern
|
||||||
from facefusion.types import ApiSecurityStrategy, AudioEncoder, Buffer, Command, EncoderSet, Fps, Resolution, SampleRate, UpdateProgress, VideoEncoder, VideoFormat
|
from facefusion.types import AudioBuffer, AudioEncoder, Command, EncoderSet, Fps, Resolution, UpdateProgress, VideoEncoder, VideoFormat
|
||||||
from facefusion.vision import detect_video_duration, detect_video_fps, pack_resolution, predict_video_frame_total
|
from facefusion.vision import detect_video_duration, detect_video_fps, pack_resolution, predict_video_frame_total
|
||||||
|
|
||||||
|
|
||||||
def run_ffmpeg_with_progress(commands : List[Command], update_progress : UpdateProgress) -> subprocess.Popen[Buffer]:
|
def run_ffmpeg_with_progress(commands : List[Command], update_progress : UpdateProgress) -> subprocess.Popen[bytes]:
|
||||||
log_level = state_manager.get_item('log_level')
|
log_level = state_manager.get_item('log_level')
|
||||||
commands.extend(ffmpeg_builder.set_progress())
|
commands.extend(ffmpeg_builder.set_progress())
|
||||||
commands.extend(ffmpeg_builder.cast_stream())
|
commands.extend(ffmpeg_builder.cast_stream())
|
||||||
@@ -45,14 +45,7 @@ def update_progress(progress : tqdm, frame_number : int) -> None:
|
|||||||
progress.update(frame_number - progress.n)
|
progress.update(frame_number - progress.n)
|
||||||
|
|
||||||
|
|
||||||
def run_ffmpeg_with_pipe(commands : List[Command], file_content : Buffer) -> subprocess.Popen[Buffer]:
|
def run_ffmpeg(commands : List[Command]) -> subprocess.Popen[bytes]:
|
||||||
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')
|
log_level = state_manager.get_item('log_level')
|
||||||
commands = ffmpeg_builder.run(commands)
|
commands = ffmpeg_builder.run(commands)
|
||||||
process = subprocess.Popen(commands, stderr = subprocess.PIPE, stdout = subprocess.PIPE)
|
process = subprocess.Popen(commands, stderr = subprocess.PIPE, stdout = subprocess.PIPE)
|
||||||
@@ -72,14 +65,14 @@ def run_ffmpeg(commands : List[Command]) -> subprocess.Popen[Buffer]:
|
|||||||
return process
|
return process
|
||||||
|
|
||||||
|
|
||||||
def open_ffmpeg(commands : List[Command]) -> subprocess.Popen[Buffer]:
|
def open_ffmpeg(commands : List[Command]) -> subprocess.Popen[bytes]:
|
||||||
commands = ffmpeg_builder.run(commands)
|
commands = ffmpeg_builder.run(commands)
|
||||||
return subprocess.Popen(commands, stdin = subprocess.PIPE, stdout = subprocess.PIPE)
|
return subprocess.Popen(commands, stdin = subprocess.PIPE, stdout = subprocess.PIPE)
|
||||||
|
|
||||||
|
|
||||||
def log_debug(process : subprocess.Popen[Buffer]) -> None:
|
def log_debug(process : subprocess.Popen[bytes]) -> None:
|
||||||
_, stderr = process.communicate()
|
_, stderr = process.communicate()
|
||||||
errors = stderr.decode().splitlines()
|
errors = stderr.decode().split(os.linesep)
|
||||||
|
|
||||||
for error in errors:
|
for error in errors:
|
||||||
if error.strip():
|
if error.strip():
|
||||||
@@ -90,7 +83,6 @@ def get_available_encoder_set() -> EncoderSet:
|
|||||||
available_encoder_set : EncoderSet =\
|
available_encoder_set : EncoderSet =\
|
||||||
{
|
{
|
||||||
'audio': [],
|
'audio': [],
|
||||||
'image': [],
|
|
||||||
'video': []
|
'video': []
|
||||||
}
|
}
|
||||||
commands = ffmpeg_builder.chain(
|
commands = ffmpeg_builder.chain(
|
||||||
@@ -102,26 +94,19 @@ def get_available_encoder_set() -> EncoderSet:
|
|||||||
if line.startswith(' a'):
|
if line.startswith(' a'):
|
||||||
audio_encoder = line.split()[1]
|
audio_encoder = line.split()[1]
|
||||||
|
|
||||||
if audio_encoder in facefusion.choices.audio_encoders and audio_encoder not in available_encoder_set.get('audio'):
|
if audio_encoder in facefusion.choices.output_audio_encoders:
|
||||||
available_encoder_set['audio'].append(audio_encoder) #type:ignore[arg-type]
|
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'):
|
if line.startswith(' v'):
|
||||||
vision_encoder = line.split()[1]
|
video_encoder = line.split()[1]
|
||||||
|
|
||||||
if vision_encoder in facefusion.choices.image_encoders and vision_encoder not in available_encoder_set.get('image'):
|
if video_encoder in facefusion.choices.output_video_encoders:
|
||||||
available_encoder_set['image'].append(vision_encoder) #type:ignore[arg-type]
|
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 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
|
return available_encoder_set
|
||||||
|
|
||||||
|
|
||||||
@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:
|
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)
|
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(state_manager.get_temp_path(), output_path, state_manager.get_item('temp_frame_format'), '%08d')
|
temp_frames_pattern = get_temp_frames_pattern(state_manager.get_temp_path(), output_path, state_manager.get_item('temp_frame_format'), '%08d')
|
||||||
@@ -129,10 +114,8 @@ def extract_frames(target_path : str, output_path : str, temp_video_resolution :
|
|||||||
ffmpeg_builder.set_input(target_path),
|
ffmpeg_builder.set_input(target_path),
|
||||||
ffmpeg_builder.set_media_resolution(pack_resolution(temp_video_resolution)),
|
ffmpeg_builder.set_media_resolution(pack_resolution(temp_video_resolution)),
|
||||||
ffmpeg_builder.set_frame_quality(0),
|
ffmpeg_builder.set_frame_quality(0),
|
||||||
ffmpeg_builder.enforce_pixel_format('rgb24'),
|
|
||||||
ffmpeg_builder.select_frame_range(trim_frame_start, trim_frame_end, temp_video_fps),
|
ffmpeg_builder.select_frame_range(trim_frame_start, trim_frame_end, temp_video_fps),
|
||||||
ffmpeg_builder.prevent_frame_drop(),
|
ffmpeg_builder.prevent_frame_drop(),
|
||||||
ffmpeg_builder.set_start_number(trim_frame_start),
|
|
||||||
ffmpeg_builder.set_output(temp_frames_pattern)
|
ffmpeg_builder.set_output(temp_frames_pattern)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -182,7 +165,7 @@ def finalize_image(output_path : str, output_image_resolution : Resolution) -> b
|
|||||||
return run_ffmpeg(commands).returncode == 0
|
return run_ffmpeg(commands).returncode == 0
|
||||||
|
|
||||||
|
|
||||||
def read_audio_buffer(target_path : str, audio_sample_rate : SampleRate, audio_sample_size : int, audio_channel_total : int) -> Optional[Buffer]:
|
def read_audio_buffer(target_path : str, audio_sample_rate : int, audio_sample_size : int, audio_channel_total : int) -> Optional[AudioBuffer]:
|
||||||
commands = ffmpeg_builder.chain(
|
commands = ffmpeg_builder.chain(
|
||||||
ffmpeg_builder.set_input(target_path),
|
ffmpeg_builder.set_input(target_path),
|
||||||
ffmpeg_builder.ignore_video_stream(),
|
ffmpeg_builder.ignore_video_stream(),
|
||||||
@@ -194,7 +177,6 @@ def read_audio_buffer(target_path : str, audio_sample_rate : SampleRate, audio_s
|
|||||||
|
|
||||||
process = open_ffmpeg(commands)
|
process = open_ffmpeg(commands)
|
||||||
audio_buffer, _ = process.communicate()
|
audio_buffer, _ = process.communicate()
|
||||||
|
|
||||||
if process.returncode == 0:
|
if process.returncode == 0:
|
||||||
return audio_buffer
|
return audio_buffer
|
||||||
return None
|
return None
|
||||||
@@ -208,7 +190,6 @@ def restore_audio(target_path : str, output_path : str, trim_frame_start : int,
|
|||||||
temp_video_path = get_temp_file_path(state_manager.get_temp_path(), output_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_format = cast(VideoFormat, get_file_format(output_path))
|
||||||
temp_video_duration = detect_video_duration(temp_video_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)
|
output_audio_encoder = fix_audio_encoder(temp_video_format, output_audio_encoder)
|
||||||
commands = ffmpeg_builder.chain(
|
commands = ffmpeg_builder.chain(
|
||||||
@@ -222,7 +203,6 @@ def restore_audio(target_path : str, output_path : str, trim_frame_start : int,
|
|||||||
ffmpeg_builder.select_media_stream('0:v:0'),
|
ffmpeg_builder.select_media_stream('0:v:0'),
|
||||||
ffmpeg_builder.select_media_stream('1:a:0'),
|
ffmpeg_builder.select_media_stream('1:a:0'),
|
||||||
ffmpeg_builder.set_video_duration(temp_video_duration),
|
ffmpeg_builder.set_video_duration(temp_video_duration),
|
||||||
ffmpeg_builder.set_faststart(output_video_format),
|
|
||||||
ffmpeg_builder.force_output(output_path)
|
ffmpeg_builder.force_output(output_path)
|
||||||
)
|
)
|
||||||
return run_ffmpeg(commands).returncode == 0
|
return run_ffmpeg(commands).returncode == 0
|
||||||
@@ -235,7 +215,6 @@ def replace_audio(audio_path : str, output_path : str) -> bool:
|
|||||||
temp_video_path = get_temp_file_path(state_manager.get_temp_path(), output_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_format = cast(VideoFormat, get_file_format(output_path))
|
||||||
temp_video_duration = detect_video_duration(temp_video_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)
|
output_audio_encoder = fix_audio_encoder(temp_video_format, output_audio_encoder)
|
||||||
commands = ffmpeg_builder.chain(
|
commands = ffmpeg_builder.chain(
|
||||||
@@ -246,13 +225,13 @@ def replace_audio(audio_path : str, output_path : str) -> bool:
|
|||||||
ffmpeg_builder.set_audio_quality(output_audio_encoder, output_audio_quality),
|
ffmpeg_builder.set_audio_quality(output_audio_encoder, output_audio_quality),
|
||||||
ffmpeg_builder.set_audio_volume(output_audio_volume),
|
ffmpeg_builder.set_audio_volume(output_audio_volume),
|
||||||
ffmpeg_builder.set_video_duration(temp_video_duration),
|
ffmpeg_builder.set_video_duration(temp_video_duration),
|
||||||
ffmpeg_builder.set_faststart(output_video_format),
|
|
||||||
ffmpeg_builder.force_output(output_path)
|
ffmpeg_builder.force_output(output_path)
|
||||||
)
|
)
|
||||||
return run_ffmpeg(commands).returncode == 0
|
return run_ffmpeg(commands).returncode == 0
|
||||||
|
|
||||||
|
|
||||||
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:
|
def merge_video(target_path : str, output_path : str, temp_video_fps : Fps, output_video_resolution : Resolution, trim_frame_start : int, trim_frame_end : int) -> bool:
|
||||||
|
output_video_fps = state_manager.get_item('output_video_fps')
|
||||||
output_video_encoder = state_manager.get_item('output_video_encoder')
|
output_video_encoder = state_manager.get_item('output_video_encoder')
|
||||||
output_video_quality = state_manager.get_item('output_video_quality')
|
output_video_quality = state_manager.get_item('output_video_quality')
|
||||||
output_video_preset = state_manager.get_item('output_video_preset')
|
output_video_preset = state_manager.get_item('output_video_preset')
|
||||||
@@ -264,11 +243,9 @@ def merge_video(target_path : str, output_path : str, temp_video_fps : Fps, outp
|
|||||||
output_video_encoder = fix_video_encoder(temp_video_format, output_video_encoder)
|
output_video_encoder = fix_video_encoder(temp_video_format, output_video_encoder)
|
||||||
commands = ffmpeg_builder.chain(
|
commands = ffmpeg_builder.chain(
|
||||||
ffmpeg_builder.set_input_fps(temp_video_fps),
|
ffmpeg_builder.set_input_fps(temp_video_fps),
|
||||||
ffmpeg_builder.set_start_number(trim_frame_start),
|
|
||||||
ffmpeg_builder.set_input(temp_frames_pattern),
|
ffmpeg_builder.set_input(temp_frames_pattern),
|
||||||
ffmpeg_builder.set_media_resolution(pack_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_encoder(output_video_encoder),
|
||||||
ffmpeg_builder.set_video_tag(output_video_encoder, temp_video_format),
|
|
||||||
ffmpeg_builder.set_video_quality(output_video_encoder, output_video_quality),
|
ffmpeg_builder.set_video_quality(output_video_encoder, output_video_quality),
|
||||||
ffmpeg_builder.set_video_preset(output_video_encoder, output_video_preset),
|
ffmpeg_builder.set_video_preset(output_video_encoder, output_video_preset),
|
||||||
ffmpeg_builder.concat(
|
ffmpeg_builder.concat(
|
||||||
@@ -285,8 +262,7 @@ def merge_video(target_path : str, output_path : str, temp_video_fps : Fps, outp
|
|||||||
|
|
||||||
|
|
||||||
def concat_video(output_path : str, temp_output_paths : List[str]) -> bool:
|
def concat_video(output_path : str, temp_output_paths : List[str]) -> bool:
|
||||||
file_descriptor, concat_video_path = tempfile.mkstemp()
|
concat_video_path = tempfile.mktemp()
|
||||||
os.close(file_descriptor)
|
|
||||||
|
|
||||||
with open(concat_video_path, 'w') as concat_video_file:
|
with open(concat_video_path, 'w') as concat_video_file:
|
||||||
for temp_output_path in temp_output_paths:
|
for temp_output_path in temp_output_paths:
|
||||||
@@ -295,13 +271,11 @@ def concat_video(output_path : str, temp_output_paths : List[str]) -> bool:
|
|||||||
concat_video_file.close()
|
concat_video_file.close()
|
||||||
|
|
||||||
output_path = os.path.abspath(output_path)
|
output_path = os.path.abspath(output_path)
|
||||||
output_video_format = cast(VideoFormat, get_file_format(output_path))
|
|
||||||
commands = ffmpeg_builder.chain(
|
commands = ffmpeg_builder.chain(
|
||||||
ffmpeg_builder.unsafe_concat(),
|
ffmpeg_builder.unsafe_concat(),
|
||||||
ffmpeg_builder.set_input(concat_video_file.name),
|
ffmpeg_builder.set_input(concat_video_file.name),
|
||||||
ffmpeg_builder.copy_video_encoder(),
|
ffmpeg_builder.copy_video_encoder(),
|
||||||
ffmpeg_builder.copy_audio_encoder(),
|
ffmpeg_builder.copy_audio_encoder(),
|
||||||
ffmpeg_builder.set_faststart(output_video_format),
|
|
||||||
ffmpeg_builder.force_output(output_path)
|
ffmpeg_builder.force_output(output_path)
|
||||||
)
|
)
|
||||||
process = run_ffmpeg(commands)
|
process = run_ffmpeg(commands)
|
||||||
@@ -310,60 +284,6 @@ def concat_video(output_path : str, temp_output_paths : List[str]) -> bool:
|
|||||||
return process.returncode == 0
|
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:
|
def fix_audio_encoder(video_format : VideoFormat, audio_encoder : AudioEncoder) -> AudioEncoder:
|
||||||
if video_format == 'avi' and audio_encoder == 'libopus':
|
if video_format == 'avi' and audio_encoder == 'libopus':
|
||||||
return 'aac'
|
return 'aac'
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ from typing import List, Optional
|
|||||||
import numpy
|
import numpy
|
||||||
|
|
||||||
from facefusion.filesystem import get_file_format
|
from facefusion.filesystem import get_file_format
|
||||||
from facefusion.types import AudioEncoder, Command, CommandSet, Duration, Fps, SampleRate, StreamMode, VideoEncoder, VideoFormat, VideoPreset
|
from facefusion.types import AudioEncoder, Command, CommandSet, Duration, Fps, StreamMode, VideoEncoder, VideoPreset
|
||||||
|
|
||||||
|
|
||||||
def run(commands : List[Command]) -> List[Command]:
|
def run(commands : List[Command]) -> List[Command]:
|
||||||
@@ -51,10 +51,6 @@ def set_input_fps(input_fps : Fps) -> List[Command]:
|
|||||||
return [ '-r', str(input_fps)]
|
return [ '-r', str(input_fps)]
|
||||||
|
|
||||||
|
|
||||||
def set_start_number(frame_number : int) -> List[Command]:
|
|
||||||
return [ '-start_number', str(frame_number) ]
|
|
||||||
|
|
||||||
|
|
||||||
def set_output(output_path : str) -> List[Command]:
|
def set_output(output_path : str) -> List[Command]:
|
||||||
return [ output_path ]
|
return [ output_path ]
|
||||||
|
|
||||||
@@ -87,14 +83,6 @@ def unsafe_concat() -> List[Command]:
|
|||||||
return [ '-f', 'concat', '-safe', '0' ]
|
return [ '-f', 'concat', '-safe', '0' ]
|
||||||
|
|
||||||
|
|
||||||
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]:
|
def set_pixel_format(video_encoder : VideoEncoder) -> List[Command]:
|
||||||
if video_encoder == 'rawvideo':
|
if video_encoder == 'rawvideo':
|
||||||
return [ '-pix_fmt', 'rgb24' ]
|
return [ '-pix_fmt', 'rgb24' ]
|
||||||
@@ -139,8 +127,12 @@ def set_media_resolution(video_resolution : str) -> List[Command]:
|
|||||||
return [ '-s', video_resolution ]
|
return [ '-s', video_resolution ]
|
||||||
|
|
||||||
|
|
||||||
def deep_copy_audio() -> List[Command]:
|
def set_image_quality(image_path : str, image_quality : int) -> List[Command]:
|
||||||
return [ '-q:a', '0' ]
|
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 set_audio_encoder(audio_codec : str) -> List[Command]:
|
def set_audio_encoder(audio_codec : str) -> List[Command]:
|
||||||
@@ -151,7 +143,7 @@ def copy_audio_encoder() -> List[Command]:
|
|||||||
return set_audio_encoder('copy')
|
return set_audio_encoder('copy')
|
||||||
|
|
||||||
|
|
||||||
def set_audio_sample_rate(audio_sample_rate : SampleRate) -> List[Command]:
|
def set_audio_sample_rate(audio_sample_rate : int) -> List[Command]:
|
||||||
return [ '-ar', str(audio_sample_rate) ]
|
return [ '-ar', str(audio_sample_rate) ]
|
||||||
|
|
||||||
|
|
||||||
@@ -187,22 +179,6 @@ def set_audio_volume(audio_volume : int) -> List[Command]:
|
|||||||
return [ '-filter:a', 'volume=' + str(audio_volume / 100) ]
|
return [ '-filter:a', 'volume=' + str(audio_volume / 100) ]
|
||||||
|
|
||||||
|
|
||||||
def 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]:
|
def set_video_encoder(video_encoder : str) -> List[Command]:
|
||||||
return [ '-c:v', video_encoder ]
|
return [ '-c:v', video_encoder ]
|
||||||
|
|
||||||
@@ -211,18 +187,6 @@ def copy_video_encoder() -> List[Command]:
|
|||||||
return set_video_encoder('copy')
|
return set_video_encoder('copy')
|
||||||
|
|
||||||
|
|
||||||
def set_faststart(video_format : VideoFormat) -> List[Command]:
|
|
||||||
if video_format in [ 'm4v', 'mov', 'mp4' ]:
|
|
||||||
return [ '-movflags', '+faststart' ]
|
|
||||||
return []
|
|
||||||
|
|
||||||
|
|
||||||
def set_video_tag(video_encoder : VideoEncoder, video_format : VideoFormat) -> List[Command]:
|
|
||||||
if video_format in [ 'm4v', 'mov', 'mp4' ] and video_encoder in [ 'libx265', 'hevc_nvenc', 'hevc_amf', 'hevc_qsv', 'hevc_videotoolbox' ]:
|
|
||||||
return [ '-tag:v', 'hvc1' ]
|
|
||||||
return []
|
|
||||||
|
|
||||||
|
|
||||||
def set_video_quality(video_encoder : VideoEncoder, video_quality : int) -> List[Command]:
|
def set_video_quality(video_encoder : VideoEncoder, video_quality : int) -> List[Command]:
|
||||||
if video_encoder in [ 'libx264', 'libx264rgb', 'libx265' ]:
|
if video_encoder in [ 'libx264', 'libx264rgb', 'libx265' ]:
|
||||||
video_compression = numpy.round(numpy.interp(video_quality, [ 0, 100 ], [ 51, 0 ])).astype(int).item()
|
video_compression = numpy.round(numpy.interp(video_quality, [ 0, 100 ], [ 51, 0 ])).astype(int).item()
|
||||||
@@ -305,5 +269,3 @@ def map_qsv_preset(video_preset : VideoPreset) -> Optional[str]:
|
|||||||
if video_preset in [ 'faster', 'fast', 'medium', 'slow', 'slower', 'veryslow' ]:
|
if video_preset in [ 'faster', 'fast', 'medium', 'slow', 'slower', 'veryslow' ]:
|
||||||
return video_preset
|
return video_preset
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,84 +0,0 @@
|
|||||||
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
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
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 ]
|
|
||||||
@@ -170,13 +170,6 @@ def create_directory(directory_path : str) -> bool:
|
|||||||
return False
|
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:
|
def remove_directory(directory_path : str) -> bool:
|
||||||
if is_directory(directory_path):
|
if is_directory(directory_path):
|
||||||
shutil.rmtree(directory_path, ignore_errors = True)
|
shutil.rmtree(directory_path, ignore_errors = True)
|
||||||
|
|||||||
@@ -3,11 +3,10 @@ import zlib
|
|||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from facefusion.filesystem import get_file_name, is_file
|
from facefusion.filesystem import get_file_name, is_file
|
||||||
from facefusion.types import Buffer
|
|
||||||
|
|
||||||
|
|
||||||
def create_hash(buffer : Buffer) -> str:
|
def create_hash(content : bytes) -> str:
|
||||||
return format(zlib.crc32(buffer), '08x')
|
return format(zlib.crc32(content), '08x')
|
||||||
|
|
||||||
|
|
||||||
def validate_hash(validate_path : str) -> bool:
|
def validate_hash(validate_path : str) -> bool:
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import importlib
|
import importlib
|
||||||
import random
|
import random
|
||||||
from functools import lru_cache
|
|
||||||
from time import sleep, time
|
from time import sleep, time
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
@@ -9,11 +8,11 @@ from onnxruntime import InferenceSession
|
|||||||
from facefusion import logger, process_manager, state_manager, translator
|
from facefusion import logger, process_manager, state_manager, translator
|
||||||
from facefusion.app_context import detect_app_context
|
from facefusion.app_context import detect_app_context
|
||||||
from facefusion.common_helper import is_windows
|
from facefusion.common_helper import is_windows
|
||||||
from facefusion.execution import create_inference_providers, has_execution_provider
|
from facefusion.execution import create_inference_session_providers, has_execution_provider
|
||||||
from facefusion.exit_helper import fatal_exit
|
from facefusion.exit_helper import fatal_exit
|
||||||
from facefusion.filesystem import get_file_name, is_file
|
from facefusion.filesystem import get_file_name, is_file
|
||||||
from facefusion.time_helper import calculate_end_time
|
from facefusion.time_helper import calculate_end_time
|
||||||
from facefusion.types import DownloadSet, ExecutionProvider, InferencePool, InferencePoolSet, InferenceProvider
|
from facefusion.types import DownloadSet, ExecutionProvider, InferencePool, InferencePoolSet
|
||||||
|
|
||||||
INFERENCE_POOL_SET : InferencePoolSet =\
|
INFERENCE_POOL_SET : InferencePoolSet =\
|
||||||
{
|
{
|
||||||
@@ -26,7 +25,7 @@ def get_inference_pool(module_name : str, model_names : List[str], model_source_
|
|||||||
while process_manager.is_checking():
|
while process_manager.is_checking():
|
||||||
sleep(0.5)
|
sleep(0.5)
|
||||||
execution_device_ids = state_manager.get_item('execution_device_ids')
|
execution_device_ids = state_manager.get_item('execution_device_ids')
|
||||||
execution_providers = state_manager.get_item('execution_providers')
|
execution_providers = resolve_execution_providers(module_name)
|
||||||
app_context = detect_app_context()
|
app_context = detect_app_context()
|
||||||
|
|
||||||
for execution_device_id in execution_device_ids:
|
for execution_device_id in execution_device_ids:
|
||||||
@@ -37,27 +36,26 @@ def get_inference_pool(module_name : str, model_names : List[str], model_source_
|
|||||||
if app_context == 'api' and INFERENCE_POOL_SET.get('cli').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)
|
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):
|
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, execution_device_id, execution_providers)
|
||||||
INFERENCE_POOL_SET[app_context][inference_context] = create_inference_pool(model_source_set, inference_providers)
|
|
||||||
|
|
||||||
current_inference_context = get_inference_context(module_name, model_names, random.choice(execution_device_ids), execution_providers)
|
current_inference_context = get_inference_context(module_name, model_names, random.choice(execution_device_ids), execution_providers)
|
||||||
return INFERENCE_POOL_SET.get(app_context).get(current_inference_context)
|
return INFERENCE_POOL_SET.get(app_context).get(current_inference_context)
|
||||||
|
|
||||||
|
|
||||||
def create_inference_pool(model_source_set : DownloadSet, inference_providers : List[InferenceProvider]) -> InferencePool:
|
def create_inference_pool(model_source_set : DownloadSet, execution_device_id : int, execution_providers : List[ExecutionProvider]) -> InferencePool:
|
||||||
inference_pool : InferencePool = {}
|
inference_pool : InferencePool = {}
|
||||||
|
|
||||||
for model_name in model_source_set.keys():
|
for model_name in model_source_set.keys():
|
||||||
model_path = model_source_set.get(model_name).get('path')
|
model_path = model_source_set.get(model_name).get('path')
|
||||||
if is_file(model_path):
|
if is_file(model_path):
|
||||||
inference_pool[model_name] = create_inference_session(model_path, inference_providers)
|
inference_pool[model_name] = create_inference_session(model_path, execution_device_id, execution_providers)
|
||||||
|
|
||||||
return inference_pool
|
return inference_pool
|
||||||
|
|
||||||
|
|
||||||
def clear_inference_pool(module_name : str, model_names : List[str]) -> None:
|
def clear_inference_pool(module_name : str, model_names : List[str]) -> None:
|
||||||
execution_device_ids = state_manager.get_item('execution_device_ids')
|
execution_device_ids = state_manager.get_item('execution_device_ids')
|
||||||
execution_providers = state_manager.get_item('execution_providers')
|
execution_providers = resolve_execution_providers(module_name)
|
||||||
app_context = detect_app_context()
|
app_context = detect_app_context()
|
||||||
|
|
||||||
if is_windows() and has_execution_provider('directml'):
|
if is_windows() and has_execution_provider('directml'):
|
||||||
@@ -69,12 +67,13 @@ def clear_inference_pool(module_name : str, model_names : List[str]) -> None:
|
|||||||
del INFERENCE_POOL_SET[app_context][inference_context]
|
del INFERENCE_POOL_SET[app_context][inference_context]
|
||||||
|
|
||||||
|
|
||||||
def create_inference_session(model_path : str, inference_providers : List[InferenceProvider]) -> InferenceSession:
|
def create_inference_session(model_path : str, execution_device_id : int, execution_providers : List[ExecutionProvider]) -> InferenceSession:
|
||||||
model_file_name = get_file_name(model_path)
|
model_file_name = get_file_name(model_path)
|
||||||
start_time = time()
|
start_time = time()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
inference_session = InferenceSession(model_path, providers = inference_providers)
|
inference_session_providers = create_inference_session_providers(execution_device_id, execution_providers)
|
||||||
|
inference_session = InferenceSession(model_path, providers = inference_session_providers)
|
||||||
logger.debug(translator.get('loading_model_succeeded').format(model_name = model_file_name, seconds = calculate_end_time(start_time)), __name__)
|
logger.debug(translator.get('loading_model_succeeded').format(model_name = model_file_name, seconds = calculate_end_time(start_time)), __name__)
|
||||||
return inference_session
|
return inference_session
|
||||||
|
|
||||||
@@ -88,15 +87,9 @@ def get_inference_context(module_name : str, model_names : List[str], execution_
|
|||||||
return inference_context
|
return inference_context
|
||||||
|
|
||||||
|
|
||||||
@lru_cache()
|
def resolve_execution_providers(module_name : str) -> List[ExecutionProvider]:
|
||||||
def resolve_static_inference_providers(module_name : str, execution_device_id : int) -> List[InferenceProvider]:
|
|
||||||
module = importlib.import_module(module_name)
|
module = importlib.import_module(module_name)
|
||||||
execution_providers = state_manager.get_item('execution_providers')
|
|
||||||
|
|
||||||
if hasattr(module, 'resolve_inference_providers'):
|
if hasattr(module, 'resolve_execution_providers'):
|
||||||
inference_providers = getattr(module, 'resolve_inference_providers')()
|
return getattr(module, 'resolve_execution_providers')()
|
||||||
|
return state_manager.get_item('execution_providers')
|
||||||
if inference_providers:
|
|
||||||
return inference_providers
|
|
||||||
|
|
||||||
return create_inference_providers(execution_device_id, execution_providers)
|
|
||||||
|
|||||||
+49
-23
@@ -10,34 +10,30 @@ from types import FrameType
|
|||||||
from facefusion import metadata
|
from facefusion import metadata
|
||||||
from facefusion.common_helper import is_linux, is_windows
|
from facefusion.common_helper import is_linux, is_windows
|
||||||
|
|
||||||
LOCALES =\
|
LOCALS =\
|
||||||
{
|
{
|
||||||
'install_dependency': 'install the {dependency} package',
|
'install_dependency': 'install the {dependency} package',
|
||||||
'force_reinstall': 'force reinstall of packages',
|
|
||||||
'skip_conda': 'skip the conda environment check',
|
'skip_conda': 'skip the conda environment check',
|
||||||
'conda_not_activated': 'conda is not activated'
|
'conda_not_activated': 'conda is not activated'
|
||||||
}
|
}
|
||||||
ONNXRUNTIME_SET =\
|
ONNXRUNTIME_SET =\
|
||||||
{
|
{
|
||||||
'default': ('onnxruntime', '1.26.0')
|
'default': ('onnxruntime', '1.23.2')
|
||||||
}
|
}
|
||||||
if is_windows() or is_linux():
|
if is_windows() or is_linux():
|
||||||
ONNXRUNTIME_SET['cuda'] = ('onnxruntime-gpu', '1.26.0')
|
ONNXRUNTIME_SET['cuda'] = ('onnxruntime-gpu', '1.23.2')
|
||||||
ONNXRUNTIME_SET['openvino'] = ('onnxruntime-openvino', '1.24.1')
|
ONNXRUNTIME_SET['openvino'] = ('onnxruntime-openvino', '1.23.0')
|
||||||
if is_windows():
|
if is_windows():
|
||||||
ONNXRUNTIME_SET['directml'] = ('onnxruntime-directml', '1.24.4')
|
ONNXRUNTIME_SET['directml'] = ('onnxruntime-directml', '1.23.0')
|
||||||
ONNXRUNTIME_SET['qnn'] = ('onnxruntime-qnn', '1.24.4')
|
|
||||||
if is_linux():
|
if is_linux():
|
||||||
ONNXRUNTIME_SET['migraphx'] = ('onnxruntime-migraphx', '1.25.0')
|
ONNXRUNTIME_SET['rocm'] = ('onnxruntime-rocm', '1.21.0')
|
||||||
ONNXRUNTIME_SET['rocm'] = ('onnxruntime-rocm', '1.22.2.post1')
|
|
||||||
|
|
||||||
|
|
||||||
def cli() -> None:
|
def cli() -> None:
|
||||||
signal.signal(signal.SIGINT, signal_exit)
|
signal.signal(signal.SIGINT, signal_exit)
|
||||||
program = ArgumentParser(formatter_class = partial(HelpFormatter, max_help_position = 50))
|
program = ArgumentParser(formatter_class = partial(HelpFormatter, max_help_position = 50))
|
||||||
program.add_argument('onnxruntime', help = LOCALES.get('install_dependency').format(dependency = 'onnxruntime'), choices = ONNXRUNTIME_SET.keys())
|
program.add_argument('--onnxruntime', help = LOCALS.get('install_dependency').format(dependency = 'onnxruntime'), choices = ONNXRUNTIME_SET.keys(), required = True)
|
||||||
program.add_argument('--force-reinstall', help = LOCALES.get('force_reinstall'), action = 'store_true')
|
program.add_argument('--skip-conda', help = LOCALS.get('skip_conda'), action = 'store_true')
|
||||||
program.add_argument('--skip-conda', help = LOCALES.get('skip_conda'), action = 'store_true')
|
|
||||||
program.add_argument('-v', '--version', version = metadata.get('name') + ' ' + metadata.get('version'), action = 'version')
|
program.add_argument('-v', '--version', version = metadata.get('name') + ' ' + metadata.get('version'), action = 'version')
|
||||||
run(program)
|
run(program)
|
||||||
|
|
||||||
@@ -49,26 +45,56 @@ def signal_exit(signum : int, frame : FrameType) -> None:
|
|||||||
def run(program : ArgumentParser) -> None:
|
def run(program : ArgumentParser) -> None:
|
||||||
args = program.parse_args()
|
args = program.parse_args()
|
||||||
has_conda = 'CONDA_PREFIX' in os.environ
|
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:
|
if not args.skip_conda and not has_conda:
|
||||||
sys.stdout.write(LOCALES.get('conda_not_activated') + os.linesep)
|
sys.stdout.write(LOCALS.get('conda_not_activated') + os.linesep)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
commands = [ shutil.which('pip'), 'install' ]
|
|
||||||
|
|
||||||
if args.force_reinstall:
|
|
||||||
commands.append('--force-reinstall')
|
|
||||||
|
|
||||||
with open('requirements.txt') as file:
|
with open('requirements.txt') as file:
|
||||||
|
|
||||||
for line in file.readlines():
|
for line in file.readlines():
|
||||||
__line__ = line.strip()
|
__line__ = line.strip()
|
||||||
if not __line__.startswith('onnxruntime'):
|
if not __line__.startswith('onnxruntime'):
|
||||||
commands.append(__line__)
|
subprocess.call([ shutil.which('pip'), 'install', line, '--force-reinstall' ])
|
||||||
|
|
||||||
onnxruntime_name, onnxruntime_version = ONNXRUNTIME_SET.get(args.onnxruntime)
|
if args.onnxruntime == 'rocm':
|
||||||
commands.append(onnxruntime_name + '==' + onnxruntime_version)
|
python_id = 'cp' + str(sys.version_info.major) + str(sys.version_info.minor)
|
||||||
|
|
||||||
subprocess.call([ shutil.which('pip'), 'uninstall', 'onnxruntime', onnxruntime_name, '-y', '-q' ])
|
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' ])
|
||||||
|
|
||||||
|
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) ])
|
||||||
|
|
||||||
subprocess.call(commands)
|
|
||||||
|
|||||||
@@ -13,8 +13,6 @@ def get_step_output_path(job_id : str, step_index : int, output_path : str) -> O
|
|||||||
|
|
||||||
if output_file_name and output_file_extension:
|
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)
|
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
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import facefusion.choices
|
|||||||
from facefusion.filesystem import create_directory, get_file_name, is_directory, is_file, move_file, remove_directory, remove_file, resolve_file_pattern
|
from facefusion.filesystem import create_directory, get_file_name, is_directory, is_file, move_file, remove_directory, remove_file, resolve_file_pattern
|
||||||
from facefusion.jobs.job_helper import get_step_output_path
|
from facefusion.jobs.job_helper import get_step_output_path
|
||||||
from facefusion.json import read_json, write_json
|
from facefusion.json import read_json, write_json
|
||||||
from facefusion.sanitizer import sanitize_job_id
|
|
||||||
from facefusion.time_helper import get_current_date_time
|
from facefusion.time_helper import get_current_date_time
|
||||||
from facefusion.types import Args, Job, JobSet, JobStatus, JobStep, JobStepStatus
|
from facefusion.types import Args, Job, JobSet, JobStatus, JobStep, JobStepStatus
|
||||||
|
|
||||||
@@ -262,6 +261,5 @@ def find_job_path(job_id : str) -> Optional[str]:
|
|||||||
|
|
||||||
def get_job_file_name(job_id : str) -> Optional[str]:
|
def get_job_file_name(job_id : str) -> Optional[str]:
|
||||||
if job_id:
|
if job_id:
|
||||||
job_id = sanitize_job_id(job_id)
|
|
||||||
return job_id + '.json'
|
return job_id + '.json'
|
||||||
return None
|
return None
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
import os
|
|
||||||
|
|
||||||
from facefusion.ffmpeg import concat_video
|
from facefusion.ffmpeg import concat_video
|
||||||
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.filesystem import are_images, are_videos, move_file, remove_file
|
||||||
from facefusion.jobs import job_helper, job_manager
|
from facefusion.jobs import job_helper, job_manager
|
||||||
from facefusion.types import JobOutputSet, JobStep, ProcessStep
|
from facefusion.types import JobOutputSet, JobStep, ProcessStep
|
||||||
|
|
||||||
@@ -61,8 +59,6 @@ def run_step(job_id : str, step_index : int, step : JobStep, process_step : Proc
|
|||||||
output_path = step_args.get('output_path')
|
output_path = step_args.get('output_path')
|
||||||
step_output_path = job_helper.get_step_output_path(job_id, step_index, 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')
|
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')
|
job_manager.set_step_status(job_id, step_index, 'failed')
|
||||||
return False
|
return False
|
||||||
@@ -83,26 +79,13 @@ def finalize_steps(job_id : str) -> bool:
|
|||||||
output_set = collect_output_set(job_id)
|
output_set = collect_output_set(job_id)
|
||||||
|
|
||||||
for output_path, temp_output_paths in output_set.items():
|
for output_path, temp_output_paths in output_set.items():
|
||||||
has_videos = are_videos(temp_output_paths)
|
if are_videos(temp_output_paths):
|
||||||
has_images = are_images(temp_output_paths)
|
|
||||||
|
|
||||||
if has_videos:
|
|
||||||
if not concat_video(output_path, temp_output_paths):
|
if not concat_video(output_path, temp_output_paths):
|
||||||
return False
|
return False
|
||||||
if not has_videos and has_images:
|
if are_images(temp_output_paths):
|
||||||
for temp_output_path in temp_output_paths:
|
for temp_output_path in temp_output_paths:
|
||||||
if not move_file(temp_output_path, output_path):
|
if not move_file(temp_output_path, output_path):
|
||||||
return False
|
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
|
return True
|
||||||
|
|
||||||
|
|
||||||
@@ -111,12 +94,8 @@ def clean_steps(job_id: str) -> bool:
|
|||||||
|
|
||||||
for temp_output_paths in output_set.values():
|
for temp_output_paths in output_set.values():
|
||||||
for temp_output_path in temp_output_paths:
|
for temp_output_path in temp_output_paths:
|
||||||
if is_file(temp_output_path):
|
|
||||||
if not remove_file(temp_output_path):
|
if not remove_file(temp_output_path):
|
||||||
return False
|
return False
|
||||||
if is_directory(temp_output_path):
|
|
||||||
if not remove_directory(temp_output_path):
|
|
||||||
return False
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,115 +0,0 @@
|
|||||||
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)
|
|
||||||
]
|
|
||||||
})()
|
|
||||||
@@ -1,88 +0,0 @@
|
|||||||
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)
|
|
||||||
]
|
|
||||||
})()
|
|
||||||
@@ -115,9 +115,6 @@ def init_ctypes(library : ctypes.CDLL) -> ctypes.CDLL:
|
|||||||
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.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_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.argtypes = [ ctypes.c_void_p ]
|
||||||
library.opus_decoder_destroy.restype = None
|
library.opus_decoder_destroy.restype = None
|
||||||
|
|
||||||
|
|||||||
@@ -1,24 +0,0 @@
|
|||||||
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
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
from facefusion.types import Locales
|
from facefusion.types import Locals
|
||||||
|
|
||||||
LOCALES : Locales =\
|
LOCALS : Locals =\
|
||||||
{
|
{
|
||||||
'en':
|
'en':
|
||||||
{
|
{
|
||||||
@@ -40,8 +40,6 @@ LOCALES : Locales =\
|
|||||||
'processing_stopped': 'processing stopped',
|
'processing_stopped': 'processing stopped',
|
||||||
'processing_image_succeeded': 'processing to image succeeded in {seconds} seconds',
|
'processing_image_succeeded': 'processing to image succeeded in {seconds} seconds',
|
||||||
'processing_image_failed': 'processing to image failed',
|
'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_succeeded': 'processing to video succeeded in {seconds} seconds',
|
||||||
'processing_video_failed': 'processing to video failed',
|
'processing_video_failed': 'processing to video failed',
|
||||||
'choose_image_source': 'choose an image for the source',
|
'choose_image_source': 'choose an image for the source',
|
||||||
@@ -103,7 +101,7 @@ LOCALES : Locales =\
|
|||||||
{
|
{
|
||||||
'install_dependency': 'choose the variant of {dependency} to install',
|
'install_dependency': 'choose the variant of {dependency} to install',
|
||||||
'skip_conda': 'skip the conda environment check',
|
'skip_conda': 'skip the conda environment check',
|
||||||
'workflow_mode': 'choose the workflow mode',
|
'workflow': 'choose the workflow',
|
||||||
'config_path': 'choose the config file to override defaults',
|
'config_path': 'choose the config file to override defaults',
|
||||||
'temp_path': 'specify the directory for the temporary resources',
|
'temp_path': 'specify the directory for the temporary resources',
|
||||||
'jobs_path': 'specify the directory to store jobs',
|
'jobs_path': 'specify the directory to store jobs',
|
||||||
@@ -129,7 +127,6 @@ LOCALES : Locales =\
|
|||||||
'reference_face_position': 'specify the position used to create the reference face',
|
'reference_face_position': 'specify the position used to create the reference face',
|
||||||
'reference_face_distance': 'specify the similarity between the reference face and target face',
|
'reference_face_distance': 'specify the similarity between the reference face and target face',
|
||||||
'reference_frame_number': 'specify the frame used to create the reference face',
|
'reference_frame_number': 'specify the frame used to create the reference face',
|
||||||
'face_tracker_score': 'specify the overlap score used to match the tracked faces',
|
|
||||||
'face_occluder_model': 'choose the model responsible for the occlusion mask',
|
'face_occluder_model': 'choose the model responsible for the occlusion mask',
|
||||||
'face_parser_model': 'choose the model responsible for the region mask',
|
'face_parser_model': 'choose the model responsible for the region mask',
|
||||||
'face_mask_types': 'mix and match different face mask types (choices: {choices})',
|
'face_mask_types': 'mix and match different face mask types (choices: {choices})',
|
||||||
@@ -141,13 +138,11 @@ LOCALES : Locales =\
|
|||||||
'trim_frame_start': 'specify the starting frame of the target video',
|
'trim_frame_start': 'specify the starting frame of the target video',
|
||||||
'trim_frame_end': 'specify the ending frame of the target video',
|
'trim_frame_end': 'specify the ending frame of the target video',
|
||||||
'temp_frame_format': 'specify the temporary resources format',
|
'temp_frame_format': 'specify the temporary resources format',
|
||||||
'target_frame_amount': 'specify the amount of target frames forwarded to the processor',
|
|
||||||
'output_image_quality': 'specify the image quality which translates to the image compression',
|
'output_image_quality': 'specify the image quality which translates to the image compression',
|
||||||
'output_image_scale': 'specify the image scale based on the target image',
|
'output_image_scale': 'specify the image scale based on the target image',
|
||||||
'output_audio_encoder': 'specify the encoder used for the audio',
|
'output_audio_encoder': 'specify the encoder used for the audio',
|
||||||
'output_audio_quality': 'specify the audio quality which translates to the audio compression',
|
'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_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_encoder': 'specify the encoder used for the video',
|
||||||
'output_video_preset': 'balance fast video processing and video file size',
|
'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_quality': 'specify the video quality which translates to the video compression',
|
||||||
@@ -163,7 +158,6 @@ LOCALES : Locales =\
|
|||||||
'benchmark_cycle_count': 'specify the amount of cycles per benchmark',
|
'benchmark_cycle_count': 'specify the amount of cycles per benchmark',
|
||||||
'api_host': 'specify the API host',
|
'api_host': 'specify the API host',
|
||||||
'api_port': 'specify the API port',
|
'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_device_ids': 'specify the devices used for processing',
|
||||||
'execution_providers': 'inference using different providers (choices: {choices}, ...)',
|
'execution_providers': 'inference using different providers (choices: {choices}, ...)',
|
||||||
'execution_thread_count': 'specify the amount of parallel threads while processing',
|
'execution_thread_count': 'specify the amount of parallel threads while processing',
|
||||||
@@ -195,7 +189,7 @@ LOCALES : Locales =\
|
|||||||
},
|
},
|
||||||
'about':
|
'about':
|
||||||
{
|
{
|
||||||
'fund': 'fund ai workstation',
|
'fund': 'fund training server',
|
||||||
'subscribe': 'become a member',
|
'subscribe': 'become a member',
|
||||||
'join': 'join our community'
|
'join': 'join our community'
|
||||||
},
|
},
|
||||||
@@ -230,7 +224,6 @@ LOCALES : Locales =\
|
|||||||
'face_selector_mode_dropdown': 'FACE SELECTOR MODE',
|
'face_selector_mode_dropdown': 'FACE SELECTOR MODE',
|
||||||
'face_selector_order_dropdown': 'FACE SELECTOR ORDER',
|
'face_selector_order_dropdown': 'FACE SELECTOR ORDER',
|
||||||
'face_selector_race_dropdown': 'FACE SELECTOR RACE',
|
'face_selector_race_dropdown': 'FACE SELECTOR RACE',
|
||||||
'face_tracker_score_slider': 'FACE TRACKER SCORE',
|
|
||||||
'face_occluder_model_dropdown': 'FACE OCCLUDER MODEL',
|
'face_occluder_model_dropdown': 'FACE OCCLUDER MODEL',
|
||||||
'face_parser_model_dropdown': 'FACE PARSER MODEL',
|
'face_parser_model_dropdown': 'FACE PARSER MODEL',
|
||||||
'voice_extractor_model_dropdown': 'VOICE EXTRACTOR MODEL',
|
'voice_extractor_model_dropdown': 'VOICE EXTRACTOR MODEL',
|
||||||
@@ -12,7 +12,6 @@ PROCESSORS_METHODS =\
|
|||||||
'clear_inference_pool',
|
'clear_inference_pool',
|
||||||
'register_args',
|
'register_args',
|
||||||
'apply_args',
|
'apply_args',
|
||||||
'get_common_modules',
|
|
||||||
'pre_check',
|
'pre_check',
|
||||||
'pre_process',
|
'pre_process',
|
||||||
'post_process',
|
'post_process',
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
from typing import List, Sequence, get_args
|
from typing import List, Sequence
|
||||||
|
|
||||||
from facefusion.common_helper import create_int_range
|
from facefusion.common_helper import create_int_range
|
||||||
from facefusion.processors.modules.age_modifier.types import AgeModifierModel
|
from facefusion.processors.modules.age_modifier.types import AgeModifierModel
|
||||||
|
|
||||||
age_modifier_models : List[AgeModifierModel] = list(get_args(AgeModifierModel))
|
age_modifier_models : List[AgeModifierModel] = [ 'styleganex_age' ]
|
||||||
|
|
||||||
age_modifier_direction_range : Sequence[int] = create_int_range(-100, 100, 1)
|
age_modifier_direction_range : Sequence[int] = create_int_range(-100, 100, 1)
|
||||||
|
|||||||
@@ -1,70 +1,34 @@
|
|||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
from types import ModuleType
|
|
||||||
from typing import List
|
|
||||||
|
|
||||||
import cv2
|
import cv2
|
||||||
import numpy
|
import numpy
|
||||||
|
|
||||||
import facefusion.capability_store
|
import facefusion.args_store
|
||||||
import facefusion.choices
|
import facefusion.choices
|
||||||
import facefusion.jobs.job_manager
|
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 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.common_helper import create_int_metavar, is_macos
|
||||||
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
|
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
|
||||||
from facefusion.face_creator import scale_face
|
from facefusion.execution import has_execution_provider
|
||||||
|
from facefusion.face_analyser import scale_face
|
||||||
from facefusion.face_helper import merge_matrix, paste_back, scale_face_landmark_5, warp_face_by_face_landmark_5
|
from facefusion.face_helper import merge_matrix, paste_back, scale_face_landmark_5, warp_face_by_face_landmark_5
|
||||||
from facefusion.face_masker import create_box_mask, create_occlusion_mask
|
from facefusion.face_masker import create_box_mask, create_occlusion_mask
|
||||||
from facefusion.face_selector import select_faces
|
from facefusion.face_selector import select_faces
|
||||||
from facefusion.filesystem import in_directory, is_image, is_video, resolve_relative_path
|
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 import choices as age_modifier_choices
|
||||||
from facefusion.processors.modules.age_modifier.types import AgeModifierDirection, AgeModifierInputs
|
from facefusion.processors.modules.age_modifier.types import AgeModifierDirection, AgeModifierInputs
|
||||||
from facefusion.processors.types import ApplyStateItem, ProcessorOutputs
|
from facefusion.processors.types import ProcessorOutputs
|
||||||
from facefusion.program_helper import find_argument_group
|
from facefusion.program_helper import find_argument_group
|
||||||
from facefusion.thread_helper import thread_semaphore
|
from facefusion.thread_helper import thread_semaphore
|
||||||
from facefusion.types import Args, DownloadScope, Face, InferencePool, ModelOptions, ModelSet, ProcessMode, VisionFrame
|
from facefusion.types import ApplyStateItem, 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
|
from facefusion.vision import match_frame_color, read_static_image, read_static_video_frame
|
||||||
|
|
||||||
|
|
||||||
@lru_cache()
|
@lru_cache()
|
||||||
def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
||||||
return\
|
return\
|
||||||
{
|
{
|
||||||
'fran':
|
|
||||||
{
|
|
||||||
'__metadata__':
|
|
||||||
{
|
|
||||||
'vendor': 'ry-lu',
|
|
||||||
'license': 'mit',
|
|
||||||
'year': 2024
|
|
||||||
},
|
|
||||||
'hashes':
|
|
||||||
{
|
|
||||||
'age_modifier':
|
|
||||||
{
|
|
||||||
'url': resolve_download_url('models-3.6.0', 'fran.hash'),
|
|
||||||
'path': resolve_relative_path('../.assets/models/fran.hash')
|
|
||||||
}
|
|
||||||
},
|
|
||||||
'sources':
|
|
||||||
{
|
|
||||||
'age_modifier':
|
|
||||||
{
|
|
||||||
'url': resolve_download_url('models-3.6.0', 'fran.onnx'),
|
|
||||||
'path': resolve_relative_path('../.assets/models/fran.onnx')
|
|
||||||
}
|
|
||||||
},
|
|
||||||
'templates':
|
|
||||||
{
|
|
||||||
'target': 'ffhq_512',
|
|
||||||
},
|
|
||||||
'sizes':
|
|
||||||
{
|
|
||||||
'target': (1024, 1024),
|
|
||||||
},
|
|
||||||
'mean': [ 0.0, 0.0, 0.0 ],
|
|
||||||
'standard_deviation': [ 1.0, 1.0, 1.0 ]
|
|
||||||
},
|
|
||||||
'styleganex_age':
|
'styleganex_age':
|
||||||
{
|
{
|
||||||
'__metadata__':
|
'__metadata__':
|
||||||
@@ -98,9 +62,7 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
|||||||
{
|
{
|
||||||
'target': (256, 256),
|
'target': (256, 256),
|
||||||
'target_with_background': (384, 384)
|
'target_with_background': (384, 384)
|
||||||
},
|
}
|
||||||
'mean': [ 0.5, 0.5, 0.5 ],
|
|
||||||
'standard_deviation': [ 0.5, 0.5, 0.5 ]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,25 +87,9 @@ def get_model_options() -> ModelOptions:
|
|||||||
def register_args(program : ArgumentParser) -> None:
|
def register_args(program : ArgumentParser) -> None:
|
||||||
group_processors = find_argument_group(program, 'processors')
|
group_processors = find_argument_group(program, 'processors')
|
||||||
if group_processors:
|
if group_processors:
|
||||||
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', 'styleganex_age'), choices = age_modifier_choices.age_modifier_models)
|
||||||
[
|
group_processors.add_argument('--age-modifier-direction', help = translator.get('help.direction', __package__), type = int, default = config.get_int_value('processors', 'age_modifier_direction', '0'), choices = age_modifier_choices.age_modifier_direction_range, metavar = create_int_metavar(age_modifier_choices.age_modifier_direction_range))
|
||||||
group_processors.add_argument(
|
facefusion.args_store.register_args([ 'age_modifier_model', 'age_modifier_direction' ], scopes = [ 'api', 'cli' ])
|
||||||
'--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:
|
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
|
||||||
@@ -151,18 +97,10 @@ def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
|
|||||||
apply_state_item('age_modifier_direction', args.get('age_modifier_direction'))
|
apply_state_item('age_modifier_direction', args.get('age_modifier_direction'))
|
||||||
|
|
||||||
|
|
||||||
def get_common_modules() -> List[ModuleType]:
|
|
||||||
return [ content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer ]
|
|
||||||
|
|
||||||
|
|
||||||
def pre_check() -> bool:
|
def pre_check() -> bool:
|
||||||
model_hash_set = get_model_options().get('hashes')
|
model_hash_set = get_model_options().get('hashes')
|
||||||
model_source_set = get_model_options().get('sources')
|
model_source_set = get_model_options().get('sources')
|
||||||
|
|
||||||
for common_module in get_common_modules():
|
|
||||||
if not common_module.pre_check():
|
|
||||||
return False
|
|
||||||
|
|
||||||
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
|
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
|
||||||
|
|
||||||
|
|
||||||
@@ -170,7 +108,6 @@ 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')):
|
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__)
|
logger.error(translator.get('choose_image_or_video_target') + translator.get('exclamation_mark'), __name__)
|
||||||
return False
|
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')):
|
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__)
|
logger.error(translator.get('specify_image_or_video_output') + translator.get('exclamation_mark'), __name__)
|
||||||
return False
|
return False
|
||||||
@@ -180,15 +117,16 @@ def pre_process(mode : ProcessMode) -> bool:
|
|||||||
def post_process() -> None:
|
def post_process() -> None:
|
||||||
read_static_image.cache_clear()
|
read_static_image.cache_clear()
|
||||||
read_static_video_frame.cache_clear()
|
read_static_video_frame.cache_clear()
|
||||||
read_static_video_chunk.cache_clear()
|
|
||||||
video_manager.clear_video_pool()
|
video_manager.clear_video_pool()
|
||||||
|
|
||||||
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
|
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
|
||||||
clear_inference_pool()
|
clear_inference_pool()
|
||||||
|
|
||||||
if state_manager.get_item('video_memory_strategy') == 'strict':
|
if state_manager.get_item('video_memory_strategy') == 'strict':
|
||||||
for common_module in get_common_modules():
|
content_analyser.clear_inference_pool()
|
||||||
common_module.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:
|
def modify_age(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
|
||||||
@@ -196,29 +134,6 @@ def modify_age(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFra
|
|||||||
model_sizes = get_model_options().get('sizes')
|
model_sizes = get_model_options().get('sizes')
|
||||||
face_landmark_5 = target_face.landmark_set.get('5/68').copy()
|
face_landmark_5 = target_face.landmark_set.get('5/68').copy()
|
||||||
crop_vision_frame, affine_matrix = warp_face_by_face_landmark_5(temp_vision_frame, face_landmark_5, model_templates.get('target'), model_sizes.get('target'))
|
crop_vision_frame, affine_matrix = warp_face_by_face_landmark_5(temp_vision_frame, face_landmark_5, model_templates.get('target'), model_sizes.get('target'))
|
||||||
|
|
||||||
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_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, 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()
|
extend_vision_frame_raw = extend_vision_frame.copy()
|
||||||
@@ -246,13 +161,14 @@ def modify_age(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFra
|
|||||||
paste_vision_frame = paste_back(temp_vision_frame, extend_vision_frame, crop_mask, extend_affine_matrix)
|
paste_vision_frame = paste_back(temp_vision_frame, extend_vision_frame, crop_mask, extend_affine_matrix)
|
||||||
return paste_vision_frame
|
return paste_vision_frame
|
||||||
|
|
||||||
return temp_vision_frame
|
|
||||||
|
|
||||||
|
|
||||||
def forward(crop_vision_frame : VisionFrame, extend_vision_frame : VisionFrame, age_modifier_direction : AgeModifierDirection) -> VisionFrame:
|
def forward(crop_vision_frame : VisionFrame, extend_vision_frame : VisionFrame, age_modifier_direction : AgeModifierDirection) -> VisionFrame:
|
||||||
age_modifier = get_inference_pool().get('age_modifier')
|
age_modifier = get_inference_pool().get('age_modifier')
|
||||||
age_modifier_inputs = {}
|
age_modifier_inputs = {}
|
||||||
|
|
||||||
|
if is_macos() and has_execution_provider('coreml'):
|
||||||
|
age_modifier.set_providers([ facefusion.choices.execution_provider_set.get('cpu') ])
|
||||||
|
|
||||||
for age_modifier_input in age_modifier.get_inputs():
|
for age_modifier_input in age_modifier.get_inputs():
|
||||||
if age_modifier_input.name == 'target':
|
if age_modifier_input.name == 'target':
|
||||||
age_modifier_inputs[age_modifier_input.name] = crop_vision_frame
|
age_modifier_inputs[age_modifier_input.name] = crop_vision_frame
|
||||||
@@ -268,24 +184,12 @@ def forward(crop_vision_frame : VisionFrame, extend_vision_frame : VisionFrame,
|
|||||||
|
|
||||||
|
|
||||||
def prepare_vision_frame(vision_frame : VisionFrame) -> VisionFrame:
|
def prepare_vision_frame(vision_frame : VisionFrame) -> VisionFrame:
|
||||||
model_mean = get_model_options().get('mean')
|
|
||||||
model_standard_deviation = get_model_options().get('standard_deviation')
|
|
||||||
vision_frame = vision_frame[:, :, ::-1] / 255.0
|
vision_frame = vision_frame[:, :, ::-1] / 255.0
|
||||||
vision_frame = (vision_frame - model_mean) / model_standard_deviation
|
vision_frame = (vision_frame - 0.5) / 0.5
|
||||||
vision_frame = numpy.expand_dims(vision_frame.transpose(2, 0, 1), axis = 0).astype(numpy.float32)
|
vision_frame = numpy.expand_dims(vision_frame.transpose(2, 0, 1), axis = 0).astype(numpy.float32)
|
||||||
return vision_frame
|
return vision_frame
|
||||||
|
|
||||||
|
|
||||||
def normalize_vision_frame(vision_frame : VisionFrame) -> VisionFrame:
|
|
||||||
model_mean = get_model_options().get('mean')
|
|
||||||
model_standard_deviation = get_model_options().get('standard_deviation')
|
|
||||||
vision_frame = vision_frame.transpose(1, 2, 0)
|
|
||||||
vision_frame = vision_frame * model_standard_deviation + model_mean
|
|
||||||
vision_frame = vision_frame.clip(0, 1)
|
|
||||||
vision_frame = vision_frame[:, :, ::-1] * 255
|
|
||||||
return vision_frame
|
|
||||||
|
|
||||||
|
|
||||||
def normalize_extend_frame(extend_vision_frame : VisionFrame) -> VisionFrame:
|
def normalize_extend_frame(extend_vision_frame : VisionFrame) -> VisionFrame:
|
||||||
model_sizes = get_model_options().get('sizes')
|
model_sizes = get_model_options().get('sizes')
|
||||||
extend_vision_frame = numpy.clip(extend_vision_frame, -1, 1)
|
extend_vision_frame = numpy.clip(extend_vision_frame, -1, 1)
|
||||||
@@ -299,13 +203,10 @@ def normalize_extend_frame(extend_vision_frame : VisionFrame) -> VisionFrame:
|
|||||||
|
|
||||||
def process_frame(inputs : AgeModifierInputs) -> ProcessorOutputs:
|
def process_frame(inputs : AgeModifierInputs) -> ProcessorOutputs:
|
||||||
reference_vision_frame = inputs.get('reference_vision_frame')
|
reference_vision_frame = inputs.get('reference_vision_frame')
|
||||||
source_vision_frames = inputs.get('source_vision_frames')
|
target_vision_frame = inputs.get('target_vision_frame')
|
||||||
target_vision_frames = inputs.get('target_vision_frames')
|
|
||||||
temp_vision_frame = inputs.get('temp_vision_frame')
|
temp_vision_frame = inputs.get('temp_vision_frame')
|
||||||
temp_vision_mask = inputs.get('temp_vision_mask')
|
temp_vision_mask = inputs.get('temp_vision_mask')
|
||||||
|
target_faces = select_faces(reference_vision_frame, target_vision_frame)
|
||||||
target_vision_frame = get_middle(target_vision_frames)
|
|
||||||
target_faces = select_faces(reference_vision_frame, source_vision_frames, target_vision_frames)
|
|
||||||
|
|
||||||
if target_faces:
|
if target_faces:
|
||||||
for target_face in target_faces:
|
for target_face in target_faces:
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
from facefusion.types import Locales
|
from facefusion.types import Locals
|
||||||
|
|
||||||
LOCALES : Locales =\
|
LOCALS : Locals =\
|
||||||
{
|
{
|
||||||
'en':
|
'en':
|
||||||
{
|
{
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
from typing import Any, List, Literal, TypeAlias, TypedDict
|
from typing import Any, Literal, TypeAlias, TypedDict
|
||||||
|
|
||||||
from numpy.typing import NDArray
|
from numpy.typing import NDArray
|
||||||
|
|
||||||
@@ -7,12 +7,11 @@ from facefusion.types import Mask, VisionFrame
|
|||||||
AgeModifierInputs = TypedDict('AgeModifierInputs',
|
AgeModifierInputs = TypedDict('AgeModifierInputs',
|
||||||
{
|
{
|
||||||
'reference_vision_frame' : VisionFrame,
|
'reference_vision_frame' : VisionFrame,
|
||||||
'source_vision_frames' : List[VisionFrame],
|
'target_vision_frame' : VisionFrame,
|
||||||
'target_vision_frames' : List[VisionFrame],
|
|
||||||
'temp_vision_frame' : VisionFrame,
|
'temp_vision_frame' : VisionFrame,
|
||||||
'temp_vision_mask' : Mask
|
'temp_vision_mask' : Mask
|
||||||
})
|
})
|
||||||
|
|
||||||
AgeModifierModel = Literal['fran', 'styleganex_age']
|
AgeModifierModel = Literal['styleganex_age']
|
||||||
|
|
||||||
AgeModifierDirection : TypeAlias = NDArray[Any]
|
AgeModifierDirection : TypeAlias = NDArray[Any]
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
from typing import List, Sequence, get_args
|
from typing import List, Sequence
|
||||||
|
|
||||||
from facefusion.common_helper import create_int_range
|
from facefusion.common_helper import create_int_range
|
||||||
from facefusion.processors.modules.background_remover.types import BackgroundRemoverModel
|
from facefusion.processors.modules.background_remover.types import BackgroundRemoverModel
|
||||||
|
|
||||||
background_remover_models : List[BackgroundRemoverModel] = list(get_args(BackgroundRemoverModel))
|
background_remover_models : List[BackgroundRemoverModel] = [ 'ben_2', 'birefnet_general', 'birefnet_portrait', 'isnet_general', 'modnet', 'ormbg', 'rmbg_1.4', 'rmbg_2.0', 'silueta', 'u2net_cloth', 'u2net_general', 'u2net_human', 'u2netp' ]
|
||||||
|
|
||||||
background_remover_color_range : Sequence[int] = create_int_range(0, 255, 1)
|
background_remover_color_range : Sequence[int] = create_int_range(0, 255, 1)
|
||||||
|
|||||||
@@ -1,28 +1,26 @@
|
|||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
from functools import lru_cache, partial
|
from functools import lru_cache, partial
|
||||||
from types import ModuleType
|
|
||||||
from typing import List, Tuple
|
from typing import List, Tuple
|
||||||
|
|
||||||
import cv2
|
import cv2
|
||||||
import numpy
|
import numpy
|
||||||
|
|
||||||
import facefusion.capability_store
|
import facefusion.args_store
|
||||||
import facefusion.choices
|
|
||||||
import facefusion.jobs.job_manager
|
import facefusion.jobs.job_manager
|
||||||
from facefusion import config, content_analyser, inference_manager, logger, state_manager, translator, video_manager
|
from facefusion import config, content_analyser, inference_manager, logger, state_manager, translator, video_manager
|
||||||
from facefusion.common_helper import is_macos, is_windows
|
from facefusion.common_helper import is_macos
|
||||||
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
|
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
|
||||||
from facefusion.execution import has_execution_provider
|
from facefusion.execution import has_execution_provider
|
||||||
from facefusion.filesystem import in_directory, is_image, is_video, resolve_relative_path
|
from facefusion.filesystem import in_directory, is_image, is_video, resolve_relative_path
|
||||||
from facefusion.normalizer import normalize_color
|
from facefusion.normalizer import normalize_color
|
||||||
from facefusion.processors.modules.background_remover import choices as background_remover_choices
|
from facefusion.processors.modules.background_remover import choices as background_remover_choices
|
||||||
from facefusion.processors.modules.background_remover.types import BackgroundRemoverInputs
|
from facefusion.processors.modules.background_remover.types import BackgroundRemoverInputs
|
||||||
from facefusion.processors.types import ApplyStateItem, ProcessorOutputs
|
from facefusion.processors.types import ProcessorOutputs
|
||||||
from facefusion.program_helper import find_argument_group
|
from facefusion.program_helper import find_argument_group
|
||||||
from facefusion.sanitizer import sanitize_int_range
|
from facefusion.sanitizer import sanitize_int_range
|
||||||
from facefusion.thread_helper import thread_semaphore
|
from facefusion.thread_helper import thread_semaphore
|
||||||
from facefusion.types import Args, DownloadScope, InferencePool, InferenceProvider, Mask, ModelOptions, ModelSet, ProcessMode, VisionFrame
|
from facefusion.types import ApplyStateItem, Args, DownloadScope, ExecutionProvider, InferencePool, Mask, ModelOptions, ModelSet, ProcessMode, VisionFrame
|
||||||
from facefusion.vision import read_static_image, read_static_video_chunk, read_static_video_frame
|
from facefusion.vision import read_static_image, read_static_video_frame
|
||||||
|
|
||||||
|
|
||||||
@lru_cache()
|
@lru_cache()
|
||||||
@@ -53,7 +51,6 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
|||||||
'path': resolve_relative_path('../.assets/models/ben_2.onnx')
|
'path': resolve_relative_path('../.assets/models/ben_2.onnx')
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'type': 'ben',
|
|
||||||
'size': (1024, 1024),
|
'size': (1024, 1024),
|
||||||
'mean': [ 0.0, 0.0, 0.0 ],
|
'mean': [ 0.0, 0.0, 0.0 ],
|
||||||
'standard_deviation': [ 1.0, 1.0, 1.0 ]
|
'standard_deviation': [ 1.0, 1.0, 1.0 ]
|
||||||
@@ -82,7 +79,6 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
|||||||
'path': resolve_relative_path('../.assets/models/birefnet_general.onnx')
|
'path': resolve_relative_path('../.assets/models/birefnet_general.onnx')
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'type': 'birefnet',
|
|
||||||
'size': (1024, 1024),
|
'size': (1024, 1024),
|
||||||
'mean': [ 0.0, 0.0, 0.0 ],
|
'mean': [ 0.0, 0.0, 0.0 ],
|
||||||
'standard_deviation': [ 1.0, 1.0, 1.0 ]
|
'standard_deviation': [ 1.0, 1.0, 1.0 ]
|
||||||
@@ -111,69 +107,10 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
|||||||
'path': resolve_relative_path('../.assets/models/birefnet_portrait.onnx')
|
'path': resolve_relative_path('../.assets/models/birefnet_portrait.onnx')
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'type': 'birefnet',
|
|
||||||
'size': (1024, 1024),
|
'size': (1024, 1024),
|
||||||
'mean': [ 0.0, 0.0, 0.0 ],
|
'mean': [ 0.0, 0.0, 0.0 ],
|
||||||
'standard_deviation': [ 1.0, 1.0, 1.0 ]
|
'standard_deviation': [ 1.0, 1.0, 1.0 ]
|
||||||
},
|
},
|
||||||
'corridor_key_1024':
|
|
||||||
{
|
|
||||||
'__metadata__':
|
|
||||||
{
|
|
||||||
'vendor': 'nikopueringer',
|
|
||||||
'license': 'Non-Commercial',
|
|
||||||
'year': 2025
|
|
||||||
},
|
|
||||||
'hashes':
|
|
||||||
{
|
|
||||||
'background_remover':
|
|
||||||
{
|
|
||||||
'url': resolve_download_url('models-3.6.0', 'corridor_key_1024.hash'),
|
|
||||||
'path': resolve_relative_path('../.assets/models/corridor_key_1024.hash')
|
|
||||||
}
|
|
||||||
},
|
|
||||||
'sources':
|
|
||||||
{
|
|
||||||
'background_remover':
|
|
||||||
{
|
|
||||||
'url': resolve_download_url('models-3.6.0', 'corridor_key_1024.onnx'),
|
|
||||||
'path': resolve_relative_path('../.assets/models/corridor_key_1024.onnx')
|
|
||||||
}
|
|
||||||
},
|
|
||||||
'type': 'corridor_key',
|
|
||||||
'size': (1024, 1024),
|
|
||||||
'mean': [ 0.485, 0.456, 0.406 ],
|
|
||||||
'standard_deviation': [ 0.229, 0.224, 0.225 ]
|
|
||||||
},
|
|
||||||
'corridor_key_2048':
|
|
||||||
{
|
|
||||||
'__metadata__':
|
|
||||||
{
|
|
||||||
'vendor': 'nikopueringer',
|
|
||||||
'license': 'Non-Commercial',
|
|
||||||
'year': 2025
|
|
||||||
},
|
|
||||||
'hashes':
|
|
||||||
{
|
|
||||||
'background_remover':
|
|
||||||
{
|
|
||||||
'url': resolve_download_url('models-3.6.0', 'corridor_key_2048.hash'),
|
|
||||||
'path': resolve_relative_path('../.assets/models/corridor_key_2048.hash')
|
|
||||||
}
|
|
||||||
},
|
|
||||||
'sources':
|
|
||||||
{
|
|
||||||
'background_remover':
|
|
||||||
{
|
|
||||||
'url': resolve_download_url('models-3.6.0', 'corridor_key_2048.onnx'),
|
|
||||||
'path': resolve_relative_path('../.assets/models/corridor_key_2048.onnx')
|
|
||||||
}
|
|
||||||
},
|
|
||||||
'type': 'corridor_key',
|
|
||||||
'size': (2048, 2048),
|
|
||||||
'mean': [ 0.485, 0.456, 0.406 ],
|
|
||||||
'standard_deviation': [ 0.229, 0.224, 0.225 ]
|
|
||||||
},
|
|
||||||
'isnet_general':
|
'isnet_general':
|
||||||
{
|
{
|
||||||
'__metadata__':
|
'__metadata__':
|
||||||
@@ -198,7 +135,6 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
|||||||
'path': resolve_relative_path('../.assets/models/isnet_general.onnx')
|
'path': resolve_relative_path('../.assets/models/isnet_general.onnx')
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'type': 'isnet',
|
|
||||||
'size': (1024, 1024),
|
'size': (1024, 1024),
|
||||||
'mean': [ 0.5, 0.5, 0.5 ],
|
'mean': [ 0.5, 0.5, 0.5 ],
|
||||||
'standard_deviation': [ 1.0, 1.0, 1.0 ]
|
'standard_deviation': [ 1.0, 1.0, 1.0 ]
|
||||||
@@ -227,7 +163,6 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
|||||||
'path': resolve_relative_path('../.assets/models/modnet.onnx')
|
'path': resolve_relative_path('../.assets/models/modnet.onnx')
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'type': 'modnet',
|
|
||||||
'size': (512, 512),
|
'size': (512, 512),
|
||||||
'mean': [ 0.5, 0.5, 0.5 ],
|
'mean': [ 0.5, 0.5, 0.5 ],
|
||||||
'standard_deviation': [ 0.5, 0.5, 0.5 ]
|
'standard_deviation': [ 0.5, 0.5, 0.5 ]
|
||||||
@@ -256,7 +191,6 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
|||||||
'path': resolve_relative_path('../.assets/models/ormbg.onnx')
|
'path': resolve_relative_path('../.assets/models/ormbg.onnx')
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'type': 'ormbg',
|
|
||||||
'size': (1024, 1024),
|
'size': (1024, 1024),
|
||||||
'mean': [ 0.0, 0.0, 0.0 ],
|
'mean': [ 0.0, 0.0, 0.0 ],
|
||||||
'standard_deviation': [ 1.0, 1.0, 1.0 ]
|
'standard_deviation': [ 1.0, 1.0, 1.0 ]
|
||||||
@@ -285,7 +219,6 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
|||||||
'path': resolve_relative_path('../.assets/models/rmbg_1.4.onnx')
|
'path': resolve_relative_path('../.assets/models/rmbg_1.4.onnx')
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'type': 'rmbg',
|
|
||||||
'size': (1024, 1024),
|
'size': (1024, 1024),
|
||||||
'mean': [ 0.5, 0.5, 0.5 ],
|
'mean': [ 0.5, 0.5, 0.5 ],
|
||||||
'standard_deviation': [ 1.0, 1.0, 1.0 ]
|
'standard_deviation': [ 1.0, 1.0, 1.0 ]
|
||||||
@@ -314,7 +247,6 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
|||||||
'path': resolve_relative_path('../.assets/models/rmbg_2.0.onnx')
|
'path': resolve_relative_path('../.assets/models/rmbg_2.0.onnx')
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'type': 'rmbg',
|
|
||||||
'size': (1024, 1024),
|
'size': (1024, 1024),
|
||||||
'mean': [ 0.485, 0.456, 0.406 ],
|
'mean': [ 0.485, 0.456, 0.406 ],
|
||||||
'standard_deviation': [ 0.229, 0.224, 0.225 ]
|
'standard_deviation': [ 0.229, 0.224, 0.225 ]
|
||||||
@@ -343,7 +275,6 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
|||||||
'path': resolve_relative_path('../.assets/models/silueta.onnx')
|
'path': resolve_relative_path('../.assets/models/silueta.onnx')
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'type': 'silueta',
|
|
||||||
'size': (320, 320),
|
'size': (320, 320),
|
||||||
'mean': [ 0.485, 0.456, 0.406 ],
|
'mean': [ 0.485, 0.456, 0.406 ],
|
||||||
'standard_deviation': [ 0.229, 0.224, 0.225 ]
|
'standard_deviation': [ 0.229, 0.224, 0.225 ]
|
||||||
@@ -372,7 +303,6 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
|||||||
'path': resolve_relative_path('../.assets/models/u2net_cloth.onnx')
|
'path': resolve_relative_path('../.assets/models/u2net_cloth.onnx')
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'type': 'u2net_cloth',
|
|
||||||
'size': (768, 768),
|
'size': (768, 768),
|
||||||
'mean': [ 0.485, 0.456, 0.406 ],
|
'mean': [ 0.485, 0.456, 0.406 ],
|
||||||
'standard_deviation': [ 0.229, 0.224, 0.225 ]
|
'standard_deviation': [ 0.229, 0.224, 0.225 ]
|
||||||
@@ -401,7 +331,6 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
|||||||
'path': resolve_relative_path('../.assets/models/u2net_general.onnx')
|
'path': resolve_relative_path('../.assets/models/u2net_general.onnx')
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'type': 'u2net',
|
|
||||||
'size': (320, 320),
|
'size': (320, 320),
|
||||||
'mean': [ 0.485, 0.456, 0.406 ],
|
'mean': [ 0.485, 0.456, 0.406 ],
|
||||||
'standard_deviation': [ 0.229, 0.224, 0.225 ]
|
'standard_deviation': [ 0.229, 0.224, 0.225 ]
|
||||||
@@ -430,7 +359,6 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
|||||||
'path': resolve_relative_path('../.assets/models/u2net_human.onnx')
|
'path': resolve_relative_path('../.assets/models/u2net_human.onnx')
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'type': 'u2net',
|
|
||||||
'size': (320, 320),
|
'size': (320, 320),
|
||||||
'mean': [ 0.485, 0.456, 0.406 ],
|
'mean': [ 0.485, 0.456, 0.406 ],
|
||||||
'standard_deviation': [ 0.229, 0.224, 0.225 ]
|
'standard_deviation': [ 0.229, 0.224, 0.225 ]
|
||||||
@@ -459,7 +387,6 @@ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
|||||||
'path': resolve_relative_path('../.assets/models/u2netp.onnx')
|
'path': resolve_relative_path('../.assets/models/u2netp.onnx')
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'type': 'u2netp',
|
|
||||||
'size': (320, 320),
|
'size': (320, 320),
|
||||||
'mean': [ 0.485, 0.456, 0.406 ],
|
'mean': [ 0.485, 0.456, 0.406 ],
|
||||||
'standard_deviation': [ 0.229, 0.224, 0.225 ]
|
'standard_deviation': [ 0.229, 0.224, 0.225 ]
|
||||||
@@ -479,13 +406,10 @@ def clear_inference_pool() -> None:
|
|||||||
inference_manager.clear_inference_pool(__name__, model_names)
|
inference_manager.clear_inference_pool(__name__, model_names)
|
||||||
|
|
||||||
|
|
||||||
def resolve_inference_providers() -> List[InferenceProvider]:
|
def resolve_execution_providers() -> List[ExecutionProvider]:
|
||||||
model_type = get_model_options().get('type')
|
if is_macos() and has_execution_provider('coreml'):
|
||||||
|
return [ 'cpu' ]
|
||||||
if is_macos() and has_execution_provider('coreml') or is_windows() and has_execution_provider('directml') and model_type == 'corridor_key':
|
return state_manager.get_item('execution_providers')
|
||||||
return [ facefusion.choices.execution_provider_set.get('cpu') ]
|
|
||||||
|
|
||||||
return []
|
|
||||||
|
|
||||||
|
|
||||||
def get_model_options() -> ModelOptions:
|
def get_model_options() -> ModelOptions:
|
||||||
@@ -496,51 +420,20 @@ def get_model_options() -> ModelOptions:
|
|||||||
def register_args(program : ArgumentParser) -> None:
|
def register_args(program : ArgumentParser) -> None:
|
||||||
group_processors = find_argument_group(program, 'processors')
|
group_processors = find_argument_group(program, 'processors')
|
||||||
if group_processors:
|
if group_processors:
|
||||||
facefusion.capability_store.register_capability_set(
|
group_processors.add_argument('--background-remover-model', help = translator.get('help.model', __package__), default = config.get_str_value('processors', 'background_remover_model', 'rmbg_2.0'), choices = background_remover_choices.background_remover_models)
|
||||||
[
|
group_processors.add_argument('--background-remover-color', help = translator.get('help.color', __package__), type = partial(sanitize_int_range, int_range = background_remover_choices.background_remover_color_range), default = config.get_int_list('processors', 'background_remover_color', '0 0 0 0'), nargs ='+')
|
||||||
group_processors.add_argument(
|
facefusion.args_store.register_args([ 'background_remover_model', 'background_remover_color' ], scopes = [ 'api', 'cli' ])
|
||||||
'--background-remover-model',
|
|
||||||
help = translator.get('help.model', __package__),
|
|
||||||
default = config.get_str_value('processors', 'background_remover_model', 'modnet'),
|
|
||||||
choices = background_remover_choices.background_remover_models
|
|
||||||
),
|
|
||||||
group_processors.add_argument(
|
|
||||||
'--background-remover-fill-color',
|
|
||||||
help = translator.get('help.fill_color', __package__),
|
|
||||||
type = partial(sanitize_int_range, int_range = background_remover_choices.background_remover_color_range),
|
|
||||||
default = config.get_int_list('processors', 'background_remover_fill_color', '0 0 0 0'),
|
|
||||||
nargs = '+'
|
|
||||||
),
|
|
||||||
group_processors.add_argument(
|
|
||||||
'--background-remover-despill-color',
|
|
||||||
help = translator.get('help.despill_color', __package__),
|
|
||||||
type = partial(sanitize_int_range, int_range = background_remover_choices.background_remover_color_range),
|
|
||||||
default = config.get_int_list('processors', 'background_remover_despill_color', '0 0 0 0'),
|
|
||||||
nargs = '+'
|
|
||||||
)
|
|
||||||
],
|
|
||||||
scopes = [ 'api', 'cli' ]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
|
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
|
||||||
apply_state_item('background_remover_model', args.get('background_remover_model'))
|
apply_state_item('background_remover_model', args.get('background_remover_model'))
|
||||||
apply_state_item('background_remover_fill_color', normalize_color(args.get('background_remover_fill_color')))
|
apply_state_item('background_remover_color', normalize_color(args.get('background_remover_color')))
|
||||||
apply_state_item('background_remover_despill_color', normalize_color(args.get('background_remover_despill_color')))
|
|
||||||
|
|
||||||
|
|
||||||
def get_common_modules() -> List[ModuleType]:
|
|
||||||
return [ content_analyser ]
|
|
||||||
|
|
||||||
|
|
||||||
def pre_check() -> bool:
|
def pre_check() -> bool:
|
||||||
model_hash_set = get_model_options().get('hashes')
|
model_hash_set = get_model_options().get('hashes')
|
||||||
model_source_set = get_model_options().get('sources')
|
model_source_set = get_model_options().get('sources')
|
||||||
|
|
||||||
for common_module in get_common_modules():
|
|
||||||
if not common_module.pre_check():
|
|
||||||
return False
|
|
||||||
|
|
||||||
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
|
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
|
||||||
|
|
||||||
|
|
||||||
@@ -548,7 +441,6 @@ 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')):
|
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__)
|
logger.error(translator.get('choose_image_or_video_target') + translator.get('exclamation_mark'), __name__)
|
||||||
return False
|
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')):
|
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__)
|
logger.error(translator.get('specify_image_or_video_output') + translator.get('exclamation_mark'), __name__)
|
||||||
return False
|
return False
|
||||||
@@ -558,38 +450,24 @@ def pre_process(mode : ProcessMode) -> bool:
|
|||||||
def post_process() -> None:
|
def post_process() -> None:
|
||||||
read_static_image.cache_clear()
|
read_static_image.cache_clear()
|
||||||
read_static_video_frame.cache_clear()
|
read_static_video_frame.cache_clear()
|
||||||
read_static_video_chunk.cache_clear()
|
|
||||||
video_manager.clear_video_pool()
|
video_manager.clear_video_pool()
|
||||||
|
|
||||||
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
|
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
|
||||||
clear_inference_pool()
|
clear_inference_pool()
|
||||||
|
|
||||||
if state_manager.get_item('video_memory_strategy') == 'strict':
|
if state_manager.get_item('video_memory_strategy') == 'strict':
|
||||||
for common_module in get_common_modules():
|
content_analyser.clear_inference_pool()
|
||||||
common_module.clear_inference_pool()
|
|
||||||
|
|
||||||
|
|
||||||
def remove_background(temp_vision_frame : VisionFrame) -> Tuple[VisionFrame, Mask]:
|
def remove_background(temp_vision_frame : VisionFrame) -> Tuple[VisionFrame, Mask]:
|
||||||
model_type = get_model_options().get('type')
|
temp_vision_mask = forward(prepare_temp_frame(temp_vision_frame))
|
||||||
|
temp_vision_mask = normalize_vision_mask(temp_vision_mask)
|
||||||
if model_type == 'corridor_key':
|
temp_vision_mask = cv2.resize(temp_vision_mask, temp_vision_frame.shape[:2][::-1])
|
||||||
remove_vision_mask, remove_vision_frame = forward_corridor_key(prepare_temp_frame(temp_vision_frame))
|
temp_vision_frame = apply_background_color(temp_vision_frame, temp_vision_mask)
|
||||||
remove_vision_frame = numpy.squeeze(remove_vision_frame).transpose(1, 2, 0)
|
return temp_vision_frame, temp_vision_mask
|
||||||
remove_vision_frame = numpy.clip(remove_vision_frame * 255, 0, 255).astype(numpy.uint8)
|
|
||||||
temp_vision_frame = cv2.resize(remove_vision_frame[:, :, ::-1], temp_vision_frame.shape[:2][::-1])
|
|
||||||
else:
|
|
||||||
remove_vision_mask = forward(prepare_temp_frame(temp_vision_frame))
|
|
||||||
|
|
||||||
remove_vision_mask = normalize_vision_mask(remove_vision_mask)
|
|
||||||
remove_vision_mask = cv2.resize(remove_vision_mask, temp_vision_frame.shape[:2][::-1])
|
|
||||||
temp_vision_frame = apply_despill_color(temp_vision_frame)
|
|
||||||
temp_vision_frame = apply_fill_color(temp_vision_frame, remove_vision_mask)
|
|
||||||
return temp_vision_frame, remove_vision_mask
|
|
||||||
|
|
||||||
|
|
||||||
def forward(temp_vision_frame : VisionFrame) -> VisionFrame:
|
def forward(temp_vision_frame : VisionFrame) -> VisionFrame:
|
||||||
background_remover = get_inference_pool().get('background_remover')
|
background_remover = get_inference_pool().get('background_remover')
|
||||||
model_type = get_model_options().get('type')
|
model_name = state_manager.get_item('background_remover_model')
|
||||||
|
|
||||||
with thread_semaphore():
|
with thread_semaphore():
|
||||||
remove_vision_frame = background_remover.run(None,
|
remove_vision_frame = background_remover.run(None,
|
||||||
@@ -597,42 +475,20 @@ def forward(temp_vision_frame : VisionFrame) -> VisionFrame:
|
|||||||
'input': temp_vision_frame
|
'input': temp_vision_frame
|
||||||
})[0]
|
})[0]
|
||||||
|
|
||||||
if model_type == 'u2net_cloth':
|
if model_name == 'u2net_cloth':
|
||||||
remove_vision_frame = numpy.argmax(remove_vision_frame, axis = 1)
|
remove_vision_frame = numpy.argmax(remove_vision_frame, axis = 1)
|
||||||
|
|
||||||
return remove_vision_frame
|
return remove_vision_frame
|
||||||
|
|
||||||
|
|
||||||
def forward_corridor_key(temp_vision_frame : VisionFrame) -> Tuple[Mask, VisionFrame]:
|
|
||||||
background_remover = get_inference_pool().get('background_remover')
|
|
||||||
|
|
||||||
with thread_semaphore():
|
|
||||||
remove_vision_mask, remove_vision_frame = background_remover.run(None,
|
|
||||||
{
|
|
||||||
'input': temp_vision_frame
|
|
||||||
})
|
|
||||||
|
|
||||||
return remove_vision_mask, remove_vision_frame
|
|
||||||
|
|
||||||
|
|
||||||
def prepare_temp_frame(temp_vision_frame : VisionFrame) -> VisionFrame:
|
def prepare_temp_frame(temp_vision_frame : VisionFrame) -> VisionFrame:
|
||||||
model_type = get_model_options().get('type')
|
|
||||||
model_size = get_model_options().get('size')
|
model_size = get_model_options().get('size')
|
||||||
model_mean = get_model_options().get('mean')
|
model_mean = get_model_options().get('mean')
|
||||||
model_standard_deviation = get_model_options().get('standard_deviation')
|
model_standard_deviation = get_model_options().get('standard_deviation')
|
||||||
|
|
||||||
if model_type == 'corridor_key':
|
|
||||||
coarse_color = temp_vision_frame[:, :, ::-1].astype(numpy.float32) / 255.0
|
|
||||||
coarse_bias = coarse_color[:, :, 1] - numpy.maximum(coarse_color[:, :, 0], coarse_color[:, :, 2])
|
|
||||||
coarse_vision_mask = cv2.resize(1.0 - numpy.clip(coarse_bias * 2.0, 0, 1), model_size)[:, :, numpy.newaxis]
|
|
||||||
|
|
||||||
temp_vision_frame = cv2.resize(temp_vision_frame, model_size)
|
temp_vision_frame = cv2.resize(temp_vision_frame, model_size)
|
||||||
temp_vision_frame = temp_vision_frame[:, :, ::-1] / 255.0
|
temp_vision_frame = temp_vision_frame[:, :, ::-1] / 255.0
|
||||||
temp_vision_frame = (temp_vision_frame - model_mean) / model_standard_deviation
|
temp_vision_frame = (temp_vision_frame - model_mean) / model_standard_deviation
|
||||||
|
|
||||||
if model_type == 'corridor_key':
|
|
||||||
temp_vision_frame = numpy.concatenate([ temp_vision_frame, coarse_vision_mask ], axis = 2)
|
|
||||||
|
|
||||||
temp_vision_frame = temp_vision_frame.transpose(2, 0, 1)
|
temp_vision_frame = temp_vision_frame.transpose(2, 0, 1)
|
||||||
temp_vision_frame = numpy.expand_dims(temp_vision_frame, axis = 0).astype(numpy.float32)
|
temp_vision_frame = numpy.expand_dims(temp_vision_frame, axis = 0).astype(numpy.float32)
|
||||||
return temp_vision_frame
|
return temp_vision_frame
|
||||||
@@ -644,32 +500,16 @@ def normalize_vision_mask(temp_vision_mask : Mask) -> Mask:
|
|||||||
return temp_vision_mask
|
return temp_vision_mask
|
||||||
|
|
||||||
|
|
||||||
def apply_fill_color(temp_vision_frame : VisionFrame, temp_vision_mask : Mask) -> VisionFrame:
|
def apply_background_color(temp_vision_frame : VisionFrame, temp_vision_mask : Mask) -> VisionFrame:
|
||||||
background_remover_fill_color = state_manager.get_item('background_remover_fill_color')
|
background_remover_color = state_manager.get_item('background_remover_color')
|
||||||
temp_vision_mask = temp_vision_mask.astype(numpy.float32) / 255
|
temp_vision_mask = temp_vision_mask.astype(numpy.float32) / 255
|
||||||
temp_vision_mask = numpy.expand_dims(temp_vision_mask, axis = 2)
|
temp_vision_mask = numpy.expand_dims(temp_vision_mask, axis = 2)
|
||||||
temp_vision_mask = (1 - temp_vision_mask) * background_remover_fill_color[-1] / 255
|
temp_vision_mask = (1 - temp_vision_mask) * background_remover_color[-1] / 255
|
||||||
fill_vision_frame = numpy.zeros_like(temp_vision_frame)
|
color_frame = numpy.zeros_like(temp_vision_frame)
|
||||||
fill_vision_frame[:, :, 0] = background_remover_fill_color[2]
|
color_frame[:, :, 0] = background_remover_color[2]
|
||||||
fill_vision_frame[:, :, 1] = background_remover_fill_color[1]
|
color_frame[:, :, 1] = background_remover_color[1]
|
||||||
fill_vision_frame[:, :, 2] = background_remover_fill_color[0]
|
color_frame[:, :, 2] = background_remover_color[0]
|
||||||
temp_vision_frame = temp_vision_frame * (1 - temp_vision_mask) + fill_vision_frame * temp_vision_mask
|
temp_vision_frame = temp_vision_frame * (1 - temp_vision_mask) + color_frame * temp_vision_mask
|
||||||
temp_vision_frame = temp_vision_frame.astype(numpy.uint8)
|
|
||||||
return temp_vision_frame
|
|
||||||
|
|
||||||
|
|
||||||
def apply_despill_color(temp_vision_frame : VisionFrame) -> VisionFrame:
|
|
||||||
background_remover_despill_color = state_manager.get_item('background_remover_despill_color')
|
|
||||||
temp_vision_frame = temp_vision_frame.astype(numpy.float32)
|
|
||||||
color_alpha = background_remover_despill_color[3] / 255.0
|
|
||||||
despill_vision_frame = numpy.zeros_like(temp_vision_frame)
|
|
||||||
despill_vision_frame[:, :, 0] = background_remover_despill_color[2]
|
|
||||||
despill_vision_frame[:, :, 1] = background_remover_despill_color[1]
|
|
||||||
despill_vision_frame[:, :, 2] = background_remover_despill_color[0]
|
|
||||||
color_weight = despill_vision_frame / numpy.maximum(numpy.max(background_remover_despill_color[:3]), 1)
|
|
||||||
color_limit = numpy.roll(temp_vision_frame, 1, 2) + numpy.roll(temp_vision_frame, -1, 2)
|
|
||||||
limit_vision_frame = numpy.minimum(temp_vision_frame, color_limit * 0.5)
|
|
||||||
temp_vision_frame = temp_vision_frame + (limit_vision_frame - temp_vision_frame) * color_alpha * color_weight
|
|
||||||
temp_vision_frame = temp_vision_frame.astype(numpy.uint8)
|
temp_vision_frame = temp_vision_frame.astype(numpy.uint8)
|
||||||
return temp_vision_frame
|
return temp_vision_frame
|
||||||
|
|
||||||
|
|||||||
@@ -1,26 +0,0 @@
|
|||||||
from facefusion.types import Locales
|
|
||||||
|
|
||||||
LOCALES : Locales =\
|
|
||||||
{
|
|
||||||
'en':
|
|
||||||
{
|
|
||||||
'help':
|
|
||||||
{
|
|
||||||
'model': 'choose the model responsible for removing the background',
|
|
||||||
'fill_color': 'apply red, green, blue and alpha values to the background',
|
|
||||||
'despill_color': 'remove red, green, blue and alpha values from the foreground'
|
|
||||||
},
|
|
||||||
'uis':
|
|
||||||
{
|
|
||||||
'model_dropdown': 'BACKGROUND REMOVER MODEL',
|
|
||||||
'fill_color_red_number': 'FILL COLOR RED',
|
|
||||||
'fill_color_green_number': 'FILL COLOR GREEN',
|
|
||||||
'fill_color_blue_number': 'FILL COLOR BLUE',
|
|
||||||
'fill_color_alpha_number': 'FILL COLOR ALPHA',
|
|
||||||
'despill_color_red_number': 'DESPILL COLOR RED',
|
|
||||||
'despill_color_green_number': 'DESPILL COLOR GREEN',
|
|
||||||
'despill_color_blue_number': 'DESPILL COLOR BLUE',
|
|
||||||
'despill_color_alpha_number': 'DESPILL COLOR ALPHA'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
from facefusion.types import Locals
|
||||||
|
|
||||||
|
LOCALS : Locals =\
|
||||||
|
{
|
||||||
|
'en':
|
||||||
|
{
|
||||||
|
'help':
|
||||||
|
{
|
||||||
|
'model': 'choose the model responsible for removing the background',
|
||||||
|
'color': 'apply red, green blue and alpha values to the background'
|
||||||
|
},
|
||||||
|
'uis':
|
||||||
|
{
|
||||||
|
'model_dropdown': 'BACKGROUND REMOVER MODEL',
|
||||||
|
'color_red_number': 'BACKGROUND COLOR RED',
|
||||||
|
'color_green_number': 'BACKGROUND COLOR GREEN',
|
||||||
|
'color_blue_number': 'BACKGROUND COLOR BLUE',
|
||||||
|
'color_alpha_number': 'BACKGROUND COLOR ALPHA'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +1,12 @@
|
|||||||
from typing import List, Literal, TypedDict
|
from typing import Literal, TypedDict
|
||||||
|
|
||||||
from facefusion.types import Mask, VisionFrame
|
from facefusion.types import Mask, VisionFrame
|
||||||
|
|
||||||
BackgroundRemoverInputs = TypedDict('BackgroundRemoverInputs',
|
BackgroundRemoverInputs = TypedDict('BackgroundRemoverInputs',
|
||||||
{
|
{
|
||||||
'target_vision_frames' : List[VisionFrame],
|
'target_vision_frame' : VisionFrame,
|
||||||
'temp_vision_frame' : VisionFrame,
|
'temp_vision_frame' : VisionFrame,
|
||||||
'temp_vision_mask' : Mask
|
'temp_vision_mask' : Mask
|
||||||
})
|
})
|
||||||
|
|
||||||
BackgroundRemoverModel = Literal['ben_2', 'birefnet_general', 'birefnet_portrait', 'corridor_key_1024', 'corridor_key_2048', 'isnet_general', 'modnet', 'ormbg', 'rmbg_1.4', 'rmbg_2.0', 'silueta', 'u2net_cloth', 'u2net_general', 'u2net_human', 'u2netp']
|
BackgroundRemoverModel = Literal['ben_2', 'birefnet_general', 'birefnet_portrait', 'isnet_general', 'modnet', 'ormbg', 'rmbg_1.4', 'rmbg_2.0', 'silueta', 'u2net_cloth', 'u2net_general', 'u2net_human', 'u2netp']
|
||||||
|
|||||||
@@ -1,29 +1,28 @@
|
|||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
from types import ModuleType
|
from typing import Tuple
|
||||||
from typing import List, Tuple
|
|
||||||
|
|
||||||
import cv2
|
import cv2
|
||||||
import numpy
|
import numpy
|
||||||
from cv2.typing import Size
|
from cv2.typing import Size
|
||||||
|
|
||||||
import facefusion.capability_store
|
import facefusion.args_store
|
||||||
import facefusion.jobs.job_manager
|
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 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.common_helper import create_int_metavar
|
||||||
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url_by_provider
|
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url_by_provider
|
||||||
from facefusion.face_creator import scale_face
|
from facefusion.face_analyser import scale_face
|
||||||
from facefusion.face_helper import paste_back, warp_face_by_face_landmark_5
|
from facefusion.face_helper import paste_back, warp_face_by_face_landmark_5
|
||||||
from facefusion.face_masker import create_area_mask, create_box_mask, create_occlusion_mask, create_region_mask
|
from facefusion.face_masker import create_area_mask, create_box_mask, create_occlusion_mask, create_region_mask
|
||||||
from facefusion.face_selector import select_faces
|
from facefusion.face_selector import select_faces
|
||||||
from facefusion.filesystem import get_file_name, in_directory, is_image, is_video, resolve_file_paths, resolve_relative_path
|
from facefusion.filesystem import get_file_name, in_directory, is_image, is_video, resolve_file_paths, resolve_relative_path
|
||||||
from facefusion.processors.modules.deep_swapper import choices as deep_swapper_choices
|
from facefusion.processors.modules.deep_swapper import choices as deep_swapper_choices
|
||||||
from facefusion.processors.modules.deep_swapper.types import DeepSwapperInputs, DeepSwapperMorph
|
from facefusion.processors.modules.deep_swapper.types import DeepSwapperInputs, DeepSwapperMorph
|
||||||
from facefusion.processors.types import ApplyStateItem, ProcessorOutputs
|
from facefusion.processors.types import ProcessorOutputs
|
||||||
from facefusion.program_helper import find_argument_group
|
from facefusion.program_helper import find_argument_group
|
||||||
from facefusion.thread_helper import thread_semaphore
|
from facefusion.thread_helper import thread_semaphore
|
||||||
from facefusion.types import Args, DownloadScope, Face, InferencePool, Mask, ModelOptions, ModelSet, ProcessMode, VisionFrame
|
from facefusion.types import ApplyStateItem, Args, DownloadScope, Face, InferencePool, Mask, ModelOptions, ModelSet, ProcessMode, VisionFrame
|
||||||
from facefusion.vision import conditional_match_frame_color, read_static_image, read_static_video_chunk, read_static_video_frame
|
from facefusion.vision import conditional_match_frame_color, read_static_image, read_static_video_frame
|
||||||
|
|
||||||
|
|
||||||
@lru_cache()
|
@lru_cache()
|
||||||
@@ -277,25 +276,9 @@ def get_model_size() -> Size:
|
|||||||
def register_args(program : ArgumentParser) -> None:
|
def register_args(program : ArgumentParser) -> None:
|
||||||
group_processors = find_argument_group(program, 'processors')
|
group_processors = find_argument_group(program, 'processors')
|
||||||
if group_processors:
|
if group_processors:
|
||||||
facefusion.capability_store.register_capability_set(
|
group_processors.add_argument('--deep-swapper-model', help = translator.get('help.model', __package__), default = config.get_str_value('processors', 'deep_swapper_model', 'iperov/elon_musk_224'), choices = deep_swapper_choices.deep_swapper_models)
|
||||||
[
|
group_processors.add_argument('--deep-swapper-morph', help = translator.get('help.morph', __package__), type = int, default = config.get_int_value('processors', 'deep_swapper_morph', '100'), choices = deep_swapper_choices.deep_swapper_morph_range, metavar = create_int_metavar(deep_swapper_choices.deep_swapper_morph_range))
|
||||||
group_processors.add_argument(
|
facefusion.args_store.register_args([ 'deep_swapper_model', 'deep_swapper_morph' ], scopes = [ 'api', 'cli' ])
|
||||||
'--deep-swapper-model',
|
|
||||||
help = translator.get('help.model', __package__),
|
|
||||||
default = config.get_str_value('processors', 'deep_swapper_model', 'iperov/elon_musk_224'),
|
|
||||||
choices = deep_swapper_choices.deep_swapper_models
|
|
||||||
),
|
|
||||||
group_processors.add_argument(
|
|
||||||
'--deep-swapper-morph',
|
|
||||||
help = translator.get('help.morph', __package__),
|
|
||||||
type = int,
|
|
||||||
default = config.get_int_value('processors', 'deep_swapper_morph', '100'),
|
|
||||||
choices = deep_swapper_choices.deep_swapper_morph_range,
|
|
||||||
metavar = create_int_metavar(deep_swapper_choices.deep_swapper_morph_range)
|
|
||||||
)
|
|
||||||
],
|
|
||||||
scopes = [ 'api', 'cli' ]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
|
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
|
||||||
@@ -303,18 +286,10 @@ def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
|
|||||||
apply_state_item('deep_swapper_morph', args.get('deep_swapper_morph'))
|
apply_state_item('deep_swapper_morph', args.get('deep_swapper_morph'))
|
||||||
|
|
||||||
|
|
||||||
def get_common_modules() -> List[ModuleType]:
|
|
||||||
return [ content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer ]
|
|
||||||
|
|
||||||
|
|
||||||
def pre_check() -> bool:
|
def pre_check() -> bool:
|
||||||
model_hash_set = get_model_options().get('hashes')
|
model_hash_set = get_model_options().get('hashes')
|
||||||
model_source_set = get_model_options().get('sources')
|
model_source_set = get_model_options().get('sources')
|
||||||
|
|
||||||
for common_module in get_common_modules():
|
|
||||||
if not common_module.pre_check():
|
|
||||||
return False
|
|
||||||
|
|
||||||
if model_hash_set and model_source_set:
|
if model_hash_set and model_source_set:
|
||||||
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
|
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
|
||||||
return True
|
return True
|
||||||
@@ -324,7 +299,6 @@ 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')):
|
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__)
|
logger.error(translator.get('choose_image_or_video_target') + translator.get('exclamation_mark'), __name__)
|
||||||
return False
|
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')):
|
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__)
|
logger.error(translator.get('specify_image_or_video_output') + translator.get('exclamation_mark'), __name__)
|
||||||
return False
|
return False
|
||||||
@@ -334,15 +308,16 @@ def pre_process(mode : ProcessMode) -> bool:
|
|||||||
def post_process() -> None:
|
def post_process() -> None:
|
||||||
read_static_image.cache_clear()
|
read_static_image.cache_clear()
|
||||||
read_static_video_frame.cache_clear()
|
read_static_video_frame.cache_clear()
|
||||||
read_static_video_chunk.cache_clear()
|
|
||||||
video_manager.clear_video_pool()
|
video_manager.clear_video_pool()
|
||||||
|
|
||||||
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
|
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
|
||||||
clear_inference_pool()
|
clear_inference_pool()
|
||||||
|
|
||||||
if state_manager.get_item('video_memory_strategy') == 'strict':
|
if state_manager.get_item('video_memory_strategy') == 'strict':
|
||||||
for common_module in get_common_modules():
|
content_analyser.clear_inference_pool()
|
||||||
common_module.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 swap_face(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
|
def swap_face(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
|
||||||
@@ -433,13 +408,10 @@ def prepare_crop_mask(crop_source_mask : Mask, crop_target_mask : Mask) -> Mask:
|
|||||||
|
|
||||||
def process_frame(inputs : DeepSwapperInputs) -> ProcessorOutputs:
|
def process_frame(inputs : DeepSwapperInputs) -> ProcessorOutputs:
|
||||||
reference_vision_frame = inputs.get('reference_vision_frame')
|
reference_vision_frame = inputs.get('reference_vision_frame')
|
||||||
source_vision_frames = inputs.get('source_vision_frames')
|
target_vision_frame = inputs.get('target_vision_frame')
|
||||||
target_vision_frames = inputs.get('target_vision_frames')
|
|
||||||
temp_vision_frame = inputs.get('temp_vision_frame')
|
temp_vision_frame = inputs.get('temp_vision_frame')
|
||||||
temp_vision_mask = inputs.get('temp_vision_mask')
|
temp_vision_mask = inputs.get('temp_vision_mask')
|
||||||
|
target_faces = select_faces(reference_vision_frame, target_vision_frame)
|
||||||
target_vision_frame = get_middle(target_vision_frames)
|
|
||||||
target_faces = select_faces(reference_vision_frame, source_vision_frames, target_vision_frames)
|
|
||||||
|
|
||||||
if target_faces:
|
if target_faces:
|
||||||
for target_face in target_faces:
|
for target_face in target_faces:
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
from facefusion.types import Locales
|
from facefusion.types import Locals
|
||||||
|
|
||||||
LOCALES : Locales =\
|
LOCALS : Locals =\
|
||||||
{
|
{
|
||||||
'en':
|
'en':
|
||||||
{
|
{
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
from typing import Any, List, TypeAlias, TypedDict
|
from typing import Any, TypeAlias, TypedDict
|
||||||
|
|
||||||
from numpy.typing import NDArray
|
from numpy.typing import NDArray
|
||||||
|
|
||||||
@@ -7,8 +7,7 @@ from facefusion.types import Mask, VisionFrame
|
|||||||
DeepSwapperInputs = TypedDict('DeepSwapperInputs',
|
DeepSwapperInputs = TypedDict('DeepSwapperInputs',
|
||||||
{
|
{
|
||||||
'reference_vision_frame' : VisionFrame,
|
'reference_vision_frame' : VisionFrame,
|
||||||
'source_vision_frames' : List[VisionFrame],
|
'target_vision_frame' : VisionFrame,
|
||||||
'target_vision_frames' : List[VisionFrame],
|
|
||||||
'temp_vision_frame' : VisionFrame,
|
'temp_vision_frame' : VisionFrame,
|
||||||
'temp_vision_mask' : Mask
|
'temp_vision_mask' : Mask
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
from typing import List, Sequence, get_args
|
from typing import List, Sequence
|
||||||
|
|
||||||
from facefusion.common_helper import create_int_range
|
from facefusion.common_helper import create_int_range
|
||||||
from facefusion.processors.modules.expression_restorer.types import ExpressionRestorerArea, ExpressionRestorerModel
|
from facefusion.processors.modules.expression_restorer.types import ExpressionRestorerArea, ExpressionRestorerModel
|
||||||
|
|
||||||
expression_restorer_models : List[ExpressionRestorerModel] = list(get_args(ExpressionRestorerModel))
|
expression_restorer_models : List[ExpressionRestorerModel] = [ 'live_portrait' ]
|
||||||
|
|
||||||
expression_restorer_areas : List[ExpressionRestorerArea] = list(get_args(ExpressionRestorerArea))
|
expression_restorer_areas : List[ExpressionRestorerArea] = [ 'upper-face', 'lower-face' ]
|
||||||
|
|
||||||
expression_restorer_factor_range : Sequence[int] = create_int_range(0, 100, 1)
|
expression_restorer_factor_range : Sequence[int] = create_int_range(0, 100, 1)
|
||||||
|
|||||||
@@ -1,17 +1,16 @@
|
|||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
from types import ModuleType
|
from typing import Tuple
|
||||||
from typing import List, Tuple
|
|
||||||
|
|
||||||
import cv2
|
import cv2
|
||||||
import numpy
|
import numpy
|
||||||
|
|
||||||
import facefusion.capability_store
|
import facefusion.args_store
|
||||||
import facefusion.jobs.job_manager
|
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 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.common_helper import create_int_metavar
|
||||||
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
|
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
|
||||||
from facefusion.face_creator import scale_face
|
from facefusion.face_analyser import scale_face
|
||||||
from facefusion.face_helper import paste_back, warp_face_by_face_landmark_5
|
from facefusion.face_helper import paste_back, warp_face_by_face_landmark_5
|
||||||
from facefusion.face_masker import create_box_mask, create_occlusion_mask
|
from facefusion.face_masker import create_box_mask, create_occlusion_mask
|
||||||
from facefusion.face_selector import select_faces
|
from facefusion.face_selector import select_faces
|
||||||
@@ -19,11 +18,11 @@ from facefusion.filesystem import in_directory, is_image, is_video, resolve_rela
|
|||||||
from facefusion.processors.live_portrait import create_rotation, limit_expression
|
from facefusion.processors.live_portrait import create_rotation, limit_expression
|
||||||
from facefusion.processors.modules.expression_restorer import choices as expression_restorer_choices
|
from facefusion.processors.modules.expression_restorer import choices as expression_restorer_choices
|
||||||
from facefusion.processors.modules.expression_restorer.types import ExpressionRestorerInputs
|
from facefusion.processors.modules.expression_restorer.types import ExpressionRestorerInputs
|
||||||
from facefusion.processors.types import ApplyStateItem, LivePortraitExpression, LivePortraitFeatureVolume, LivePortraitMotionPoints, LivePortraitPitch, LivePortraitRoll, LivePortraitScale, LivePortraitTranslation, LivePortraitYaw, ProcessorOutputs
|
from facefusion.processors.types import LivePortraitExpression, LivePortraitFeatureVolume, LivePortraitMotionPoints, LivePortraitPitch, LivePortraitRoll, LivePortraitScale, LivePortraitTranslation, LivePortraitYaw, ProcessorOutputs
|
||||||
from facefusion.program_helper import find_argument_group
|
from facefusion.program_helper import find_argument_group
|
||||||
from facefusion.thread_helper import conditional_thread_semaphore, thread_semaphore
|
from facefusion.thread_helper import conditional_thread_semaphore, thread_semaphore
|
||||||
from facefusion.types import Args, DownloadScope, Face, InferencePool, ModelOptions, ModelSet, ProcessMode, VisionFrame
|
from facefusion.types import ApplyStateItem, Args, DownloadScope, Face, InferencePool, ModelOptions, ModelSet, ProcessMode, VisionFrame
|
||||||
from facefusion.vision import read_static_image, read_static_video_chunk, read_static_video_frame
|
from facefusion.vision import read_static_image, read_static_video_frame
|
||||||
|
|
||||||
|
|
||||||
@lru_cache()
|
@lru_cache()
|
||||||
@@ -100,33 +99,10 @@ def get_model_options() -> ModelOptions:
|
|||||||
def register_args(program : ArgumentParser) -> None:
|
def register_args(program : ArgumentParser) -> None:
|
||||||
group_processors = find_argument_group(program, 'processors')
|
group_processors = find_argument_group(program, 'processors')
|
||||||
if group_processors:
|
if group_processors:
|
||||||
facefusion.capability_store.register_capability_set(
|
group_processors.add_argument('--expression-restorer-model', help = translator.get('help.model', __package__), default = config.get_str_value('processors', 'expression_restorer_model', 'live_portrait'), choices = expression_restorer_choices.expression_restorer_models)
|
||||||
[
|
group_processors.add_argument('--expression-restorer-factor', help = translator.get('help.factor', __package__), type = int, default = config.get_int_value('processors', 'expression_restorer_factor', '80'), choices = expression_restorer_choices.expression_restorer_factor_range, metavar = create_int_metavar(expression_restorer_choices.expression_restorer_factor_range))
|
||||||
group_processors.add_argument(
|
group_processors.add_argument('--expression-restorer-areas', help = translator.get('help.areas', __package__).format(choices = ', '.join(expression_restorer_choices.expression_restorer_areas)), default = config.get_str_list('processors', 'expression_restorer_areas', ' '.join(expression_restorer_choices.expression_restorer_areas)), choices = expression_restorer_choices.expression_restorer_areas, nargs ='+', metavar ='EXPRESSION_RESTORER_AREAS')
|
||||||
'--expression-restorer-model',
|
facefusion.args_store.register_args([ 'expression_restorer_model', 'expression_restorer_factor', 'expression_restorer_areas' ], scopes = [ 'api', 'cli' ])
|
||||||
help = translator.get('help.model', __package__),
|
|
||||||
default = config.get_str_value('processors', 'expression_restorer_model', 'live_portrait'),
|
|
||||||
choices = expression_restorer_choices.expression_restorer_models
|
|
||||||
),
|
|
||||||
group_processors.add_argument(
|
|
||||||
'--expression-restorer-factor',
|
|
||||||
help = translator.get('help.factor', __package__),
|
|
||||||
type = int,
|
|
||||||
default = config.get_int_value('processors', 'expression_restorer_factor', '80'),
|
|
||||||
choices = expression_restorer_choices.expression_restorer_factor_range,
|
|
||||||
metavar = create_int_metavar(expression_restorer_choices.expression_restorer_factor_range)
|
|
||||||
),
|
|
||||||
group_processors.add_argument(
|
|
||||||
'--expression-restorer-areas',
|
|
||||||
help = translator.get('help.areas', __package__).format(choices = ', '.join(expression_restorer_choices.expression_restorer_areas)),
|
|
||||||
default = config.get_str_list('processors', 'expression_restorer_areas', ' '.join(expression_restorer_choices.expression_restorer_areas)),
|
|
||||||
choices = expression_restorer_choices.expression_restorer_areas,
|
|
||||||
nargs = '+',
|
|
||||||
metavar = 'EXPRESSION_RESTORER_AREAS'
|
|
||||||
)
|
|
||||||
],
|
|
||||||
scopes = [ 'api', 'cli' ]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
|
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
|
||||||
@@ -135,18 +111,10 @@ def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
|
|||||||
apply_state_item('expression_restorer_areas', args.get('expression_restorer_areas'))
|
apply_state_item('expression_restorer_areas', args.get('expression_restorer_areas'))
|
||||||
|
|
||||||
|
|
||||||
def get_common_modules() -> List[ModuleType]:
|
|
||||||
return [ content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer ]
|
|
||||||
|
|
||||||
|
|
||||||
def pre_check() -> bool:
|
def pre_check() -> bool:
|
||||||
model_hash_set = get_model_options().get('hashes')
|
model_hash_set = get_model_options().get('hashes')
|
||||||
model_source_set = get_model_options().get('sources')
|
model_source_set = get_model_options().get('sources')
|
||||||
|
|
||||||
for common_module in get_common_modules():
|
|
||||||
if not common_module.pre_check():
|
|
||||||
return False
|
|
||||||
|
|
||||||
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
|
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
|
||||||
|
|
||||||
|
|
||||||
@@ -157,7 +125,6 @@ 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')):
|
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__)
|
logger.error(translator.get('choose_image_or_video_target') + translator.get('exclamation_mark'), __name__)
|
||||||
return False
|
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')):
|
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__)
|
logger.error(translator.get('specify_image_or_video_output') + translator.get('exclamation_mark'), __name__)
|
||||||
return False
|
return False
|
||||||
@@ -167,15 +134,16 @@ def pre_process(mode : ProcessMode) -> bool:
|
|||||||
def post_process() -> None:
|
def post_process() -> None:
|
||||||
read_static_image.cache_clear()
|
read_static_image.cache_clear()
|
||||||
read_static_video_frame.cache_clear()
|
read_static_video_frame.cache_clear()
|
||||||
read_static_video_chunk.cache_clear()
|
|
||||||
video_manager.clear_video_pool()
|
video_manager.clear_video_pool()
|
||||||
|
|
||||||
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
|
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
|
||||||
clear_inference_pool()
|
clear_inference_pool()
|
||||||
|
|
||||||
if state_manager.get_item('video_memory_strategy') == 'strict':
|
if state_manager.get_item('video_memory_strategy') == 'strict':
|
||||||
for common_module in get_common_modules():
|
content_analyser.clear_inference_pool()
|
||||||
common_module.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 restore_expression(target_face : Face, target_vision_frame : VisionFrame, temp_vision_frame : VisionFrame) -> VisionFrame:
|
def restore_expression(target_face : Face, target_vision_frame : VisionFrame, temp_vision_frame : VisionFrame) -> VisionFrame:
|
||||||
@@ -286,13 +254,10 @@ def normalize_crop_frame(crop_vision_frame : VisionFrame) -> VisionFrame:
|
|||||||
|
|
||||||
def process_frame(inputs : ExpressionRestorerInputs) -> ProcessorOutputs:
|
def process_frame(inputs : ExpressionRestorerInputs) -> ProcessorOutputs:
|
||||||
reference_vision_frame = inputs.get('reference_vision_frame')
|
reference_vision_frame = inputs.get('reference_vision_frame')
|
||||||
source_vision_frames = inputs.get('source_vision_frames')
|
target_vision_frame = inputs.get('target_vision_frame')
|
||||||
target_vision_frames = inputs.get('target_vision_frames')
|
|
||||||
temp_vision_frame = inputs.get('temp_vision_frame')
|
temp_vision_frame = inputs.get('temp_vision_frame')
|
||||||
temp_vision_mask = inputs.get('temp_vision_mask')
|
temp_vision_mask = inputs.get('temp_vision_mask')
|
||||||
|
target_faces = select_faces(reference_vision_frame, target_vision_frame)
|
||||||
target_vision_frame = get_middle(target_vision_frames)
|
|
||||||
target_faces = select_faces(reference_vision_frame, source_vision_frames, target_vision_frames)
|
|
||||||
|
|
||||||
if target_faces:
|
if target_faces:
|
||||||
for target_face in target_faces:
|
for target_face in target_faces:
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
from facefusion.types import Locales
|
from facefusion.types import Locals
|
||||||
|
|
||||||
LOCALES : Locales =\
|
LOCALS : Locals =\
|
||||||
{
|
{
|
||||||
'en':
|
'en':
|
||||||
{
|
{
|
||||||
@@ -6,7 +6,7 @@ ExpressionRestorerInputs = TypedDict('ExpressionRestorerInputs',
|
|||||||
{
|
{
|
||||||
'reference_vision_frame' : VisionFrame,
|
'reference_vision_frame' : VisionFrame,
|
||||||
'source_vision_frames' : List[VisionFrame],
|
'source_vision_frames' : List[VisionFrame],
|
||||||
'target_vision_frames' : List[VisionFrame],
|
'target_vision_frame' : VisionFrame,
|
||||||
'temp_vision_frame' : VisionFrame,
|
'temp_vision_frame' : VisionFrame,
|
||||||
'temp_vision_mask' : Mask
|
'temp_vision_mask' : Mask
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
from typing import List, get_args
|
from typing import List
|
||||||
|
|
||||||
from facefusion.processors.modules.face_debugger.types import FaceDebuggerItem
|
from facefusion.processors.modules.face_debugger.types import FaceDebuggerItem
|
||||||
|
|
||||||
face_debugger_items : List[FaceDebuggerItem] = list(get_args(FaceDebuggerItem))
|
face_debugger_items : List[FaceDebuggerItem] = [ 'bounding-box', 'face-landmark-5', 'face-landmark-5/68', 'face-landmark-68', 'face-landmark-68/5', 'face-mask' ]
|
||||||
|
|||||||
@@ -1,25 +1,22 @@
|
|||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
from types import ModuleType
|
|
||||||
from typing import List
|
|
||||||
|
|
||||||
import cv2
|
import cv2
|
||||||
import numpy
|
import numpy
|
||||||
|
|
||||||
import facefusion.capability_store
|
import facefusion.args_store
|
||||||
import facefusion.jobs.job_manager
|
import facefusion.jobs.job_manager
|
||||||
from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, logger, state_manager, translator, video_manager
|
from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, logger, state_manager, translator, video_manager
|
||||||
from facefusion.common_helper import get_middle
|
from facefusion.face_analyser import scale_face
|
||||||
from facefusion.face_creator import scale_face
|
|
||||||
from facefusion.face_helper import warp_face_by_face_landmark_5
|
from facefusion.face_helper import warp_face_by_face_landmark_5
|
||||||
from facefusion.face_masker import create_area_mask, create_box_mask, create_occlusion_mask, create_region_mask
|
from facefusion.face_masker import create_area_mask, create_box_mask, create_occlusion_mask, create_region_mask
|
||||||
from facefusion.face_selector import select_faces
|
from facefusion.face_selector import select_faces
|
||||||
from facefusion.filesystem import in_directory, is_image, is_video
|
from facefusion.filesystem import in_directory, is_image, is_video
|
||||||
from facefusion.processors.modules.face_debugger import choices as face_debugger_choices
|
from facefusion.processors.modules.face_debugger import choices as face_debugger_choices
|
||||||
from facefusion.processors.modules.face_debugger.types import FaceDebuggerInputs
|
from facefusion.processors.modules.face_debugger.types import FaceDebuggerInputs
|
||||||
from facefusion.processors.types import ApplyStateItem, ProcessorOutputs
|
from facefusion.processors.types import ProcessorOutputs
|
||||||
from facefusion.program_helper import find_argument_group
|
from facefusion.program_helper import find_argument_group
|
||||||
from facefusion.types import Args, Face, InferencePool, ProcessMode, VisionFrame
|
from facefusion.types import ApplyStateItem, Args, Face, InferencePool, ProcessMode, VisionFrame
|
||||||
from facefusion.vision import read_static_image, read_static_video_chunk, read_static_video_frame
|
from facefusion.vision import read_static_image, read_static_video_frame
|
||||||
|
|
||||||
|
|
||||||
def get_inference_pool() -> InferencePool:
|
def get_inference_pool() -> InferencePool:
|
||||||
@@ -33,33 +30,15 @@ def clear_inference_pool() -> None:
|
|||||||
def register_args(program : ArgumentParser) -> None:
|
def register_args(program : ArgumentParser) -> None:
|
||||||
group_processors = find_argument_group(program, 'processors')
|
group_processors = find_argument_group(program, 'processors')
|
||||||
if group_processors:
|
if group_processors:
|
||||||
facefusion.capability_store.register_capability_set(
|
group_processors.add_argument('--face-debugger-items', help = translator.get('help.items', __package__).format(choices = ', '.join(face_debugger_choices.face_debugger_items)), default = config.get_str_list('processors', 'face_debugger_items', 'face-landmark-5/68 face-mask'), choices = face_debugger_choices.face_debugger_items, nargs = '+', metavar = 'FACE_DEBUGGER_ITEMS')
|
||||||
[
|
facefusion.args_store.register_args([ 'face_debugger_items' ], scopes = [ 'api', 'cli' ])
|
||||||
group_processors.add_argument(
|
|
||||||
'--face-debugger-items',
|
|
||||||
help = translator.get('help.items', __package__).format(choices = ', '.join(face_debugger_choices.face_debugger_items)),
|
|
||||||
default = config.get_str_list('processors', 'face_debugger_items', 'face-landmark-5/68 face-mask'),
|
|
||||||
choices = face_debugger_choices.face_debugger_items,
|
|
||||||
nargs = '+',
|
|
||||||
metavar = 'FACE_DEBUGGER_ITEMS'
|
|
||||||
)
|
|
||||||
],
|
|
||||||
scopes = [ 'api', 'cli' ]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
|
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
|
||||||
apply_state_item('face_debugger_items', args.get('face_debugger_items'))
|
apply_state_item('face_debugger_items', args.get('face_debugger_items'))
|
||||||
|
|
||||||
|
|
||||||
def get_common_modules() -> List[ModuleType]:
|
|
||||||
return [ content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer ]
|
|
||||||
|
|
||||||
|
|
||||||
def pre_check() -> bool:
|
def pre_check() -> bool:
|
||||||
for common_module in get_common_modules():
|
|
||||||
if not common_module.pre_check():
|
|
||||||
return False
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
@@ -67,7 +46,6 @@ 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')):
|
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__)
|
logger.error(translator.get('choose_image_or_video_target') + translator.get('exclamation_mark'), __name__)
|
||||||
return False
|
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')):
|
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__)
|
logger.error(translator.get('specify_image_or_video_output') + translator.get('exclamation_mark'), __name__)
|
||||||
return False
|
return False
|
||||||
@@ -77,12 +55,14 @@ def pre_process(mode : ProcessMode) -> bool:
|
|||||||
def post_process() -> None:
|
def post_process() -> None:
|
||||||
read_static_image.cache_clear()
|
read_static_image.cache_clear()
|
||||||
read_static_video_frame.cache_clear()
|
read_static_video_frame.cache_clear()
|
||||||
read_static_video_chunk.cache_clear()
|
|
||||||
video_manager.clear_video_pool()
|
video_manager.clear_video_pool()
|
||||||
|
|
||||||
if state_manager.get_item('video_memory_strategy') == 'strict':
|
if state_manager.get_item('video_memory_strategy') == 'strict':
|
||||||
for common_module in get_common_modules():
|
content_analyser.clear_inference_pool()
|
||||||
common_module.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 debug_face(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
|
def debug_face(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
|
||||||
@@ -111,22 +91,21 @@ def debug_face(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFra
|
|||||||
|
|
||||||
def draw_bounding_box(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
|
def draw_bounding_box(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
|
||||||
temp_vision_frame = numpy.ascontiguousarray(temp_vision_frame)
|
temp_vision_frame = numpy.ascontiguousarray(temp_vision_frame)
|
||||||
|
box_color = 0, 0, 255
|
||||||
|
border_color = 100, 100, 255
|
||||||
bounding_box = target_face.bounding_box.astype(numpy.int32)
|
bounding_box = target_face.bounding_box.astype(numpy.int32)
|
||||||
x1, y1, x2, y2 = bounding_box
|
x1, y1, x2, y2 = bounding_box
|
||||||
box_color = 0, 0, 255
|
|
||||||
border_scale = calculate_scale(temp_vision_frame)
|
|
||||||
border_color = 100, 100, 255
|
|
||||||
|
|
||||||
cv2.rectangle(temp_vision_frame, (x1, y1), (x2, y2), box_color, border_scale)
|
cv2.rectangle(temp_vision_frame, (x1, y1), (x2, y2), box_color, 2)
|
||||||
|
|
||||||
if target_face.angle == 0:
|
if target_face.angle == 0:
|
||||||
cv2.line(temp_vision_frame, (x1, y1), (x2, y1), border_color, border_scale + 1)
|
cv2.line(temp_vision_frame, (x1, y1), (x2, y1), border_color, 3)
|
||||||
if target_face.angle == 180:
|
if target_face.angle == 180:
|
||||||
cv2.line(temp_vision_frame, (x1, y2), (x2, y2), border_color, border_scale + 1)
|
cv2.line(temp_vision_frame, (x1, y2), (x2, y2), border_color, 3)
|
||||||
if target_face.angle == 90:
|
if target_face.angle == 90:
|
||||||
cv2.line(temp_vision_frame, (x2, y1), (x2, y2), border_color, border_scale + 1)
|
cv2.line(temp_vision_frame, (x2, y1), (x2, y2), border_color, 3)
|
||||||
if target_face.angle == 270:
|
if target_face.angle == 270:
|
||||||
cv2.line(temp_vision_frame, (x1, y1), (x1, y2), border_color, border_scale + 1)
|
cv2.line(temp_vision_frame, (x1, y1), (x1, y2), border_color, 3)
|
||||||
|
|
||||||
return temp_vision_frame
|
return temp_vision_frame
|
||||||
|
|
||||||
@@ -140,15 +119,11 @@ def draw_face_mask(target_face : Face, temp_vision_frame : VisionFrame) -> Visio
|
|||||||
crop_vision_frame, affine_matrix = warp_face_by_face_landmark_5(temp_vision_frame, face_landmark_5_68, 'arcface_128', (512, 512))
|
crop_vision_frame, affine_matrix = warp_face_by_face_landmark_5(temp_vision_frame, face_landmark_5_68, 'arcface_128', (512, 512))
|
||||||
inverse_matrix = cv2.invertAffineTransform(affine_matrix)
|
inverse_matrix = cv2.invertAffineTransform(affine_matrix)
|
||||||
temp_size = temp_vision_frame.shape[:2][::-1]
|
temp_size = temp_vision_frame.shape[:2][::-1]
|
||||||
mask_scale = calculate_scale(temp_vision_frame)
|
|
||||||
mask_color = 0, 255, 0
|
mask_color = 0, 255, 0
|
||||||
|
|
||||||
if numpy.array_equal(face_landmark_5, face_landmark_5_68):
|
if numpy.array_equal(face_landmark_5, face_landmark_5_68):
|
||||||
mask_color = 255, 255, 0
|
mask_color = 255, 255, 0
|
||||||
|
|
||||||
if target_face.origin == 'refill':
|
|
||||||
mask_color = 0, 165, 255
|
|
||||||
|
|
||||||
if 'box' in state_manager.get_item('face_mask_types'):
|
if 'box' in state_manager.get_item('face_mask_types'):
|
||||||
box_mask = create_box_mask(crop_vision_frame, 0, state_manager.get_item('face_mask_padding'))
|
box_mask = create_box_mask(crop_vision_frame, 0, state_manager.get_item('face_mask_padding'))
|
||||||
crop_masks.append(box_mask)
|
crop_masks.append(box_mask)
|
||||||
@@ -171,7 +146,7 @@ def draw_face_mask(target_face : Face, temp_vision_frame : VisionFrame) -> Visio
|
|||||||
inverse_vision_frame = cv2.warpAffine(crop_mask, inverse_matrix, temp_size)
|
inverse_vision_frame = cv2.warpAffine(crop_mask, inverse_matrix, temp_size)
|
||||||
inverse_vision_frame = cv2.threshold(inverse_vision_frame, 100, 255, cv2.THRESH_BINARY)[1]
|
inverse_vision_frame = cv2.threshold(inverse_vision_frame, 100, 255, cv2.THRESH_BINARY)[1]
|
||||||
inverse_contours, _ = cv2.findContours(inverse_vision_frame, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
|
inverse_contours, _ = cv2.findContours(inverse_vision_frame, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
|
||||||
cv2.drawContours(temp_vision_frame, inverse_contours, -1, mask_color, mask_scale)
|
cv2.drawContours(temp_vision_frame, inverse_contours, -1, mask_color, 2)
|
||||||
|
|
||||||
return temp_vision_frame
|
return temp_vision_frame
|
||||||
|
|
||||||
@@ -179,17 +154,13 @@ def draw_face_mask(target_face : Face, temp_vision_frame : VisionFrame) -> Visio
|
|||||||
def draw_face_landmark_5(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
|
def draw_face_landmark_5(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
|
||||||
temp_vision_frame = numpy.ascontiguousarray(temp_vision_frame)
|
temp_vision_frame = numpy.ascontiguousarray(temp_vision_frame)
|
||||||
face_landmark_5 = target_face.landmark_set.get('5')
|
face_landmark_5 = target_face.landmark_set.get('5')
|
||||||
point_scale = calculate_scale(temp_vision_frame)
|
|
||||||
point_color = 0, 0, 255
|
point_color = 0, 0, 255
|
||||||
|
|
||||||
if target_face.origin == 'refill':
|
|
||||||
point_color = 0, 165, 255
|
|
||||||
|
|
||||||
if numpy.any(face_landmark_5):
|
if numpy.any(face_landmark_5):
|
||||||
face_landmark_5 = face_landmark_5.astype(numpy.int32)
|
face_landmark_5 = face_landmark_5.astype(numpy.int32)
|
||||||
|
|
||||||
for point in face_landmark_5:
|
for point in face_landmark_5:
|
||||||
cv2.circle(temp_vision_frame, tuple(point), point_scale, point_color, -1)
|
cv2.circle(temp_vision_frame, tuple(point), 3, point_color, -1)
|
||||||
|
|
||||||
return temp_vision_frame
|
return temp_vision_frame
|
||||||
|
|
||||||
@@ -198,20 +169,16 @@ def draw_face_landmark_5_68(target_face : Face, temp_vision_frame : VisionFrame)
|
|||||||
temp_vision_frame = numpy.ascontiguousarray(temp_vision_frame)
|
temp_vision_frame = numpy.ascontiguousarray(temp_vision_frame)
|
||||||
face_landmark_5 = target_face.landmark_set.get('5')
|
face_landmark_5 = target_face.landmark_set.get('5')
|
||||||
face_landmark_5_68 = target_face.landmark_set.get('5/68')
|
face_landmark_5_68 = target_face.landmark_set.get('5/68')
|
||||||
point_scale = calculate_scale(temp_vision_frame)
|
|
||||||
point_color = 0, 255, 0
|
point_color = 0, 255, 0
|
||||||
|
|
||||||
if numpy.array_equal(face_landmark_5, face_landmark_5_68):
|
if numpy.array_equal(face_landmark_5, face_landmark_5_68):
|
||||||
point_color = 255, 255, 0
|
point_color = 255, 255, 0
|
||||||
|
|
||||||
if target_face.origin == 'refill':
|
|
||||||
point_color = 0, 165, 255
|
|
||||||
|
|
||||||
if numpy.any(face_landmark_5_68):
|
if numpy.any(face_landmark_5_68):
|
||||||
face_landmark_5_68 = face_landmark_5_68.astype(numpy.int32)
|
face_landmark_5_68 = face_landmark_5_68.astype(numpy.int32)
|
||||||
|
|
||||||
for point in face_landmark_5_68:
|
for point in face_landmark_5_68:
|
||||||
cv2.circle(temp_vision_frame, tuple(point), point_scale, point_color, -1)
|
cv2.circle(temp_vision_frame, tuple(point), 3, point_color, -1)
|
||||||
|
|
||||||
return temp_vision_frame
|
return temp_vision_frame
|
||||||
|
|
||||||
@@ -220,20 +187,16 @@ def draw_face_landmark_68(target_face : Face, temp_vision_frame : VisionFrame) -
|
|||||||
temp_vision_frame = numpy.ascontiguousarray(temp_vision_frame)
|
temp_vision_frame = numpy.ascontiguousarray(temp_vision_frame)
|
||||||
face_landmark_68 = target_face.landmark_set.get('68')
|
face_landmark_68 = target_face.landmark_set.get('68')
|
||||||
face_landmark_68_5 = target_face.landmark_set.get('68/5')
|
face_landmark_68_5 = target_face.landmark_set.get('68/5')
|
||||||
point_scale = calculate_scale(temp_vision_frame)
|
|
||||||
point_color = 0, 255, 0
|
point_color = 0, 255, 0
|
||||||
|
|
||||||
if numpy.array_equal(face_landmark_68, face_landmark_68_5):
|
if numpy.array_equal(face_landmark_68, face_landmark_68_5):
|
||||||
point_color = 255, 255, 0
|
point_color = 255, 255, 0
|
||||||
|
|
||||||
if target_face.origin == 'refill':
|
|
||||||
point_color = 0, 165, 255
|
|
||||||
|
|
||||||
if numpy.any(face_landmark_68):
|
if numpy.any(face_landmark_68):
|
||||||
face_landmark_68 = face_landmark_68.astype(numpy.int32)
|
face_landmark_68 = face_landmark_68.astype(numpy.int32)
|
||||||
|
|
||||||
for point in face_landmark_68:
|
for point in face_landmark_68:
|
||||||
cv2.circle(temp_vision_frame, tuple(point), point_scale, point_color, -1)
|
cv2.circle(temp_vision_frame, tuple(point), 3, point_color, -1)
|
||||||
|
|
||||||
return temp_vision_frame
|
return temp_vision_frame
|
||||||
|
|
||||||
@@ -241,36 +204,23 @@ def draw_face_landmark_68(target_face : Face, temp_vision_frame : VisionFrame) -
|
|||||||
def draw_face_landmark_68_5(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
|
def draw_face_landmark_68_5(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
|
||||||
temp_vision_frame = numpy.ascontiguousarray(temp_vision_frame)
|
temp_vision_frame = numpy.ascontiguousarray(temp_vision_frame)
|
||||||
face_landmark_68_5 = target_face.landmark_set.get('68/5')
|
face_landmark_68_5 = target_face.landmark_set.get('68/5')
|
||||||
point_scale = calculate_scale(temp_vision_frame)
|
|
||||||
point_color = 255, 255, 0
|
point_color = 255, 255, 0
|
||||||
|
|
||||||
if target_face.origin == 'refill':
|
|
||||||
point_color = 0, 165, 255
|
|
||||||
|
|
||||||
if numpy.any(face_landmark_68_5):
|
if numpy.any(face_landmark_68_5):
|
||||||
face_landmark_68_5 = face_landmark_68_5.astype(numpy.int32)
|
face_landmark_68_5 = face_landmark_68_5.astype(numpy.int32)
|
||||||
|
|
||||||
for point in face_landmark_68_5:
|
for point in face_landmark_68_5:
|
||||||
cv2.circle(temp_vision_frame, tuple(point), point_scale, point_color, -1)
|
cv2.circle(temp_vision_frame, tuple(point), 3, point_color, -1)
|
||||||
|
|
||||||
return temp_vision_frame
|
return temp_vision_frame
|
||||||
|
|
||||||
|
|
||||||
def calculate_scale(temp_vision_frame : VisionFrame) -> int:
|
|
||||||
frame_height, _ = temp_vision_frame.shape[:2]
|
|
||||||
frame_scale = round(frame_height / 270)
|
|
||||||
return max(1, min(10, frame_scale))
|
|
||||||
|
|
||||||
|
|
||||||
def process_frame(inputs : FaceDebuggerInputs) -> ProcessorOutputs:
|
def process_frame(inputs : FaceDebuggerInputs) -> ProcessorOutputs:
|
||||||
reference_vision_frame = inputs.get('reference_vision_frame')
|
reference_vision_frame = inputs.get('reference_vision_frame')
|
||||||
source_vision_frames = inputs.get('source_vision_frames')
|
target_vision_frame = inputs.get('target_vision_frame')
|
||||||
target_vision_frames = inputs.get('target_vision_frames')
|
|
||||||
temp_vision_frame = inputs.get('temp_vision_frame')
|
temp_vision_frame = inputs.get('temp_vision_frame')
|
||||||
temp_vision_mask = inputs.get('temp_vision_mask')
|
temp_vision_mask = inputs.get('temp_vision_mask')
|
||||||
|
target_faces = select_faces(reference_vision_frame, target_vision_frame)
|
||||||
target_vision_frame = get_middle(target_vision_frames)
|
|
||||||
target_faces = select_faces(reference_vision_frame, source_vision_frames, target_vision_frames)
|
|
||||||
|
|
||||||
if target_faces:
|
if target_faces:
|
||||||
for target_face in target_faces:
|
for target_face in target_faces:
|
||||||
@@ -278,3 +228,5 @@ def process_frame(inputs : FaceDebuggerInputs) -> ProcessorOutputs:
|
|||||||
temp_vision_frame = debug_face(target_face, temp_vision_frame)
|
temp_vision_frame = debug_face(target_face, temp_vision_frame)
|
||||||
|
|
||||||
return temp_vision_frame, temp_vision_mask
|
return temp_vision_frame, temp_vision_mask
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
from facefusion.types import Locales
|
from facefusion.types import Locals
|
||||||
|
|
||||||
LOCALES : Locales =\
|
LOCALS : Locals =\
|
||||||
{
|
{
|
||||||
'en':
|
'en':
|
||||||
{
|
{
|
||||||
@@ -1,12 +1,11 @@
|
|||||||
from typing import List, Literal, TypedDict
|
from typing import Literal, TypedDict
|
||||||
|
|
||||||
from facefusion.types import Mask, VisionFrame
|
from facefusion.types import Mask, VisionFrame
|
||||||
|
|
||||||
FaceDebuggerInputs = TypedDict('FaceDebuggerInputs',
|
FaceDebuggerInputs = TypedDict('FaceDebuggerInputs',
|
||||||
{
|
{
|
||||||
'reference_vision_frame' : VisionFrame,
|
'reference_vision_frame' : VisionFrame,
|
||||||
'source_vision_frames' : List[VisionFrame],
|
'target_vision_frame' : VisionFrame,
|
||||||
'target_vision_frames' : List[VisionFrame],
|
|
||||||
'temp_vision_frame' : VisionFrame,
|
'temp_vision_frame' : VisionFrame,
|
||||||
'temp_vision_mask' : Mask
|
'temp_vision_mask' : Mask
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
from typing import List, Sequence, get_args
|
from typing import List, Sequence
|
||||||
|
|
||||||
from facefusion.common_helper import create_float_range
|
from facefusion.common_helper import create_float_range
|
||||||
from facefusion.processors.modules.face_editor.types import FaceEditorModel
|
from facefusion.processors.modules.face_editor.types import FaceEditorModel
|
||||||
|
|
||||||
face_editor_models : List[FaceEditorModel] = list(get_args(FaceEditorModel))
|
face_editor_models : List[FaceEditorModel] = [ 'live_portrait' ]
|
||||||
|
|
||||||
face_editor_eyebrow_direction_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
|
face_editor_eyebrow_direction_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
|
||||||
face_editor_eye_gaze_horizontal_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
|
face_editor_eye_gaze_horizontal_range : Sequence[float] = create_float_range(-1.0, 1.0, 0.05)
|
||||||
|
|||||||
@@ -1,17 +1,16 @@
|
|||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
from types import ModuleType
|
from typing import Tuple
|
||||||
from typing import List, Tuple
|
|
||||||
|
|
||||||
import cv2
|
import cv2
|
||||||
import numpy
|
import numpy
|
||||||
|
|
||||||
import facefusion.capability_store
|
import facefusion.args_store
|
||||||
import facefusion.jobs.job_manager
|
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 import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, inference_manager, logger, state_manager, translator, video_manager
|
||||||
from facefusion.common_helper import create_float_metavar, get_middle
|
from facefusion.common_helper import create_float_metavar
|
||||||
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
|
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
|
||||||
from facefusion.face_creator import scale_face
|
from facefusion.face_analyser import scale_face
|
||||||
from facefusion.face_helper import paste_back, scale_face_landmark_5, warp_face_by_face_landmark_5
|
from facefusion.face_helper import paste_back, scale_face_landmark_5, warp_face_by_face_landmark_5
|
||||||
from facefusion.face_masker import create_box_mask
|
from facefusion.face_masker import create_box_mask
|
||||||
from facefusion.face_selector import select_faces
|
from facefusion.face_selector import select_faces
|
||||||
@@ -19,11 +18,11 @@ from facefusion.filesystem import in_directory, is_image, is_video, resolve_rela
|
|||||||
from facefusion.processors.live_portrait import create_rotation, limit_angle, limit_expression
|
from facefusion.processors.live_portrait import create_rotation, limit_angle, limit_expression
|
||||||
from facefusion.processors.modules.face_editor import choices as face_editor_choices
|
from facefusion.processors.modules.face_editor import choices as face_editor_choices
|
||||||
from facefusion.processors.modules.face_editor.types import FaceEditorInputs
|
from facefusion.processors.modules.face_editor.types import FaceEditorInputs
|
||||||
from facefusion.processors.types import ApplyStateItem, LivePortraitExpression, LivePortraitFeatureVolume, LivePortraitMotionPoints, LivePortraitPitch, LivePortraitRoll, LivePortraitRotation, LivePortraitScale, LivePortraitTranslation, LivePortraitYaw, ProcessorOutputs
|
from facefusion.processors.types import LivePortraitExpression, LivePortraitFeatureVolume, LivePortraitMotionPoints, LivePortraitPitch, LivePortraitRoll, LivePortraitRotation, LivePortraitScale, LivePortraitTranslation, LivePortraitYaw, ProcessorOutputs
|
||||||
from facefusion.program_helper import find_argument_group
|
from facefusion.program_helper import find_argument_group
|
||||||
from facefusion.thread_helper import conditional_thread_semaphore, thread_semaphore
|
from facefusion.thread_helper import conditional_thread_semaphore, thread_semaphore
|
||||||
from facefusion.types import Args, DownloadScope, Face, FaceLandmark68, InferencePool, ModelOptions, ModelSet, ProcessMode, VisionFrame
|
from facefusion.types import ApplyStateItem, Args, DownloadScope, Face, FaceLandmark68, InferencePool, ModelOptions, ModelSet, ProcessMode, VisionFrame
|
||||||
from facefusion.vision import read_static_image, read_static_video_chunk, read_static_video_frame
|
from facefusion.vision import read_static_image, read_static_video_frame
|
||||||
|
|
||||||
|
|
||||||
@lru_cache()
|
@lru_cache()
|
||||||
@@ -130,129 +129,22 @@ def get_model_options() -> ModelOptions:
|
|||||||
def register_args(program : ArgumentParser) -> None:
|
def register_args(program : ArgumentParser) -> None:
|
||||||
group_processors = find_argument_group(program, 'processors')
|
group_processors = find_argument_group(program, 'processors')
|
||||||
if group_processors:
|
if group_processors:
|
||||||
facefusion.capability_store.register_capability_set(
|
group_processors.add_argument('--face-editor-model', help = translator.get('help.model', __package__), default = config.get_str_value('processors', 'face_editor_model', 'live_portrait'), choices = face_editor_choices.face_editor_models)
|
||||||
[
|
group_processors.add_argument('--face-editor-eyebrow-direction', help = translator.get('help.eyebrow_direction', __package__), type = float, default = config.get_float_value('processors', 'face_editor_eyebrow_direction', '0'), choices = face_editor_choices.face_editor_eyebrow_direction_range, metavar = create_float_metavar(face_editor_choices.face_editor_eyebrow_direction_range))
|
||||||
group_processors.add_argument(
|
group_processors.add_argument('--face-editor-eye-gaze-horizontal', help = translator.get('help.eye_gaze_horizontal', __package__), type = float, default = config.get_float_value('processors', 'face_editor_eye_gaze_horizontal', '0'), choices = face_editor_choices.face_editor_eye_gaze_horizontal_range, metavar = create_float_metavar(face_editor_choices.face_editor_eye_gaze_horizontal_range))
|
||||||
'--face-editor-model',
|
group_processors.add_argument('--face-editor-eye-gaze-vertical', help = translator.get('help.eye_gaze_vertical', __package__), type = float, default = config.get_float_value('processors', 'face_editor_eye_gaze_vertical', '0'), choices = face_editor_choices.face_editor_eye_gaze_vertical_range, metavar = create_float_metavar(face_editor_choices.face_editor_eye_gaze_vertical_range))
|
||||||
help = translator.get('help.model', __package__),
|
group_processors.add_argument('--face-editor-eye-open-ratio', help = translator.get('help.eye_open_ratio', __package__), type = float, default = config.get_float_value('processors', 'face_editor_eye_open_ratio', '0'), choices = face_editor_choices.face_editor_eye_open_ratio_range, metavar = create_float_metavar(face_editor_choices.face_editor_eye_open_ratio_range))
|
||||||
default = config.get_str_value('processors', 'face_editor_model', 'live_portrait'),
|
group_processors.add_argument('--face-editor-lip-open-ratio', help = translator.get('help.lip_open_ratio', __package__), type = float, default = config.get_float_value('processors', 'face_editor_lip_open_ratio', '0'), choices = face_editor_choices.face_editor_lip_open_ratio_range, metavar = create_float_metavar(face_editor_choices.face_editor_lip_open_ratio_range))
|
||||||
choices = face_editor_choices.face_editor_models
|
group_processors.add_argument('--face-editor-mouth-grim', help = translator.get('help.mouth_grim', __package__), type = float, default = config.get_float_value('processors', 'face_editor_mouth_grim', '0'), choices = face_editor_choices.face_editor_mouth_grim_range, metavar = create_float_metavar(face_editor_choices.face_editor_mouth_grim_range))
|
||||||
),
|
group_processors.add_argument('--face-editor-mouth-pout', help = translator.get('help.mouth_pout', __package__), type = float, default = config.get_float_value('processors', 'face_editor_mouth_pout', '0'), choices = face_editor_choices.face_editor_mouth_pout_range, metavar = create_float_metavar(face_editor_choices.face_editor_mouth_pout_range))
|
||||||
group_processors.add_argument(
|
group_processors.add_argument('--face-editor-mouth-purse', help = translator.get('help.mouth_purse', __package__), type = float, default = config.get_float_value('processors', 'face_editor_mouth_purse', '0'), choices = face_editor_choices.face_editor_mouth_purse_range, metavar = create_float_metavar(face_editor_choices.face_editor_mouth_purse_range))
|
||||||
'--face-editor-eyebrow-direction',
|
group_processors.add_argument('--face-editor-mouth-smile', help = translator.get('help.mouth_smile', __package__), type = float, default = config.get_float_value('processors', 'face_editor_mouth_smile', '0'), choices = face_editor_choices.face_editor_mouth_smile_range, metavar = create_float_metavar(face_editor_choices.face_editor_mouth_smile_range))
|
||||||
help = translator.get('help.eyebrow_direction', __package__),
|
group_processors.add_argument('--face-editor-mouth-position-horizontal', help = translator.get('help.mouth_position_horizontal', __package__), type = float, default = config.get_float_value('processors', 'face_editor_mouth_position_horizontal', '0'), choices = face_editor_choices.face_editor_mouth_position_horizontal_range, metavar = create_float_metavar(face_editor_choices.face_editor_mouth_position_horizontal_range))
|
||||||
type = float,
|
group_processors.add_argument('--face-editor-mouth-position-vertical', help = translator.get('help.mouth_position_vertical', __package__), type = float, default = config.get_float_value('processors', 'face_editor_mouth_position_vertical', '0'), choices = face_editor_choices.face_editor_mouth_position_vertical_range, metavar = create_float_metavar(face_editor_choices.face_editor_mouth_position_vertical_range))
|
||||||
default = config.get_float_value('processors', 'face_editor_eyebrow_direction', '0'),
|
group_processors.add_argument('--face-editor-head-pitch', help = translator.get('help.head_pitch', __package__), type = float, default = config.get_float_value('processors', 'face_editor_head_pitch', '0'), choices = face_editor_choices.face_editor_head_pitch_range, metavar = create_float_metavar(face_editor_choices.face_editor_head_pitch_range))
|
||||||
choices = face_editor_choices.face_editor_eyebrow_direction_range,
|
group_processors.add_argument('--face-editor-head-yaw', help = translator.get('help.head_yaw', __package__), type = float, default = config.get_float_value('processors', 'face_editor_head_yaw', '0'), choices = face_editor_choices.face_editor_head_yaw_range, metavar = create_float_metavar(face_editor_choices.face_editor_head_yaw_range))
|
||||||
metavar = create_float_metavar(face_editor_choices.face_editor_eyebrow_direction_range)
|
group_processors.add_argument('--face-editor-head-roll', help = translator.get('help.head_roll', __package__), type = float, default = config.get_float_value('processors', 'face_editor_head_roll', '0'), choices = face_editor_choices.face_editor_head_roll_range, metavar = create_float_metavar(face_editor_choices.face_editor_head_roll_range))
|
||||||
),
|
facefusion.args_store.register_args([ 'face_editor_model', 'face_editor_eyebrow_direction', 'face_editor_eye_gaze_horizontal', 'face_editor_eye_gaze_vertical', 'face_editor_eye_open_ratio', 'face_editor_lip_open_ratio', 'face_editor_mouth_grim', 'face_editor_mouth_pout', 'face_editor_mouth_purse', 'face_editor_mouth_smile', 'face_editor_mouth_position_horizontal', 'face_editor_mouth_position_vertical', 'face_editor_head_pitch', 'face_editor_head_yaw', 'face_editor_head_roll' ], scopes = [ 'api', 'cli' ])
|
||||||
group_processors.add_argument(
|
|
||||||
'--face-editor-eye-gaze-horizontal',
|
|
||||||
help = translator.get('help.eye_gaze_horizontal', __package__),
|
|
||||||
type = float,
|
|
||||||
default = config.get_float_value('processors', 'face_editor_eye_gaze_horizontal', '0'),
|
|
||||||
choices = face_editor_choices.face_editor_eye_gaze_horizontal_range,
|
|
||||||
metavar = create_float_metavar(face_editor_choices.face_editor_eye_gaze_horizontal_range)
|
|
||||||
),
|
|
||||||
group_processors.add_argument(
|
|
||||||
'--face-editor-eye-gaze-vertical',
|
|
||||||
help = translator.get('help.eye_gaze_vertical', __package__),
|
|
||||||
type = float,
|
|
||||||
default = config.get_float_value('processors', 'face_editor_eye_gaze_vertical', '0'),
|
|
||||||
choices = face_editor_choices.face_editor_eye_gaze_vertical_range,
|
|
||||||
metavar = create_float_metavar(face_editor_choices.face_editor_eye_gaze_vertical_range)
|
|
||||||
),
|
|
||||||
group_processors.add_argument(
|
|
||||||
'--face-editor-eye-open-ratio',
|
|
||||||
help = translator.get('help.eye_open_ratio', __package__),
|
|
||||||
type = float,
|
|
||||||
default = config.get_float_value('processors', 'face_editor_eye_open_ratio', '0'),
|
|
||||||
choices = face_editor_choices.face_editor_eye_open_ratio_range,
|
|
||||||
metavar = create_float_metavar(face_editor_choices.face_editor_eye_open_ratio_range)
|
|
||||||
),
|
|
||||||
group_processors.add_argument(
|
|
||||||
'--face-editor-lip-open-ratio',
|
|
||||||
help = translator.get('help.lip_open_ratio', __package__),
|
|
||||||
type = float,
|
|
||||||
default = config.get_float_value('processors', 'face_editor_lip_open_ratio', '0'),
|
|
||||||
choices = face_editor_choices.face_editor_lip_open_ratio_range,
|
|
||||||
metavar = create_float_metavar(face_editor_choices.face_editor_lip_open_ratio_range)
|
|
||||||
),
|
|
||||||
group_processors.add_argument(
|
|
||||||
'--face-editor-mouth-grim',
|
|
||||||
help = translator.get('help.mouth_grim', __package__),
|
|
||||||
type = float,
|
|
||||||
default = config.get_float_value('processors', 'face_editor_mouth_grim', '0'),
|
|
||||||
choices = face_editor_choices.face_editor_mouth_grim_range,
|
|
||||||
metavar = create_float_metavar(face_editor_choices.face_editor_mouth_grim_range)
|
|
||||||
),
|
|
||||||
group_processors.add_argument(
|
|
||||||
'--face-editor-mouth-pout',
|
|
||||||
help = translator.get('help.mouth_pout', __package__),
|
|
||||||
type = float,
|
|
||||||
default = config.get_float_value('processors', 'face_editor_mouth_pout', '0'),
|
|
||||||
choices = face_editor_choices.face_editor_mouth_pout_range,
|
|
||||||
metavar = create_float_metavar(face_editor_choices.face_editor_mouth_pout_range)
|
|
||||||
),
|
|
||||||
group_processors.add_argument(
|
|
||||||
'--face-editor-mouth-purse',
|
|
||||||
help = translator.get('help.mouth_purse', __package__),
|
|
||||||
type = float,
|
|
||||||
default = config.get_float_value('processors', 'face_editor_mouth_purse', '0'),
|
|
||||||
choices = face_editor_choices.face_editor_mouth_purse_range,
|
|
||||||
metavar = create_float_metavar(face_editor_choices.face_editor_mouth_purse_range)
|
|
||||||
),
|
|
||||||
group_processors.add_argument(
|
|
||||||
'--face-editor-mouth-smile',
|
|
||||||
help = translator.get('help.mouth_smile', __package__),
|
|
||||||
type = float,
|
|
||||||
default = config.get_float_value('processors', 'face_editor_mouth_smile', '0'),
|
|
||||||
choices = face_editor_choices.face_editor_mouth_smile_range,
|
|
||||||
metavar = create_float_metavar(face_editor_choices.face_editor_mouth_smile_range)
|
|
||||||
),
|
|
||||||
group_processors.add_argument(
|
|
||||||
'--face-editor-mouth-position-horizontal',
|
|
||||||
help = translator.get('help.mouth_position_horizontal', __package__),
|
|
||||||
type = float,
|
|
||||||
default = config.get_float_value('processors', 'face_editor_mouth_position_horizontal', '0'),
|
|
||||||
choices = face_editor_choices.face_editor_mouth_position_horizontal_range,
|
|
||||||
metavar = create_float_metavar(face_editor_choices.face_editor_mouth_position_horizontal_range)
|
|
||||||
),
|
|
||||||
group_processors.add_argument(
|
|
||||||
'--face-editor-mouth-position-vertical',
|
|
||||||
help = translator.get('help.mouth_position_vertical', __package__),
|
|
||||||
type = float,
|
|
||||||
default = config.get_float_value('processors', 'face_editor_mouth_position_vertical', '0'),
|
|
||||||
choices = face_editor_choices.face_editor_mouth_position_vertical_range,
|
|
||||||
metavar = create_float_metavar(face_editor_choices.face_editor_mouth_position_vertical_range)
|
|
||||||
),
|
|
||||||
group_processors.add_argument(
|
|
||||||
'--face-editor-head-pitch',
|
|
||||||
help = translator.get('help.head_pitch', __package__),
|
|
||||||
type = float,
|
|
||||||
default = config.get_float_value('processors', 'face_editor_head_pitch', '0'),
|
|
||||||
choices = face_editor_choices.face_editor_head_pitch_range,
|
|
||||||
metavar = create_float_metavar(face_editor_choices.face_editor_head_pitch_range)
|
|
||||||
),
|
|
||||||
group_processors.add_argument(
|
|
||||||
'--face-editor-head-yaw',
|
|
||||||
help = translator.get('help.head_yaw', __package__),
|
|
||||||
type = float,
|
|
||||||
default = config.get_float_value('processors', 'face_editor_head_yaw', '0'),
|
|
||||||
choices = face_editor_choices.face_editor_head_yaw_range,
|
|
||||||
metavar = create_float_metavar(face_editor_choices.face_editor_head_yaw_range)
|
|
||||||
),
|
|
||||||
group_processors.add_argument(
|
|
||||||
'--face-editor-head-roll',
|
|
||||||
help = translator.get('help.head_roll', __package__),
|
|
||||||
type = float,
|
|
||||||
default = config.get_float_value('processors', 'face_editor_head_roll', '0'),
|
|
||||||
choices = face_editor_choices.face_editor_head_roll_range,
|
|
||||||
metavar = create_float_metavar(face_editor_choices.face_editor_head_roll_range)
|
|
||||||
)
|
|
||||||
],
|
|
||||||
scopes = [ 'api', 'cli' ]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
|
def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
|
||||||
@@ -273,18 +165,10 @@ def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
|
|||||||
apply_state_item('face_editor_head_roll', args.get('face_editor_head_roll'))
|
apply_state_item('face_editor_head_roll', args.get('face_editor_head_roll'))
|
||||||
|
|
||||||
|
|
||||||
def get_common_modules() -> List[ModuleType]:
|
|
||||||
return [ content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer ]
|
|
||||||
|
|
||||||
|
|
||||||
def pre_check() -> bool:
|
def pre_check() -> bool:
|
||||||
model_hash_set = get_model_options().get('hashes')
|
model_hash_set = get_model_options().get('hashes')
|
||||||
model_source_set = get_model_options().get('sources')
|
model_source_set = get_model_options().get('sources')
|
||||||
|
|
||||||
for common_module in get_common_modules():
|
|
||||||
if not common_module.pre_check():
|
|
||||||
return False
|
|
||||||
|
|
||||||
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
|
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
|
||||||
|
|
||||||
|
|
||||||
@@ -292,7 +176,6 @@ 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')):
|
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__)
|
logger.error(translator.get('choose_image_or_video_target') + translator.get('exclamation_mark'), __name__)
|
||||||
return False
|
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')):
|
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__)
|
logger.error(translator.get('specify_image_or_video_output') + translator.get('exclamation_mark'), __name__)
|
||||||
return False
|
return False
|
||||||
@@ -302,15 +185,16 @@ def pre_process(mode : ProcessMode) -> bool:
|
|||||||
def post_process() -> None:
|
def post_process() -> None:
|
||||||
read_static_image.cache_clear()
|
read_static_image.cache_clear()
|
||||||
read_static_video_frame.cache_clear()
|
read_static_video_frame.cache_clear()
|
||||||
read_static_video_chunk.cache_clear()
|
|
||||||
video_manager.clear_video_pool()
|
video_manager.clear_video_pool()
|
||||||
|
|
||||||
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
|
if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
|
||||||
clear_inference_pool()
|
clear_inference_pool()
|
||||||
|
|
||||||
if state_manager.get_item('video_memory_strategy') == 'strict':
|
if state_manager.get_item('video_memory_strategy') == 'strict':
|
||||||
for common_module in get_common_modules():
|
content_analyser.clear_inference_pool()
|
||||||
common_module.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 edit_face(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
|
def edit_face(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
|
||||||
@@ -599,13 +483,10 @@ def normalize_crop_frame(crop_vision_frame : VisionFrame) -> VisionFrame:
|
|||||||
|
|
||||||
def process_frame(inputs : FaceEditorInputs) -> ProcessorOutputs:
|
def process_frame(inputs : FaceEditorInputs) -> ProcessorOutputs:
|
||||||
reference_vision_frame = inputs.get('reference_vision_frame')
|
reference_vision_frame = inputs.get('reference_vision_frame')
|
||||||
source_vision_frames = inputs.get('source_vision_frames')
|
target_vision_frame = inputs.get('target_vision_frame')
|
||||||
target_vision_frames = inputs.get('target_vision_frames')
|
|
||||||
temp_vision_frame = inputs.get('temp_vision_frame')
|
temp_vision_frame = inputs.get('temp_vision_frame')
|
||||||
temp_vision_mask = inputs.get('temp_vision_mask')
|
temp_vision_mask = inputs.get('temp_vision_mask')
|
||||||
|
target_faces = select_faces(reference_vision_frame, target_vision_frame)
|
||||||
target_vision_frame = get_middle(target_vision_frames)
|
|
||||||
target_faces = select_faces(reference_vision_frame, source_vision_frames, target_vision_frames)
|
|
||||||
|
|
||||||
if target_faces:
|
if target_faces:
|
||||||
for target_face in target_faces:
|
for target_face in target_faces:
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
from facefusion.types import Locales
|
from facefusion.types import Locals
|
||||||
|
|
||||||
LOCALES : Locales =\
|
LOCALS : Locals =\
|
||||||
{
|
{
|
||||||
'en':
|
'en':
|
||||||
{
|
{
|
||||||
@@ -1,12 +1,11 @@
|
|||||||
from typing import List, Literal, TypedDict
|
from typing import Literal, TypedDict
|
||||||
|
|
||||||
from facefusion.types import Mask, VisionFrame
|
from facefusion.types import Mask, VisionFrame
|
||||||
|
|
||||||
FaceEditorInputs = TypedDict('FaceEditorInputs',
|
FaceEditorInputs = TypedDict('FaceEditorInputs',
|
||||||
{
|
{
|
||||||
'reference_vision_frame' : VisionFrame,
|
'reference_vision_frame' : VisionFrame,
|
||||||
'source_vision_frames' : List[VisionFrame],
|
'target_vision_frame' : VisionFrame,
|
||||||
'target_vision_frames' : List[VisionFrame],
|
|
||||||
'temp_vision_frame' : VisionFrame,
|
'temp_vision_frame' : VisionFrame,
|
||||||
'temp_vision_mask' : Mask
|
'temp_vision_mask' : Mask
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
from typing import List, Sequence, get_args
|
from typing import List, Sequence
|
||||||
|
|
||||||
from facefusion.common_helper import create_float_range, create_int_range
|
from facefusion.common_helper import create_float_range, create_int_range
|
||||||
from facefusion.processors.modules.face_enhancer.types import FaceEnhancerModel
|
from facefusion.processors.modules.face_enhancer.types import FaceEnhancerModel
|
||||||
|
|
||||||
face_enhancer_models : List[FaceEnhancerModel] = list(get_args(FaceEnhancerModel))
|
face_enhancer_models : List[FaceEnhancerModel] = [ 'codeformer', 'gfpgan_1.2', 'gfpgan_1.3', 'gfpgan_1.4', 'gpen_bfr_256', 'gpen_bfr_512', 'gpen_bfr_1024', 'gpen_bfr_2048', 'restoreformer_plus_plus' ]
|
||||||
|
|
||||||
face_enhancer_blend_range : Sequence[int] = create_int_range(0, 100, 1)
|
face_enhancer_blend_range : Sequence[int] = create_int_range(0, 100, 1)
|
||||||
|
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user