diff --git a/facefusion/apis/asset_helper.py b/facefusion/apis/asset_helper.py index 71e22f52..1fa42892 100644 --- a/facefusion/apis/asset_helper.py +++ b/facefusion/apis/asset_helper.py @@ -1,12 +1,13 @@ import asyncio import os +import time import uuid from typing import List, Optional from starlette.datastructures import UploadFile import facefusion.choices -from facefusion import ffmpeg, state_manager +from facefusion import ffmpeg, process_manager, state_manager, translator from facefusion.face_creator import get_many_faces from facefusion.face_helper import warp_face_by_bounding_box from facefusion.face_selector import sort_faces_by_order @@ -46,6 +47,16 @@ def detect_media_type_by_format(file_format : str) -> Optional[MediaType]: 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') + time_current = time.monotonic() + + process_manager.stage('upload', translator.get('uploading'), + { + 'unit': 'percent', + 'current': 0, + 'started_at': time_current, + 'updated_at': time_current + }) + process_manager.count(upload_files) for upload_file in upload_files: file_format = get_file_format(upload_file.filename) @@ -67,6 +78,8 @@ async def save_asset_files(upload_files : List[UploadFile]) -> List[str]: if media_type == 'video' and await asyncio.to_thread(ffmpeg.sanitize_video, upload_file.file, asset_path, api_security_strategy): asset_paths.append(asset_path) + process_manager.update() + return asset_paths diff --git a/facefusion/locales.py b/facefusion/locales.py index 9fa11931..0d376629 100644 --- a/facefusion/locales.py +++ b/facefusion/locales.py @@ -19,6 +19,7 @@ LOCALES : Locales =\ 'processing': 'processing', 'merging': 'merging', 'downloading': 'downloading', + 'uploading': 'uploading', 'temp_frames_not_found': 'temporary frames not found', 'copying_image': 'copying image with a resolution of {resolution}', 'copying_image_succeeded': 'copying image succeeded', diff --git a/facefusion/types.py b/facefusion/types.py index c9eba83f..77c73569 100755 --- a/facefusion/types.py +++ b/facefusion/types.py @@ -219,7 +219,7 @@ Progress = TypedDict('Progress', 'updated_at' : float }) -StageType = Literal['check', 'download', 'analyse', 'copy', 'extract', 'spawn', 'infer', 'merge', 'finalize', 'serve', 'stream'] +StageType = Literal['check', 'download', 'upload', 'analyse', 'copy', 'extract', 'spawn', 'infer', 'merge', 'finalize', 'serve', 'stream'] Stage = TypedDict('Stage', { 'type' : StageType, diff --git a/tests/apis/test_asset_helper.py b/tests/apis/test_asset_helper.py index a55c7d74..88642f7a 100644 --- a/tests/apis/test_asset_helper.py +++ b/tests/apis/test_asset_helper.py @@ -1,7 +1,13 @@ -import pytest +import asyncio +import tempfile -from facefusion.apis.asset_helper import detect_media_type_by_path, extract_image_metadata +import pytest +from starlette.datastructures import UploadFile + +from facefusion import process_manager, state_manager +from facefusion.apis.asset_helper import detect_media_type_by_path, extract_image_metadata, save_asset_files from facefusion.download import conditional_download +from facefusion.filesystem import is_image from tests.assert_helper import get_test_example_file, get_test_examples_directory @@ -14,6 +20,12 @@ def before_all() -> None: 'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/target-240p.mp4' ]) + state_manager.init() + state_manager.init_item('temp_path', tempfile.gettempdir()) + state_manager.init_item('api_security_strategy', 'strict') + + process_manager.init() + def test_detect_media_type() -> None: assert detect_media_type_by_path(get_test_example_file('source.jpg')) == 'image' @@ -25,3 +37,13 @@ def test_extract_image_metadata() -> None: image_metadata = extract_image_metadata(get_test_example_file('source.jpg')) assert image_metadata.get('resolution') == (1024, 1024) + + +def test_save_asset_files() -> None: + with open(get_test_example_file('source.jpg'), 'rb') as file: + asset_paths = asyncio.run(save_asset_files([ UploadFile(file, filename = 'source.jpg') ])) + + assert process_manager.get_stages()[-1].get('type') == 'upload' + assert process_manager.get_stages()[-1].get('progress').get('current') == 1 + + assert is_image(asset_paths[0]) is True