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
File size: 1,302 Bytes
7c5f9ba | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | """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)])))
|