|
|
import cv2
|
|
|
import torch
|
|
|
import numpy as np
|
|
|
import os
|
|
|
from depth_anything_v2.dpt import DepthAnythingV2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DEVICE = 'cuda' if torch.cuda.is_available() else \
|
|
|
'mps' if torch.backends.mps.is_available() else 'cpu'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model_configs = {
|
|
|
'vits': {'encoder': 'vits', 'features': 64, 'out_channels': [48, 96, 192, 384]},
|
|
|
'vitb': {'encoder': 'vitb', 'features': 128, 'out_channels': [96, 192, 384, 768]},
|
|
|
'vitl': {'encoder': 'vitl', 'features': 256, 'out_channels': [256, 512, 1024, 1024]},
|
|
|
'vitg': {'encoder': 'vitg', 'features': 384, 'out_channels': [1536, 1536, 1536, 1536]},
|
|
|
}
|
|
|
|
|
|
encoder = 'vits'
|
|
|
|
|
|
model = DepthAnythingV2(**model_configs[encoder])
|
|
|
model.load_state_dict(torch.load(f'checkpoints/depth_anything_v2_{encoder}.pth', map_location='cpu'))
|
|
|
model = model.to(DEVICE).eval()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
input_path = '/root/1/img/orig_001_i0.png'
|
|
|
output_dir = '/root/1/depth_output'
|
|
|
os.makedirs(output_dir, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
raw_img = cv2.imread(input_path)
|
|
|
if raw_img is None:
|
|
|
raise FileNotFoundError(f"未找到图像文件: {input_path}")
|
|
|
|
|
|
depth = model.infer_image(raw_img)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
npy_path = os.path.join(output_dir, 'depth.npy')
|
|
|
np.save(npy_path, depth)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
depth_vis = (depth - depth.min()) / (depth.max() - depth.min() + 1e-8)
|
|
|
depth_vis = (depth_vis * 255).astype(np.uint8)
|
|
|
depth_color = cv2.applyColorMap(depth_vis, cv2.COLORMAP_INFERNO)
|
|
|
cv2.imwrite(os.path.join(output_dir, 'depth_vis.png'), depth_color)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
depth_tensor = torch.tensor(depth_vis, dtype=torch.float32) / 255.0
|
|
|
depth_tensor = depth_tensor.unsqueeze(0).repeat(3, 1, 1).unsqueeze(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
torch.save(depth_tensor, os.path.join(output_dir, 'depth_tensor.pt'))
|
|
|
|
|
|
print(f"✅ 深度推理完成:")
|
|
|
print(f" - 原始深度: {npy_path}")
|
|
|
print(f" - 可视化图像: {os.path.join(output_dir, 'depth_vis.png')}")
|
|
|
print(f" - Tensor文件: {os.path.join(output_dir, 'depth_tensor.pt')}")
|
|
|
print(f"Tensor形状: {depth_tensor.shape}")
|
|
|
|