Views
No views yet
1git clone https://github.com/MackinationsAi/Upgraded-Depth-Anything-V2.git
2cd Upgraded-Depth-Anything-V2
3one_click_install.bat1cd Upgraded-Depth-Anything-V2
2venv\scripts\activate
3python test.py /path/to/your/image.jpg (or .png)1import cv2
2import torch
3import numpy as np
4import os
5import argparse
6
7from safetensors.torch import load_file
8from depth_anything_v2.dpt import DepthAnythingV2
9
10# Argument parser for input image path
11parser = argparse.ArgumentParser(description="Depth map inference using DepthAnythingV2 model.")
12parser.add_argument("input_image_path", type=str, help="Path to the input image")
13args = parser.parse_args()
14
15# Determine the directory of this script
16script_dir = os.path.dirname(os.path.abspath(__file__))
17
18# Set output path relative to the script directory
19output_image_path = os.path.join(script_dir, "base_udav2_hf-code-test.png")
20checkpoint_path = os.path.join(script_dir, "checkpoints", "depth_anything_v2_vitl.safetensors")
21
22# Device selection: CUDA, MPS, or CPU
23if torch.cuda.is_available():
24 device = torch.device('cuda')
25elif torch.backends.mps.is_available():
26 device = torch.device('mps')
27else:
28 device = torch.device('cpu')
29
30model = DepthAnythingV2(encoder='vitl', features=256, out_channels=[256, 512, 1024, 1024])
31
32state_dict = load_file(checkpoint_path, device='cpu')
33
34model.load_state_dict(state_dict)
35model.to(device)
36model.eval()
37
38# Load the input image
39raw_img = cv2.imread(args.input_image_path)
40
41# Infer the depth map
42depth = model.infer_image(raw_img) # HxW raw depth map
43
44# Normalize the depth map to 0-255 for saving as an image
45depth_normalized = cv2.normalize(depth, None, 0, 255, cv2.NORM_MINMAX)
46depth_normalized = depth_normalized.astype(np.uint8)
47
48cv2.imwrite(output_image_path, depth_normalized)
49print(f"Depth map saved at {output_image_path}")1@article{depth_anything_v2,
2 title={Depth Anything V2},
3 author={Yang, Lihe & Kang, Bingyi & Huang, Zilong & Zhao, Zhen & Xu, Xiaogang & Feng, Jiashi & Zhao, Hengshuang},
4 journal={arXiv:2406.09414},
5 year={2024}
6}
7
8@inproceedings{depth_anything_v1,
9 title={Depth Anything: Unleashing the Power of Large-Scale Unlabeled Data},
10 author={Yang, Lihe & Kang, Bingyi & Huang, Zilong & Xu, Xiaogang & Feng, Jiashi & Zhao, Hengshuang},
11 booktitle={CVPR},
12 year={2024}
13}