Skip to content

Commit

Permalink
Merge pull request #34 from jandom/jandom/fix/mypy-typechecking
Browse files Browse the repository at this point in the history
mypy: further fixes, now tests passing
  • Loading branch information
Eric-Canas authored Jul 29, 2024
2 parents 3ecc8a6 + cf081bf commit b5507e4
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions qreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@
("shift-jis", "big5") if os.name == "nt" else ("big5", "shift-jis")
)

from numpy.typing import NDArray

CorrectionsType = typing.Literal["cropped_bbox", "corrected_perspective"]
FlavorType = typing.Literal["original", "inverted", "grayscale"]
ReencodeToType = typing.Union[str, typing.Sequence[str], None]


@dataclass(frozen=True)
Expand Down Expand Up @@ -82,7 +85,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: ReencodeToType = DEFAULT_REENCODINGS,
weights_folder: str | None = None,
):
"""
Expand Down Expand Up @@ -119,7 +122,7 @@ def __init__(
assert isinstance(
reencode_to, (tuple, list)
), f"reencode_to must be a str, tuple, list or None. Got {type(reencode_to)}"
self.reencode_to = reencode_to
self.reencode_to = tuple(reencode_to)

def detect(
self, image: np.ndarray, is_bgr: bool = False
Expand Down Expand Up @@ -193,12 +196,10 @@ def decode(

def detect_and_decode(
self, image: np.ndarray, return_detections: bool = False, is_bgr: bool = False
) -> (
tuple[
dict[str, np.ndarray | float | tuple[float | int, float | int]], str | None
]
| tuple[str | None, ...]
):
) -> typing.Tuple[
typing.Tuple[str | None, ...],
tuple[dict[str, np.ndarray | float | tuple[float | int, float | int]]] | None,
]:
"""
This method will decode the **QR** codes in the given image and return the decoded strings
(or None, if any of them was detected but not decoded).
Expand All @@ -224,7 +225,7 @@ def detect_and_decode(
if return_detections:
return decoded_qrs, detections
else:
return decoded_qrs
return decoded_qrs, None

def get_detection_result_from_polygon(
self,
Expand Down Expand Up @@ -331,7 +332,7 @@ def _decode_qr_zbar(
results=decodedQR,
)
# For QRs with black background and white foreground, try to invert the image
inverted_image = image = 255 - rescaled_image
inverted_image = np.array(255) - rescaled_image
decodedQR = decodeQR(inverted_image, symbols=[ZBarSymbol.QRCODE])
if len(decodedQR) > 0:
return wrap(
Expand Down

0 comments on commit b5507e4

Please sign in to comment.