Skip to content

Commit 0d454a1

Browse files
Merge pull request #1041 from adamtheturtle/include-target-data
Include target data
2 parents 7220a28 + b302627 commit 0d454a1

File tree

6 files changed

+162
-0
lines changed

6 files changed

+162
-0
lines changed

docs/source/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
html_show_copyright = False
7777
html_show_sphinx = False
7878
html_show_sourcelink = False
79+
autoclass_content = 'both'
7980

8081
html_theme_options = {
8182
'show_powered_by': 'false',

docs/source/index.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ Reference
1515
---------
1616

1717
.. automodule:: vws
18+
:undoc-members:
19+
:members:
20+
21+
.. automodule:: vws.include_target_data
22+
:undoc-members:
1823
:members:
1924

2025
.. toctree::

spelling_private_dict.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
AuthenticationFailure
22
BadImage
33
ImageTooLarge
4+
MaxNumResultsOutOfRange
45
MetadataTooLarge
56
ProjectInactive
67
TargetNameExist

src/vws/include_target_data.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
Tools for managing ``CloudRecoService.query``'s ``include_target_data``.
3+
"""
4+
5+
from enum import Enum
6+
7+
8+
class CloudRecoIncludeTargetData(Enum):
9+
"""
10+
Options for the ``include_target_data`` parameter of foo.
11+
"""
12+
13+
TOP = 'top'
14+
NONE = 'none'
15+
ALL = 'all'

src/vws/query.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from ._authorization import authorization_header, rfc_1123_date
1313
from ._result_codes import raise_for_result_code
1414
from .exceptions import MaxNumResultsOutOfRange
15+
from .include_target_data import CloudRecoIncludeTargetData
1516

1617

1718
class CloudRecoService:
@@ -39,6 +40,8 @@ def query(
3940
self,
4041
image: io.BytesIO,
4142
max_num_results: int = 1,
43+
include_target_data:
44+
CloudRecoIncludeTargetData = CloudRecoIncludeTargetData.TOP,
4245
) -> List[Dict[str, Any]]:
4346
"""
4447
Use the Vuforia Web Query API to make an Image Recognition Query.
@@ -49,6 +52,16 @@ def query(
4952
5053
Args:
5154
image: The image to make a query against.
55+
max_num_results: The maximum number of matching targets to be
56+
returned.
57+
include_target_data: "Indicates if target_data records shall be
58+
returned for the matched targets. Accepted values are top
59+
(default value, only return target_data for top ranked match),
60+
none (return no target_data), all (for all matched targets)".
61+
62+
Raises:
63+
~vws.exceptions.MaxNumResultsOutOfRange: `max_num_results`` is not
64+
within the range (1, 50).
5265
5366
Returns:
5467
An ordered list of target details of matching targets.
@@ -57,6 +70,8 @@ def query(
5770
body = {
5871
'image': ('image.jpeg', image_content, 'image/jpeg'),
5972
'max_num_results': (None, int(max_num_results), 'text/plain'),
73+
'include_target_data':
74+
(None, include_target_data.value, 'text/plain'),
6075
}
6176
date = rfc_1123_date()
6277
request_path = '/v1/query'

tests/test_query.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from vws import VWS, CloudRecoService
1313
from vws.exceptions import MaxNumResultsOutOfRange
14+
from vws.include_target_data import CloudRecoIncludeTargetData
1415

1516

1617
class TestQuery:
@@ -167,3 +168,127 @@ def test_too_many(
167168
'Accepted range is from 1 to 50 (inclusive).'
168169
)
169170
assert str(exc.value) == exc.value.response.text == expected_value
171+
172+
173+
class TestIncludeTargetData:
174+
"""
175+
Tests for the ``include_target_data`` parameter of ``query``.
176+
"""
177+
178+
def test_default(
179+
self,
180+
vws_client: VWS,
181+
cloud_reco_client: CloudRecoService,
182+
high_quality_image: io.BytesIO,
183+
) -> None:
184+
"""
185+
By default, target data is only returned in the top match.
186+
"""
187+
target_id = vws_client.add_target(
188+
name=uuid.uuid4().hex,
189+
width=1,
190+
image=high_quality_image,
191+
)
192+
target_id_2 = vws_client.add_target(
193+
name=uuid.uuid4().hex,
194+
width=1,
195+
image=high_quality_image,
196+
)
197+
vws_client.wait_for_target_processed(target_id=target_id)
198+
vws_client.wait_for_target_processed(target_id=target_id_2)
199+
top_match, second_match = cloud_reco_client.query(
200+
image=high_quality_image,
201+
max_num_results=2,
202+
)
203+
assert 'target_data' in top_match
204+
assert 'target_data' not in second_match
205+
206+
def test_top(
207+
self,
208+
vws_client: VWS,
209+
cloud_reco_client: CloudRecoService,
210+
high_quality_image: io.BytesIO,
211+
) -> None:
212+
"""
213+
When ``CloudRecoIncludeTargetData.TOP`` is given, target data is only
214+
returned in the top match.
215+
"""
216+
target_id = vws_client.add_target(
217+
name=uuid.uuid4().hex,
218+
width=1,
219+
image=high_quality_image,
220+
)
221+
target_id_2 = vws_client.add_target(
222+
name=uuid.uuid4().hex,
223+
width=1,
224+
image=high_quality_image,
225+
)
226+
vws_client.wait_for_target_processed(target_id=target_id)
227+
vws_client.wait_for_target_processed(target_id=target_id_2)
228+
top_match, second_match = cloud_reco_client.query(
229+
image=high_quality_image,
230+
max_num_results=2,
231+
include_target_data=CloudRecoIncludeTargetData.TOP,
232+
)
233+
assert 'target_data' in top_match
234+
assert 'target_data' not in second_match
235+
236+
def test_none(
237+
self,
238+
vws_client: VWS,
239+
cloud_reco_client: CloudRecoService,
240+
high_quality_image: io.BytesIO,
241+
) -> None:
242+
"""
243+
When ``CloudRecoIncludeTargetData.NONE`` is given, target data is not
244+
returned in any match.
245+
"""
246+
target_id = vws_client.add_target(
247+
name=uuid.uuid4().hex,
248+
width=1,
249+
image=high_quality_image,
250+
)
251+
target_id_2 = vws_client.add_target(
252+
name=uuid.uuid4().hex,
253+
width=1,
254+
image=high_quality_image,
255+
)
256+
vws_client.wait_for_target_processed(target_id=target_id)
257+
vws_client.wait_for_target_processed(target_id=target_id_2)
258+
top_match, second_match = cloud_reco_client.query(
259+
image=high_quality_image,
260+
max_num_results=2,
261+
include_target_data=CloudRecoIncludeTargetData.NONE,
262+
)
263+
assert 'target_data' not in top_match
264+
assert 'target_data' not in second_match
265+
266+
def test_all(
267+
self,
268+
vws_client: VWS,
269+
cloud_reco_client: CloudRecoService,
270+
high_quality_image: io.BytesIO,
271+
) -> None:
272+
"""
273+
When ``CloudRecoIncludeTargetData.ALL`` is given, target data is
274+
returned in all matches.
275+
"""
276+
target_id = vws_client.add_target(
277+
name=uuid.uuid4().hex,
278+
width=1,
279+
image=high_quality_image,
280+
)
281+
target_id_2 = vws_client.add_target(
282+
name=uuid.uuid4().hex,
283+
width=1,
284+
image=high_quality_image,
285+
)
286+
vws_client.wait_for_target_processed(target_id=target_id)
287+
vws_client.wait_for_target_processed(target_id=target_id_2)
288+
top_match, second_match = cloud_reco_client.query(
289+
image=high_quality_image,
290+
max_num_results=2,
291+
include_target_data=CloudRecoIncludeTargetData.ALL,
292+
)
293+
assert 'target_data' in top_match
294+
assert 'target_data' in second_match

0 commit comments

Comments
 (0)