Skip to content

Commit 99269c7

Browse files
Merge pull request #747 from VWS-Python/exp2
Change target to remove unhashable type
2 parents 95de79b + a519fcc commit 99269c7

File tree

6 files changed

+22
-60
lines changed

6 files changed

+22
-60
lines changed

src/mock_vws/_flask_server/storage.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import base64
66
import dataclasses
77
import datetime
8-
import io
98
import random
109
from http import HTTPStatus
1110
from typing import List, Tuple
@@ -79,11 +78,10 @@ def create_target(database_name: str) -> Tuple[str, int]:
7978
]
8079
image_base64 = request.json['image_base64']
8180
image_bytes = base64.b64decode(image_base64)
82-
image = io.BytesIO(image_bytes)
8381
target = Target(
8482
name=request.json['name'],
8583
width=request.json['width'],
86-
image=image,
84+
image_value=image_bytes,
8785
active_flag=request.json['active_flag'],
8886
processing_time_seconds=request.json['processing_time_seconds'],
8987
application_metadata=request.json['application_metadata'],
@@ -138,11 +136,9 @@ def update_target(database_name: str, target_id: str) -> Tuple[str, int]:
138136
target.application_metadata,
139137
)
140138

141-
image_file = target.image
139+
image_value = target.image_value
142140
if 'image' in request.json:
143-
image = request.json['image']
144-
decoded = base64.b64decode(image)
145-
image_file = io.BytesIO(decoded)
141+
image_value = base64.b64decode(request.json['image'])
146142

147143
# In the real implementation, the tracking rating can stay the same.
148144
# However, for demonstration purposes, the tracking rating changes but
@@ -159,7 +155,7 @@ def update_target(database_name: str, target_id: str) -> Tuple[str, int]:
159155
width=width,
160156
active_flag=active_flag,
161157
application_metadata=application_metadata,
162-
image=image_file,
158+
image_value=image_value,
163159
processed_tracking_rating=processed_tracking_rating,
164160
last_modified_date=last_modified_date,
165161
)

src/mock_vws/_flask_server/vws.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@
77

88
import base64
99
import email.utils
10-
import io
1110
import json
1211
import uuid
1312
from http import HTTPStatus
1413
from typing import Final, List, Set
1514

1615
import requests
1716
from flask import Flask, Response, request
18-
from PIL import Image
1917

2018
from mock_vws._constants import ResultCodes, TargetStatuses
2119
from mock_vws._database_matchers import get_database_matching_server_keys
@@ -118,14 +116,10 @@ def add_target() -> Response:
118116
if active_flag is None:
119117
active_flag = True
120118

