Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix mypy typechecks #32

Merged
merged 2 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
43 changes: 23 additions & 20 deletions qreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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,
):
"""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 (
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
[metadata]
description_file=README.md
license_files=LICENSE
license_files=LICENSE

[mypy]
[mypy-pyzbar.*]
ignore_missing_imports = True
[mypy-qrdet.*]
ignore_missing_imports = True
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
"pyzbar",
"qrdet>=2.5",
],
extras_require={
"tests": ["mypy"],
},
classifiers=[
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
Expand Down
Loading