VanLinLin commited on
Commit
5d472bd
·
1 Parent(s): 7325378

change get_depth_normap.py location

Browse files
README.md CHANGED
@@ -41,7 +41,10 @@ git clone https://github.com/DepthAnything/Depth-Anything-V2.git
41
  ```
42
  2. Download the [pretrain model of depth anything v2](https://huggingface.co/depth-anything/Depth-Anything-V2-Large/resolve/main/depth_anything_v2_vitl.pth?download=true)
43
 
44
- 3. Run ```python Depth-Anything-V2/get_depth_normap.py```
 
 
 
45
 
46
  Now folder structure will be
47
  ```bash
@@ -65,12 +68,12 @@ output_dir
65
  ├──...
66
  ```
67
 
68
- 4. Clone [DINOv2](https://github.com/facebookresearch/dinov2.git)
69
  ```bash
70
  git clone https://github.com/facebookresearch/dinov2.git
71
  ```
72
 
73
- 5. Download [shadow removal weight](https://drive.google.com/file/d/1USD5sLvEcgFqIg7BDzc1OuInzSx3GnUN/view?usp=drive_link)
74
 
75
  ```bash
76
  gdown 1USD5sLvEcgFqIg7BDzc1OuInzSx3GnUN
 
41
  ```
42
  2. Download the [pretrain model of depth anything v2](https://huggingface.co/depth-anything/Depth-Anything-V2-Large/resolve/main/depth_anything_v2_vitl.pth?download=true)
43
 
44
+ 3. Run ```get_depth_normap.py``` to create depth and normal map.
45
+ ```python
46
+ python get_depth_normap.py
47
+ ```
48
 
49
  Now folder structure will be
50
  ```bash
 
68
  ├──...
69
  ```
70
 
71
+ 1. Clone [DINOv2](https://github.com/facebookresearch/dinov2.git)
72
  ```bash
73
  git clone https://github.com/facebookresearch/dinov2.git
74
  ```
75
 
76
+ 1. Download [shadow removal weight](https://drive.google.com/file/d/1USD5sLvEcgFqIg7BDzc1OuInzSx3GnUN/view?usp=drive_link)
77
 
78
  ```bash
79
  gdown 1USD5sLvEcgFqIg7BDzc1OuInzSx3GnUN
