2023-12-20 00:00:32 +01:00
|
|
|
from typing import Any, Dict, List
|
|
|
|
|
from cv2.typing import Size
|
|
|
|
|
from functools import lru_cache
|
2024-03-20 10:02:08 +01:00
|
|
|
from time import sleep
|
2023-12-20 00:00:32 +01:00
|
|
|
import cv2
|
|
|
|
|
import numpy
|
|
|
|
|
import onnxruntime
|
|
|
|
|
|
|
|
|
|
import facefusion.globals
|
2024-03-20 10:02:08 +01:00
|
|
|
from facefusion import process_manager
|
2024-04-19 13:35:36 +02:00
|
|
|
from facefusion.thread_helper import thread_lock, conditional_thread_semaphore
|
2024-02-14 14:08:29 +01:00
|
|
|
from facefusion.typing import FaceLandmark68, VisionFrame, Mask, Padding, FaceMaskRegion, ModelSet
|
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
|
|
|
from facefusion.execution import apply_execution_provider_options
|
2024-04-09 15:40:55 +02:00
|
|
|
from facefusion.filesystem import resolve_relative_path, is_file
|
2023-12-20 00:00:32 +01:00
|
|
|
from facefusion.download import conditional_download
|
|
|
|
|
|
|
|
|
|
FACE_OCCLUDER = None
|
|
|
|
|
FACE_PARSER = None
|
|
|
|
|
MODELS : ModelSet =\
|
|
|
|
|
{
|
|
|
|
|
'face_occluder':
|
|
|
|
|
{
|
|
|
|
|
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models/face_occluder.onnx',
|
|
|
|
|
'path': resolve_relative_path('../.assets/models/face_occluder.onnx')
|
|
|
|
|
},
|
|
|
|
|
'face_parser':
|
|
|
|
|
{
|
|
|
|
|
'url': 'https://github.com/facefusion/facefusion-assets/releases/download/models/face_parser.onnx',
|
|
|
|
|
'path': resolve_relative_path('../.assets/models/face_parser.onnx')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
FACE_MASK_REGIONS : Dict[FaceMaskRegion, int] =\
|
|
|
|
|
{
|
|
|
|
|
'skin': 1,
|
|
|
|
|
'left-eyebrow': 2,
|
|
|
|
|
'right-eyebrow': 3,
|
|
|
|
|
'left-eye': 4,
|
|
|
|
|
'right-eye': 5,
|
2024-04-13 11:27:55 +02:00
|
|
|
'glasses': 6,
|
2023-12-20 00:00:32 +01:00
|
|
|
'nose': 10,
|
|
|
|
|
'mouth': 11,
|
|
|
|
|
'upper-lip': 12,
|
|
|
|
|
'lower-lip': 13
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_face_occluder() -> Any:
|
|
|
|
|
global FACE_OCCLUDER
|
|
|
|
|
|
2024-04-19 13:35:36 +02:00
|
|
|
with thread_lock():
|
2024-04-09 15:40:55 +02:00
|
|
|
while process_manager.is_checking():
|
|
|
|
|
sleep(0.5)
|
2023-12-20 00:00:32 +01:00
|
|
|
if FACE_OCCLUDER is None:
|
|
|
|
|
model_path = MODELS.get('face_occluder').get('path')
|
2024-01-19 19:57:48 +01:00
|
|
|
FACE_OCCLUDER = onnxruntime.InferenceSession(model_path, providers = apply_execution_provider_options(facefusion.globals.execution_providers))
|
2023-12-20 00:00:32 +01:00
|
|
|
return FACE_OCCLUDER
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_face_parser() -> Any:
|
|
|
|
|
global FACE_PARSER
|
|
|
|
|
|
2024-04-19 13:35:36 +02:00
|
|
|
with thread_lock():
|
2024-03-20 10:02:08 +01:00
|
|
|
while process_manager.is_checking():
|
|
|
|
|
sleep(0.5)
|
2023-12-20 00:00:32 +01:00
|
|
|
if FACE_PARSER is None:
|
|
|
|
|
model_path = MODELS.get('face_parser').get('path')
|
2024-01-19 19:57:48 +01:00
|
|
|
FACE_PARSER = onnxruntime.InferenceSession(model_path, providers = apply_execution_provider_options(facefusion.globals.execution_providers))
|
2023-12-20 00:00:32 +01:00
|
|
|
return FACE_PARSER
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def clear_face_occluder() -> None:
|
|
|
|
|
global FACE_OCCLUDER
|
|
|
|
|
|
|
|
|
|
FACE_OCCLUDER = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def clear_face_parser() -> None:
|
|
|
|
|
global FACE_PARSER
|
|
|
|
|
|
|
|
|
|
FACE_PARSER = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def pre_check() -> bool:
|
2024-04-09 15:40:55 +02:00
|
|
|
download_directory_path = resolve_relative_path('../.assets/models')
|
|
|
|
|
model_urls =\
|
|
|
|
|
[
|
|
|
|
|
MODELS.get('face_occluder').get('url'),
|
|
|
|
|
MODELS.get('face_parser').get('url')
|
|
|
|
|
]
|
|
|
|
|
model_paths =\
|
|
|
|
|
[
|
|
|
|
|
MODELS.get('face_occluder').get('path'),
|
|
|
|
|
MODELS.get('face_parser').get('path')
|
|
|
|
|
]
|
|
|
|
|
|
2023-12-20 00:00:32 +01:00
|
|
|
if not facefusion.globals.skip_download:
|
2024-03-20 10:02:08 +01:00
|
|
|
process_manager.check()
|
2023-12-20 00:00:32 +01:00
|
|
|
conditional_download(download_directory_path, model_urls)
|
2024-03-20 10:02:08 +01:00
|
|
|
process_manager.end()
|
2024-04-09 15:40:55 +02:00
|
|
|
return all(is_file(model_path) for model_path in model_paths)
|
2023-12-20 00:00:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@lru_cache(maxsize = None)
|
|
|
|
|
def create_static_box_mask(crop_size : Size, face_mask_blur : float, face_mask_padding : Padding) -> Mask:
|
|
|
|
|
blur_amount = int(crop_size[0] * 0.5 * face_mask_blur)
|
|
|
|
|
blur_area = max(blur_amount // 2, 1)
|
2024-02-14 14:08:29 +01:00
|
|
|
box_mask : Mask = numpy.ones(crop_size, numpy.float32)
|
2023-12-20 00:00:32 +01:00
|
|
|
box_mask[:max(blur_area, int(crop_size[1] * face_mask_padding[0] / 100)), :] = 0
|
|
|
|
|
box_mask[-max(blur_area, int(crop_size[1] * face_mask_padding[2] / 100)):, :] = 0
|
|
|
|
|
box_mask[:, :max(blur_area, int(crop_size[0] * face_mask_padding[3] / 100))] = 0
|
|
|
|
|
box_mask[:, -max(blur_area, int(crop_size[0] * face_mask_padding[1] / 100)):] = 0
|
|
|
|
|
if blur_amount > 0:
|
|
|
|
|
box_mask = cv2.GaussianBlur(box_mask, (0, 0), blur_amount * 0.25)
|
|
|
|
|
return box_mask
|
|
|
|
|
|
|
|
|
|
|
2024-02-14 14:08:29 +01:00
|
|
|
def create_occlusion_mask(crop_vision_frame : VisionFrame) -> Mask:
|
2023-12-20 00:00:32 +01:00
|
|
|
face_occluder = get_face_occluder()
|
2024-02-14 14:08:29 +01:00
|
|
|
prepare_vision_frame = cv2.resize(crop_vision_frame, face_occluder.get_inputs()[0].shape[1:3][::-1])
|
|
|
|
|
prepare_vision_frame = numpy.expand_dims(prepare_vision_frame, axis = 0).astype(numpy.float32) / 255
|
|
|
|
|
prepare_vision_frame = prepare_vision_frame.transpose(0, 1, 2, 3)
|
2024-04-19 13:35:36 +02:00
|
|
|
with conditional_thread_semaphore(facefusion.globals.execution_providers):
|
|
|
|
|
occlusion_mask : Mask = face_occluder.run(None,
|
|
|
|
|
{
|
|
|
|
|
face_occluder.get_inputs()[0].name: prepare_vision_frame
|
|
|
|
|
})[0][0]
|
2023-12-20 00:00:32 +01:00
|
|
|
occlusion_mask = occlusion_mask.transpose(0, 1, 2).clip(0, 1).astype(numpy.float32)
|
2024-02-14 14:08:29 +01:00
|
|
|
occlusion_mask = cv2.resize(occlusion_mask, crop_vision_frame.shape[:2][::-1])
|
|
|
|
|
occlusion_mask = (cv2.GaussianBlur(occlusion_mask.clip(0, 1), (0, 0), 5).clip(0.5, 1) - 0.5) * 2
|
2023-12-20 00:00:32 +01:00
|
|
|
return occlusion_mask
|
|
|
|
|
|
|
|
|
|
|
2024-02-14 14:08:29 +01:00
|
|
|
def create_region_mask(crop_vision_frame : VisionFrame, face_mask_regions : List[FaceMaskRegion]) -> Mask:
|
2023-12-20 00:00:32 +01:00
|
|
|
face_parser = get_face_parser()
|
2024-02-14 14:08:29 +01:00
|
|
|
prepare_vision_frame = cv2.flip(cv2.resize(crop_vision_frame, (512, 512)), 1)
|
|
|
|
|
prepare_vision_frame = numpy.expand_dims(prepare_vision_frame, axis = 0).astype(numpy.float32)[:, :, ::-1] / 127.5 - 1
|
|
|
|
|
prepare_vision_frame = prepare_vision_frame.transpose(0, 3, 1, 2)
|
2024-04-19 13:35:36 +02:00
|
|
|
with conditional_thread_semaphore(facefusion.globals.execution_providers):
|
|
|
|
|
region_mask : Mask = face_parser.run(None,
|
|
|
|
|
{
|
|
|
|
|
face_parser.get_inputs()[0].name: prepare_vision_frame
|
|
|
|
|
})[0][0]
|
2023-12-20 00:00:32 +01:00
|
|
|
region_mask = numpy.isin(region_mask.argmax(0), [ FACE_MASK_REGIONS[region] for region in face_mask_regions ])
|
2024-02-14 14:08:29 +01:00
|
|
|
region_mask = cv2.resize(region_mask.astype(numpy.float32), crop_vision_frame.shape[:2][::-1])
|
|
|
|
|
region_mask = (cv2.GaussianBlur(region_mask.clip(0, 1), (0, 0), 5).clip(0.5, 1) - 0.5) * 2
|
2023-12-20 00:00:32 +01:00
|
|
|
return region_mask
|
2024-02-14 14:08:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_mouth_mask(face_landmark_68 : FaceLandmark68) -> Mask:
|
|
|
|
|
convex_hull = cv2.convexHull(face_landmark_68[numpy.r_[3:14, 31:36]].astype(numpy.int32))
|
2024-04-09 15:40:55 +02:00
|
|
|
mouth_mask : Mask = numpy.zeros((512, 512)).astype(numpy.float32)
|
2024-02-14 14:08:29 +01:00
|
|
|
mouth_mask = cv2.fillConvexPoly(mouth_mask, convex_hull, 1.0)
|
|
|
|
|
mouth_mask = cv2.erode(mouth_mask.clip(0, 1), numpy.ones((21, 3)))
|
|
|
|
|
mouth_mask = cv2.GaussianBlur(mouth_mask, (0, 0), sigmaX = 1, sigmaY = 15)
|
|
|
|
|
return mouth_mask
|