簡(jiǎn)單記錄孵淘,防止日后重復(fù)工作
如何判斷正深度逆深度
depth_anything v1/v2輸出的都是逆深度
0代表無(wú)窮遠(yuǎn)
255代表距離為0
- 對(duì)于相對(duì)深度:先看相對(duì)深度圖中遠(yuǎn)近對(duì)應(yīng)位置是黑還是白奔穿,只需建立一個(gè)和輸出深度相同尺寸的向量筋量,全部賦值為0邑茄,看看對(duì)應(yīng)是黑還是白(借助顏色)
zeros_depth = np.zeros(np.shape(depth))
depth = zeros_depth
- 對(duì)于絕對(duì)深度
絕對(duì)深度如何判斷呆瞻?一樣漠烧,把數(shù)據(jù)范圍拉成0-255柴我,仿照相對(duì)深度
Environment
depth_anything for /home/zzx/Depth-Anything/run.py
depth_anything_metric for /home/zzx/Depth-Anything/metric_depth/depth_to_pointcloud.py
Need to fix
File "/home/cjd/Depth-Anything/torchhub/facebookresearch_dinov2_main/vision_transformer.py", line 219, in prepare_tokens_with_masks
x = x + self.interpolate_pos_encoding(x, w, h)
File "/home/cjd/Depth-Anything/torchhub/facebookresearch_dinov2_main/vision_transformer.py", line 199, in interpolate_pos_encoding
patch_pos_embed = nn.functional.interpolate(
TypeError: interpolate() got an unexpected keyword argument 'antialias'
Run Metric Notes
/home/zzx/Depth-Anything/metric_depth/depth_to_pointcloud.py
-
Configure the config file
DATASET = 'nyu'
/home/zzx/Depth-Anything/metric_depth/zoedepth/utils/config.py
-
Choose the pt model file
parser.add_argument
Indoor:
/home/zzx/Depth-Anything/checkpoints/depth_anything_metric_depth_indoor.pt
Outdoor:
/home/zzx/Depth-Anything/checkpoints/depth_anything_metric_depth_outdoor.pt
Process Images and MonoDepth
import cv2
from PIL import Image
import torchvision.transforms as transforms
DATASET = 'nyu'
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
FINAL_HEIGHT = 256
FINAL_WIDTH = 256
color_image = Image.open(image_path).convert('RGB')
image_tensor = transforms.ToTensor()(color_image).unsqueeze(0).to(DEVICE) # 1, 3, 1552, 2326
pred = model(image_tensor, dataset=DATASET)
if isinstance(pred, dict):
pred = pred.get('metric_depth', pred.get('out')) # 1, 1, 392, 518
elif isinstance(pred, (list, tuple)):
pred = pred[-1]
pred = pred.squeeze().detach().cpu().numpy() # 1, 392, 518
# depth pred
pred_color=cv2.applyColorMap(cv2.convertScaleAbs(pred,alpha=15),cv2.COLORMAP_JET)
pred_color=Image.fromarray(pred_color).resize((FINAL_WIDTH, FINAL_HEIGHT), Image.NEAREST)
# photo
resized_color_image = color_image.resize((FINAL_WIDTH, FINAL_HEIGHT), Image.LANCZOS)
# save
photo = Image.new('RGB', (FINAL_WIDTH, FINAL_HEIGHT * 2 ))
photo.paste(resized_color_image, (0, 0))
photo.paste(pred_color, (0, FINAL_HEIGHT))
photo.save(os.path.join(OUTPUT_DIR, os.path.splitext(os.path.basename(image_path))[0] + ".png"))
View the size of PNG file
from PIL import Image
image = Image.open('/home/zzx/Depth-Anything/assets/examples/demo20.png')
width, height = image.size
print("Width:", width)
print("Height:", height)
Merge left and right images
from PIL import Image
import os
# 設(shè)置文件夾路徑
indoor_folder = "/home/zzx/Depth-Anything/output/Metric/Indoor"
outdoor_folder = "/home/zzx/Depth-Anything/output/Metric/Outdoor"
OUTPUT_DIR = '/home/zzx/Depth-Anything/output/Metric/Compare'
indoor_files = os.listdir(indoor_folder)
outdoor_files = os.listdir(outdoor_folder)
for filename in indoor_files:
if filename.endswith(".png"):
indoor_path = os.path.join(indoor_folder, filename)
outdoor_path = os.path.join(outdoor_folder, filename)
indoor_image = Image.open(indoor_path)
outdoor_image = Image.open(outdoor_path)
combined_image = Image.new("RGB", (indoor_image.width * 2, indoor_image.height))
combined_image.paste(indoor_image, (0, 0))
combined_image.paste(outdoor_image, (indoor_image.width, 0))
output_path = os.path.join(OUTPUT_DIR, filename)
combined_image.save(output_path)
Use the metric depth
import sys
sys.path.append('/home/cjd/Depth-Anything/metric_depth')
from zoedepth.models.builder import build_model
from zoedepth.utils.config import get_config
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
DATASET = 'kitti'
config = get_config('zoedepth', "eval", DATASET)
config.pretrained_resource = 'local::/home/cjd/Depth-Anything/checkpoints/depth_anything_metric_depth_outdoor.pt'
mono_model = build_model(config).to(DEVICE)
mono_model.eval()
color_image = Image.open(imfile).convert('RGB')
image_tensor = transforms.ToTensor()(color_image).unsqueeze(0).to(DEVICE)
depth_tensor = mono_model(image_tensor, dataset=DATASET)
if isinstance(depth_tensor, dict):
depth_tensor = depth_tensor.get('metric_depth', depth_tensor.get('out'))
elif isinstance(depth_tensor, (list, tuple)):
depth_tensor = depth_tensor[-1]
depth_tensor = torch.nn.functional.interpolate(depth_tensor.float(),size=(image_size[0], image_size[1]),mode="nearest").squeeze()
depth_tensor[depth_tensor>60.0] = 0.0