api in progress

pull/152/head
Tran Xen 2023-07-19 00:11:28 +02:00
parent b7e1b0dd8f
commit 51f284748d
5 changed files with 90 additions and 2 deletions

View File

@ -5,4 +5,5 @@ opencv-python==4.7.0.72
dill==0.3.6
pandas
ifnude
cython
cython
pydantic==1.10.9

39
scripts/api.py Normal file
View File

@ -0,0 +1,39 @@
from PIL import Image
import numpy as np
from fastapi import FastAPI, Body
from fastapi.exceptions import HTTPException
from modules.api.models import *
from modules.api import api
from scripts.roop_globals import VERSION_FLAG
import gradio as gr
def encode_to_base64(image):
if type(image) is str:
return image
elif type(image) is Image.Image:
return api.encode_pil_to_base64(image)
elif type(image) is np.ndarray:
return encode_np_to_base64(image)
else:
return ""
def encode_np_to_base64(image):
pil = Image.fromarray(image)
return api.encode_pil_to_base64(pil)
def roop_api(_: gr.Blocks, app: FastAPI):
@app.get("/roop/version")
async def version():
return {"version": VERSION_FLAG}
@app.get("/roop/swap_face")
async def swap_face():
return {"version": VERSION_FLAG}
try:
import modules.script_callbacks as script_callbacks
script_callbacks.on_app_started(roop_api)
except:
pass

View File

@ -135,3 +135,5 @@ class FaceSwapUnitSettings:
assert(not np.array_equal(self._blended_faces.embedding,self.reference_face.embedding) if len(self.faces) > 1 else True), "Blended faces cannot be the same as reference face if len(face)>0"
return self._blended_faces

View File

@ -0,0 +1,46 @@
from scripts.roop_swapping import swapper
import numpy as np
import base64
import io
from dataclasses import dataclass, fields
from typing import Dict, List, Set, Tuple, Union, Optional
import dill as pickle
import gradio as gr
from insightface.app.common import Face
from PIL import Image
from scripts.roop_utils.imgutils import (pil_to_cv2,convert_to_sd)
from scripts.roop_logging import logger
@dataclass
class FaceSwapUnitDTO :
# The image given in reference
source_img: str
# The checkpoint file
source_face : str
# base64 batch source images
batch_files: List[str]
# Will blend faces if True
blend_faces: bool
# Enable this unit
enable: bool
# Use same gender filtering
same_gender: bool
# If True, discard images with low similarity
check_similarity : bool
# if True will compute similarity and add it to the image info
compute_similarity :bool
# Minimum similarity against the used face (reference, batch or checkpoint)
min_sim: float
# Minimum similarity against the reference (reference or checkpoint if checkpoint is given)
min_ref_sim: float
# The face index to use for swapping
faces_index: int
# Swap in the source image in img2img (before processing)
swap_in_source: bool
# Swap in the generated image in img2img (always on for txt2img)
swap_in_generated: bool

View File

@ -3,7 +3,7 @@ import os
MODELS_DIR = os.path.abspath(os.path.join("models","roop"))
ANALYZER_DIR = os.path.abspath(os.path.join(MODELS_DIR, "analysers"))
VERSION_FLAG = "v0.1.3"
VERSION_FLAG = "v0.3.0"
EXTENSION_PATH=os.path.join("extensions","sd-webui-roop")
SD_CONVERT_SCORE = 0.7