Advertisement
Guest User

Depth anything simplified and 16 bit

a guest
Jun 23rd, 2024
618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. import argparse
  2. import cv2
  3. import glob
  4. import matplotlib
  5. import numpy as np
  6. import os
  7. import torch
  8. import warnings
  9. from tqdm import tqdm
  10. from safetensors.torch import load_file
  11.  
  12. from depth_anything_v2.dpt import DepthAnythingV2
  13.  
  14. # Code upgraded by: MackinationsAi
  15.  
  16. warnings.filterwarnings("ignore", message=".*cudnnStatus.*")
  17.  
  18. def process_image(img_path, output_path, input_size, encoder, pred_only, grayscale):
  19. DEVICE = 'cuda' if torch.cuda.is_available() else 'mps' if torch.backends.mps.is_available() else 'cpu'
  20.  
  21. model_configs = {
  22. 'vits': {'encoder': 'vits', 'features': 64, 'out_channels': [48, 96, 192, 384]},
  23. 'vitb': {'encoder': 'vitb', 'features': 128, 'out_channels': [96, 192, 384, 768]},
  24. 'vitl': {'encoder': 'vitl', 'features': 256, 'out_channels': [256, 512, 1024, 1024]},
  25. 'vitg': {'encoder': 'vitg', 'features': 384, 'out_channels': [1536, 1536, 1536, 1536]}
  26. }
  27.  
  28. depth_anything = DepthAnythingV2(**model_configs[encoder])
  29. state_dict = load_file(f'checkpoints/depth_anything_v2_{encoder}.safetensors')
  30. depth_anything.load_state_dict(state_dict)
  31. depth_anything = depth_anything.to(DEVICE).eval()
  32.  
  33. if os.path.isfile(img_path) and (img_path.endswith('.png') or img_path.endswith('.jpg') or img_path.endswith('.jpeg')):
  34. filenames = [img_path]
  35. else:
  36. filenames = glob.glob(os.path.join(img_path, '**/*.*'), recursive=True)
  37. filenames = [f for f in filenames if f.endswith(('.png', '.jpg', '.jpeg'))]
  38.  
  39. os.makedirs(output_path, exist_ok=True)
  40.  
  41. cmap = matplotlib.colormaps.get_cmap('gray')
  42.  
  43. for k, filename in enumerate(tqdm(filenames, desc="Processing images", unit="image")):
  44. print(f'Progress {k+1}/{len(filenames)}: {filename}')
  45.  
  46. raw_image = cv2.imread(filename)
  47.  
  48. depth = depth_anything.infer_image(raw_image, input_size)
  49. depth = (depth - depth.min()) / (depth.max() - depth.min()) * 65025.0
  50. depth = depth.astype(np.uint16)
  51.  
  52. depth_gray = np.repeat(depth[..., np.newaxis], 3, axis=-1)
  53. cv2.imwrite(os.path.join(output_path, os.path.splitext(os.path.basename(filename))[0] + '_depth_grayscale.png'), depth_gray)
  54.  
  55. def main():
  56. while True:
  57. parser = argparse.ArgumentParser(description='Depth Anything V2')
  58.  
  59. parser.add_argument('--img-path', type=str, help='Path to the image file or directory containing images')
  60. parser.add_argument('--input-size', type=int, default=2018)
  61. parser.add_argument('--outdir', type=str, default='vis_img_depth', help='Output directory')
  62.  
  63. parser.add_argument('--encoder', type=str, default='vitl', choices=['vits', 'vitb', 'vitl', 'vitg'])
  64. parser.add_argument('--pred-only', dest='pred_only', action='store_true', help='only display the prediction')
  65. parser.add_argument('--grayscale', dest='grayscale', action='store_true', help='do not apply colorful palette')
  66.  
  67. args = parser.parse_args()
  68.  
  69. if not args.img_path:
  70. args.img_path = input("Path to image file/directory, can right click a file and Copy as Path, remove quotation marks if any: ").strip()
  71.  
  72. if not args.outdir:
  73. args.outdir = input("Please enter the output directory (default is 'vis_img_depth'): ").strip() or 'vis_depth'
  74.  
  75. process_image(args.img_path, args.outdir, args.input_size, args.encoder, args.pred_only, args.grayscale)
  76.  
  77. again = input("Convert another image? Y/N: ").strip().lower()
  78. if again not in ['y', 'yes']:
  79. break
  80.  
  81. if __name__ == '__main__':
  82. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement