@ -38,8 +38,11 @@ def sanitize_name(name: str) -> str:
def build_face_checkpoint_and_save (
def build_face_checkpoint_and_save (
images : List [ PILImage ] , name : str , overwrite : bool = False , path : str = None
images : List [ PILImage ] ,
) - > PILImage :
name : str ,
overwrite : bool = False ,
path : Optional [ str ] = None ,
) - > Optional [ PILImage ] :
"""
"""
Builds a face checkpoint using the provided image files , performs face swapping ,
Builds a face checkpoint using the provided image files , performs face swapping ,
and saves the result to a file . If a blended face is successfully obtained and the face swapping
and saves the result to a file . If a blended face is successfully obtained and the face swapping
@ -57,8 +60,12 @@ def build_face_checkpoint_and_save(
name = sanitize_name ( name )
name = sanitize_name ( name )
images = images or [ ]
images = images or [ ]
logger . info ( " Build %s with %s images " , name , len ( images ) )
logger . info ( " Build %s with %s images " , name , len ( images ) )
faces = swapper . get_faces_from_img_files ( images )
faces : List [ Face ] = swapper . get_faces_from_img_files ( images = images )
blended_face = swapper . blend_faces ( faces )
if faces is None or len ( faces ) == 0 :
logger . error ( " No source faces found " )
return None
blended_face : Optional [ Face ] = swapper . blend_faces ( faces )
preview_path = os . path . join (
preview_path = os . path . join (
scripts . basedir ( ) , " extensions " , " sd-webui-faceswaplab " , " references "
scripts . basedir ( ) , " extensions " , " sd-webui-faceswaplab " , " references "
)
)
@ -85,40 +92,51 @@ def build_face_checkpoint_and_save(
" Failed to open reference image, cannot create preview : That should not happen unless you deleted the references folder or change the detection threshold. "
" Failed to open reference image, cannot create preview : That should not happen unless you deleted the references folder or change the detection threshold. "
)
)
else :
else :
result = swapper . swap_face (
result : swapper . ImageResult = swapper . swap_face (
target_faces = [ target_face ] ,
target_faces = [ target_face ] ,
source_face = blended_face ,
source_face = blended_face ,
target_img = reference_preview_img ,
target_img = reference_preview_img ,
model = get_swap_models ( ) [ 0 ] ,
model = get_swap_models ( ) [ 0 ] ,
swapping_options = InswappperOptions ( face_restorer_name = " Codeformer " ) ,
swapping_options = InswappperOptions (
face_restorer_name = " CodeFormer " ,
restorer_visibility = 1 ,
upscaler_name = " Lanczos " ,
codeformer_weight = 1 ,
improved_mask = True ,
color_corrections = False ,
sharpen = True ,
) ,
)
)
preview_image = result . image
preview_image = result . image
if path :
if path :
file_path = path
file_path = path
else :
else :
file_path = os . path . join ( get_checkpoint_path ( ) , f " { name } .safetensors " )
file_path = os . path . join (
if not overwrite :
get_checkpoint_path ( ) , f " { name } .safetensors "
file_number = 1
)
while os . path . exists ( file_path ) :
if not overwrite :
file_path = os . path . join (
file_number = 1
get_checkpoint_path ( ) , f " { name } _ { file_number } .safetensors "
while os . path . exists ( file_path ) :
)
file_path = os . path . join (
file_number + = 1
get_checkpoint_path ( ) ,
save_face ( filename = file_path , face = blended_face )
f " { name } _ { file_number } .safetensors " ,
preview_image . save ( file_path + " .png " )
)
try :
file_number + = 1
data = load_face ( file_path )
save_face ( filename = file_path , face = blended_face )
logger . debug ( data )
preview_image . save ( file_path + " .png " )
except Exception as e :
try :
logger . error ( " Error loading checkpoint, after creation %s " , e )
data = load_face ( file_path )
traceback . print_exc ( )
logger . debug ( data )
except Exception as e :
return preview_image
logger . error ( " Error loading checkpoint, after creation %s " , e )
traceback . print_exc ( )
return preview_image
else :
else :
logger . error ( " No face found " )
logger . error ( " No face found " )
return None
return None # type: ignore
except Exception as e :
except Exception as e :
logger . error ( " Failed to build checkpoint %s " , e )
logger . error ( " Failed to build checkpoint %s " , e )
traceback . print_exc ( )
traceback . print_exc ( )
@ -139,7 +157,7 @@ def save_face(face: Face, filename: str) -> None:
raise e
raise e
def load_face ( name : str ) - > Face:
def load_face ( name : str ) - > Optional[ Face] :
if name . startswith ( " data:application/face;base64, " ) :
if name . startswith ( " data:application/face;base64, " ) :
with tempfile . NamedTemporaryFile ( delete = True ) as temp_file :
with tempfile . NamedTemporaryFile ( delete = True ) as temp_file :
api_utils . base64_to_safetensors ( name , temp_file . name )
api_utils . base64_to_safetensors ( name , temp_file . name )