Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,8 +10,9 @@ from transformers import (
|
|
| 10 |
CLIPTextModel,
|
| 11 |
CLIPTextModelWithProjection,
|
| 12 |
)
|
| 13 |
-
from diffusers import DDPMScheduler,
|
| 14 |
from typing import List
|
|
|
|
| 15 |
import torch
|
| 16 |
import os
|
| 17 |
from transformers import AutoTokenizer
|
|
@@ -21,7 +22,7 @@ from torchvision import transforms
|
|
| 21 |
import apply_net
|
| 22 |
from preprocess.humanparsing.run_parsing import Parsing
|
| 23 |
from preprocess.openpose.run_openpose import OpenPose
|
| 24 |
-
from detectron2.data.detection_utils import convert_PIL_to_numpy,
|
| 25 |
from torchvision.transforms.functional import to_pil_image
|
| 26 |
|
| 27 |
|
|
@@ -32,214 +33,280 @@ def pil_to_binary_mask(pil_image, threshold=0):
|
|
| 32 |
mask = np.zeros(binary_mask.shape, dtype=np.uint8)
|
| 33 |
for i in range(binary_mask.shape[0]):
|
| 34 |
for j in range(binary_mask.shape[1]):
|
| 35 |
-
if binary_mask[i,
|
| 36 |
-
mask[i,
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
def add_watermark(main_image, logo_path='logo.png', position='bottom-left', size_percentage=10):
|
| 41 |
-
logo = Image.open(logo_path).convert('RGBA')
|
| 42 |
-
main_width, main_height = main_image.size
|
| 43 |
-
logo_width = int(main_width * size_percentage / 100)
|
| 44 |
-
logo_height = int(logo.size[1] * (logo_width / logo.size[0]))
|
| 45 |
-
logo = logo.resize((logo_width, logo_height), Image.Resampling.LANCZOS)
|
| 46 |
-
|
| 47 |
-
if main_image.mode != 'RGBA':
|
| 48 |
-
main_image = main_image.convert('RGBA')
|
| 49 |
-
|
| 50 |
-
watermarked = Image.new('RGBA', main_image.size, (0, 0, 0, 0))
|
| 51 |
-
watermarked.paste(main_image, (0, 0))
|
| 52 |
-
|
| 53 |
-
if position == 'bottom-left':
|
| 54 |
-
pos = (10, main_height - logo_height - 10)
|
| 55 |
-
elif position == 'bottom-right':
|
| 56 |
-
pos = (main_width - logo_width - 10, main_height - logo_height - 10)
|
| 57 |
-
elif position == 'top-right':
|
| 58 |
-
pos = (main_width - logo_width - 10, 10)
|
| 59 |
-
elif position == 'top-left':
|
| 60 |
-
pos = (10, 10)
|
| 61 |
-
|
| 62 |
-
watermarked.paste(logo, pos, logo)
|
| 63 |
-
return watermarked.convert('RGB')
|
| 64 |
|
| 65 |
|
| 66 |
base_path = 'yisol/IDM-VTON'
|
| 67 |
example_path = os.path.join(os.path.dirname(__file__), 'example')
|
| 68 |
|
| 69 |
-
unet = UNet2DConditionModel.from_pretrained(
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
noise_scheduler = DDPMScheduler.from_pretrained(base_path, subfolder="scheduler")
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
parsing_model = Parsing(0)
|
| 80 |
openpose_model = OpenPose(0)
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
pipe = TryonPipeline.from_pretrained(
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
)
|
| 103 |
pipe.unet_encoder = UNet_Encoder
|
| 104 |
|
| 105 |
@spaces.GPU
|
| 106 |
-
def start_tryon(dict,
|
| 107 |
device = "cuda"
|
|
|
|
| 108 |
openpose_model.preprocessor.body_estimation.model.to(device)
|
| 109 |
pipe.to(device)
|
| 110 |
pipe.unet_encoder.to(device)
|
| 111 |
|
| 112 |
-
garm_img
|
| 113 |
-
human_img_orig = dict["background"].convert("RGB")
|
| 114 |
-
|
| 115 |
if is_checked_crop:
|
| 116 |
width, height = human_img_orig.size
|
| 117 |
target_width = int(min(width, height * (3 / 4)))
|
| 118 |
target_height = int(min(height, width * (4 / 3)))
|
| 119 |
-
left = (width - target_width) /
|
| 120 |
-
top = (height - target_height) /
|
| 121 |
-
|
|
|
|
|
|
|
| 122 |
crop_size = cropped_img.size
|
| 123 |
-
human_img = cropped_img.resize((768,
|
| 124 |
else:
|
| 125 |
-
human_img = human_img_orig.resize((768,
|
|
|
|
| 126 |
|
| 127 |
if is_checked:
|
| 128 |
-
keypoints = openpose_model(human_img.resize((384,
|
| 129 |
-
model_parse, _ = parsing_model(human_img.resize((384,
|
| 130 |
-
mask,
|
| 131 |
-
mask = mask.resize((768,
|
| 132 |
else:
|
| 133 |
mask = pil_to_binary_mask(dict['layers'][0].convert("RGB").resize((768, 1024)))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
-
mask_gray = (1 - transforms.ToTensor()(mask)) * tensor_transfrom(human_img)
|
| 136 |
-
mask_gray = to_pil_image((mask_gray + 1.0) / 2.0)
|
| 137 |
|
| 138 |
-
human_img_arg = _apply_exif_orientation(human_img.resize((384,
|
| 139 |
human_img_arg = convert_PIL_to_numpy(human_img_arg, format="BGR")
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
'./ckpt/densepose/model_final_162be9.pkl', 'dp_segm', '-v',
|
| 143 |
-
'--opts', 'MODEL.DEVICE', 'cuda'))
|
| 144 |
-
pose_img = args.func(args, human_img_arg)
|
| 145 |
-
pose_img = Image.fromarray(pose_img[:, :, ::-1]).resize((768, 1024))
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
with torch.no_grad():
|
|
|
|
| 148 |
with torch.cuda.amp.autocast():
|
| 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 |
if is_checked_crop:
|
| 189 |
-
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
-
|
| 193 |
|
| 194 |
-
# --- Gradio UI setup ---
|
| 195 |
-
garm_list = os.listdir(os.path.join(example_path, "cloth"))
|
| 196 |
-
garm_list_path = [os.path.join(example_path, "cloth", g) for g in garm_list]
|
| 197 |
-
human_list = os.listdir(os.path.join(example_path, "human"))
|
| 198 |
-
human_list_path = [os.path.join(example_path, "human", h) for h in human_list]
|
| 199 |
-
human_ex_list = [{'background': h, 'layers': None, 'composite': None} for h in human_list_path]
|
| 200 |
|
| 201 |
image_blocks = gr.Blocks().queue()
|
| 202 |
with image_blocks as demo:
|
| 203 |
-
gr.Markdown(
|
| 204 |
-
|
| 205 |
-
<div style="text-align: center; background: linear-gradient(135deg, #2541b2 0%, #1a237e 100%); padding: 2.5rem; color: white; border-radius: 0 0 20px 20px; margin-bottom: 2rem; box-shadow: 0 4px 6px rgba(0,0,0,0.1);">
|
| 206 |
-
<h1 style="color: white; font-size: 2.5rem; font-weight: 600; margin-bottom: 1rem;">Deradh Virtual Try-On Experience</h1>
|
| 207 |
-
<div style="margin: 1rem 0;">
|
| 208 |
-
<a href="https://deradh.com" style="color: white; text-decoration: none; padding: 0.5rem 1rem; border: 2px solid white; border-radius: 25px; transition: all 0.3s ease;">
|
| 209 |
-
Visit Deradh.com
|
| 210 |
-
</a>
|
| 211 |
-
</div>
|
| 212 |
-
</div>
|
| 213 |
-
<div style="text-align: center; padding: 1rem; color: #6ed7fe; font-size: 1.2rem; font-weight: 500; margin-bottom: 2rem;">
|
| 214 |
-
Experience the future of fashion with our AI-powered virtual try-on technology. Every user gets 2-3 free trials per day.
|
| 215 |
-
</div>
|
| 216 |
-
""")
|
| 217 |
-
|
| 218 |
with gr.Row():
|
| 219 |
with gr.Column():
|
| 220 |
imgs = gr.ImageEditor(sources='upload', type="pil", label='Human. Mask with pen or use auto-masking', interactive=True)
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
with gr.Column():
|
| 226 |
garm_img = gr.Image(label="Garment", sources='upload', type="pil")
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
with gr.Column():
|
| 231 |
-
image_out = gr.Image(label="
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
|
| 245 |
-
image_blocks.launch()
|
|
|
|
| 10 |
CLIPTextModel,
|
| 11 |
CLIPTextModelWithProjection,
|
| 12 |
)
|
| 13 |
+
from diffusers import DDPMScheduler,AutoencoderKL
|
| 14 |
from typing import List
|
| 15 |
+
|
| 16 |
import torch
|
| 17 |
import os
|
| 18 |
from transformers import AutoTokenizer
|
|
|
|
| 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 |
from torchvision.transforms.functional import to_pil_image
|
| 27 |
|
| 28 |
|
|
|
|
| 33 |
mask = np.zeros(binary_mask.shape, dtype=np.uint8)
|
| 34 |
for i in range(binary_mask.shape[0]):
|
| 35 |
for j in range(binary_mask.shape[1]):
|
| 36 |
+
if binary_mask[i,j] == True :
|
| 37 |
+
mask[i,j] = 1
|
| 38 |
+
mask = (mask*255).astype(np.uint8)
|
| 39 |
+
output_mask = Image.fromarray(mask)
|
| 40 |
+
return output_mask
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
base_path = 'yisol/IDM-VTON'
|
| 44 |
example_path = os.path.join(os.path.dirname(__file__), 'example')
|
| 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 |
+
base_path,
|
| 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 |
+
# "stabilityai/stable-diffusion-xl-base-1.0",
|
| 87 |
+
UNet_Encoder = UNet2DConditionModel_ref.from_pretrained(
|
| 88 |
+
base_path,
|
| 89 |
+
subfolder="unet_encoder",
|
| 90 |
+
torch_dtype=torch.float16,
|
| 91 |
+
)
|
| 92 |
|
| 93 |
parsing_model = Parsing(0)
|
| 94 |
openpose_model = OpenPose(0)
|
| 95 |
|
| 96 |
+
UNet_Encoder.requires_grad_(False)
|
| 97 |
+
image_encoder.requires_grad_(False)
|
| 98 |
+
vae.requires_grad_(False)
|
| 99 |
+
unet.requires_grad_(False)
|
| 100 |
+
text_encoder_one.requires_grad_(False)
|
| 101 |
+
text_encoder_two.requires_grad_(False)
|
| 102 |
+
tensor_transfrom = transforms.Compose(
|
| 103 |
+
[
|
| 104 |
+
transforms.ToTensor(),
|
| 105 |
+
transforms.Normalize([0.5], [0.5]),
|
| 106 |
+
]
|
| 107 |
+
)
|
| 108 |
|
| 109 |
pipe = TryonPipeline.from_pretrained(
|
| 110 |
+
base_path,
|
| 111 |
+
unet=unet,
|
| 112 |
+
vae=vae,
|
| 113 |
+
feature_extractor= CLIPImageProcessor(),
|
| 114 |
+
text_encoder = text_encoder_one,
|
| 115 |
+
text_encoder_2 = text_encoder_two,
|
| 116 |
+
tokenizer = tokenizer_one,
|
| 117 |
+
tokenizer_2 = tokenizer_two,
|
| 118 |
+
scheduler = noise_scheduler,
|
| 119 |
+
image_encoder=image_encoder,
|
| 120 |
+
torch_dtype=torch.float16,
|
| 121 |
)
|
| 122 |
pipe.unet_encoder = UNet_Encoder
|
| 123 |
|
| 124 |
@spaces.GPU
|
| 125 |
+
def start_tryon(dict,garm_img,garment_des,is_checked,is_checked_crop,denoise_steps,seed):
|
| 126 |
device = "cuda"
|
| 127 |
+
|
| 128 |
openpose_model.preprocessor.body_estimation.model.to(device)
|
| 129 |
pipe.to(device)
|
| 130 |
pipe.unet_encoder.to(device)
|
| 131 |
|
| 132 |
+
garm_img= garm_img.convert("RGB").resize((768,1024))
|
| 133 |
+
human_img_orig = dict["background"].convert("RGB")
|
| 134 |
+
|
| 135 |
if is_checked_crop:
|
| 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)))
|
| 139 |
+
left = (width - target_width) / 2
|
| 140 |
+
top = (height - target_height) / 2
|
| 141 |
+
right = (width + target_width) / 2
|
| 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 is_checked:
|
| 151 |
+
keypoints = openpose_model(human_img.resize((384,512)))
|
| 152 |
+
model_parse, _ = parsing_model(human_img.resize((384,512)))
|
| 153 |
+
mask, mask_gray = get_mask_location('hd', "upper_body", model_parse, keypoints)
|
| 154 |
+
mask = mask.resize((768,1024))
|
| 155 |
else:
|
| 156 |
mask = pil_to_binary_mask(dict['layers'][0].convert("RGB").resize((768, 1024)))
|
| 157 |
+
# mask = transforms.ToTensor()(mask)
|
| 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 |
+
human_img_arg = _apply_exif_orientation(human_img.resize((384,512)))
|
| 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 |
+
garm_list = os.listdir(os.path.join(example_path,"cloth"))
|
| 245 |
+
garm_list_path = [os.path.join(example_path,"cloth",garm) for garm in garm_list]
|
| 246 |
+
|
| 247 |
+
human_list = os.listdir(os.path.join(example_path,"human"))
|
| 248 |
+
human_list_path = [os.path.join(example_path,"human",human) for human in human_list]
|
| 249 |
+
|
| 250 |
+
human_ex_list = []
|
| 251 |
+
for ex_human in human_list_path:
|
| 252 |
+
ex_dict= {}
|
| 253 |
+
ex_dict['background'] = ex_human
|
| 254 |
+
ex_dict['layers'] = None
|
| 255 |
+
ex_dict['composite'] = None
|
| 256 |
+
human_ex_list.append(ex_dict)
|
| 257 |
|
| 258 |
+
##default human
|
| 259 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
|
| 261 |
image_blocks = gr.Blocks().queue()
|
| 262 |
with image_blocks as demo:
|
| 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 |
+
example = gr.Examples(
|
| 274 |
+
inputs=imgs,
|
| 275 |
+
examples_per_page=10,
|
| 276 |
+
examples=human_ex_list
|
| 277 |
+
)
|
| 278 |
|
| 279 |
with gr.Column():
|
| 280 |
garm_img = gr.Image(label="Garment", sources='upload', type="pil")
|
| 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 |
+
denoise_steps = gr.Number(label="Denoising Steps", minimum=20, maximum=40, value=30, step=1)
|
| 303 |
+
seed = gr.Number(label="Seed", minimum=-1, maximum=2147483647, step=1, value=42)
|
| 304 |
+
|
| 305 |
+
|
| 306 |
+
|
| 307 |
+
try_button.click(fn=start_tryon, inputs=[imgs, garm_img, prompt, is_checked,is_checked_crop, denoise_steps, seed], outputs=[image_out,masked_img], api_name='tryon')
|
| 308 |
+
|
| 309 |
+
|
| 310 |
+
|
| 311 |
|
| 312 |
+
image_blocks.launch()
|