Skip to content

Pre processing

determine_scaling_aspect_ratio(image_height, image_width, max_height, max_width)

Determine the scaling aspect ratio.

Parameters:

Name Type Description Default
image_height int

The height of the image.

required
image_width int

The width of the image.

required
max_height int

The maximum height of the image.

required
max_width int

The maximum width of the image.

required

Returns:

Type Description
Optional[float]

The scaling aspect ratio.

Source code in inference_sdk/http/utils/pre_processing.py
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def determine_scaling_aspect_ratio(
    image_height: int,
    image_width: int,
    max_height: int,
    max_width: int,
) -> Optional[float]:
    """Determine the scaling aspect ratio.

    Args:
        image_height: The height of the image.
        image_width: The width of the image.
        max_height: The maximum height of the image.
        max_width: The maximum width of the image.

    Returns:
        The scaling aspect ratio.
    """
    height_scaling_ratio = max_height / image_height
    width_scaling_ratio = max_width / image_width
    min_scaling_ratio = min(height_scaling_ratio, width_scaling_ratio)
    return min_scaling_ratio if min_scaling_ratio < 1.0 else None

resize_opencv_image(image, max_height, max_width)

Resize an OpenCV image.

Parameters:

Name Type Description Default
image ndarray

The image to resize.

required
max_height Optional[int]

The maximum height of the image.

required
max_width Optional[int]

The maximum width of the image.

required

Returns:

Type Description
Tuple[ndarray, Optional[float]]

The resized image and the scaling factor.

Source code in inference_sdk/http/utils/pre_processing.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def resize_opencv_image(
    image: np.ndarray,
    max_height: Optional[int],
    max_width: Optional[int],
) -> Tuple[np.ndarray, Optional[float]]:
    """Resize an OpenCV image.

    Args:
        image: The image to resize.
        max_height: The maximum height of the image.
        max_width: The maximum width of the image.

    Returns:
        The resized image and the scaling factor.
    """
    if max_width is None or max_height is None:
        return image, None
    height, width = image.shape[:2]
    scaling_ratio = determine_scaling_aspect_ratio(
        image_height=height,
        image_width=width,
        max_height=max_height,
        max_width=max_width,
    )
    if scaling_ratio is None:
        return image, None
    resized_image = cv2.resize(
        src=image, dsize=None, fx=scaling_ratio, fy=scaling_ratio
    )
    return resized_image, scaling_ratio

resize_pillow_image(image, max_height, max_width)

Resize a Pillow image.

Parameters:

Name Type Description Default
image Image

The image to resize.

required
max_height Optional[int]

The maximum height of the image.

required
max_width Optional[int]

The maximum width of the image.

required

Returns:

Type Description
Tuple[Image, Optional[float]]

The resized image and the scaling factor.

Source code in inference_sdk/http/utils/pre_processing.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
def resize_pillow_image(
    image: Image.Image,
    max_height: Optional[int],
    max_width: Optional[int],
) -> Tuple[Image.Image, Optional[float]]:
    """Resize a Pillow image.

    Args:
        image: The image to resize.
        max_height: The maximum height of the image.
        max_width: The maximum width of the image.

    Returns:
        The resized image and the scaling factor.
    """
    if max_width is None or max_height is None:
        return image, None
    width, height = image.size
    scaling_ratio = determine_scaling_aspect_ratio(
        image_height=height,
        image_width=width,
        max_height=max_height,
        max_width=max_width,
    )
    if scaling_ratio is None:
        return image, None
    new_width = round(scaling_ratio * width)
    new_height = round(scaling_ratio * height)
    return image.resize(size=(new_width, new_height)), scaling_ratio