Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,27 +2,7 @@ import sys
|
|
2 |
import os
|
3 |
|
4 |
sys.path.append('./')
|
5 |
-
#libraries
|
6 |
os.system("pip install gradio accelerate==0.25.0 torchmetrics==1.2.1 tqdm==4.66.1 fastapi==0.111.0 transformers==4.36.2 diffusers==0.25 einops==0.7.0 bitsandbytes scipy==1.11.1 opencv-python gradio==4.24.0 fvcore cloudpickle omegaconf pycocotools basicsr av onnxruntime==1.16.2 peft==0.11.1 huggingface_hub==0.24.7 --no-deps")
|
7 |
-
os.system("pip install ninja")
|
8 |
-
|
9 |
-
#DensePose
|
10 |
-
os.system("git clone https://github.com/facebookresearch/detectron2.git")
|
11 |
-
os.system("!pip install git+https://github.com/facebookresearch/detectron2@main#subdirectory=projects/DensePose")
|
12 |
-
from detectron2.config import get_cfg
|
13 |
-
import torch
|
14 |
-
import cv2
|
15 |
-
import numpy as np
|
16 |
-
from detectron2.engine import DefaultPredictor
|
17 |
-
from densepose import add_densepose_config
|
18 |
-
from densepose.vis.extractor import DensePoseResultExtractor
|
19 |
-
from densepose.vis.densepose_results import DensePoseResultsFineSegmentationVisualizer as Visualizer
|
20 |
-
# Initialize Detectron2 configuration for DensePose
|
21 |
-
cfg = get_cfg()
|
22 |
-
add_densepose_config(cfg)
|
23 |
-
cfg.merge_from_file("detectron2/projects/DensePose/configs/densepose_rcnn_R_50_FPN_s1x.yaml")
|
24 |
-
cfg.MODEL.WEIGHTS = "https://dl.fbaipublicfiles.com/densepose/densepose_rcnn_R_50_FPN_s1x/165712039/model_final_162be9.pkl"
|
25 |
-
|
26 |
import spaces
|
27 |
from fastapi import FastAPI
|
28 |
app = FastAPI()
|
@@ -40,6 +20,9 @@ from transformers import (
|
|
40 |
)
|
41 |
from diffusers import DDPMScheduler,AutoencoderKL
|
42 |
from typing import List
|
|
|
|
|
|
|
43 |
from transformers import AutoTokenizer
|
44 |
import numpy as np
|
45 |
from torchvision import transforms
|
@@ -61,7 +44,6 @@ def pil_to_binary_mask(pil_image, threshold=0):
|
|
61 |
return output_mask
|
62 |
|
63 |
|
64 |
-
|
65 |
base_path = 'yisol/IDM-VTON'
|
66 |
|
67 |
unet = UNet2DConditionModel.from_pretrained(
|
@@ -142,31 +124,11 @@ pipe = TryonPipeline.from_pretrained(
|
|
142 |
pipe.unet_encoder = UNet_Encoder
|
143 |
|
144 |
@spaces.GPU
|
145 |
-
def start_tryon(person_img, mask_img, cloth_img, garment_des, denoise_steps):
|
146 |
# Assuming device is set up (e.g., "cuda" or "cpu")
|
147 |
pipe.to(device)
|
148 |
pipe.unet_encoder.to(device)
|
149 |
|
150 |
-
cfg.MODEL.DEVICE = device
|
151 |
-
predictor = DefaultPredictor(cfg)
|
152 |
-
# Read the input image
|
153 |
-
height, width, _ = person_img.shape
|
154 |
-
|
155 |
-
# Process the image
|
156 |
-
with torch.no_grad():
|
157 |
-
outputs = predictor(person_img)['instances']
|
158 |
-
|
159 |
-
results = DensePoseResultExtractor()(outputs)
|
160 |
-
cmap = cv2.COLORMAP_VIRIDIS
|
161 |
-
|
162 |
-
# Create a base image with a black background
|
163 |
-
background = np.zeros((height, width, 3), dtype=np.uint8)
|
164 |
-
|
165 |
-
#Visualize the results on the black background
|
166 |
-
visualizer = Visualizer(alpha=1, cmap=cmap)
|
167 |
-
pose_img = visualizer.visualize(background, results)
|
168 |
-
|
169 |
-
|
170 |
# Resize and prepare images
|
171 |
garm_img = cloth_img.convert("RGB").resize((768, 1024))
|
172 |
human_img = person_img.convert("RGB").resize((768, 1024))
|
@@ -219,7 +181,8 @@ def start_tryon(person_img, mask_img, cloth_img, garment_des, denoise_steps):
|
|
219 |
garm_tensor = tensor_transfrom(garm_img).unsqueeze(0).to(device, torch.float16)
|
220 |
|
221 |
# Prepare the generator with optional seed
|
222 |
-
generator = torch.Generator(device).manual_seed(
|
|
|
223 |
# Generate the virtual try-on output image
|
224 |
images = pipe(
|
225 |
prompt_embeds=prompt_embeds.to(device, torch.float16),
|
@@ -251,6 +214,7 @@ with image_blocks as demo:
|
|
251 |
with gr.Row():
|
252 |
with gr.Column():
|
253 |
person_img = gr.Image(label='Person Image', sources='upload', type="pil")
|
|
|
254 |
mask_img = gr.Image(label='Mask Image', sources='upload', type="pil")
|
255 |
|
256 |
with gr.Column():
|
@@ -259,11 +223,12 @@ with image_blocks as demo:
|
|
259 |
|
260 |
with gr.Column():
|
261 |
denoise_steps = gr.Number(label="Denoising Steps", minimum=20, maximum=40, value=30, step=1)
|
|
|
262 |
|
263 |
with gr.Column():
|
264 |
image_out = gr.Image(label="Output Image", elem_id="output-img", show_share_button=False)
|
265 |
|
266 |
try_button = gr.Button(value="Try-on")
|
267 |
-
try_button.click(fn=start_tryon, inputs=[person_img, mask_img, cloth_img, garment_des, denoise_steps], outputs=[image_out], api_name='tryon')
|
268 |
|
269 |
image_blocks.launch()
|
|
|
2 |
import os
|
3 |
|
4 |
sys.path.append('./')
|
|
|
5 |
os.system("pip install gradio accelerate==0.25.0 torchmetrics==1.2.1 tqdm==4.66.1 fastapi==0.111.0 transformers==4.36.2 diffusers==0.25 einops==0.7.0 bitsandbytes scipy==1.11.1 opencv-python gradio==4.24.0 fvcore cloudpickle omegaconf pycocotools basicsr av onnxruntime==1.16.2 peft==0.11.1 huggingface_hub==0.24.7 --no-deps")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
import spaces
|
7 |
from fastapi import FastAPI
|
8 |
app = FastAPI()
|
|
|
20 |
)
|
21 |
from diffusers import DDPMScheduler,AutoencoderKL
|
22 |
from typing import List
|
23 |
+
|
24 |
+
import torch
|
25 |
+
import os
|
26 |
from transformers import AutoTokenizer
|
27 |
import numpy as np
|
28 |
from torchvision import transforms
|
|
|
44 |
return output_mask
|
45 |
|
46 |
|
|
|
47 |
base_path = 'yisol/IDM-VTON'
|
48 |
|
49 |
unet = UNet2DConditionModel.from_pretrained(
|
|
|
124 |
pipe.unet_encoder = UNet_Encoder
|
125 |
|
126 |
@spaces.GPU
|
127 |
+
def start_tryon(person_img, pose_img, mask_img, cloth_img, garment_des, denoise_steps, seed):
|
128 |
# Assuming device is set up (e.g., "cuda" or "cpu")
|
129 |
pipe.to(device)
|
130 |
pipe.unet_encoder.to(device)
|
131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
# Resize and prepare images
|
133 |
garm_img = cloth_img.convert("RGB").resize((768, 1024))
|
134 |
human_img = person_img.convert("RGB").resize((768, 1024))
|
|
|
181 |
garm_tensor = tensor_transfrom(garm_img).unsqueeze(0).to(device, torch.float16)
|
182 |
|
183 |
# Prepare the generator with optional seed
|
184 |
+
generator = torch.Generator(device).manual_seed(seed) if seed is not None else None
|
185 |
+
|
186 |
# Generate the virtual try-on output image
|
187 |
images = pipe(
|
188 |
prompt_embeds=prompt_embeds.to(device, torch.float16),
|
|
|
214 |
with gr.Row():
|
215 |
with gr.Column():
|
216 |
person_img = gr.Image(label='Person Image', sources='upload', type="pil")
|
217 |
+
pose_img = gr.Image(label='Pose Image', sources='upload', type="pil")
|
218 |
mask_img = gr.Image(label='Mask Image', sources='upload', type="pil")
|
219 |
|
220 |
with gr.Column():
|
|
|
223 |
|
224 |
with gr.Column():
|
225 |
denoise_steps = gr.Number(label="Denoising Steps", minimum=20, maximum=40, value=30, step=1)
|
226 |
+
seed = gr.Number(label="Seed", minimum=-1, maximum=2147483647, step=1, value=42)
|
227 |
|
228 |
with gr.Column():
|
229 |
image_out = gr.Image(label="Output Image", elem_id="output-img", show_share_button=False)
|
230 |
|
231 |
try_button = gr.Button(value="Try-on")
|
232 |
+
try_button.click(fn=start_tryon, inputs=[person_img, pose_img, mask_img, cloth_img, garment_des, denoise_steps, seed], outputs=[image_out], api_name='tryon')
|
233 |
|
234 |
image_blocks.launch()
|