Skip to content

Loaders

load_directory_inference_input(directory_path, image_extensions)

Load an inference input from a directory.

Parameters:

Name Type Description Default
directory_path str

The path to the directory.

required
image_extensions Optional[List[str]]

The extensions of the images.

required

Returns:

Type Description
None

The generator of the inference input.

Source code in inference_sdk/http/utils/loaders.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def load_directory_inference_input(
    directory_path: str,
    image_extensions: Optional[List[str]],
) -> Generator[Tuple[Union[str, int], np.ndarray], None, None]:
    """Load an inference input from a directory.

    Args:
        directory_path: The path to the directory.
        image_extensions: The extensions of the images.

    Returns:
        The generator of the inference input.
    """
    paths = {
        path.as_posix().lower()
        for path in sv.list_files_with_extensions(
            directory=directory_path,
            extensions=image_extensions,
        )
    }
    # making a set due to case-insensitive behaviour of Windows
    # see: https://stackoverflow.com/questions/7199039/file-paths-in-windows-environment-not-case-sensitive
    for path in paths:
        yield path, cv2.imread(path)

load_image_from_string(reference, max_height=None, max_width=None)

Load an image from a string.

Parameters:

Name Type Description Default
reference str

The reference to the image.

required
max_height Optional[int]

The maximum height of the image.

None
max_width Optional[int]

The maximum width of the image.

None

Returns:

Type Description
Tuple[str, Optional[float]]

The image and the scaling factor.

Source code in inference_sdk/http/utils/loaders.py
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
def load_image_from_string(
    reference: str,
    max_height: Optional[int] = None,
    max_width: Optional[int] = None,
) -> Tuple[str, Optional[float]]:
    """Load an image from a string.

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

    Returns:
        The image and the scaling factor.
    """
    if uri_is_http_link(uri=reference):
        return load_image_from_url(
            url=reference, max_height=max_height, max_width=max_width
        )
    if os.path.exists(reference):
        if max_height is None or max_width is None:
            with open(reference, "rb") as f:
                img_bytes = f.read()
            img_base64_str = encode_base_64(payload=img_bytes)
            return img_base64_str, None
        local_image = cv2.imread(reference)
        if local_image is None:
            raise EncodingError(f"Could not load image from {reference}")
        local_image, scaling_factor = resize_opencv_image(
            image=local_image,
            max_height=max_height,
            max_width=max_width,
        )
        return numpy_array_to_base64_jpeg(image=local_image), scaling_factor
    if max_height is not None and max_width is not None:
        image_bytes = base64.b64decode(reference)
        image = bytes_to_opencv_image(payload=image_bytes)
        image, scaling_factor = resize_opencv_image(
            image=image,
            max_height=max_height,
            max_width=max_width,
        )
        return numpy_array_to_base64_jpeg(image=image), scaling_factor
    return reference, None

load_image_from_string_async(reference, max_height=None, max_width=None) async

Load an image from a string asynchronously.

Parameters:

Name Type Description Default
reference str

The reference to the image.

required
max_height Optional[int]

The maximum height of the image.

None
max_width Optional[int]

The maximum width of the image.

None

Returns:

Type Description
Tuple[str, Optional[float]]

The image and the scaling factor.

Source code in inference_sdk/http/utils/loaders.py
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
async def load_image_from_string_async(
    reference: str,
    max_height: Optional[int] = None,
    max_width: Optional[int] = None,
) -> Tuple[str, Optional[float]]:
    """Load an image from a string asynchronously.

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

    Returns:
        The image and the scaling factor.
    """
    if uri_is_http_link(uri=reference):
        return await load_image_from_url_async(
            url=reference, max_height=max_height, max_width=max_width
        )
    if os.path.exists(reference):
        local_image = cv2.imread(reference)
        if local_image is None:
            raise EncodingError(f"Could not load image from {reference}")
        local_image, scaling_factor = resize_opencv_image(
            image=local_image,
            max_height=max_height,
            max_width=max_width,
        )
        return numpy_array_to_base64_jpeg(image=local_image), scaling_factor
    if max_height is not None and max_width is not None:
        image_bytes = base64.b64decode(reference)
        image = bytes_to_opencv_image(payload=image_bytes)
        image, scaling_factor = resize_opencv_image(
            image=image,
            max_height=max_height,
            max_width=max_width,
        )
        return numpy_array_to_base64_jpeg(image=image), scaling_factor
    return reference, None

load_image_from_url(url, max_height=None, max_width=None)

Load an image from a URL.

Parameters:

Name Type Description Default
url str

