gangweix commited on
Commit
b8c99d8
·
verified ·
1 Parent(s): 0f4cff1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -74
app.py CHANGED
@@ -41,67 +41,86 @@ css = """
41
 
42
  set_seed(666)
43
 
44
- DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
45
- default_steps = 10
46
- model = PixelPerfectDepth(sampling_steps=default_steps)
47
- ckpt_path = hf_hub_download(
48
- repo_id="gangweix/Pixel-Perfect-Depth",
49
- filename="ppd.pth",
50
- repo_type="model"
51
- )
52
- state_dict = torch.load(ckpt_path, map_location="cpu")
53
- model.load_state_dict(state_dict, strict=False)
54
- model = model.to(DEVICE).eval()
55
-
56
- moge_model = MoGeModel.from_pretrained("Ruicheng/moge-2-vitl-normal").to(DEVICE).eval()
57
-
58
-
59
-
60
- title = "# Pixel-Perfect Depth"
61
- description = """Official demo for **Pixel-Perfect Depth**.
62
- Please refer to our [paper](), [project page](https://pixel-perfect-depth.github.io), and [github](https://github.com/gangweix/pixel-perfect-depth) for more details."""
63
-
64
- @spaces.GPU
65
- def predict_depth(image, denoise_steps):
66
- depth, resize_image = model.infer_image(image, sampling_steps=denoise_steps)
67
- return depth, resize_image
68
-
69
- @spaces.GPU
70
- def predict_moge_depth(image):
71
- image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
72
- image = torch.tensor(image / 255, dtype=torch.float32, device=DEVICE).permute(2, 0, 1)
73
- metric_depth, mask, intrinsics = moge_model.infer(image)
74
- metric_depth[~mask] = metric_depth[mask].max()
75
- return metric_depth, mask, intrinsics
76
-
77
- with gr.Blocks(css=css) as demo:
78
- gr.Markdown(title)
79
- gr.Markdown(description)
80
- gr.Markdown("### Depth Prediction demo")
81
-
82
- with gr.Row():
83
- # Left: input image + settings
84
- with gr.Column():
85
- input_image = gr.Image(label="Input Image", image_mode="RGB", type='numpy', elem_id='img-display-input')
86
- with gr.Accordion(label="Settings", open=False):
87
- denoise_steps = gr.Slider(label="Denoising Steps", minimum=1, maximum=50, value=10, step=1)
88
- apply_filter = gr.Checkbox(label="Apply filter points", value=True)
89
- submit_btn = gr.Button(value="Predict Depth")
90
-
91
- # Right: 3D point cloud + depth
92
- with gr.Column():
93
- with gr.Tabs():
94
- with gr.Tab("3D View"):
95
- model_3d = gr.Model3D(display_mode="solid", label="3D Point Map", clear_color=[1,1,1,1], height="60vh")
96
- with gr.Tab("Depth"):
97
- depth_map = ImageSlider(label="Depth Map with Slider View", elem_id='img-display-output', position=0.5)
98
 
99
- concat_file = gr.File(label="Concatenated visualization (image+depth)", elem_id="image-depth-download")
100
- raw_depth_file = gr.File(label="Raw depth output (saved as .npy)", elem_id="download")
101
- pcd_file = gr.File(label="Point Cloud (.ply)", elem_id="download-ply")
 
 
 
 
 
 
 
 
 
 
102
 
103
  cmap = matplotlib.colormaps.get_cmap('Spectral')
104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  def on_submit(image, denoise_steps, apply_filter):
106
 
107
  H, W = image.shape[:2]
@@ -148,22 +167,6 @@ with gr.Blocks(css=css) as demo:
148
 
149
  return [(image, colored_depth), tmp_ply.name, tmp_concat.name, tmp_raw_depth.name, tmp_ply.name]
150
 
151
- submit_btn.click(
152
- on_submit,
153
- inputs=[input_image, denoise_steps, apply_filter],
154
- outputs=[depth_map, model_3d, concat_file, raw_depth_file, pcd_file]
155
- )
156
-
157
- example_files = os.listdir('assets/examples')
158
- example_files.sort()
159
- example_files = [os.path.join('assets/examples', filename) for filename in example_files]
160
- examples = gr.Examples(
161
- examples=example_files,
162
- inputs=[input_image],
163
- outputs=[depth_map, model_3d, concat_file, raw_depth_file, pcd_file],
164
- fn=on_submit
165
- )
166
-
167
 
168
  if __name__ == '__main__':
169
- demo.queue().launch(share=True)
 
41
 
42
  set_seed(666)
43
 
44
+ def main(share=True):
45
+ print("Initializing Pixel-Perfect Depth Demo...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
+ DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
48
+ default_steps = 10
49
+ model = PixelPerfectDepth(sampling_steps=default_steps)
50
+ ckpt_path = hf_hub_download(
51
+ repo_id="gangweix/Pixel-Perfect-Depth",
52
+ filename="ppd.pth",
53
+ repo_type="model"
54
+ )
55
+ state_dict = torch.load(ckpt_path, map_location="cpu")
56
+ model.load_state_dict(state_dict, strict=False)
57
+ model = model.to(DEVICE).eval()
58
+
59
+ moge_model = MoGeModel.from_pretrained("Ruicheng/moge-2-vitl-normal").to(DEVICE).eval()
60
 
61
  cmap = matplotlib.colormaps.get_cmap('Spectral')
62
 
63
+ title = "# Pixel-Perfect Depth"
64
+ description = """Official demo for **Pixel-Perfect Depth**.
65
+ Please refer to our [paper](), [project page](https://pixel-perfect-depth.github.io), and [github](https://github.com/gangweix/pixel-perfect-depth) for more details."""
66
+
67
+ @spaces.GPU
68
+ def predict_depth(image, denoise_steps):
69
+ depth, resize_image = model.infer_image(image, sampling_steps=denoise_steps)
70
+ return depth, resize_image
71
+
72
+ @spaces.GPU
73
+ def predict_moge_depth(image):
74
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
75
+ image = torch.tensor(image / 255, dtype=torch.float32, device=DEVICE).permute(2, 0, 1)
76
+ metric_depth, mask, intrinsics = moge_model.infer(image)
77
+ metric_depth[~mask] = metric_depth[mask].max()
78
+ return metric_depth, mask, intrinsics
79
+
80
+ with gr.Blocks(css=css) as demo:
81
+ gr.Markdown(title)
82
+ gr.Markdown(description)
83
+ gr.Markdown("### Depth Prediction demo")
84
+
85
+ with gr.Row():
86
+ # Left: input image + settings
87
+ with gr.Column():
88
+ input_image = gr.Image(label="Input Image", image_mode="RGB", type='numpy', elem_id='img-display-input')
89
+ with gr.Accordion(label="Settings", open=False):
90
+ denoise_steps = gr.Slider(label="Denoising Steps", minimum=1, maximum=50, value=10, step=1)
91
+ apply_filter = gr.Checkbox(label="Apply filter points", value=True)
92
+ submit_btn = gr.Button(value="Predict Depth")
93
+
94
+ # Right: 3D point cloud + depth
95
+ with gr.Column():
96
+ with gr.Tabs():
97
+ with gr.Tab("3D View"):
98
+ model_3d = gr.Model3D(display_mode="solid", label="3D Point Map", clear_color=[1,1,1,1], height="60vh")
99
+ with gr.Tab("Depth"):
100
+ depth_map = ImageSlider(label="Depth Map with Slider View", elem_id='img-display-output', position=0.5)
101
+
102
+ concat_file = gr.File(label="Concatenated visualization (image+depth)", elem_id="image-depth-download")
103
+ raw_depth_file = gr.File(label="Raw depth output (saved as .npy)", elem_id="download")
104
+ pcd_file = gr.File(label="Point Cloud (.ply)", elem_id="download-ply")
105
+
106
+ submit_btn.click(
107
+ on_submit,
108
+ inputs=[input_image, denoise_steps, apply_filter],
109
+ outputs=[depth_map, model_3d, concat_file, raw_depth_file, pcd_file]
110
+ )
111
+
112
+ example_files = os.listdir('assets/examples')
113
+ example_files.sort()
114
+ example_files = [os.path.join('assets/examples', filename) for filename in example_files]
115
+ examples = gr.Examples(
116
+ examples=example_files,
117
+ inputs=[input_image],
118
+ outputs=[depth_map, model_3d, concat_file, raw_depth_file, pcd_file],
119
+ fn=on_submit
120
+ )
121
+
122
+ demo.queue().launch(share=share)
123
+
124
  def on_submit(image, denoise_steps, apply_filter):
125
 
126
  H, W = image.shape[:2]
 
167
 
168
  return [(image, colored_depth), tmp_ply.name, tmp_concat.name, tmp_raw_depth.name, tmp_ply.name]
169
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
 
171
  if __name__ == '__main__':
172
+ main(share=True)