* Mark as NEXT * Reduce caching to avoid RAM explosion * Reduce caching to avoid RAM explosion * Update dependencies * add face-detector-pad-factor * update facefusion.ini * fix test * change pad to margin * fix order * add prepare margin * use 50% max margin * Minor fixes part2 * Minor fixes part3 * Minor fixes part4 * Minor fixes part1 * Downgrade onnxruntime as of BiRefNet broken on CPU add test update update facefusion.ini add birefnet * rename models add more models * Fix versions * Add .claude to gitignore * add normalize color add 4 channel add colors * worflows * cleanup * cleanup * cleanup * cleanup * add more models (#961) * Fix naming * changes * Fix style and mock Gradio * Fix style and mock Gradio * Fix style and mock Gradio * apply clamp * remove clamp * Add normalizer test * Introduce sanitizer for the rescue (#963) * Introduce sanitizer for the rescue * Introduce sanitizer for the rescue * Introduce sanitizer for the rescue * prepare ffmpeg for alpha support * Some cleanup * Some cleanup * Fix CI * List as TypeAlias is not allowed (#967) * List as TypeAlias is not allowed * List as TypeAlias is not allowed * List as TypeAlias is not allowed * List as TypeAlias is not allowed * Add mpeg and mxf support (#968) * Add mpeg support * Add mxf support * Adjust fix_xxx_encoder for the new formats * Extend output pattern for batch-run (#969) * Extend output pattern for batch-run * Add {target_extension} to allowed mixed files * Catch invalid output pattern keys * alpha support * cleanup * cleanup * add ProcessorOutputs type * fix preview and streamer, support alpha for background_remover * Refactor/open close processors (#972) * Introduce open/close processors * Add locales for translator * Introduce __autoload__ for translator * More cleanup * Fix import issues * Resolve the scope situation for locals * Fix installer by not using translator * Fixes after merge * Fixes after merge * Fix translator keys in ui * Use LOCALS in installer * Update and partial fix DirectML * Use latest onnxruntime * Fix performance * Fix lint issues * fix mask * fix lint * fix lint * Remove default from translator.get() * remove 'framerate=' * fix test * Rename and reorder models * Align naming * add alpha preview * fix frame-by-frame * Add alpha effect via css * preview support alpha channel * fix preview modes * Use official assets repositories * Add support for u2net_cloth * fix naming * Add more models * Add vendor, license and year direct to the models * Add vendor, license and year direct to the models * Update dependencies, Minor CSS adjustment * Ready for 3.5.0 * Fix naming * Update about messages * Fix return * Use groups to show/hide * Update preview * Conditional merge mask * Conditional merge mask * Fix import order --------- Co-authored-by: harisreedhar <h4harisreedhar.s.s@gmail.com> Co-authored-by: Harisreedhar <46858047+harisreedhar@users.noreply.github.com>
94 lines
2.9 KiB
Python
94 lines
2.9 KiB
Python
from functools import lru_cache
|
|
from typing import Tuple
|
|
|
|
import numpy
|
|
|
|
from facefusion import inference_manager
|
|
from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url
|
|
from facefusion.face_helper import warp_face_by_face_landmark_5
|
|
from facefusion.filesystem import resolve_relative_path
|
|
from facefusion.thread_helper import conditional_thread_semaphore
|
|
from facefusion.types import DownloadScope, Embedding, FaceLandmark5, InferencePool, ModelOptions, ModelSet, VisionFrame
|
|
|
|
|
|
@lru_cache()
|
|
def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
|
|
return\
|
|
{
|
|
'arcface':
|
|
{
|
|
'__metadata__':
|
|
{
|
|
'vendor': 'InsightFace',
|
|
'license': 'Non-Commercial',
|
|
'year': 2018
|
|
},
|
|
'hashes':
|
|
{
|
|
'face_recognizer':
|
|
{
|
|
'url': resolve_download_url('models-3.0.0', 'arcface_w600k_r50.hash'),
|
|
'path': resolve_relative_path('../.assets/models/arcface_w600k_r50.hash')
|
|
}
|
|
},
|
|
'sources':
|
|
{
|
|
'face_recognizer':
|
|
{
|
|
'url': resolve_download_url('models-3.0.0', 'arcface_w600k_r50.onnx'),
|
|
'path': resolve_relative_path('../.assets/models/arcface_w600k_r50.onnx')
|
|
}
|
|
},
|
|
'template': 'arcface_112_v2',
|
|
'size': (112, 112)
|
|
}
|
|
}
|
|
|
|
|
|
def get_inference_pool() -> InferencePool:
|
|
model_names = [ 'arcface' ]
|
|
model_source_set = get_model_options().get('sources')
|
|
|
|
return inference_manager.get_inference_pool(__name__, model_names, model_source_set)
|
|
|
|
|
|
def clear_inference_pool() -> None:
|
|
model_names = [ 'arcface' ]
|
|
inference_manager.clear_inference_pool(__name__, model_names)
|
|
|
|
|
|
def get_model_options() -> ModelOptions:
|
|
return create_static_model_set('full').get('arcface')
|
|
|
|
|
|
def pre_check() -> bool:
|
|
model_hash_set = get_model_options().get('hashes')
|
|
model_source_set = get_model_options().get('sources')
|
|
|
|
return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
|
|
|
|
|
|
def calculate_face_embedding(temp_vision_frame : VisionFrame, face_landmark_5 : FaceLandmark5) -> Tuple[Embedding, Embedding]:
|
|
model_template = get_model_options().get('template')
|
|
model_size = get_model_options().get('size')
|
|
crop_vision_frame, matrix = warp_face_by_face_landmark_5(temp_vision_frame, face_landmark_5, model_template, model_size)
|
|
crop_vision_frame = crop_vision_frame / 127.5 - 1
|
|
crop_vision_frame = crop_vision_frame[:, :, ::-1].transpose(2, 0, 1).astype(numpy.float32)
|
|
crop_vision_frame = numpy.expand_dims(crop_vision_frame, axis = 0)
|
|
face_embedding = forward(crop_vision_frame)
|
|
face_embedding = face_embedding.ravel()
|
|
face_embedding_norm = face_embedding / numpy.linalg.norm(face_embedding)
|
|
return face_embedding, face_embedding_norm
|
|
|
|
|
|
def forward(crop_vision_frame : VisionFrame) -> Embedding:
|
|
face_recognizer = get_inference_pool().get('face_recognizer')
|
|
|
|
with conditional_thread_semaphore():
|
|
face_embedding = face_recognizer.run(None,
|
|
{
|
|
'input': crop_vision_frame
|
|
})[0]
|
|
|
|
return face_embedding
|