Skip to content

Commit 18063fa

Browse files
committed
Inlined variables
Trusted my IDE to do this automatically
1 parent 5305f45 commit 18063fa

File tree

1 file changed

+37
-66
lines changed

1 file changed

+37
-66
lines changed

src/core.py

Lines changed: 37 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -72,42 +72,9 @@ def core_generation_funnel(outpath, inputimages, inputdepthmaps, inputnames, inp
7272
inputdepthmaps_complete = all([x is not None for x in inputdepthmaps])
7373

7474
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]
8375
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]
9476
net_height = inp[go.NET_HEIGHT]
9577
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]
11178
stereo_separation = inp[go.STEREO_SEPARATION]
11279

11380
if ops is None:
@@ -122,12 +89,12 @@ def core_generation_funnel(outpath, inputimages, inputdepthmaps, inputnames, inp
12289
# TODO: this still should not be here
12390
background_removed_images = []
12491
# 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])
12895
background_removed_images = inputimages
12996
else:
130-
background_removed_images = batched_background_removal(inputimages, rembg_model)
97+
background_removed_images = batched_background_removal(inputimages, inp[go.REMBG_MODEL])
13198

13299
# init torch device
133100
if depthmap_compute_device == 'GPU' and not torch.cuda.is_available():
@@ -151,7 +118,7 @@ def core_generation_funnel(outpath, inputimages, inputdepthmaps, inputnames, inp
151118
try:
152119
if not inputdepthmaps_complete:
153120
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])
155122
model = model_holder.depth_model
156123
pix2pix_model = model_holder.pix2pix_model
157124

@@ -181,7 +148,7 @@ def core_generation_funnel(outpath, inputimages, inputdepthmaps, inputnames, inp
181148
out = np.asarray(dimg, dtype="float")[:, :, 0]
182149
else:
183150
# override net size (size may be different for different images)
184-
if net_size_match:
151+
if inp[go.NET_SIZE_MATCH]:
185152
# Round up to a multiple of 32 to avoid potential issues
186153
net_width = (inputimages[count].width + 31) // 32 * 32
187154
net_height = (inputimages[count].height + 31) // 32 * 32
@@ -194,9 +161,9 @@ def core_generation_funnel(outpath, inputimages, inputdepthmaps, inputnames, inp
194161
# TODO: some models may output negative values, maybe these should be clamped to zero.
195162
if raw_prediction_invert:
196163
out *= -1
197-
if clipdepth:
164+
if inp[go.CLIPDEPTH]:
198165
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])
200167
else:
201168
# Regretfully, the depthmap is broken and will be replaced with a black image
202169
out = np.zeros(raw_prediction.shape)
@@ -211,12 +178,12 @@ def core_generation_funnel(outpath, inputimages, inputdepthmaps, inputnames, inp
211178
"""Depthmap (near=bright), as uint16"""
212179

213180
# if 3dinpainting, store maps for processing in second pass
214-
if gen_inpainted_mesh:
181+
if inp[go.GEN_INPAINTED_MESH]:
215182
inpaint_imgs.append(inputimages[count])
216183
inpaint_depths.append(img_output)
217184

218185
# applying background masks after depth
219-
if gen_rembg:
186+
if inp[go.GEN_REMBG]:
220187
print('applying background masks')
221188
background_removed_image = background_removed_images[count]
222189
# maybe a threshold cut would be better on the line below.
@@ -227,7 +194,7 @@ def core_generation_funnel(outpath, inputimages, inputdepthmaps, inputnames, inp
227194

228195
generated_images[count]['background_removed'] = background_removed_image
229196

230-
if save_background_removal_masks:
197+
if inp[go.SAVE_BACKGROUND_REMOVAL_MASKS]:
231198
bg_array = (1 - bg_mask.astype('int8')) * 255
232199
mask_array = np.stack((bg_array, bg_array, bg_array, bg_array), axis=2)
233200
mask_image = Image.fromarray(mask_array.astype(np.uint8))
@@ -236,41 +203,42 @@ def core_generation_funnel(outpath, inputimages, inputdepthmaps, inputnames, inp
236203

237204
# A weird quirk: if user tries to save depthmap, whereas input depthmap is used,
238205
# 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
244211
img_concat = Image.fromarray(np.concatenate(
245212
(inputimages[count], convert_i16_to_rgb(img_depth, inputimages[count])),
246213
axis=axis))
247214
generated_images[count]['concat_depth'] = img_concat
248215
else:
249216
generated_images[count]['depth'] = Image.fromarray(img_depth)
250217

251-
if gen_stereo:
218+
if inp[go.GEN_STEREO]:
252219
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])
255223
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]
257225

258-
if gen_normalmap:
226+
if inp[go.GEN_NORMALMAP]:
259227
generated_images[count]['normalmap'] = create_normalmap(
260228
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]
265233
)
266234

267-
if gen_heatmap:
235+
if inp[go.GEN_HEATMAP]:
268236
from dzoedepth.utils.misc import colorize
269237
heatmap = Image.fromarray(colorize(img_output, cmap='inferno'))
270238
generated_images[count]['heatmap'] = heatmap
271239

272240
# gen mesh
273-
if gen_simple_mesh:
241+
if inp[go.GEN_SIMPLE_MESH]:
274242
print(f"\nGenerating (occluded) mesh ..")
275243
basename = 'depthmap'
276244
meshsimple_fi = get_uniquefn(outpath, basename, 'obj')
@@ -279,9 +247,9 @@ def core_generation_funnel(outpath, inputimages, inputdepthmaps, inputnames, inp
279247
depthi = raw_prediction if raw_prediction is not None else out
280248
depthi_min, depthi_max = depthi.min(), depthi.max()
281249
# 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:
283251
# 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
285253
depthi = depthi_max - depthi + depthi_min
286254
depth_max = depthi.max()
287255
depth_min = depthi.min()
@@ -296,7 +264,8 @@ def core_generation_funnel(outpath, inputimages, inputdepthmaps, inputnames, inp
296264
# offset
297265
depthi = depthi + 1.0
298266

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]))
300269
mesh.export(meshsimple_fi)
301270

302271
print("Computing output(s) done.")
@@ -306,7 +275,7 @@ def core_generation_funnel(outpath, inputimages, inputdepthmaps, inputnames, inp
306275
suggestion = "ERROR: out of memory, could not generate depthmap!\nPlease try a different model"
307276
if device != torch.device("cpu"):
308277
suggestion += ", or try using the CPU"
309-
if boost:
278+
if inp[go.BOOST]:
310279
suggestion += ", or disable BOOST"
311280
print(f"{suggestion}.")
312281
else:
@@ -326,9 +295,11 @@ def core_generation_funnel(outpath, inputimages, inputdepthmaps, inputnames, inp
326295

327296
# TODO: This should not be here
328297
mesh_fi = None
329-
if gen_inpainted_mesh:
298+
if inp[go.GEN_INPAINTED_MESH]:
330299
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")
332303
except Exception as e:
333304
print(f'{str(e)}, some issue with generating inpainted mesh')
334305

0 commit comments

Comments
 (0)