Spaces:
Running
on
Zero
Running
on
Zero
File size: 13,008 Bytes
e9b2bf3 50eec37 1c237e7 7680376 1972eaa 50eec37 e9b2bf3 1c237e7 7680376 e9b2bf3 50eec37 e9b2bf3 7680376 f7747cb 7680376 09df6e5 afe2486 09df6e5 afe2486 09df6e5 20dcbc9 09df6e5 e9b2bf3 1c237e7 e9b2bf3 3d636e1 e9b2bf3 4bb9b6b e9b2bf3 09df6e5 3d636e1 09df6e5 e9b2bf3 f7747cb e9b2bf3 50eec37 e1d5d34 c135610 e1d5d34 1972eaa e1d5d34 1972eaa e1d5d34 50eec37 1c237e7 e9b2bf3 50eec37 1972eaa 50eec37 1c237e7 2941d7e e9b2bf3 50eec37 e9b2bf3 3d636e1 e9b2bf3 50eec37 e9b2bf3 3d636e1 e9b2bf3 50eec37 e9b2bf3 2941d7e e9b2bf3 50eec37 1c237e7 6231739 1c237e7 e9b2bf3 9a48b83 1c237e7 e1d5d34 1972eaa 1c237e7 50eec37 1b66097 59cd0a4 50eec37 1b66097 7680376 276f9da 6bdcae3 7ade22f 50eec37 6bdcae3 a6d619e c186242 d540c9c 09b97b3 6bdcae3 7ade22f aec6dd4 9715c38 a6d619e aec6dd4 6bdcae3 d3ea6db 6bdcae3 09b97b3 c6c98b5 09b97b3 96a51f0 a92c609 1b66097 96a51f0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
import logging
import os
import sys
from typing import Any, Mapping, Sequence, Union
import gradio as gr
import numpy as np
import spaces
import time
import torch
import yaml
from huggingface_hub import hf_hub_download
from PIL import Image
import folder_paths
from nodes import NODE_CLASS_MAPPINGS
# Load available models from HF
hf_hub_download(
repo_id="Phips/2xNomosUni_span_multijpg_ldl",
filename="2xNomosUni_span_multijpg_ldl.safetensors",
local_dir="models/upscale_models",
)
hf_hub_download(
repo_id="ezioruan/inswapper_128.onnx",
filename="inswapper_128.onnx",
local_dir="models/insightface",
)
hf_hub_download(
repo_id="ziixzz/codeformer-v0.1.0.pth",
filename="codeformer-v0.1.0.pth",
local_dir="models/facerestore_models",
)
hf_hub_download(
repo_id="gmk123/GFPGAN",
filename="detection_Resnet50_Final.pth",
local_dir="models/facedetection",
)
hf_hub_download(
repo_id="gmk123/GFPGAN",
filename="parsing_parsenet.pth",
local_dir="models/facedetection",
)
hf_hub_download(
repo_id="vladmandic/insightface-faceanalysis",
filename="buffalo_l.zip",
local_dir="models/insightface/models",
)
hf_hub_download(
repo_id="model2/advance_face_model",
filename="advance_face_model.safetensors",
local_dir="models/reactor/faces",
)
# ReActor has its own special snowflake installation
os.system("cd custom_nodes/ComfyUI-ReActor && python install.py")
def import_custom_nodes() -> None:
"""Find all custom nodes in the custom_nodes folder and add those node objects to NODE_CLASS_MAPPINGS
This function sets up a new asyncio event loop, initializes the PromptServer,
creates a PromptQueue, and initializes the custom nodes.
"""
import asyncio
import execution
import server
from nodes import init_extra_nodes
# Creating a new event loop and setting it as the default loop
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
# Creating an instance of PromptServer with the loop
server_instance = server.PromptServer(loop)
execution.PromptQueue(server_instance)
# Initializing custom nodes
init_extra_nodes()
# Preload nodes, models.
import_custom_nodes()
loadimage = NODE_CLASS_MAPPINGS["LoadImage"]()
getimagesize = NODE_CLASS_MAPPINGS["GetImageSize+"]()
upscalemodelloader = NODE_CLASS_MAPPINGS["UpscaleModelLoader"]()
reactorloadfacemodel = NODE_CLASS_MAPPINGS["ReActorLoadFaceModel"]()
FACE_MODEL = reactorloadfacemodel.load_model(
face_model="advance_face_model.safetensors"
)
imageresize = NODE_CLASS_MAPPINGS["ImageResize+"]()
reactorfaceswap = NODE_CLASS_MAPPINGS["ReActorFaceSwap"]()
imageupscalewithmodel = NODE_CLASS_MAPPINGS["ImageUpscaleWithModel"]()
UPSCALE_MODEL = upscalemodelloader.load_model(model_name="2xNomosUni_span_multijpg_ldl.safetensors")
def load_extra_path_config(yaml_path):
with open(yaml_path, "r", encoding="utf-8") as stream:
config = yaml.safe_load(stream)
yaml_dir = os.path.dirname(os.path.abspath(yaml_path))
for c in config:
conf = config[c]
if conf is None:
continue
base_path = None
if "base_path" in conf:
base_path = conf.pop("base_path")
base_path = os.path.expandvars(os.path.expanduser(base_path))
if not os.path.isabs(base_path):
base_path = os.path.abspath(os.path.join(yaml_dir, base_path))
is_default = False
if "is_default" in conf:
is_default = conf.pop("is_default")
for x in conf:
for y in conf[x].split("\n"):
if len(y) == 0:
continue
full_path = y
if base_path:
full_path = os.path.join(base_path, full_path)
elif not os.path.isabs(full_path):
full_path = os.path.abspath(os.path.join(yaml_dir, y))
normalized_path = os.path.normpath(full_path)
logging.info(
"Adding extra search path {} {}".format(x, normalized_path)
)
folder_paths.add_model_folder_path(x, normalized_path, is_default)
def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any:
"""Returns the value at the given index of a sequence or mapping.
If the object is a sequence (like list or string), returns the value at the given index.
If the object is a mapping (like a dictionary), returns the value at the index-th key.
Some return a dictionary, in these cases, we look for the "results" key
Args:
obj (Union[Sequence, Mapping]): The object to retrieve the value from.
index (int): The index of the value to retrieve.
Returns:
Any: The value at the given index.
"""
try:
return obj[index]
except KeyError:
return obj["result"][index]
def find_path(name: str, path: str = None) -> str:
"""
Recursively looks at parent folders starting from the given path until it finds the given name.
Returns the path as a Path object if found, or None otherwise.
"""
# If no path is given, use the current working directory
if path is None:
path = os.getcwd()
# Check if the current directory contains the name
if name in os.listdir(path):
path_name = os.path.join(path, name)
print(f"{name} found: {path_name}")
return path_name
# Get the parent directory
parent_directory = os.path.dirname(path)
# If the parent directory is the same as the current directory, we've reached the root and stop the search
if parent_directory == path:
return None
# Recursively call the function with the parent directory
return find_path(name, parent_directory)
def add_comfyui_directory_to_sys_path() -> None:
"""
Add 'ComfyUI' to the sys.path
"""
comfyui_path = find_path("ComfyUI")
if comfyui_path is not None and os.path.isdir(comfyui_path):
sys.path.append(comfyui_path)
print(f"'{comfyui_path}' added to sys.path")
def add_extra_model_paths() -> None:
"""
Parse the optional extra_model_paths.yaml file and add the parsed paths to the sys.path.
"""
extra_model_paths = find_path("extra_model_paths.yaml")
if extra_model_paths is not None:
load_extra_path_config(extra_model_paths)
else:
print("Could not find the extra_model_paths config file.")
def clear_tmp_directory():
print("Cleaning up /tmp directory...")
image_extensions = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp'}
for root, dirs, files in os.walk('/tmp'):
for file in files:
if not file.startswith('advance-blurred-') and not file.startswith('before.jpg') and not file.startswith('after.jpg'):
ext = os.path.splitext(file)[1].lower()
if ext in image_extensions:
full_path = os.path.join(root, file)
try:
os.remove(full_path)
print("Deleted an image.")
except Exception as e:
print("Failed to delete an image.")
add_comfyui_directory_to_sys_path()
add_extra_model_paths()
@spaces.GPU(duration=60)
def advance_blur(input_image):
start_time = time.time()
with torch.inference_mode():
loaded_input_image = loadimage.load_image(
image=input_image,
)
image_size = getimagesize.execute(
image=get_value_at_index(loaded_input_image, 0),
)
original_width = get_value_at_index(image_size, 0)
original_height = get_value_at_index(image_size, 1)
resized_input_image = imageresize.execute(
width=2560,
height=2560,
interpolation="bicubic",
method="keep proportion",
condition="downscale if bigger",
multiple_of=0,
image=get_value_at_index(loaded_input_image, 0),
)
swapped_image = reactorfaceswap.execute(
enabled=True,
swap_model="inswapper_128.onnx",
facedetection="retinaface_resnet50",
face_restore_model="codeformer-v0.1.0.pth",
face_restore_visibility=1,
codeformer_weight=1,
detect_gender_input="no",
detect_gender_source="no",
input_faces_index="0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99",
source_faces_index="0",
console_log_level=2,
input_image=get_value_at_index(resized_input_image, 0),
face_model=get_value_at_index(FACE_MODEL, 0),
)
upscaled_image = imageupscalewithmodel.upscale(
upscale_model=get_value_at_index(UPSCALE_MODEL, 0),
image=get_value_at_index(swapped_image, 0),
)
final_image = imageresize.execute(
width=original_width,
height=original_height,
interpolation="lanczos",
method="keep proportion",
condition="downscale if bigger",
multiple_of=0,
image=get_value_at_index(upscaled_image, 0),
)
img = Image.fromarray(
np.clip(
(255.0 * get_value_at_index(final_image, 0)[0].cpu().numpy()), 0, 255
).astype(np.uint8)
)
outpath = f"advance-blurred-{os.urandom(16).hex()}.jpg"
img.save(outpath, quality=80, dpi=(72, 72))
# Clean up the /tmp directory
clear_tmp_directory()
# Log the time taken for the process
end_time = time.time()
elapsed_time = end_time - start_time
print(f"Processing time: {elapsed_time:.2f} seconds")
return outpath
if __name__ == "__main__":
# Updated, more flexible CSS
css_code = ""
with gr.Blocks(css=css_code, theme=gr.themes.Base()) as app:
gr.Markdown("# 🥸 Advance Blur")
with gr.Accordion("More info", open=False):
gr.Markdown(
"""
**Advance Blur** is an anonymization tool that leverages a sophisticated
technique known as "Vance Blurring" to enhance privacy for your images.
**Features:**
- **Blur Faces**: Automatically detects and replaces faces with the image of the ideal American male.
- **Enhance Privacy:** Removes sensitive information from images (GPS, EXIF, etc.)
- **Safe and secure:** No data is stored long-term or shared with others. System is fully reset on a regular basis.
**Disclaimer:**
Advance Blur is intended for entertainment purposes only. Any resemblance
to actual persons is entirely coincidental, karmic, and comedic (as a decent parody
should).
Advance Blur only seeks to perfect images using the depiction of the ideal American male.
**Instructions:**
1. Upload your image.
2. Click the "Submit" button to apply "Vance Blurring" to your image.
3. Download the blurred image by long-clicking on it to Copy, or tap down-arrow to Save.
4. Share the image with your friends and family, feeling confident in your privacy!
**Tips:**
- For best results, use high-quality images at medium-ranges.
- Works best when faces are front-facing and well-lit.
- Always check the final image before sharing.
"""
)
with gr.Row(max_height=500):
gr.Image(
value="before.jpg",
label="Before",
show_label=True,
interactive=False,
)
gr.Image(
value="after.jpg",
label="After",
show_label=True,
interactive=False,
)
with gr.Row():
with gr.Column():
input_image = gr.Image(
type="filepath",
label="Upload Your Image",
elem_id="fixed-image-size",
show_label=True,
)
submit_btn = gr.Button("Submit", variant="primary")
with gr.Column():
output_image = gr.Image(
label="\"Vance Blurred\" Result",
elem_id="fixed-image-size",
show_label=True,
)
# Trigger your blur function
submit_btn.click(fn=advance_blur, inputs=[input_image], outputs=[output_image])
# Launch the app
app.launch(share=True)
|