Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,138 +1,235 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from src.tryon_pipeline import StableDiffusionXLInpaintPipeline as TryonPipeline
|
| 5 |
from src.unet_hacked_garmnet import UNet2DConditionModel as UNet2DConditionModel_ref
|
| 6 |
from src.unet_hacked_tryon import UNet2DConditionModel
|
|
|
|
| 7 |
from transformers import (
|
| 8 |
CLIPImageProcessor,
|
| 9 |
CLIPVisionModelWithProjection,
|
| 10 |
CLIPTextModel,
|
| 11 |
CLIPTextModelWithProjection,
|
|
|
|
| 12 |
)
|
| 13 |
-
from diffusers import DDPMScheduler,AutoencoderKL
|
| 14 |
-
from typing import List
|
| 15 |
|
| 16 |
-
import
|
| 17 |
-
|
| 18 |
-
from transformers import AutoTokenizer
|
| 19 |
-
import numpy as np
|
| 20 |
-
from utils_mask import get_mask_location
|
| 21 |
-
from torchvision import transforms
|
| 22 |
import apply_net
|
|
|
|
| 23 |
from preprocess.humanparsing.run_parsing import Parsing
|
| 24 |
from preprocess.openpose.run_openpose import OpenPose
|
| 25 |
-
from detectron2.data.detection_utils import convert_PIL_to_numpy,_apply_exif_orientation
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
def pil_to_binary_mask(pil_image, threshold=0):
|
| 30 |
np_image = np.array(pil_image)
|
| 31 |
grayscale_image = Image.fromarray(np_image).convert("L")
|
| 32 |
binary_mask = np.array(grayscale_image) > threshold
|
| 33 |
-
mask =
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
base_path =
|
| 44 |
-
|
| 45 |
|
| 46 |
-
unet = UNet2DConditionModel.from_pretrained(
|
| 47 |
-
base_path,
|
| 48 |
-
subfolder="unet",
|
| 49 |
-
torch_dtype=torch.float16,
|
| 50 |
-
)
|
| 51 |
-
unet.requires_grad_(False)
|
| 52 |
-
tokenizer_one = AutoTokenizer.from_pretrained(
|
| 53 |
-
base_path,
|
| 54 |
-
subfolder="tokenizer",
|
| 55 |
-
revision=None,
|
| 56 |
-
use_fast=False,
|
| 57 |
-
)
|
| 58 |
-
tokenizer_two = AutoTokenizer.from_pretrained(
|
| 59 |
-
base_path,
|
| 60 |
-
subfolder="tokenizer_2",
|
| 61 |
-
revision=None,
|
| 62 |
-
use_fast=False,
|
| 63 |
-
)
|
| 64 |
noise_scheduler = DDPMScheduler.from_pretrained(base_path, subfolder="scheduler")
|
| 65 |
|
| 66 |
-
text_encoder_one = CLIPTextModel.from_pretrained(
|
| 67 |
-
|
| 68 |
-
subfolder="text_encoder",
|
| 69 |
-
torch_dtype=torch.float16,
|
| 70 |
-
)
|
| 71 |
-
text_encoder_two = CLIPTextModelWithProjection.from_pretrained(
|
| 72 |
-
base_path,
|
| 73 |
-
subfolder="text_encoder_2",
|
| 74 |
-
torch_dtype=torch.float16,
|
| 75 |
-
)
|
| 76 |
-
image_encoder = CLIPVisionModelWithProjection.from_pretrained(
|
| 77 |
-
base_path,
|
| 78 |
-
subfolder="image_encoder",
|
| 79 |
-
torch_dtype=torch.float16,
|
| 80 |
-
)
|
| 81 |
-
vae = AutoencoderKL.from_pretrained(base_path,
|
| 82 |
-
subfolder="vae",
|
| 83 |
-
torch_dtype=torch.float16,
|
| 84 |
-
)
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
subfolder="unet_encoder",
|
| 90 |
-
torch_dtype=torch.float16,
|
| 91 |
-
)
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
parsing_model = Parsing(0)
|
| 94 |
openpose_model = OpenPose(0)
|
| 95 |
|
| 96 |
-
|
| 97 |
-
image_encoder
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
text_encoder_one.requires_grad_(False)
|
| 101 |
-
text_encoder_two.requires_grad_(False)
|
| 102 |
tensor_transfrom = transforms.Compose(
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
transforms.Normalize([0.5], [0.5]),
|
| 106 |
-
]
|
| 107 |
-
)
|
| 108 |
|
| 109 |
pipe = TryonPipeline.from_pretrained(
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
)
|
| 122 |
pipe.unet_encoder = UNet_Encoder
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
pipe.to(device)
|
| 130 |
pipe.unet_encoder.to(device)
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
| 136 |
width, height = human_img_orig.size
|
| 137 |
target_width = int(min(width, height * (3 / 4)))
|
| 138 |
target_height = int(min(height, width * (4 / 3)))
|
|
@@ -142,172 +239,224 @@ def start_tryon(dict,garm_img,garment_des,is_checked,is_checked_crop,denoise_ste
|
|
| 142 |
bottom = (height + target_height) / 2
|
| 143 |
cropped_img = human_img_orig.crop((left, top, right, bottom))
|
| 144 |
crop_size = cropped_img.size
|
| 145 |
-
human_img = cropped_img.resize((768,1024))
|
| 146 |
else:
|
| 147 |
-
human_img = human_img_orig.resize((768,1024))
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
if
|
| 151 |
-
keypoints = openpose_model(human_img.resize((384,512)))
|
| 152 |
-
model_parse, _ = parsing_model(human_img.resize((384,512)))
|
| 153 |
-
mask,
|
| 154 |
-
mask = mask.resize((768,1024))
|
| 155 |
else:
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
# mask = mask.unsqueeze(0)
|
| 159 |
-
mask_gray = (1-transforms.ToTensor()(mask)) * tensor_transfrom(human_img)
|
| 160 |
-
mask_gray = to_pil_image((mask_gray+1.0)/2.0)
|
| 161 |
|
|
|
|
|
|
|
| 162 |
|
| 163 |
-
|
|
|
|
| 164 |
human_img_arg = convert_PIL_to_numpy(human_img_arg, format="BGR")
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
args = apply_net.create_argument_parser().parse_args(('show', './configs/densepose_rcnn_R_50_FPN_s1x.yaml', './ckpt/densepose/model_final_162be9.pkl', 'dp_segm', '-v', '--opts', 'MODEL.DEVICE', 'cuda'))
|
| 169 |
-
# verbosity = getattr(args, "verbosity", None)
|
| 170 |
-
pose_img = args.func(args,human_img_arg)
|
| 171 |
-
pose_img = pose_img[:,:,::-1]
|
| 172 |
-
pose_img = Image.fromarray(pose_img).resize((768,1024))
|
| 173 |
-
|
| 174 |
-
with torch.no_grad():
|
| 175 |
-
# Extract the images
|
| 176 |
-
with torch.cuda.amp.autocast():
|
| 177 |
-
with torch.no_grad():
|
| 178 |
-
prompt = "model is wearing " + garment_des
|
| 179 |
-
negative_prompt = "monochrome, lowres, bad anatomy, worst quality, low quality"
|
| 180 |
-
with torch.inference_mode():
|
| 181 |
-
(
|
| 182 |
-
prompt_embeds,
|
| 183 |
-
negative_prompt_embeds,
|
| 184 |
-
pooled_prompt_embeds,
|
| 185 |
-
negative_pooled_prompt_embeds,
|
| 186 |
-
) = pipe.encode_prompt(
|
| 187 |
-
prompt,
|
| 188 |
-
num_images_per_prompt=1,
|
| 189 |
-
do_classifier_free_guidance=True,
|
| 190 |
-
negative_prompt=negative_prompt,
|
| 191 |
-
)
|
| 192 |
-
|
| 193 |
-
prompt = "a photo of " + garment_des
|
| 194 |
-
negative_prompt = "monochrome, lowres, bad anatomy, worst quality, low quality"
|
| 195 |
-
if not isinstance(prompt, List):
|
| 196 |
-
prompt = [prompt] * 1
|
| 197 |
-
if not isinstance(negative_prompt, List):
|
| 198 |
-
negative_prompt = [negative_prompt] * 1
|
| 199 |
-
with torch.inference_mode():
|
| 200 |
-
(
|
| 201 |
-
prompt_embeds_c,
|
| 202 |
-
_,
|
| 203 |
-
_,
|
| 204 |
-
_,
|
| 205 |
-
) = pipe.encode_prompt(
|
| 206 |
-
prompt,
|
| 207 |
-
num_images_per_prompt=1,
|
| 208 |
-
do_classifier_free_guidance=False,
|
| 209 |
-
negative_prompt=negative_prompt,
|
| 210 |
-
)
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
pose_img = tensor_transfrom(pose_img).unsqueeze(0).to(device,torch.float16)
|
| 215 |
-
garm_tensor = tensor_transfrom(garm_img).unsqueeze(0).to(device,torch.float16)
|
| 216 |
-
generator = torch.Generator(device).manual_seed(seed) if seed is not None else None
|
| 217 |
-
images = pipe(
|
| 218 |
-
prompt_embeds=prompt_embeds.to(device,torch.float16),
|
| 219 |
-
negative_prompt_embeds=negative_prompt_embeds.to(device,torch.float16),
|
| 220 |
-
pooled_prompt_embeds=pooled_prompt_embeds.to(device,torch.float16),
|
| 221 |
-
negative_pooled_prompt_embeds=negative_pooled_prompt_embeds.to(device,torch.float16),
|
| 222 |
-
num_inference_steps=denoise_steps,
|
| 223 |
-
generator=generator,
|
| 224 |
-
strength = 1.0,
|
| 225 |
-
pose_img = pose_img.to(device,torch.float16),
|
| 226 |
-
text_embeds_cloth=prompt_embeds_c.to(device,torch.float16),
|
| 227 |
-
cloth = garm_tensor.to(device,torch.float16),
|
| 228 |
-
mask_image=mask,
|
| 229 |
-
image=human_img,
|
| 230 |
-
height=1024,
|
| 231 |
-
width=768,
|
| 232 |
-
ip_adapter_image = garm_img.resize((768,1024)),
|
| 233 |
-
guidance_scale=2.0,
|
| 234 |
-
)[0]
|
| 235 |
-
|
| 236 |
-
if is_checked_crop:
|
| 237 |
-
out_img = images[0].resize(crop_size)
|
| 238 |
-
human_img_orig.paste(out_img, (int(left), int(top)))
|
| 239 |
-
return human_img_orig, mask_gray
|
| 240 |
-
else:
|
| 241 |
-
return images[0], mask_gray
|
| 242 |
-
# return images[0], mask_gray
|
| 243 |
|
| 244 |
-
|
| 245 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
|
| 247 |
-
|
| 248 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
-
##default human
|
| 259 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
gr.Markdown("## IDM-VTON 👕👔👚")
|
| 264 |
-
gr.Markdown("Virtual Try-on with your image and garment image. Check out the [source codes](https://github.com/yisol/IDM-VTON) and the [model](https://huggingface.co/yisol/IDM-VTON)")
|
| 265 |
-
with gr.Row():
|
| 266 |
-
with gr.Column():
|
| 267 |
-
imgs = gr.ImageEditor(sources='upload', type="pil", label='Human. Mask with pen or use auto-masking', interactive=True)
|
| 268 |
-
with gr.Row():
|
| 269 |
-
is_checked = gr.Checkbox(label="Yes", info="Use auto-generated mask (Takes 5 seconds)",value=True)
|
| 270 |
-
with gr.Row():
|
| 271 |
-
is_checked_crop = gr.Checkbox(label="Yes", info="Use auto-crop & resizing",value=False)
|
| 272 |
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
examples_per_page=10,
|
| 276 |
-
examples=human_ex_list
|
| 277 |
-
)
|
| 278 |
|
|
|
|
| 279 |
with gr.Column():
|
| 280 |
-
|
| 281 |
-
with gr.Row(elem_id="prompt-container"):
|
| 282 |
-
with gr.Row():
|
| 283 |
-
prompt = gr.Textbox(placeholder="Description of garment ex) Short Sleeve Round Neck T-shirts", show_label=False, elem_id="prompt")
|
| 284 |
-
example = gr.Examples(
|
| 285 |
-
inputs=garm_img,
|
| 286 |
-
examples_per_page=8,
|
| 287 |
-
examples=garm_list_path)
|
| 288 |
-
with gr.Column():
|
| 289 |
-
# image_out = gr.Image(label="Output", elem_id="output-img", height=400)
|
| 290 |
-
masked_img = gr.Image(label="Masked image output", elem_id="masked-img",show_share_button=False)
|
| 291 |
-
with gr.Column():
|
| 292 |
-
# image_out = gr.Image(label="Output", elem_id="output-img", height=400)
|
| 293 |
-
image_out = gr.Image(label="Output", elem_id="output-img",show_share_button=False)
|
| 294 |
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
with gr.Column():
|
| 299 |
-
try_button = gr.Button(value="Try-on")
|
| 300 |
-
with gr.Accordion(label="Advanced Settings", open=False):
|
| 301 |
with gr.Row():
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
|
|
|
|
|
|
|
| 306 |
|
| 307 |
-
|
|
|
|
|
|
|
| 308 |
|
| 309 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
|
| 312 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
import os
|
| 3 |
+
import time
|
| 4 |
+
import tempfile
|
| 5 |
+
from typing import List, Optional, Tuple
|
| 6 |
+
|
| 7 |
import spaces
|
| 8 |
import gradio as gr
|
| 9 |
from PIL import Image
|
| 10 |
+
|
| 11 |
+
import torch
|
| 12 |
+
import numpy as np
|
| 13 |
+
from torchvision import transforms
|
| 14 |
+
from torchvision.transforms.functional import to_pil_image
|
| 15 |
+
|
| 16 |
+
from huggingface_hub import login, snapshot_download
|
| 17 |
+
|
| 18 |
from src.tryon_pipeline import StableDiffusionXLInpaintPipeline as TryonPipeline
|
| 19 |
from src.unet_hacked_garmnet import UNet2DConditionModel as UNet2DConditionModel_ref
|
| 20 |
from src.unet_hacked_tryon import UNet2DConditionModel
|
| 21 |
+
|
| 22 |
from transformers import (
|
| 23 |
CLIPImageProcessor,
|
| 24 |
CLIPVisionModelWithProjection,
|
| 25 |
CLIPTextModel,
|
| 26 |
CLIPTextModelWithProjection,
|
| 27 |
+
AutoTokenizer,
|
| 28 |
)
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
from diffusers import DDPMScheduler, AutoencoderKL
|
| 31 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
import apply_net
|
| 33 |
+
from utils_mask import get_mask_location
|
| 34 |
from preprocess.humanparsing.run_parsing import Parsing
|
| 35 |
from preprocess.openpose.run_openpose import OpenPose
|
| 36 |
+
from detectron2.data.detection_utils import convert_PIL_to_numpy, _apply_exif_orientation
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# =========================
|
| 40 |
+
# Auth (optional)
|
| 41 |
+
# =========================
|
| 42 |
+
DEMO_USER = os.getenv("DEMO_USER", "").strip()
|
| 43 |
+
DEMO_PASS = os.getenv("DEMO_PASS", "").strip()
|
| 44 |
+
APP_AUTH = (DEMO_USER, DEMO_PASS) if (DEMO_USER and DEMO_PASS) else None
|
| 45 |
+
|
| 46 |
+
# =========================
|
| 47 |
+
# Garments dataset autoload
|
| 48 |
+
# =========================
|
| 49 |
+
GARMENT_DIR = "garments"
|
| 50 |
+
ALLOWED_EXTS = (".png", ".jpg", ".jpeg", ".webp")
|
| 51 |
+
GARMENTS_DATASET = os.getenv("GARMENTS_DATASET", "").strip() # e.g. "ArmanRV/armanrv-garments"
|
| 52 |
+
HF_TOKEN = os.getenv("HF_TOKEN", "").strip()
|
| 53 |
+
|
| 54 |
+
def ensure_garments_downloaded() -> None:
|
| 55 |
+
"""
|
| 56 |
+
Downloads garments from HF Dataset into ./garments to avoid Space repo 1GB limit.
|
| 57 |
+
"""
|
| 58 |
+
os.makedirs(GARMENT_DIR, exist_ok=True)
|
| 59 |
+
|
| 60 |
+
if HF_TOKEN:
|
| 61 |
+
try:
|
| 62 |
+
login(token=HF_TOKEN, add_to_git_credential=False)
|
| 63 |
+
print("HF login: OK")
|
| 64 |
+
except Exception as e:
|
| 65 |
+
print("HF login: FAILED:", str(e)[:200])
|
| 66 |
+
|
| 67 |
+
if not GARMENTS_DATASET:
|
| 68 |
+
print("GARMENTS_DATASET not set. Using local ./garments (if any).")
|
| 69 |
+
return
|
| 70 |
+
|
| 71 |
+
try:
|
| 72 |
+
# Download snapshot to local garments/ (no symlinks for HF container)
|
| 73 |
+
snapshot_download(
|
| 74 |
+
repo_id=GARMENTS_DATASET,
|
| 75 |
+
repo_type="dataset",
|
| 76 |
+
local_dir=GARMENT_DIR,
|
| 77 |
+
local_dir_use_symlinks=False,
|
| 78 |
+
token=HF_TOKEN if HF_TOKEN else None,
|
| 79 |
+
)
|
| 80 |
+
print(f"Garments dataset downloaded: {GARMENTS_DATASET} -> {GARMENT_DIR}/")
|
| 81 |
+
except Exception as e:
|
| 82 |
+
print("Garments download FAILED:", str(e)[:300])
|
| 83 |
+
|
| 84 |
+
def list_garments() -> List[str]:
|
| 85 |
+
try:
|
| 86 |
+
files = []
|
| 87 |
+
for f in os.listdir(GARMENT_DIR):
|
| 88 |
+
if f.lower().endswith(ALLOWED_EXTS) and not f.startswith("."):
|
| 89 |
+
files.append(f)
|
| 90 |
+
files.sort()
|
| 91 |
+
return files
|
| 92 |
+
except Exception:
|
| 93 |
+
return []
|
| 94 |
+
|
| 95 |
+
def garment_path(filename: str) -> str:
|
| 96 |
+
return os.path.join(GARMENT_DIR, filename)
|
| 97 |
+
|
| 98 |
+
def load_garment_pil(filename: str) -> Optional[Image.Image]:
|
| 99 |
+
if not filename:
|
| 100 |
+
return None
|
| 101 |
+
path = garment_path(filename)
|
| 102 |
+
if not os.path.exists(path):
|
| 103 |
+
return None
|
| 104 |
+
try:
|
| 105 |
+
return Image.open(path).convert("RGB")
|
| 106 |
+
except Exception:
|
| 107 |
+
return None
|
| 108 |
+
|
| 109 |
+
def build_gallery_items(files: List[str]):
|
| 110 |
+
# (image_path, caption) — caption empty for clean UI
|
| 111 |
+
return [(garment_path(f), "") for f in files]
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
# =========================
|
| 115 |
+
# Small helpers
|
| 116 |
+
# =========================
|
| 117 |
+
def clamp_int(x, lo, hi):
|
| 118 |
+
try:
|
| 119 |
+
x = int(x)
|
| 120 |
+
except Exception:
|
| 121 |
+
x = lo
|
| 122 |
+
return max(lo, min(hi, x))
|
| 123 |
|
| 124 |
def pil_to_binary_mask(pil_image, threshold=0):
|
| 125 |
np_image = np.array(pil_image)
|
| 126 |
grayscale_image = Image.fromarray(np_image).convert("L")
|
| 127 |
binary_mask = np.array(grayscale_image) > threshold
|
| 128 |
+
mask = (binary_mask.astype(np.uint8) * 255)
|
| 129 |
+
return Image.fromarray(mask)
|
| 130 |
+
|
| 131 |
+
# global simple rate limit (helps avoid spam during internal demo)
|
| 132 |
+
_last_call_ts = 0.0
|
| 133 |
+
def allow_call(min_interval_sec: float = 2.5) -> Tuple[bool, str]:
|
| 134 |
+
global _last_call_ts
|
| 135 |
+
now = time.time()
|
| 136 |
+
if now - _last_call_ts < min_interval_sec:
|
| 137 |
+
wait = min_interval_sec - (now - _last_call_ts)
|
| 138 |
+
return False, f"⏳ Подождите {wait:.1f} сек."
|
| 139 |
+
_last_call_ts = now
|
| 140 |
+
return True, ""
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
# =========================
|
| 144 |
+
# Model init (local IDM-VTON)
|
| 145 |
+
# =========================
|
| 146 |
+
base_path = "yisol/IDM-VTON"
|
| 147 |
+
|
| 148 |
+
# device policy
|
| 149 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 150 |
+
DTYPE = torch.float16 if DEVICE == "cuda" else torch.float32
|
| 151 |
+
|
| 152 |
+
print("DEVICE:", DEVICE, "DTYPE:", DTYPE)
|
| 153 |
+
|
| 154 |
+
# Load components
|
| 155 |
+
unet = UNet2DConditionModel.from_pretrained(base_path, subfolder="unet", torch_dtype=DTYPE)
|
| 156 |
+
unet.requires_grad_(False)
|
| 157 |
|
| 158 |
+
tokenizer_one = AutoTokenizer.from_pretrained(base_path, subfolder="tokenizer", revision=None, use_fast=False)
|
| 159 |
+
tokenizer_two = AutoTokenizer.from_pretrained(base_path, subfolder="tokenizer_2", revision=None, use_fast=False)
|
| 160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
noise_scheduler = DDPMScheduler.from_pretrained(base_path, subfolder="scheduler")
|
| 162 |
|
| 163 |
+
text_encoder_one = CLIPTextModel.from_pretrained(base_path, subfolder="text_encoder", torch_dtype=DTYPE)
|
| 164 |
+
text_encoder_two = CLIPTextModelWithProjection.from_pretrained(base_path, subfolder="text_encoder_2", torch_dtype=DTYPE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
+
image_encoder = CLIPVisionModelWithProjection.from_pretrained(base_path, subfolder="image_encoder", torch_dtype=DTYPE)
|
| 167 |
+
|
| 168 |
+
vae = AutoencoderKL.from_pretrained(base_path, subfolder="vae", torch_dtype=DTYPE)
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
+
UNet_Encoder = UNet2DConditionModel_ref.from_pretrained(base_path, subfolder="unet_encoder", torch_dtype=DTYPE)
|
| 171 |
+
UNet_Encoder.requires_grad_(False)
|
| 172 |
+
|
| 173 |
+
# Parsing/OpenPose init
|
| 174 |
+
# These are heavy; GPU intended. On CPU it may be very slow.
|
| 175 |
parsing_model = Parsing(0)
|
| 176 |
openpose_model = OpenPose(0)
|
| 177 |
|
| 178 |
+
# Freeze
|
| 179 |
+
for m in [UNet_Encoder, image_encoder, vae, unet, text_encoder_one, text_encoder_two]:
|
| 180 |
+
m.requires_grad_(False)
|
| 181 |
+
|
|
|
|
|
|
|
| 182 |
tensor_transfrom = transforms.Compose(
|
| 183 |
+
[transforms.ToTensor(), transforms.Normalize([0.5], [0.5])]
|
| 184 |
+
)
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
pipe = TryonPipeline.from_pretrained(
|
| 187 |
+
base_path,
|
| 188 |
+
unet=unet,
|
| 189 |
+
vae=vae,
|
| 190 |
+
feature_extractor=CLIPImageProcessor(),
|
| 191 |
+
text_encoder=text_encoder_one,
|
| 192 |
+
text_encoder_2=text_encoder_two,
|
| 193 |
+
tokenizer=tokenizer_one,
|
| 194 |
+
tokenizer_2=tokenizer_two,
|
| 195 |
+
scheduler=noise_scheduler,
|
| 196 |
+
image_encoder=image_encoder,
|
| 197 |
+
torch_dtype=DTYPE,
|
| 198 |
)
|
| 199 |
pipe.unet_encoder = UNet_Encoder
|
| 200 |
|
| 201 |
+
|
| 202 |
+
# =========================
|
| 203 |
+
# Inference
|
| 204 |
+
# =========================
|
| 205 |
+
@spaces.GPU # ok on dedicated GPU too
|
| 206 |
+
def start_tryon(
|
| 207 |
+
human_pil: Image.Image,
|
| 208 |
+
garm_img: Image.Image,
|
| 209 |
+
auto_mask: bool = True,
|
| 210 |
+
crop_center: bool = True,
|
| 211 |
+
denoise_steps: int = 25,
|
| 212 |
+
seed: int = 42,
|
| 213 |
+
):
|
| 214 |
+
"""
|
| 215 |
+
Simplified local try-on.
|
| 216 |
+
Returns: (output_image, masked_preview)
|
| 217 |
+
"""
|
| 218 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 219 |
+
dtype = torch.float16 if device == "cuda" else torch.float32
|
| 220 |
+
|
| 221 |
+
# move heavy models
|
| 222 |
+
if device == "cuda":
|
| 223 |
+
openpose_model.preprocessor.body_estimation.model.to(device)
|
| 224 |
pipe.to(device)
|
| 225 |
pipe.unet_encoder.to(device)
|
| 226 |
|
| 227 |
+
# resize inputs to expected
|
| 228 |
+
garm_img = garm_img.convert("RGB").resize((768, 1024))
|
| 229 |
+
human_img_orig = human_pil.convert("RGB")
|
| 230 |
+
|
| 231 |
+
# optional center crop
|
| 232 |
+
if crop_center:
|
| 233 |
width, height = human_img_orig.size
|
| 234 |
target_width = int(min(width, height * (3 / 4)))
|
| 235 |
target_height = int(min(height, width * (4 / 3)))
|
|
|
|
| 239 |
bottom = (height + target_height) / 2
|
| 240 |
cropped_img = human_img_orig.crop((left, top, right, bottom))
|
| 241 |
crop_size = cropped_img.size
|
| 242 |
+
human_img = cropped_img.resize((768, 1024))
|
| 243 |
else:
|
| 244 |
+
human_img = human_img_orig.resize((768, 1024))
|
| 245 |
+
|
| 246 |
+
# mask
|
| 247 |
+
if auto_mask:
|
| 248 |
+
keypoints = openpose_model(human_img.resize((384, 512)))
|
| 249 |
+
model_parse, _ = parsing_model(human_img.resize((384, 512)))
|
| 250 |
+
mask, _ = get_mask_location("hd", "upper_body", model_parse, keypoints)
|
| 251 |
+
mask = mask.resize((768, 1024))
|
| 252 |
else:
|
| 253 |
+
# if someday you add manual mask, you can pass it here
|
| 254 |
+
mask = Image.new("L", (768, 1024), 0)
|
|
|
|
|
|
|
|
|
|
| 255 |
|
| 256 |
+
mask_gray = (1 - transforms.ToTensor()(mask)) * tensor_transfrom(human_img)
|
| 257 |
+
mask_gray = to_pil_image((mask_gray + 1.0) / 2.0)
|
| 258 |
|
| 259 |
+
# densepose
|
| 260 |
+
human_img_arg = _apply_exif_orientation(human_img.resize((384, 512)))
|
| 261 |
human_img_arg = convert_PIL_to_numpy(human_img_arg, format="BGR")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
|
| 263 |
+
args = apply_net.create_argument_parser().parse_args((
|
| 264 |
+
"show",
|
| 265 |
+
"./configs/densepose_rcnn_R_50_FPN_s1x.yaml",
|
| 266 |
+
"./ckpt/densepose/model_final_162be9.pkl",
|
| 267 |
+
"dp_segm",
|
| 268 |
+
"-v",
|
| 269 |
+
"--opts",
|
| 270 |
+
"MODEL.DEVICE",
|
| 271 |
+
"cuda" if device == "cuda" else "cpu",
|
| 272 |
+
))
|
| 273 |
+
pose_img = args.func(args, human_img_arg)
|
| 274 |
+
pose_img = pose_img[:, :, ::-1]
|
| 275 |
+
pose_img = Image.fromarray(pose_img).resize((768, 1024))
|
| 276 |
+
|
| 277 |
+
# prompts (fixed, like your API demo)
|
| 278 |
+
garment_des = "a garment"
|
| 279 |
+
prompt_main = "model is wearing " + garment_des
|
| 280 |
+
prompt_cloth = "a photo of " + garment_des
|
| 281 |
+
negative_prompt = "monochrome, lowres, bad anatomy, worst quality, low quality"
|
| 282 |
+
|
| 283 |
+
denoise_steps = clamp_int(denoise_steps, 20, 40)
|
| 284 |
+
seed = clamp_int(seed, 0, 999999)
|
| 285 |
+
|
| 286 |
+
# inference
|
| 287 |
+
with torch.no_grad():
|
| 288 |
+
if device == "cuda":
|
| 289 |
+
autocast_ctx = torch.cuda.amp.autocast()
|
| 290 |
+
else:
|
| 291 |
+
# no autocast on cpu
|
| 292 |
+
class _NoCtx:
|
| 293 |
+
def __enter__(self): return None
|
| 294 |
+
def __exit__(self, *args): return False
|
| 295 |
+
autocast_ctx = _NoCtx()
|
| 296 |
+
|
| 297 |
+
with autocast_ctx:
|
| 298 |
+
# encode prompts
|
| 299 |
+
(
|
| 300 |
+
prompt_embeds,
|
| 301 |
+
negative_prompt_embeds,
|
| 302 |
+
pooled_prompt_embeds,
|
| 303 |
+
negative_pooled_prompt_embeds,
|
| 304 |
+
) = pipe.encode_prompt(
|
| 305 |
+
prompt_main,
|
| 306 |
+
num_images_per_prompt=1,
|
| 307 |
+
do_classifier_free_guidance=True,
|
| 308 |
+
negative_prompt=negative_prompt,
|
| 309 |
+
)
|
| 310 |
|
| 311 |
+
(
|
| 312 |
+
prompt_embeds_c,
|
| 313 |
+
_,
|
| 314 |
+
_,
|
| 315 |
+
_,
|
| 316 |
+
) = pipe.encode_prompt(
|
| 317 |
+
[prompt_cloth],
|
| 318 |
+
num_images_per_prompt=1,
|
| 319 |
+
do_classifier_free_guidance=False,
|
| 320 |
+
negative_prompt=[negative_prompt],
|
| 321 |
+
)
|
| 322 |
|
| 323 |
+
pose_t = tensor_transfrom(pose_img).unsqueeze(0).to(device=device, dtype=dtype)
|
| 324 |
+
garm_t = tensor_transfrom(garm_img).unsqueeze(0).to(device=device, dtype=dtype)
|
| 325 |
+
|
| 326 |
+
generator = torch.Generator(device).manual_seed(seed)
|
| 327 |
+
|
| 328 |
+
images = pipe(
|
| 329 |
+
prompt_embeds=prompt_embeds.to(device=device, dtype=dtype),
|
| 330 |
+
negative_prompt_embeds=negative_prompt_embeds.to(device=device, dtype=dtype),
|
| 331 |
+
pooled_prompt_embeds=pooled_prompt_embeds.to(device=device, dtype=dtype),
|
| 332 |
+
negative_pooled_prompt_embeds=negative_pooled_prompt_embeds.to(device=device, dtype=dtype),
|
| 333 |
+
num_inference_steps=denoise_steps,
|
| 334 |
+
generator=generator,
|
| 335 |
+
strength=1.0,
|
| 336 |
+
pose_img=pose_t,
|
| 337 |
+
text_embeds_cloth=prompt_embeds_c.to(device=device, dtype=dtype),
|
| 338 |
+
cloth=garm_t,
|
| 339 |
+
mask_image=mask,
|
| 340 |
+
image=human_img,
|
| 341 |
+
height=1024,
|
| 342 |
+
width=768,
|
| 343 |
+
ip_adapter_image=garm_img.resize((768, 1024)),
|
| 344 |
+
guidance_scale=2.0,
|
| 345 |
+
)[0]
|
| 346 |
+
|
| 347 |
+
out_img = images[0]
|
| 348 |
+
if crop_center:
|
| 349 |
+
out_img_rs = out_img.resize(crop_size)
|
| 350 |
+
human_img_orig.paste(out_img_rs, (int(left), int(top)))
|
| 351 |
+
return human_img_orig, mask_gray
|
| 352 |
+
return out_img, mask_gray
|
| 353 |
+
|
| 354 |
+
|
| 355 |
+
# =========================
|
| 356 |
+
# UI (API-like)
|
| 357 |
+
# =========================
|
| 358 |
+
CUSTOM_CSS = """
|
| 359 |
+
footer {display:none !important;}
|
| 360 |
+
#api-info {display:none !important;}
|
| 361 |
+
div[class*="footer"] {display:none !important;}
|
| 362 |
+
button[aria-label="Settings"] {display:none !important;}
|
| 363 |
+
"""
|
| 364 |
+
|
| 365 |
+
def refresh_catalog():
|
| 366 |
+
ensure_garments_downloaded()
|
| 367 |
+
files = list_garments()
|
| 368 |
+
items = build_gallery_items(files)
|
| 369 |
+
status = "✅ Каталог обновлён" if files else "⚠️ Каталог пуст (dataset не скачался или нет файлов)"
|
| 370 |
+
return items, files, None, status
|
| 371 |
+
|
| 372 |
+
def on_gallery_select(files_list: List[str], evt: gr.SelectData):
|
| 373 |
+
if not files_list:
|
| 374 |
+
return None, "⚠️ Каталог пуст"
|
| 375 |
+
idx = int(evt.index) if evt.index is not None else 0
|
| 376 |
+
idx = max(0, min(idx, len(files_list) - 1))
|
| 377 |
+
return files_list[idx], f"👕 Выбрано: {files_list[idx]}"
|
| 378 |
+
|
| 379 |
+
def tryon_ui(person_pil, selected_filename):
|
| 380 |
+
ok, msg = allow_call(2.5)
|
| 381 |
+
if not ok:
|
| 382 |
+
return None, None, msg
|
| 383 |
+
|
| 384 |
+
if person_pil is None:
|
| 385 |
+
return None, None, "❌ Загрузите фото человека"
|
| 386 |
+
if not selected_filename:
|
| 387 |
+
return None, None, "❌ Выберите одежду из каталога"
|
| 388 |
+
|
| 389 |
+
garm = load_garment_pil(selected_filename)
|
| 390 |
+
if garm is None:
|
| 391 |
+
return None, None, "❌ Не удалось загрузить выбранную одежду"
|
| 392 |
+
|
| 393 |
+
out, masked = start_tryon(
|
| 394 |
+
human_pil=person_pil,
|
| 395 |
+
garm_img=garm,
|
| 396 |
+
auto_mask=True,
|
| 397 |
+
crop_center=True,
|
| 398 |
+
denoise_steps=25,
|
| 399 |
+
seed=42,
|
| 400 |
+
)
|
| 401 |
+
return out, masked, "✅ Готово"
|
| 402 |
|
|
|
|
| 403 |
|
| 404 |
+
# ensure garments present at startup (best effort)
|
| 405 |
+
ensure_garments_downloaded()
|
| 406 |
+
_initial_files = list_garments()
|
| 407 |
+
_initial_items = build_gallery_items(_initial_files)
|
| 408 |
|
| 409 |
+
with gr.Blocks(title="Virtual Try-On Rendez-vous", css=CUSTOM_CSS) as demo:
|
| 410 |
+
gr.Markdown("# Virtual Try-On Rendez-vous")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 411 |
|
| 412 |
+
garment_files_state = gr.State(_initial_files)
|
| 413 |
+
selected_garment_state = gr.State(None)
|
|
|
|
|
|
|
|
|
|
| 414 |
|
| 415 |
+
with gr.Row():
|
| 416 |
with gr.Column():
|
| 417 |
+
person = gr.Image(label="Фото человека", type="pil", height=420)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 418 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 419 |
with gr.Row():
|
| 420 |
+
refresh_btn = gr.Button("🔄 Обновить каталог одежды", variant="secondary")
|
| 421 |
+
selected_label = gr.Markdown("👕 Выберите одежду ниже")
|
| 422 |
+
|
| 423 |
+
garment_gallery = gr.Gallery(
|
| 424 |
+
label="Одежда для примерки",
|
| 425 |
+
value=_initial_items,
|
| 426 |
+
columns=4,
|
| 427 |
+
height=340,
|
| 428 |
+
allow_preview=True,
|
| 429 |
+
)
|
| 430 |
|
| 431 |
+
run = gr.Button("Примерить", variant="primary")
|
| 432 |
+
status = gr.Textbox(value="Ожидание...", interactive=False)
|
| 433 |
|
| 434 |
+
with gr.Column():
|
| 435 |
+
out = gr.Image(label="Результат", type="pil", height=520)
|
| 436 |
+
masked = gr.Image(label="Маска/предпросмотр (служебное)", type="pil", height=320)
|
| 437 |
|
| 438 |
+
garment_gallery.select(
|
| 439 |
+
fn=on_gallery_select,
|
| 440 |
+
inputs=[garment_files_state],
|
| 441 |
+
outputs=[selected_garment_state, selected_label],
|
| 442 |
+
)
|
| 443 |
|
| 444 |
+
refresh_btn.click(
|
| 445 |
+
fn=refresh_catalog,
|
| 446 |
+
inputs=[],
|
| 447 |
+
outputs=[garment_gallery, garment_files_state, selected_garment_state, status],
|
| 448 |
+
)
|
| 449 |
|
| 450 |
+
run.click(
|
| 451 |
+
fn=tryon_ui,
|
| 452 |
+
inputs=[person, selected_garment_state],
|
| 453 |
+
outputs=[out, masked, status],
|
| 454 |
+
)
|
| 455 |
|
| 456 |
+
if __name__ == "__main__":
|
| 457 |
+
demo.launch(
|
| 458 |
+
server_name="0.0.0.0",
|
| 459 |
+
server_port=7860,
|
| 460 |
+
share=False,
|
| 461 |
+
auth=APP_AUTH,
|
| 462 |
+
)
|