Spaces:
Running
on
Zero
Running
on
Zero
Delete run.py
Browse files
run.py
DELETED
@@ -1,74 +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('--img-path', type=str)
|
16 |
-
parser.add_argument('--input-size', type=int, default=518)
|
17 |
-
parser.add_argument('--outdir', type=str, default='./vis_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.img_path):
|
41 |
-
if args.img_path.endswith('txt'):
|
42 |
-
with open(args.img_path, 'r') as f:
|
43 |
-
filenames = f.read().splitlines()
|
44 |
-
else:
|
45 |
-
filenames = [args.img_path]
|
46 |
-
else:
|
47 |
-
filenames = glob.glob(os.path.join(args.img_path, '**/*'), recursive=True)
|
48 |
-
|
49 |
-
os.makedirs(args.outdir, exist_ok=True)
|
50 |
-
|
51 |
-
cmap = matplotlib.colormaps.get_cmap('Spectral_r')
|
52 |
-
|
53 |
-
for k, filename in enumerate(filenames):
|
54 |
-
print(f'Progress {k+1}/{len(filenames)}: {filename}')
|
55 |
-
|
56 |
-
raw_image = cv2.imread(filename)
|
57 |
-
|
58 |
-
depth = depth_anything.infer_image(raw_image, args.input_size)
|
59 |
-
|
60 |
-
depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0
|
61 |
-
depth = depth.astype(np.uint8)
|
62 |
-
|
63 |
-
if args.grayscale:
|
64 |
-
depth = np.repeat(depth[..., np.newaxis], 3, axis=-1)
|
65 |
-
else:
|
66 |
-
depth = (cmap(depth)[:, :, :3] * 255)[:, :, ::-1].astype(np.uint8)
|
67 |
-
|
68 |
-
if args.pred_only:
|
69 |
-
cv2.imwrite(os.path.join(args.outdir, os.path.splitext(os.path.basename(filename))[0] + '.png'), depth)
|
70 |
-
else:
|
71 |
-
split_region = np.ones((raw_image.shape[0], 50, 3), dtype=np.uint8) * 255
|
72 |
-
combined_result = cv2.hconcat([raw_image, split_region, depth])
|
73 |
-
|
74 |
-
cv2.imwrite(os.path.join(args.outdir, os.path.splitext(os.path.basename(filename))[0] + '.png'), combined_result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|