guangkaixu commited on
Commit
704db5a
1 Parent(s): 6b95e7c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -13
app.py CHANGED
@@ -57,18 +57,17 @@ def process_image_check(path_input):
57
  "Missing image in the first pane: upload a file or use one from the gallery below."
58
  )
59
 
60
- def process_image(
61
  pipe,
62
  path_input,
63
- mode='depth',
64
  processing_res=default_image_processing_res,
65
  ):
66
  name_base, name_ext = os.path.splitext(os.path.basename(path_input))
67
  print(f"Processing image {name_base}{name_ext}")
68
 
69
  path_output_dir = tempfile.mkdtemp()
70
- path_out_fp32 = os.path.join(path_output_dir, f"{name_base}_%s_fp32.npy" %mode)
71
- path_out_vis = os.path.join(path_output_dir, f"{name_base}_%s_colored.png" %mode)
72
 
73
  input_image = Image.open(path_input)
74
 
@@ -86,12 +85,79 @@ def process_image(
86
  np.save(path_out_fp32, depth_pred)
87
  depth_colored.save(path_out_vis)
88
 
89
- if mode == 'depth':
90
- path_out_16bit = os.path.join(path_output_dir, f"{name_base}_%s_16bit.png" %mode)
91
- depth_16bit = (depth_pred * 65535.0).astype(np.uint16)
92
- Image.fromarray(depth_16bit).save(path_out_16bit, mode="I;16")
93
- else:
94
- path_out_16bit = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
  return (
97
  [path_out_16bit, path_out_vis],
@@ -99,9 +165,9 @@ def process_image(
99
  )
100
 
101
  def run_demo_server(pipe_depth, pipe_normal, pipe_dis):
102
- process_pipe_depth = spaces.GPU(functools.partial(process_image, pipe_depth, mode='depth'))
103
- process_pipe_normal = spaces.GPU(functools.partial(process_image, pipe_normal, mode='normal'))
104
- process_pipe_dis = spaces.GPU(functools.partial(process_image, pipe_dis, mode='seg'))
105
  gradio_theme = gr.themes.Default()
106
 
107
  with gr.Blocks(
 
57
  "Missing image in the first pane: upload a file or use one from the gallery below."
58
  )
59
 
60
+ def process_depth(
61
  pipe,
62
  path_input,
 
63
  processing_res=default_image_processing_res,
64
  ):
65
  name_base, name_ext = os.path.splitext(os.path.basename(path_input))
66
  print(f"Processing image {name_base}{name_ext}")
67
 
68
  path_output_dir = tempfile.mkdtemp()
69
+ path_out_fp32 = os.path.join(path_output_dir, f"{name_base}_depth_fp32.npy")
70
+ path_out_vis = os.path.join(path_output_dir, f"{name_base}_depth_colored.png")
71
 
72
  input_image = Image.open(path_input)
73
 
 
85
  np.save(path_out_fp32, depth_pred)
86
  depth_colored.save(path_out_vis)
87
 
88
+ path_out_16bit = os.path.join(path_output_dir, f"{name_base}_%s_16bit.png" %mode)
89
+ depth_16bit = (depth_pred * 65535.0).astype(np.uint16)
90
+ Image.fromarray(depth_16bit).save(path_out_16bit, mode="I;16")
91
+
92
+ return (
93
+ [path_out_16bit, path_out_vis],
94
+ [path_out_16bit, path_out_fp32, path_out_vis],
95
+ )
96
+
97
+ def process_normal(
98
+ pipe,
99
+ path_input,
100
+ processing_res=default_image_processing_res,
101
+ ):
102
+ name_base, name_ext = os.path.splitext(os.path.basename(path_input))
103
+ print(f"Processing image {name_base}{name_ext}")
104
+
105
+ path_output_dir = tempfile.mkdtemp()
106
+ path_out_fp32 = os.path.join(path_output_dir, f"{name_base}_normal_fp32.npy")
107
+ path_out_vis = os.path.join(path_output_dir, f"{name_base}_normal_colored.png")
108
+
109
+ input_image = Image.open(path_input)
110
+
111
+ pipe_out = pipe(
112
+ input_image,
113
+ processing_res=processing_res,
114
+ batch_size=1 if processing_res == 0 else 0,
115
+ show_progress_bar=False,
116
+ mode=mode,
117
+ )
118
+
119
+ depth_pred = pipe_out.pred_np
120
+ depth_colored = pipe_out.pred_colored
121
+
122
+ np.save(path_out_fp32, depth_pred)
123
+ depth_colored.save(path_out_vis)
124
+
125
+ path_out_16bit = None
126
+
127
+ return (
128
+ [path_out_16bit, path_out_vis],
129
+ [path_out_16bit, path_out_fp32, path_out_vis],
130
+ )
131
+
132
+ def process_dis(
133
+ pipe,
134
+ path_input,
135
+ processing_res=default_image_processing_res,
136
+ ):
137
+ name_base, name_ext = os.path.splitext(os.path.basename(path_input))
138
+ print(f"Processing image {name_base}{name_ext}")
139
+
140
+ path_output_dir = tempfile.mkdtemp()
141
+ path_out_fp32 = os.path.join(path_output_dir, f"{name_base}_dis_fp32.npy")
142
+ path_out_vis = os.path.join(path_output_dir, f"{name_base}_dis_colored.png")
143
+
144
+ input_image = Image.open(path_input)
145
+
146
+ pipe_out = pipe(
147
+ input_image,
148
+ processing_res=processing_res,
149
+ batch_size=1 if processing_res == 0 else 0,
150
+ show_progress_bar=False,
151
+ mode=mode,
152
+ )
153
+
154
+ depth_pred = pipe_out.pred_np
155
+ depth_colored = pipe_out.pred_colored
156
+
157
+ np.save(path_out_fp32, depth_pred)
158
+ depth_colored.save(path_out_vis)
159
+
160
+ path_out_16bit = None
161
 
162
  return (
163
  [path_out_16bit, path_out_vis],
 
165
  )
166
 
167
  def run_demo_server(pipe_depth, pipe_normal, pipe_dis):
168
+ process_pipe_depth = spaces.GPU(functools.partial(process_depth, pipe_depth))
169
+ process_pipe_normal = spaces.GPU(functools.partial(process_normal, pipe_normal))
170
+ process_pipe_dis = spaces.GPU(functools.partial(process_dis, pipe_dis))
171
  gradio_theme = gr.themes.Default()
172
 
173
  with gr.Blocks(