* Replace audio whenever set via source * add H264_qsv&HEVC_qsv (#768) * Update ffmpeg.py * Update choices.py * Update typing.py * Fix spaces and newlines * Fix return type * Introduce hififace swapper * Disable stream for expression restorer * Webcam polishing part1 (#796) * Cosmetics on ignore comments * Testing for replace audio * Testing for restore audio * Testing for restore audio * Fix replace_audio() * Remove shortest and use fixed video duration * Remove shortest and use fixed video duration * Prevent duplicate entries to local PATH * Do hard exit on invalid args * Need for Python 3.10 * Fix state of face selector * Fix OpenVINO by aliasing GPU.0 to GPU * Fix OpenVINO by aliasing GPU.0 to GPU * Fix/age modifier styleganex 512 (#798) * fix * styleganex template * changes * changes * fix occlusion mask * add age modifier scale * change * change * hardcode * Cleanup * Use model_sizes and model_templates variables * No need for prepare when just 2 lines of code * Someone used spaces over tabs * Revert back [0][0] --------- Co-authored-by: harisreedhar <h4harisreedhar.s.s@gmail.com> * Feat/update gradio5 (#799) * Update to Gradio 5 * Remove overrides for Gradio * Fix dark mode for Gradio * Polish errors * More styles for tabs and co * Make slider inputs and reset like a unit * Make slider inputs and reset like a unit * Adjust naming * Improved color matching (#800) * aura fix * fix import * move to vision.py * changes * changes * changes * changes * further reduction * add test * better test * change name * Minor cleanup * Minor cleanup * Minor cleanup * changes (#801) * Switch to official assets repo * Add __pycache__ to gitignore * Gradio pinned python-multipart to 0.0.12 * Update dependencies * Feat/temp path second try (#802) * Terminate base directory from temp helper * Partial adjust program codebase * Move arguments around * Make `-j` absolete * Resolve args * Fix job register keys * Adjust date test * Finalize temp path * Update onnxruntime * Update dependencies * Adjust color for checkboxes * Revert due terrible performance * Fix/enforce vp9 for webm (#805) * Simple fix to enforce vp9 for webm * Remove suggest methods from program helper * Cleanup ffmpeg.py a bit * Update onnxruntime (second try) * Update onnxruntime (second try) * Remove cudnn_conv_algo_search tweaks * Remove cudnn_conv_algo_search tweaks * changes * add both mask instead of multiply * adaptive color correction * changes * remove model size requirement * changes * add to facefusion.ini * changes * changes * changes * Add namespace for dfm creators * Release five frame enhancer models * Remove vendor from model name * Remove vendor from model name * changes * changes * changes * changes * Feat/download providers (#809) * Introduce download providers * update processors download method * add ui * Fix CI * Adjust UI component order, Use download resolver for benchmark * Remove is_download_done() * Introduce download provider set, Remove choices method from execution, cast all dict keys() via list() * Fix spacing --------- Co-authored-by: harisreedhar <h4harisreedhar.s.s@gmail.com> * Fix model paths for 3.1.0 * Introduce bulk-run (#810) * Introduce bulk-run * Make bulk run bullet proof * Integration test for bulk-run * new alignment * Add safer global named resolve_file_pattern() (#811) * Allow bulk runner with target pattern only * changes * changes * Update Python to 3.12 for CI (#813) * changes * Improve NVIDIA device lookups * Rename template key to deepfacelive * Fix name * Improve resolve download * Rename bulk-run to batch-run * Make deep swapper inputs universal * Add more deepfacelive models * Use different morph value * Feat/simplify hashes sources download (#814) * Extract download directory path from assets path * Fix lint * Fix force-download command, Fix urls in frame enhancer * changes * fix warp_face_by_bounding_box dtype error * DFM Morph (#816) * changes * Improve wording, Replace [None], SideQuest: clean forward() of age modifier * SideQuest: clean forward() of face enhancer --------- Co-authored-by: henryruhs <info@henryruhs.com> * Fix preview refresh after slide * Add more deepfacelive models (#817) * Add more deepfacelive models * Add more deepfacelive models * Fix deep swapper sizes * Kill accent colors, Number input styles for Chrome * Simplify thumbnail-item looks * Fix first black screen * Introduce model helper * ci.yml: Add macOS on ARM64 to the testing (#818) * ci.yml: Add macOS on ARM64 to the testing * ci.yml: uses: AnimMouse/setup-ffmpeg@v1 * ci.yml: strategy: matrix: os: macos-latest, * - name: Set up FFmpeg * Update .github/workflows/ci.yml * Update ci.yml --------- Co-authored-by: Henry Ruhs <info@henryruhs.com> * Show/hide morph slider for deep swapper (#822) * remove dfl_head and update dfl_whole_face template * Add deep swapper models by Mats * Add deep swapper models by Druuzil * Add deep swapper models by Rumateus * Implement face enhancer weight for codeformer, Side Quest: has proces… (#823) * Implement face enhancer weight for codeformer, Side Quest: has processor checks * Fix typo * Fix face enhancer blend in UI * Use static model set creation * Add deep swapper models by Jen * Introduce create_static_model_set() everywhere (#824) * Move clear over to the UI (#825) * Fix model key * Undo restore_audio() * Switch to latest XSeg * Switch to latest XSeg * Switch to latest XSeg * Use resolve_download_url() everywhere, Vanish --skip-download flag * Fix resolve_download_url * Fix space * Kill resolve_execution_provider_keys() and move fallbacks where they belong * Kill resolve_execution_provider_keys() and move fallbacks where they belong * Remove as this does not work * Change TempFrameFormat order * Fix CoreML partially * Remove duplicates (Rumateus is the creator) * Add deep swapper models by Edel * Introduce download scopes (#826) * Introduce download scopes * Limit download scopes to force-download command * Change source-paths behaviour * Fix space * Update README * Rename create_log_level_program to create_misc_program * Fix wording * Fix wording * Update dependencies * Use tolerant for video_memory_strategy in benchmark * Feat/ffmpeg with progress (#827) * FFmpeg with progress bar * Fix typing * FFmpeg with progress bar part2 * Restore streaming wording * Change order in choices and typing * Introduce File using list_directory() (#830) * Feat/local deep swapper models (#832) * Local model support for deep swapper * Local model support for deep swapper part2 * Local model support for deep swapper part3 * Update yet another dfm by Druuzil * Refactor/choices and naming (#833) * Refactor choices, imports and naming * Refactor choices, imports and naming * Fix styles for tabs, Restore toast * Update yet another dfm by Druuzil * Feat/face masker models (#834) * Introduce face masker models * Introduce face masker models * Introduce face masker models * Register needed step keys * Provide different XSeg models * Simplify model context * Fix out of range for trim frame, Fix ffmpeg extraction count (#836) * Fix out of range for trim frame, Fix ffmpeg extraction count * Move restrict of trim frame to the core, Make sure all values are within the range * Fix and merge testing * Fix typing * Add region mask for deep swapper * Adjust wording * Move FACE_MASK_REGIONS to choices * Update dependencies * Feat/download provider fallback (#837) * Introduce download providers fallback, Use CURL everywhre * Fix CI * Use readlines() over readline() to avoid while * Use readlines() over readline() to avoid while * Use readlines() over readline() to avoid while * Use communicate() over wait() * Minor updates for testing * Stop webcam on source image change * Feat/webcam improvements (#838) * Detect available webcams * Fix CI, Move webcam id dropdown to the sidebar, Disable warnings * Fix CI * Remove signal on hard_exit() to prevent exceptions * Fix border color in toast timer * Prepare release * Update preview * Update preview * Hotfix progress bar --------- Co-authored-by: DDXDB <38449595+DDXDB@users.noreply.github.com> Co-authored-by: harisreedhar <h4harisreedhar.s.s@gmail.com> Co-authored-by: Harisreedhar <46858047+harisreedhar@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
235 lines
8.8 KiB
Python
235 lines
8.8 KiB
Python
from functools import lru_cache
|
|
from typing import List, Sequence, Tuple
|
|
|
|
import cv2
|
|
import numpy
|
|
from cv2.typing import Size
|
|
|
|
from facefusion.typing import Anchors, Angle, BoundingBox, Distance, FaceDetectorModel, FaceLandmark5, FaceLandmark68, Mask, Matrix, Points, Scale, Score, Translation, VisionFrame, WarpTemplate, WarpTemplateSet
|
|
|
|
WARP_TEMPLATES : WarpTemplateSet =\
|
|
{
|
|
'arcface_112_v1': numpy.array(
|
|
[
|
|
[ 0.35473214, 0.45658929 ],
|
|
[ 0.64526786, 0.45658929 ],
|
|
[ 0.50000000, 0.61154464 ],
|
|
[ 0.37913393, 0.77687500 ],
|
|
[ 0.62086607, 0.77687500 ]
|
|
]),
|
|
'arcface_112_v2': numpy.array(
|
|
[
|
|
[ 0.34191607, 0.46157411 ],
|
|
[ 0.65653393, 0.45983393 ],
|
|
[ 0.50022500, 0.64050536 ],
|
|
[ 0.37097589, 0.82469196 ],
|
|
[ 0.63151696, 0.82325089 ]
|
|
]),
|
|
'arcface_128_v2': numpy.array(
|
|
[
|
|
[ 0.36167656, 0.40387734 ],
|
|
[ 0.63696719, 0.40235469 ],
|
|
[ 0.50019687, 0.56044219 ],
|
|
[ 0.38710391, 0.72160547 ],
|
|
[ 0.61507734, 0.72034453 ]
|
|
]),
|
|
'dfl_whole_face': numpy.array(
|
|
[
|
|
[ 0.35342266, 0.39285716 ],
|
|
[ 0.62797622, 0.39285716 ],
|
|
[ 0.48660713, 0.54017860 ],
|
|
[ 0.38839287, 0.68750011 ],
|
|
[ 0.59821427, 0.68750011 ]
|
|
]),
|
|
'ffhq_512': numpy.array(
|
|
[
|
|
[ 0.37691676, 0.46864664 ],
|
|
[ 0.62285697, 0.46912813 ],
|
|
[ 0.50123859, 0.61331904 ],
|
|
[ 0.39308822, 0.72541100 ],
|
|
[ 0.61150205, 0.72490465 ]
|
|
]),
|
|
'mtcnn_512': numpy.array(
|
|
[
|
|
[ 0.36562865, 0.46733799 ],
|
|
[ 0.63305391, 0.46585885 ],
|
|
[ 0.50019127, 0.61942959 ],
|
|
[ 0.39032951, 0.77598822 ],
|
|
[ 0.61178945, 0.77476328 ]
|
|
]),
|
|
'styleganex_384': numpy.array(
|
|
[
|
|
[ 0.42353745, 0.52289879 ],
|
|
[ 0.57725008, 0.52319972 ],
|
|
[ 0.50123859, 0.61331904 ],
|
|
[ 0.43364461, 0.68337652 ],
|
|
[ 0.57015325, 0.68306005 ]
|
|
])
|
|
}
|
|
|
|
|
|
def estimate_matrix_by_face_landmark_5(face_landmark_5 : FaceLandmark5, warp_template : WarpTemplate, crop_size : Size) -> Matrix:
|
|
normed_warp_template = WARP_TEMPLATES.get(warp_template) * crop_size
|
|
affine_matrix = cv2.estimateAffinePartial2D(face_landmark_5, normed_warp_template, method = cv2.RANSAC, ransacReprojThreshold = 100)[0]
|
|
return affine_matrix
|
|
|
|
|
|
def warp_face_by_face_landmark_5(temp_vision_frame : VisionFrame, face_landmark_5 : FaceLandmark5, warp_template : WarpTemplate, crop_size : Size) -> Tuple[VisionFrame, Matrix]:
|
|
affine_matrix = estimate_matrix_by_face_landmark_5(face_landmark_5, warp_template, crop_size)
|
|
crop_vision_frame = cv2.warpAffine(temp_vision_frame, affine_matrix, crop_size, borderMode = cv2.BORDER_REPLICATE, flags = cv2.INTER_AREA)
|
|
return crop_vision_frame, affine_matrix
|
|
|
|
|
|
def warp_face_by_bounding_box(temp_vision_frame : VisionFrame, bounding_box : BoundingBox, crop_size : Size) -> Tuple[VisionFrame, Matrix]:
|
|
source_points = numpy.array([ [ bounding_box[0], bounding_box[1] ], [bounding_box[2], bounding_box[1] ], [ bounding_box[0], bounding_box[3] ] ]).astype(numpy.float32)
|
|
target_points = numpy.array([ [ 0, 0 ], [ crop_size[0], 0 ], [ 0, crop_size[1] ] ]).astype(numpy.float32)
|
|
affine_matrix = cv2.getAffineTransform(source_points, target_points)
|
|
if bounding_box[2] - bounding_box[0] > crop_size[0] or bounding_box[3] - bounding_box[1] > crop_size[1]:
|
|
interpolation_method = cv2.INTER_AREA
|
|
else:
|
|
interpolation_method = cv2.INTER_LINEAR
|
|
crop_vision_frame = cv2.warpAffine(temp_vision_frame, affine_matrix, crop_size, flags = interpolation_method)
|
|
return crop_vision_frame, affine_matrix
|
|
|
|
|
|
def warp_face_by_translation(temp_vision_frame : VisionFrame, translation : Translation, scale : float, crop_size : Size) -> Tuple[VisionFrame, Matrix]:
|
|
affine_matrix = numpy.array([ [ scale, 0, translation[0] ], [ 0, scale, translation[1] ] ])
|
|
crop_vision_frame = cv2.warpAffine(temp_vision_frame, affine_matrix, crop_size)
|
|
return crop_vision_frame, affine_matrix
|
|
|
|
|
|
def paste_back(temp_vision_frame : VisionFrame, crop_vision_frame : VisionFrame, crop_mask : Mask, affine_matrix : Matrix) -> VisionFrame:
|
|
inverse_matrix = cv2.invertAffineTransform(affine_matrix)
|
|
temp_size = temp_vision_frame.shape[:2][::-1]
|
|
inverse_mask = cv2.warpAffine(crop_mask, inverse_matrix, temp_size).clip(0, 1)
|
|
inverse_vision_frame = cv2.warpAffine(crop_vision_frame, inverse_matrix, temp_size, borderMode = cv2.BORDER_REPLICATE)
|
|
paste_vision_frame = temp_vision_frame.copy()
|
|
paste_vision_frame[:, :, 0] = inverse_mask * inverse_vision_frame[:, :, 0] + (1 - inverse_mask) * temp_vision_frame[:, :, 0]
|
|
paste_vision_frame[:, :, 1] = inverse_mask * inverse_vision_frame[:, :, 1] + (1 - inverse_mask) * temp_vision_frame[:, :, 1]
|
|
paste_vision_frame[:, :, 2] = inverse_mask * inverse_vision_frame[:, :, 2] + (1 - inverse_mask) * temp_vision_frame[:, :, 2]
|
|
return paste_vision_frame
|
|
|
|
|
|
@lru_cache(maxsize = None)
|
|
def create_static_anchors(feature_stride : int, anchor_total : int, stride_height : int, stride_width : int) -> Anchors:
|
|
y, x = numpy.mgrid[:stride_height, :stride_width][::-1]
|
|
anchors = numpy.stack((y, x), axis = -1)
|
|
anchors = (anchors * feature_stride).reshape((-1, 2))
|
|
anchors = numpy.stack([ anchors ] * anchor_total, axis = 1).reshape((-1, 2))
|
|
return anchors
|
|
|
|
|
|
def create_rotated_matrix_and_size(angle : Angle, size : Size) -> Tuple[Matrix, Size]:
|
|
rotated_matrix = cv2.getRotationMatrix2D((size[0] / 2, size[1] / 2), angle, 1)
|
|
rotated_size = numpy.dot(numpy.abs(rotated_matrix[:, :2]), size)
|
|
rotated_matrix[:, -1] += (rotated_size - size) * 0.5 #type:ignore[misc]
|
|
rotated_size = int(rotated_size[0]), int(rotated_size[1])
|
|
return rotated_matrix, rotated_size
|
|
|
|
|
|
def create_bounding_box(face_landmark_68 : FaceLandmark68) -> BoundingBox:
|
|
min_x, min_y = numpy.min(face_landmark_68, axis = 0)
|
|
max_x, max_y = numpy.max(face_landmark_68, axis = 0)
|
|
bounding_box = normalize_bounding_box(numpy.array([ min_x, min_y, max_x, max_y ]))
|
|
return bounding_box
|
|
|
|
|
|
def normalize_bounding_box(bounding_box : BoundingBox) -> BoundingBox:
|
|
x1, y1, x2, y2 = bounding_box
|
|
x1, x2 = sorted([ x1, x2 ])
|
|
y1, y2 = sorted([ y1, y2 ])
|
|
return numpy.array([ x1, y1, x2, y2 ])
|
|
|
|
|
|
def transform_points(points : Points, matrix : Matrix) -> Points:
|
|
points = points.reshape(-1, 1, 2)
|
|
points = cv2.transform(points, matrix) #type:ignore[assignment]
|
|
points = points.reshape(-1, 2)
|
|
return points
|
|
|
|
|
|
def transform_bounding_box(bounding_box : BoundingBox, matrix : Matrix) -> BoundingBox:
|
|
points = numpy.array(
|
|
[
|
|
[ bounding_box[0], bounding_box[1] ],
|
|
[ bounding_box[2], bounding_box[1] ],
|
|
[ bounding_box[2], bounding_box[3] ],
|
|
[ bounding_box[0], bounding_box[3] ]
|
|
])
|
|
points = transform_points(points, matrix)
|
|
x1, y1 = numpy.min(points, axis = 0)
|
|
x2, y2 = numpy.max(points, axis = 0)
|
|
return normalize_bounding_box(numpy.array([ x1, y1, x2, y2 ]))
|
|
|
|
|
|
def distance_to_bounding_box(points : Points, distance : Distance) -> BoundingBox:
|
|
x1 = points[:, 0] - distance[:, 0]
|
|
y1 = points[:, 1] - distance[:, 1]
|
|
x2 = points[:, 0] + distance[:, 2]
|
|
y2 = points[:, 1] + distance[:, 3]
|
|
bounding_box = numpy.column_stack([ x1, y1, x2, y2 ])
|
|
return bounding_box
|
|
|
|
|
|
def distance_to_face_landmark_5(points : Points, distance : Distance) -> FaceLandmark5:
|
|
x = points[:, 0::2] + distance[:, 0::2]
|
|
y = points[:, 1::2] + distance[:, 1::2]
|
|
face_landmark_5 = numpy.stack((x, y), axis = -1)
|
|
return face_landmark_5
|
|
|
|
|
|
def scale_face_landmark_5(face_landmark_5 : FaceLandmark5, scale : Scale) -> FaceLandmark5:
|
|
face_landmark_5_scale = face_landmark_5 - face_landmark_5[2]
|
|
face_landmark_5_scale *= scale
|
|
face_landmark_5_scale += face_landmark_5[2]
|
|
return face_landmark_5_scale
|
|
|
|
|
|
def convert_to_face_landmark_5(face_landmark_68 : FaceLandmark68) -> FaceLandmark5:
|
|
face_landmark_5 = numpy.array(
|
|
[
|
|
numpy.mean(face_landmark_68[36:42], axis = 0),
|
|
numpy.mean(face_landmark_68[42:48], axis = 0),
|
|
face_landmark_68[30],
|
|
face_landmark_68[48],
|
|
face_landmark_68[54]
|
|
])
|
|
return face_landmark_5
|
|
|
|
|
|
def estimate_face_angle(face_landmark_68 : FaceLandmark68) -> Angle:
|
|
x1, y1 = face_landmark_68[0]
|
|
x2, y2 = face_landmark_68[16]
|
|
theta = numpy.arctan2(y2 - y1, x2 - x1)
|
|
theta = numpy.degrees(theta) % 360
|
|
angles = numpy.linspace(0, 360, 5)
|
|
index = numpy.argmin(numpy.abs(angles - theta))
|
|
face_angle = int(angles[index] % 360)
|
|
return face_angle
|
|
|
|
|
|
def apply_nms(bounding_boxes : List[BoundingBox], face_scores : List[Score], score_threshold : float, nms_threshold : float) -> Sequence[int]:
|
|
normed_bounding_boxes = [ (x1, y1, x2 - x1, y2 - y1) for (x1, y1, x2, y2) in bounding_boxes ]
|
|
keep_indices = cv2.dnn.NMSBoxes(normed_bounding_boxes, face_scores, score_threshold = score_threshold, nms_threshold = nms_threshold)
|
|
return keep_indices
|
|
|
|
|
|
def get_nms_threshold(face_detector_model : FaceDetectorModel, face_detector_angles : List[Angle]) -> float:
|
|
if face_detector_model == 'many':
|
|
return 0.1
|
|
if len(face_detector_angles) == 2:
|
|
return 0.3
|
|
if len(face_detector_angles) == 3:
|
|
return 0.2
|
|
if len(face_detector_angles) == 4:
|
|
return 0.1
|
|
return 0.4
|
|
|
|
|
|
def merge_matrix(matrices : List[Matrix]) -> Matrix:
|
|
merged_matrix = numpy.vstack([ matrices[0], [ 0, 0, 1 ] ])
|
|
for matrix in matrices[1:]:
|
|
matrix = numpy.vstack([ matrix, [ 0, 0, 1 ] ])
|
|
merged_matrix = numpy.dot(merged_matrix, matrix)
|
|
return merged_matrix[:2, :]
|