121-
image = request_json['image']
122-
decoded = base64.b64decode(image)
123-
image_file = io.BytesIO(decoded)
124-
125119
new_target = Target(
126120
name=name,
127121
width=request_json['width'],
128-
image=image_file,
122+
image_value=base64.b64decode(request_json['image']),
129123
active_flag=active_flag,
130124
processing_time_seconds=processing_time_seconds,
131125
application_metadata=request_json.get('application_metadata'),
@@ -380,7 +374,7 @@ def get_duplicates(target_id: str) -> Response:
380374
similar_targets: List[str] = [
381375
other.target_id
382376
for other in other_targets
383-
if Image.open(other.image) == Image.open(target.image)
377+
if other.image_value == target.image_value
384378
and TargetStatuses.FAILED.value not in (target.status, other.status)
385379
and TargetStatuses.PROCESSING.value != other.status
386380
and other.active_flag

src/mock_vws/_query_tools.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,6 @@ class ActiveMatchingTargetsDeleteProcessing(Exception):
3030
"""
3131

3232

33-
def _images_match(image: io.BytesIO, another_image: io.BytesIO) -> bool:
34-
"""
35-
Given two images, return whether they are matching.
36-
37-
In the real Vuforia, this matching is fuzzy.
38-
For now, we check exact byte matching.
39-
40-
See https://github.com/VWS-Python/vws-python-mock/issues/3 for changing
41-
that.
42-
"""
43-
return bool(image.getvalue() == another_image.getvalue())
44-
45-
4633
def get_query_match_response_text(
4734
request_headers: Dict[str, str],
4835
request_body: bytes,
@@ -90,9 +77,8 @@ def get_query_match_response_text(
9077
[include_target_data] = parsed.get('include_target_data', ['top'])
9178
include_target_data = include_target_data.lower()
9279

93-
[image_bytes] = parsed['image']
94-
assert isinstance(image_bytes, bytes)
95-
image = io.BytesIO(image_bytes)
80+
[image_value] = parsed['image']
81+
assert isinstance(image_value, bytes)
9682
gmt = ZoneInfo('GMT')
9783
now = datetime.datetime.now(tz=gmt)
9884

@@ -117,7 +103,7 @@ def get_query_match_response_text(
117103
matching_targets = [
118104
target
119105
for target in database.targets
120-
if _images_match(image=target.image, another_image=image)
106+
if target.image_value == image_value
121107
]
122108

123109
not_deleted_matches = [

src/mock_vws/_requests_mock_server/mock_web_services_api.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@
99
import dataclasses
1010
import datetime
1111
import email.utils
12-
import io
1312
import random
1413
import uuid
1514
from http import HTTPStatus
1615
from typing import Any, Callable, Dict, List, Set, Tuple, Union
1716

1817
import wrapt
1918
from backports.zoneinfo import ZoneInfo
20-
from PIL import Image
2119
from requests_mock import DELETE, GET, POST, PUT
2220
from requests_mock.request import _RequestObjectProxy
2321
from requests_mock.response import _Context
@@ -181,18 +179,12 @@ def add_target(
181179
False: False,
182180
}[given_active_flag]
183181

184-
image = request.json()['image']
185-
decoded = base64.b64decode(image)
186-
image_file = io.BytesIO(decoded)
187-
188-
name = request.json()['name']
189-
width = request.json()['width']
190182
application_metadata = request.json().get('application_metadata')
191183

192184
new_target = Target(
193-
name=name,
194-
width=width,
195-
image=image_file,
185+
name=request.json()['name'],
186+
width=request.json()['width'],
187+
image_value=base64.b64decode(request.json()['image']),
196188
active_flag=active_flag,
197189
processing_time_seconds=self._processing_time_seconds,
198190
application_metadata=application_metadata,
@@ -426,7 +418,7 @@ def get_duplicates(
426418
similar_targets: List[str] = [
427419
other.target_id
428420
for other in other_targets
429-
if Image.open(other.image) == Image.open(target.image)
421+
if other.image_value == target.image_value
430422
and TargetStatuses.FAILED.value
431423
not in (target.status, other.status)
432424
and TargetStatuses.PROCESSING.value != other.status
@@ -496,11 +488,9 @@ def update_target(
496488
target.application_metadata,
497489
)
498490

499-
image_file = target.image
491+
image_value = target.image_value
500492
if 'image' in request.json():
501-
image = request.json()['image']
502-
decoded = base64.b64decode(image)
503-
image_file = io.BytesIO(decoded)
493+
image_value = base64.b64decode(request.json()['image'])
504494

505495
if 'active_flag' in request.json() and active_flag is None:
506496
raise Fail(status_code=HTTPStatus.BAD_REQUEST)
@@ -526,7 +516,7 @@ def update_target(
526516
width=width,
527517
active_flag=active_flag,
528518
application_metadata=application_metadata,
529-
image=image_file,
519+
image_value=image_value,
530520
processed_tracking_rating=processed_tracking_rating,
531521
last_modified_date=last_modified_date,
532522
)

src/mock_vws/target.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ class Target: # pylint: disable=too-many-instance-attributes
6767

6868
active_flag: bool
6969
application_metadata: Optional[str]
70-
# Comparison of io.BytesIO compares the object, not the file contents.
71-
# data we care about, so we leave this.
72-
image: io.BytesIO = field(compare=False)
70+
image_value: bytes
7371
name: str
7472
processing_time_seconds: float
7573
width: float
@@ -95,7 +93,8 @@ def _post_processing_status(self) -> TargetStatuses:
9593
How VWS determines this is unknown, but it relates to how suitable the
9694
target is for detection.
9795
"""
98-
image = Image.open(self.image)
96+
image_file = io.BytesIO(self.image_value)
97+
image = Image.open(image_file)
9998
image_stat = ImageStat.Stat(image)
10099

101100
average_std_dev = statistics.mean(image_stat.stddev)
@@ -171,9 +170,8 @@ def from_dict(cls, target_dict: TargetDict) -> Target:
171170
active_flag = target_dict['active_flag']
172171
width = target_dict['width']
173172
image_base64 = target_dict['image_base64']
173+
image_value = base64.b64decode(image_base64)
174174
processed_tracking_rating = target_dict['processed_tracking_rating']
175-
image_bytes = base64.b64decode(image_base64)
176-
image = io.BytesIO(image_bytes)
177175
processing_time_seconds = target_dict['processing_time_seconds']
178176
application_metadata = target_dict['application_metadata']
179177
target_id = target_dict['target_id']
@@ -196,7 +194,7 @@ def from_dict(cls, target_dict: TargetDict) -> Target:
196194
name=name,
197195
active_flag=active_flag,
198196
width=width,
199-
image=image,
197+
image_value=image_value,
200198
processing_time_seconds=processing_time_seconds,
201199
application_metadata=application_metadata,
202200
delete_date=delete_date,
@@ -214,8 +212,7 @@ def to_dict(self) -> TargetDict:
214212
if self.delete_date:
215213
delete_date = datetime.datetime.isoformat(self.delete_date)
216214

217-
image_value = self.image.getvalue()
218-
image_base64 = base64.encodebytes(image_value).decode()
215+
image_base64 = base64.encodebytes(self.image_value).decode()
219216

220217
return {
221218
'name': self.name,

tests/mock_vws/test_usage.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,6 @@ def test_to_dict(self, high_quality_image: io.BytesIO) -> None:
539539
assert json.dumps(target_dict)
540540

541541
new_target = Target.from_dict(target_dict=target_dict)
542-
assert new_target.image.getvalue() == target.image.getvalue()
543542
assert new_target == target
544543

545544
def test_to_dict_deleted(self, high_quality_image: io.BytesIO) -> None:

0 commit comments

Comments
 (0)