Spaces:
Runtime error
Runtime error
haotongl commited on
Commit ·
71ea1f8
1
Parent(s): fa2db85
inital version
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222import osimport timeimport shutilfrom pathlib import Pathfrom typing import Unionimport atexitimport spacesfrom concurrent.futures import ThreadPoolExecutorimport open3d as o3dimport trimeshimport gradio as grfrom gradio_imageslider import ImageSliderimport cv2import numpy as npimport clickimport imageiofrom promptda.promptda import PromptDAfrom promptda.utils.io_wrapper import load_image, load_depthfrom promptda.utils.depth_utils import visualize_depth, unproject_depth# import torchDEVICE = 'cuda'# if torch.cuda.is_available(# ) else 'mps' if torch.backends.mps.is_available() else 'cpu'# model = PromptDA.from_pretrained('depth-anything/promptda_vitl').to(DEVICE).eval()model = PromptDA.from_pretrained('depth-anything/promptda_vitl').eval()thread_pool_executor = ThreadPoolExecutor(max_workers=1)def delete_later(path: Union[str, os.PathLike], delay: int = 300):print(f"Deleting file: {path}")def _delete():try:if os.path.isfile(path):os.remove(path)print(f"Deleted file: {path}")elif os.path.isdir(path):shutil.rmtree(path)print(f"Deleted directory: {path}")except:passdef _wait_and_delete():time.sleep(delay)_delete(path)thread_pool_executor.submit(_wait_and_delete)atexit.register(_delete)@spaces.GPUdef run_with_gpu(image, prompt_depth):image = image.to(DEVICE)prompt_depth = prompt_depth.to(DEVICE)model.to(DEVICE)depth = model.predict(image, prompt_depth)depth = depth[0, 0].detach().cpu().numpy()return depthdef check_is_stray_scanner_app_capture(input_dir):assert os.path.exists(os.path.join(input_dir, 'rgb.mp4')), 'rgb.mp4 not found'passdef run(input_file, resolution):# unzip zip fileinput_file = input_file.nameroot_dir = os.path.dirname(input_file)scene_name = input_file.split('/')[-1].split('.')[0]input_dir = os.path.join(root_dir, scene_name)cmd = f'unzip -o {input_file} -d {root_dir}'os.system(cmd)check_is_stray_scanner_app_capture(input_dir)# extract rgb imagesos.makedirs(os.path.join(input_dir, 'rgb'), exist_ok=True)cmd = f'ffmpeg -i {input_dir}/rgb.mp4 -start_number 0 -frames:v 10 -q:v 2 {input_dir}/rgb/%06d.jpg'os.system(cmd)# Loading & Inferenceimage_path = os.path.join(input_dir, 'rgb', '000000.jpg')image = load_image(image_path)prompt_depth_path = os.path.join(input_dir, 'depth/000000.png')prompt_depth = load_depth(prompt_depth_path)depth = run_with_gpu(image, prompt_depth)color = (image[0].permute(1,2,0).cpu().numpy() * 255.).astype(np.uint8)# Visualization filevis_depth, depth_min, depth_max = visualize_depth(depth, ret_minmax=True)vis_prompt_depth = visualize_depth(prompt_depth[0, 0].detach().cpu().numpy(), depth_min=depth_min, depth_max=depth_max)vis_prompt_depth = cv2.resize(vis_prompt_depth, (vis_depth.shape[1], vis_depth.shape[0]), interpolation=cv2.INTER_NEAREST)# PLY Fileixt_path = os.path.join(input_dir, f'camera_matrix.csv')ixt = np.loadtxt(ixt_path, delimiter=',')orig_max = 1920now_max = max(color.shape[1], color.shape[0])scale = orig_max / now_maxixt[:2] = ixt[:2] / scalepcd = unproject_depth(depth, ixt=ixt, color=color, ret_pcd=True)ply_path = os.path.join(input_dir, f'pointcloud.ply')o3d.io.write_point_cloud(ply_path, pcd)glb_path = os.path.join(input_dir, f'pointcloud.glb')scene_3d = trimesh.Scene()glb_colors = np.asarray(pcd.colors).astype(np.float32)glb_colors = np.concatenate([glb_colors, np.ones_like(glb_colors[:, :1])], axis=1)# glb_colors = (np.asarray(pcd.colors) * 255).astype(np.uint8)pcd_data = trimesh.PointCloud(vertices=np.asarray(pcd.points) * np.array([[1, -1, -1]]),colors=glb_colors.astype(np.float64),)scene_3d.add_geometry(pcd_data)scene_3d.export(file_obj=glb_path)# o3d.io.write_point_cloud(glb_path, pcd)# Depth Map Original Valuedepth_path = os.path.join(input_dir, f'depth.png')output_depth = (depth * 1000).astype(np.uint16)imageio.imwrite(depth_path, output_depth)delete_later(Path(input_dir))delete_later(Path(input_file))return color, (vis_depth, vis_prompt_depth), Path(glb_path), Path(ply_path).as_posix(), Path(depth_path).as_posix()DESCRIPTION = """# Estimate accurate and high-resolution depth maps from your iPhone capture.Project Page: [Prompt Depth Anything](https://promptda.github.io/)## Requirements:1. iPhone 12 Pro or later Pro models, iPad 2020 Pro or later Pro models2. Free iOS App: [Stray Scanner App](https://apps.apple.com/us/app/stray-scanner/id1557051662)## Testing Steps:1. Capture a scene with the Stray Scanner App.2. Use the iPhone [Files App](https://apps.apple.com/us/app/files/id1232058109) to compress it into a zip file and transfer it to your computer. (Long press the capture folder to compress)3. Upload the zip file and click "Submit" to get the depth map of the first frame.Note:- Currently, this demo only supports inference for the first frame. If you need to obtain all depth frames, please refer to our [GitHub repo](https://github.com/DepthAnything/PromptDA).- The depth map is stored as uint16, with a unit of millimeters."""@click.command()@click.option('--share', is_flag=True, help='Whether to run the app in shared mode.')def main(share: bool):with gr.Blocks(theme=gr.themes.Soft()) as demo:gr.Markdown(DESCRIPTION)with gr.Row():input_file = gr.File(type="filepath", label="Stray scanner app capture zip file")resolution = gr.Dropdown(choices=['756x1008', '1428x1904'], value='756x1008', label="Inference resolution")submit_btn = gr.Button("Submit")gr.Examples(examples=[["data/assets/example0_chair.zip", "756x1008"]],inputs=[input_file, resolution],label="Examples",)with gr.Row():with gr.Column():output_rgb = gr.Image(type="numpy", label="RGB Image")with gr.Column():output_depths = ImageSlider(label="Output depth / prompt depth", position=0.5)with gr.Row():with gr.Column():output_3d_model = gr.Model3D(label="3D Viewer", display_mode='solid', clear_color=[1.0, 1.0, 1.0, 1.0])with gr.Column():output_ply = gr.File(type="filepath", label="Download the unprojected point cloud as .ply file", height=30)output_depth_map = gr.File(type="filepath", label="Download the depth map as .png file", height=30)outputs = [output_rgb,output_depths,output_3d_model,output_ply,output_depth_map,]# gr.Examples(examples=[# ["data/assets/example0_chair.zip", "756x1008"]# ],# fn=run,# inputs=[input_file, resolution],# outputs=outputs,# label="Examples",# cache_examples=True,# )submit_btn.click(run,inputs=[input_file, resolution],outputs=outputs)demo.launch(share=share)# def main(share: bool):# gr.Interface(# fn=run,# inputs=[# gr.File(type="filepath", label="Upload a stray scanner app capture zip file"),# gr.Dropdown(choices=['756x1008', '1428x1904'], value='756x1008', label="Inference resolution")# ],# outputs=[# gr.Image(type="numpy", label="RGB Image"),# ImageSlider(label="Depth map / prompt depth", position=0.5),# gr.Model3D(label="3D Viewer", display_mode='solid', clear_color=[1.0, 1.0, 1.0, 1.0]),# gr.File(type="filepath", label="Download the unprojected point cloud as .ply file"),# gr.File(type="filepath", label="Download the depth map as .png file"),# ],# title=None,# description=DESCRIPTION,# clear_btn=None,# allow_flagging="never",# theme=gr.themes.Soft(),# examples=[# ["data/assets/8b98276b0a.zip"]# ]# ).launch(share=True)if __name__ == '__main__':main()123456789101112import spacesimport gradio as gr@spaces.GPUdef generate(prompt):return 'hello, world'gr.Interface(fn=generate,inputs=gr.Text(),outputs=gr.Gallery(),).launch()
app.py+8-218