init project
Browse files38 unmodified lines394041424344454639 unmodified lines86878889909192153 unmodified lines24624724824925025147 unmodified lines299300301302303304138 unmodified lines44344444544644744874 unmodified lines52352452552652752852953053127 unmodified lines55956056156256356456528 unmodified lines59459559659759859960060160260360417 unmodified lines62262362462562662762862963063163263363463563663763863964064164264364464564664764864965065138 unmodified linessilent = Falsedevice = 'cuda' if torch.cuda.is_available() else 'cpu'pe3r = Models(device)def _convert_scene_output_to_glb(outdir, imgs, pts3d, mask, focals, cams2world, cam_size=0.05,39 unmodified linesscene.export(file_obj=outfile)return outfile@spaces.GPU(duration=180)def get_3D_model_from_scene(outdir, scene, min_conf_thr=3, as_pointcloud=False, mask_sky=False,clean_depth=False, transparent_cams=False, cam_size=0.05):"""153 unmodified lines@torch.no_graddef get_mask_from_img_sam1(mobilesamv2, yolov8, sam1_image, yolov8_image, original_size, input_size, transform):sam_mask=[]img_area = original_size[0] * original_size[1]47 unmodified lines@torch.no_graddef get_cog_feats(images):cog_seg_maps = []rev_cog_seg_maps = []inference_state = pe3r.sam2.init_state(images=images.sam2_images, video_height=images.sam2_video_size[0], video_width=images.sam2_video_size[1])138 unmodified lines"""if len(filelist) < 2:raise gradio.Error("Please input at least 2 images.")images = Images(filelist=filelist, device=device)74 unmodified linesdef get_3D_object_from_scene(outdir, text, threshold, scene, min_conf_thr, as_pointcloud,mask_sky, clean_depth, transparent_cams, cam_size):texts = [text]inputs = pe3r.siglip_tokenizer(text=texts, padding="max_length", return_tensors="pt")inputs = {key: value.to('cuda') for key, value in inputs.items()}with torch.no_grad():text_feats =pe3r.siglip.get_text_features(**inputs)text_feats = text_feats / text_feats.norm(dim=-1, keepdim=True)27 unmodified lineswith tempfile.TemporaryDirectory(suffix='pe3r_gradio_demo') as tmpdirname:recon_fun = functools.partial(get_reconstructed_scene, tmpdirname)model_from_scene_fun = functools.partial(get_3D_model_from_scene, tmpdirname)get_3D_object_from_scene_fun = functools.partial(get_3D_object_from_scene, tmpdirname)with gradio.Blocks(css=""".gradio-container {margin: 0 !important; min-width: 100%};""", title="PE3R Demo") as demo:28 unmodified lines# adjust the camera size in the output pointcloudcam_size = gradio.Slider(label="cam_size", value=0.05, minimum=0.001, maximum=0.1, step=0.001, visible=False)with gradio.Row():as_pointcloud = gradio.Checkbox(value=True, label="As pointcloud")# two post process implementedmask_sky = gradio.Checkbox(value=False, label="Mask sky", visible=False)clean_depth = gradio.Checkbox(value=True, label="Clean-up depthmaps", visible=False)transparent_cams = gradio.Checkbox(value=True, label="Transparent cameras")with gradio.Row():text_input = gradio.Textbox(label="Query Text")17 unmodified linesmask_sky, clean_depth, transparent_cams, cam_size,scenegraph_type, winsize, refid],outputs=[scene, outmodel]) # , outgallerymin_conf_thr.release(fn=model_from_scene_fun,inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,clean_depth, transparent_cams, cam_size],outputs=outmodel)cam_size.change(fn=model_from_scene_fun,inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,clean_depth, transparent_cams, cam_size],outputs=outmodel)as_pointcloud.change(fn=model_from_scene_fun,inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,clean_depth, transparent_cams, cam_size],outputs=outmodel)mask_sky.change(fn=model_from_scene_fun,inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,clean_depth, transparent_cams, cam_size],outputs=outmodel)clean_depth.change(fn=model_from_scene_fun,inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,clean_depth, transparent_cams, cam_size],outputs=outmodel)transparent_cams.change(model_from_scene_fun,inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,clean_depth, transparent_cams, cam_size],outputs=outmodel)find_btn.click(fn=get_3D_object_from_scene_fun,inputs=[text_input, threshold, scene, min_conf_thr, as_pointcloud, mask_sky,clean_depth, transparent_cams, cam_size],38 unmodified lines3940414243444539 unmodified lines85868788899091153 unmodified lines24524624724824925025125247 unmodified lines300301302303304305306138 unmodified lines44544644744844945045145274 unmodified lines52752852953053153253353453553653727 unmodified lines56556656756856957057128 unmodified lines60060160260360460560660760860961017 unmodified lines62862963063163263363463563663763863964064164264364464564664764864965065165265365465565665738 unmodified linessilent = Falsepe3r = Models('cpu')def _convert_scene_output_to_glb(outdir, imgs, pts3d, mask, focals, cams2world, cam_size=0.05,39 unmodified linesscene.export(file_obj=outfile)return outfile# @spaces.GPU(duration=180)def get_3D_model_from_scene(outdir, scene, min_conf_thr=3, as_pointcloud=False, mask_sky=False,clean_depth=False, transparent_cams=False, cam_size=0.05):"""153 unmodified lines@torch.no_graddef get_mask_from_img_sam1(mobilesamv2, yolov8, sam1_image, yolov8_image, original_size, input_size, transform):device = 'cuda' if torch.cuda.is_available() else 'cpu'sam_mask=[]img_area = original_size[0] * original_size[1]47 unmodified lines@torch.no_graddef get_cog_feats(images):device = 'cuda' if torch.cuda.is_available() else 'cpu'cog_seg_maps = []rev_cog_seg_maps = []inference_state = pe3r.sam2.init_state(images=images.sam2_images, video_height=images.sam2_video_size[0], video_width=images.sam2_video_size[1])138 unmodified lines"""if len(filelist) < 2:raise gradio.Error("Please input at least 2 images.")device = 'cuda' if torch.cuda.is_available() else 'cpu'images = Images(filelist=filelist, device=device)74 unmodified linesdef get_3D_object_from_scene(outdir, text, threshold, scene, min_conf_thr, as_pointcloud,mask_sky, clean_depth, transparent_cams, cam_size):device = 'cuda' if torch.cuda.is_available() else 'cpu'texts = [text]inputs = pe3r.siglip_tokenizer(text=texts, padding="max_length", return_tensors="pt")inputs = {key: value.to(device) for key, value in inputs.items()}with torch.no_grad():text_feats =pe3r.siglip.get_text_features(**inputs)text_feats = text_feats / text_feats.norm(dim=-1, keepdim=True)27 unmodified lineswith tempfile.TemporaryDirectory(suffix='pe3r_gradio_demo') as tmpdirname:recon_fun = functools.partial(get_reconstructed_scene, tmpdirname)# model_from_scene_fun = functools.partial(get_3D_model_from_scene, tmpdirname)get_3D_object_from_scene_fun = functools.partial(get_3D_object_from_scene, tmpdirname)with gradio.Blocks(css=""".gradio-container {margin: 0 !important; min-width: 100%};""", title="PE3R Demo") as demo:28 unmodified lines# adjust the camera size in the output pointcloudcam_size = gradio.Slider(label="cam_size", value=0.05, minimum=0.001, maximum=0.1, step=0.001, visible=False)with gradio.Row():as_pointcloud = gradio.Checkbox(value=True, label="As pointcloud", visible=False)# two post process implementedmask_sky = gradio.Checkbox(value=False, label="Mask sky", visible=False)clean_depth = gradio.Checkbox(value=True, label="Clean-up depthmaps", visible=False)transparent_cams = gradio.Checkbox(value=True, label="Transparent cameras", visible=False)with gradio.Row():text_input = gradio.Textbox(label="Query Text")17 unmodified linesmask_sky, clean_depth, transparent_cams, cam_size,scenegraph_type, winsize, refid],outputs=[scene, outmodel]) # , outgallery# min_conf_thr.release(fn=model_from_scene_fun,# inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,# clean_depth, transparent_cams, cam_size],# outputs=outmodel)# cam_size.change(fn=model_from_scene_fun,# inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,# clean_depth, transparent_cams, cam_size],# outputs=outmodel)# as_pointcloud.change(fn=model_from_scene_fun,# inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,# clean_depth, transparent_cams, cam_size],# outputs=outmodel)# mask_sky.change(fn=model_from_scene_fun,# inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,# clean_depth, transparent_cams, cam_size],# outputs=outmodel)# clean_depth.change(fn=model_from_scene_fun,# inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,# clean_depth, transparent_cams, cam_size],# outputs=outmodel)# transparent_cams.change(model_from_scene_fun,# inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,# clean_depth, transparent_cams, cam_size],# outputs=outmodel)find_btn.click(fn=get_3D_object_from_scene_fun,inputs=[text_input, threshold, scene, min_conf_thr, as_pointcloud, mask_sky,clean_depth, transparent_cams, cam_size],
app.py+37-31