7
7
from typing import List , OrderedDict
8
8
9
9
import numpy as np
10
- from skimage .metrics import structural_similarity
11
10
12
11
import qoi
13
12
17
16
18
17
OPENCV_AVAILABLE = True
19
18
except ImportError :
20
- warnings .warn ("Couldn't find OpenCV - you can still run this, but OpenCV tests will be disabled." )
19
+ warnings .warn (
20
+ (
21
+ "Couldn't find OpenCV - you can still run this, but OpenCV tests/functionality will be disabled, including"
22
+ " lossy qoi (which uses OpenCV to resize the image)"
23
+ )
24
+ )
21
25
22
26
PIL_AVAILABLE = False
23
27
try :
27
31
except ImportError :
28
32
warnings .warn ("Couldn't find PIL - you can still run this, but PIL tests will be disabled." )
29
33
34
+ try :
35
+ from skimage .metrics import structural_similarity as img_similarity
36
+
37
+ similarity_name = "SSIM"
38
+ except ImportError :
39
+ warnings .warn ("Couldn't find skimage.metrics.structural_similarity so using MSE." )
40
+
41
+ def img_similarity (a , b , channel_axis = None ):
42
+ return ((a - b ) ** 2 ).mean ()
43
+
44
+ similarity_name = "MSE"
45
+
30
46
31
47
@dataclass
32
48
class TestResult :
@@ -37,7 +53,7 @@ class TestResult:
37
53
encode_ms : float
38
54
encode_size : float
39
55
decode_ms : float
40
- ssim : float
56
+ img_similarity : float
41
57
42
58
43
59
def timeit (f , warmup = 3 , tests = 10 ):
@@ -61,7 +77,7 @@ def bench_qoi(rgb, test_name, warmup=3, tests=10):
61
77
encode_ms = encode_ms ,
62
78
encode_size = len (bites ),
63
79
decode_ms = decode_ms ,
64
- ssim = structural_similarity (rgb , decoded , channel_axis = 2 ),
80
+ img_similarity = img_similarity (rgb , decoded , channel_axis = 2 ),
65
81
)
66
82
67
83
@@ -83,7 +99,7 @@ def decode():
83
99
encode_ms = encode_ms ,
84
100
encode_size = len (bites ),
85
101
decode_ms = decode_ms ,
86
- ssim = structural_similarity (rgb , decoded , channel_axis = 2 ),
102
+ img_similarity = img_similarity (rgb , decoded , channel_axis = 2 ),
87
103
)
88
104
89
105
@@ -124,7 +140,7 @@ def encode():
124
140
encode_ms = encode_ms ,
125
141
encode_size = len (bites ),
126
142
decode_ms = decode_ms ,
127
- ssim = structural_similarity (rgb , decoded , channel_axis = 2 ),
143
+ img_similarity = img_similarity (rgb , decoded , channel_axis = 2 ),
128
144
)
129
145
130
146
@@ -161,7 +177,7 @@ def encode():
161
177
encode_ms = encode_ms ,
162
178
encode_size = len (bites ),
163
179
decode_ms = decode_ms ,
164
- ssim = structural_similarity (rgb , decoded , channel_axis = 2 ),
180
+ img_similarity = img_similarity (rgb , decoded , channel_axis = 2 ),
165
181
)
166
182
167
183
@@ -185,7 +201,7 @@ def bench_methods(
185
201
)
186
202
if qoi and (implementations is None or "qoi" in implementations ):
187
203
yield from bench_qoi (rgb , test_name = name , warmup = warmup , tests = tests )
188
- if qoi and (implementations is None or "qoi-lossy" in implementations ):
204
+ if qoi and (implementations is None or "qoi-lossy" in implementations ) and OPENCV_AVAILABLE :
189
205
yield from bench_qoi_lossy (rgb , test_name = name , warmup = warmup , tests = tests , scale = qoi_lossy_scale )
190
206
191
207
@@ -202,7 +218,7 @@ def totable(results: List[TestResult]):
202
218
encode_ms = "Encode (ms)" ,
203
219
encode_size = "Encode (kb)" ,
204
220
decode_ms = "Decode (ms)" ,
205
- ssim = "SSIM" ,
221
+ img_similarity = similarity_name ,
206
222
)
207
223
208
224
# Convert to dicts of strings
@@ -212,7 +228,7 @@ def totable(results: List[TestResult]):
212
228
for k in fields (res ):
213
229
name = k .name
214
230
v = getattr (res , name )
215
- if name == "ssim " or name .endswith ("_ms" ):
231
+ if name == "img_similarity " or name .endswith ("_ms" ):
216
232
v = f"{ v :.2f} "
217
233
elif name .endswith ("_size" ):
218
234
v = f"{ v / 1024 :.1f} "
0 commit comments