Remove aDetailer processing
Browse files- app.py +1 -119
- requirements.txt +0 -1
app.py
CHANGED
|
@@ -9,7 +9,6 @@ import time
|
|
| 9 |
import urllib.parse
|
| 10 |
import urllib.request
|
| 11 |
import uuid
|
| 12 |
-
from functools import lru_cache
|
| 13 |
import warnings
|
| 14 |
from asyncio.base_events import BaseEventLoop
|
| 15 |
from concurrent.futures import ThreadPoolExecutor
|
|
@@ -45,7 +44,7 @@ from fastapi.responses import FileResponse, Response
|
|
| 45 |
from gradio_client import Client
|
| 46 |
from gradio.routes import App
|
| 47 |
from huggingface_hub import download_bucket_files, list_bucket_tree
|
| 48 |
-
from PIL import Image, ImageDraw,
|
| 49 |
from PIL.PngImagePlugin import PngInfo
|
| 50 |
from pydantic import BaseModel, ConfigDict, Field
|
| 51 |
from starlette.background import BackgroundTask
|
|
@@ -89,19 +88,6 @@ MODEL_KINDS = ("checkpoints", "vae", "loras")
|
|
| 89 |
MODEL_SUFFIXES = (
|
| 90 |
".bin", ".ckpt", ".pkl", ".pt", ".pt2", ".pth", ".safetensors", ".sft",
|
| 91 |
)
|
| 92 |
-
AD_PASSES = (
|
| 93 |
-
(
|
| 94 |
-
"Eyes.pt",
|
| 95 |
-
"masterpiece, best quality, eyes, detailed eyes",
|
| 96 |
-
),
|
| 97 |
-
)
|
| 98 |
-
AD_MODELS = tuple(name for name, _ in AD_PASSES)
|
| 99 |
-
AD_CONF = 0.3
|
| 100 |
-
AD_PADDING = 32
|
| 101 |
-
AD_BLUR = 4
|
| 102 |
-
AD_DENOISE = 0.3
|
| 103 |
-
AD_STEPS = 22
|
| 104 |
-
AD_CFG = 5
|
| 105 |
ASSETS = {
|
| 106 |
"checkpoint": (
|
| 107 |
"52_novaAnimeXL_ilV190.safetensors",
|
|
@@ -377,10 +363,6 @@ def init_comfy():
|
|
| 377 |
folder_paths.add_model_folder_path(kind, str(DATA_DIR / kind))
|
| 378 |
folder_paths.add_model_folder_path(kind, str(local), is_default=True)
|
| 379 |
|
| 380 |
-
ad_dir = DATA_DIR / "ultralytics"
|
| 381 |
-
ad_dir.mkdir(parents=True, exist_ok=True)
|
| 382 |
-
folder_paths.add_model_folder_path("ultralytics", str(ad_dir))
|
| 383 |
-
|
| 384 |
state.update(
|
| 385 |
apply_lora=comfy.sd.load_lora_for_models,
|
| 386 |
chains={},
|
|
@@ -521,20 +503,6 @@ def generate_core(body):
|
|
| 521 |
samples=samples,
|
| 522 |
)[0]
|
| 523 |
|
| 524 |
-
sampler = request.second_sampler if request.upscale else request.sampler
|
| 525 |
-
scheduler = request.second_scheduler if request.upscale else request.scheduler
|
| 526 |
-
model_name = (
|
| 527 |
-
request.second_model
|
| 528 |
-
if request.upscale and request.second_model
|
| 529 |
-
else request.model
|
| 530 |
-
)
|
| 531 |
-
model, clip = load_model(model_name)
|
| 532 |
-
negative = state["encode"].encode(clip=clip, text=request.negative)[0]
|
| 533 |
-
image = run_adetailer(
|
| 534 |
-
image, model, clip, negative, sampler, scheduler,
|
| 535 |
-
secrets.randbits(64),
|
| 536 |
-
)
|
| 537 |
-
|
| 538 |
return image[0], first_seed, second_seed
|
| 539 |
|
| 540 |
|
|
@@ -653,74 +621,6 @@ def stage_model(kind, name):
|
|
| 653 |
return target
|
| 654 |
|
| 655 |
|
| 656 |
-
@lru_cache(maxsize=len(AD_MODELS))
|
| 657 |
-
def load_ad_model(name):
|
| 658 |
-
from ultralytics import YOLO
|
| 659 |
-
path = state["folders"].get_full_path("ultralytics", name)
|
| 660 |
-
if path is None:
|
| 661 |
-
raise ValueError(f"aDetailer model not found: {name}")
|
| 662 |
-
log(f"Loading aDetailer model {name}")
|
| 663 |
-
return YOLO(path)
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
def ad_detect(image_np, name):
|
| 667 |
-
h, w = image_np.shape[:2]
|
| 668 |
-
pil = Image.fromarray(np.clip(image_np * 255, 0, 255).astype(np.uint8))
|
| 669 |
-
masks = []
|
| 670 |
-
yolo = load_ad_model(name)
|
| 671 |
-
results = yolo(pil, conf=AD_CONF, verbose=False)
|
| 672 |
-
for result in results:
|
| 673 |
-
if result.boxes is None:
|
| 674 |
-
continue
|
| 675 |
-
for box in result.boxes.xyxy.cpu().numpy():
|
| 676 |
-
x1 = max(0, int(box[0]) - AD_PADDING)
|
| 677 |
-
y1 = max(0, int(box[1]) - AD_PADDING)
|
| 678 |
-
x2 = min(w, int(box[2]) + AD_PADDING)
|
| 679 |
-
y2 = min(h, int(box[3]) + AD_PADDING)
|
| 680 |
-
mask = Image.new("L", (w, h), 0)
|
| 681 |
-
ImageDraw.Draw(mask).rectangle([x1, y1, x2, y2], fill=255)
|
| 682 |
-
mask = mask.filter(ImageFilter.GaussianBlur(AD_BLUR))
|
| 683 |
-
mask_np = np.array(mask).astype(np.float32) / 255.0
|
| 684 |
-
masks.append(torch.from_numpy(mask_np))
|
| 685 |
-
return masks
|
| 686 |
-
|
| 687 |
-
|
| 688 |
-
def run_adetailer(image, model, clip, negative, sampler, scheduler, seed):
|
| 689 |
-
for name, prompt in AD_PASSES:
|
| 690 |
-
image_np = image[0].detach().numpy()
|
| 691 |
-
masks = ad_detect(image_np, name)
|
| 692 |
-
if not masks:
|
| 693 |
-
continue
|
| 694 |
-
positive = state["encode"].encode(clip=clip, text=prompt)[0]
|
| 695 |
-
|
| 696 |
-
mask = torch.stack(masks).amax(dim=0).unsqueeze(0)
|
| 697 |
-
latent = state["vae_encode"].encode(
|
| 698 |
-
vae=state["vae"],
|
| 699 |
-
pixels=image,
|
| 700 |
-
)[0]
|
| 701 |
-
latent["noise_mask"] = mask
|
| 702 |
-
samples = run_sampler(
|
| 703 |
-
model,
|
| 704 |
-
seed,
|
| 705 |
-
AD_STEPS,
|
| 706 |
-
AD_CFG,
|
| 707 |
-
sampler,
|
| 708 |
-
scheduler,
|
| 709 |
-
positive,
|
| 710 |
-
negative,
|
| 711 |
-
latent,
|
| 712 |
-
AD_DENOISE,
|
| 713 |
-
)
|
| 714 |
-
detailed = state["decode"].decode(
|
| 715 |
-
vae=state["vae"],
|
| 716 |
-
samples=samples,
|
| 717 |
-
)[0]
|
| 718 |
-
blend = mask.unsqueeze(-1).to(image)
|
| 719 |
-
image = image * (1 - blend) + detailed * blend
|
| 720 |
-
|
| 721 |
-
return image
|
| 722 |
-
|
| 723 |
-
|
| 724 |
def stored_image_tensor(path):
|
| 725 |
with Image.open(BytesIO(stored_bytes(path))) as image:
|
| 726 |
pixels = np.asarray(image.convert("RGB"), dtype=np.float32) / 255
|
|
@@ -797,15 +697,6 @@ def generate_comparison_cell(request, output):
|
|
| 797 |
vae=state["vae"],
|
| 798 |
samples=samples,
|
| 799 |
)[0]
|
| 800 |
-
image = run_adetailer(
|
| 801 |
-
image,
|
| 802 |
-
second_model,
|
| 803 |
-
second_clip,
|
| 804 |
-
negative,
|
| 805 |
-
request.sampler,
|
| 806 |
-
request.scheduler,
|
| 807 |
-
MATRIX_SEED,
|
| 808 |
-
)
|
| 809 |
save_named_image(tensor_image(image), output)
|
| 810 |
return request.output
|
| 811 |
|
|
@@ -902,15 +793,6 @@ def generate_matrix_cell_core(body):
|
|
| 902 |
vae=state["vae"],
|
| 903 |
samples=samples,
|
| 904 |
)[0]
|
| 905 |
-
image = run_adetailer(
|
| 906 |
-
image,
|
| 907 |
-
model,
|
| 908 |
-
clip,
|
| 909 |
-
negative,
|
| 910 |
-
MATRIX_SAMPLER,
|
| 911 |
-
MATRIX_SCHEDULER,
|
| 912 |
-
MATRIX_SEED,
|
| 913 |
-
)
|
| 914 |
save_named_image(tensor_image(image), output)
|
| 915 |
return f"{first_id}-{second_id}"
|
| 916 |
|
|
|
|
| 9 |
import urllib.parse
|
| 10 |
import urllib.request
|
| 11 |
import uuid
|
|
|
|
| 12 |
import warnings
|
| 13 |
from asyncio.base_events import BaseEventLoop
|
| 14 |
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
| 44 |
from gradio_client import Client
|
| 45 |
from gradio.routes import App
|
| 46 |
from huggingface_hub import download_bucket_files, list_bucket_tree
|
| 47 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 48 |
from PIL.PngImagePlugin import PngInfo
|
| 49 |
from pydantic import BaseModel, ConfigDict, Field
|
| 50 |
from starlette.background import BackgroundTask
|
|
|
|
| 88 |
MODEL_SUFFIXES = (
|
| 89 |
".bin", ".ckpt", ".pkl", ".pt", ".pt2", ".pth", ".safetensors", ".sft",
|
| 90 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
ASSETS = {
|
| 92 |
"checkpoint": (
|
| 93 |
"52_novaAnimeXL_ilV190.safetensors",
|
|
|
|
| 363 |
folder_paths.add_model_folder_path(kind, str(DATA_DIR / kind))
|
| 364 |
folder_paths.add_model_folder_path(kind, str(local), is_default=True)
|
| 365 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 366 |
state.update(
|
| 367 |
apply_lora=comfy.sd.load_lora_for_models,
|
| 368 |
chains={},
|
|
|
|
| 503 |
samples=samples,
|
| 504 |
)[0]
|
| 505 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 506 |
return image[0], first_seed, second_seed
|
| 507 |
|
| 508 |
|
|
|
|
| 621 |
return target
|
| 622 |
|
| 623 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 624 |
def stored_image_tensor(path):
|
| 625 |
with Image.open(BytesIO(stored_bytes(path))) as image:
|
| 626 |
pixels = np.asarray(image.convert("RGB"), dtype=np.float32) / 255
|
|
|
|
| 697 |
vae=state["vae"],
|
| 698 |
samples=samples,
|
| 699 |
)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 700 |
save_named_image(tensor_image(image), output)
|
| 701 |
return request.output
|
| 702 |
|
|
|
|
| 793 |
vae=state["vae"],
|
| 794 |
samples=samples,
|
| 795 |
)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 796 |
save_named_image(tensor_image(image), output)
|
| 797 |
return f"{first_id}-{second_id}"
|
| 798 |
|
requirements.txt
CHANGED
|
@@ -5,6 +5,5 @@ huggingface_hub>=1.6
|
|
| 5 |
numpy
|
| 6 |
Pillow
|
| 7 |
py7zr==1.1.2
|
| 8 |
-
ultralytics
|
| 9 |
uvicorn
|
| 10 |
-r https://raw.githubusercontent.com/Comfy-Org/ComfyUI/master/requirements.txt
|
|
|
|
| 5 |
numpy
|
| 6 |
Pillow
|
| 7 |
py7zr==1.1.2
|
|
|
|
| 8 |
uvicorn
|
| 9 |
-r https://raw.githubusercontent.com/Comfy-Org/ComfyUI/master/requirements.txt
|