Skip to content

Post processing

adjust_bbox_coordinates_to_client_scaling_factor(bbox, scaling_factor)

Adjust a bbox coordinates to the client scaling factor.

Parameters:

Name Type Description Default
bbox dict

The bbox to adjust.

required
scaling_factor float

The scaling factor.

required

Returns:

Type Description
dict

The adjusted bbox.

Source code in inference_sdk/http/utils/post_processing.py
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
def adjust_bbox_coordinates_to_client_scaling_factor(
    bbox: dict,
    scaling_factor: float,
) -> dict:
    """Adjust a bbox coordinates to the client scaling factor.

    Args:
        bbox: The bbox to adjust.
        scaling_factor: The scaling factor.

    Returns:
        The adjusted bbox.
    """
    bbox["x"] = bbox["x"] / scaling_factor
    bbox["y"] = bbox["y"] / scaling_factor
    bbox["width"] = bbox["width"] / scaling_factor
    bbox["height"] = bbox["height"] / scaling_factor
    return bbox

adjust_object_detection_predictions_to_client_scaling_factor(predictions, scaling_factor)

Adjust a list of object detection predictions to the client scaling factor.

Parameters:

Name Type Description Default
predictions List[dict]

The list of object detection predictions.

required
scaling_factor float

The scaling factor.

required

Returns:

Type Description
List[dict]

The adjusted list of object detection predictions.

Source code in inference_sdk/http/utils/post_processing.py
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
def adjust_object_detection_predictions_to_client_scaling_factor(
    predictions: List[dict],
    scaling_factor: float,
) -> List[dict]:
    """Adjust a list of object detection predictions to the client scaling factor.

    Args:
        predictions: The list of object detection predictions.
        scaling_factor: The scaling factor.

    Returns:
        The adjusted list of object detection predictions.
    """
    result = []
    for prediction in predictions:
        prediction = adjust_bbox_coordinates_to_client_scaling_factor(
            bbox=prediction,
            scaling_factor=scaling_factor,
        )
        result.append(prediction)
    return result

adjust_points_coordinates_to_client_scaling_factor(points, scaling_factor)

Adjust a list of points coordinates to the client scaling factor.

Parameters:

Name Type Description Default
points List[dict]

The list of points.

required
scaling_factor float

The scaling factor.

required

Returns:

Type Description
List[dict]

The adjusted list of points.

Source code in inference_sdk/http/utils/post_processing.py
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
def adjust_points_coordinates_to_client_scaling_factor(
    points: List[dict],
    scaling_factor: float,
) -> List[dict]:
    """Adjust a list of points coordinates to the client scaling factor.

    Args:
        points: The list of points.
        scaling_factor: The scaling factor.

    Returns:
        The adjusted list of points.
    """
    result = []
    for point in points:
        point["x"] = point["x"] / scaling_factor
        point["y"] = point["y"] / scaling_factor
        result.append(point)
    return result

adjust_prediction_to_client_scaling_factor(prediction, scaling_factor)

Adjust a prediction to the client scaling factor.

Parameters:

Name Type Description Default
prediction dict

The prediction to adjust.

required
scaling_factor Optional[float]

The scaling factor.

required

Returns:

Type Description
dict

The adjusted prediction.

Source code in inference_sdk/http/utils/post_processing.py
204
205
206
207
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
def adjust_prediction_to_client_scaling_factor(
    prediction: dict,
    scaling_factor: Optional[float],
) -> dict:
    """Adjust a prediction to the client scaling factor.

    Args:
        prediction: The prediction to adjust.
        scaling_factor: The scaling factor.

    Returns:
        The adjusted prediction.
    """
    if scaling_factor is None or prediction.get("is_stub", False):
        return prediction
    if "image" in prediction:
        prediction["image"] = {
            "width": round(prediction["image"]["width"] / scaling_factor),
            "height": round(prediction["image"]["height"] / scaling_factor),
        }
    if predictions_should_not_be_post_processed(prediction=prediction):
        return prediction
    if "points" in prediction["predictions"][0]:
        prediction["predictions"] = (
            adjust_prediction_with_bbox_and_points_to_client_scaling_factor(
                predictions=prediction["predictions"],
                scaling_factor=scaling_factor,
                points_key="points",
            )
        )
    elif "keypoints" in prediction["predictions"][0]:
        prediction["predictions"] = (
            adjust_prediction_with_bbox_and_points_to_client_scaling_factor(
                predictions=prediction["predictions"],
                scaling_factor=scaling_factor,
                points_key="keypoints",
            )
        )
    elif "x" in prediction["predictions"][0] and "y" in prediction["predictions"][0]:
        prediction["predictions"] = (
            adjust_object_detection_predictions_to_client_scaling_factor(
                predictions=prediction["predictions"],
                scaling_factor=scaling_factor,
            )
        )
    return prediction

