# fiftyone.utils.patches¶

Image patch utilities.

Classes:

 ImagePatchesExtractor(samples, patches_field) Class for iterating over the labeled/unlabeled image patches in a collection.

Functions:

 extract_patch(img, detection[, …]) Extracts the patch from the image. parse_patches(doc, patches_field[, …]) Parses the patches from the given document.
class fiftyone.utils.patches.ImagePatchesExtractor(samples, patches_field, include_labels=False, force_rgb=False, force_square=False, alpha=None)

Bases: object

Class for iterating over the labeled/unlabeled image patches in a collection.

By default, this class emits only the image patches, but you can set include_labels to True to emit (img_patch, label) tuples.

Parameters
• patches_field – the name of the field defining the image patches in each sample to extract. Must be of type fiftyone.core.labels.Detection, fiftyone.core.labels.Detections, fiftyone.core.labels.Polyline, or fiftyone.core.labels.Polylines

• include_labels (False) – whether to emit (img_patch, label) tuples rather than just image patches

• force_rgb (False) – whether to force convert the images to RGB

• force_square (False) – whether to minimally manipulate the patch bounding boxes into squares prior to extraction

• alpha (None) – an optional expansion/contraction to apply to the patches before extracting them, in [-1, inf). If provided, the length and width of the box are expanded (or contracted, when alpha < 0) by (100 * alpha)%. For example, set alpha = 1.1 to expand the boxes by 10%, and set alpha = 0.9 to contract the boxes by 10%

fiftyone.utils.patches.parse_patches(doc, patches_field, handle_missing='skip')

Parses the patches from the given document.

Parameters
Returns

a fiftyone.core.labels.Detections instance, or None

fiftyone.utils.patches.extract_patch(img, detection, force_square=False, alpha=None)

Extracts the patch from the image.

Parameters
• img – a numpy image array

• detection – a fiftyone.core.labels.Detection defining the patch

• force_square (False) – whether to minimally manipulate the patch bounding box into a square prior to extraction

• alpha (None) – an optional expansion/contraction to apply to the patch before extracting it, in [-1, inf). If provided, the length and width of the box are expanded (or contracted, when alpha < 0) by (100 * alpha)%. For example, set alpha = 1.1 to expand the box by 10%, and set alpha = 0.9 to contract the box by 10%

Returns

the image patch