Update utils/dc_utils.py
Browse files5 unmodified lines6789101112131415165 unmodified lines22232425262728293031323337 unmodified lines7172737475767778798081828384858687885 unmodified linesimport numpy as npimport matplotlib.cm as cmimport imageiotry:from decord import VideoReader, cpuDECORD_AVAILABLE = Trueexcept:import cv2DECORD_AVAILABLE = Falsedef ensure_even(value):5 unmodified linesoriginal_height, original_width = vid.get_batch([0]).shape[1:3]height = original_heightwidth = original_widthprint(f'==> original video size: {original_height} x {original_width}')if max_res > 0 and max(height, width) > max_res:scale = max_res / max(original_height, original_width)height = ensure_even(round(original_height * scale))width = ensure_even(round(original_width * scale))print(f'==> downsample video size: {height} x {width}')vid = VideoReader(video_path, ctx=cpu(0), width=width, height=height)37 unmodified linesreturn frames, fpsdef save_video(frames, output_video_path, fps=10, is_depths=False, grayscale=False):writer = imageio.get_writer(output_video_path, fps=fps, macro_block_size=1, codec='libx264', ffmpeg_params=['-crf', '18'])if is_depths:colormap = np.array(cm.get_cmap("inferno").colors)d_min, d_max = frames.min(), frames.max()for i in range(frames.shape[0]):depth = frames[i]depth_norm = ((depth - d_min) / (d_max - d_min) * 255).astype(np.uint8)depth_vis = (colormap[depth_norm] * 255).astype(np.uint8) if not grayscale else depth_normwriter.append_data(depth_vis)else:for i in range(frames.shape[0]):writer.append_data(frames[i])writer.close()5 unmodified lines6789101112131415165 unmodified lines2223242526272829303137 unmodified lines697071727374757677787980815 unmodified linesimport numpy as npimport matplotlib.cm as cmimport imageioimport cv2try:from decord import VideoReader, cpuDECORD_AVAILABLE = Trueexcept:DECORD_AVAILABLE = Falsedef ensure_even(value):5 unmodified linesoriginal_height, original_width = vid.get_batch([0]).shape[1:3]height = original_heightwidth = original_widthif max_res > 0 and max(height, width) > max_res:scale = max_res / max(original_height, original_width)height = ensure_even(round(original_height * scale))width = ensure_even(round(original_width * scale))vid = VideoReader(video_path, ctx=cpu(0), width=width, height=height)37 unmodified linesreturn frames, fpsdef save_video(frames, depths, output_video_path, fps=10, grayscale=False):writer = imageio.get_writer(output_video_path, fps=fps, macro_block_size=1, codec='libx264', ffmpeg_params=['-crf', '18'])colormap = np.array(cm.get_cmap("inferno").colors)d_min, d_max = depths.min(), depths.max()for i in range(frames.shape[0]):depth = depths[i]depth_norm = ((depth - d_min) / (d_max - d_min) * 255).astype(np.uint8)depth_vis = (colormap[depth_norm] * 255).astype(np.uint8) if not grayscale else cv2.cvtColor(depth_norm, cv2.COLOR_GRAY2BGR)writer.append_data(np.hstack((frames[i], depth_vis)))writer.close()
utils/dc_utils.py+9-16