The URL of the image.

required
max_height Optional[int]

The maximum height of the image.

None
max_width Optional[int]

The maximum width of the image.

None

Returns:

Type Description
Tuple[str, Optional[float]]

The image and the scaling factor.

Source code in inference_sdk/http/utils/loaders.py
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
def load_image_from_url(
    url: str,
    max_height: Optional[int] = None,
    max_width: Optional[int] = None,
) -> Tuple[str, Optional[float]]:
    """Load an image from a URL.

    Args:
        url: The URL of the image.
        max_height: The maximum height of the image.
        max_width: The maximum width of the image.

    Returns:
        The image and the scaling factor.
    """
    response = requests.get(url)
    response.raise_for_status()
    if max_height is None or max_width is None:
        return encode_base_64(response.content), None
    image = bytes_to_opencv_image(payload=response.content)
    resized_image, scaling_factor = resize_opencv_image(
        image=image,
        max_height=max_height,
        max_width=max_width,
    )
    serialised_image = numpy_array_to_base64_jpeg(image=resized_image)
    return serialised_image, scaling_factor

load_image_from_url_async(url, max_height=None, max_width=None) async

Load an image from a URL asynchronously.

Parameters:

Name Type Description Default
url str

The URL of the image.

required
max_height Optional[int]

The maximum height of the image.

None
max_width Optional[int]

The maximum width of the image.

None

Returns:

Type Description
Tuple[str, Optional[float]]

The image and the scaling factor.

Source code in inference_sdk/http/utils/loaders.py
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
async def load_image_from_url_async(
    url: str,
    max_height: Optional[int] = None,
    max_width: Optional[int] = None,
) -> Tuple[str, Optional[float]]:
    """Load an image from a URL asynchronously.

    Args:
        url: The URL of the image.
        max_height: The maximum height of the image.
        max_width: The maximum width of the image.

    Returns:
        The image and the scaling factor.
    """
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            response.raise_for_status()
            response_payload = await response.read()
    if max_height is None or max_width is None:
        return encode_base_64(response_payload), None
    image = bytes_to_opencv_image(payload=response_payload)
    resized_image, scaling_factor = resize_opencv_image(
        image=image,
        max_height=max_height,
        max_width=max_width,
    )
    serialised_image = numpy_array_to_base64_jpeg(image=resized_image)
    return serialised_image, scaling_factor

load_nested_batches_of_inference_input(inference_input, max_height=None, max_width=None)

Load a nested batch of inference input.

Parameters:

Name Type Description Default
inference_input Union[list, ImagesReference]

The inference input.

required
max_height Optional[int]

The maximum height of the image.

None
max_width Optional[int]

The maximum width of the image.

None

Returns:

Type Description
Union[Tuple[str, Optional[float]], list]

The nested batch of inference input.

Source code in inference_sdk/http/utils/loaders.py
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
def load_nested_batches_of_inference_input(
    inference_input: Union[list, ImagesReference],
    max_height: Optional[int] = None,
    max_width: Optional[int] = None,
) -> Union[Tuple[str, Optional[float]], list]:
    """Load a nested batch of inference input.

    Args:
        inference_input: The inference input.
        max_height: The maximum height of the image.
        max_width: The maximum width of the image.

    Returns:
        The nested batch of inference input.
    """
    if not isinstance(inference_input, list):
        return load_static_inference_input(
            inference_input=inference_input,
            max_height=max_height,
            max_width=max_width,
        )[0]
    result = []
    for element in inference_input:
        result.append(
            load_nested_batches_of_inference_input(
                inference_input=element,
                max_height=max_height,
                max_width=max_width,
            )
        )
    return result

load_static_inference_input(inference_input, max_height=None, max_width=None)

Load a static inference input.

Parameters:

Name Type Description Default
inference_input Union[ImagesReference, List[ImagesReference]]

The inference input.

required
max_height Optional[int]

The maximum height of the image.

None
max_width Optional[int]

The maximum width of the image.

None

Returns:

Type Description
List[Tuple[str, Optional[float]]]

The list of the inference input.

