Hide navigation sidebar
Hide table of contents sidebar
Skip to content
Back to top
Toggle table of contents sidebar

# Document detection

The DocumentDetector class detects documents in images.

Important

The document detector requires the relevant AI models to run (see AI Models for details). It also requires the document templates to be loaded (see Document database for details).

## Automatic document identification

The document type can be determined automatically by analysing the document image on the basis of pre-loaded document templates.

## Example

The example below demonstrates how to detect a document from an image.

Python

    # initialize the document detector
    detector = id3document.DocumentDetector(
        model = id3document.DocumentModel.DOCUMENT_DETECTOR_2A,
        document_width_ratio = 1,
        thread_count = 4
    )

    # load an image from a file.
    image = id3document.DocumentImage.from_file(document_filename, id3document.PixelFormat.BGR_24_BITS)

    # detect the document
    tic = time.perf_counter()
    document = detector.detect_document(image, id3document.Rectangle())
    toc = time.perf_counter()
    print(f"Document detected in {1000*(toc-tic):0.1f} ms")
    print(f"Confidence score: {document.confidence}")

    if document.confidence >= detector.confidence_threshold:
        # show document information
        info = document.info
        print(f"Document information:")
        print(f"- Identifier  : {info.identifier}")
        print(f"- Name        : {info.name}")
        print(f"- Description : {info.description}")
        print(f"- Category    : {info.category.name}")
        print(f"- Country     : {info.country}")
        print(f"- Format      : {info.format.name}")
        print(f"- Date        : {info.date}")
        print(f"- MRZ type    : {info.mrz_type.name}")
        print(f"- Page number : {info.page_number}")

        image_crop = detector.align_document(image, document)
        image_crop.to_file("data/crop.png", 0)

        print(f"Document boundaries:")
        for point in document.bounds:
            print(f"- ({point.x}, {point.y})")

## See also

- AI Models

- Document database

- DocumentDetector Class

- DetectedDocument Class