Remove semaphore where possible

This commit is contained in:
henryruhs
2023-10-14 18:36:33 +02:00
parent 3a9a0f017b
commit d5bb7009b6
3 changed files with 9 additions and 11 deletions
+4 -3
View File
@@ -1,11 +1,12 @@
from typing import Tuple, Dict
from typing import Tuple, Dict, Any
import cv2
import numpy
from cv2.typing import Size
from facefusion.typing import Face, Frame, Matrix, Template
TEMPLATES : Dict[Template, numpy.array] =\
TEMPLATES : Dict[Template, numpy.ndarray[Any, Any]] =\
{
'arcface': numpy.array(
[
@@ -26,7 +27,7 @@ TEMPLATES : Dict[Template, numpy.array] =\
}
def warp_face(target_face : Face, temp_frame : Frame, template : Template, size = cv2.typing.Size) -> Tuple[Frame, Matrix]:
def warp_face(target_face : Face, temp_frame : Frame, template : Template, size : Size) -> Tuple[Frame, Matrix]:
affine_matrix = cv2.estimateAffinePartial2D(target_face.kps, TEMPLATES[template], method = cv2.LMEDS)[0]
crop_frame = cv2.warpAffine(temp_frame, affine_matrix, size)
return crop_frame, affine_matrix
@@ -17,7 +17,6 @@ from facefusion.processors.frame import globals as frame_processors_globals
from facefusion.processors.frame import choices as frame_processors_choices
FRAME_PROCESSOR = None
THREAD_SEMAPHORE : threading.Semaphore = threading.Semaphore()
THREAD_LOCK : threading.Lock = threading.Lock()
NAME = 'FACEFUSION.FRAME_PROCESSOR.FACE_ENHANCER'
MODELS : Dict[str, ModelValue] =\
@@ -137,8 +136,7 @@ def enhance_face(target_face: Face, temp_frame: Frame) -> Frame:
frame_processor_inputs[frame_processor_input.name] = crop_frame
if frame_processor_input.name == 'weight':
frame_processor_inputs[frame_processor_input.name] = numpy.array([ 1 ], dtype = numpy.double)
with THREAD_SEMAPHORE:
crop_frame = frame_processor.run(None, frame_processor_inputs)[0][0]
crop_frame = frame_processor.run(None, frame_processor_inputs)[0][0]
crop_frame = normalize_crop_frame(crop_frame)
paste_frame = paste_back(temp_frame, crop_frame, affine_matrix)
temp_frame = blend_frame(temp_frame, paste_frame)
@@ -22,7 +22,6 @@ from facefusion.processors.frame import choices as frame_processors_choices
FRAME_PROCESSOR = None
MODEL_MATRIX = None
THREAD_SEMAPHORE : threading.Semaphore = threading.Semaphore()
THREAD_LOCK : threading.Lock = threading.Lock()
NAME = 'FACEFUSION.FRAME_PROCESSOR.FACE_SWAPPER'
MODELS : Dict[str, ModelValue] =\
@@ -141,16 +140,16 @@ def post_process() -> None:
def swap_face(source_face : Face, target_face : Face, temp_frame : Frame) -> Frame:
frame_processor = get_frame_processor()
source_face = prepare_source_face(source_face)
crop_frame, affine_matrix = warp_face(target_face, temp_frame, 'arcface', (128, 128))
crop_frame = prepare_crop_frame(crop_frame)
frame_processor_inputs = {}
for frame_processor_input in frame_processor.get_inputs():
if frame_processor_input.name == 'source':
frame_processor_inputs[frame_processor_input.name] = source_face
if frame_processor_input.name == 'target':
frame_processor_inputs[frame_processor_input.name] = crop_frame
if frame_processor_input.name == 'source':
frame_processor_inputs[frame_processor_input.name] = prepare_source_face(source_face)
with THREAD_SEMAPHORE:
crop_frame = frame_processor.run(None, frame_processor_inputs)[0][0]
crop_frame = frame_processor.run(None, frame_processor_inputs)[0][0]
crop_frame = normalize_crop_frame(crop_frame)
temp_frame = paste_back(temp_frame, crop_frame, affine_matrix)
return temp_frame