Spaces:
Runtime error
Runtime error
hasibzunair
commited on
Commit
β’
9bc6e8f
1
Parent(s):
4758155
put back background in try-on output
Browse files
app.py
CHANGED
@@ -67,8 +67,31 @@ print("########################Setup done!########################")
|
|
67 |
print(f"####Using {device}.#####")
|
68 |
u2net = u2net_load.model(model_name = 'u2netp')
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
# Main inference function
|
71 |
-
def inference(clothing_image, person_image, remove_bg):
|
72 |
"""
|
73 |
Do try-on!
|
74 |
"""
|
@@ -101,6 +124,8 @@ def inference(clothing_image, person_image, remove_bg):
|
|
101 |
img = remove(img, alpha_matting=True, alpha_matting_erode_size=15)
|
102 |
print("Removing background from person image..")
|
103 |
img = ImageOps.fit(img, (192, 256), Image.BICUBIC).convert("RGB")
|
|
|
|
|
104 |
img_path = os.path.join('Data_preprocessing/test_img', img_name)
|
105 |
img.save(img_path)
|
106 |
resize_time = time.time()
|
@@ -126,7 +151,15 @@ def inference(clothing_image, person_image, remove_bg):
|
|
126 |
os.system("python test.py --name decavtonfifapretrain")
|
127 |
tryon_image = Image.open("results/test/try-on/person.png")
|
128 |
print("Size of image is: ", tryon_image.size)
|
129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
|
131 |
|
132 |
title = "Virtual Dressing Room"
|
@@ -136,9 +169,10 @@ thumbnail = None # "./pathtothumbnail.png"
|
|
136 |
|
137 |
gr.Interface(
|
138 |
inference,
|
139 |
-
[gr.inputs.Image(type='filepath', label="Clothing Image"),
|
140 |
gr.inputs.Image(type='filepath', label="Person Image"),
|
141 |
-
gr.inputs.Radio(choices=["yes","no"], default="no", label="Remove background from person image?")
|
|
|
142 |
gr.outputs.Image(type="filepath", label="Predicted Output"),
|
143 |
examples=[["./examples/1/cloth.jpg", "./examples/1/person.jpg"],
|
144 |
["./examples/2/cloth.jpg", "./examples/2/person.jpg"]],
|
|
|
67 |
print(f"####Using {device}.#####")
|
68 |
u2net = u2net_load.model(model_name = 'u2netp')
|
69 |
|
70 |
+
|
71 |
+
def composite_background(img_mask, person_image_path, tryon_image_path):
|
72 |
+
"""Put background back on the person image after tryon."""
|
73 |
+
person = np.array(Image.open(person_image_path))
|
74 |
+
# tryon image
|
75 |
+
tryon = np.array(Image.open(tryon_image_path))
|
76 |
+
# persom image mask from rembg
|
77 |
+
p_mask = np.array(img_mask)
|
78 |
+
# make binary mask
|
79 |
+
p_mask = np.where(p_mask>0, 1, 0)
|
80 |
+
# invert mask
|
81 |
+
p_mask_inv = np.logical_not(p_mask)
|
82 |
+
# make bg without person
|
83 |
+
background = person * np.stack((p_mask_inv, p_mask_inv, p_mask_inv), axis=2)
|
84 |
+
# make tryon image without background
|
85 |
+
tryon_nobg = tryon * np.stack((p_mask, p_mask, p_mask), axis=2)
|
86 |
+
tryon_nobg = tryon_nobg.astype("uint8")
|
87 |
+
# composite
|
88 |
+
tryon_with_bg = np.add(tryon_nobg, background)
|
89 |
+
tryon_with_bg_pil = Image.fromarray(np.uint8(tryon_with_bg)).convert('RGB')
|
90 |
+
tryon_with_bg_pil.save("results/test/try-on/tryon_with_bg.png")
|
91 |
+
|
92 |
+
|
93 |
# Main inference function
|
94 |
+
def inference(clothing_image, person_image, remove_bg, retrieve_bg):
|
95 |
"""
|
96 |
Do try-on!
|
97 |
"""
|
|
|
124 |
img = remove(img, alpha_matting=True, alpha_matting_erode_size=15)
|
125 |
print("Removing background from person image..")
|
126 |
img = ImageOps.fit(img, (192, 256), Image.BICUBIC).convert("RGB")
|
127 |
+
# Get binary from person image, used in def_composite_background
|
128 |
+
img_mask = remove(img, alpha_matting=True, alpha_matting_erode_size=15, only_mask=True)
|
129 |
img_path = os.path.join('Data_preprocessing/test_img', img_name)
|
130 |
img.save(img_path)
|
131 |
resize_time = time.time()
|
|
|
151 |
os.system("python test.py --name decavtonfifapretrain")
|
152 |
tryon_image = Image.open("results/test/try-on/person.png")
|
153 |
print("Size of image is: ", tryon_image.size)
|
154 |
+
|
155 |
+
# Return try-on with background added back on the person image
|
156 |
+
if retrieve_bg == "yes":
|
157 |
+
composite_background(img_mask, 'Data_preprocessing/test_img/person.png',
|
158 |
+
'results/test/try-on/person.png')
|
159 |
+
return os.path.join("results/test/try-on/tryon_with_bg.png")
|
160 |
+
# Return only try-on result
|
161 |
+
else:
|
162 |
+
return os.path.join("results/test/try-on/person.png")
|
163 |
|
164 |
|
165 |
title = "Virtual Dressing Room"
|
|
|
169 |
|
170 |
gr.Interface(
|
171 |
inference,
|
172 |
+
[gr.inputs.Image(type='filepath', label="Clothing Image"),
|
173 |
gr.inputs.Image(type='filepath', label="Person Image"),
|
174 |
+
gr.inputs.Radio(choices=["yes","no"], default="no", label="Remove background from the person image?"),
|
175 |
+
gr.inputs.Radio(choices=["yes","no"], default="no", label="Retrieve original background from the person image?")],
|
176 |
gr.outputs.Image(type="filepath", label="Predicted Output"),
|
177 |
examples=[["./examples/1/cloth.jpg", "./examples/1/person.jpg"],
|
178 |
["./examples/2/cloth.jpg", "./examples/2/person.jpg"]],
|