Download examples/quickstart.py from changh95/DepthPro2: direct link, hf CLI and curl.
- Browser
- Download file 2.29 kB
-
https://huggingface.co/changh95/DepthPro2/resolve/main/examples/quickstart.py
- Command line
-
hf download hf://changh95/DepthPro2/examples/quickstart.py
-
curl -L -o quickstart.py https://huggingface.co/changh95/DepthPro2/resolve/main/examples/quickstart.py
2.29 kB
| #!/usr/bin/env python3 | |
| # SPDX-License-Identifier: Apache-2.0 | |
| """DepthPro2 quickstart (the Python API): depth of the shipped sample image, then of a short clip. | |
| python examples/quickstart.py [--size L] [--image photo.jpg] [--cpu] | |
| Needs ttnn (a tt-metal build with the patches in ../patches/, or the container image) and one p150; ``--cpu`` runs | |
| the same pipeline on the float32 CPU reference instead (slow, no chip). The weights are downloaded from Apple's | |
| repositories at the pinned commits (Apple Machine Learning Research Model License: research purposes only). | |
| """ | |
| import argparse | |
| import sys | |
| from pathlib import Path | |
| sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "code")) | |
| from tt_depthpro2 import DepthPro2, load_sample # noqa: E402 | |
| def main() -> None: | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--size", default="L", help="S, B, L or H") | |
| ap.add_argument("--image", default=None, help="an image file (default: the shipped sample)") | |
| ap.add_argument("--cpu", action="store_true", help="the float32 CPU reference instead of the p150") | |
| a = ap.parse_args() | |
| image = a.image or load_sample() | |
| with DepthPro2.from_pretrained(a.size, backend="cpu" if a.cpu else "tt") as model: | |
| out = model.infer(image) | |
| st = out.stats() | |
| print(f"{model!r}\n{Path(image).name}: {out.input_size[1]}x{out.input_size[0]} (bucket {out.bucket}, " | |
| f"resized {out.resized}), f_px {out.focallength_px:.1f} ({out.focal_source}), depth median " | |
| f"{st['median']:.2f} m (5-95 %: {st['p05']:.2f}-{st['p95']:.2f} m), {st['infinite_pixels']} sky pixels, " | |
| f"{out.timing_ms['total']:.1f} ms") | |
| png, meta = out.png16() # uint16 = round(depth_m * 1000); 0 = infinite (sky) | |
| Path("depth16.png").write_bytes(png) | |
| print("wrote depth16.png", meta["shape"], "saturated", meta["saturated_pixels"]) | |
| # a video: frames of one size, buffered clips of `window` frames (here the image repeated) | |
| frames = [image] * model.config.window | |
| for frame in model.infer_video(frames): | |
| if frame.frame_index in (0, len(frames) - 1): | |
| print(f"video frame {frame.frame_index}: median {frame.stats()['median']:.2f} m") | |
| if __name__ == "__main__": | |
| main() | |