gangweix commited on
Commit
38ace0a
·
verified ·
1 Parent(s): 1f69cfa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -18
app.py CHANGED
@@ -18,10 +18,10 @@ css = """
18
  max-height: 100vh;
19
  }
20
  #img-display-input {
21
- max-height: 80vh;
22
  }
23
  #img-display-output {
24
- max-height: 80vh;
25
  }
26
  #download {
27
  height: 62px;
@@ -64,38 +64,41 @@ with gr.Blocks(css=css) as demo:
64
  input_image = gr.Image(label="Input Image", type='numpy', elem_id='img-display-input')
65
  depth_image_slider = ImageSlider(label="Depth Map with Slider View", elem_id='img-display-output', position=0.5)
66
  submit = gr.Button(value="Predict Depth")
67
- gray_depth_file = gr.File(label="Grayscale depth map", elem_id="download",)
68
- raw_file = gr.File(label="16-bit raw output (can be considered as disparity)", elem_id="download",)
69
 
70
  cmap = matplotlib.colormaps.get_cmap('Spectral')
71
 
72
  def on_submit(image):
73
  original_image = image.copy()
74
 
75
- h, w = image.shape[:2]
76
-
77
  depth = predict_depth(image[:, :, ::-1])
78
 
79
- raw_depth = Image.fromarray(depth.astype('uint16'))
80
- tmp_raw_depth = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
81
- raw_depth.save(tmp_raw_depth.name)
82
 
83
- depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0
84
- depth = depth.astype(np.uint8)
85
- colored_depth = (cmap(depth)[:, :, :3] * 255).astype(np.uint8)
86
 
87
- gray_depth = Image.fromarray(depth)
88
- tmp_gray_depth = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
89
- gray_depth.save(tmp_gray_depth.name)
90
 
91
- return [(original_image, colored_depth), tmp_gray_depth.name, tmp_raw_depth.name]
92
 
93
- submit.click(on_submit, inputs=[input_image], outputs=[depth_image_slider, gray_depth_file, raw_file])
 
 
 
 
94
 
95
  example_files = os.listdir('assets/examples')
96
  example_files.sort()
97
  example_files = [os.path.join('assets/examples', filename) for filename in example_files]
98
- examples = gr.Examples(examples=example_files, inputs=[input_image], outputs=[depth_image_slider, gray_depth_file, raw_file], fn=on_submit)
 
 
 
 
 
99
 
100
 
101
  if __name__ == '__main__':
 
18
  max-height: 100vh;
19
  }
20
  #img-display-input {
21
+ max-height: 100vh;
22
  }
23
  #img-display-output {
24
+ max-height: 100vh;
25
  }
26
  #download {
27
  height: 62px;
 
64
  input_image = gr.Image(label="Input Image", type='numpy', elem_id='img-display-input')
65
  depth_image_slider = ImageSlider(label="Depth Map with Slider View", elem_id='img-display-output', position=0.5)
66
  submit = gr.Button(value="Predict Depth")
67
+ raw_file = gr.File(label="Raw depth output (saved as .npy)", elem_id="download",)
 
68
 
69
  cmap = matplotlib.colormaps.get_cmap('Spectral')
70
 
71
  def on_submit(image):
72
  original_image = image.copy()
73
 
 
 
74
  depth = predict_depth(image[:, :, ::-1])
75
 
76
+ # save raw depth (npy)
77
+ tmp_raw_depth = tempfile.NamedTemporaryFile(suffix='.npy', delete=False)
78
+ np.save(tmp_raw_depth.name, depth)
79
 
 
 
 
80
 
81
+ depth_vis = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0
82
+ depth_vis = depth_vis.astype(np.uint8)
83
+ colored_depth = (cmap(depth_vis)[:, :, :3] * 255).astype(np.uint8)
84
 
85
+ return [(original_image, colored_depth), tmp_raw_depth.name]
86
 
87
+ submit.click(
88
+ on_submit,
89
+ inputs=[input_image],
90
+ outputs=[depth_image_slider, raw_file]
91
+ )
92
 
93
  example_files = os.listdir('assets/examples')
94
  example_files.sort()
95
  example_files = [os.path.join('assets/examples', filename) for filename in example_files]
96
+ examples = gr.Examples(
97
+ examples=example_files,
98
+ inputs=[input_image],
99
+ outputs=[depth_image_slider, raw_file],
100
+ fn=on_submit
101
+ )
102
 
103
 
104
  if __name__ == '__main__':