diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 035effd..01095aa 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -54,7 +54,7 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install . + pip install ".[tests]" - name: Run mypy run: |- mypy --install-types --non-interactive qreader.py diff --git a/qreader.py b/qreader.py index bc43c58..8fbba5e 100644 --- a/qreader.py +++ b/qreader.py @@ -41,21 +41,24 @@ ("shift-jis", "big5") if os.name == "nt" else ("big5", "shift-jis") ) +CorrectionsType = typing.Literal["cropped_bbox", "corrected_perspective"] +FlavorType = typing.Literal["original", "inverted", "grayscale"] + @dataclass(frozen=True) class DecodeQRResult: scale_factor: float - corrections: typing.Literal["cropped_bbox", "corrected_perspective"] - flavor: typing.Literal["original", "inverted", "grayscale"] - blur_kernel_sizes: tuple[tuple[int, int]] | None + corrections: CorrectionsType + flavor: FlavorType + blur_kernel_sizes: tuple[tuple[int, int], ...] | None image: np.ndarray result: Decoded def wrap( scale_factor: float, - corrections: typing.Literal["cropped_bbox", "corrected_perspective"], - flavor: typing.Literal["original", "inverted", "grayscale"], + corrections: CorrectionsType, + flavor: FlavorType, blur_kernel_sizes: tuple[tuple[int, int], ...] | None, image: np.ndarray, results: typing.List[Decoded], @@ -79,7 +82,7 @@ def __init__( self, model_size: str = "s", min_confidence: float = 0.5, - reencode_to: str | tuple[str] | list[str] | None = DEFAULT_REENCODINGS, + reencode_to: str | tuple[str, ...] | list[str] | None = DEFAULT_REENCODINGS, weights_folder: str | None = None, ): """ @@ -225,11 +228,7 @@ def detect_and_decode( def get_detection_result_from_polygon( self, - quadrilateral_xy: ( - np.ndarray - | tuple[tuple[float | int, float | int], ...] - | list[list[float | int, float | int]] - ), + quadrilateral_xy: np.ndarray | typing.Sequence[typing.Union[float, int]], ) -> dict[str, np.ndarray | float | tuple[float | int, float | int]]: """ This method will simulate a detection result from the given quadrilateral. This is useful when you have detected @@ -299,11 +298,13 @@ def _decode_qr_zbar( image=cropped_quad, padded_quad_xy=updated_detection[PADDED_QUAD_XY] ) + corrections = { + "cropped_bbox": cropped_bbox, + "corrected_perspective": corrected_perspective, + } + for scale_factor in (1, 0.5, 2, 0.25, 3, 4): - for label, image in { - "cropped_bbox": cropped_bbox, - "corrected_perspective": corrected_perspective, - }.items(): + for label, image in corrections.items(): # If rescaled_image will be larger than 1024px, skip it # TODO: Decide a minimum size for the QRs based on the resize benchmark if ( @@ -323,7 +324,7 @@ def _decode_qr_zbar( if len(decodedQR) > 0: return wrap( scale_factor=scale_factor, - corrections=label, + corrections=typing.cast(CorrectionsType, label), flavor="original", blur_kernel_sizes=None, image=rescaled_image, @@ -335,7 +336,7 @@ def _decode_qr_zbar( if len(decodedQR) > 0: return wrap( scale_factor=scale_factor, - corrections=label, + corrections=typing.cast(CorrectionsType, label), flavor="inverted", blur_kernel_sizes=None, image=inverted_image, @@ -356,7 +357,7 @@ def _decode_qr_zbar( if len(decodedQR) > 0: return wrap( scale_factor=scale_factor, - corrections=label, + corrections=typing.cast(CorrectionsType, label), flavor="grayscale", blur_kernel_sizes=((5, 5), (7, 7)), image=gray, @@ -381,7 +382,7 @@ def _decode_qr_zbar( if len(decodedQR) > 0: return wrap( scale_factor=scale_factor, - corrections=label, + corrections=typing.cast(CorrectionsType, label), flavor="grayscale", blur_kernel_sizes=((3, 3),), image=sharpened_gray, @@ -437,7 +438,9 @@ def __correct_perspective( return dst_img def __threshold_and_blur_decodings( - self, image: np.ndarray, blur_kernel_sizes: tuple[tuple[int, int]] = ((3, 3),) + self, + image: np.ndarray, + blur_kernel_sizes: tuple[tuple[int, int], ...] = ((3, 3),), ) -> list[Decoded]: """ Try to decode the QR code just with pyzbar, pre-processing the image with different blur and threshold diff --git a/setup.cfg b/setup.cfg index fe869c8..b042752 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,3 +1,9 @@ [metadata] description_file=README.md -license_files=LICENSE \ No newline at end of file +license_files=LICENSE + +[mypy] +[mypy-pyzbar.*] +ignore_missing_imports = True +[mypy-qrdet.*] +ignore_missing_imports = True diff --git a/setup.py b/setup.py index 75785f8..be98f3b 100644 --- a/setup.py +++ b/setup.py @@ -20,6 +20,9 @@ "pyzbar", "qrdet>=2.5", ], + extras_require={ + "tests": ["mypy"], + }, classifiers=[ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers",