Skip to content

Commit d47cf4b

Browse files
committed
Only test a limited number of image files
1 parent 3f924b3 commit d47cf4b

File tree

3 files changed

+53
-66
lines changed

3 files changed

+53
-66
lines changed

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ warn_untyped_fields = true
141141

142142
module = [
143143
"brisque",
144+
"cv2",
144145
"docker",
145146
"docker.errors",
146147
"docker.models.networks",
@@ -252,6 +253,7 @@ requires-python = ">=3.10"
252253
dependencies = [
253254
"Pillow",
254255
"brisque",
256+
"opencv-python",
255257
"flask",
256258
"multipart",
257259
"numpy",

src/mock_vws/target.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ def _time_now() -> datetime.datetime:
5555

5656
def _quality(image_content: bytes) -> int:
5757
"""
58+
Get a quality score for an image.
59+
60+
This is a rough approximation of the quality score used by Vuforia, but
61+
is not accurate. For example, our "corrupted_image" fixture is rated as -2
62+
by Vuforia, but is rated as 0 by this function.
63+
5864
Args:
5965
image_content: The image content.
6066
@@ -65,15 +71,15 @@ def _quality(image_content: bytes) -> int:
6571
image = Image.open(fp=image_file)
6672
image_array = np.asarray(a=image)
6773
obj = brisque.BRISQUE(url=False)
68-
min_height = min_width = 2
69-
if image.height < min_height or image.width < min_width:
70-
return -2
7174
# We avoid a barrage of warnings from the BRISQUE library.
7275
with np.errstate(divide="ignore", invalid="ignore"):
73-
score = obj.score(img=image_array)
76+
try:
77+
score = obj.score(img=image_array)
78+
except cv2.error: # pylint: disable=no-member
79+
return 0
7480
if math.isnan(score):
7581
return 0
76-
return min(int(score / 5), 5)
82+
return int(score / 20)
7783

7884

7985
@dataclass(frozen=True, eq=True)
@@ -172,14 +178,7 @@ def tracking_rating(self) -> int:
172178
if time_since_upload <= pre_rating_time:
173179
return -1
174180

175-
try:
176-
return _quality(image_content=self.image_value)
177-
except cv2.error as exc:
178-
breakpoint()
179-
print(exc)
180-
return -2
181-
else:
182-
return 0
181+
return _quality(image_content=self.image_value)
183182

184183
@classmethod
185184
def from_dict(cls, target_dict: TargetDict) -> Target:

tests/mock_vws/test_get_target.py

Lines changed: 39 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from __future__ import annotations
77

88
import uuid
9-
from enum import Enum, auto
109
from typing import TYPE_CHECKING
1110

1211
import pytest
@@ -120,73 +119,60 @@ def test_success_status(
120119
assert new_tracking_rating == tracking_rating
121120

122121

122+
def _get_target_tracking_rating(
123+
vws_client: VWS,
124+
image_file: io.BytesIO,
125+
) -> int:
126+
"""
127+
Get the tracking rating of a target.
128+
"""
129+
target_id = vws_client.add_target(
130+
name=f"example_{uuid.uuid4().hex}",
131+
width=1,
132+
image=image_file,
133+
active_flag=True,
134+
application_metadata=None,
135+
)
136+
137+
vws_client.wait_for_target_processed(target_id=target_id)
138+
target_details = vws_client.get_target_record(target_id=target_id)
139+
return target_details.target_record.tracking_rating
140+
141+
123142
@pytest.mark.usefixtures("verify_mock_vuforia")
124143
class TestTargetTrackingRating:
125144
"""
126-
Tests which exercise the target tracking_rating, and check the image fixtures we
127-
use.
145+
Tests which exercise the target tracking_rating, and check the image
146+
fixtures we use.
128147
"""
129148

130149
@staticmethod
131-
# We use almost all of the image fixtures here.
132-
# We do not use images which we expect to fail to upload (`png_too_large`, `bad_image_file`).
133150
def test_target_quality(
134151
vws_client: VWS,
135152
high_quality_image: io.BytesIO,
136-
image_file_failed_state: io.BytesIO,
137153
image_file_success_state_low_rating: io.BytesIO,
138154
corrupted_image_file: io.BytesIO,
139-
different_high_quality_image: io.BytesIO,
140155
) -> None:
141156
"""
142157
The target tracking rating is as expected.
143158
"""
144-
145-
class TrackingRating(Enum):
146-
ZERO = auto()
147-
NEGATIVE = auto()
148-
POSITIVE = auto()
149-
150-
target_id_expected_rating_pairs = []
151-
for image_file in (
152-
high_quality_image,
153-
image_file_failed_state,
154-
# image_file_success_state_low_rating,
155-
corrupted_image_file,
156-
# different_high_quality_image,
157-
):
158-
target_id = vws_client.add_target(
159-
name=f"example_{uuid.uuid4().hex}",
160-
width=1,
161-
image=image_file,
162-
active_flag=True,
163-
application_metadata=None,
164-
)
165-
166-
expected_tracking_rating = {
167-
high_quality_image: TrackingRating.POSITIVE,
168-
image_file_failed_state: TrackingRating.ZERO,
169-
image_file_success_state_low_rating: TrackingRating.ZERO,
170-
corrupted_image_file: TrackingRating.NEGATIVE,
171-
different_high_quality_image: TrackingRating.ZERO,
172-
}[image_file]
173-
target_id_expected_rating_pairs.append(
174-
(target_id, expected_tracking_rating),
175-
)
176-
177-
for (
178-
target_id,
179-
expected_tracking_rating,
180-
) in target_id_expected_rating_pairs:
181-
vws_client.wait_for_target_processed(target_id=target_id)
182-
183-
target_details = vws_client.get_target_record(target_id=target_id)
184-
if expected_tracking_rating == TrackingRating.ZERO:
185-
assert target_details.target_record.tracking_rating == 0
186-
elif expected_tracking_rating == TrackingRating.NEGATIVE:
187-
assert target_details.target_record.tracking_rating < 0
188-
else:
189-
assert target_details.target_record.tracking_rating > 0
159+
high_quality_image_tracking_rating = _get_target_tracking_rating(
160+
vws_client=vws_client,
161+
image_file=high_quality_image,
162+
)
163+
low_quality_image_tracking_rating = _get_target_tracking_rating(
164+
vws_client=vws_client,
165+
image_file=image_file_success_state_low_rating,
166+
)
167+
corrupted_image_file_tracking_rating = _get_target_tracking_rating(
168+
vws_client=vws_client,
169+
image_file=corrupted_image_file,
170+
)
171+
assert (
172+
high_quality_image_tracking_rating
173+
> low_quality_image_tracking_rating
174+
>= corrupted_image_file_tracking_rating
175+
)
190176

191177

192178
@pytest.mark.usefixtures("verify_mock_vuforia")

0 commit comments

Comments
 (0)