Compare commits

...

25 Commits
0.0.2 ... main

Author SHA1 Message Date
Somdev Sangwan de29743ef2 archive notice 2026-03-13 15:00:10 +05:30
Somdev Sangwan efb2c6c1cd archive project and add final notice 2023-10-08 12:18:53 +05:30
Somdev Sangwan 4cb63f5516
Merge pull request #242 from maxchiron/main
Update install.py
2023-10-06 09:51:59 +05:30
maxchiron cb9ce1e880
Update install.py 2023-09-12 17:20:34 +08:00
Somdev Sangwan 3176d477d7
Merge pull request #214 from SpenserCai/main
support api
2023-08-22 02:37:08 +05:30
Somdev Sangwan ab6646b5e8
fix model link 2023-08-22 02:36:55 +05:30
SpenserCai 00fd9ad8a9 change error default 2023-08-21 17:05:48 +08:00
SpenserCai 8381a8e52c support api 2023-08-20 22:39:32 +08:00
Somdev Sangwan e6333fbe5a
gpu has no performance benefit 2023-07-07 14:52:59 +05:30
Somdev Sangwan ddc02ee1a9
Fix path for windows 2023-06-25 08:56:53 +05:30
Somdev Sangwan cdcd31b164
Update README.md 2023-06-22 16:22:04 +05:30
Somdev Sangwan a116b2cb71
enable GPU support 2023-06-22 16:17:58 +05:30
Somdev Sangwan 124f7d7acf
fix batch 2023-06-22 15:36:54 +05:30
Somdev Sangwan ef41538594
fix batch args 2023-06-22 14:50:12 +05:30
Somdev Sangwan 8934e1f180
fix postprocessbatch 2023-06-22 09:05:15 +05:30
Somdev Sangwan 9723e8b84c
common error fix 2023-06-22 08:19:24 +05:30
Somdev Sangwan b9071381a3
Update faceswap.py 2023-06-22 05:32:29 +05:30
Somdev Sangwan 8191992cb5
Merge pull request #38 from j129008/main
load model function fix
2023-06-22 05:30:33 +05:30
davidchang 29acad245e load model function fix 2023-06-22 06:03:36 +08:00
Somdev Sangwan 4ce41b6b34
Update faceswap.py 2023-06-21 20:11:16 +05:30
Somdev Sangwan ccca35e43a
typo fix 2023-06-21 13:07:53 +05:30
Somdev Sangwan fc791abd29
API compatibility 2023-06-20 21:08:36 +05:30
Somdev Sangwan a9d53983b2
tips 2023-06-20 14:45:23 +05:30
Somdev Sangwan 450d91d6b6
clarity 2023-06-20 14:36:53 +05:30
Somdev Sangwan cc4d9dd585
maximize compatibility 2023-06-19 10:26:28 +05:30
5 changed files with 106 additions and 26 deletions

View File

