QR, Bar Codes

Note

It’s required to install codes from Optional dependencies section.

Extractor & Decoder

class aiviro.modules.codes.CodesExtractor(pdf_data: PDFRobot | PDFConvertor | List[ndarray] | None = None)

Codes extractor detects and extracts QR and Bar codes from PDFs and images. For list of supported codes, see https://pypi.org/project/pyzbar/ package documentation.

Parameters:

pdf_data – Source of the image data for processing

Example:

>>> from aiviro.modules.pdf import create_pdf_robot
>>> from aiviro.modules.codes import CodesExtractor
>>> r = create_pdf_robot("path/to/file.pdf")
>>> ce = CodesExtractor(r)
>>> qr_codes = ce.qr_codes(page=1)
>>> bar_codes = ce.bar_codes(page=0)
>>> from aiviro.modules.codes import ZBarSymbol
>>> r = create_pdf_robot("path/to/file.pdf")
>>> ce = CodesExtractor(r)
>>> custom_codes = ce.any_codes(code_types=[ZBarSymbol.PDF417])
qr_codes(page: int = 0) List[CodeData]

Extracts QR codes.

Parameters:

page – Index of the page to process

bar_codes(page: int = 0) List[CodeData]

Extracts Bar codes.

Parameters:

page – Index of the page to process

any_codes(page: int, code_types: List[ZBarSymbol]) List[CodeData]

Extracts any of the supported codes.

Parameters:
  • page – Index of the page to process

  • code_types – Types of codes to extract

static any_codes_img(image: ndarray, code_types: List[ZBarSymbol]) List[CodeData]

Extracts any of the supported codes from input image.

Parameters:
  • image – Image containing codes

  • code_types – Types of codes to extract

class aiviro.modules.codes.BaseCodeDecoder

Base class to inherit from for creating custom code decoder.

abstract decode(raw_text: CodeData, ignore_exception: bool = False) T

Main decoding logic

Parameters:
  • raw_text – Raw text data

  • ignore_exception – Ignore QR codes which cannot be decoded

Czech Invoice Format

class aiviro.modules.codes.CzechQRInvoiceDecoder

Decodes QR data based on https://qr-faktura.cz/ format definition. See CzechQRInvoiceData for further information.

Example:

>>> from aiviro.modules.pdf import create_pdf_robot
>>> from aiviro.modules.codes import CodesExtractor, CzechQRInvoiceDecoder
>>> r = create_pdf_robot("path/to/file.pdf")
>>> qr_data = CodesExtractor(r).qr_codes()
>>> if qr_data:
>>>     decoded_data = CzechQRInvoiceDecoder().decode(qr_data[0])
>>>     invoice_id = decoded_data.get_value("ID")
class aiviro.modules.codes.CzechQRInvoiceData(_dict_values: ~typing.Dict[str, str] = <factory>)

Data class containing decoded data from QR code.

get(key: str) str | None

Returns value based on specified key, or None if key doesn’t exist

get_value(key: str) str

Returns value based on the specified key.

property currency: str

Returns type of currency, based on ‘CC’ key.

property total_amount: Decimal

Returns total_amount, based on ‘AM’ key.

get_decimal(key: str = 'AM') Decimal

Returns value in Decimal format based on input key.

get_date(key: str = 'DD') date

Returns value in date format based on input key.