Spaces:
Runtime error
Runtime error
Update run/app.py
Browse files- run/app.py +126 -1
run/app.py
CHANGED
|
@@ -1,4 +1,118 @@
|
|
| 1 |
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
app = FastAPI()
|
| 4 |
|
|
@@ -11,4 +125,15 @@ def hello():
|
|
| 11 |
"""
|
| 12 |
Hi!
|
| 13 |
"""
|
| 14 |
-
return {"From": "Luwi"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
import os
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
import sys
|
| 5 |
+
import torch
|
| 6 |
+
from PIL import Image, ImageOps
|
| 7 |
+
|
| 8 |
+
from utils_ootd import get_mask_location
|
| 9 |
+
|
| 10 |
+
PROJECT_ROOT = Path(__file__).absolute().parents[1].absolute()
|
| 11 |
+
sys.path.insert(0, str(PROJECT_ROOT))
|
| 12 |
+
|
| 13 |
+
from preprocess.openpose.run_openpose import OpenPose
|
| 14 |
+
from preprocess.humanparsing.run_parsing import Parsing
|
| 15 |
+
from ootd.inference_ootd_hd import OOTDiffusionHD
|
| 16 |
+
from ootd.inference_ootd_dc import OOTDiffusionDC
|
| 17 |
+
|
| 18 |
+
openpose_model_hd = OpenPose(0)
|
| 19 |
+
parsing_model_hd = Parsing(0)
|
| 20 |
+
ootd_model_hd = OOTDiffusionHD(0)
|
| 21 |
+
|
| 22 |
+
openpose_model_dc = OpenPose(1)
|
| 23 |
+
parsing_model_dc = Parsing(1)
|
| 24 |
+
ootd_model_dc = OOTDiffusionDC(1)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
category_dict = ['upperbody', 'lowerbody', 'dress']
|
| 28 |
+
category_dict_utils = ['upper_body', 'lower_body', 'dresses']
|
| 29 |
+
|
| 30 |
+
example_path = os.path.join(os.path.dirname(__file__), 'examples')
|
| 31 |
+
model_hd = os.path.join(example_path, 'model/model_1.png')
|
| 32 |
+
garment_hd = os.path.join(example_path, 'garment/03244_00.jpg')
|
| 33 |
+
model_dc = os.path.join(example_path, 'model/model_8.png')
|
| 34 |
+
garment_dc = os.path.join(example_path, 'garment/048554_1.jpg')
|
| 35 |
+
|
| 36 |
+
import spaces
|
| 37 |
+
|
| 38 |
+
@spaces.GPU
|
| 39 |
+
def process_hd(vton_img, garm_img, n_samples, n_steps, image_scale, seed):
|
| 40 |
+
model_type = 'hd'
|
| 41 |
+
category = 0 # 0:upperbody; 1:lowerbody; 2:dress
|
| 42 |
+
|
| 43 |
+
with torch.no_grad():
|
| 44 |
+
openpose_model_hd.preprocessor.body_estimation.model.to('cuda')
|
| 45 |
+
ootd_model_hd.pipe.to('cuda')
|
| 46 |
+
ootd_model_hd.image_encoder.to('cuda')
|
| 47 |
+
ootd_model_hd.text_encoder.to('cuda')
|
| 48 |
+
|
| 49 |
+
garm_img = Image.open(garm_img).resize((768, 1024))
|
| 50 |
+
vton_img = Image.open(vton_img).resize((768, 1024))
|
| 51 |
+
keypoints = openpose_model_hd(vton_img.resize((384, 512)))
|
| 52 |
+
model_parse, _ = parsing_model_hd(vton_img.resize((384, 512)))
|
| 53 |
+
|
| 54 |
+
mask, mask_gray = get_mask_location(model_type, category_dict_utils[category], model_parse, keypoints)
|
| 55 |
+
mask = mask.resize((768, 1024), Image.NEAREST)
|
| 56 |
+
mask_gray = mask_gray.resize((768, 1024), Image.NEAREST)
|
| 57 |
+
|
| 58 |
+
masked_vton_img = Image.composite(mask_gray, vton_img, mask)
|
| 59 |
+
|
| 60 |
+
images = ootd_model_hd(
|
| 61 |
+
model_type=model_type,
|
| 62 |
+
category=category_dict[category],
|
| 63 |
+
image_garm=garm_img,
|
| 64 |
+
image_vton=masked_vton_img,
|
| 65 |
+
mask=mask,
|
| 66 |
+
image_ori=vton_img,
|
| 67 |
+
num_samples=n_samples,
|
| 68 |
+
num_steps=n_steps,
|
| 69 |
+
image_scale=image_scale,
|
| 70 |
+
seed=seed,
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
return images
|
| 74 |
+
|
| 75 |
+
@spaces.GPU
|
| 76 |
+
def process_dc(vton_img, garm_img, category, n_samples, n_steps, image_scale, seed):
|
| 77 |
+
model_type = 'dc'
|
| 78 |
+
if category == 'Upper-body':
|
| 79 |
+
category = 0
|
| 80 |
+
elif category == 'Lower-body':
|
| 81 |
+
category = 1
|
| 82 |
+
else:
|
| 83 |
+
category =2
|
| 84 |
+
|
| 85 |
+
with torch.no_grad():
|
| 86 |
+
openpose_model_dc.preprocessor.body_estimation.model.to('cuda')
|
| 87 |
+
ootd_model_dc.pipe.to('cuda')
|
| 88 |
+
ootd_model_dc.image_encoder.to('cuda')
|
| 89 |
+
ootd_model_dc.text_encoder.to('cuda')
|
| 90 |
+
|
| 91 |
+
garm_img = Image.open(garm_img).resize((768, 1024))
|
| 92 |
+
vton_img = Image.open(vton_img).resize((768, 1024))
|
| 93 |
+
keypoints = openpose_model_dc(vton_img.resize((384, 512)))
|
| 94 |
+
model_parse, _ = parsing_model_dc(vton_img.resize((384, 512)))
|
| 95 |
+
|
| 96 |
+
mask, mask_gray = get_mask_location(model_type, category_dict_utils[category], model_parse, keypoints)
|
| 97 |
+
mask = mask.resize((768, 1024), Image.NEAREST)
|
| 98 |
+
mask_gray = mask_gray.resize((768, 1024), Image.NEAREST)
|
| 99 |
+
|
| 100 |
+
masked_vton_img = Image.composite(mask_gray, vton_img, mask)
|
| 101 |
+
|
| 102 |
+
images = ootd_model_dc(
|
| 103 |
+
model_type=model_type,
|
| 104 |
+
category=category_dict[category],
|
| 105 |
+
image_garm=garm_img,
|
| 106 |
+
image_vton=masked_vton_img,
|
| 107 |
+
mask=mask,
|
| 108 |
+
image_ori=vton_img,
|
| 109 |
+
num_samples=n_samples,
|
| 110 |
+
num_steps=n_steps,
|
| 111 |
+
image_scale=image_scale,
|
| 112 |
+
seed=seed,
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
return images
|
| 116 |
|
| 117 |
app = FastAPI()
|
| 118 |
|
|
|
|
| 125 |
"""
|
| 126 |
Hi!
|
| 127 |
"""
|
| 128 |
+
return {"From": "Luwi"}
|
| 129 |
+
|
| 130 |
+
@app.post("/test")
|
| 131 |
+
def test():
|
| 132 |
+
vimg = file("https://levihsu-ootdiffusion.hf.space/--replicas/1b6rr/file=/tmp/gradio/2e0cca23e744c036b3905c4b6167371632942e1c/model_1.png")
|
| 133 |
+
gimg = file("https://levihsu-ootdiffusion.hf.space/--replicas/1b6rr/file=/tmp/gradio/31c958b21068795c7a90552fc6dc123282b4c7ab/00126_00.jpg")
|
| 134 |
+
category = "Upper-body"
|
| 135 |
+
n_samples = 1
|
| 136 |
+
n_steps = 20
|
| 137 |
+
image_scale = 1
|
| 138 |
+
seed = -1
|
| 139 |
+
return process_dc(vimg, gimg, category, n_samples, n_steps, image_scale, seed)
|