mtwohey2 commited on
Commit
9a5564e
verified
1 Parent(s): 18f585c

Create util/dc_utils.py

Browse files
Files changed (1) hide show
  1. util/dc_utils.py +88 -0
util/dc_utils.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This file is originally from DepthCrafter/depthcrafter/utils.py at main 路 Tencent/DepthCrafter
2
+ # SPDX-License-Identifier: MIT License license
3
+ #
4
+ # This file may have been modified by ByteDance Ltd. and/or its affiliates on [date of modification]
5
+ # Original file is released under [ MIT License license], with the full license text available at [https://github.com/Tencent/DepthCrafter?tab=License-1-ov-file].
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):
17
+ return value if value % 2 == 0 else value + 1
18
+
19
+ def read_video_frames(video_path, process_length, target_fps=-1, max_res=-1):
20
+ if DECORD_AVAILABLE:
21
+ vid = VideoReader(video_path, ctx=cpu(0))
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
+
34
+ fps = vid.get_avg_fps() if target_fps == -1 else target_fps
35
+ stride = round(vid.get_avg_fps() / fps)
36
+ stride = max(stride, 1)
37
+ frames_idx = list(range(0, len(vid), stride))
38
+ if process_length != -1 and process_length < len(frames_idx):
39
+ frames_idx = frames_idx[:process_length]
40
+ frames = vid.get_batch(frames_idx).asnumpy()
41
+ else:
42
+ cap = cv2.VideoCapture(video_path)
43
+ original_fps = cap.get(cv2.CAP_PROP_FPS)
44
+ original_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
45
+ original_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
46
+
47
+ if max_res > 0 and max(original_height, original_width) > max_res:
48
+ scale = max_res / max(original_height, original_width)
49
+ height = round(original_height * scale)
50
+ width = round(original_width * scale)
51
+
52
+ fps = original_fps if target_fps < 0 else target_fps
53
+
54
+ stride = max(round(original_fps / fps), 1)
55
+
56
+ frames = []
57
+ frame_count = 0
58
+ while cap.isOpened():
59
+ ret, frame = cap.read()
60
+ if not ret or (process_length > 0 and frame_count >= process_length):
61
+ break
62
+ if frame_count % stride == 0:
63
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Convert BGR to RGB
64
+ if max_res > 0 and max(original_height, original_width) > max_res:
65
+ frame = cv2.resize(frame, (width, height)) # Resize frame
66
+ frames.append(frame)
67
+ frame_count += 1
68
+ cap.release()
69
+ frames = np.stack(frames, axis=0)
70
+
71
+ return frames, fps
72
+
73
+
74
+ def save_video(frames, output_video_path, fps=10, is_depths=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)
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()