Merge pull request #438 from KY00KIM/main

UPDATE: Add api-endpoint for building face model
This commit is contained in:
Евгений Гурьев | Eugene Gourieff | 古仁 2024-09-15 17:45:30 +07:00 committed by GitHub
commit f680b43100
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 5 deletions

View File

@ -25,7 +25,7 @@ from modules.api import api
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_helpers import get_facemodels
@ -187,6 +187,17 @@ def reactor_api(_: gr.Blocks, app: FastAPI):
facemodels = [os.path.split(model)[1].split(".")[0] for model in get_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:
import modules.script_callbacks as script_callbacks
script_callbacks.on_app_started(reactor_api)

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)
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 = []
embeddings = []
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):
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)
if isinstance(face, str):
# 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:
embedding_shape = face.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
faces.append(face)
embeddings.append(face.embedding)