File size: 3,327 Bytes
2a3f6b2
 
 
 
 
 
 
 
 
 
 
0151d07
2a3f6b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
---
license: cc-by-nc-4.0

language:
- en
pipeline_tag: depth-estimation
tags:
- depth
- relative depth
---

# Depth-Anything-V2-Large (Transformers version)

## Introduction
Depth Anything V2 is trained from 595K synthetic labeled images & 62M+ real unlabeled images, providing the most capable monocular depth estimation (MDE) model with the following features:
- more fine-grained details than Depth Anything V1
- more robust than Depth Anything V1 & SD-based models (e.g., Marigold, Geowizard)
- more efficient (10x faster) & more lightweight than SD-based models
- impressive fine-tuned performance with our pre-trained models

## Installation

```bash
git clone https://github.com/MackinationsAi/Upgraded-Depth-Anything-V2.git
cd Upgraded-Depth-Anything-V2
one_click_install.bat
```

## Usage

Please refer to the [README.md](https://github.com/MackinationsAi/Upgraded-Depth-Anything-V2/blob/main/README.md) for actual usage.

## Test Code

```bash
cd Upgraded-Depth-Anything-V2
venv\scripts\activate
python test.py /path/to/your/image.jpg (or .png)
```
Create a test.py script using the code below:

```python
import cv2
import torch
import numpy as np
import os
import argparse

from safetensors.torch import load_file
from depth_anything_v2.dpt import DepthAnythingV2

# Argument parser for input image path
parser = argparse.ArgumentParser(description="Depth map inference using DepthAnythingV2 model.")
parser.add_argument("input_image_path", type=str, help="Path to the input image")
args = parser.parse_args()

# Determine the directory of this script
script_dir = os.path.dirname(os.path.abspath(__file__))

# Set output path relative to the script directory
output_image_path = os.path.join(script_dir, "base_udav2_hf-code-test.png")
checkpoint_path = os.path.join(script_dir, "checkpoints", "depth_anything_v2_vitl.safetensors")

# Device selection: CUDA, MPS, or CPU
if torch.cuda.is_available():
    device = torch.device('cuda')
elif torch.backends.mps.is_available():
    device = torch.device('mps')
else:
    device = torch.device('cpu')

model = DepthAnythingV2(encoder='vitl', features=256, out_channels=[256, 512, 1024, 1024])

state_dict = load_file(checkpoint_path, device='cpu')

model.load_state_dict(state_dict)
model.to(device)
model.eval()

# Load the input image
raw_img = cv2.imread(args.input_image_path)

# Infer the depth map
depth = model.infer_image(raw_img)  # HxW raw depth map

# Normalize the depth map to 0-255 for saving as an image
depth_normalized = cv2.normalize(depth, None, 0, 255, cv2.NORM_MINMAX)
depth_normalized = depth_normalized.astype(np.uint8)

cv2.imwrite(output_image_path, depth_normalized)
print(f"Depth map saved at {output_image_path}")
```

## Citation

If you find this project useful, please consider citing [MackinationsAi](https://github.com/MackinationsAi/) & the following:

```bibtex
@article{depth_anything_v2,
  title={Depth Anything V2},
  author={Yang, Lihe & Kang, Bingyi & Huang, Zilong & Zhao, Zhen & Xu, Xiaogang & Feng, Jiashi & Zhao, Hengshuang},
  journal={arXiv:2406.09414},
  year={2024}
}

@inproceedings{depth_anything_v1,
  title={Depth Anything: Unleashing the Power of Large-Scale Unlabeled Data}, 
  author={Yang, Lihe & Kang, Bingyi & Huang, Zilong & Xu, Xiaogang & Feng, Jiashi & Zhao, Hengshuang},
  booktitle={CVPR},
  year={2024}
}