Skip to content

Commit 1092a3e

Browse files
Merge pull request #1512 from VWS-Python/test-image-quality
Test image quality
2 parents b15b37f + d47cf4b commit 1092a3e

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from zoneinfo import ZoneInfo
1515

1616
import brisque
17+
import cv2
1718
import numpy as np
1819
from PIL import Image, ImageStat
1920

@@ -54,6 +55,12 @@ def _time_now() -> datetime.datetime:
5455

5556
def _quality(image_content: bytes) -> int:
5657
"""
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+
5764
Args:
5865
image_content: The image content.
5966
@@ -66,7 +73,10 @@ def _quality(image_content: bytes) -> int:
6673
obj = brisque.BRISQUE(url=False)
6774
# We avoid a barrage of warnings from the BRISQUE library.
6875
with np.errstate(divide="ignore", invalid="ignore"):
69-
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
7080
if math.isnan(score):
7181
return 0
7282
return int(score / 20)
@@ -168,10 +178,7 @@ def tracking_rating(self) -> int:
168178
if time_since_upload <= pre_rating_time:
169179
return -1
170180

171-
if self._post_processing_status == TargetStatuses.SUCCESS:
172-
return _quality(image_content=self.image_value)
173-
174-
return 0
181+
return _quality(image_content=self.image_value)
175182

176183
@classmethod
177184
def from_dict(cls, target_dict: TargetDict) -> Target:

tests/mock_vws/test_get_target.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,62 @@ def test_success_status(
119119
assert new_tracking_rating == tracking_rating
120120

121121

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+
142+
@pytest.mark.usefixtures("verify_mock_vuforia")
143+
class TestTargetTrackingRating:
144+
"""
145+
Tests which exercise the target tracking_rating, and check the image
146+
fixtures we use.
147+
"""
148+
149+
@staticmethod
150+
def test_target_quality(
151+
vws_client: VWS,
152+
high_quality_image: io.BytesIO,
153+
image_file_success_state_low_rating: io.BytesIO,
154+
corrupted_image_file: io.BytesIO,
155+
) -> None:
156+
"""
157+
The target tracking rating is as expected.
158+
"""
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+
)
176+
177+
122178
@pytest.mark.usefixtures("verify_mock_vuforia")
123179
class TestInactiveProject:
124180
"""

0 commit comments

Comments
 (0)