Source code in inference_sdk/http/utils/loaders.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
def load_static_inference_input(
    inference_input: Union[ImagesReference, List[ImagesReference]],
    max_height: Optional[int] = None,
    max_width: Optional[int] = None,
) -> List[Tuple[str, Optional[float]]]:
    """Load a static inference input.

    Args:
        inference_input: The inference input.
        max_height: The maximum height of the image.
        max_width: The maximum width of the image.

    Returns:
        The list of the inference input.
    """
    if issubclass(type(inference_input), list):
        results = []
        for element in inference_input:
            results.extend(
                load_static_inference_input(
                    inference_input=element,
                    max_height=max_height,
                    max_width=max_width,
                )
            )
        return results
    if issubclass(type(inference_input), str):
        return [
            load_image_from_string(
                reference=inference_input, max_height=max_height, max_width=max_width
            )
        ]
    if issubclass(type(inference_input), np.ndarray):
        image, scaling_factor = resize_opencv_image(
            image=inference_input,
            max_height=max_height,
            max_width=max_width,
        )
        return [(numpy_array_to_base64_jpeg(image=image), scaling_factor)]
    if issubclass(type(inference_input), Image.Image):
        image, scaling_factor = resize_pillow_image(
            image=inference_input,
            max_height=max_height,
            max_width=max_width,
        )
        return [(pillow_image_to_base64_jpeg(image=image), scaling_factor)]
    raise InvalidInputFormatError(
        f"Unknown type of input ({inference_input.__class__.__name__}) submitted."
    )

load_static_inference_input_async(inference_input, max_height=None, max_width=None) async

Load a static inference input asynchronously.

Parameters:

Name Type Description Default
inference_input Union[ImagesReference, List[ImagesReference]]

The inference input.

required
max_height Optional[int]

The maximum height of the image.

None
max_width Optional[int]

The maximum width of the image.

None

Returns:

Type Description
List[Tuple[str, Optional[float]]]

The list of the inference input.

Source code in inference_sdk/http/utils/loaders.py
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
async def load_static_inference_input_async(
    inference_input: Union[ImagesReference, List[ImagesReference]],
    max_height: Optional[int] = None,
    max_width: Optional[int] = None,
) -> List[Tuple[str, Optional[float]]]:
    """Load a static inference input asynchronously.

    Args:
        inference_input: The inference input.
        max_height: The maximum height of the image.
        max_width: The maximum width of the image.

    Returns:
        The list of the inference input.
    """
    if issubclass(type(inference_input), list):
        results = []
        for element in inference_input:
            results.extend(
                await load_static_inference_input_async(
                    inference_input=element,
                    max_height=max_height,
                    max_width=max_width,
                )
            )
        return results
    if issubclass(type(inference_input), str):
        return [
            await load_image_from_string_async(
                reference=inference_input, max_height=max_height, max_width=max_width
            )
        ]
    if issubclass(type(inference_input), np.ndarray):
        image, scaling_factor = resize_opencv_image(
            image=inference_input,
            max_height=max_height,
            max_width=max_width,
        )
        return [(numpy_array_to_base64_jpeg(image=image), scaling_factor)]
    if issubclass(type(inference_input), Image.Image):
        image, scaling_factor = resize_pillow_image(
            image=inference_input,
            max_height=max_height,
            max_width=max_width,
        )
        return [(pillow_image_to_base64_jpeg(image=image), scaling_factor)]
    raise InvalidInputFormatError(
        f"Unknown type of input ({inference_input.__class__.__name__}) submitted."
    )

load_stream_inference_input(input_uri, image_extensions)

Load an inference input from a stream.

Parameters:

Name Type Description Default
input_uri str

The URI of the input.

required
image_extensions Optional[List[str]]

The extensions of the images.

required

Returns:

Type Description
None

The generator of the inference input.

Source code in inference_sdk/http/utils/loaders.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def load_stream_inference_input(
    input_uri: str,
    image_extensions: Optional[List[str]],
) -> Generator[Tuple[Union[str, int], np.ndarray], None, None]:
    """Load an inference input from a stream.

    Args:
        input_uri: The URI of the input.
        image_extensions: The extensions of the images.

    Returns:
        The generator of the inference input.
    """
    if os.path.isdir(input_uri):
        yield from load_directory_inference_input(
            directory_path=input_uri, image_extensions=image_extensions
        )
    else:
        yield from enumerate(sv.get_video_frames_generator(source_path=input_uri))

Check if the URI is an HTTP link.

Parameters:

Name Type Description Default
uri str

The URI to check.

required

Returns:

Type Description
bool

True if the URI is an HTTP link, False otherwise.

Source code in inference_sdk/http/utils/loaders.py
355
356
357
358
359
360
361
362
363
364
def uri_is_http_link(uri: str) -> bool:
    """Check if the URI is an HTTP link.

    Args:
        uri: The URI to check.

    Returns:
        True if the URI is an HTTP link, False otherwise.
    """
    return uri.startswith("http://") or uri.startswith("https://")