LiheYoung commited on
Commit
7f2e027
1 Parent(s): c3dfe03

Delete run_video.py

Browse files
Files changed (1) hide show
  1. run_video.py +0 -92
run_video.py DELETED
@@ -1,92 +0,0 @@
1
- import argparse
2
- import cv2
3
- import glob
4
- import matplotlib
5
- import numpy as np
6
- import os
7
- import torch
8
-
9
- from depth_anything_v2.dpt import DepthAnythingV2
10
-
11
-
12
- if __name__ == '__main__':
13
- parser = argparse.ArgumentParser(description='Depth Anything V2')
14
-
15
- parser.add_argument('--video-path', type=str)
16
- parser.add_argument('--input-size', type=int, default=518)
17
- parser.add_argument('--outdir', type=str, default='./vis_video_depth')
18
-
19
- parser.add_argument('--encoder', type=str, default='vitl', choices=['vits', 'vitb', 'vitl', 'vitg'])
20
-
21
- parser.add_argument('--pred-only', dest='pred_only', action='store_true', help='only display the prediction')
22
- parser.add_argument('--grayscale', dest='grayscale', action='store_true', help='do not apply colorful palette')
23
-
24
- args = parser.parse_args()
25
-
26
- DEVICE = 'cuda' if torch.cuda.is_available() else 'mps' if torch.backends.mps.is_available() else 'cpu'
27
-
28
- # 'we are undergoing company review procedures to release Depth-Anything-Giant checkpoint
29
- model_configs = {
30
- 'vits': {'encoder': 'vits', 'features': 64, 'out_channels': [48, 96, 192, 384]},
31
- 'vitb': {'encoder': 'vitb', 'features': 128, 'out_channels': [96, 192, 384, 768]},
32
- 'vitl': {'encoder': 'vitl', 'features': 256, 'out_channels': [256, 512, 1024, 1024]},
33
- 'vitg': {'encoder': 'vitg', 'features': 384, 'out_channels': [1536, 1536, 1536, 1536]}
34
- }
35
-
36
- depth_anything = DepthAnythingV2(**model_configs[args.encoder])
37
- depth_anything.load_state_dict(torch.load(f'checkpoints/depth_anything_v2_{args.encoder}.pth', map_location='cpu'))
38
- depth_anything = depth_anything.to(DEVICE).eval()
39
-
40
- if os.path.isfile(args.video_path):
41
- if args.video_path.endswith('txt'):
42
- with open(args.video_path, 'r') as f:
43
- lines = f.read().splitlines()
44
- else:
45
- filenames = [args.video_path]
46
- else:
47
- filenames = glob.glob(os.path.join(args.video_path, '**/*'), recursive=True)
48
-
49
- os.makedirs(args.outdir, exist_ok=True)
50
-
51
- margin_width = 50
52
- cmap = matplotlib.colormaps.get_cmap('Spectral_r')
53
-
54
- for k, filename in enumerate(filenames):
55
- print(f'Progress {k+1}/{len(filenames)}: {filename}')
56
-
57
- raw_video = cv2.VideoCapture(filename)
58
- frame_width, frame_height = int(raw_video.get(cv2.CAP_PROP_FRAME_WIDTH)), int(raw_video.get(cv2.CAP_PROP_FRAME_HEIGHT))
59
- frame_rate = int(raw_video.get(cv2.CAP_PROP_FPS))
60
- if args.pred_only:
61
- output_width = frame_width
62
- else:
63
- output_width = frame_width * 2 + margin_width
64
-
65
- output_path = os.path.join(args.outdir, os.path.splitext(os.path.basename(filename))[0] + '.mp4')
66
- out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*"mp4v"), frame_rate, (output_width, frame_height))
67
-
68
- while raw_video.isOpened():
69
- ret, raw_frame = raw_video.read()
70
- if not ret:
71
- break
72
-
73
- depth = depth_anything.infer_image(raw_frame, args.input_size)
74
-
75
- depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0
76
- depth = depth.astype(np.uint8)
77
-
78
- if args.grayscale:
79
- depth = np.repeat(depth[..., np.newaxis], 3, axis=-1)
80
- else:
81
- depth = (cmap(depth)[:, :, :3] * 255)[:, :, ::-1].astype(np.uint8)
82
-
83
- if args.pred_only:
84
- out.write(depth)
85
- else:
86
- split_region = np.ones((frame_height, margin_width, 3), dtype=np.uint8) * 255
87
- combined_frame = cv2.hconcat([raw_frame, split_region, depth])
88
-
89
- out.write(combined_frame)
90
-
91
- raw_video.release()
92
- out.release()