Spaces:
Running
on
Zero
Running
on
Zero
jiajl1
commited on
Commit
·
c2cb399
1
Parent(s):
ecf5d59
support category
Browse files
app.py
CHANGED
|
@@ -13,6 +13,7 @@ from transformers import (
|
|
| 13 |
from diffusers import DDPMScheduler,AutoencoderKL
|
| 14 |
from typing import List
|
| 15 |
|
|
|
|
| 16 |
import torch
|
| 17 |
import os
|
| 18 |
from transformers import AutoTokenizer
|
|
@@ -123,16 +124,16 @@ pipe = TryonPipeline.from_pretrained(
|
|
| 123 |
pipe.unet_encoder = UNet_Encoder
|
| 124 |
|
| 125 |
@spaces.GPU
|
| 126 |
-
def start_tryon(dict,garm_img,garment_des,is_checked,is_checked_crop,denoise_steps,seed):
|
| 127 |
device = "cuda"
|
| 128 |
-
|
| 129 |
openpose_model.preprocessor.body_estimation.model.to(device)
|
| 130 |
pipe.to(device)
|
| 131 |
pipe.unet_encoder.to(device)
|
| 132 |
|
| 133 |
garm_img= garm_img.convert("RGB").resize((768,1024))
|
| 134 |
-
human_img_orig = dict["background"].convert("RGB")
|
| 135 |
-
|
| 136 |
if is_checked_crop:
|
| 137 |
width, height = human_img_orig.size
|
| 138 |
target_width = int(min(width, height * (3 / 4)))
|
|
@@ -151,7 +152,7 @@ def start_tryon(dict,garm_img,garment_des,is_checked,is_checked_crop,denoise_ste
|
|
| 151 |
if is_checked:
|
| 152 |
keypoints = openpose_model(human_img.resize((384,512)))
|
| 153 |
model_parse, _ = parsing_model(human_img.resize((384,512)))
|
| 154 |
-
mask, mask_gray = get_mask_location('hd',
|
| 155 |
mask = mask.resize((768,1024))
|
| 156 |
else:
|
| 157 |
mask = pil_to_binary_mask(dict['layers'][0].convert("RGB").resize((768, 1024)))
|
|
@@ -163,15 +164,15 @@ def start_tryon(dict,garm_img,garment_des,is_checked,is_checked_crop,denoise_ste
|
|
| 163 |
|
| 164 |
human_img_arg = _apply_exif_orientation(human_img.resize((384,512)))
|
| 165 |
human_img_arg = convert_PIL_to_numpy(human_img_arg, format="BGR")
|
| 166 |
-
|
| 167 |
-
|
| 168 |
|
| 169 |
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'))
|
| 170 |
# verbosity = getattr(args, "verbosity", None)
|
| 171 |
-
pose_img = args.func(args,human_img_arg)
|
| 172 |
-
pose_img = pose_img[:,:,::-1]
|
| 173 |
pose_img = Image.fromarray(pose_img).resize((768,1024))
|
| 174 |
-
|
| 175 |
with torch.no_grad():
|
| 176 |
# Extract the images
|
| 177 |
with torch.cuda.amp.autocast():
|
|
@@ -190,7 +191,7 @@ def start_tryon(dict,garm_img,garment_des,is_checked,is_checked_crop,denoise_ste
|
|
| 190 |
do_classifier_free_guidance=True,
|
| 191 |
negative_prompt=negative_prompt,
|
| 192 |
)
|
| 193 |
-
|
| 194 |
prompt = "((best quality, masterpiece, ultra-detailed, high quality photography, photo realistic)), a photo of " + garment_des
|
| 195 |
negative_prompt = "monochrome, lowres, bad anatomy, worst quality, normal quality, low quality, blurry, jpeg artifacts, sketch"
|
| 196 |
if not isinstance(prompt, List):
|
|
@@ -227,7 +228,7 @@ def start_tryon(dict,garm_img,garment_des,is_checked,is_checked_crop,denoise_ste
|
|
| 227 |
text_embeds_cloth=prompt_embeds_c.to(device,torch.float16),
|
| 228 |
cloth = garm_tensor.to(device,torch.float16),
|
| 229 |
mask_image=mask,
|
| 230 |
-
image=human_img,
|
| 231 |
height=1024,
|
| 232 |
width=768,
|
| 233 |
ip_adapter_image = garm_img.resize((768,1024)),
|
|
@@ -235,8 +236,8 @@ def start_tryon(dict,garm_img,garment_des,is_checked,is_checked_crop,denoise_ste
|
|
| 235 |
)[0]
|
| 236 |
|
| 237 |
if is_checked_crop:
|
| 238 |
-
out_img = images[0].resize(crop_size)
|
| 239 |
-
human_img_orig.paste(out_img, (int(left), int(top)))
|
| 240 |
return human_img_orig, mask_gray
|
| 241 |
else:
|
| 242 |
return images[0], mask_gray
|
|
@@ -280,6 +281,12 @@ with image_blocks as demo:
|
|
| 280 |
imgs = gr.ImageEditor(sources='upload', type="pil", label='AI虛擬模特兒-請啟用全自動偵測模式或是使用畫筆在需要更衣的部位塗抹註記', interactive=True)
|
| 281 |
with gr.Row():
|
| 282 |
is_checked = gr.Checkbox(label="Yes", info="啟用全自動偵測更衣模式",value=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
with gr.Row():
|
| 284 |
is_checked_crop = gr.Checkbox(label="Yes", info="開啟自動剪裁並調整圖片大小模式",value=False)
|
| 285 |
|
|
@@ -309,9 +316,9 @@ with image_blocks as demo:
|
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
-
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')
|
|
|
|
| 313 |
|
| 314 |
-
|
| 315 |
|
| 316 |
|
| 317 |
image_blocks.launch()
|
|
|
|
| 13 |
from diffusers import DDPMScheduler,AutoencoderKL
|
| 14 |
from typing import List
|
| 15 |
|
| 16 |
+
|
| 17 |
import torch
|
| 18 |
import os
|
| 19 |
from transformers import AutoTokenizer
|
|
|
|
| 124 |
pipe.unet_encoder = UNet_Encoder
|
| 125 |
|
| 126 |
@spaces.GPU
|
| 127 |
+
def start_tryon(dict,garm_img,garment_des,is_checked,is_checked_crop,denoise_steps,seed,category):
|
| 128 |
device = "cuda"
|
| 129 |
+
|
| 130 |
openpose_model.preprocessor.body_estimation.model.to(device)
|
| 131 |
pipe.to(device)
|
| 132 |
pipe.unet_encoder.to(device)
|
| 133 |
|
| 134 |
garm_img= garm_img.convert("RGB").resize((768,1024))
|
| 135 |
+
human_img_orig = dict["background"].convert("RGB")
|
| 136 |
+
|
| 137 |
if is_checked_crop:
|
| 138 |
width, height = human_img_orig.size
|
| 139 |
target_width = int(min(width, height * (3 / 4)))
|
|
|
|
| 152 |
if is_checked:
|
| 153 |
keypoints = openpose_model(human_img.resize((384,512)))
|
| 154 |
model_parse, _ = parsing_model(human_img.resize((384,512)))
|
| 155 |
+
mask, mask_gray = get_mask_location('hd', category, model_parse, keypoints)
|
| 156 |
mask = mask.resize((768,1024))
|
| 157 |
else:
|
| 158 |
mask = pil_to_binary_mask(dict['layers'][0].convert("RGB").resize((768, 1024)))
|
|
|
|
| 164 |
|
| 165 |
human_img_arg = _apply_exif_orientation(human_img.resize((384,512)))
|
| 166 |
human_img_arg = convert_PIL_to_numpy(human_img_arg, format="BGR")
|
| 167 |
+
|
| 168 |
+
|
| 169 |
|
| 170 |
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'))
|
| 171 |
# verbosity = getattr(args, "verbosity", None)
|
| 172 |
+
pose_img = args.func(args,human_img_arg)
|
| 173 |
+
pose_img = pose_img[:,:,::-1]
|
| 174 |
pose_img = Image.fromarray(pose_img).resize((768,1024))
|
| 175 |
+
|
| 176 |
with torch.no_grad():
|
| 177 |
# Extract the images
|
| 178 |
with torch.cuda.amp.autocast():
|
|
|
|
| 191 |
do_classifier_free_guidance=True,
|
| 192 |
negative_prompt=negative_prompt,
|
| 193 |
)
|
| 194 |
+
|
| 195 |
prompt = "((best quality, masterpiece, ultra-detailed, high quality photography, photo realistic)), a photo of " + garment_des
|
| 196 |
negative_prompt = "monochrome, lowres, bad anatomy, worst quality, normal quality, low quality, blurry, jpeg artifacts, sketch"
|
| 197 |
if not isinstance(prompt, List):
|
|
|
|
| 228 |
text_embeds_cloth=prompt_embeds_c.to(device,torch.float16),
|
| 229 |
cloth = garm_tensor.to(device,torch.float16),
|
| 230 |
mask_image=mask,
|
| 231 |
+
image=human_img,
|
| 232 |
height=1024,
|
| 233 |
width=768,
|
| 234 |
ip_adapter_image = garm_img.resize((768,1024)),
|
|
|
|
| 236 |
)[0]
|
| 237 |
|
| 238 |
if is_checked_crop:
|
| 239 |
+
out_img = images[0].resize(crop_size)
|
| 240 |
+
human_img_orig.paste(out_img, (int(left), int(top)))
|
| 241 |
return human_img_orig, mask_gray
|
| 242 |
else:
|
| 243 |
return images[0], mask_gray
|
|
|
|
| 281 |
imgs = gr.ImageEditor(sources='upload', type="pil", label='AI虛擬模特兒-請啟用全自動偵測模式或是使用畫筆在需要更衣的部位塗抹註記', interactive=True)
|
| 282 |
with gr.Row():
|
| 283 |
is_checked = gr.Checkbox(label="Yes", info="啟用全自動偵測更衣模式",value=True)
|
| 284 |
+
with gr.Row():
|
| 285 |
+
category = gr.Dropdown(
|
| 286 |
+
choices=["upper_body", "lower_body", "dresses"],
|
| 287 |
+
label="Category",
|
| 288 |
+
value="upper_body"
|
| 289 |
+
)
|
| 290 |
with gr.Row():
|
| 291 |
is_checked_crop = gr.Checkbox(label="Yes", info="開啟自動剪裁並調整圖片大小模式",value=False)
|
| 292 |
|
|
|
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
+
try_button.click(fn=start_tryon, inputs=[imgs, garm_img, prompt, is_checked,is_checked_crop, denoise_steps, seed, category], outputs=[image_out,masked_img], api_name='tryon')
|
| 320 |
+
|
| 321 |
|
|
|
|
| 322 |
|
| 323 |
|
| 324 |
image_blocks.launch()
|