-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquery.py
More file actions
162 lines (148 loc) · 4.53 KB
/
Copy pathquery.py
File metadata and controls
162 lines (148 loc) · 4.53 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"""A CLI for the Vuforia Cloud Recognition Service API."""
import contextlib
import dataclasses
import io
import sys
from collections.abc import Generator
from pathlib import Path
import click
import yaml
from beartype import beartype
from vws import CloudRecoService
from vws.exceptions.cloud_reco_exceptions import (
AuthenticationFailureError,
BadImageError,
InactiveProjectError,
RequestTimeTooSkewedError,
)
from vws.exceptions.custom_exceptions import (
RequestEntityTooLargeError,
)
from vws.include_target_data import CloudRecoIncludeTargetData
from vws_cli import __version__
from vws_cli.options.credentials import (
client_access_key_option,
client_secret_key_option,
)
from vws_cli.options.timeout import (
connection_timeout_seconds_option,
read_timeout_seconds_option,
)
@beartype
@contextlib.contextmanager
def _handle_vwq_exceptions() -> Generator[None]:
"""Show error messages and catch exceptions from ``VWS-Python``."""
try:
yield
except BadImageError:
error_message = (
"Error: The given image is corrupted or the format is not "
"supported."
)
except InactiveProjectError:
error_message = (
"Error: The project associated with the given keys is inactive."
)
except AuthenticationFailureError:
error_message = "The given secret key was incorrect."
except RequestTimeTooSkewedError:
error_message = (
"Error: Vuforia reported that the time given with this request "
"was outside the expected range. "
"This may be because the system clock is out of sync."
)
except RequestEntityTooLargeError:
error_message = "Error: The given image is too large."
else:
return
click.echo(message=error_message, err=True)
sys.exit(1)
_image_argument = click.argument(
"image",
type=click.Path(
exists=True,
file_okay=True,
dir_okay=False,
path_type=Path,
),
)
_MAX_NUM_RESULTS_DEFAULT = 50
_max_num_results_option = click.option(
"--max-num-results",
type=click.IntRange(min=1, max=_MAX_NUM_RESULTS_DEFAULT),
default=1,
help=(
"The maximum number of matching targets to be returned. "
f"Must be <= {_MAX_NUM_RESULTS_DEFAULT}."
),
show_default=True,
)
_include_target_data_option = click.option(
"--include-target-data",
type=click.Choice(
choices=CloudRecoIncludeTargetData,
case_sensitive=False,
),
default=CloudRecoIncludeTargetData.TOP.lower(),
help=(
"Whether target_data records shall be returned for the matched "
"targets. Accepted values are top (default value, only return "
"target_data for top ranked match), none (return no target_data), "
"all (for all matched targets)."
),
show_default=True,
)
_base_vwq_url_option = click.option(
"--base-vwq-url",
type=click.STRING,
default="https://cloudreco.vuforia.com",
help="The base URL for the VWQ API.",
show_default=True,
)
@click.command(name="vuforia-cloud-reco")
@_image_argument
@client_access_key_option
@client_secret_key_option
@_include_target_data_option
@_max_num_results_option
@_base_vwq_url_option
@connection_timeout_seconds_option
@read_timeout_seconds_option
@_handle_vwq_exceptions()
# We set the ``version`` parameter because in PyInstaller binaries,
# ``pkg_resources`` is not available.
#
# Click uses ``pkg_resources`` to determine the version if it is not given.
@click.version_option(version=__version__)
@beartype
def vuforia_cloud_reco(
*,
image: Path,
client_access_key: str,
client_secret_key: str,
max_num_results: int,
include_target_data: CloudRecoIncludeTargetData,
base_vwq_url: str,
connection_timeout_seconds: float,
read_timeout_seconds: float,
) -> None:
"""Make a request to the Vuforia Cloud Recognition Service API."""
client = CloudRecoService(
client_access_key=client_access_key,
client_secret_key=client_secret_key,
base_vwq_url=base_vwq_url,
request_timeout_seconds=(
connection_timeout_seconds,
read_timeout_seconds,
),
)
query_result = client.query(
image=io.BytesIO(initial_bytes=image.read_bytes()),
max_num_results=max_num_results,
include_target_data=include_target_data,
)
query_result_dict_list = [
dataclasses.asdict(obj=res) for res in query_result
]
yaml_list = yaml.dump(data=query_result_dict_list)
click.echo(message=yaml_list)