Depth-Anything icon indicating copy to clipboard operation
Depth-Anything copied to clipboard

Run metric_depth/evaluate.py in a single image

Open Russell-hub opened this issue 10 months ago • 1 comments

Does any one know how to evaluate a single image's depth instead of a dataset.

Russell-hub avatar Apr 21 '24 07:04 Russell-hub

At this time this would require creating a "custom dataset" and editing various configs. You should be able to use the "pointcloud.py" and "evaluate.py" to create custom code setup do this. I was able to load a custom dataset this way, and even single images by modifying the process_images() in pountcloud.py.

Note: If you can figure out how to acquire the predicted depth close to absolute depth close. Please let me know, I had issues with getting to this point myself and had to move on for the time being. I was not able to use the checkpoint to get a valid depth without a very high error rate. Your results may vary.

The "Infer()" from evaluate.py will be needed.

This is how my main function looks for loading the checkpoint. I kept the settings from pointcloud.py but had to change the "INPUT_DIR" and "OUTPUT_DIR" data/myfiles

main

# Main Function

  if __name__ == "__main__":
      parser = argparse.ArgumentParser()
      parser.add_argument("-m", "--model", type=str, default='zoedepth', help="Name of the model to test")
      print(os.getcwd())
      parser.add_argument("-p", "--pretrained_resource", type=str, default='local::../checkpoints/depth_anything_metric_depth_indoor.pt', help="Pretrained resource to use for fetching weights.")
  
    args = parser.parse_args()
    main(args.model, args.pretrained_resource)

#--- Process Images function
def process_images(model):
    if not os.path.exists(OUTPUT_DIR):
        os.makedirs(OUTPUT_DIR)

    image_paths = glob.glob(os.path.join(INPUT_DIR, '*.png')) + glob.glob(os.path.join(INPUT_DIR, '*.jpg')) + glob.glob(os.path.join(INPUT_DIR, '*.JPEG'))
    for image_path in tqdm(image_paths, desc="Processing Images"):
        try: 
          
            #perform inferences based on evaluate.py that uses zoedepth.
            #open image
            color_image = Image.open(image_path).convert('RGB')
            
            ## perform  transforms
            image_tensor = transforms.ToTensor()(color_image).unsqueeze(0).to('cuda' if torch.cuda.is_available() else 'cpu')
            
            # Perform inference
            pred = infer(model, image_tensor, dataset=DATASET)
            
            #convert image from tensor for visualization and saving
            pred =  colorize(pred.squeeze().cpu().numpy(),0,10)

            Image.fromarray(pred).save(os.path.join(OUTPUT_DIR,  os.path.splitext(os.path.basename(image_path))[0] + "_pred.png"))

        except Exception as e:
            print(f"Error processing {image_path}: {e}") 

I apologize for the funky code blocks. It won't show the way I want. ~I won't have much time to provide any further assistance. Hopefully this information provides a good starting point for you.

Good luck!

kyk37 avatar May 07 '24 16:05 kyk37