Addbuild facemodel api

This commit is contained in:
KYOOKIM 2024-04-28 08:15:44 +09:00
parent d2e78be2b3
commit d2f6b7d29d
2 changed files with 25 additions and 5 deletions

View File

@ -20,7 +20,7 @@ from modules.api import api
import gradio as gr import gradio as gr
from scripts.reactor_swapper import EnhancementOptions, swap_face, DetectionOptions from scripts.reactor_swapper import EnhancementOptions, blend_faces, swap_face, DetectionOptions
from scripts.reactor_logger import logger from scripts.reactor_logger import logger
from scripts.reactor_helpers import get_facemodels from scripts.reactor_helpers import get_facemodels
@ -163,6 +163,17 @@ def reactor_api(_: gr.Blocks, app: FastAPI):
facemodels = [os.path.split(model)[1].split(".")[0] for model in get_facemodels()] facemodels = [os.path.split(model)[1].split(".")[0] for model in get_facemodels()]
return {"facemodels": facemodels} return {"facemodels": facemodels}
@app.post("/reactor/facemodels")
def build_face_model(
source_images: list[str] = Body([""],title="Source Face Image List"),
name: str = Body("",title="Face Model Name"),
compute_method: int = Body(0,title="Compute Method (Mean, Median, Mode)"),
shape_check: bool = Body(False,title="Check Embedding Shape"),
):
images = [api.decode_base64_to_image(img) for img in source_images]
blend_faces(images, name, compute_method, shape_check, is_api=True)
return {"facemodels": [os.path.split(model)[1].split(".")[0] for model in get_facemodels()]}
try: try:
import modules.script_callbacks as script_callbacks import modules.script_callbacks as script_callbacks

View File

@ -661,13 +661,19 @@ def build_face_model(image: Image.Image, name: str, save_model: bool = True, det
logger.error(no_face_msg) logger.error(no_face_msg)
return no_face_msg return no_face_msg
def blend_faces(images_list: List, name: str, compute_method: int = 0, shape_check: bool = False): def blend_faces(images_list: List, name: str, compute_method: int = 0, shape_check: bool = False, is_api: bool = False):
faces = [] faces = []
embeddings = [] embeddings = []
images: List[Image.Image] = [] images: List[Image.Image] = []
images, images_names = get_images_from_list(images_list) if not is_api:
images, images_names = get_images_from_list(images_list)
else:
images = images_list
for i,image in enumerate(images): for i,image in enumerate(images):
logger.status(f"Building Face Model for {images_names[i]}...") if not is_api:
logger.status(f"Building Face Model for {images_names[i]}...")
else:
logger.status(f"Building Face Model for Image {i+1}...")
face = build_face_model(image,str(i),save_model=False) face = build_face_model(image,str(i),save_model=False)
if isinstance(face, str): if isinstance(face, str):
# logger.error(f"No faces found in {images_names[i]}, skipping") # logger.error(f"No faces found in {images_names[i]}, skipping")
@ -676,7 +682,10 @@ def blend_faces(images_list: List, name: str, compute_method: int = 0, shape_che
if i == 0: if i == 0:
embedding_shape = face.embedding.shape embedding_shape = face.embedding.shape
elif face.embedding.shape != embedding_shape: elif face.embedding.shape != embedding_shape:
logger.error(f"Embedding Shape Mismatch for {images_names[i]}, skipping") if not is_api:
logger.error(f"Embedding Shape Mismatch for {images_names[i]}, skipping")
else:
logger.error(f"Embedding Shape Mismatch for Image {i+1}, skipping")
continue continue
faces.append(face) faces.append(face)
embeddings.append(face.embedding) embeddings.append(face.embedding)