Spaces:
Build error
Build error
parokshsaxena commited on
Commit ยท
3242b28
1
Parent(s): ce7b1a5
using crop if original image AR is not 2x3
Browse files123 unmodified lines1241251261271281291301311321331341351361371381391401411 unmodified line1431441451461471481491501511 unmodified line15315415515615715815916016117 unmodified lines1791801811821831841855 unmodified lines19119219319419519619784 unmodified lines2822832842852862872882892902912922932942952966 unmodified lines303304305306307308309310123 unmodified linespipe.unet_encoder = UNet_Encoder# Standard size of shein imagesWIDTH = int(4160/5)HEIGHT = int(6240/5)# Standard size on which model is trained#WIDTH = int(768)#HEIGHT = int(1024)POSE_WIDTH = int(WIDTH/2) # int(WIDTH/2)POSE_HEIGHT = int(HEIGHT/2) #int(HEIGHT/2)CATEGORY = "upper_body" # "lower_body"@spaces.GPUdef start_tryon(dict,garm_img,garment_des, background_img, is_checked,is_checked_crop,denoise_steps,seed):#device = "cuda"device = 'cuda:0' if torch.cuda.is_available() else 'cpu'1 unmodified linepipe.to(device)pipe.unet_encoder.to(device)#human_img_orig = dict["background"].convert("RGB") # ImageEditorhuman_img_orig = dict.convert("RGB") # Image# 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 ARWIDTH, HEIGHT = human_img_orig.sizedivision_factor = math.ceil(WIDTH/1000)1 unmodified lineHEIGHT = int(HEIGHT/division_factor)POSE_WIDTH = int(WIDTH/2)POSE_HEIGHT = int(HEIGHT/2)garm_img= garm_img.convert("RGB").resize((WIDTH,HEIGHT))if is_checked_crop:width, height = human_img_orig.sizetarget_width = int(min(width, height * (3 / 4)))target_height = int(min(height, width * (4 / 3)))17 unmodified linesmask = mask.resize((WIDTH, HEIGHT))logging.info("Mask location on model identified")else:mask = pil_to_binary_mask(dict['layers'][0].convert("RGB").resize((WIDTH, HEIGHT)))# mask = transforms.ToTensor()(mask)# mask = mask.unsqueeze(0)mask_gray = (1-transforms.ToTensor()(mask)) * tensor_transfrom(human_img)5 unmodified linesargs = 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'))# verbosity = getattr(args, "verbosity", None)pose_img = args.func(args,human_img_arg)pose_img = pose_img[:,:,::-1]84 unmodified lineshuman_list_path = [os.path.join(example_path,"human",human) for human in human_list]human_ex_list = []human_ex_list = human_list_path # Image""" if using ImageEditor instead of Image while taking input, use this - ImageEditorfor ex_human in human_list_path:ex_dict= {}ex_dict['background'] = ex_humanex_dict['layers'] = Noneex_dict['composite'] = Nonehuman_ex_list.append(ex_dict)"""##default human6 unmodified lineswith gr.Column():# changing from ImageEditor to Image to allow easy passing of data through API# instead of passing {"dictionary": <>} ( which is failing ), we can directly pass the image#imgs = gr.ImageEditor(sources='upload', type="pil", label='Human. Mask with pen or use auto-masking', interactive=True)imgs = gr.Image(sources='upload', type='pil',label='Human. Mask with pen or use auto-masking')with gr.Row():is_checked = gr.Checkbox(label="Yes", info="Use auto-generated mask (Takes 5 seconds)",value=True)with gr.Row():123 unmodified lines1241251261271281291301311321331341351361371381391401411421431441451461471481491 unmodified line1511521531541551561571581591601611621 unmodified line16416516616716816917017117217317417517617717 unmodified lines1951961971981992002015 unmodified lines20720820921021121221384 unmodified lines2982993003013023033043053063073083093103113126 unmodified lines319320321322323324325326123 unmodified linespipe.unet_encoder = UNet_Encoder# Standard size of shein images#WIDTH = int(4160/5)#HEIGHT = int(6240/5)# Standard size on which model is trainedWIDTH = int(768)HEIGHT = int(1024)POSE_WIDTH = int(WIDTH/2) # int(WIDTH/2)POSE_HEIGHT = int(HEIGHT/2) #int(HEIGHT/2)CATEGORY = "upper_body" # "lower_body"def is_cropping_required(width, height):# If aspect ratio is 1.5, which is same as standard 2x3 ( 768x1024 ), then no need to crop, else cropaspect_ratio = round(height/width, 2)if aspect_ratio == 2:return Falsereturn True@spaces.GPUdef start_tryon(human_img_dict,garm_img,garment_des, background_img, is_checked,is_checked_crop,denoise_steps,seed):#device = "cuda"device = 'cuda:0' if torch.cuda.is_available() else 'cpu'1 unmodified linepipe.to(device)pipe.unet_encoder.to(device)if isinstance(human_img_dict, dict):human_img_orig = human_img_dict["background"].convert("RGB") # ImageEditorelse:human_img_orig = dict.convert("RGB") # Image"""# 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 ARWIDTH, HEIGHT = human_img_orig.sizedivision_factor = math.ceil(WIDTH/1000)1 unmodified lineHEIGHT = int(HEIGHT/division_factor)POSE_WIDTH = int(WIDTH/2)POSE_HEIGHT = int(HEIGHT/2)"""# is_checked_crop as True if original AR is not same as 2x3 as expected by modelw, h = human_img_orig.sizeis_checked_crop = is_cropping_required(w, h)garm_img= garm_img.convert("RGB").resize((WIDTH,HEIGHT))if is_checked_crop:# This will crop the image to make it Aspect Ratio of 3 x 4. And then at the end revert it back to original dimentionswidth, height = human_img_orig.sizetarget_width = int(min(width, height * (3 / 4)))target_height = int(min(height, width * (4 / 3)))17 unmodified linesmask = mask.resize((WIDTH, HEIGHT))logging.info("Mask location on model identified")else:mask = pil_to_binary_mask(human_img_dict['layers'][0].convert("RGB").resize((WIDTH, HEIGHT)))# mask = transforms.ToTensor()(mask)# mask = mask.unsqueeze(0)mask_gray = (1-transforms.ToTensor()(mask)) * tensor_transfrom(human_img)5 unmodified linesargs = 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))# verbosity = getattr(args, "verbosity", None)pose_img = args.func(args,human_img_arg)pose_img = pose_img[:,:,::-1]84 unmodified lineshuman_list_path = [os.path.join(example_path,"human",human) for human in human_list]human_ex_list = []#human_ex_list = human_list_path # Image#""" if using ImageEditor instead of Image while taking input, use this - ImageEditorfor ex_human in human_list_path:ex_dict= {}ex_dict['background'] = ex_humanex_dict['layers'] = Noneex_dict['composite'] = Nonehuman_ex_list.append(ex_dict)#"""##default human6 unmodified lineswith gr.Column():# changing from ImageEditor to Image to allow easy passing of data through API# instead of passing {"dictionary": <>} ( which is failing ), we can directly pass the imageimgs = gr.ImageEditor(sources='upload', type="pil", label='Human. Mask with pen or use auto-masking', interactive=True)#imgs = gr.Image(sources='upload', type='pil',label='Human. Mask with pen or use auto-masking')with gr.Row():is_checked = gr.Checkbox(label="Yes", info="Use auto-generated mask (Takes 5 seconds)",value=True)with gr.Row():
app.py+30-14