@ -1,3 +1,11 @@
## This project has been archived
**When I originally built this project, I approached it primarily as a technical experiment in generative media. Not long after releasing it, my view of the broader second-order effects around software in this category changed.**
**Because of that, I chose to stop development and archive the repository.**
Update: This repository has been archived to prevent pull requests.
# roop for StableDiffusion
This is an extension for StableDiffusion's [AUTOMATIC1111 web-ui](https://github.com/AUTOMATIC1111/stable-diffusion-webui/) that allows face-replacement in images. It is based on [roop](https://github.com/s0md3v/roop) but will be developed seperately.
@ -8,18 +16,21 @@ This is an extension for StableDiffusion's [AUTOMATIC1111 web-ui](https://github
This software is meant to be a productive contribution to the rapidly growing AI-generated media industry. It will help artists with tasks such as animating a custom character or using the character as a model for clothing etc.
The developers of this software are aware of its possible unethical applicaitons and are committed to take preventative measures against them. It has a built-in check which prevents the program from working on inappropriate media including but not limited to nudity, graphic content, sensitive material such as war footage etc. We will continue to develop this project in the positive direction while adhering to law and ethics. This project may be shut down or include watermarks on the output if requested by law.
The developers of this software are aware of its possible unethical applicaitons and are committed to take preventative measures against them. It has a built-in check which prevents the program from working on inappropriate media. We will continue to develop this project in the positive direction while adhering to law and ethics. This project may be shut down or include watermarks on the output if requested by law.
Users of this software are expected to use this software responsibly while abiding the local law. If face of a real person is being used, users are suggested to get consent from the concerned person and clearly mention that it is a deepfake when posting content online. Developers of this software will not be responsible for actions of end-users.
## Installation
First of all, if you can't install it for some reason, don't open an issue here. Google your errors.
To install the extension, follow these steps:
> On Windows, download and install [Visual Studio](https://visualstudio.microsoft.com/downloads/). During the install, make sure to include the Python and C++ packages.
+ Run this command: `pip install insightface==0.7.3`
+ In web-ui, go to the "Extensions" tab and use this URL `https://github.com/s0md3v/sd-webui-roop` in the "install from URL" tab.
+ Restart the UI
+ Close webui and run it again
+ If you encounter `'NoneType' object has no attribute 'get'` error, download the [inswapper_128.onnx](https://huggingface.co/henryruhs/roop/resolve/main/inswapper_128.onnx) model and put it inside `<webui_dir>/models/roop/` directory.
On Windows, Microsoft Visual C++ 14.0 or greater must be installed before installing the extension. [During the install, make sure to include the Python and C++ packages.](https://github.com/s0md3v/roop/issues/153)
For rest of the errors, use google. Good luck.
## Usage
@ -27,19 +38,16 @@ On Windows, Microsoft Visual C++ 14.0 or greater must be installed before instal
2. Turn on the "Enable" checkbox
3. That's it, now the generated result will have the face you selected
### The result face is blurry
Use the "Restore Face" option. You can also try the "Upscaler" option or for more finer control, use an upscaler from the "Extras" tab.
## Tips
#### Getting good quality results
First of all, make sure the "Restore Face" option is enabled. You can also try the "Upscaler" option or for more finer control, use an upscaler from the "Extras" tab.
### There are multiple faces in result
Select the face numbers you wish to swap using the "Comma separated face number(s)" option.
For even better quality, use img2img with denoise set to `0.1` and gradually increase it until you get a balance of quality and resembelance.
### The face didn't get swapped
#### Replacing specific faces
If there are multiple faces in an image, select the face numbers you wish to swap using the "Comma separated face number(s)" option.
#### The face didn't get swapped?
Did you click "Enable"?
If you did and your console doesn't show any errors, it means roop detected that your image is either NSFW or wasn't able to detect a face at all.
### Img2Img
You can choose to activate the swap on the source image or on the generated image, or on both using the checkboxes. Activating on source image allows you to start from a given base and apply the diffusion process to it.
Inpainting should work but only the masked part will be swapped.

View File

@ -8,7 +8,7 @@ import urllib.request
req_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), "requirements.txt")
models_dir = os.path.abspath("models/roop")
model_url = "https://huggingface.co/henryruhs/roop/resolve/main/inswapper_128.onnx"
model_url = "https://github.com/dream80/roop_colab/releases/download/v0.0.1/inswapper_128.onnx"
model_name = os.path.basename(model_url)
model_path = os.path.join(models_dir, model_name)

68
scripts/api.py Normal file
View File

@ -0,0 +1,68 @@
'''
Author: SpenserCai
Date: 2023-08-20 17:28:26
version:
LastEditors: SpenserCai
LastEditTime: 2023-08-21 17:05:30
Description: file content
'''
from fastapi import FastAPI, Body
from modules.api.models import *
from modules import scripts, shared
from modules.api import api
from modules import paths_internal
import gradio as gr
from PIL import Image
from scripts.faceswap import get_models
from scripts.swapper import UpscaleOptions, swap_face, ImageResult
def get_face_restorer(str):
for restorer in shared.face_restorers:
if restorer.name() == str:
return restorer
return None
def get_full_model(model_name):
models = get_models()
for model in models:
if model.split("/")[-1] == model_name:
return model
return None
def roop_api(_: gr.Blocks, app: FastAPI):
@app.post("/roop/image")
async def roop_image(
source_image: str = Body("",title="source face image"),
target_image: str = Body("",title="target image"),
face_index: list[int] = Body([0],title="face index"),
scale: int = Body(1,title="scale"),
upscale_visibility: float = Body(1,title="upscale visibility"),
face_restorer: str = Body("None",title="face restorer"),
restorer_visibility: float = Body(1,title="face restorer"),
model: str = Body("inswapper_128.onnx",title="model"),
):
s_image = api.decode_base64_to_image(source_image)
t_image = api.decode_base64_to_image(target_image)
f_index = set(face_index)
up_options = UpscaleOptions(scale=scale, upscale_visibility=upscale_visibility,face_restorer=get_face_restorer(face_restorer),restorer_visibility=restorer_visibility)
use_model = get_full_model(model)
if use_model is None:
Exception("Model not found")
result = swap_face(s_image, t_image, use_model, f_index, up_options)
return {"image": api.encode_pil_to_base64(result.image())}
@app.get("/roop/models")
async def roop_models():
models = []
for model in get_models():
models.append(model.split("/")[-1])
return {"models": models}
try:
import modules.script_callbacks as script_callbacks
script_callbacks.on_app_started(roop_api)
except:
pass

View File

@ -1,3 +1,4 @@
import os
import gradio as gr
import modules.scripts as scripts
from modules.upscaler import Upscaler, UpscalerData
@ -18,12 +19,8 @@ import os
def get_models():
models_path = os.path.join(
scripts.basedir(), "extensions/sd-webui-roop/models/*"
)
models_path = os.path.join(scripts.basedir(), "models" + os.path.sep + "roop" + os.path.sep + "*")
models = glob.glob(models_path)
models_path = os.path.join(scripts.basedir(), "models/roop/*")
models += glob.glob(models_path)
models = [x for x in models if x.endswith(".onnx") or x.endswith(".pth")]
return models
@ -176,7 +173,7 @@ class FaceSwapScript(scripts.Script):
else:
logger.error(f"Please provide a source face")
def postprocess_batch(self, p, *args, **kwargs):
def postprocess_batch(self, *args, **kwargs):
if self.enable:
return images

View File

@ -17,7 +17,7 @@ from modules.face_restoration import FaceRestoration, restore_faces
from modules.upscaler import Upscaler, UpscalerData
from scripts.roop_logging import logger
providers = onnxruntime.get_available_providers()
providers = ["CPUExecutionProvider"]
@dataclass
@ -28,8 +28,6 @@ class UpscaleOptions:
face_restorer: FaceRestoration = None
restorer_visibility: float = 0.5
ANALYSIS_MODEL = insightface.app.FaceAnalysis(name="buffalo_l", providers=providers)
FS_MODEL = None
CURRENT_FS_MODEL_PATH = None
@ -75,7 +73,7 @@ def upscale_image(image: Image, upscale_options: UpscaleOptions):
def get_face_single(img_data: np.ndarray, face_index=0, det_size=(640, 640)):
face_analyser = copy.deepcopy(ANALYSIS_MODEL)
face_analyser = insightface.app.FaceAnalysis(name="buffalo_l", providers=providers)
face_analyser.prepare(ctx_id=0, det_size=det_size)
face = face_analyser.get(img_data)
@ -111,6 +109,15 @@ def swap_face(
converted = convert_to_sd(target_img)
scale, fn = converted[0], converted[1]
if model is not None and not scale:
if isinstance(source_img, str): # source_img is a base64 string
import base64, io
if 'base64,' in source_img: # check if the base64 string has a data URL scheme
base64_data = source_img.split('base64,')[-1]
img_bytes = base64.b64decode(base64_data)
else:
# if no data URL scheme, just decode
img_bytes = base64.b64decode(source_img)
source_img = Image.open(io.BytesIO(img_bytes))
source_img = cv2.cvtColor(np.array(source_img), cv2.COLOR_RGB2BGR)
target_img = cv2.cvtColor(np.array(target_img), cv2.COLOR_RGB2BGR)
source_face = get_face_single(source_img, face_index=0)