-
Notifications
You must be signed in to change notification settings - Fork 69
/
image_property.py
421 lines (337 loc) · 12.8 KB
/
image_property.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import math
from abc import ABC, abstractmethod
from typing import Any, Dict, List, Optional, Union, overload
import numpy as np
import pandas as pd
from PIL import ImageFilter, ImageStat
from PIL.Image import Image
from cleanvision.issue_managers import IssueType
from cleanvision.utils.constants import MAX_RESOLUTION_FOR_BLURRY_DETECTION
from cleanvision.utils.utils import get_is_issue_colname, get_score_colname
class ImageProperty(ABC):
name: str
@property
@abstractmethod
def score_columns(self) -> List[str]:
pass
@staticmethod
def check_params(**kwargs: Any) -> None:
allowed_kwargs: Dict[str, Any] = {
"image": Image,
"dark_issue_data": pd.DataFrame,
"threshold": float,
}
for name, value in kwargs.items():
if name not in allowed_kwargs:
raise ValueError(f"{name} is not a valid keyword argument.")
if value is not None and not isinstance(value, allowed_kwargs[name]):
raise ValueError(
f"Valid type for keyword argument {name} can only be {allowed_kwargs[name]}. {name} cannot be type {type(name)}. "
)
@abstractmethod
def calculate(self, image: Image) -> Dict[str, Union[float, str]]:
raise NotImplementedError
@abstractmethod
def get_scores(
self, raw_scores: pd.DataFrame, issue_type: str, **kwargs: Any
) -> Any:
self.check_params(**kwargs)
return
def mark_issue(
self,
scores: pd.DataFrame,
issue_type: str,
threshold: Optional[float] = None,
) -> pd.DataFrame:
is_issue = pd.DataFrame(index=scores.index)
is_issue_colname, score_colname = get_is_issue_colname(
issue_type
), get_score_colname(issue_type)
is_issue[is_issue_colname] = scores[score_colname] < threshold
return is_issue
def calc_avg_brightness(image: Image) -> float:
stat = ImageStat.Stat(image)
try:
red, green, blue = stat.mean
except ValueError:
red, green, blue = (
stat.mean[0],
stat.mean[0],
stat.mean[0],
) # deals with black and white images
cur_bright: float = calculate_brightness(red, green, blue)
return cur_bright
@overload
def calculate_brightness(red: float, green: float, blue: float) -> float: ...
@overload
def calculate_brightness(
red: "np.ndarray[Any, Any]",
green: "np.ndarray[Any, Any]",
blue: "np.ndarray[Any, Any]",
) -> "np.ndarray[Any, Any]": ...
def calculate_brightness(
red: Union[float, "np.ndarray[Any, Any]"],
green: Union[float, "np.ndarray[Any, Any]"],
blue: Union[float, "np.ndarray[Any, Any]"],
) -> Union[float, "np.ndarray[Any, Any]"]:
cur_bright = (
np.sqrt(0.241 * (red * red) + 0.691 * (green * green) + 0.068 * (blue * blue))
) / 255
return cur_bright
def calc_percentile_brightness(
image: Image, percentiles: List[int]
) -> "np.ndarray[Any, Any]":
imarr = np.asarray(image)
if len(imarr.shape) == 3:
r, g, b = (
imarr[:, :, 0].astype("int"),
imarr[:, :, 1].astype("int"),
imarr[:, :, 2].astype("int"),
)
pixel_brightness = calculate_brightness(
r, g, b
) # np.sqrt(0.241 * r * r + 0.691 * g * g + 0.068 * b * b)
else:
pixel_brightness = imarr / 255.0
perc_values: "np.ndarray[Any, Any]" = np.percentile(pixel_brightness, percentiles)
return perc_values
class BrightnessProperty(ImageProperty):
name: str = "brightness"
@property
def score_columns(self) -> List[str]:
return self._score_columns
def __init__(self, issue_type: str) -> None:
self.issue_type = issue_type
self._score_columns = [
(
"brightness_perc_99"
if self.issue_type == IssueType.DARK.value
else "brightness_perc_5"
)
]
def calculate(self, image: Image) -> Dict[str, Union[float, str]]:
percentiles = [1, 5, 10, 15, 90, 95, 99]
perc_values = calc_percentile_brightness(image, percentiles=percentiles)
raw_values = {
f"brightness_perc_{p}": value for p, value in zip(percentiles, perc_values)
}
raw_values[self.name] = calc_avg_brightness(image)
return raw_values
def get_scores(
self,
raw_scores: pd.DataFrame,
issue_type: str,
**kwargs: Any,
) -> pd.DataFrame:
super().get_scores(raw_scores, issue_type, **kwargs)
assert raw_scores is not None # all values are between 0 and 1
scores = pd.DataFrame(index=raw_scores.index)
if issue_type == IssueType.DARK.value:
scores[get_score_colname(issue_type)] = raw_scores[self.score_columns[0]]
else:
scores[get_score_colname(issue_type)] = (
1 - raw_scores[self.score_columns[0]]
)
return scores
def calc_aspect_ratio(image: Image) -> float:
width, height = image.size
size_score = min(width / height, height / width) # consider extreme shapes
assert isinstance(size_score, float)
return size_score
class AspectRatioProperty(ImageProperty):
name: str = "aspect_ratio"
@property
def score_columns(self) -> List[str]:
return self._score_columns
def __init__(self) -> None:
self._score_columns = [self.name]
def calculate(self, image: Image) -> Dict[str, Union[float, str]]:
return {self.name: calc_aspect_ratio(image)}
def get_scores(
self,
raw_scores: pd.DataFrame,
issue_type: str,
**kwargs: Any,
) -> pd.DataFrame:
super().get_scores(raw_scores, issue_type, **kwargs)
scores = pd.DataFrame(index=raw_scores.index)
scores[get_score_colname(issue_type)] = raw_scores[self.score_columns[0]]
return scores
def calc_entropy(image: Image) -> float:
entropy = image.entropy()
assert isinstance(
entropy, float
) # PIL does not have type ann stub so need to assert function return
return entropy
class EntropyProperty(ImageProperty):
name: str = "entropy"
@property
def score_columns(self) -> List[str]:
return self._score_columns
def __init__(self) -> None:
self._score_columns = [self.name]
def calculate(self, image: Image) -> Dict[str, Union[float, str]]:
return {self.name: calc_entropy(image)}
def get_scores(
self,
raw_scores: pd.DataFrame,
issue_type: str,
normalizing_factor: float = 1.0,
**kwargs: Any,
) -> pd.DataFrame:
super().get_scores(raw_scores, issue_type, **kwargs)
assert raw_scores is not None
scores = pd.DataFrame(index=raw_scores.index)
scores_data = normalizing_factor * raw_scores[self.score_columns[0]]
scores_data[scores_data > 1] = 1
scores[get_score_colname(issue_type)] = scores_data
return scores
def calc_blurriness(gray_image: Image) -> float:
edges = get_edges(gray_image)
blurriness = ImageStat.Stat(edges).var[0]
return np.sqrt(blurriness) # type:ignore
def calc_std_grayscale(gray_image: Image) -> float:
return np.std(gray_image.histogram()) # type: ignore
class BlurrinessProperty(ImageProperty):
name = "blurriness"
@property
def score_columns(self) -> List[str]:
return self._score_columns
def __init__(self) -> None:
self._score_columns = [self.name, "blurriness_grayscale_std"]
self.max_resolution = MAX_RESOLUTION_FOR_BLURRY_DETECTION
def calculate(self, image: Image) -> Dict[str, Union[float, str]]:
ratio = max(image.width, image.height) / self.max_resolution
if ratio > 1:
resized_image = image.resize(
(max(int(image.width // ratio), 1), max(int(image.height // ratio), 1))
)
else:
resized_image = image.copy()
gray_image = resized_image.convert("L")
return {
self.name: calc_blurriness(gray_image),
"blurriness_grayscale_std": calc_std_grayscale(gray_image),
}
def get_scores(
self,
raw_scores: pd.DataFrame,
issue_type: str,
normalizing_factor: float = 1.0,
color_threshold: float = 1.0,
**kwargs: Any,
) -> pd.DataFrame:
super().get_scores(raw_scores, issue_type, **kwargs)
blur_scores = 1 - np.exp(-1 * raw_scores[self.name] * normalizing_factor)
std_scores = 1 - np.exp(
-1 * raw_scores["blurriness_grayscale_std"] * normalizing_factor
)
std_scores[std_scores <= color_threshold] = 0
scores = pd.DataFrame(index=raw_scores.index)
scores[get_score_colname(issue_type)] = np.minimum(blur_scores + std_scores, 1)
return scores
def get_edges(gray_image: Image) -> Image:
edges = gray_image.filter(ImageFilter.FIND_EDGES)
return edges
def calc_color_space(image: Image) -> str:
return get_image_mode(image)
def calc_image_area_sqrt(image: Image) -> float:
w, h = image.size
return math.sqrt(w) * math.sqrt(h)
class ColorSpaceProperty(ImageProperty):
name = "color_space"
@property
def score_columns(self) -> List[str]:
return self._score_columns
def __init__(self) -> None:
self._score_columns = [self.name]
def calculate(self, image: Image) -> Dict[str, Union[float, str]]:
return {self.name: calc_color_space(image)}
def get_scores(
self,
raw_scores: pd.DataFrame,
issue_type: str,
**kwargs: Any,
) -> pd.DataFrame:
super().get_scores(raw_scores, issue_type, **kwargs)
assert raw_scores is not None
scores = pd.DataFrame(index=raw_scores.index)
scores[get_score_colname(issue_type)] = [
0 if x == "L" else 1 for x in raw_scores[self.score_columns[0]]
]
return scores
def mark_issue(
self, scores: pd.DataFrame, issue_type: str, threshold: Optional[float] = None
) -> pd.DataFrame:
is_issue = pd.DataFrame(index=scores.index)
is_issue_colname, score_colname = get_is_issue_colname(
issue_type
), get_score_colname(issue_type)
is_issue[is_issue_colname] = (1 - scores[score_colname]).astype("bool")
return is_issue
class SizeProperty(ImageProperty):
name = "size"
@property
def score_columns(self) -> List[str]:
return self._score_columns
def __init__(self) -> None:
self._score_columns = [self.name]
self.threshold = 0.5 # todo: this ensures that the scores are evenly distributed across the range
def calculate(self, image: Image) -> Dict[str, Union[float, str]]:
return {self.name: calc_image_area_sqrt(image)}
def get_scores(
self,
raw_scores: pd.DataFrame,
issue_type: str,
iqr_factor: float = 3.0,
**kwargs: Any,
) -> pd.DataFrame:
super().get_scores(raw_scores, issue_type, **kwargs)
assert raw_scores is not None
size = raw_scores[self.name]
q1, q3 = np.percentile(size, [25, 75])
size_iqr = q3 - q1
min_threshold, max_threshold = (
q1 - iqr_factor * size_iqr,
q3 + iqr_factor * size_iqr,
)
mid_threshold = (min_threshold + max_threshold) / 2
threshold_gap = max_threshold - min_threshold
distance = np.absolute(size - mid_threshold)
if threshold_gap > 0:
norm_value = threshold_gap
self.threshold = 0.5
elif threshold_gap == 0:
norm_value = mid_threshold
self.threshold = 1.0
else:
raise ValueError("threshold_gap should be non negative")
norm_dist = distance / norm_value
score_values = 1 - np.clip(norm_dist, 0, 1)
scores = pd.DataFrame(index=raw_scores.index)
scores[get_score_colname(issue_type)] = score_values
return scores
def mark_issue(
self, scores: pd.DataFrame, issue_type: str, threshold: Optional[float] = None
) -> pd.DataFrame:
threshold = self.threshold if threshold is None else threshold
is_issue_colname, score_colname = get_is_issue_colname(
issue_type
), get_score_colname(issue_type)
is_issue = pd.DataFrame(index=scores.index)
is_issue[is_issue_colname] = scores[score_colname] < threshold
return is_issue
def get_image_mode(image: Image) -> str:
if image.mode:
image_mode = image.mode
assert isinstance(image_mode, str)
return image_mode
else:
imarr = np.asarray(image)
if len(imarr.shape) == 2 or (
len(imarr.shape) == 3
and (np.diff(imarr.reshape(-1, 3).T, axis=0) == 0).all()
):
return "L"
else:
return "UNK"