File size: 1,984 Bytes
2b156ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# ---------------------------------------------------------------------
# Copyright (c) 2026 Qualcomm Technologies, Inc. and/or its subsidiaries.
# SPDX-License-Identifier: BSD-3-Clause
# ---------------------------------------------------------------------

from __future__ import annotations

from qai_hub_models.utils.args import (
    demo_model_from_cli_args,
    get_model_cli_parser,
    get_on_device_demo_parser,
    validate_on_device_demo_args,
)
from qai_hub_models.utils.asset_loaders import CachedWebModelAsset, load_image
from qai_hub_models.utils.display import display_or_save_image

from .app import DepthProApp
from .model import MODEL_ID, DepthPro

# Reuse the midas depth-estimation fixture — any indoor/outdoor natural image
# works; keeping this out-of-tree avoids uploading a fresh asset just for
# the initial recipe.
INPUT_IMAGE_ADDRESS = CachedWebModelAsset.from_asset_store(
    "midas", 3, "test_input_image.jpg"
)


def main(is_test: bool = False) -> None:
    parser = get_model_cli_parser(DepthPro)
    parser = get_on_device_demo_parser(parser, add_output_dir=True)
    parser.add_argument(
        "--image",
        type=str,
        default=INPUT_IMAGE_ADDRESS,
        help="image file path or URL",
    )
    args = parser.parse_args([] if is_test else None)
    model = demo_model_from_cli_args(DepthPro, MODEL_ID, args)
    validate_on_device_demo_args(args, MODEL_ID)

    (_, _, height, width) = model.get_input_spec()["image"][0]
    image = load_image(args.image)
    print("Model Loaded")

    app = DepthProApp(model, height, width)  # type: ignore[arg-type]
    prediction = app.estimate_depth(image)

    print(
        f"Predicted field of view: {prediction.field_of_view:.2f} deg  "
        f"(focal length: {prediction.focal_length_px:.1f} px)"
    )

    if not is_test:
        display_or_save_image(
            prediction.heatmap, args.output_dir, "out_heatmap.png", "heatmap"
        )


if __name__ == "__main__":
    main()