@@ -72,42 +72,9 @@ def core_generation_funnel(outpath, inputimages, inputdepthmaps, inputnames, inp
72
72
inputdepthmaps_complete = all ([x is not None for x in inputdepthmaps ])
73
73
74
74
inp = CoreGenerationFunnelInp (inp )
75
- gen_rembg = inp [go .GEN_REMBG ]
76
- rembg_model = inp [go .REMBG_MODEL ]
77
- boost = inp [go .BOOST ]
78
- clipdepth = inp [go .CLIPDEPTH ]
79
- clipdepth_far = inp [go .CLIPDEPTH_FAR ]
80
- clipdepth_near = inp [go .CLIPDEPTH_NEAR ]
81
- output_depth_combine = inp [go .OUTPUT_DEPTH_COMBINE ]
82
- output_depth_combine_axis = inp [go .OUTPUT_DEPTH_COMBINE_AXIS ]
83
75
depthmap_compute_device = inp [go .COMPUTE_DEVICE ]
84
- gen_simple_mesh = inp [go .GEN_SIMPLE_MESH ]
85
- gen_normalmap = inp [go .GEN_NORMALMAP ]
86
- gen_stereo = inp [go .GEN_STEREO ]
87
- gen_inpainted_mesh = inp [go .GEN_INPAINTED_MESH ]
88
- gen_inpainted_mesh_demos = inp [go .GEN_INPAINTED_MESH_DEMOS ]
89
- output_depth_invert = inp [go .OUTPUT_DEPTH_INVERT ]
90
- net_size_match = inp [go .NET_SIZE_MATCH ]
91
- simple_mesh_occlude = inp [go .SIMPLE_MESH_OCCLUDE ]
92
- simple_mesh_spherical = inp [go .SIMPLE_MESH_SPHERICAL ]
93
- model_type = inp [go .MODEL_TYPE ]
94
76
net_height = inp [go .NET_HEIGHT ]
95
77
net_width = inp [go .NET_WIDTH ]
96
- normalmap_pre_blur = inp [go .NORMALMAP_PRE_BLUR ]
97
- normalmap_pre_blur_kernel = inp [go .NORMALMAP_PRE_BLUR_KERNEL ]
98
- normalmap_sobel = inp [go .NORMALMAP_SOBEL ]
99
- normalmap_sobel_kernel = inp [go .NORMALMAP_SOBEL_KERNEL ]
100
- normalmap_post_blur = inp [go .NORMALMAP_POST_BLUR ]
101
- normalmap_post_blur_kernel = inp [go .NORMALMAP_POST_BLUR_KERNEL ]
102
- normalmap_invert = inp [go .NORMALMAP_INVERT ]
103
- pre_depth_background_removal = inp [go .PRE_DEPTH_BACKGROUND_REMOVAL ]
104
- save_background_removal_masks = inp [go .SAVE_BACKGROUND_REMOVAL_MASKS ]
105
- do_output_depth = inp [go .DO_OUTPUT_DEPTH ]
106
- gen_heatmap = inp [go .GEN_HEATMAP ]
107
- stereo_balance = inp [go .STEREO_BALANCE ]
108
- stereo_divergence = inp [go .STEREO_DIVERGENCE ]
109
- stereo_fill_algo = inp [go .STEREO_FILL_ALGO ]
110
- stereo_modes = inp [go .STEREO_MODES ]
111
78
stereo_separation = inp [go .STEREO_SEPARATION ]
112
79
113
80
if ops is None :
@@ -122,12 +89,12 @@ def core_generation_funnel(outpath, inputimages, inputdepthmaps, inputnames, inp
122
89
# TODO: this still should not be here
123
90
background_removed_images = []
124
91
# remove on base image before depth calculation
125
- if gen_rembg :
126
- if pre_depth_background_removal :
127
- inputimages = batched_background_removal (inputimages , rembg_model )
92
+ if inp [ go . GEN_REMBG ] :
93
+ if inp [ go . PRE_DEPTH_BACKGROUND_REMOVAL ] :
94
+ inputimages = batched_background_removal (inputimages , inp [ go . REMBG_MODEL ] )
128
95
background_removed_images = inputimages
129
96
else :
130
- background_removed_images = batched_background_removal (inputimages , rembg_model )
97
+ background_removed_images = batched_background_removal (inputimages , inp [ go . REMBG_MODEL ] )
131
98
132
99
# init torch device
133
100
if depthmap_compute_device == 'GPU' and not torch .cuda .is_available ():
@@ -151,7 +118,7 @@ def core_generation_funnel(outpath, inputimages, inputdepthmaps, inputnames, inp
151
118
try :
152
119
if not inputdepthmaps_complete :
153
120
print ("Loading model(s) .." )
154
- model_holder .ensure_models (model_type , device , boost )
121
+ model_holder .ensure_models (inp [ go . MODEL_TYPE ] , device , inp [ go . BOOST ] )
155
122
model = model_holder .depth_model
156
123
pix2pix_model = model_holder .pix2pix_model
157
124
@@ -181,7 +148,7 @@ def core_generation_funnel(outpath, inputimages, inputdepthmaps, inputnames, inp
181
148
out = np .asarray (dimg , dtype = "float" )[:, :, 0 ]
182
149
else :
183
150
# override net size (size may be different for different images)
184
- if net_size_match :
151
+ if inp [ go . NET_SIZE_MATCH ] :
185
152
# Round up to a multiple of 32 to avoid potential issues
186
153
net_width = (inputimages [count ].width + 31 ) // 32 * 32
187
154
net_height = (inputimages [count ].height + 31 ) // 32 * 32
@@ -194,9 +161,9 @@ def core_generation_funnel(outpath, inputimages, inputdepthmaps, inputnames, inp
194
161
# TODO: some models may output negative values, maybe these should be clamped to zero.
195
162
if raw_prediction_invert :
196
163
out *= - 1
197
- if clipdepth :
164
+ if inp [ go . CLIPDEPTH ] :
198
165
out = (out - out .min ()) / (out .max () - out .min ()) # normalize to [0; 1]
199
- out = np .clip (out , clipdepth_far , clipdepth_near )
166
+ out = np .clip (out , inp [ go . CLIPDEPTH_FAR ], inp [ go . CLIPDEPTH_NEAR ] )
200
167
else :
201
168
# Regretfully, the depthmap is broken and will be replaced with a black image
202
169
out = np .zeros (raw_prediction .shape )
@@ -211,12 +178,12 @@ def core_generation_funnel(outpath, inputimages, inputdepthmaps, inputnames, inp
211
178
"""Depthmap (near=bright), as uint16"""
212
179
213
180
# if 3dinpainting, store maps for processing in second pass
214
- if gen_inpainted_mesh :
181
+ if inp [ go . GEN_INPAINTED_MESH ] :
215
182
inpaint_imgs .append (inputimages [count ])
216
183
inpaint_depths .append (img_output )
217
184
218
185
# applying background masks after depth
219
- if gen_rembg :
186
+ if inp [ go . GEN_REMBG ] :
220
187
print ('applying background masks' )
221
188
background_removed_image = background_removed_images [count ]
222
189
# maybe a threshold cut would be better on the line below.
@@ -227,7 +194,7 @@ def core_generation_funnel(outpath, inputimages, inputdepthmaps, inputnames, inp
227
194
228
195
generated_images [count ]['background_removed' ] = background_removed_image
229
196
230
- if save_background_removal_masks :
197
+ if inp [ go . SAVE_BACKGROUND_REMOVAL_MASKS ] :
231
198
bg_array = (1 - bg_mask .astype ('int8' )) * 255
232
199
mask_array = np .stack ((bg_array , bg_array , bg_array , bg_array ), axis = 2 )
233
200
mask_image = Image .fromarray (mask_array .astype (np .uint8 ))
@@ -236,41 +203,42 @@ def core_generation_funnel(outpath, inputimages, inputdepthmaps, inputnames, inp
236
203
237
204
# A weird quirk: if user tries to save depthmap, whereas input depthmap is used,
238
205
# depthmap will be outputed, even if output_depth_combine is used.
239
- if do_output_depth and inputdepthmaps [count ] is None :
240
- if do_output_depth :
241
- img_depth = cv2 .bitwise_not (img_output ) if output_depth_invert else img_output
242
- if output_depth_combine :
243
- axis = 1 if output_depth_combine_axis == 'Horizontal' else 0
206
+ if inp [ go . DO_OUTPUT_DEPTH ] and inputdepthmaps [count ] is None :
207
+ if inp [ go . DO_OUTPUT_DEPTH ] :
208
+ img_depth = cv2 .bitwise_not (img_output ) if inp [ go . OUTPUT_DEPTH_INVERT ] else img_output
209
+ if inp [ go . OUTPUT_DEPTH_COMBINE ] :
210
+ axis = 1 if inp [ go . OUTPUT_DEPTH_COMBINE_AXIS ] == 'Horizontal' else 0
244
211
img_concat = Image .fromarray (np .concatenate (
245
212
(inputimages [count ], convert_i16_to_rgb (img_depth , inputimages [count ])),
246
213
axis = axis ))
247
214
generated_images [count ]['concat_depth' ] = img_concat
248
215
else :
249
216
generated_images [count ]['depth' ] = Image .fromarray (img_depth )
250
217
251
- if gen_stereo :
218
+ if inp [ go . GEN_STEREO ] :
252
219
print ("Generating stereoscopic images.." )
253
- stereoimages = create_stereoimages (inputimages [count ], img_output , stereo_divergence , inp ,
254
- stereo_modes , stereo_balance , stereo_fill_algo )
220
+ stereoimages = create_stereoimages (inputimages [count ], img_output , inp [go .STEREO_DIVERGENCE ], inp ,
221
+ inp [go .STEREO_MODES ], inp [go .STEREO_BALANCE ],
222
+ inp [go .STEREO_FILL_ALGO ])
255
223
for c in range (0 , len (stereoimages )):
256
- generated_images [count ][stereo_modes [c ]] = stereoimages [c ]
224
+ generated_images [count ][inp [ go . STEREO_MODES ] [c ]] = stereoimages [c ]
257
225
258
- if gen_normalmap :
226
+ if inp [ go . GEN_NORMALMAP ] :
259
227
generated_images [count ]['normalmap' ] = create_normalmap (
260
228
img_output ,
261
- normalmap_pre_blur_kernel if normalmap_pre_blur else None ,
262
- normalmap_sobel_kernel if normalmap_sobel else None ,
263
- normalmap_post_blur_kernel if normalmap_post_blur else None ,
264
- normalmap_invert
229
+ inp [ go . NORMALMAP_PRE_BLUR_KERNEL ] if inp [ go . NORMALMAP_PRE_BLUR ] else None ,
230
+ inp [ go . NORMALMAP_SOBEL_KERNEL ] if inp [ go . NORMALMAP_SOBEL ] else None ,
231
+ inp [ go . NORMALMAP_POST_BLUR_KERNEL ] if inp [ go . NORMALMAP_POST_BLUR ] else None ,
232
+ inp [ go . NORMALMAP_INVERT ]
265
233
)
266
234
267
- if gen_heatmap :
235
+ if inp [ go . GEN_HEATMAP ] :
268
236
from dzoedepth .utils .misc import colorize
269
237
heatmap = Image .fromarray (colorize (img_output , cmap = 'inferno' ))
270
238
generated_images [count ]['heatmap' ] = heatmap
271
239
272
240
# gen mesh
273
- if gen_simple_mesh :
241
+ if inp [ go . GEN_SIMPLE_MESH ] :
274
242
print (f"\n Generating (occluded) mesh .." )
275
243
basename = 'depthmap'
276
244
meshsimple_fi = get_uniquefn (outpath , basename , 'obj' )
@@ -279,9 +247,9 @@ def core_generation_funnel(outpath, inputimages, inputdepthmaps, inputnames, inp
279
247
depthi = raw_prediction if raw_prediction is not None else out
280
248
depthi_min , depthi_max = depthi .min (), depthi .max ()
281
249
# try to map output to sensible values for non zoedepth models, boost, or custom maps
282
- if model_type not in [7 , 8 , 9 ] or boost or inputdepthmaps [count ] is not None :
250
+ if inp [ go . MODEL_TYPE ] not in [7 , 8 , 9 ] or inp [ go . BOOST ] or inputdepthmaps [count ] is not None :
283
251
# invert if midas
284
- if model_type > 0 or inputdepthmaps [count ] is not None : # TODO: Weird
252
+ if inp [ go . MODEL_TYPE ] > 0 or inputdepthmaps [count ] is not None : # TODO: Weird
285
253
depthi = depthi_max - depthi + depthi_min
286
254
depth_max = depthi .max ()
287
255
depth_min = depthi .min ()
@@ -296,7 +264,8 @@ def core_generation_funnel(outpath, inputimages, inputdepthmaps, inputnames, inp
296
264
# offset
297
265
depthi = depthi + 1.0
298
266
299
- mesh = create_mesh (inputimages [count ], depthi , keep_edges = not simple_mesh_occlude , spherical = simple_mesh_spherical )
267
+ mesh = create_mesh (inputimages [count ], depthi , keep_edges = not inp [go .SIMPLE_MESH_OCCLUDE ],
268
+ spherical = (inp [go .SIMPLE_MESH_SPHERICAL ]))
300
269
mesh .export (meshsimple_fi )
301
270
302
271
print ("Computing output(s) done." )
@@ -306,7 +275,7 @@ def core_generation_funnel(outpath, inputimages, inputdepthmaps, inputnames, inp
306
275
suggestion = "ERROR: out of memory, could not generate depthmap!\n Please try a different model"
307
276
if device != torch .device ("cpu" ):
308
277
suggestion += ", or try using the CPU"
309
- if boost :
278
+ if inp [ go . BOOST ] :
310
279
suggestion += ", or disable BOOST"
311
280
print (f"{ suggestion } ." )
312
281
else :
@@ -326,9 +295,11 @@ def core_generation_funnel(outpath, inputimages, inputdepthmaps, inputnames, inp
326
295
327
296
# TODO: This should not be here
328
297
mesh_fi = None
329
- if gen_inpainted_mesh :
298
+ if inp [ go . GEN_INPAINTED_MESH ] :
330
299
try :
331
- mesh_fi = run_3dphoto (device , inpaint_imgs , inpaint_depths , inputnames , outpath , gen_inpainted_mesh_demos , 1 , "mp4" )
300
+ mesh_fi = run_3dphoto (device , inpaint_imgs , inpaint_depths , inputnames , outpath ,
301
+ inp [go .GEN_INPAINTED_MESH_DEMOS ],
302
+ 1 , "mp4" )
332
303
except Exception as e :
333
304
print (f'{ str (e )} , some issue with generating inpainted mesh' )
334
305
0 commit comments