adjust_prediction_with_bbox_and_points_to_client_scaling_factor(predictions, scaling_factor, points_key)

Adjust a list of predictions with bbox and points to the client scaling factor.

Parameters:

Name Type Description Default
predictions List[dict]

The list of predictions.

required
scaling_factor float

The scaling factor.

required
points_key str

The key of the points.

required

Returns:

Type Description
List[dict]

The adjusted list of predictions.

Source code in inference_sdk/http/utils/post_processing.py
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
def adjust_prediction_with_bbox_and_points_to_client_scaling_factor(
    predictions: List[dict],
    scaling_factor: float,
    points_key: str,
) -> List[dict]:
    """Adjust a list of predictions with bbox and points to the client scaling factor.

    Args:
        predictions: The list of predictions.
        scaling_factor: The scaling factor.
        points_key: The key of the points.

    Returns:
        The adjusted list of predictions.
    """
    result = []
    for prediction in predictions:
        prediction = adjust_bbox_coordinates_to_client_scaling_factor(
            bbox=prediction,
            scaling_factor=scaling_factor,
        )
        prediction[points_key] = adjust_points_coordinates_to_client_scaling_factor(
            points=prediction[points_key],
            scaling_factor=scaling_factor,
        )
        result.append(prediction)
    return result

combine_clip_embeddings(embeddings)

Combine clip embeddings.

Parameters:

Name Type Description Default
embeddings Union[dict, List[dict]]

The embeddings to combine.

required

Returns:

Type Description
List[dict]

The combined embeddings.

Source code in inference_sdk/http/utils/post_processing.py
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
def combine_clip_embeddings(embeddings: Union[dict, List[dict]]) -> List[dict]:
    """Combine clip embeddings.

    Args:
        embeddings: The embeddings to combine.

    Returns:
        The combined embeddings.
    """
    if issubclass(type(embeddings), list):
        result = []
        for e in embeddings:
            result.extend(combine_clip_embeddings(embeddings=e))
        return result
    frame_id = embeddings["frame_id"]
    time = embeddings["time"]
    if len(embeddings["embeddings"]) > 1:
        new_embeddings = [
            {"frame_id": frame_id, "time": time, "embeddings": [e]}
            for e in embeddings["embeddings"]
        ]
    else:
        new_embeddings = [embeddings]
    return new_embeddings

combine_gaze_detections(detections)

Combine gaze detections.

Parameters:

Name Type Description Default
detections Union[dict, List[Union[dict, List[dict]]]]

The detections to combine.

required

Returns:

Type Description
Union[dict, List[Dict]]

The combined detections.

Source code in inference_sdk/http/utils/post_processing.py
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
def combine_gaze_detections(
    detections: Union[dict, List[Union[dict, List[dict]]]]
) -> Union[dict, List[Dict]]:
    """Combine gaze detections.

    Args:
        detections: The detections to combine.

    Returns:
        The combined detections.
    """
    if not issubclass(type(detections), list):
        return detections
    detections = [e if issubclass(type(e), list) else [e] for e in detections]
    return list(itertools.chain.from_iterable(detections))

decode_workflow_output(workflow_output, expected_format)

Decode a workflow output.

Parameters:

Name Type Description Default
workflow_output Dict[str, Any]

The workflow output to decode.

required
expected_format VisualisationResponseFormat

The expected format of the workflow output.

required

Returns:

Type Description
Dict[str, Any]

The decoded workflow output.

