Spaces:
Running
on
Zero
Running
on
Zero
parokshsaxena
commited on
Commit
β’
3242b28
1
Parent(s):
ce7b1a5
using crop if original image AR is not 2x3
Browse files
app.py
CHANGED
@@ -124,18 +124,26 @@ pipe = TryonPipeline.from_pretrained(
|
|
124 |
pipe.unet_encoder = UNet_Encoder
|
125 |
|
126 |
# Standard size of shein images
|
127 |
-
WIDTH = int(4160/5)
|
128 |
-
HEIGHT = int(6240/5)
|
129 |
# Standard size on which model is trained
|
130 |
-
|
131 |
-
|
132 |
POSE_WIDTH = int(WIDTH/2) # int(WIDTH/2)
|
133 |
POSE_HEIGHT = int(HEIGHT/2) #int(HEIGHT/2)
|
134 |
|
135 |
CATEGORY = "upper_body" # "lower_body"
|
136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
@spaces.GPU
|
138 |
-
def start_tryon(
|
139 |
#device = "cuda"
|
140 |
device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
|
141 |
|
@@ -143,9 +151,12 @@ def start_tryon(dict,garm_img,garment_des, background_img, is_checked,is_checked
|
|
143 |
pipe.to(device)
|
144 |
pipe.unet_encoder.to(device)
|
145 |
|
146 |
-
|
147 |
-
|
|
|
|
|
148 |
|
|
|
149 |
# Derive HEIGHT & WIDTH such that width is not more than 1000. This will cater to both Shein images (4160x6240) of 3:4 AR and model standard images ( 768x1024 ) of 2:3 AR
|
150 |
WIDTH, HEIGHT = human_img_orig.size
|
151 |
division_factor = math.ceil(WIDTH/1000)
|
@@ -153,9 +164,14 @@ def start_tryon(dict,garm_img,garment_des, background_img, is_checked,is_checked
|
|
153 |
HEIGHT = int(HEIGHT/division_factor)
|
154 |
POSE_WIDTH = int(WIDTH/2)
|
155 |
POSE_HEIGHT = int(HEIGHT/2)
|
|
|
|
|
|
|
|
|
156 |
|
157 |
garm_img= garm_img.convert("RGB").resize((WIDTH,HEIGHT))
|
158 |
if is_checked_crop:
|
|
|
159 |
width, height = human_img_orig.size
|
160 |
target_width = int(min(width, height * (3 / 4)))
|
161 |
target_height = int(min(height, width * (4 / 3)))
|
@@ -179,7 +195,7 @@ def start_tryon(dict,garm_img,garment_des, background_img, is_checked,is_checked
|
|
179 |
mask = mask.resize((WIDTH, HEIGHT))
|
180 |
logging.info("Mask location on model identified")
|
181 |
else:
|
182 |
-
mask = pil_to_binary_mask(
|
183 |
# mask = transforms.ToTensor()(mask)
|
184 |
# mask = mask.unsqueeze(0)
|
185 |
mask_gray = (1-transforms.ToTensor()(mask)) * tensor_transfrom(human_img)
|
@@ -191,7 +207,7 @@ def start_tryon(dict,garm_img,garment_des, background_img, is_checked,is_checked
|
|
191 |
|
192 |
|
193 |
|
194 |
-
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',
|
195 |
# verbosity = getattr(args, "verbosity", None)
|
196 |
pose_img = args.func(args,human_img_arg)
|
197 |
pose_img = pose_img[:,:,::-1]
|
@@ -282,15 +298,15 @@ human_list = os.listdir(os.path.join(example_path,"human"))
|
|
282 |
human_list_path = [os.path.join(example_path,"human",human) for human in human_list]
|
283 |
|
284 |
human_ex_list = []
|
285 |
-
human_ex_list = human_list_path # Image
|
286 |
-
""" if using ImageEditor instead of Image while taking input, use this - ImageEditor
|
287 |
for ex_human in human_list_path:
|
288 |
ex_dict= {}
|
289 |
ex_dict['background'] = ex_human
|
290 |
ex_dict['layers'] = None
|
291 |
ex_dict['composite'] = None
|
292 |
human_ex_list.append(ex_dict)
|
293 |
-
"""
|
294 |
##default human
|
295 |
|
296 |
|
@@ -303,8 +319,8 @@ with image_blocks as demo:
|
|
303 |
with gr.Column():
|
304 |
# changing from ImageEditor to Image to allow easy passing of data through API
|
305 |
# instead of passing {"dictionary": <>} ( which is failing ), we can directly pass the image
|
306 |
-
|
307 |
-
imgs = gr.Image(sources='upload', type='pil',label='Human. Mask with pen or use auto-masking')
|
308 |
with gr.Row():
|
309 |
is_checked = gr.Checkbox(label="Yes", info="Use auto-generated mask (Takes 5 seconds)",value=True)
|
310 |
with gr.Row():
|
|
|
124 |
pipe.unet_encoder = UNet_Encoder
|
125 |
|
126 |
# Standard size of shein images
|
127 |
+
#WIDTH = int(4160/5)
|
128 |
+
#HEIGHT = int(6240/5)
|
129 |
# Standard size on which model is trained
|
130 |
+
WIDTH = int(768)
|
131 |
+
HEIGHT = int(1024)
|
132 |
POSE_WIDTH = int(WIDTH/2) # int(WIDTH/2)
|
133 |
POSE_HEIGHT = int(HEIGHT/2) #int(HEIGHT/2)
|
134 |
|
135 |
CATEGORY = "upper_body" # "lower_body"
|
136 |
|
137 |
+
def is_cropping_required(width, height):
|
138 |
+
# If aspect ratio is 1.5, which is same as standard 2x3 ( 768x1024 ), then no need to crop, else crop
|
139 |
+
aspect_ratio = round(height/width, 2)
|
140 |
+
if aspect_ratio == 2:
|
141 |
+
return False
|
142 |
+
return True
|
143 |
+
|
144 |
+
|
145 |
@spaces.GPU
|
146 |
+
def start_tryon(human_img_dict,garm_img,garment_des, background_img, is_checked,is_checked_crop,denoise_steps,seed):
|
147 |
#device = "cuda"
|
148 |
device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
|
149 |
|
|
|
151 |
pipe.to(device)
|
152 |
pipe.unet_encoder.to(device)
|
153 |
|
154 |
+
if isinstance(human_img_dict, dict):
|
155 |
+
human_img_orig = human_img_dict["background"].convert("RGB") # ImageEditor
|
156 |
+
else:
|
157 |
+
human_img_orig = dict.convert("RGB") # Image
|
158 |
|
159 |
+
"""
|
160 |
# Derive HEIGHT & WIDTH such that width is not more than 1000. This will cater to both Shein images (4160x6240) of 3:4 AR and model standard images ( 768x1024 ) of 2:3 AR
|
161 |
WIDTH, HEIGHT = human_img_orig.size
|
162 |
division_factor = math.ceil(WIDTH/1000)
|
|
|
164 |
HEIGHT = int(HEIGHT/division_factor)
|
165 |
POSE_WIDTH = int(WIDTH/2)
|
166 |
POSE_HEIGHT = int(HEIGHT/2)
|
167 |
+
"""
|
168 |
+
# is_checked_crop as True if original AR is not same as 2x3 as expected by model
|
169 |
+
w, h = human_img_orig.size
|
170 |
+
is_checked_crop = is_cropping_required(w, h)
|
171 |
|
172 |
garm_img= garm_img.convert("RGB").resize((WIDTH,HEIGHT))
|
173 |
if is_checked_crop:
|
174 |
+
# This will crop the image to make it Aspect Ratio of 3 x 4. And then at the end revert it back to original dimentions
|
175 |
width, height = human_img_orig.size
|
176 |
target_width = int(min(width, height * (3 / 4)))
|
177 |
target_height = int(min(height, width * (4 / 3)))
|
|
|
195 |
mask = mask.resize((WIDTH, HEIGHT))
|
196 |
logging.info("Mask location on model identified")
|
197 |
else:
|
198 |
+
mask = pil_to_binary_mask(human_img_dict['layers'][0].convert("RGB").resize((WIDTH, HEIGHT)))
|
199 |
# mask = transforms.ToTensor()(mask)
|
200 |
# mask = mask.unsqueeze(0)
|
201 |
mask_gray = (1-transforms.ToTensor()(mask)) * tensor_transfrom(human_img)
|
|
|
207 |
|
208 |
|
209 |
|
210 |
+
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', device))
|
211 |
# verbosity = getattr(args, "verbosity", None)
|
212 |
pose_img = args.func(args,human_img_arg)
|
213 |
pose_img = pose_img[:,:,::-1]
|
|
|
298 |
human_list_path = [os.path.join(example_path,"human",human) for human in human_list]
|
299 |
|
300 |
human_ex_list = []
|
301 |
+
#human_ex_list = human_list_path # Image
|
302 |
+
#""" if using ImageEditor instead of Image while taking input, use this - ImageEditor
|
303 |
for ex_human in human_list_path:
|
304 |
ex_dict= {}
|
305 |
ex_dict['background'] = ex_human
|
306 |
ex_dict['layers'] = None
|
307 |
ex_dict['composite'] = None
|
308 |
human_ex_list.append(ex_dict)
|
309 |
+
#"""
|
310 |
##default human
|
311 |
|
312 |
|
|
|
319 |
with gr.Column():
|
320 |
# changing from ImageEditor to Image to allow easy passing of data through API
|
321 |
# instead of passing {"dictionary": <>} ( which is failing ), we can directly pass the image
|
322 |
+
imgs = gr.ImageEditor(sources='upload', type="pil", label='Human. Mask with pen or use auto-masking', interactive=True)
|
323 |
+
#imgs = gr.Image(sources='upload', type='pil',label='Human. Mask with pen or use auto-masking')
|
324 |
with gr.Row():
|
325 |
is_checked = gr.Checkbox(label="Yes", info="Use auto-generated mask (Takes 5 seconds)",value=True)
|
326 |
with gr.Row():
|