Skip to content

Commit 6d8c6c3

Browse files
committed
Use colour checks configuration.
1 parent fe4484f commit 6d8c6c3

15 files changed

+310
-200
lines changed

.coveragerc

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ sigterm = True
55
exclude_lines =
66
pragma: no cover
77
if __name__ == .__main__.:
8+
if TYPE_CHECKING:
89
pass

colour_checker_detection/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
__major_version__ = "0"
6868
__minor_version__ = "2"
6969
__change_version__ = "1"
70-
__version__ = ".".join((__major_version__, __minor_version__, __change_version__))
70+
__version__ = f"{__major_version__}.{__minor_version__}.{__change_version__}"
7171

7272
try:
7373
_version = (
@@ -79,7 +79,7 @@
7979
.strip()
8080
.decode("utf-8")
8181
)
82-
except Exception:
82+
except Exception: # noqa: BLE001
8383
_version = __version__
8484

8585
colour.utilities.ANCILLARY_COLOUR_SCIENCE_PACKAGES[ # pyright: ignore

colour_checker_detection/detection/common.py

+33-32
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,27 @@
1919

2020
from __future__ import annotations
2121

22+
import typing
2223
from dataclasses import dataclass
2324

2425
import cv2
2526
import numpy as np
2627
from colour.algebra import linear_conversion
2728
from colour.characterisation import CCS_COLOURCHECKERS
28-
from colour.hints import (
29-
Any,
30-
ArrayLike,
31-
Dict,
32-
DTypeFloat,
33-
DTypeInt,
34-
Literal,
35-
NDArrayFloat,
36-
NDArrayInt,
37-
Tuple,
38-
Type,
39-
cast,
40-
)
29+
30+
if typing.TYPE_CHECKING:
31+
from colour.hints import (
32+
Any,
33+
ArrayLike,
34+
Dict,
35+
DTypeFloat,
36+
DTypeInt,
37+
Literal,
38+
NDArrayReal,
39+
Type,
40+
)
41+
42+
from colour.hints import NDArrayFloat, NDArrayInt, NDArrayReal, Tuple, cast
4143
from colour.models import XYZ_to_RGB, xyY_to_XYZ
4244
from colour.utilities import (
4345
MixinDataclassIterable,
@@ -275,7 +277,7 @@ def swatch_masks(
275277
offset_v = height / swatches_v / 2
276278
for j in np.linspace(offset_v, height - offset_v, swatches_v):
277279
for i in np.linspace(offset_h, width - offset_h, swatches_h):
278-
masks.append(
280+
masks.append( # noqa: PERF401
279281
as_int32_array(
280282
[
281283
j - samples_half,
@@ -352,7 +354,7 @@ def reformat_image(
352354
cv2.WARP_FILL_OUTLIERS, # pyright: ignore
353355
cv2.WARP_INVERSE_MAP, # pyright: ignore
354356
] = cv2.INTER_CUBIC,
355-
) -> NDArrayInt | NDArrayFloat:
357+
) -> NDArrayReal:
356358
"""
357359
Reformat given image so that it is horizontal and resizes it to given target
358360
width.
@@ -432,10 +434,10 @@ def reformat_image(
432434

433435

434436
def transform_image(
435-
image,
436-
translation=np.array([0, 0]),
437-
rotation=0,
438-
scale=np.array([1, 1]),
437+
image: ArrayLike,
438+
translation: ArrayLike = (0, 0),
439+
rotation: float = 0,
440+
scale: ArrayLike = (1, 1),
439441
interpolation_method: Literal[
440442
cv2.INTER_AREA, # pyright: ignore
441443
cv2.INTER_CUBIC, # pyright: ignore
@@ -448,7 +450,7 @@ def transform_image(
448450
cv2.WARP_FILL_OUTLIERS, # pyright: ignore
449451
cv2.WARP_INVERSE_MAP, # pyright: ignore
450452
] = cv2.INTER_CUBIC,
451-
) -> NDArrayInt | NDArrayFloat:
453+
) -> NDArrayReal:
452454
"""
453455
Transform given image using given translation, rotation and scale values.
454456
@@ -550,7 +552,7 @@ def transform_image(
550552
transform += as_float32_array([[0, 0, t_x], [0, 0, t_y]])
551553

552554
return cast(
553-
NDArrayInt | NDArrayFloat,
555+
NDArrayReal,
554556
cv2.warpAffine(
555557
image,
556558
transform,
@@ -563,7 +565,7 @@ def transform_image(
563565

564566
def detect_contours(
565567
image: ArrayLike, additional_data: bool = False, **kwargs: Any
566-
) -> Tuple[NDArrayInt] | Tuple[Tuple[NDArrayInt], NDArrayInt | NDArrayFloat]:
568+
) -> Tuple[NDArrayInt] | Tuple[Tuple[NDArrayInt], NDArrayReal]:
567569
"""
568570
Detect the contours of given image using given settings.
569571
@@ -648,7 +650,7 @@ def detect_contours(
648650
iterations=settings.convolution_iterations,
649651
)
650652

651-
image_k = cast(NDArrayInt | NDArrayFloat, image_k)
653+
image_k = cast(NDArrayReal, image_k)
652654

653655
# Detecting contours.
654656
contours, _hierarchy = cv2.findContours(
@@ -659,8 +661,7 @@ def detect_contours(
659661

660662
if additional_data:
661663
return contours, image_k
662-
else:
663-
return contours
664+
return contours
664665

665666

666667
def is_square(contour: ArrayLike, tolerance: float = 0.015) -> bool:
@@ -730,13 +731,11 @@ def contour_centroid(contour: ArrayLike) -> Tuple[float, float]:
730731

731732
moments = cv2.moments(contour)
732733

733-
centroid = (
734+
return (
734735
moments["m10"] / moments["m00"],
735736
moments["m01"] / moments["m00"],
736737
)
737738

738-
return centroid
739-
740739

741740
def scale_contour(contour: ArrayLike, factor: ArrayLike) -> NDArrayFloat:
742741
"""
@@ -773,9 +772,7 @@ def scale_contour(contour: ArrayLike, factor: ArrayLike) -> NDArrayFloat:
773772

774773
centroid = contour_centroid(contour)
775774

776-
scaled_contour = (contour - centroid) * factor + centroid
777-
778-
return scaled_contour
775+
return (contour - centroid) * factor + centroid
779776

780777

781778
def approximate_contour(
@@ -995,7 +992,11 @@ class DataDetectionColourChecker(MixinDataclassIterable):
995992

996993

997994
def sample_colour_checker(
998-
image: ArrayLike, quadrilateral, rectangle, samples=32, **kwargs
995+
image: ArrayLike,
996+
quadrilateral: ArrayLike,
997+
rectangle: ArrayLike,
998+
samples: int = 32,
999+
**kwargs: Any,
9991000
) -> DataDetectionColourChecker:
10001001
"""
10011002
Sample the colour checker using the given source quadrilateral, i.e.,

colour_checker_detection/detection/inference.py

+66-19
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,23 @@
1717
import subprocess
1818
import sys
1919
import tempfile
20+
import typing
2021

2122
import cv2
2223
import numpy as np
23-
from colour.hints import (
24-
Any,
25-
ArrayLike,
26-
Callable,
27-
Dict,
28-
NDArrayFloat,
29-
NDArrayInt,
30-
Tuple,
31-
cast,
32-
)
24+
25+
if typing.TYPE_CHECKING:
26+
from colour.hints import (
27+
Any,
28+
ArrayLike,
29+
Callable,
30+
Dict,
31+
Literal,
32+
NDArrayFloat,
33+
Tuple,
34+
)
35+
36+
from colour.hints import NDArrayReal, cast
3337
from colour.io import convert_bit_depth, read_image, write_image
3438
from colour.models import eotf_inverse_sRGB, eotf_sRGB
3539
from colour.plotting import CONSTANTS_COLOUR_STYLE, plot_image
@@ -136,7 +140,7 @@ def inferencer_default(
136140
cctf_encoding: Callable = eotf_inverse_sRGB,
137141
apply_cctf_encoding: bool = True,
138142
show: bool = False,
139-
) -> NDArrayInt | NDArrayFloat:
143+
) -> NDArrayReal:
140144
"""
141145
Predict the colour checker rectangles in given image using
142146
*Ultralytics YOLOv8*.
@@ -224,17 +228,60 @@ def inferencer_default(
224228
"""Inferred classes."""
225229

226230

231+
@typing.overload
232+
def detect_colour_checkers_inference(
233+
image: str | ArrayLike,
234+
samples: int = ...,
235+
cctf_decoding: Callable = ...,
236+
apply_cctf_decoding: bool = ...,
237+
inferencer: Callable = ...,
238+
inferencer_kwargs: dict | None = ...,
239+
show: bool = ...,
240+
additional_data: Literal[True] = True,
241+
**kwargs: Any,
242+
) -> Tuple[DataDetectionColourChecker, ...]: ...
243+
244+
245+
@typing.overload
246+
def detect_colour_checkers_inference(
247+
image: str | ArrayLike,
248+
samples: int = ...,
249+
cctf_decoding: Callable = ...,
250+
apply_cctf_decoding: bool = ...,
251+
inferencer: Callable = ...,
252+
inferencer_kwargs: dict | None = ...,
253+
show: bool = ...,
254+
*,
255+
additional_data: Literal[False],
256+
**kwargs: Any,
257+
) -> Tuple[NDArrayFloat, ...]: ...
258+
259+
260+
@typing.overload
261+
def detect_colour_checkers_inference(
262+
image: str | ArrayLike,
263+
samples: int,
264+
cctf_decoding: Callable,
265+
apply_cctf_decoding: bool,
266+
inferencer: Callable,
267+
inferencer_kwargs: dict | None,
268+
show: bool,
269+
additional_data: Literal[False],
270+
**kwargs: Any,
271+
) -> Tuple[NDArrayFloat, ...]: ...
272+
273+
227274
def detect_colour_checkers_inference(
228275
image: str | ArrayLike,
229276
samples: int = 32,
230-
cctf_decoding=eotf_sRGB,
277+
cctf_decoding: Callable = eotf_sRGB,
231278
apply_cctf_decoding: bool = False,
232279
inferencer: Callable = inferencer_default,
233280
inferencer_kwargs: dict | None = None,
234281
show: bool = False,
235282
additional_data: bool = False,
236283
**kwargs: Any,
237-
) -> Tuple[DataDetectionColourChecker | NDArrayFloat, ...]:
284+
) -> Tuple[DataDetectionColourChecker, ...] | Tuple[NDArrayFloat, ...]:
238285
"""
239286
Detect the colour checkers swatches in given image using inference.
240287
@@ -375,7 +422,7 @@ def detect_colour_checkers_inference(
375422
if apply_cctf_decoding:
376423
image = cctf_decoding(image)
377424

378-
image = cast(NDArrayInt | NDArrayFloat, image)
425+
image = cast(NDArrayReal, image)
379426

380427
rectangle = as_int32_array(
381428
[
@@ -443,8 +490,8 @@ def detect_colour_checkers_inference(
443490

444491
if additional_data:
445492
return tuple(colour_checkers_data)
446-
else:
447-
return tuple(
448-
colour_checker_data.swatch_colours
449-
for colour_checker_data in colour_checkers_data
450-
)
493+
494+
return tuple(
495+
colour_checker_data.swatch_colours
496+
for colour_checker_data in colour_checkers_data
497+
)

0 commit comments

Comments
 (0)