* Cosmetic changes * Cosmetic changes * Run single warm up for the benchmark suite * Use latest version of Gradio * More testing * Introduce basic installer * Fix typo * Move more to installer file * Fix the installer with the uninstall all trick * Adjust wording * Fix coreml in installer * Allow Pyhton 3.9 * Add VENV to installer * Just some cosmetics * Just some cosmetics * Dedicated headless mode, Refine API of UI layouts * Use --headless for pytest * Fix testing for Windows * Normalize output path that lacks extension * Fix CI for Windows * Fix CI for Windows * UI to change output path * Add conda support for the installer * Improve installer quite a bit * Drop conda support * Install community wheels for coreml silicon * Improve output video component * Fix silicon wheel downloading * Remove venv from installer as we cannot activate via subprocess * Use join to create wheel name * Refine the output path normalization * Refine the output path normalization * Introduce ProcessMode and rename some methods * Introduce ProcessMode and rename some methods * Basic webcam integration and open_ffmpeg() * Basic webcam integration part2 * Benchmark resolutions now selectable * Rename benchmark resolution back to benchmark runs * Fix repeating output path in UI * Keep output_path untouched if not resolvable * Add more cases to normalize output path * None for those tests that don't take source path into account * Finish basic webcam integration, UI layout now with custom run() * Fix CI and hide link in webcam UI * Cosmetics on webcam UI * Move get_device to utilities * Fix CI * Introduce output-image-quality, Show and hide UI according to target media type * Benchmark with partial result updates * fix: trim frame sliders not appearing after draggin video * fix: output and temp frame setting inputs not appearing * Fix: set increased update delay to 250ms to let Gradio update conditional inputs properly * Reverted .gitignore * Adjust timings * Remove timeout hacks and get fully event driven * Update dependencies * Update dependencies * Revert NSFW library, Conditional unset trim args * Face selector works better on preview slider release * Add limit resources to UI * Introduce vision.py for all CV2 operations, Rename some methods * Add restoring audio failed * Decouple updates for preview image and preview frame slider, Move reduce_preview_frame to vision * Refactor detect_fps based on JSON output * Only webcam when open * More conditions to vision.py * Add udp and v4l2 streaming to webcam UI * Detect v4l2 device to be used * Refactor code a bit * Use static max memory for UI * Fix CI * Looks stable to me * Update preview * Update preview --------- Co-authored-by: Sumit <vizsumit@gmail.com>
106 lines
3.7 KiB
Python
106 lines
3.7 KiB
Python
import os
|
|
import sys
|
|
import importlib
|
|
import psutil
|
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
from queue import Queue
|
|
from types import ModuleType
|
|
from typing import Any, List, Callable
|
|
from tqdm import tqdm
|
|
|
|
import facefusion.globals
|
|
from facefusion import wording
|
|
|
|
FRAME_PROCESSORS_MODULES : List[ModuleType] = []
|
|
FRAME_PROCESSORS_METHODS =\
|
|
[
|
|
'get_frame_processor',
|
|
'clear_frame_processor',
|
|
'pre_check',
|
|
'pre_process',
|
|
'process_frame',
|
|
'process_frames',
|
|
'process_image',
|
|
'process_video',
|
|
'post_process'
|
|
]
|
|
|
|
|
|
def load_frame_processor_module(frame_processor : str) -> Any:
|
|
try:
|
|
frame_processor_module = importlib.import_module('facefusion.processors.frame.modules.' + frame_processor)
|
|
for method_name in FRAME_PROCESSORS_METHODS:
|
|
if not hasattr(frame_processor_module, method_name):
|
|
raise NotImplementedError
|
|
except ModuleNotFoundError:
|
|
sys.exit(wording.get('frame_processor_not_loaded').format(frame_processor = frame_processor))
|
|
except NotImplementedError:
|
|
sys.exit(wording.get('frame_processor_not_implemented').format(frame_processor = frame_processor))
|
|
return frame_processor_module
|
|
|
|
|
|
def get_frame_processors_modules(frame_processors : List[str]) -> List[ModuleType]:
|
|
global FRAME_PROCESSORS_MODULES
|
|
|
|
if not FRAME_PROCESSORS_MODULES:
|
|
for frame_processor in frame_processors:
|
|
frame_processor_module = load_frame_processor_module(frame_processor)
|
|
FRAME_PROCESSORS_MODULES.append(frame_processor_module)
|
|
return FRAME_PROCESSORS_MODULES
|
|
|
|
|
|
def clear_frame_processors_modules() -> None:
|
|
global FRAME_PROCESSORS_MODULES
|
|
|
|
for frame_processor_module in get_frame_processors_modules(facefusion.globals.frame_processors):
|
|
frame_processor_module.clear_frame_processor()
|
|
FRAME_PROCESSORS_MODULES = []
|
|
|
|
|
|
def multi_process_frame(source_path : str, temp_frame_paths : List[str], process_frames: Callable[[str, List[str], Any], None], update: Callable[[], None]) -> None:
|
|
with ThreadPoolExecutor(max_workers = facefusion.globals.execution_thread_count) as executor:
|
|
futures = []
|
|
queue = create_queue(temp_frame_paths)
|
|
queue_per_future = max(len(temp_frame_paths) // facefusion.globals.execution_thread_count * facefusion.globals.execution_queue_count, 1)
|
|
while not queue.empty():
|
|
future = executor.submit(process_frames, source_path, pick_queue(queue, queue_per_future), update)
|
|
futures.append(future)
|
|
for future in as_completed(futures):
|
|
future.result()
|
|
|
|
|
|
def create_queue(temp_frame_paths : List[str]) -> Queue[str]:
|
|
queue : Queue[str] = Queue()
|
|
for frame_path in temp_frame_paths:
|
|
queue.put(frame_path)
|
|
return queue
|
|
|
|
|
|
def pick_queue(queue : Queue[str], queue_per_future : int) -> List[str]:
|
|
queues = []
|
|
for _ in range(queue_per_future):
|
|
if not queue.empty():
|
|
queues.append(queue.get())
|
|
return queues
|
|
|
|
|
|
def process_video(source_path : str, frame_paths : List[str], process_frames : Callable[[str, List[str], Any], None]) -> None:
|
|
progress_bar_format = '{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}{postfix}]'
|
|
total = len(frame_paths)
|
|
with tqdm(total = total, desc = wording.get('processing'), unit = 'frame', dynamic_ncols = True, bar_format = progress_bar_format) as progress:
|
|
multi_process_frame(source_path, frame_paths, process_frames, lambda: update_progress(progress))
|
|
|
|
|
|
def update_progress(progress : Any = None) -> None:
|
|
process = psutil.Process(os.getpid())
|
|
memory_usage = process.memory_info().rss / 1024 / 1024 / 1024
|
|
progress.set_postfix(
|
|
{
|
|
'memory_usage': '{:.2f}'.format(memory_usage).zfill(5) + 'GB',
|
|
'execution_providers': facefusion.globals.execution_providers,
|
|
'execution_thread_count': facefusion.globals.execution_thread_count,
|
|
'execution_queue_count': facefusion.globals.execution_queue_count
|
|
})
|
|
progress.refresh()
|
|
progress.update(1)
|