Addbuild facemodel api
This commit is contained in:
parent
d2e78be2b3
commit
d2f6b7d29d
@ -20,7 +20,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
|
||||
|
||||
@ -163,6 +163,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
|
||||
|
||||
|
||||
@ -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] = []
|
||||
if not is_api:
|
||||
images, images_names = get_images_from_list(images_list)
|
||||
else:
|
||||
images = images_list
|
||||
for i,image in enumerate(images):
|
||||
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:
|
||||
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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user