Skip to content

Commit

Permalink
Merge branch 'Eric-Canas:main' into zbar-poly-return
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkillman10 authored Jul 22, 2024
2 parents d9f2da5 + 3ecc8a6 commit 153c2a1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
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,13 +41,16 @@
("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

Expand All @@ -58,8 +61,8 @@ class DecodeQRResult:

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 Down Expand Up @@ -91,7 +94,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 @@ -269,11 +272,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 @@ -343,11 +342,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 @@ -368,7 +369,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 @@ -384,7 +385,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 @@ -408,7 +409,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 Down Expand Up @@ -436,7 +437,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 @@ -501,7 +502,9 @@ def __get_dst_pts(self, corrected_perspective: np.ndarray) -> np.ndarray:
)

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

0 comments on commit 153c2a1

Please sign in to comment.