2023-12-20 00:00:32 +01:00
|
|
|
from typing import List, Optional
|
|
|
|
|
import glob
|
|
|
|
|
import os
|
|
|
|
|
import shutil
|
|
|
|
|
import tempfile
|
|
|
|
|
import filetype
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
import facefusion.globals
|
|
|
|
|
|
|
|
|
|
TEMP_DIRECTORY_PATH = os.path.join(tempfile.gettempdir(), 'facefusion')
|
|
|
|
|
TEMP_OUTPUT_VIDEO_NAME = 'temp.mp4'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_temp_frame_paths(target_path : str) -> List[str]:
|
|
|
|
|
temp_frames_pattern = get_temp_frames_pattern(target_path, '*')
|
|
|
|
|
return sorted(glob.glob(temp_frames_pattern))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_temp_frames_pattern(target_path : str, temp_frame_prefix : str) -> str:
|
|
|
|
|
temp_directory_path = get_temp_directory_path(target_path)
|
|
|
|
|
return os.path.join(temp_directory_path, temp_frame_prefix + '.' + facefusion.globals.temp_frame_format)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_temp_directory_path(target_path : str) -> str:
|
|
|
|
|
target_name, _ = os.path.splitext(os.path.basename(target_path))
|
|
|
|
|
return os.path.join(TEMP_DIRECTORY_PATH, target_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_temp_output_video_path(target_path : str) -> str:
|
|
|
|
|
temp_directory_path = get_temp_directory_path(target_path)
|
|
|
|
|
return os.path.join(temp_directory_path, TEMP_OUTPUT_VIDEO_NAME)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_temp(target_path : str) -> None:
|
|
|
|
|
temp_directory_path = get_temp_directory_path(target_path)
|
|
|
|
|
Path(temp_directory_path).mkdir(parents = True, exist_ok = True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def move_temp(target_path : str, output_path : str) -> None:
|
|
|
|
|
temp_output_video_path = get_temp_output_video_path(target_path)
|
|
|
|
|
if is_file(temp_output_video_path):
|
|
|
|
|
if is_file(output_path):
|
|
|
|
|
os.remove(output_path)
|
|
|
|
|
shutil.move(temp_output_video_path, output_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def clear_temp(target_path : str) -> None:
|
|
|
|
|
temp_directory_path = get_temp_directory_path(target_path)
|
|
|
|
|
parent_directory_path = os.path.dirname(temp_directory_path)
|
|
|
|
|
if not facefusion.globals.keep_temp and is_directory(temp_directory_path):
|
Next (#436)
* Rename landmark 5 variables
* Mark as NEXT
* Render tabs for multiple ui layout usage
* Allow many face detectors at once, Add face detector tweaks
* Remove face detector tweaks for now (kinda placebo)
* Fix lint issues
* Allow rendering the landmark-5 and landmark-5/68 via debugger
* Fix naming
* Convert face landmark based on confidence score
* Convert face landmark based on confidence score
* Add scrfd face detector model (#397)
* Add scrfd face detector model
* Switch to scrfd_2.5g.onnx model
* Just some renaming
* Downgrade OpenCV, Add SYSTEM_VERSION_COMPAT=0 for MacOS
* Improve naming
* prepare detect frame outside of semaphore
* Feat/process manager (#399)
* Minor naming
* Introduce process manager to start and stop
* Introduce process manager to start and stop
* Introduce process manager to start and stop
* Introduce process manager to start and stop
* Introduce process manager to start and stop
* Remove useless test for now
* Avoid useless variables
* Show stop once is_processing is True
* Allow to stop ffmpeg processing too
* Implement output image resolution (#403)
* Implement output image resolution
* Reorder code
* Simplify output logic and therefore fix bug
* Frame-enhancer-onnx (#404)
* changes
* changes
* changes
* changes
* add models
* update workflow
* Some cleanup
* Some cleanup
* Feat/frame enhancer polishing (#410)
* Some cleanup
* Polish the frame enhancer
* Frame Enhancer: Add more models, optimize processing
* Minor changes
* Improve readability of create_tile_frames and merge_tile_frames
* We don't have enough models yet
* Feat/face landmarker score (#413)
* Introduce face landmarker score
* Fix testing
* Fix testing
* Use release for score related sliders
* Reduce face landmark fallbacks
* Scores and landmarks in Face dict, Change color-theme in face debugger
* Scores and landmarks in Face dict, Change color-theme in face debugger
* Fix some naming
* Add 8K support (for whatever reasons)
* Fix testing
* Using get() for face.landmarks
* Introduce statistics
* More statistics
* Limit the histogram equalization
* Enable queue() for default layout
* Improve copy_image()
* Fix error when switching detector model
* Always set UI values with globals if possible
* Use different logic for output image and output video resolutions
* Enforce re-download if file size is off
* Remove unused method
* Remove unused method
* Remove unused warning filter
* Improved output path normalization (#419)
* Handle some exceptions
* Handle some exceptions
* Cleanup
* Prevent countless thread locks
* Listen to user feedback
* Fix webp edge case
* Feat/cuda device detection (#424)
* Introduce cuda device detection
* Introduce cuda device detection
* it's gtx
* Move logic to run_nvidia_smi()
* Finalize execution device naming
* Finalize execution device naming
* Merge execution_helper.py to execution.py
* Undo lowercase of values
* Undo lowercase of values
* Finalize naming
* Add missing entry to ini
* fix lip_syncer preview (#426)
* fix lip_syncer preview
* change
* Refresh preview on trim changes
* Cleanup frame enhancers and remove useless scale in merge_video() (#428)
* Keep lips over the whole video once lip syncer is enabled (#430)
* Keep lips over the whole video once lip syncer is enabled
* changes
* changes
* Fix spacing
* Use empty audio frame on silence
* Use empty audio frame on silence
* Fix ConfigParser encoding (#431)
facefusion.ini is UTF8 encoded but config.py doesn't specify encoding which results in corrupted entries when non english characters are used.
Affected entries:
source_paths
target_path
output_path
* Adjust spacing
* Improve the GTX 16 series detection
* Use general exception to catch ParseError
* Use general exception to catch ParseError
* Host frame enhancer models4
* Use latest onnxruntime
* Minor changes in benchmark UI
* Different approach to cancel ffmpeg process
* Add support for amd amf encoders (#433)
* Add amd_amf encoders
* remove -rc cqp from amf encoder parameters
* Improve terminal output, move success messages to debug mode
* Improve terminal output, move success messages to debug mode
* Minor update
* Minor update
* onnxruntime 1.17.1 matches cuda 12.2
* Feat/improved scaling (#435)
* Prevent useless temp upscaling, Show resolution and fps in terminal output
* Remove temp frame quality
* Remove temp frame quality
* Tiny cleanup
* Default back to png for temp frames, Remove pix_fmt from frame extraction due mjpeg error
* Fix inswapper fallback by onnxruntime
* Fix inswapper fallback by major onnxruntime
* Fix inswapper fallback by major onnxruntime
* Add testing for vision restrict methods
* Fix left / right face mask regions, add left-ear and right-ear
* Flip right and left again
* Undo ears - does not work with box mask
* Prepare next release
* Fix spacing
* 100% quality when using jpg for temp frames
* Use span_kendata_x4 as default as of speed
* benchmark optimal tile and pad
* Undo commented out code
* Add real_esrgan_x4_fp16 model
* Be strict when using many face detectors
---------
Co-authored-by: Harisreedhar <46858047+harisreedhar@users.noreply.github.com>
Co-authored-by: aldemoth <159712934+aldemoth@users.noreply.github.com>
2024-03-14 19:56:54 +01:00
|
|
|
shutil.rmtree(temp_directory_path, ignore_errors = True)
|
2023-12-20 00:00:32 +01:00
|
|
|
if os.path.exists(parent_directory_path) and not os.listdir(parent_directory_path):
|
|
|
|
|
os.rmdir(parent_directory_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_file(file_path : str) -> bool:
|
|
|
|
|
return bool(file_path and os.path.isfile(file_path))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_directory(directory_path : str) -> bool:
|
|
|
|
|
return bool(directory_path and os.path.isdir(directory_path))
|
|
|
|
|
|
|
|
|
|
|
2024-02-14 14:08:29 +01:00
|
|
|
def is_audio(audio_path : str) -> bool:
|
|
|
|
|
return is_file(audio_path) and filetype.helpers.is_audio(audio_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def has_audio(audio_paths : List[str]) -> bool:
|
|
|
|
|
if audio_paths:
|
|
|
|
|
return any(is_audio(audio_path) for audio_path in audio_paths)
|
2023-12-20 00:00:32 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
2024-02-14 14:08:29 +01:00
|
|
|
def is_image(image_path : str) -> bool:
|
|
|
|
|
return is_file(image_path) and filetype.helpers.is_image(image_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def has_image(image_paths: List[str]) -> bool:
|
2023-12-20 00:00:32 +01:00
|
|
|
if image_paths:
|
2024-02-14 14:08:29 +01:00
|
|
|
return any(is_image(image_path) for image_path in image_paths)
|
2023-12-20 00:00:32 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_video(video_path : str) -> bool:
|
2024-02-14 14:08:29 +01:00
|
|
|
return is_file(video_path) and filetype.helpers.is_video(video_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def filter_audio_paths(paths : List[str]) -> List[str]:
|
|
|
|
|
if paths:
|
|
|
|
|
return [ path for path in paths if is_audio(path) ]
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def filter_image_paths(paths : List[str]) -> List[str]:
|
|
|
|
|
if paths:
|
|
|
|
|
return [ path for path in paths if is_image(path) ]
|
|
|
|
|
return []
|
2023-12-20 00:00:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def resolve_relative_path(path : str) -> str:
|
|
|
|
|
return os.path.abspath(os.path.join(os.path.dirname(__file__), path))
|
|
|
|
|
|
|
|
|
|
|
2024-01-19 19:57:48 +01:00
|
|
|
def list_directory(directory_path : str) -> Optional[List[str]]:
|
|
|
|
|
if is_directory(directory_path):
|
|
|
|
|
files = os.listdir(directory_path)
|
2024-02-14 14:08:29 +01:00
|
|
|
return sorted([ Path(file).stem for file in files if not Path(file).stem.startswith(('.', '__')) ])
|
2023-12-20 00:00:32 +01:00
|
|
|
return None
|