DepthPro2-S-ONNX / example.py
christianengel's picture
Focal-free ONNX export of Depth Pro 2 S
a1016d5 verified
Raw History Blame Contribute Delete
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-S-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)])))