Source code in inference_sdk/http/utils/post_processing.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def decode_workflow_output(
    workflow_output: Dict[str, Any],
    expected_format: VisualisationResponseFormat,
) -> Dict[str, Any]:
    """Decode a workflow output.

    Args:
        workflow_output: The workflow output to decode.
        expected_format: The expected format of the workflow output.

    Returns:
        The decoded workflow output.
    """
    result = {}
    for key, value in workflow_output.items():
        if is_workflow_image(value=value):
            value = decode_workflow_output_image(
                value=value,
                expected_format=expected_format,
            )
        elif issubclass(type(value), dict):
            value = decode_workflow_output(
                workflow_output=value, expected_format=expected_format
            )
        elif issubclass(type(value), list):
            value = decode_workflow_output_list(
                elements=value,
                expected_format=expected_format,
            )
        result[key] = value
    return result

decode_workflow_output_image(value, expected_format)

Decode a workflow output image.

Parameters:

Name Type Description Default
value Dict[str, Any]

The value to decode.

required
expected_format VisualisationResponseFormat

The expected format of the value.

required

Returns:

Type Description
Union[str, ndarray, Image]

The decoded value.

Source code in inference_sdk/http/utils/post_processing.py
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
def decode_workflow_output_image(
    value: Dict[str, Any],
    expected_format: VisualisationResponseFormat,
) -> Union[str, np.ndarray, Image.Image]:
    """Decode a workflow output image.

    Args:
        value: The value to decode.
        expected_format: The expected format of the value.

    Returns:
        The decoded value.
    """
    if expected_format is VisualisationResponseFormat.BASE64:
        return value["value"]
    return transform_base64_visualisation(
        visualisation=value["value"],
        expected_format=expected_format,
    )

decode_workflow_output_list(elements, expected_format)

Decode a list of workflow outputs.

Parameters:

Name Type Description Default
elements List[Any]

The list of elements to decode.

required
expected_format VisualisationResponseFormat

The expected format of the elements.

required

Returns:

Type Description
List[Any]

The decoded list of elements.

Source code in inference_sdk/http/utils/post_processing.py
 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
104
105
106
107
108
109
def decode_workflow_output_list(
    elements: List[Any],
    expected_format: VisualisationResponseFormat,
) -> List[Any]:
    """Decode a list of workflow outputs.

    Args:
        elements: The list of elements to decode.
        expected_format: The expected format of the elements.

    Returns:
        The decoded list of elements.
    """
    result = []
    for element in elements:
        if is_workflow_image(value=element):
            element = decode_workflow_output_image(
                value=element,
                expected_format=expected_format,
            )
        elif issubclass(type(element), dict):
            element = decode_workflow_output(
                workflow_output=element, expected_format=expected_format
            )
        elif issubclass(type(element), list):
            element = decode_workflow_output_list(
                elements=element,
                expected_format=expected_format,
            )
        result.append(element)
    return result

decode_workflow_outputs(workflow_outputs, expected_format)

Decode a list of workflow outputs.

Parameters:

Name Type Description Default
workflow_outputs List[Dict[str, Any]]

The list of workflow outputs.

required
expected_format VisualisationResponseFormat

The expected format of the workflow outputs.

required

Returns:

Type Description
List[Dict[str, Any]]

The decoded list of workflow outputs.

Source code in inference_sdk/http/utils/post_processing.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def decode_workflow_outputs(
    workflow_outputs: List[Dict[str, Any]],
    expected_format: VisualisationResponseFormat,
) -> List[Dict[str, Any]]:
    """Decode a list of workflow outputs.

    Args:
        workflow_outputs: The list of workflow outputs.
        expected_format: The expected format of the workflow outputs.

    Returns:
        The decoded list of workflow outputs.
    """
    return [
        decode_workflow_output(
            workflow_output=workflow_output,
            expected_format=expected_format,
        )
        for workflow_output in workflow_outputs
    ]

filter_model_descriptions(descriptions, model_id)

Filter model descriptions.

Parameters:

Name Type Description Default
descriptions List[ModelDescription]

The list of model descriptions.

required
model_id str

The model ID.

required

Returns:

Type Description
Optional[ModelDescription]

The filtered model description.

Source code in inference_sdk/http/utils/post_processing.py
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
def filter_model_descriptions(
    descriptions: List[ModelDescription],
    model_id: str,
) -> Optional[ModelDescription]:
    """Filter model descriptions.

    Args:
        descriptions: The list of model descriptions.
        model_id: The model ID.

    Returns:
        The filtered model description.
    """
    matching_models = [d for d in descriptions if d.model_id == model_id]
    if len(matching_models) > 0:
        return matching_models[0]
    return None

