ZenosArrows commited on
Commit
77d19ea
·
verified ·
1 Parent(s): 0e33555

Update utils/dc_utils.py

Browse files
Files changed (1) hide show
  1. utils/dc_utils.py +9 -16
utils/dc_utils.py CHANGED
@@ -6,11 +6,11 @@
6
  import numpy as np
7
  import matplotlib.cm as cm
8
  import imageio
 
9
  try:
10
  from decord import VideoReader, cpu
11
  DECORD_AVAILABLE = True
12
  except:
13
- import cv2
14
  DECORD_AVAILABLE = False
15
 
16
  def ensure_even(value):
@@ -22,12 +22,10 @@ def read_video_frames(video_path, process_length, target_fps=-1, max_res=-1):
22
  original_height, original_width = vid.get_batch([0]).shape[1:3]
23
  height = original_height
24
  width = original_width
25
- print(f'==> original video size: {original_height} x {original_width}')
26
  if max_res > 0 and max(height, width) > max_res:
27
  scale = max_res / max(original_height, original_width)
28
  height = ensure_even(round(original_height * scale))
29
  width = ensure_even(round(original_width * scale))
30
- print(f'==> downsample video size: {height} x {width}')
31
 
32
  vid = VideoReader(video_path, ctx=cpu(0), width=width, height=height)
33
 
@@ -71,18 +69,13 @@ def read_video_frames(video_path, process_length, target_fps=-1, max_res=-1):
71
  return frames, fps
72
 
73
 
74
- def save_video(frames, output_video_path, fps=10, is_depths=False, grayscale=False):
75
  writer = imageio.get_writer(output_video_path, fps=fps, macro_block_size=1, codec='libx264', ffmpeg_params=['-crf', '18'])
76
- if is_depths:
77
- colormap = np.array(cm.get_cmap("inferno").colors)
78
- d_min, d_max = frames.min(), frames.max()
79
- for i in range(frames.shape[0]):
80
- depth = frames[i]
81
- depth_norm = ((depth - d_min) / (d_max - d_min) * 255).astype(np.uint8)
82
- depth_vis = (colormap[depth_norm] * 255).astype(np.uint8) if not grayscale else depth_norm
83
- writer.append_data(depth_vis)
84
- else:
85
- for i in range(frames.shape[0]):
86
- writer.append_data(frames[i])
87
-
88
  writer.close()
 
6
  import numpy as np
7
  import matplotlib.cm as cm
8
  import imageio
9
+ import cv2
10
  try:
11
  from decord import VideoReader, cpu
12
  DECORD_AVAILABLE = True
13
  except:
 
14
  DECORD_AVAILABLE = False
15
 
16
  def ensure_even(value):
 
22
  original_height, original_width = vid.get_batch([0]).shape[1:3]
23
  height = original_height
24
  width = original_width
 
25
  if max_res > 0 and max(height, width) > max_res:
26
  scale = max_res / max(original_height, original_width)
27
  height = ensure_even(round(original_height * scale))
28
  width = ensure_even(round(original_width * scale))
 
29
 
30
  vid = VideoReader(video_path, ctx=cpu(0), width=width, height=height)
31
 
 
69
  return frames, fps
70
 
71
 
72
+ def save_video(frames, depths, output_video_path, fps=10, grayscale=False):
73
  writer = imageio.get_writer(output_video_path, fps=fps, macro_block_size=1, codec='libx264', ffmpeg_params=['-crf', '18'])
74
+ colormap = np.array(cm.get_cmap("inferno").colors)
75
+ d_min, d_max = depths.min(), depths.max()
76
+ for i in range(frames.shape[0]):
77
+ depth = depths[i]
78
+ depth_norm = ((depth - d_min) / (d_max - d_min) * 255).astype(np.uint8)
79
+ depth_vis = (colormap[depth_norm] * 255).astype(np.uint8) if not grayscale else cv2.cvtColor(depth_norm, cv2.COLOR_GRAY2BGR)
80
+ writer.append_data(np.hstack((frames[i], depth_vis)))
 
 
 
 
 
81
  writer.close()