Spaces:
Runtime error
Runtime error
Update app.py
Browse files65 unmodified lines66676869707172737475767778798081828384858687882 unmodified lines9192939495969798991003 unmodified lines10410510610710810911011111212 unmodified lines12512612712812913013113232 unmodified lines16516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721811 unmodified lines2302312322332342352362372382392402412421 unmodified line24424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630765 unmodified lines[255, 0, 255], [255, 0, 170], [255, 0, 85], [255, 0, 0]]kp_src = []skeleton = []count = 0color_idx = 0prev_pt = Noneprev_pt_idx = Noneprev_clicked = Noneoriginal_support_image = Nonecheckpoint_path = ''def process(query_img,cfg_path='configs/demo_b.py'):global skeletoncfg = Config.fromfile(cfg_path)kp_src_np = np.array(kp_src).copy().astype(np.float32)kp_src_np[:, 0] = kp_src_np[:, 0] / 128. * cfg.model.encoder_config.img_sizekp_src_np[:, 1] = kp_src_np[:, 1] / 128. * cfg.model.encoder_config.img_sizekp_src_np = np.flip(kp_src_np, 1).copy()kp_src_tensor = torch.tensor(kp_src_np).float()preprocess = transforms.Compose([2 unmodified linesResize_Pad(cfg.model.encoder_config.img_size,cfg.model.encoder_config.img_size)])if len(skeleton) == 0:skeleton = [(0, 0)]support_img = preprocess(original_support_image).flip(0)[None]np_query = np.array(query_img)[:, :, ::-1].copy()q_img = preprocess(np_query).flip(0)[None]# Create heatmap from keypoints3 unmodified linescfg.model.encoder_config.img_size])data_cfg['joint_weights'] = Nonedata_cfg['use_different_joint_weights'] = Falsekp_src_3d = torch.cat((kp_src_tensor, torch.zeros(kp_src_tensor.shape[0], 1)), dim=-1)kp_src_3d_weight = torch.cat((torch.ones_like(kp_src_tensor),torch.zeros(kp_src_tensor.shape[0], 1)), dim=-1)target_s, target_weight_s = genHeatMap._msra_generate_target(data_cfg,12 unmodified lines'target_q': None,'target_weight_q': None,'return_loss': False,'img_metas': [{'sample_skeleton': [skeleton],'query_skeleton': skeleton,'sample_joints_3d': [kp_src_3d],'query_joints_3d': kp_src_3d,'sample_center': [kp_src_tensor.mean(dim=0)],32 unmodified linesvis_s_weight,None,vis_q_weight,skeleton,None,torch.tensor(outputs['points']).squeeze(0),)return outwith gr.Blocks() as demo:gr.Markdown('''# Pose Anything DemoWe present a novel approach to category agnostic pose estimation that leverages the inherent geometrical relations between keypoints through a newly designed Graph Transformer Decoder. By capturing and incorporating this crucial structural information, our method enhances the accuracy of keypoint localization, marking a significant departure from conventional CAPE techniques that treat keypoints as isolated entities.### [Paper](https://arxiv.org/abs/2311.17891) | [Official Repo](https://github.com/orhir/PoseAnything)## Instructions1. Upload an image of the object you want to pose on the **left** image.2. Click on the **left** image to mark keypoints.3. Click on the keypoints on the **right** image to mark limbs.4. Upload an image of the object you want to pose to the query image (**bottom**).5. Click **Evaluate** to pose the query image.''')with gr.Row():support_img = gr.Image(label="Support Image",type="pil",info='Click to mark keypoints').style(height=256, width=256)posed_support = gr.Image(label="Posed Support Image",type="pil",interactive=False).style(height=256, width=256)with gr.Row():query_img = gr.Image(label="Query Image",type="pil").style(height=256, width=256)with gr.Row():eval_btn = gr.Button(value="Evaluate")with gr.Row():output_img = gr.Plot(label="Output Image", height=256, width=256)def get_select_coords(kp_support,limb_support,evt: gr.SelectData,r=0.015):pixels_in_queue = set()pixels_in_queue.add((evt.index[1], evt.index[0]))while len(pixels_in_queue) > 0:pixel = pixels_in_queue.pop()if pixel[0] is not None and pixel[1] is not None and pixel not in kp_src:kp_src.append(pixel)else:print("Invalid pixel")if limb_support is None:11 unmodified linesdraw_pose.ellipse(twoPointList, fill=(255, 0, 0, 255))draw_limb.ellipse(twoPointList, fill=(255, 0, 0, 255))return canvas_kp, canvas_limbdef get_limbs(kp_support,evt: gr.SelectData,r=0.02, width=0.02):global count, color_idx, prev_pt, skeleton, prev_pt_idx, prev_clickedcurr_pixel = (evt.index[1], evt.index[0])pixels_in_queue = set()pixels_in_queue.add((evt.index[1], evt.index[0]))1 unmodified linew, h = canvas_kp.sizer = int(r * w)width = int(width * w)while (len(pixels_in_queue) > 0 andcurr_pixel != prev_clicked andlen(kp_src) > 0):pixel = pixels_in_queue.pop()prev_clicked = pixelclosest_point = min(kp_src,key=lambda p: (p[0] - pixel[0]) ** 2 +(p[1] - pixel[1]) ** 2)closest_point_index = kp_src.index(closest_point)draw_limb = ImageDraw.Draw(canvas_kp)if color_idx < len(COLORS):c = COLORS[color_idx]else:c = random.choices(range(256), k=3)leftUpPoint = (closest_point[1] - r, closest_point[0] - r)rightDownPoint = (closest_point[1] + r, closest_point[0] + r)twoPointList = [leftUpPoint, rightDownPoint]draw_limb.ellipse(twoPointList, fill=tuple(c))if count == 0:prev_pt = closest_point[1], closest_point[0]prev_pt_idx = closest_point_indexcount = count + 1else:if prev_pt_idx != closest_point_index:# Create Line and add Limbdraw_limb.line([prev_pt, (closest_point[1], closest_point[0])],fill=tuple(c),width=width)skeleton.append((prev_pt_idx, closest_point_index))color_idx = color_idx + 1else:draw_limb.ellipse(twoPointList, fill=(255, 0, 0, 255))count = 0return canvas_kpdef set_query(support_img):global original_support_imageskeleton.clear()kp_src.clear()original_support_image = np.array(support_img)[:, :, ::-1].copy()support_img = support_img.resize((128, 128), Image.Resampling.LANCZOS)return support_img, support_imgsupport_img.select(get_select_coords,[support_img, posed_support],[support_img, posed_support],)support_img.upload(set_query,inputs=support_img,outputs=[support_img,posed_support])posed_support.select(get_limbs,posed_support,posed_support)eval_btn.click(fn=process,inputs=[query_img],outputs=output_img)if __name__ == "__main__":parser = argparse.ArgumentParser(description='Pose Anything Demo')65 unmodified lines66676869707172737475767778792 unmodified lines828384858687888990913 unmodified lines959697989910010110210312 unmodified lines11611711811912012112212332 unmodified lines15615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123211 unmodified lines2442452462472482492502512522532542552561 unmodified line25825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831965 unmodified lines[255, 0, 255], [255, 0, 170], [255, 0, 85], [255, 0, 0]]def process(query_img, state,cfg_path='configs/demo_b.py'):cfg = Config.fromfile(cfg_path)kp_src_np = np.array(state['kp_src']).copy().astype(np.float32)kp_src_np[:, 0] = kp_src_np[:,0] / 128. * cfg.model.encoder_config.img_sizekp_src_np[:, 1] = kp_src_np[:,1] / 128. * cfg.model.encoder_config.img_sizekp_src_np = np.flip(kp_src_np, 1).copy()kp_src_tensor = torch.tensor(kp_src_np).float()preprocess = transforms.Compose([2 unmodified linesResize_Pad(cfg.model.encoder_config.img_size,cfg.model.encoder_config.img_size)])if len(state['skeleton']) == 0:skeleton = [(0, 0)]support_img = preprocess(state['original_support_image']).flip(0)[None]np_query = np.array(query_img)[:, :, ::-1].copy()q_img = preprocess(np_query).flip(0)[None]# Create heatmap from keypoints3 unmodified linescfg.model.encoder_config.img_size])data_cfg['joint_weights'] = Nonedata_cfg['use_different_joint_weights'] = Falsekp_src_3d = torch.concatenate((kp_src_tensor, torch.zeros(kp_src_tensor.shape[0], 1)), dim=-1)kp_src_3d_weight = torch.concatenate((torch.ones_like(kp_src_tensor),torch.zeros(kp_src_tensor.shape[0], 1)), dim=-1)target_s, target_weight_s = genHeatMap._msra_generate_target(data_cfg,12 unmodified lines'target_q': None,'target_weight_q': None,'return_loss': False,'img_metas': [{'sample_skeleton': [state['skeleton']],'query_skeleton': state['skeleton'],'sample_joints_3d': [kp_src_3d],'query_joints_3d': kp_src_3d,'sample_center': [kp_src_tensor.mean(dim=0)],32 unmodified linesvis_s_weight,None,vis_q_weight,state['skeleton'],None,torch.tensor(outputs['points']).squeeze(0),)return out, statewith gr.Blocks() as demo:state = gr.State({'kp_src': [],'skeleton': [],'count': 0,'color_idx': 0,'prev_pt': None,'prev_pt_idx': None,'prev_clicked': None,'original_support_image': None,})gr.Markdown('''# Pose Anything DemoWe present a novel approach to category agnostic pose estimation thatleverages the inherent geometrical relations between keypoints through anewly designed Graph Transformer Decoder. By capturing and incorporatingthis crucial structural information, our method enhances the accuracy ofkeypoint localization, marking a significant departure from conventionalCAPE techniques that treat keypoints as isolated entities.### [Paper](https://arxiv.org/abs/2311.17891) | [Official Repo](https://github.com/orhir/PoseAnything)## Instructions1. Upload an image of the object you want to pose on the **left** image.2. Click on the **left** image to mark keypoints.3. Click on the keypoints on the **right** image to mark limbs.4. Upload an image of the object you want to pose to the query image (**bottom**).5. Click **Evaluate** to pose the query image.''')with gr.Row():support_img = gr.Image(label="Support Image",type="pil",info='Click to mark keypoints').style(height=400, width=400)posed_support = gr.Image(label="Posed Support Image",type="pil",interactive=False).style(height=400,width=400)with gr.Row():query_img = gr.Image(label="Query Image",type="pil").style(height=400, width=400)with gr.Row():eval_btn = gr.Button(value="Evaluate")with gr.Row():output_img = gr.Plot(label="Output Image", height=400, width=400)def get_select_coords(kp_support,limb_support,state,evt: gr.SelectData,r=0.015):# global original_support_image# if len(kp_src) == 0:# original_support_image = np.array(kp_support)[:, :,# ::-1].copy()pixels_in_queue = set()pixels_in_queue.add((evt.index[1], evt.index[0]))while len(pixels_in_queue) > 0:pixel = pixels_in_queue.pop()if pixel[0] is not None and pixel[1] is not None and pixel not in state['kp_src']:state['kp_src'].append(pixel)else:print("Invalid pixel")if limb_support is None:11 unmodified linesdraw_pose.ellipse(twoPointList, fill=(255, 0, 0, 255))draw_limb.ellipse(twoPointList, fill=(255, 0, 0, 255))return canvas_kp, canvas_limb, statedef get_limbs(kp_support,state,evt: gr.SelectData,r=0.02, width=0.02):curr_pixel = (evt.index[1], evt.index[0])pixels_in_queue = set()pixels_in_queue.add((evt.index[1], evt.index[0]))1 unmodified linew, h = canvas_kp.sizer = int(r * w)width = int(width * w)while len(pixels_in_queue) > 0 and curr_pixel != state['prev_clicked']:pixel = pixels_in_queue.pop()state['prev_clicked'] = pixelclosest_point = min(state['kp_src'],key=lambda p: (p[0] - pixel[0]) ** 2 +(p[1] - pixel[1]) ** 2)closest_point_index = state['kp_src'].index(closest_point)draw_limb = ImageDraw.Draw(canvas_kp)if state['color_idx'] < len(COLORS):c = COLORS[state['color_idx']]else:c = random.choices(range(256), k=3)leftUpPoint = (closest_point[1] - r, closest_point[0] - r)rightDownPoint = (closest_point[1] + r, closest_point[0] + r)twoPointList = [leftUpPoint, rightDownPoint]draw_limb.ellipse(twoPointList, fill=tuple(c))if state['count'] == 0:state['prev_pt'] = closest_point[1], closest_point[0]state['prev_pt_idx'] = closest_point_indexstate['count'] = state['count'] + 1else:if state['prev_pt_idx'] != closest_point_index:# Create Line and add Limbdraw_limb.line([state['prev_pt'], (closest_point[1], closest_point[0])],fill=tuple(c),width=width)state['skeleton'].append((state['prev_pt_idx'], closest_point_index))state['color_idx'] = state['color_idx'] + 1else:draw_limb.ellipse(twoPointList, fill=(255, 0, 0, 255))state['count'] = 0return canvas_kp, statedef set_qery(support_img, state):state['skeleton'].clear()state['kp_src'].clear()state['original_support_image'] = np.array(support_img)[:, :, ::-1].copy()support_img = support_img.resize((128, 128), Image.Resampling.LANCZOS)return support_img, support_img, statesupport_img.select(get_select_coords,[support_img, posed_support, state],[support_img, posed_support, state])support_img.upload(set_qery,inputs=[support_img, state],outputs=[support_img, posed_support, state])posed_support.select(get_limbs,[posed_support, state],[posed_support, state])eval_btn.click(fn=process,inputs=[query_img, state],outputs=[output_img, state])if __name__ == "__main__":parser = argparse.ArgumentParser(description='Pose Anything Demo')
app.py+83-71