get_depth_normap.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ sys.path.append('Depth-Anything-V2')
3
+
4
+ import cv2
5
+ import torch
6
+ import matplotlib.pyplot as plt
7
+ import numpy as np
8
+ from PIL import Image
9
+ from depth_anything_v2.dpt import DepthAnythingV2
10
+ from pathlib import Path
11
+ from tqdm.auto import tqdm
12
+ import argparse
13
+
14
+
15
+ def parse_args():
16
+ parser = argparse.ArgumentParser(description='Generate depth and normal maps from images')
17
+ parser.add_argument('--source_root', type=str, default='test_dir',
18
+ help='Root directory containing the images')
19
+ parser.add_argument('--model_path', type=str,
20
+ default='depth_anything_v2_vitl.pth',
21
+ help='Path to the depth model checkpoint')
22
+ return parser.parse_args()
23
+
24
+
25
+ def generate_depth_maps(source_root, model_path):
26
+ source_root = Path(source_root)
27
+ origin = source_root / 'origin'
28
+ to_thermal_list = [origin]
29
+
30
+ model = DepthAnythingV2(encoder='vitl', features=256, out_channels=[256, 512, 1024, 1024]).cuda()
31
+ model.load_state_dict(torch.load(model_path, map_location='cpu'))
32
+ model.eval()
33
+
34
+ thermal_path = source_root / 'depth'
35
+
36
+ with torch.inference_mode():
37
+ for to_thermal_item in to_thermal_list:
38
+ folder_name = to_thermal_item.stem
39
+ dst_path = thermal_path
40
+
41
+ dst_path.mkdir(parents=True, exist_ok=True)
42
+
43
+ bar = tqdm(to_thermal_item.glob('*'))
44
+
45
+ for image_path in bar:
46
+ try:
47
+ raw_img = cv2.imread(str(image_path))
48
+ depth = model.infer_image(raw_img) # HxW raw depth map
49
+
50
+ depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0
51
+ depth = depth.astype(np.uint8)
52
+
53
+ print(depth.shape)
54
+ np.save(f'{dst_path}/{image_path.stem}.npy', depth)
55
+
56
+ except Exception as e:
57
+ print(e)
58
+ continue
59
+
60
+ return thermal_path
61
+
62
+
63
+ def calculate_normal_map(img_path: Path, ksize=5):
64
+ # 讀取深度圖
65
+ depth = np.load(img_path).astype(np.float32)
66
+
67
+ # 計算 X、Y 方向的梯度
68
+ dx = cv2.Sobel(depth, cv2.CV_32F, 1, 0, ksize=ksize)
69
+ dy = cv2.Sobel(depth, cv2.CV_32F, 0, 1, ksize=ksize)
70
+
71
+ # 假設 Z 軸方向為 -1
72
+ dz = np.ones_like(dx) * -1
73
+
74
+ # 組合成法向量 (Nx, Ny, Nz)
75
+ normals = np.stack((dx, dy, dz), axis=-1)
76
+
77
+ # 進行歸一化
78
+ norm = np.linalg.norm(normals, axis=-1, keepdims=True)
79
+ normals /= (norm + 1e-6) # 避免除零錯誤
80
+
81
+ # 轉換為 0-255 的 RGB 影像 (HWC)
82
+ normal_map = (normals + 1) / 2 * 255
83
+ normal_map = normal_map.astype("uint8")
84
+
85
+ normal_map = normal_map.transpose(2, 0, 1) # (H, W, C) -> (C, H, W)
86
+
87
+ return normal_map
88
+
89
+
90
+ def generate_normal_maps(source_root, ksize=5):
91
+ source_root = Path(source_root)
92
+ depth_root = source_root / 'depth'
93
+ normal_root = source_root / 'normal'
94
+ normal_root.mkdir(parents=True, exist_ok=True)
95
+
96
+ bar = tqdm(list(depth_root.glob('*.npy')))
97
+
98
+ for depth_img_path in bar:
99
+ img_name = depth_img_path.name
100
+
101
+ normal_map = calculate_normal_map(depth_img_path, ksize=ksize)
102
+
103
+ np.save(f'{normal_root}/{img_name}', normal_map)
104
+
105
+
106
+ def main():
107
+ args = parse_args()
108
+
109
+ print(f"Generating depth maps from images in {args.source_root}")
110
+ depth_path = generate_depth_maps(args.source_root, args.model_path)
111
+
112
+ print(f"Generating normal maps from depth maps")
113
+ generate_normal_maps(args.source_root)
114
+
115
+ print("Processing complete!")
116
+
117
+
118
+ if __name__ == "__main__":
119
+ main()
utils/__pycache__/__init__.cpython-39.pyc ADDED
Binary file (277 Bytes). View file
 
utils/__pycache__/dataset_utils.cpython-39.pyc ADDED
Binary file (2.29 kB). View file
 
utils/__pycache__/dir_utils.cpython-39.pyc ADDED
Binary file (889 Bytes). View file
 
utils/__pycache__/image_utils.cpython-39.pyc ADDED
Binary file (6.53 kB). View file
 
utils/__pycache__/loader.cpython-39.pyc ADDED
Binary file (758 Bytes). View file
 
utils/__pycache__/misc.cpython-39.pyc ADDED
Binary file (4.39 kB). View file
 
utils/__pycache__/model_utils.cpython-39.pyc ADDED
Binary file (3.46 kB). View file
 
utils/__pycache__/shadow_mask_evaluate.cpython-39.pyc ADDED
Binary file (4.13 kB). View file
 
utils/__pycache__/tta.cpython-39.pyc ADDED
Binary file (5.03 kB). View file