コンテンツにスキップ

correct_rotation

Return the input image in the coordinate frame of an analysis result.

When rotation correction (enable_preprocess=True) is applied, all coordinates in the result refer to the orientation-corrected image. Pass the original input image together with its result to get the image that matches those coordinates, e.g. before create_searchable_pdf() or figure export. If no rotation was applied, the image is returned as is.

Parameters:

Name Type Description Default
img ndarray

The original input image passed to the analyzer.

required
result

An analysis result with a preprocess field (DocumentAnalyzerSchema, OCRSchema, TableSemanticParserSchema).

required

Returns:

Type Description

np.ndarray: The image whose orientation matches the result coordinates.

Source code in src/yomitoku/utils/rotation.py
def correct_rotation(img, result):
    """
    Return the input image in the coordinate frame of an analysis result.

    When rotation correction (``enable_preprocess=True``) is applied, all
    coordinates in the result refer to the orientation-corrected image. Pass
    the original input image together with its result to get the image that
    matches those coordinates, e.g. before ``create_searchable_pdf()`` or
    figure export. If no rotation was applied, the image is returned as is.

    Args:
        img (np.ndarray): The original input image passed to the analyzer.
        result: An analysis result with a ``preprocess`` field
            (DocumentAnalyzerSchema, OCRSchema, TableSemanticParserSchema).

    Returns:
        np.ndarray: The image whose orientation matches the result coordinates.
    """
    preprocess = getattr(result, "preprocess", None)
    if preprocess is None:
        return img
    return rotate_by_angle(img, preprocess.angle)