Instructions to use christianengel/DepthPro2-B-ONNX with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- TensorRT
How to use christianengel/DepthPro2-B-ONNX with TensorRT:
# No code snippets available yet for this library. # To use this model, check the repository files and the library's documentation. # Want to help? PRs adding snippets are welcome at: # https://github.com/huggingface/huggingface.js
- Notebooks
- Google Colab
- Kaggle
Download example.py from christianengel/DepthPro2-B-ONNX: direct link, hf CLI and curl.
- Browser
- Download file 1.3 kB
-
https://huggingface.co/christianengel/DepthPro2-B-ONNX/resolve/main/example.py
- Command line
-
hf download hf://christianengel/DepthPro2-B-ONNX/example.py
-
curl -L -o example.py https://huggingface.co/christianengel/DepthPro2-B-ONNX/resolve/main/example.py
1.3 kB
| """Metric depth from one photo with a known focal length.""" | |
| import json | |
| import numpy as np | |
| import onnxruntime as ort | |
| from huggingface_hub import hf_hub_download | |
| from PIL import Image | |
| repo = "christianengel/DepthPro2-B-ONNX" | |
| config = json.load(open(hf_hub_download(repo, "config.json"))) | |
| height, width = config["input"]["height"], config["input"]["width"] | |
| session = ort.InferenceSession( | |
| hf_hub_download(repo, "onnx/model_fp16.onnx"), | |
| providers=["CUDAExecutionProvider", "CPUExecutionProvider"], | |
| ) | |
| image = Image.open("photo.jpg").convert("RGB") | |
| fx = 1400.0 # horizontal focal length of photo.jpg in pixels, e.g. CameraInfo.k[0] | |
| # Scale to cover the input size and center-crop; the focal length scales with the image. | |
| scale = max(width / image.width, height / image.height) | |
| size = (max(width, round(image.width * scale)), max(height, round(image.height * scale))) | |
| left, top = (size[0] - width) // 2, (size[1] - height) // 2 | |
| image = image.resize(size, Image.Resampling.LANCZOS).crop((left, top, left + width, top + height)) | |
| pixels = np.asarray(image, np.float32).transpose(2, 0, 1)[None] / 255 | |
| f_px = np.array([fx * scale], np.float32) | |
| depth = session.run(None, {"pixel_values": pixels, "f_px": f_px})[0][0] # (H, W) meters | |
| print(depth.shape, float(np.median(depth[np.isfinite(depth)]))) | |