is_workflow_image(value)

Check if the value is a workflow image.

Parameters:

Name Type Description Default
value Any

The value to check.

required

Returns:

Type Description
bool

True if the value is a workflow image, False otherwise.

Source code in inference_sdk/http/utils/post_processing.py
112
113
114
115
116
117
118
119
120
121
def is_workflow_image(value: Any) -> bool:
    """Check if the value is a workflow image.

    Args:
        value: The value to check.

    Returns:
        True if the value is a workflow image, False otherwise.
    """
    return issubclass(type(value), dict) and value.get("type") == "base64"

predictions_should_not_be_post_processed(prediction)

Check if the predictions should not be post-processed.

Parameters:

Name Type Description Default
prediction dict

The prediction to check.

required

Returns:

Type Description
bool

True if the predictions should not be post-processed, False otherwise.

Source code in inference_sdk/http/utils/post_processing.py
252
253
254
255
256
257
258
259
260
261
262
263
264
265
def predictions_should_not_be_post_processed(prediction: dict) -> bool:
    """Check if the predictions should not be post-processed.

    Args:
        prediction: The prediction to check.

    Returns:
        True if the predictions should not be post-processed, False otherwise.
    """
    return (
        "predictions" not in prediction
        or not issubclass(type(prediction["predictions"]), list)
        or len(prediction["predictions"]) == 0
    )

response_contains_jpeg_image(response)

Check if the response contains a JPEG image.

Parameters:

Name Type Description Default
response Response

The response to check.

required

Returns:

Type Description
bool

True if the response contains a JPEG image, False otherwise.

Source code in inference_sdk/http/utils/post_processing.py
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
def response_contains_jpeg_image(response: Response) -> bool:
    """Check if the response contains a JPEG image.

    Args:
        response: The response to check.

    Returns:
        True if the response contains a JPEG image, False otherwise.
    """
    content_type = None
    for header_name in CONTENT_TYPE_HEADERS:
        if header_name in response.headers:
            content_type = response.headers[header_name]
            break
    if content_type is None:
        return False
    return "image/jpeg" in content_type

transform_base64_visualisation(visualisation, expected_format)

Transform a base64 visualisation.

Parameters:

Name Type Description Default
visualisation str

The visualisation to transform.

required
expected_format VisualisationResponseFormat

The expected format of the visualisation.

required

Returns:

Type Description
Union[str, ndarray, Image]

The transformed visualisation.

Source code in inference_sdk/http/utils/post_processing.py
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
def transform_base64_visualisation(
    visualisation: str,
    expected_format: VisualisationResponseFormat,
) -> Union[str, np.ndarray, Image.Image]:
    """Transform a base64 visualisation.

    Args:
        visualisation: The visualisation to transform.
        expected_format: The expected format of the visualisation.

    Returns:
        The transformed visualisation.
    """
    visualisation_bytes = base64.b64decode(visualisation)
    return transform_visualisation_bytes(
        visualisation=visualisation_bytes, expected_format=expected_format
    )

transform_visualisation_bytes(visualisation, expected_format)

Transform a visualisation bytes.

Parameters:

Name Type Description Default
visualisation bytes

The visualisation to transform.

required
expected_format VisualisationResponseFormat

The expected format of the visualisation.

required

Returns:

Type Description
Union[str, ndarray, Image]

The transformed visualisation.

Source code in inference_sdk/http/utils/post_processing.py
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
def transform_visualisation_bytes(
    visualisation: bytes,
    expected_format: VisualisationResponseFormat,
) -> Union[str, np.ndarray, Image.Image]:
    """Transform a visualisation bytes.

    Args:
        visualisation: The visualisation to transform.
        expected_format: The expected format of the visualisation.

    Returns:
        The transformed visualisation.
    """
    if expected_format not in IMAGES_TRANSCODING_METHODS:
        raise NotImplementedError(
            f"Expected format: {expected_format} is not supported in terms of visualisations transcoding."
        )
    transcoding_method = IMAGES_TRANSCODING_METHODS[expected_format]
    return transcoding_method(visualisation)